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

These changes released with importlib_metadata 7.2.0.
This commit is contained in:
Jason R. Coombs 2024-06-20 15:00:39 -04:00 committed by GitHub
parent c1553bc34a
commit 85d90b59e2
No known key found for this signature in database
GPG key ID: B5690EEEBB952194
4 changed files with 11 additions and 28 deletions

View file

@ -1,10 +1,8 @@
import os
import sys import sys
import copy import copy
import json import json
import shutil import shutil
import pathlib import pathlib
import tempfile
import textwrap import textwrap
import functools import functools
import contextlib import contextlib
@ -27,29 +25,12 @@ except (ImportError, AttributeError):
@contextlib.contextmanager @contextlib.contextmanager
def tempdir(): def tmp_path():
tmpdir = tempfile.mkdtemp() """
try: Like os_helper.temp_dir, but yields a pathlib.Path.
yield pathlib.Path(tmpdir) """
finally: with os_helper.temp_dir() as path:
shutil.rmtree(tmpdir) yield pathlib.Path(path)
@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
@contextlib.contextmanager @contextlib.contextmanager
@ -70,7 +51,7 @@ class Fixtures:
class SiteDir(Fixtures): class SiteDir(Fixtures):
def setUp(self): def setUp(self):
super().setUp() super().setUp()
self.site_dir = self.fixtures.enter_context(tempdir()) self.site_dir = self.fixtures.enter_context(tmp_path())
class OnSysPath(Fixtures): class OnSysPath(Fixtures):

View file

@ -109,7 +109,7 @@ class APITests(
Entry points should only be exposed for the first package Entry points should only be exposed for the first package
on sys.path with a given name (even when normalized). 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)) self.fixtures.enter_context(self.add_sys_path(alt_site_dir))
alt_pkg = { alt_pkg = {
"DistInfo_pkg-1.1.0.dist-info": { "DistInfo_pkg-1.1.0.dist-info": {

View file

@ -138,7 +138,7 @@ class NameNormalizationTests(fixtures.OnSysPath, fixtures.SiteDir, unittest.Test
fixtures.build_files(self.make_pkg('abc'), self.site_dir) fixtures.build_files(self.make_pkg('abc'), self.site_dir)
before = list(_unique(distributions())) 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)) self.fixtures.enter_context(self.add_sys_path(alt_site_dir))
fixtures.build_files(self.make_pkg('ABC'), alt_site_dir) fixtures.build_files(self.make_pkg('ABC'), alt_site_dir)
after = list(_unique(distributions())) after = list(_unique(distributions()))

View file

@ -0,0 +1,2 @@
Cleaned up fixtures for importlib.metadata tests and consolidated behavior
with 'test.support.os_helper'.