Add --warn to turn messages down to warnings

Add --warn argument to turn messages down to warnings.  Warnings do
not result in a error-code output, but still print out the same values.

In the output prefix with either [E] or [W].  Add warning count to
final output.

Change-Id: Ib2ce41fc64fe2571dfaf5555cc1bafd71cbf9dd7
This commit is contained in:
Ian Wienand
2014-12-08 12:53:02 +11:00
parent 336f4eb790
commit e3238b83ab
2 changed files with 44 additions and 8 deletions

View File

@@ -96,28 +96,47 @@ class BashateRun(object):
def __init__(self):
# TODO(mrodden): rename these to match convention
self.ERRORS = 0
self.IGNORE = None
self.IGNORE_LIST = None
self.WARNINGS = 0
self.WARNINGS_LIST = None
def register_ignores(self, ignores):
if ignores:
self.IGNORE = '^(' + '|'.join(ignores.split(',')) + ')'
self.IGNORE_LIST = '^(' + '|'.join(ignores.split(',')) + ')'
def register_warnings(self, warnings):
if warnings:
self.WARNINGS_LIST = '^(' + '|'.join(warnings.split(',')) + ')'
def should_ignore(self, error):
return self.IGNORE and re.search(self.IGNORE, error)
return self.IGNORE_LIST and re.search(self.IGNORE_LIST, error)
def should_warn(self, error):
return self.WARNINGS_LIST and re.search(self.WARNINGS_LIST, error)
def print_error(self, error, line,
filename=None, filelineno=None):
if self.should_ignore(error):
return
warn = self.should_warn(error)
if not filename:
filename = fileinput.filename()
if not filelineno:
filelineno = fileinput.filelineno()
self.ERRORS = self.ERRORS + 1
self.log_error(error, line, filename, filelineno)
if warn:
self.WARNINGS = self.WARNINGS + 1
else:
self.ERRORS = self.ERRORS + 1
def log_error(self, error, line, filename, filelineno):
print("%s: '%s'" % (error, line.rstrip('\n')))
self.log_error(error, line, filename, filelineno, warn)
def log_error(self, error, line, filename, filelineno, warn=False):
print("[%(warn)s] %(error)s: '%(line)s'" %
{'warn': "W" if warn else "E",
'error': error,
'line': line.rstrip('\n')})
print(" - %s : L%s" % (filename, filelineno))
def check_files(self, files, verbose):
@@ -216,6 +235,8 @@ def main():
parser.add_argument('files', metavar='file', nargs='*',
help='files to scan for errors')
parser.add_argument('-i', '--ignore', help='Rules to ignore')
parser.add_argument('-w', '--warn',
help='Rules to warn (rather than error)')
parser.add_argument('-v', '--verbose', action='store_true', default=False)
opts = parser.parse_args()
@@ -226,6 +247,7 @@ def main():
run = BashateRun()
run.register_ignores(opts.ignore)
run.register_warnings(opts.warn)
try:
run.check_files(files, opts.verbose)
@@ -233,7 +255,10 @@ def main():
print("bashate: %s" % e)
return 1
if run.ERRORS:
if run.WARNINGS > 0:
print("%d bashate warning(s) found" % run.WARNINGS)
if run.ERRORS > 0:
print("%d bashate error(s) found" % run.ERRORS)
return 1
return 0

View File

@@ -200,6 +200,17 @@ class TestBashateSamples(base.TestCase):
self.assertEqual(0, self.run.ERRORS)
def test_sample_warning(self):
# reuse a couple of the above files to make sure we turn
# errors down to warnings if requested
test_files = ['bashate/tests/samples/E011_bad.sh',
'bashate/tests/samples/E041_bad.sh']
self.run.register_warnings('E011,E041')
self.run.check_files(test_files, False)
self.assertEqual(0, self.run.ERRORS)
self.assertEqual(4, self.run.WARNINGS)
def test_pre_zero_dot_one_sample_file(self):
"""Test the sample file with all pre 0.1.0 release checks.