From f5a24b097c7e00c6cf8a2976479db153fb5982e3 Mon Sep 17 00:00:00 2001 From: Ayaz Salikhov Date: Fri, 30 Aug 2019 19:51:08 +0300 Subject: [PATCH] Add an ability to specify max line length Change-Id: I554b4a993b30462d454ef4984a9091c38365f9fa Closes-Bug: #1842109 --- bashate/bashate.py | 12 +++++++----- bashate/messages.py | 10 +++++----- bashate/tests/samples/E006_bad_custom_length.sh | 8 ++++++++ bashate/tests/test_bashate.py | 15 +++++++++++++-- .../custom-line-length-083e8077951a8ead.yaml | 5 +++++ 5 files changed, 38 insertions(+), 12 deletions(-) create mode 100644 bashate/tests/samples/E006_bad_custom_length.sh create mode 100644 releasenotes/notes/custom-line-length-083e8077951a8ead.yaml diff --git a/bashate/bashate.py b/bashate/bashate.py index e20df8b..0425841 100755 --- a/bashate/bashate.py +++ b/bashate/bashate.py @@ -59,8 +59,8 @@ 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: +def check_no_long_lines(line, report, max_line_length): + if len(line.rstrip("\r\n")) > max_line_length: report.print_error(MESSAGES['E006'].msg, line) @@ -306,7 +306,7 @@ class BashateRun(object): 'error': error.replace(":", "", 1), 'line': line.rstrip('\n')}) - def check_files(self, files, verbose): + def check_files(self, files, verbose, max_line_length=79): logical_line = "" token = False @@ -401,7 +401,7 @@ class BashateRun(object): # separatley. Stick with what works... for line in logical_line: check_no_trailing_whitespace(line, report) - check_no_long_lines(line, report) + check_no_long_lines(line, report, max_line_length) check_for_do(line, report) check_if_then(line, report) check_function_decl(line, report) @@ -431,6 +431,8 @@ def main(args=None): help='Rules to always warn (rather than error)') parser.add_argument('-e', '--error', help='Rules to always error (rather than warn)') + parser.add_argument('--max-line-length', default=79, type=int, + help='Max line length') parser.add_argument('-v', '--verbose', action='store_true', default=False) parser.add_argument('--version', action='store_true', help='show bashate version number and exit', @@ -457,7 +459,7 @@ def main(args=None): run.register_errors(opts.error) try: - run.check_files(files, opts.verbose) + run.check_files(files, opts.verbose, opts.max_line_length) except IOError as e: print("bashate: %s" % e) return 1 diff --git a/bashate/messages.py b/bashate/messages.py index 994b761..1df90fc 100644 --- a/bashate/messages.py +++ b/bashate/messages.py @@ -93,11 +93,11 @@ _messages = { '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. + many other places that lines longer than a standard terminal width + (default=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' }, diff --git a/bashate/tests/samples/E006_bad_custom_length.sh b/bashate/tests/samples/E006_bad_custom_length.sh new file mode 100644 index 0000000..f88b461 --- /dev/null +++ b/bashate/tests/samples/E006_bad_custom_length.sh @@ -0,0 +1,8 @@ +#!/bin/bash + +# lines longer than 10 columns give warnings + +: 0123456789 +: 1 2 3 4 5 6 7 8 + +: 012345 diff --git a/bashate/tests/test_bashate.py b/bashate/tests/test_bashate.py index 83a2939..b1325e7 100644 --- a/bashate/tests/test_bashate.py +++ b/bashate/tests/test_bashate.py @@ -64,8 +64,11 @@ class TestBashate(base.TestCase): result = bashate.main(['--verbose', '/path/to/fileA', '/path/to/fileB']) - m_run_obj.check_files.assert_called_with(['/path/to/fileA', - '/path/to/fileB'], True) + m_run_obj.check_files.assert_called_with( + ['/path/to/fileA', '/path/to/fileB'], + True, + 79 + ) expected_return = 1 self.assertEqual(expected_return, result) @@ -176,6 +179,14 @@ class TestBashateSamples(base.TestCase): self.assert_error_found('E006', 6) self.assert_error_found('E006', 8) + def test_sample_E006_bad_custom_max_line_length(self): + test_files = ['bashate/tests/samples/E006_bad_custom_length.sh'] + self.run.check_files(test_files, False, 10) + + self.assertEqual(self.run.warning_count, 2) + self.assert_error_found('E006', 5) + self.assert_error_found('E006', 6) + def test_sample_E006_good(self): test_files = ['bashate/tests/samples/E006_good.sh'] self.run.check_files(test_files, False) diff --git a/releasenotes/notes/custom-line-length-083e8077951a8ead.yaml b/releasenotes/notes/custom-line-length-083e8077951a8ead.yaml new file mode 100644 index 0000000..0f20a17 --- /dev/null +++ b/releasenotes/notes/custom-line-length-083e8077951a8ead.yaml @@ -0,0 +1,5 @@ +--- +features: + - | + Adds an option ``--max-line-length`` to specify the maximum length + of lines for check E006.