summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorNed Batchelder <ned@nedbatchelder.com>2019-07-06 17:17:07 -0400
committerNed Batchelder <ned@nedbatchelder.com>2019-07-06 17:21:57 -0400
commit9ae35a5f6a4fed487ef6c14edd6b4eb4f5c1b7f0 (patch)
treeb7ffb1ea7045f75bcb78070e728693b7b0fd8023
parente5a59865b038ac575c4f8ca9e79c5b4802018ff1 (diff)
downloadpython-coveragepy-git-9ae35a5f6a4fed487ef6c14edd6b4eb4f5c1b7f0.tar.gz
Add tests of bug #806, and ensure it's fixed even if the program ends with an exception
-rw-r--r--CHANGES.rst4
-rw-r--r--CONTRIBUTORS.txt1
-rw-r--r--coverage/execfile.py9
-rw-r--r--tests/test_process.py30
4 files changed, 40 insertions, 4 deletions
diff --git a/CHANGES.rst b/CHANGES.rst
index 4fb6e112..52ec0761 100644
--- a/CHANGES.rst
+++ b/CHANGES.rst
@@ -41,6 +41,9 @@ Unreleased
be reported as warnings. As with other warnings, you can suppress them with
the ``[run] disable_warnings`` configuration setting.
+- Coverage.py no longer fails if the user program deletes its current
+ directory, closing `issue 806`_. Thanks, Dan Hemberger.
+
- The scrollbar markers in the HTML report now accurately show the highlighted
lines, regardless of what categories of line are highlighted.
@@ -50,6 +53,7 @@ Unreleased
- The deprecated `Reporter.file_reporters` property has been removed.
.. _ShiningPanda: https://wiki.jenkins.io/display/JENKINS/ShiningPanda+Plugin
+.. _issue 806: https://github.com/nedbat/coveragepy/pull/806
.. _changes_50a5:
diff --git a/CONTRIBUTORS.txt b/CONTRIBUTORS.txt
index 972e818b..06eef4f7 100644
--- a/CONTRIBUTORS.txt
+++ b/CONTRIBUTORS.txt
@@ -33,6 +33,7 @@ Christine Lytwynec
Christoph Zwerschke
Conrad Ho
Cosimo Lupo
+Dan Hemberger
Dan Riti
Dan Wandschneider
Danek Duvall
diff --git a/coverage/execfile.py b/coverage/execfile.py
index 4edbc8ac..972d8f1b 100644
--- a/coverage/execfile.py
+++ b/coverage/execfile.py
@@ -192,12 +192,11 @@ class PyRunner(object):
raise CoverageException(msg.format(filename=self.arg0, exc=exc))
# Execute the code object.
+ # Return to the original directory in case the test code exits in
+ # a non-existent directory.
+ cwd = os.getcwd()
try:
- # Return to the original directory in case the test code exits in
- # a non-existent directory.
- cwd = os.getcwd()
exec(code, main_mod.__dict__)
- os.chdir(cwd)
except SystemExit: # pylint: disable=try-except-raise
# The user called sys.exit(). Just pass it along to the upper
# layers, where it will be handled.
@@ -236,6 +235,8 @@ class PyRunner(object):
raise ExceptionDuringRun(typ, err, tb.tb_next)
else:
sys.exit(1)
+ finally:
+ os.chdir(cwd)
def run_python_module(args):
diff --git a/tests/test_process.py b/tests/test_process.py
index 0979cc2c..a5303338 100644
--- a/tests/test_process.py
+++ b/tests/test_process.py
@@ -1293,6 +1293,36 @@ class UnicodeFilePathsTest(CoverageTest):
self.assertEqual(out, report_expected)
+class YankedDirectoryTest(CoverageTest):
+ """Tests of what happens when the current directory is deleted."""
+
+ BUG_806 = """\
+ import os
+ import sys
+ import tempfile
+
+ tmpdir = tempfile.mkdtemp()
+ os.chdir(tmpdir)
+ os.rmdir(tmpdir)
+ print(sys.argv[1])
+ """
+
+ def test_removing_directory(self):
+ self.make_file("bug806.py", self.BUG_806)
+ out = self.run_command("coverage run bug806.py noerror")
+ self.assertEqual(out, "noerror\n")
+
+ def test_removing_directory_with_error(self):
+ self.make_file("bug806.py", self.BUG_806)
+ out = self.run_command("coverage run bug806.py")
+ self.assertEqual(out, textwrap.dedent("""\
+ Traceback (most recent call last):
+ File "bug806.py", line 8, in <module>
+ print(sys.argv[1])
+ IndexError: list index out of range
+ """))
+
+
def possible_pth_dirs():
"""Produce a sequence of directories for trying to write .pth files."""
# First look through sys.path, and if we find a .pth file, then it's a good