Skip to content

Commit

Permalink
fix: warn about more source file problems
Browse files Browse the repository at this point in the history
  • Loading branch information
nedbat committed Nov 10, 2021
1 parent 23f567f commit f3a70c9
Show file tree
Hide file tree
Showing 4 changed files with 20 additions and 8 deletions.
6 changes: 6 additions & 0 deletions CHANGES.rst
Original file line number Diff line number Diff line change
Expand Up @@ -29,6 +29,11 @@ Unreleased
- Fix: Complex conditionals over excluded lines could have incorrectly reported
a missing branch (`issue 1271`_). This is now fixed.

- Fix: More exceptions are now handled when trying to parse source files for
reporting. Problems that used to terminate coverage.py can now be handled
with ``[report] ignore_errors``. This helps with plugins failing to read
files (`django_coverage_plugin issue 78`_).

- Fix: Removed another vestige of jQuery from the source tarball
(`issue 840`_).

Expand All @@ -37,6 +42,7 @@ Unreleased
I'd rather not "fix" unsupported interfaces, it's actually nicer with a
default value.

.. _django_coverage_plugin issue 78: https://github.com/nedbat/django_coverage_plugin/issues/78
.. _issue 1147: https://github.com/nedbat/coveragepy/issues/1147
.. _issue 1271: https://github.com/nedbat/coveragepy/issues/1271
.. _issue 1273: https://github.com/nedbat/coveragepy/issues/1273
Expand Down
4 changes: 1 addition & 3 deletions coverage/python.py
Original file line number Diff line number Diff line change
Expand Up @@ -57,9 +57,7 @@ def get_python_source(filename):
break
else:
# Couldn't find source.
exc_msg = f"No source for code: '{filename}'.\n"
exc_msg += "Aborting report output, consider using -i."
raise NoSource(exc_msg)
raise NoSource(f"No source for code: '{filename}'.")

# Replace \f because of http://bugs.python.org/issue19035
source = source.replace(b'\f', b' ')
Expand Down
11 changes: 7 additions & 4 deletions coverage/report.py
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@

import sys

from coverage.exceptions import CoverageException, NoSource, NotPython
from coverage.exceptions import CoverageException, NotPython
from coverage.files import prep_patterns, FnmatchMatcher
from coverage.misc import ensure_dir_for_file, file_be_gone

Expand Down Expand Up @@ -70,9 +70,6 @@ def get_analysis_to_report(coverage, morfs):
for fr in sorted(file_reporters):
try:
analysis = coverage._analyze(fr)
except NoSource:
if not config.ignore_errors:
raise
except NotPython:
# Only report errors for .py files, and only if we didn't
# explicitly suppress those errors.
Expand All @@ -84,5 +81,11 @@ def get_analysis_to_report(coverage, morfs):
coverage._warn(msg, slug="couldnt-parse")
else:
raise
except Exception as exc:
if config.ignore_errors:
msg = f"Couldn't parse '{fr.filename}': {exc}".rstrip()
coverage._warn(msg, slug="couldnt-parse")
else:
raise
else:
yield (fr, analysis)
7 changes: 6 additions & 1 deletion tests/test_xml.py
Original file line number Diff line number Diff line change
Expand Up @@ -135,7 +135,12 @@ def test_no_source(self):
cov = coverage.Coverage()
self.start_import_stop(cov, "innocuous")
os.remove("innocuous.py")
cov.xml_report(ignore_errors=True)
with pytest.warns(Warning) as warns:
cov.xml_report(ignore_errors=True)
assert_coverage_warnings(
warns,
re.compile(r"Couldn't parse '.*innocuous.py'. \(couldnt-parse\)"),
)
self.assert_exists("coverage.xml")

def test_filename_format_showing_everything(self):
Expand Down

0 comments on commit f3a70c9

Please sign in to comment.