Skip to content

Commit 1aae774

Browse files
Jaap Roesnedbat
authored andcommitted
fix: raise a Coverage-handlable exception if a file can't be read. #78
1 parent 7b1628b commit 1aae774

File tree

2 files changed

+75
-1
lines changed

2 files changed

+75
-1
lines changed

django_coverage_plugin/plugin.py

Lines changed: 8 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -8,6 +8,10 @@
88
import os.path
99
import re
1010

11+
try:
12+
from coverage.exceptions import NoSource
13+
except ImportError: # for coverage 5.x
14+
from coverage.misc import NoSource
1115
import coverage.plugin
1216
import django
1317
import django.template
@@ -303,7 +307,10 @@ def __init__(self, filename):
303307

304308
def source(self):
305309
if self._source is None:
306-
self._source = read_template_source(self.filename)
310+
try:
311+
self._source = read_template_source(self.filename)
312+
except (IOError, UnicodeError) as exc:
313+
raise NoSource("Couldn't read {}: {}".format(self.filename, exc))
307314
return self._source
308315

309316
def lines(self):

tests/test_source.py

Lines changed: 67 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,11 @@
33

44
"""Tests of template inheritance for django_coverage_plugin."""
55

6+
try:
7+
from coverage.exceptions import NoSource
8+
except ImportError: # for coverage 5.x
9+
from coverage.misc import NoSource
10+
611
from .plugin_test import DjangoPluginTestCase
712

813

@@ -64,3 +69,65 @@ def test_customized_extensions(self):
6469
self.assert_analysis([1], name="phd.tex", missing=[1])
6570
# The editor leave-behinds are not in the measured files.
6671
self.assert_measured_files("main.html", "unused.html", "phd.tex")
72+
73+
def test_non_utf8_error(self):
74+
# A non-UTF8 text file will raise an error.
75+
self.make_file(".coveragerc", """\
76+
[run]
77+
plugins = django_coverage_plugin
78+
source = .
79+
""")
80+
# This is a template that is rendered.
81+
self.make_template(name="main.html", text="Hello")
82+
# Extra file containing a word encoded in CP-1252
83+
self.make_file(self._path("static/changelog.txt"), bytes=b"sh\xf6n")
84+
85+
text = self.run_django_coverage(name="main.html")
86+
self.assertEqual(text, "Hello")
87+
88+
self.assert_measured_files("main.html", "static/changelog.txt")
89+
self.assert_analysis([1], name="main.html")
90+
with self.assertRaisesRegexp(NoSource, r"changelog.txt.*invalid start byte"):
91+
self.cov.html_report()
92+
93+
def test_non_utf8_omitted(self):
94+
# If we omit the directory with the non-UTF8 file, all is well.
95+
self.make_file(".coveragerc", """\
96+
[run]
97+
plugins = django_coverage_plugin
98+
source = .
99+
[report]
100+
omit = */static/*
101+
""")
102+
# This is a template that is rendered.
103+
self.make_template(name="main.html", text="Hello")
104+
# Extra file containing a word encoded in CP-1252
105+
self.make_file(self._path("static/changelog.txt"), bytes=b"sh\xf6n")
106+
107+
text = self.run_django_coverage(name="main.html")
108+
self.assertEqual(text, "Hello")
109+
110+
self.assert_measured_files("main.html", "static/changelog.txt")
111+
self.assert_analysis([1], name="main.html")
112+
self.cov.html_report()
113+
114+
def test_non_utf8_ignored(self):
115+
# If we ignore reporting errors, a non-UTF8 text file is fine.
116+
self.make_file(".coveragerc", """\
117+
[run]
118+
plugins = django_coverage_plugin
119+
source = .
120+
[report]
121+
ignore_errors = True
122+
""")
123+
# This is a template that is rendered.
124+
self.make_template(name="main.html", text="Hello")
125+
# Extra file containing a word encoded in CP-1252
126+
self.make_file(self._path("static/changelog.txt"), bytes=b"sh\xf6n")
127+
128+
text = self.run_django_coverage(name="main.html")
129+
self.assertEqual(text, "Hello")
130+
131+
self.assert_measured_files("main.html", "static/changelog.txt")
132+
self.assert_analysis([1], name="main.html")
133+
self.cov.html_report()

0 commit comments

Comments
 (0)