Skip to content

Commit 0f89eb3

Browse files
authored
Merge pull request #288 from python/debt/zip-fixtures
Convert static zip fixtures to proper fixtures derived from disk-based fixtures.
2 parents e540919 + d3a7f69 commit 0f89eb3

File tree

10 files changed

+83
-158
lines changed

10 files changed

+83
-158
lines changed

importlib_resources/tests/test_read.py

+2-4
Original file line numberDiff line numberDiff line change
@@ -58,15 +58,13 @@ class ReadDiskTests(ReadTests, unittest.TestCase):
5858

5959
class ReadZipTests(ReadTests, util.ZipSetup, unittest.TestCase):
6060
def test_read_submodule_resource(self):
61-
submodule = import_module('ziptestdata.subdirectory')
61+
submodule = import_module('data01.subdirectory')
6262
result = resources.files(submodule).joinpath('binary.file').read_bytes()
6363
self.assertEqual(result, b'\0\1\2\3')
6464

6565
def test_read_submodule_resource_by_name(self):
6666
result = (
67-
resources.files('ziptestdata.subdirectory')
68-
.joinpath('binary.file')
69-
.read_bytes()
67+
resources.files('data01.subdirectory').joinpath('binary.file').read_bytes()
7068
)
7169
self.assertEqual(result, b'\0\1\2\3')
7270

importlib_resources/tests/test_resource.py

+31-70
Original file line numberDiff line numberDiff line change
@@ -1,15 +1,11 @@
1-
import contextlib
21
import sys
32
import unittest
43
import importlib_resources as resources
5-
import uuid
64
import pathlib
75

86
from . import data01
9-
from . import zipdata01, zipdata02
107
from . import util
118
from importlib import import_module
12-
from ._compat import import_helper, os_helper, unlink
139

1410

1511
class ResourceTests:
@@ -89,136 +85,89 @@ def test_package_has_no_reader_fallback(self):
8985

9086

9187
class ResourceFromZipsTest01(util.ZipSetupBase, unittest.TestCase):
92-
ZIP_MODULE = zipdata01 # type: ignore
88+
ZIP_MODULE = 'data01'
9389

9490
def test_is_submodule_resource(self):
95-
submodule = import_module('ziptestdata.subdirectory')
91+
submodule = import_module('data01.subdirectory')
9692
self.assertTrue(resources.files(submodule).joinpath('binary.file').is_file())
9793

9894
def test_read_submodule_resource_by_name(self):
9995
self.assertTrue(
100-
resources.files('ziptestdata.subdirectory')
101-
.joinpath('binary.file')
102-
.is_file()
96+
resources.files('data01.subdirectory').joinpath('binary.file').is_file()
10397
)
10498

10599
def test_submodule_contents(self):
106-
submodule = import_module('ziptestdata.subdirectory')
100+
submodule = import_module('data01.subdirectory')
107101
self.assertEqual(
108102
names(resources.files(submodule)), {'__init__.py', 'binary.file'}
109103
)
110104

111105
def test_submodule_contents_by_name(self):
112106
self.assertEqual(
113-
names(resources.files('ziptestdata.subdirectory')),
107+
names(resources.files('data01.subdirectory')),
114108
{'__init__.py', 'binary.file'},
115109
)
116110

117111
def test_as_file_directory(self):
118-
with resources.as_file(resources.files('ziptestdata')) as data:
119-
assert data.name == 'ziptestdata'
112+
with resources.as_file(resources.files('data01')) as data:
113+
assert data.name == 'data01'
120114
assert data.is_dir()
121115
assert data.joinpath('subdirectory').is_dir()
122116
assert len(list(data.iterdir()))
123117
assert not data.parent.exists()
124118

125119

126120
class ResourceFromZipsTest02(util.ZipSetupBase, unittest.TestCase):
127-
ZIP_MODULE = zipdata02 # type: ignore
121+
ZIP_MODULE = 'data02'
128122

129123
def test_unrelated_contents(self):
130124
"""
131125
Test thata zip with two unrelated subpackages return
132126
distinct resources. Ref python/importlib_resources#44.
133127
"""
134128
self.assertEqual(
135-
names(resources.files('ziptestdata.one')),
129+
names(resources.files('data02.one')),
136130
{'__init__.py', 'resource1.txt'},
137131
)
138132
self.assertEqual(
139-
names(resources.files('ziptestdata.two')),
133+
names(resources.files('data02.two')),
140134
{'__init__.py', 'resource2.txt'},
141135
)
142136

143137

144-
@contextlib.contextmanager
145-
def zip_on_path(dir):
146-
data_path = pathlib.Path(zipdata01.__file__)
147-
source_zip_path = data_path.parent.joinpath('ziptestdata.zip')
148-
zip_path = pathlib.Path(dir) / f'{uuid.uuid4()}.zip'
149-
zip_path.write_bytes(source_zip_path.read_bytes())
150-
sys.path.append(str(zip_path))
151-
import_module('ziptestdata')
152-
153-
try:
154-
yield
155-
finally:
156-
with contextlib.suppress(ValueError):
157-
sys.path.remove(str(zip_path))
158-
159-
with contextlib.suppress(KeyError):
160-
del sys.path_importer_cache[str(zip_path)]
161-
del sys.modules['ziptestdata']
162-
163-
with contextlib.suppress(OSError):
164-
unlink(zip_path)
165-
166-
167-
class DeletingZipsTest(unittest.TestCase):
138+
class DeletingZipsTest(util.ZipSetupBase, unittest.TestCase):
168139
"""Having accessed resources in a zip file should not keep an open
169140
reference to the zip.
170141
"""
171142

172-
def setUp(self):
173-
self.fixtures = contextlib.ExitStack()
174-
self.addCleanup(self.fixtures.close)
175-
176-
modules = import_helper.modules_setup()
177-
self.addCleanup(import_helper.modules_cleanup, *modules)
178-
179-
temp_dir = self.fixtures.enter_context(os_helper.temp_dir())
180-
self.fixtures.enter_context(zip_on_path(temp_dir))
181-
182143
def test_iterdir_does_not_keep_open(self):
183-
[item.name for item in resources.files('ziptestdata').iterdir()]
144+
[item.name for item in resources.files('data01').iterdir()]
184145

185146
def test_is_file_does_not_keep_open(self):
186-
resources.files('ziptestdata').joinpath('binary.file').is_file()
147+
resources.files('data01').joinpath('binary.file').is_file()
187148

188149
def test_is_file_failure_does_not_keep_open(self):
189-
resources.files('ziptestdata').joinpath('not-present').is_file()
150+
resources.files('data01').joinpath('not-present').is_file()
190151

191152
@unittest.skip("Desired but not supported.")
192153
def test_as_file_does_not_keep_open(self): # pragma: no cover
193-
resources.as_file(resources.files('ziptestdata') / 'binary.file')
154+
resources.as_file(resources.files('data01') / 'binary.file')
194155

195156
def test_entered_path_does_not_keep_open(self):
196157
"""
197158
Mimic what certifi does on import to make its bundle
198159
available for the process duration.
199160
"""
200-
resources.as_file(resources.files('ziptestdata') / 'binary.file').__enter__()
161+
resources.as_file(resources.files('data01') / 'binary.file').__enter__()
201162

202163
def test_read_binary_does_not_keep_open(self):
203-
resources.files('ziptestdata').joinpath('binary.file').read_bytes()
164+
resources.files('data01').joinpath('binary.file').read_bytes()
204165

205166
def test_read_text_does_not_keep_open(self):
206-
resources.files('ziptestdata').joinpath('utf-8.file').read_text(
207-
encoding='utf-8'
208-
)
209-
167+
resources.files('data01').joinpath('utf-8.file').read_text(encoding='utf-8')
210168

211-
class ResourceFromNamespaceTest01(unittest.TestCase):
212-
site_dir = str(pathlib.Path(__file__).parent)
213-
214-
@classmethod
215-
def setUpClass(cls):
216-
sys.path.append(cls.site_dir)
217-
218-
@classmethod
219-
def tearDownClass(cls):
220-
sys.path.remove(cls.site_dir)
221169

170+
class ResourceFromNamespaceTests:
222171
def test_is_submodule_resource(self):
223172
self.assertTrue(
224173
resources.files(import_module('namespacedata01'))
@@ -248,5 +197,17 @@ def test_submodule_contents_by_name(self):
248197
self.assertEqual(contents, {'binary.file', 'utf-8.file', 'utf-16.file'})
249198

250199

200+
class ResourceFromNamespaceDiskTests(ResourceFromNamespaceTests, unittest.TestCase):
201+
site_dir = str(pathlib.Path(__file__).parent)
202+
203+
@classmethod
204+
def setUpClass(cls):
205+
sys.path.append(cls.site_dir)
206+
207+
@classmethod
208+
def tearDownClass(cls):
209+
sys.path.remove(cls.site_dir)
210+
211+
251212
if __name__ == '__main__':
252213
unittest.main()

importlib_resources/tests/update-zips.py

-53
This file was deleted.

importlib_resources/tests/util.py

+17-31
Original file line numberDiff line numberDiff line change
@@ -4,11 +4,12 @@
44
import sys
55
import types
66
import pathlib
7+
import contextlib
78

89
from . import data01
9-
from . import zipdata01
1010
from ..abc import ResourceReader
11-
from ._compat import import_helper
11+
from ._compat import import_helper, os_helper
12+
from . import zip as zip_
1213

1314

1415
from importlib.machinery import ModuleSpec
@@ -141,39 +142,24 @@ def test_useless_loader(self):
141142

142143

143144
class ZipSetupBase:
144-
ZIP_MODULE = None
145-
146-
@classmethod
147-
def setUpClass(cls):
148-
data_path = pathlib.Path(cls.ZIP_MODULE.__file__)
149-
data_dir = data_path.parent
150-
cls._zip_path = str(data_dir / 'ziptestdata.zip')
151-
sys.path.append(cls._zip_path)
152-
cls.data = importlib.import_module('ziptestdata')
153-
154-
@classmethod
155-
def tearDownClass(cls):
156-
try:
157-
sys.path.remove(cls._zip_path)
158-
except ValueError:
159-
pass
160-
161-
try:
162-
del sys.path_importer_cache[cls._zip_path]
163-
del sys.modules[cls.data.__name__]
164-
except KeyError:
165-
pass
166-
167-
try:
168-
del cls.data
169-
del cls._zip_path
170-
except AttributeError:
171-
pass
145+
ZIP_MODULE = 'data01'
172146

173147
def setUp(self):
148+
self.fixtures = contextlib.ExitStack()
149+
self.addCleanup(self.fixtures.close)
150+
174151
modules = import_helper.modules_setup()
175152
self.addCleanup(import_helper.modules_cleanup, *modules)
176153

154+
temp_dir = self.fixtures.enter_context(os_helper.temp_dir())
155+
modules = pathlib.Path(temp_dir) / 'zipped modules.zip'
156+
src_path = pathlib.Path(__file__).parent.joinpath(self.ZIP_MODULE)
157+
self.fixtures.enter_context(
158+
import_helper.DirsOnSysPath(str(zip_.make_zip_file(src_path, modules)))
159+
)
160+
161+
self.data = importlib.import_module(self.ZIP_MODULE)
162+
177163

178164
class ZipSetup(ZipSetupBase):
179-
ZIP_MODULE = zipdata01 # type: ignore
165+
pass

importlib_resources/tests/zip.py

+32
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,32 @@
1+
"""
2+
Generate zip test data files.
3+
"""
4+
5+
import contextlib
6+
import os
7+
import pathlib
8+
import zipfile
9+
10+
import zipp
11+
12+
13+
def make_zip_file(src, dst):
14+
"""
15+
Zip the files in src into a new zipfile at dst.
16+
"""
17+
with zipfile.ZipFile(dst, 'w') as zf:
18+
for src_path, rel in walk(src):
19+
dst_name = src.name / pathlib.PurePosixPath(rel.as_posix())
20+
zf.write(src_path, dst_name)
21+
zipp.CompleteDirs.inject(zf)
22+
return dst
23+
24+
25+
def walk(datapath):
26+
for dirpath, dirnames, filenames in os.walk(datapath):
27+
with contextlib.suppress(ValueError):
28+
dirnames.remove('__pycache__')
29+
for filename in filenames:
30+
res = pathlib.Path(dirpath) / filename
31+
rel = res.relative_to(datapath)
32+
yield res, rel

importlib_resources/tests/zipdata01/__init__.py

Whitespace-only changes.
-876 Bytes
Binary file not shown.

importlib_resources/tests/zipdata02/__init__.py

Whitespace-only changes.
-698 Bytes
Binary file not shown.

setup.cfg

+1
Original file line numberDiff line numberDiff line change
@@ -44,6 +44,7 @@ testing =
4444
pytest-ruff
4545

4646
# local
47+
zipp >= 3.17
4748

4849
docs =
4950
# upstream

0 commit comments

Comments
 (0)