|
1 | | -import contextlib |
2 | 1 | import sys |
3 | 2 | import unittest |
4 | 3 | import importlib_resources as resources |
5 | | -import uuid |
6 | 4 | import pathlib |
7 | 5 |
|
8 | 6 | from . import data01 |
9 | | -from . import zipdata01, zipdata02 |
10 | 7 | from . import util |
11 | 8 | from importlib import import_module |
12 | | -from ._compat import import_helper, os_helper, unlink |
13 | 9 |
|
14 | 10 |
|
15 | 11 | class ResourceTests: |
@@ -89,136 +85,89 @@ def test_package_has_no_reader_fallback(self): |
89 | 85 |
|
90 | 86 |
|
91 | 87 | class ResourceFromZipsTest01(util.ZipSetupBase, unittest.TestCase): |
92 | | - ZIP_MODULE = zipdata01 # type: ignore |
| 88 | + ZIP_MODULE = 'data01' |
93 | 89 |
|
94 | 90 | def test_is_submodule_resource(self): |
95 | | - submodule = import_module('ziptestdata.subdirectory') |
| 91 | + submodule = import_module('data01.subdirectory') |
96 | 92 | self.assertTrue(resources.files(submodule).joinpath('binary.file').is_file()) |
97 | 93 |
|
98 | 94 | def test_read_submodule_resource_by_name(self): |
99 | 95 | self.assertTrue( |
100 | | - resources.files('ziptestdata.subdirectory') |
101 | | - .joinpath('binary.file') |
102 | | - .is_file() |
| 96 | + resources.files('data01.subdirectory').joinpath('binary.file').is_file() |
103 | 97 | ) |
104 | 98 |
|
105 | 99 | def test_submodule_contents(self): |
106 | | - submodule = import_module('ziptestdata.subdirectory') |
| 100 | + submodule = import_module('data01.subdirectory') |
107 | 101 | self.assertEqual( |
108 | 102 | names(resources.files(submodule)), {'__init__.py', 'binary.file'} |
109 | 103 | ) |
110 | 104 |
|
111 | 105 | def test_submodule_contents_by_name(self): |
112 | 106 | self.assertEqual( |
113 | | - names(resources.files('ziptestdata.subdirectory')), |
| 107 | + names(resources.files('data01.subdirectory')), |
114 | 108 | {'__init__.py', 'binary.file'}, |
115 | 109 | ) |
116 | 110 |
|
117 | 111 | 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' |
120 | 114 | assert data.is_dir() |
121 | 115 | assert data.joinpath('subdirectory').is_dir() |
122 | 116 | assert len(list(data.iterdir())) |
123 | 117 | assert not data.parent.exists() |
124 | 118 |
|
125 | 119 |
|
126 | 120 | class ResourceFromZipsTest02(util.ZipSetupBase, unittest.TestCase): |
127 | | - ZIP_MODULE = zipdata02 # type: ignore |
| 121 | + ZIP_MODULE = 'data02' |
128 | 122 |
|
129 | 123 | def test_unrelated_contents(self): |
130 | 124 | """ |
131 | 125 | Test thata zip with two unrelated subpackages return |
132 | 126 | distinct resources. Ref python/importlib_resources#44. |
133 | 127 | """ |
134 | 128 | self.assertEqual( |
135 | | - names(resources.files('ziptestdata.one')), |
| 129 | + names(resources.files('data02.one')), |
136 | 130 | {'__init__.py', 'resource1.txt'}, |
137 | 131 | ) |
138 | 132 | self.assertEqual( |
139 | | - names(resources.files('ziptestdata.two')), |
| 133 | + names(resources.files('data02.two')), |
140 | 134 | {'__init__.py', 'resource2.txt'}, |
141 | 135 | ) |
142 | 136 |
|
143 | 137 |
|
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): |
168 | 139 | """Having accessed resources in a zip file should not keep an open |
169 | 140 | reference to the zip. |
170 | 141 | """ |
171 | 142 |
|
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 | | - |
182 | 143 | 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()] |
184 | 145 |
|
185 | 146 | 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() |
187 | 148 |
|
188 | 149 | 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() |
190 | 151 |
|
191 | 152 | @unittest.skip("Desired but not supported.") |
192 | 153 | 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') |
194 | 155 |
|
195 | 156 | def test_entered_path_does_not_keep_open(self): |
196 | 157 | """ |
197 | 158 | Mimic what certifi does on import to make its bundle |
198 | 159 | available for the process duration. |
199 | 160 | """ |
200 | | - resources.as_file(resources.files('ziptestdata') / 'binary.file').__enter__() |
| 161 | + resources.as_file(resources.files('data01') / 'binary.file').__enter__() |
201 | 162 |
|
202 | 163 | 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() |
204 | 165 |
|
205 | 166 | 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') |
210 | 168 |
|
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) |
221 | 169 |
|
| 170 | +class ResourceFromNamespaceTests: |
222 | 171 | def test_is_submodule_resource(self): |
223 | 172 | self.assertTrue( |
224 | 173 | resources.files(import_module('namespacedata01')) |
@@ -248,5 +197,17 @@ def test_submodule_contents_by_name(self): |
248 | 197 | self.assertEqual(contents, {'binary.file', 'utf-8.file', 'utf-16.file'}) |
249 | 198 |
|
250 | 199 |
|
| 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 | + |
251 | 212 | if __name__ == '__main__': |
252 | 213 | unittest.main() |
0 commit comments