Skip to content

Commit 85d90b5

Browse files
authored
gh-120801: Refactor importlib.metadata fixtures. (#120803)
These changes released with importlib_metadata 7.2.0.
1 parent c1553bc commit 85d90b5

File tree

4 files changed

+11
-28
lines changed

4 files changed

+11
-28
lines changed

Lib/test/test_importlib/metadata/fixtures.py

Lines changed: 7 additions & 26 deletions
Original file line numberDiff line numberDiff line change
@@ -1,10 +1,8 @@
1-
import os
21
import sys
32
import copy
43
import json
54
import shutil
65
import pathlib
7-
import tempfile
86
import textwrap
97
import functools
108
import contextlib
@@ -27,29 +25,12 @@
2725

2826

2927
@contextlib.contextmanager
30-
def tempdir():
31-
tmpdir = tempfile.mkdtemp()
32-
try:
33-
yield pathlib.Path(tmpdir)
34-
finally:
35-
shutil.rmtree(tmpdir)
36-
37-
38-
@contextlib.contextmanager
39-
def save_cwd():
40-
orig = os.getcwd()
41-
try:
42-
yield
43-
finally:
44-
os.chdir(orig)
45-
46-
47-
@contextlib.contextmanager
48-
def tempdir_as_cwd():
49-
with tempdir() as tmp:
50-
with save_cwd():
51-
os.chdir(str(tmp))
52-
yield tmp
28+
def tmp_path():
29+
"""
30+
Like os_helper.temp_dir, but yields a pathlib.Path.
31+
"""
32+
with os_helper.temp_dir() as path:
33+
yield pathlib.Path(path)
5334

5435

5536
@contextlib.contextmanager
@@ -70,7 +51,7 @@ def setUp(self):
7051
class SiteDir(Fixtures):
7152
def setUp(self):
7253
super().setUp()
73-
self.site_dir = self.fixtures.enter_context(tempdir())
54+
self.site_dir = self.fixtures.enter_context(tmp_path())
7455

7556

7657
class OnSysPath(Fixtures):

Lib/test/test_importlib/metadata/test_api.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -109,7 +109,7 @@ def test_entry_points_unique_packages_normalized(self):
109109
Entry points should only be exposed for the first package
110110
on sys.path with a given name (even when normalized).
111111
"""
112-
alt_site_dir = self.fixtures.enter_context(fixtures.tempdir())
112+
alt_site_dir = self.fixtures.enter_context(fixtures.tmp_path())
113113
self.fixtures.enter_context(self.add_sys_path(alt_site_dir))
114114
alt_pkg = {
115115
"DistInfo_pkg-1.1.0.dist-info": {

Lib/test/test_importlib/metadata/test_main.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -138,7 +138,7 @@ def test_unique_distributions(self):
138138
fixtures.build_files(self.make_pkg('abc'), self.site_dir)
139139
before = list(_unique(distributions()))
140140

141-
alt_site_dir = self.fixtures.enter_context(fixtures.tempdir())
141+
alt_site_dir = self.fixtures.enter_context(fixtures.tmp_path())
142142
self.fixtures.enter_context(self.add_sys_path(alt_site_dir))
143143
fixtures.build_files(self.make_pkg('ABC'), alt_site_dir)
144144
after = list(_unique(distributions()))
Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,2 @@
1+
Cleaned up fixtures for importlib.metadata tests and consolidated behavior
2+
with 'test.support.os_helper'.

0 commit comments

Comments
 (0)