Skip to content

Commit 6ffdcb9

Browse files
committed
Extract ZipFixtures
1 parent 08d7def commit 6ffdcb9

File tree

2 files changed

+29
-31
lines changed

2 files changed

+29
-31
lines changed

tests/fixtures.py

Lines changed: 25 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -10,6 +10,14 @@
1010
from .py39compat import FS_NONASCII
1111
from typing import Dict, Union
1212

13+
try:
14+
from importlib import resources
15+
16+
getattr(resources, 'files')
17+
getattr(resources, 'as_file')
18+
except (ImportError, AttributeError):
19+
import importlib_resources as resources # type: ignore
20+
1321

1422
@contextlib.contextmanager
1523
def tempdir():
@@ -285,3 +293,20 @@ def DALS(str):
285293
class NullFinder:
286294
def find_module(self, name):
287295
pass
296+
297+
298+
class ZipFixtures:
299+
root = 'tests.data'
300+
301+
def _fixture_on_path(self, filename):
302+
pkg_file = resources.files(self.root).joinpath(filename)
303+
file = self.resources.enter_context(resources.as_file(pkg_file))
304+
assert file.name.startswith('example-'), file.name
305+
sys.path.insert(0, str(file))
306+
self.resources.callback(sys.path.pop, 0)
307+
308+
def setUp(self):
309+
# Add self.zip_name to the front of sys.path.
310+
self.resources = contextlib.ExitStack()
311+
self.addCleanup(self.resources.close)
312+
self._fixture_on_path(self.zip_name)

tests/test_zip.py

Lines changed: 4 additions & 31 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
import sys
22
import unittest
33

4-
from contextlib import ExitStack
4+
from . import fixtures
55
from importlib_metadata import (
66
PackageNotFoundError,
77
distribution,
@@ -11,31 +11,9 @@
1111
version,
1212
)
1313

14-
try:
15-
from importlib import resources
1614

17-
getattr(resources, 'files')
18-
getattr(resources, 'as_file')
19-
except (ImportError, AttributeError):
20-
import importlib_resources as resources # type: ignore
21-
22-
23-
class TestZip(unittest.TestCase):
24-
root = 'tests.data'
25-
26-
def _fixture_on_path(self, filename):
27-
pkg_file = resources.files(self.root).joinpath(filename)
28-
file = self.resources.enter_context(resources.as_file(pkg_file))
29-
assert file.name.startswith('example-'), file.name
30-
sys.path.insert(0, str(file))
31-
self.resources.callback(sys.path.pop, 0)
32-
33-
def setUp(self):
34-
# Find the path to the example-*.whl so we can add it to the front of
35-
# sys.path, where we'll then try to find the metadata thereof.
36-
self.resources = ExitStack()
37-
self.addCleanup(self.resources.close)
38-
self._fixture_on_path('example-21.12-py3-none-any.whl')
15+
class TestZip(fixtures.ZipFixtures, unittest.TestCase):
16+
zip_name = 'example-21.12-py3-none-any.whl'
3917

4018
def test_zip_version(self):
4119
self.assertEqual(version('example'), '21.12')
@@ -68,12 +46,7 @@ def test_one_distribution(self):
6846

6947

7048
class TestEgg(TestZip):
71-
def setUp(self):
72-
# Find the path to the example-*.egg so we can add it to the front of
73-
# sys.path, where we'll then try to find the metadata thereof.
74-
self.resources = ExitStack()
75-
self.addCleanup(self.resources.close)
76-
self._fixture_on_path('example-21.12-py3.6.egg')
49+
zip_name = 'example-21.12-py3.6.egg'
7750

7851
def test_files(self):
7952
for file in files('example'):

0 commit comments

Comments
 (0)