Skip to content

Commit 4717d80

Browse files
committed
Prefer importlib_resources 1.3 or later for loading tests. Closes #123.
1 parent ee3bed1 commit 4717d80

File tree

2 files changed

+15
-12
lines changed

2 files changed

+15
-12
lines changed

importlib_metadata/tests/test_zip.py

Lines changed: 14 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -7,9 +7,11 @@
77
)
88

99
try:
10-
from importlib.resources import path
11-
except ImportError:
12-
from importlib_resources import path
10+
from importlib import resources
11+
getattr(resources, 'files')
12+
getattr(resources, 'as_file')
13+
except AttributeError:
14+
import importlib_resources as resources
1315

1416
try:
1517
from contextlib import ExitStack
@@ -20,15 +22,19 @@
2022
class TestZip(unittest.TestCase):
2123
root = 'importlib_metadata.tests.data'
2224

25+
def _fixture_on_path(self, filename):
26+
pkg_file = resources.files(self.root).joinpath(filename)
27+
file = self.resources.enter_context(resources.as_file(pkg_file))
28+
assert file.name.startswith('example-'), file.name
29+
sys.path.insert(0, str(file))
30+
self.resources.callback(sys.path.pop, 0)
31+
2332
def setUp(self):
2433
# Find the path to the example-*.whl so we can add it to the front of
2534
# sys.path, where we'll then try to find the metadata thereof.
2635
self.resources = ExitStack()
2736
self.addCleanup(self.resources.close)
28-
wheel = self.resources.enter_context(
29-
path(self.root, 'example-21.12-py3-none-any.whl'))
30-
sys.path.insert(0, str(wheel))
31-
self.resources.callback(sys.path.pop, 0)
37+
self._fixture_on_path('example-21.12-py3-none-any.whl')
3238

3339
def test_zip_version(self):
3440
self.assertEqual(version('example'), '21.12')
@@ -66,10 +72,7 @@ def setUp(self):
6672
# sys.path, where we'll then try to find the metadata thereof.
6773
self.resources = ExitStack()
6874
self.addCleanup(self.resources.close)
69-
egg = self.resources.enter_context(
70-
path(self.root, 'example-21.12-py3.6.egg'))
71-
sys.path.insert(0, str(egg))
72-
self.resources.callback(sys.path.pop, 0)
75+
self._fixture_on_path('example-21.12-py3.6.egg')
7376

7477
def test_files(self):
7578
for file in files('example'):

setup.cfg

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -53,7 +53,7 @@ universal=1
5353

5454
[options.extras_require]
5555
testing =
56-
importlib_resources; python_version < "3.7"
56+
importlib_resources>=1.3; python_version < "3.9"
5757
packaging
5858
docs =
5959
sphinx

0 commit comments

Comments
 (0)