diff --git a/README.rst b/README.rst index 0ec3cb2..28d416b 100644 --- a/README.rst +++ b/README.rst @@ -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 ~~~~~~~~~~~~~~~~ diff --git a/bashate/bashate.py b/bashate/bashate.py index 2e76dce..df6fc53 100644 --- a/bashate/bashate.py +++ b/bashate/bashate.py @@ -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[ \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) diff --git a/bashate/messages.py b/bashate/messages.py index 480f68c..029d98f 100644 --- a/bashate/messages.py +++ b/bashate/messages.py @@ -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': diff --git a/bashate/tests/samples/E006_bad.sh b/bashate/tests/samples/E006_bad.sh new file mode 100644 index 0000000..9f929b0 --- /dev/null +++ b/bashate/tests/samples/E006_bad.sh @@ -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 diff --git a/bashate/tests/samples/E006_good.sh b/bashate/tests/samples/E006_good.sh new file mode 100644 index 0000000..c9fb607 --- /dev/null +++ b/bashate/tests/samples/E006_good.sh @@ -0,0 +1,6 @@ +#!/bin/bash + +# no lines longer than 79 columns + +: 34567890123456789012345678901234567890123456789012345678901234567890123456789 +: 1 2 3 4 5 6 7 diff --git a/bashate/tests/test_bashate.py b/bashate/tests/test_bashate.py index 729301c..248e7ac 100644 --- a/bashate/tests/test_bashate.py +++ b/bashate/tests/test_bashate.py @@ -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)