From d395de616b5154dfe21e0d0d3228342b0fbd935e Mon Sep 17 00:00:00 2001 From: Sean Dague Date: Fri, 20 Jun 2014 07:29:22 -0400 Subject: [PATCH] don't use fileinput loop there are issues where the state of a previous file will not get reset correctly if we implicitly use the fileinput loop. To avoid this break the looping over the file list out one level. We also need to change the tests as this now requires that we always send down an array instead of a string. Change-Id: I781c1b642eb15fc7e2914791d5b77e8a4752db79 --- bashate/bashate.py | 79 ++++++++++++++++++----------------- bashate/tests/test_bashate.py | 12 +++--- 2 files changed, 46 insertions(+), 45 deletions(-) diff --git a/bashate/bashate.py b/bashate/bashate.py index 654510d..be70123 100644 --- a/bashate/bashate.py +++ b/bashate/bashate.py @@ -150,52 +150,53 @@ def check_files(files, verbose): prev_line = "" prev_lineno = 0 - for line in fileinput.input(files): - if fileinput.isfirstline(): - # if in_multiline when the new file starts then we didn't - # find the end of a heredoc in the last file. - if in_multiline: - print_error('E012: heredoc did not end before EOF', - multiline_line, - filename=prev_file, filelineno=multiline_start) - in_multiline = False + for fname in files: + for line in fileinput.input(fname): + if fileinput.isfirstline(): + # if in_multiline when the new file starts then we didn't + # find the end of a heredoc in the last file. + if in_multiline: + print_error('E012: heredoc did not end before EOF', + multiline_line, + filename=prev_file, filelineno=multiline_start) + in_multiline = False - # last line of a previous file should always end with a - # newline - if prev_file and not prev_line.endswith('\n'): - print_error('E004: file did not end with a newline', - prev_line, - filename=prev_file, filelineno=prev_lineno) + # last line of a previous file should always end with a + # newline + if prev_file and not prev_line.endswith('\n'): + print_error('E004: file did not end with a newline', + prev_line, + filename=prev_file, filelineno=prev_lineno) - prev_file = fileinput.filename() + prev_file = fileinput.filename() - if verbose: - print("Running bashate on %s" % fileinput.filename()) + if verbose: + print("Running bashate on %s" % fileinput.filename()) - # NOTE(sdague): multiline processing of heredocs is interesting - if not in_multiline: - logical_line = line - token = starts_multiline(line) - if token: - in_multiline = True - multiline_start = fileinput.filelineno() - multiline_line = line - continue - else: - logical_line = logical_line + line - if not end_of_multiline(line, token): - continue + # NOTE(sdague): multiline processing of heredocs is interesting + if not in_multiline: + logical_line = line + token = starts_multiline(line) + if token: + in_multiline = True + multiline_start = fileinput.filelineno() + multiline_line = line + continue else: - in_multiline = False + logical_line = logical_line + line + if not end_of_multiline(line, token): + continue + else: + in_multiline = False - check_no_trailing_whitespace(logical_line) - check_indents(logical_line) - check_for_do(logical_line) - check_if_then(logical_line) - check_function_decl(logical_line) + check_no_trailing_whitespace(logical_line) + check_indents(logical_line) + check_for_do(logical_line) + check_if_then(logical_line) + check_function_decl(logical_line) - prev_line = logical_line - prev_lineno = fileinput.filelineno() + prev_line = logical_line + prev_lineno = fileinput.filelineno() def get_options(): diff --git a/bashate/tests/test_bashate.py b/bashate/tests/test_bashate.py index bd51156..52a7deb 100644 --- a/bashate/tests/test_bashate.py +++ b/bashate/tests/test_bashate.py @@ -78,14 +78,14 @@ class TestBashateSamples(base.TestCase): (error, lineno)) def test_sample_E001(self): - test_file = 'bashate/tests/samples/E001_bad.sh' - bashate.check_files(test_file, False) + test_files = ['bashate/tests/samples/E001_bad.sh'] + bashate.check_files(test_files, False) self.assert_error_found('E001', 4) def test_sample_E002(self): - test_file = 'bashate/tests/samples/E002_bad.sh' - bashate.check_files(test_file, False) + test_files = ['bashate/tests/samples/E002_bad.sh'] + bashate.check_files(test_files, False) self.assert_error_found('E002', 3) @@ -101,8 +101,8 @@ class TestBashateSamples(base.TestCase): have their own separate unit test and/or sample file checks. """ - test_file = 'bashate/tests/samples/legacy_sample.sh' - bashate.check_files(test_file, False) + test_files = ['bashate/tests/samples/legacy_sample.sh'] + bashate.check_files(test_files, False) # NOTE(mrodden): E012 actually requires iterating more than one # file to detect at the moment; this is bug