Add distinction between reported and ignored errors

This allows us to properly exit if no errors were reported (due to,
for example, # noqa).
This commit is contained in:
Ian Cordasco 2016-06-07 18:02:53 -05:00
parent 9ebaa5c69c
commit 8362fa7acd
No known key found for this signature in database
GPG key ID: 656D3395E4A9791A
3 changed files with 43 additions and 7 deletions

View file

@ -163,8 +163,9 @@ class Manager(object):
def _handle_results(self, filename, results): def _handle_results(self, filename, results):
style_guide = self.style_guide style_guide = self.style_guide
reported_results_count = 0
for (error_code, line_number, column, text, physical_line) in results: for (error_code, line_number, column, text, physical_line) in results:
style_guide.handle_error( reported_results_count += style_guide.handle_error(
code=error_code, code=error_code,
filename=filename, filename=filename,
line_number=line_number, line_number=line_number,
@ -172,6 +173,7 @@ class Manager(object):
text=text, text=text,
physical_line=physical_line, physical_line=physical_line,
) )
return reported_results_count
def _run_checks_from_queue(self): def _run_checks_from_queue(self):
LOG.info('Running checks in parallel') LOG.info('Running checks in parallel')
@ -221,17 +223,24 @@ class Manager(object):
] ]
def report(self): def report(self):
# type: () -> (int, int)
"""Report all of the errors found in the managed file checkers. """Report all of the errors found in the managed file checkers.
This iterates over each of the checkers and reports the errors sorted This iterates over each of the checkers and reports the errors sorted
by line number. by line number.
:returns:
A tuple of the total results found and the results reported.
:rtype:
tuple(int, int)
""" """
results_found = 0 results_reported = results_found = 0
for checker in self.checkers: for checker in self.checkers:
results = sorted(checker.results, key=lambda tup: (tup[2], tup[3])) results = sorted(checker.results, key=lambda tup: (tup[2], tup[3]))
self._handle_results(checker.filename, results) results_reported += self._handle_results(checker.filename,
results)
results_found += len(results) results_found += len(results)
return results_found return (results_found, results_reported)
def run_parallel(self): def run_parallel(self):
"""Run the checkers in parallel.""" """Run the checkers in parallel."""

View file

@ -403,7 +403,11 @@ class Application(object):
number of errors, warnings, and other messages found. number of errors, warnings, and other messages found.
""" """
LOG.info('Reporting errors') LOG.info('Reporting errors')
self.result_count = self.file_checker_manager.report() results = self.file_checker_manager.report()
self.total_result_count, self.result_count = results
LOG.info('Found a total of %d results and reported %d',
self.total_result_count,
self.result_count)
def _run(self, argv): def _run(self, argv):
# type: (Union[NoneType, List[str]]) -> NoneType # type: (Union[NoneType, List[str]]) -> NoneType

View file

@ -233,8 +233,29 @@ class StyleGuide(object):
def handle_error(self, code, filename, line_number, column_number, text, def handle_error(self, code, filename, line_number, column_number, text,
physical_line=None): physical_line=None):
# type: (str, str, int, int, str) -> NoneType # type: (str, str, int, int, str) -> int
"""Handle an error reported by a check.""" """Handle an error reported by a check.
:param str code:
The error code found, e.g., E123.
:param str filename:
The file in which the error was found.
:param int line_number:
The line number (where counting starts at 1) at which the error
occurs.
:param int column_number:
The column number (where counting starts at 1) at which the error
occurs.
:param str text:
The text of the error message.
:param str physical_line:
The actual physical line causing the error.
:returns:
1 if the error was reported. 0 if it was ignored. This is to allow
for counting of the number of errors found that were not ignored.
:rtype:
int
"""
error = Error(code, filename, line_number, column_number, text, error = Error(code, filename, line_number, column_number, text,
physical_line) physical_line)
if error.filename is None or error.filename == '-': if error.filename is None or error.filename == '-':
@ -247,6 +268,8 @@ class StyleGuide(object):
is_included_in_diff): is_included_in_diff):
self.formatter.handle(error) self.formatter.handle(error)
self.listener.notify(error.code, error) self.listener.notify(error.code, error)
return 1
return 0
def add_diff_ranges(self, diffinfo): def add_diff_ranges(self, diffinfo):
"""Update the StyleGuide to filter out information not in the diff. """Update the StyleGuide to filter out information not in the diff.