|
1 | 1 | import sys
|
2 | 2 | import unittest
|
3 | 3 | import importlib_resources as resources
|
| 4 | +import uuid |
| 5 | + |
| 6 | +from importlib_resources._compat import Path |
4 | 7 |
|
5 | 8 | from . import data01
|
6 | 9 | from . import zipdata01, zipdata02
|
7 | 10 | from . import util
|
8 | 11 | from importlib import import_module
|
| 12 | +from ._compat import import_helper, unlink |
9 | 13 |
|
10 | 14 |
|
11 | 15 | class ResourceTests:
|
@@ -166,5 +170,72 @@ def test_namespaces_cannot_have_resources(self):
|
166 | 170 | 'importlib_resources.tests.data03.namespace', 'resource1.txt')
|
167 | 171 |
|
168 | 172 |
|
| 173 | +class DeletingZipsTest(unittest.TestCase): |
| 174 | + """Having accessed resources in a zip file should not keep an open |
| 175 | + reference to the zip. |
| 176 | + """ |
| 177 | + ZIP_MODULE = zipdata01 |
| 178 | + |
| 179 | + def setUp(self): |
| 180 | + modules = import_helper.modules_setup() |
| 181 | + self.addCleanup(import_helper.modules_cleanup, *modules) |
| 182 | + |
| 183 | + data_path = Path(self.ZIP_MODULE.__file__) |
| 184 | + data_dir = data_path.parent |
| 185 | + self.source_zip_path = data_dir / 'ziptestdata.zip' |
| 186 | + self.zip_path = Path.cwd() / '{}.zip'.format(uuid.uuid4()) |
| 187 | + self.zip_path.write_bytes(self.source_zip_path.read_bytes()) |
| 188 | + sys.path.append(str(self.zip_path)) |
| 189 | + self.data = import_module('ziptestdata') |
| 190 | + |
| 191 | + def tearDown(self): |
| 192 | + try: |
| 193 | + sys.path.remove(str(self.zip_path)) |
| 194 | + except ValueError: |
| 195 | + pass |
| 196 | + |
| 197 | + try: |
| 198 | + del sys.path_importer_cache[str(self.zip_path)] |
| 199 | + del sys.modules[self.data.__name__] |
| 200 | + except KeyError: |
| 201 | + pass |
| 202 | + |
| 203 | + try: |
| 204 | + unlink(self.zip_path) |
| 205 | + except OSError: |
| 206 | + # If the test fails, this will probably fail too |
| 207 | + pass |
| 208 | + |
| 209 | + def test_contents_does_not_keep_open(self): |
| 210 | + c = resources.contents('ziptestdata') |
| 211 | + self.zip_path.unlink() |
| 212 | + |
| 213 | + def test_is_resource_does_not_keep_open(self): |
| 214 | + c = resources.is_resource('ziptestdata', 'binary.file') |
| 215 | + self.zip_path.unlink() |
| 216 | + |
| 217 | + def test_is_resource_failure_does_not_keep_open(self): |
| 218 | + c = resources.is_resource('ziptestdata', 'not-present') |
| 219 | + self.zip_path.unlink() |
| 220 | + |
| 221 | + def test_path_does_not_keep_open(self): |
| 222 | + c = resources.path('ziptestdata', 'binary.file') |
| 223 | + self.zip_path.unlink() |
| 224 | + |
| 225 | + def test_entered_path_does_not_keep_open(self): |
| 226 | + # This is what certifi does on import to make its bundle |
| 227 | + # available for the process duration. |
| 228 | + c = resources.path('ziptestdata', 'binary.file').__enter__() |
| 229 | + self.zip_path.unlink() |
| 230 | + |
| 231 | + def test_read_binary_does_not_keep_open(self): |
| 232 | + c = resources.read_binary('ziptestdata', 'binary.file') |
| 233 | + self.zip_path.unlink() |
| 234 | + |
| 235 | + def test_read_text_does_not_keep_open(self): |
| 236 | + c = resources.read_text('ziptestdata', 'utf-8.file', encoding='utf-8') |
| 237 | + self.zip_path.unlink() |
| 238 | + |
| 239 | + |
169 | 240 | if __name__ == '__main__':
|
170 | 241 | unittest.main()
|
0 commit comments