Merge "Add an ability to specify max line length"

This commit is contained in:
Zuul 2020-02-04 02:55:12 +00:00 committed by Gerrit Code Review
commit 49c5f16d62
5 changed files with 38 additions and 12 deletions

View File

@ -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

View File

@ -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'
},

View File

@ -0,0 +1,8 @@
#!/bin/bash
# lines longer than 10 columns give warnings
: 0123456789
: 1 2 3 4 5 6 7 8
: 012345

View File

@ -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)

View File

@ -0,0 +1,5 @@
---
features:
- |
Adds an option ``--max-line-length`` to specify the maximum length
of lines for check E006.