Skip to content

gh-120801: Refactor importlib.metadata fixtures. #120803

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 2 commits into from
Jun 20, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
33 changes: 7 additions & 26 deletions Lib/test/test_importlib/metadata/fixtures.py
Original file line number Diff line number Diff line change
@@ -1,10 +1,8 @@
import os
import sys
import copy
import json
import shutil
import pathlib
import tempfile
import textwrap
import functools
import contextlib
Expand All @@ -27,29 +25,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
Expand All @@ -70,7 +51,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):
Expand Down
2 changes: 1 addition & 1 deletion Lib/test/test_importlib/metadata/test_api.py
Original file line number Diff line number Diff line change
Expand Up @@ -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": {
Expand Down
2 changes: 1 addition & 1 deletion Lib/test/test_importlib/metadata/test_main.py
Original file line number Diff line number Diff line change
Expand Up @@ -138,7 +138,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()))
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
Cleaned up fixtures for importlib.metadata tests and consolidated behavior
with 'test.support.os_helper'.
Loading