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
This commit is contained in:
@@ -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():
|
||||
|
||||
@@ -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
|
||||
|
||||
Reference in New Issue
Block a user