Skip to content

Commit f3a70c9

Browse files
committed
fix: warn about more source file problems
1 parent 23f567f commit f3a70c9

File tree

4 files changed

+20
-8
lines changed

4 files changed

+20
-8
lines changed

CHANGES.rst

+6
Original file line numberDiff line numberDiff line change
@@ -29,6 +29,11 @@ Unreleased
2929
- Fix: Complex conditionals over excluded lines could have incorrectly reported
3030
a missing branch (`issue 1271`_). This is now fixed.
3131

32+
- Fix: More exceptions are now handled when trying to parse source files for
33+
reporting. Problems that used to terminate coverage.py can now be handled
34+
with ``[report] ignore_errors``. This helps with plugins failing to read
35+
files (`django_coverage_plugin issue 78`_).
36+
3237
- Fix: Removed another vestige of jQuery from the source tarball
3338
(`issue 840`_).
3439

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

45+
.. _django_coverage_plugin issue 78: https://github.com/nedbat/django_coverage_plugin/issues/78
4046
.. _issue 1147: https://github.com/nedbat/coveragepy/issues/1147
4147
.. _issue 1271: https://github.com/nedbat/coveragepy/issues/1271
4248
.. _issue 1273: https://github.com/nedbat/coveragepy/issues/1273

coverage/python.py

+1-3
Original file line numberDiff line numberDiff line change
@@ -57,9 +57,7 @@ def get_python_source(filename):
5757
break
5858
else:
5959
# Couldn't find source.
60-
exc_msg = f"No source for code: '{filename}'.\n"
61-
exc_msg += "Aborting report output, consider using -i."
62-
raise NoSource(exc_msg)
60+
raise NoSource(f"No source for code: '{filename}'.")
6361

6462
# Replace \f because of http://bugs.python.org/issue19035
6563
source = source.replace(b'\f', b' ')

coverage/report.py

+7-4
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,7 @@
55

66
import sys
77

8-
from coverage.exceptions import CoverageException, NoSource, NotPython
8+
from coverage.exceptions import CoverageException, NotPython
99
from coverage.files import prep_patterns, FnmatchMatcher
1010
from coverage.misc import ensure_dir_for_file, file_be_gone
1111

@@ -70,9 +70,6 @@ def get_analysis_to_report(coverage, morfs):
7070
for fr in sorted(file_reporters):
7171
try:
7272
analysis = coverage._analyze(fr)
73-
except NoSource:
74-
if not config.ignore_errors:
75-
raise
7673
except NotPython:
7774
# Only report errors for .py files, and only if we didn't
7875
# explicitly suppress those errors.
@@ -84,5 +81,11 @@ def get_analysis_to_report(coverage, morfs):
8481
coverage._warn(msg, slug="couldnt-parse")
8582
else:
8683
raise
84+
except Exception as exc:
85+
if config.ignore_errors:
86+
msg = f"Couldn't parse '{fr.filename}': {exc}".rstrip()
87+
coverage._warn(msg, slug="couldnt-parse")
88+
else:
89+
raise
8790
else:
8891
yield (fr, analysis)

tests/test_xml.py

+6-1
Original file line numberDiff line numberDiff line change
@@ -135,7 +135,12 @@ def test_no_source(self):
135135
cov = coverage.Coverage()
136136
self.start_import_stop(cov, "innocuous")
137137
os.remove("innocuous.py")
138-
cov.xml_report(ignore_errors=True)
138+
with pytest.warns(Warning) as warns:
139+
cov.xml_report(ignore_errors=True)
140+
assert_coverage_warnings(
141+
warns,
142+
re.compile(r"Couldn't parse '.*innocuous.py'. \(couldnt-parse\)"),
143+
)
139144
self.assert_exists("coverage.xml")
140145

141146
def test_filename_format_showing_everything(self):

0 commit comments

Comments
 (0)