summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorNed Batchelder <ned@nedbatchelder.com>2021-03-21 16:50:00 -0400
committerNed Batchelder <ned@nedbatchelder.com>2021-03-22 07:04:13 -0400
commit34660a217c70b810f7ec5630963f8c37e7a208dc (patch)
tree5f69bcb8666f43eb99f7e91c451d6f134b9783ec
parentf0f6faaa69333aac63b0df7423eded8fa25f87ac (diff)
downloadpython-coveragepy-git-34660a217c70b810f7ec5630963f8c37e7a208dc.tar.gz
refactor: simplify temp dir cd code
-rw-r--r--tests/mixins.py25
1 files changed, 10 insertions, 15 deletions
diff --git a/tests/mixins.py b/tests/mixins.py
index ef9cdb6f..0a175dae 100644
--- a/tests/mixins.py
+++ b/tests/mixins.py
@@ -18,7 +18,7 @@ import pytest
from coverage import env
from coverage.backward import importlib
-from tests.helpers import remove_files
+from tests.helpers import change_dir, remove_files
class PytestBase(object):
@@ -63,24 +63,19 @@ class TempDirMixin(object):
@pytest.fixture(autouse=True)
def _temp_dir(self, tmpdir_factory):
"""Create a temp dir for the tests, if they want it."""
- old_dir = None
if self.run_in_temp_dir:
tmpdir = tmpdir_factory.mktemp("")
self.temp_dir = str(tmpdir)
- old_dir = os.getcwd()
- tmpdir.chdir()
-
- # Modules should be importable from this temp directory. We don't
- # use '' because we make lots of different temp directories and
- # nose's caching importer can get confused. The full path prevents
- # problems.
- sys.path.insert(0, os.getcwd())
-
- try:
+ with change_dir(self.temp_dir):
+ # Modules should be importable from this temp directory. We don't
+ # use '' because we make lots of different temp directories and
+ # nose's caching importer can get confused. The full path prevents
+ # problems.
+ sys.path.insert(0, os.getcwd())
+
+ yield None
+ else:
yield None
- finally:
- if old_dir is not None:
- os.chdir(old_dir)
def make_file(self, filename, text="", bytes=b"", newline=None):
"""Create a file for testing.