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