diff options
-rw-r--r-- | coverage/report.py | 2 | ||||
-rw-r--r-- | test/test_html.py | 44 | ||||
-rw-r--r-- | test/test_summary.py | 6 |
3 files changed, 47 insertions, 5 deletions
diff --git a/coverage/report.py b/coverage/report.py index f6a9c2a8..e351340f 100644 --- a/coverage/report.py +++ b/coverage/report.py @@ -87,5 +87,5 @@ class Reporter(object): except NotPython: # Only report errors for .py files, and only if we didn't # explicitly suppress those errors. - if cu.should_be_python(".py") and not self.config.ignore_errors: + if cu.should_be_python() and not self.config.ignore_errors: raise diff --git a/test/test_html.py b/test/test_html.py index c7b56576..c5c87d2a 100644 --- a/test/test_html.py +++ b/test/test_html.py @@ -2,6 +2,8 @@ import os.path, sys import coverage +from coverage.misc import NotPython + sys.path.insert(0, os.path.split(__file__)[0]) # Force relative import for Py3k from coveragetest import CoverageTest @@ -116,7 +118,7 @@ class HtmlTest(CoverageTest): # In this case, everything changes because the coverage settings have # changed. self.create_initial_files() - self.run_coverage() + self.run_coverage(timid=False) index1 = open("htmlcov/index.html").read() self.remove_html_files() @@ -153,3 +155,43 @@ class HtmlTest(CoverageTest): fixed_index2 = index2.replace("XYZZY", self.real_coverage_version) self.assertMultiLineEqual(index1, fixed_index2) + +class HtmlWithUnparsableFilesTest(CoverageTest): + """Test the behavior when measuring unparsable files.""" + + def test_dotpy_not_python(self): + self.make_file("innocuous.py", "a = 1") + cov = coverage.coverage() + cov.start() + self.import_local_file("innocuous") + cov.stop() + self.make_file("innocuous.py", "<h1>This isn't python!</h1>") + self.assertRaisesRegexp( + NotPython, + "Couldn't parse '.*innocuous.py' as Python source: '.*' at line 1", + cov.html_report + ) + + def test_dotpy_not_python_ignored(self): + self.make_file("innocuous.py", "a = 1") + cov = coverage.coverage() + cov.start() + self.import_local_file("innocuous") + cov.stop() + self.make_file("innocuous.py", "<h1>This isn't python!</h1>") + cov.html_report(ignore_errors=True) + self.assert_exists("htmlcov/index.html") + # this would be better as a glob, if the html layout changes: + self.assert_doesnt_exist("htmlcov/innocuous.html") + + def test_dothtml_not_python(self): + # We run a .html file, and when reporting, we can't parse it as + # Python. Since it wasn't .py, no error is reported. + + # Run an "html" file + self.make_file("innocuous.html", "a = 1") + self.run_command("coverage run innocuous.html") + # Before reporting, change it to be an HTML file. + self.make_file("innocuous.html", "<h1>This isn't python at all!</h1>") + output = self.run_command("coverage html") + self.assertEqual(output.strip(), "No data to report.") diff --git a/test/test_summary.py b/test/test_summary.py index 8b264b55..71fbb1a6 100644 --- a/test/test_summary.py +++ b/test/test_summary.py @@ -144,7 +144,7 @@ class SummaryTest(CoverageTest): last = re.sub("parse '.*mycode.py", "parse 'mycode.py", last) # The actual error message varies version to version last = re.sub(": '.*' at", ": 'error' at", last) - self.assertEqual(last, + self.assertEqual(last, "mycode NotPython: " "Couldn't parse 'mycode.py' as Python source: " "'error' at line 1" @@ -172,10 +172,10 @@ class SummaryTest(CoverageTest): # Before reporting, change it to be an HTML file. self.make_file("mycode.html", "<h1>This isn't python at all!</h1>") report = self.report_from_command("coverage -r mycode.html") - + # Name Stmts Miss Cover # ---------------------------- - + self.assertEqual(self.line_count(report), 2) |