add E006: check for lines longer than 79 columns
This mimics the widely accepted convention from PEP8 and many other places that lines longer than 79 columns can not only cause problems when reading/writing code, but also often indicate a bad smell, e.g. too many levels of indentation due to overly complex functions which require refactoring into smaller chunks. Change-Id: Ic2532676e46e93f129d590d1fa7a044ef65f50fb
This commit is contained in:
parent
77e8ad47de
commit
625fdaf2c4
|
@ -26,6 +26,7 @@ Basic white space errors, for consistent indenting
|
|||
- E002: ensure that indents are only spaces, and not hard tabs
|
||||
- E003: ensure all indents are a multiple of 4 spaces
|
||||
- E004: file did not end with a newline
|
||||
- E006: check for lines longer than 79 columns
|
||||
|
||||
Structure Errors
|
||||
~~~~~~~~~~~~~~~~
|
||||
|
|
|
@ -55,6 +55,11 @@ def check_no_trailing_whitespace(line, report):
|
|||
report.print_error(MESSAGES['E001'].msg, line)
|
||||
|
||||
|
||||
def check_no_long_lines(line, report):
|
||||
if len(line.rstrip("\r\n")) > 79:
|
||||
report.print_error(MESSAGES['E006'].msg, line)
|
||||
|
||||
|
||||
def check_indents(line, report):
|
||||
m = re.search('^(?P<indent>[ \t]+)', line)
|
||||
if m:
|
||||
|
@ -238,6 +243,7 @@ class BashateRun(object):
|
|||
logical_line = ll_split[0].rstrip()
|
||||
|
||||
check_no_trailing_whitespace(logical_line, report)
|
||||
check_no_long_lines(logical_line, report)
|
||||
check_indents(logical_line, report)
|
||||
check_for_do(logical_line, report)
|
||||
check_if_then(logical_line, report)
|
||||
|
|
|
@ -88,6 +88,19 @@ _messages = {
|
|||
""",
|
||||
'default': 'W'
|
||||
},
|
||||
'E006': {
|
||||
'msg': 'Line too long',
|
||||
'long_msg':
|
||||
"""
|
||||
This check mimics the widely accepted convention from PEP8 and
|
||||
many other places that lines longer than 79 columns can not
|
||||
only cause problems when reading/writing code, but also often
|
||||
indicates a bad smell, e.g. too many levels of indentation due
|
||||
to overly complex functions which require refactoring into
|
||||
smaller chunks.
|
||||
""",
|
||||
'default': 'W'
|
||||
},
|
||||
'E010': {
|
||||
'msg': 'The "do" should be on same line as %s',
|
||||
'long_msg':
|
||||
|
|
|
@ -0,0 +1,8 @@
|
|||
#!/bin/bash
|
||||
|
||||
# lines longer than 79 columns
|
||||
|
||||
: 345678901234567890123456789012345678901234567890123456789012345678901234567890
|
||||
: 1 2 3 4 5 6 7 8
|
||||
# next line goes over by virtue of trailing whitespace
|
||||
: 1 2 3 4 5 6 7
|
|
@ -0,0 +1,6 @@
|
|||
#!/bin/bash
|
||||
|
||||
# no lines longer than 79 columns
|
||||
|
||||
: 34567890123456789012345678901234567890123456789012345678901234567890123456789
|
||||
: 1 2 3 4 5 6 7
|
|
@ -139,6 +139,31 @@ class TestBashateSamples(base.TestCase):
|
|||
|
||||
self.assert_error_found('E002', 3)
|
||||
|
||||
def test_sample_E006_bad(self):
|
||||
test_files = ['bashate/tests/samples/E006_bad.sh']
|
||||
self.run.check_files(test_files, False)
|
||||
|
||||
self.assertEqual(self.run.warning_count, 3)
|
||||
self.assert_error_found('E006', 5)
|
||||
self.assert_error_found('E006', 6)
|
||||
self.assert_error_found('E006', 8)
|
||||
|
||||
def test_sample_E006_bad_ignore_trailing_ws(self):
|
||||
self.run.register_ignores('E001')
|
||||
test_files = ['bashate/tests/samples/E006_bad.sh']
|
||||
self.run.check_files(test_files, False)
|
||||
|
||||
self.assertEqual(self.run.warning_count, 3)
|
||||
self.assert_error_found('E006', 5)
|
||||
self.assert_error_found('E006', 6)
|
||||
self.assert_error_found('E006', 8)
|
||||
|
||||
def test_sample_E006_good(self):
|
||||
test_files = ['bashate/tests/samples/E006_good.sh']
|
||||
self.run.check_files(test_files, False)
|
||||
|
||||
self.assertEqual(self.run.warning_count, 0)
|
||||
|
||||
def test_sample_E010_good(self):
|
||||
test_files = ['bashate/tests/samples/E010_good.sh']
|
||||
self.run.check_files(test_files, False)
|
||||
|
|
Loading…
Reference in New Issue