From 7543956017305b6ca9640718e6944b6b144767e2 Mon Sep 17 00:00:00 2001 From: Julian Berman Date: Tue, 31 Jul 2012 10:17:00 -0400 Subject: Don't report coverage for non-Python source files even if __file__ says they were executed. Closes #82 --- coverage/parser.py | 11 ++++++++++- test/test_html.py | 18 ++++++++++++++++++ 2 files changed, 28 insertions(+), 1 deletion(-) diff --git a/coverage/parser.py b/coverage/parser.py index f868d357..a9cceb35 100644 --- a/coverage/parser.py +++ b/coverage/parser.py @@ -203,7 +203,16 @@ class CodeParser(object): statements. """ - self._raw_parse() + + try: + self._raw_parse() + except tokenize.TokenError: + _, tokerr, _ = sys.exc_info() + msg, lineno = tokerr.args + raise NotPython( + "Couldn't parse '%s' as Python source: '%s' at %s" % + (self.filename, msg, lineno) + ) excluded_lines = self.first_lines(self.excluded) ignore = excluded_lines + list(self.docstrings) diff --git a/test/test_html.py b/test/test_html.py index c5c87d2a..96f98c83 100644 --- a/test/test_html.py +++ b/test/test_html.py @@ -195,3 +195,21 @@ class HtmlWithUnparsableFilesTest(CoverageTest): self.make_file("innocuous.html", "

This isn't python at all!

") output = self.run_command("coverage html") self.assertEqual(output.strip(), "No data to report.") + + def test_execed_liar_ignored(self): + """ + Jinja2 sets __file__ to be a non-Python file, and then execs code. + + If that file contains non-Python code, a TokenError shouldn't + have been raised when writing the HTML report. + + """ + + source = "exec compile('', '', 'exec') in {'__file__' : 'liar.html'}" + self.make_file("liar.py", source) + self.make_file("liar.html", "{# Whoops, not python code #}") + cov = coverage.coverage() + cov.start() + self.import_local_file("liar") + cov.stop() + cov.html_report() -- cgit v1.2.1 From 77a5085b2af747466d06568ad026b6e8495949b3 Mon Sep 17 00:00:00 2001 From: Ned Batchelder Date: Tue, 31 Jul 2012 21:56:22 -0400 Subject: Finish the paperwork on Julian's patch. --- AUTHORS.txt | 1 + CHANGES.txt | 5 +++++ coverage/parser.py | 1 - test/test_html.py | 1 - 4 files changed, 6 insertions(+), 2 deletions(-) diff --git a/AUTHORS.txt b/AUTHORS.txt index ef8ef276..1bb1d0c6 100644 --- a/AUTHORS.txt +++ b/AUTHORS.txt @@ -6,6 +6,7 @@ Other contributions have been made by: Marc Abramowitz Chris Adams Geoff Bache +Julian Berman Titus Brown Brett Cannon Pablo Carballo diff --git a/CHANGES.txt b/CHANGES.txt index 196ea1df..40408850 100644 --- a/CHANGES.txt +++ b/CHANGES.txt @@ -12,11 +12,16 @@ Version 3.5.3b1 - Files encoded as UTF-8 with a BOM are now properly handled, fixing `issue 179`_. Thanks, Pablo Carballo. +- Fixed more cases of non-Python files being reported as Python source, and + then not being able to parse them as Python. Closes `issue 82` (again). + Thanks, Julian Berman. + - Try to do a better job of the impossible task of detecting when we can't build the C extension, fixing `issue 183`_. - Testing is now done with `tox`_. +.. _issue 82: https://bitbucket.org/ned/coveragepy/issue/82/tokenerror-when-generating-html-report .. _issue 179: https://bitbucket.org/ned/coveragepy/issue/179/htmlreporter-fails-when-source-file-is .. _issue 183: https://bitbucket.org/ned/coveragepy/issue/183/install-fails-for-python-23 .. _tox: http://tox.readthedocs.org/ diff --git a/coverage/parser.py b/coverage/parser.py index a9cceb35..636a8353 100644 --- a/coverage/parser.py +++ b/coverage/parser.py @@ -203,7 +203,6 @@ class CodeParser(object): statements. """ - try: self._raw_parse() except tokenize.TokenError: diff --git a/test/test_html.py b/test/test_html.py index 96f98c83..3ebdb10b 100644 --- a/test/test_html.py +++ b/test/test_html.py @@ -204,7 +204,6 @@ class HtmlWithUnparsableFilesTest(CoverageTest): have been raised when writing the HTML report. """ - source = "exec compile('', '', 'exec') in {'__file__' : 'liar.html'}" self.make_file("liar.py", source) self.make_file("liar.html", "{# Whoops, not python code #}") -- cgit v1.2.1 From 1cac130c6c95804657c62c3944fd299a6690e73a Mon Sep 17 00:00:00 2001 From: Ned Batchelder Date: Tue, 31 Jul 2012 22:20:06 -0400 Subject: Make the new test also run in Py3 --- test/test_html.py | 5 ++++- 1 file changed, 4 insertions(+), 1 deletion(-) diff --git a/test/test_html.py b/test/test_html.py index 3ebdb10b..36275fa0 100644 --- a/test/test_html.py +++ b/test/test_html.py @@ -204,7 +204,10 @@ class HtmlWithUnparsableFilesTest(CoverageTest): have been raised when writing the HTML report. """ - source = "exec compile('', '', 'exec') in {'__file__' : 'liar.html'}" + if sys.version_info < (3, 0): + source = "exec compile('','','exec') in {'__file__': 'liar.html'}" + else: + source = "exec(compile('','','exec'), {'__file__': 'liar.html'})" self.make_file("liar.py", source) self.make_file("liar.html", "{# Whoops, not python code #}") cov = coverage.coverage() -- cgit v1.2.1 From 6527a504c0871621563bb5d2b4147a065beaa55e Mon Sep 17 00:00:00 2001 From: Ned Batchelder Date: Wed, 1 Aug 2012 13:51:54 -0400 Subject: Add a positive assertion to the recent cant-parse-html test --- test/test_html.py | 1 + 1 file changed, 1 insertion(+) diff --git a/test/test_html.py b/test/test_html.py index 36275fa0..1877a30d 100644 --- a/test/test_html.py +++ b/test/test_html.py @@ -215,3 +215,4 @@ class HtmlWithUnparsableFilesTest(CoverageTest): self.import_local_file("liar") cov.stop() cov.html_report() + self.assert_exists("htmlcov/index.html") -- cgit v1.2.1