|
7 | 7 | ) |
8 | 8 |
|
9 | 9 | 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 |
13 | 15 |
|
14 | 16 | try: |
15 | 17 | from contextlib import ExitStack |
|
20 | 22 | class TestZip(unittest.TestCase): |
21 | 23 | root = 'importlib_metadata.tests.data' |
22 | 24 |
|
| 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 | + |
23 | 32 | def setUp(self): |
24 | 33 | # Find the path to the example-*.whl so we can add it to the front of |
25 | 34 | # sys.path, where we'll then try to find the metadata thereof. |
26 | 35 | self.resources = ExitStack() |
27 | 36 | 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') |
32 | 38 |
|
33 | 39 | def test_zip_version(self): |
34 | 40 | self.assertEqual(version('example'), '21.12') |
@@ -66,10 +72,7 @@ def setUp(self): |
66 | 72 | # sys.path, where we'll then try to find the metadata thereof. |
67 | 73 | self.resources = ExitStack() |
68 | 74 | 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') |
73 | 76 |
|
74 | 77 | def test_files(self): |
75 | 78 | for file in files('example'): |
|
0 commit comments