diff --git a/newsfragments/+76d2542b.feature.rst b/newsfragments/+76d2542b.feature.rst new file mode 100644 index 00000000..43b0e391 --- /dev/null +++ b/newsfragments/+76d2542b.feature.rst @@ -0,0 +1 @@ +Updated fixtures for python/cpython#120801. diff --git a/tests/compat/py39.py b/tests/compat/py39.py index 16c8b574..9476eb35 100644 --- a/tests/compat/py39.py +++ b/tests/compat/py39.py @@ -2,7 +2,7 @@ os_helper = try_import('os_helper') or from_test_support( - 'FS_NONASCII', 'skip_unless_symlink' + 'FS_NONASCII', 'skip_unless_symlink', 'temp_dir' ) import_helper = try_import('import_helper') or from_test_support( 'modules_setup', 'modules_cleanup' diff --git a/tests/fixtures.py b/tests/fixtures.py index f8082df0..1bf9a803 100644 --- a/tests/fixtures.py +++ b/tests/fixtures.py @@ -1,10 +1,8 @@ -import os import sys import copy import json import shutil import pathlib -import tempfile import textwrap import functools import contextlib @@ -26,29 +24,12 @@ @contextlib.contextmanager -def tempdir(): - tmpdir = tempfile.mkdtemp() - try: - yield pathlib.Path(tmpdir) - finally: - shutil.rmtree(tmpdir) - - -@contextlib.contextmanager -def save_cwd(): - orig = os.getcwd() - try: - yield - finally: - os.chdir(orig) - - -@contextlib.contextmanager -def tempdir_as_cwd(): - with tempdir() as tmp: - with save_cwd(): - os.chdir(str(tmp)) - yield tmp +def tmp_path(): + """ + Like os_helper.temp_dir, but yields a pathlib.Path. + """ + with os_helper.temp_dir() as path: + yield pathlib.Path(path) @contextlib.contextmanager @@ -69,7 +50,7 @@ def setUp(self): class SiteDir(Fixtures): def setUp(self): super().setUp() - self.site_dir = self.fixtures.enter_context(tempdir()) + self.site_dir = self.fixtures.enter_context(tmp_path()) class OnSysPath(Fixtures): diff --git a/tests/test_api.py b/tests/test_api.py index a85c62ad..a93065cb 100644 --- a/tests/test_api.py +++ b/tests/test_api.py @@ -109,7 +109,7 @@ def test_entry_points_unique_packages_normalized(self): Entry points should only be exposed for the first package on sys.path with a given name (even when normalized). """ - alt_site_dir = self.fixtures.enter_context(fixtures.tempdir()) + alt_site_dir = self.fixtures.enter_context(fixtures.tmp_path()) self.fixtures.enter_context(self.add_sys_path(alt_site_dir)) alt_pkg = { "DistInfo_pkg-1.1.0.dist-info": { diff --git a/tests/test_main.py b/tests/test_main.py index af79e698..32241c00 100644 --- a/tests/test_main.py +++ b/tests/test_main.py @@ -135,7 +135,7 @@ def test_unique_distributions(self): fixtures.build_files(self.make_pkg('abc'), self.site_dir) before = list(_unique(distributions())) - alt_site_dir = self.fixtures.enter_context(fixtures.tempdir()) + alt_site_dir = self.fixtures.enter_context(fixtures.tmp_path()) self.fixtures.enter_context(self.add_sys_path(alt_site_dir)) fixtures.build_files(self.make_pkg('ABC'), alt_site_dir) after = list(_unique(distributions()))