summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
-rw-r--r--coverage/html.py10
-rw-r--r--tests/test_html.py23
2 files changed, 31 insertions, 2 deletions
diff --git a/coverage/html.py b/coverage/html.py
index 159ae581..6c811107 100644
--- a/coverage/html.py
+++ b/coverage/html.py
@@ -238,7 +238,15 @@ class HtmlReporter(Reporter):
}))
if sys.version_info < (3, 0):
- html = html.decode(encoding)
+ try:
+ html = html.decode(encoding)
+ except UnicodeDecodeError as e:
+ sample = e.object[max([0, e.start-30]):e.start+30]
+ raise CoverageException(
+ "Couldn't decode %r as %s: %r" % (
+ cu.filename, e.encoding, sample
+ )
+ )
html_filename = flat_rootname + ".html"
html_path = os.path.join(self.directory, html_filename)
diff --git a/tests/test_html.py b/tests/test_html.py
index b728847f..de967f86 100644
--- a/tests/test_html.py
+++ b/tests/test_html.py
@@ -1,7 +1,7 @@
# -*- coding: utf-8 -*-
"""Tests that HTML generation is awesome."""
-import os.path, re
+import os.path, re, sys
import coverage
import coverage.html
from coverage.misc import CoverageException, NotPython, NoSource
@@ -267,6 +267,27 @@ class HtmlWithUnparsableFilesTest(CoverageTest):
cov.html_report()
self.assert_exists("htmlcov/index.html")
+ if sys.version_info < (3, 0):
+ def test_decode_error(self):
+ # imp.load_module won't load a file with an undecodable character
+ # in a comment, though Python will run them. So we'll change the
+ # file after running.
+ self.make_file("main.py", "import sub.not_ascii")
+ self.make_file("sub/__init__.py")
+ self.make_file("sub/not_ascii.py", """\
+ a = 1 # Isn't this great?
+ """)
+ cov = coverage.coverage()
+ self.start_import_stop(cov, "main")
+
+ # Create the undecodable version of the file.
+ self.make_file("sub/not_ascii.py", """\
+ a = 1 # Isn't this great?\xcb
+ """)
+ msg = r"Couldn't decode '.*sub/not_ascii.py' as ascii: .*\\xcb.*"
+ with self.assertRaisesRegex(CoverageException, msg):
+ cov.html_report()
+
class HtmlTest(CoverageTest):
"""Moar HTML tests."""