| 
									
										
										
										
											2023-02-18 16:29:22 -05:00
										 |  |  | import contextlib | 
					
						
							| 
									
										
										
										
											2017-12-30 15:18:06 -05:00
										 |  |  | import sys | 
					
						
							|  |  |  | import unittest | 
					
						
							| 
									
										
										
										
											2020-10-25 14:21:46 -04:00
										 |  |  | import uuid | 
					
						
							|  |  |  | import pathlib | 
					
						
							| 
									
										
										
										
											2017-12-30 15:18:06 -05:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2022-01-22 21:38:26 -05:00
										 |  |  | from . import data01 | 
					
						
							|  |  |  | from . import zipdata01, zipdata02 | 
					
						
							| 
									
										
										
										
											2022-07-24 20:53:10 -04:00
										 |  |  | from . import util | 
					
						
							| 
									
										
										
										
											2018-03-27 09:59:38 -07:00
										 |  |  | from importlib import resources, import_module | 
					
						
							| 
									
										
										
										
											2023-02-18 16:29:22 -05:00
										 |  |  | from test.support import import_helper, os_helper | 
					
						
							| 
									
										
										
										
											2020-10-25 14:21:46 -04:00
										 |  |  | from test.support.os_helper import unlink | 
					
						
							| 
									
										
										
										
											2017-12-30 15:18:06 -05:00
										 |  |  | 
 | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | class ResourceTests: | 
					
						
							|  |  |  |     # Subclasses are expected to set the `data` attribute. | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2021-12-18 21:28:49 -05:00
										 |  |  |     def test_is_file_exists(self): | 
					
						
							|  |  |  |         target = resources.files(self.data) / 'binary.file' | 
					
						
							|  |  |  |         self.assertTrue(target.is_file()) | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |     def test_is_file_missing(self): | 
					
						
							|  |  |  |         target = resources.files(self.data) / 'not-a-file' | 
					
						
							|  |  |  |         self.assertFalse(target.is_file()) | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |     def test_is_dir(self): | 
					
						
							|  |  |  |         target = resources.files(self.data) / 'subdirectory' | 
					
						
							|  |  |  |         self.assertFalse(target.is_file()) | 
					
						
							|  |  |  |         self.assertTrue(target.is_dir()) | 
					
						
							| 
									
										
										
										
											2017-12-30 15:18:06 -05:00
										 |  |  | 
 | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | class ResourceDiskTests(ResourceTests, unittest.TestCase): | 
					
						
							|  |  |  |     def setUp(self): | 
					
						
							|  |  |  |         self.data = data01 | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | class ResourceZipTests(ResourceTests, util.ZipSetup, unittest.TestCase): | 
					
						
							|  |  |  |     pass | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2021-12-18 21:28:49 -05:00
										 |  |  | def names(traversable): | 
					
						
							|  |  |  |     return {item.name for item in traversable.iterdir()} | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2017-12-30 15:18:06 -05:00
										 |  |  | class ResourceLoaderTests(unittest.TestCase): | 
					
						
							|  |  |  |     def test_resource_contents(self): | 
					
						
							|  |  |  |         package = util.create_package( | 
					
						
							| 
									
										
										
										
											2021-03-04 13:43:00 -05:00
										 |  |  |             file=data01, path=data01.__file__, contents=['A', 'B', 'C'] | 
					
						
							|  |  |  |         ) | 
					
						
							| 
									
										
										
										
											2021-12-18 21:28:49 -05:00
										 |  |  |         self.assertEqual(names(resources.files(package)), {'A', 'B', 'C'}) | 
					
						
							| 
									
										
										
										
											2017-12-30 15:18:06 -05:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2021-12-18 21:28:49 -05:00
										 |  |  |     def test_is_file(self): | 
					
						
							| 
									
										
										
										
											2017-12-30 15:18:06 -05:00
										 |  |  |         package = util.create_package( | 
					
						
							| 
									
										
										
										
											2021-03-04 13:43:00 -05:00
										 |  |  |             file=data01, path=data01.__file__, contents=['A', 'B', 'C', 'D/E', 'D/F'] | 
					
						
							|  |  |  |         ) | 
					
						
							| 
									
										
										
										
											2021-12-18 21:28:49 -05:00
										 |  |  |         self.assertTrue(resources.files(package).joinpath('B').is_file()) | 
					
						
							| 
									
										
										
										
											2017-12-30 15:18:06 -05:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2021-12-18 21:28:49 -05:00
										 |  |  |     def test_is_dir(self): | 
					
						
							| 
									
										
										
										
											2017-12-30 15:18:06 -05:00
										 |  |  |         package = util.create_package( | 
					
						
							| 
									
										
										
										
											2021-03-04 13:43:00 -05:00
										 |  |  |             file=data01, path=data01.__file__, contents=['A', 'B', 'C', 'D/E', 'D/F'] | 
					
						
							|  |  |  |         ) | 
					
						
							| 
									
										
										
										
											2021-12-18 21:28:49 -05:00
										 |  |  |         self.assertTrue(resources.files(package).joinpath('D').is_dir()) | 
					
						
							| 
									
										
										
										
											2017-12-30 15:18:06 -05:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2021-12-18 21:28:49 -05:00
										 |  |  |     def test_resource_missing(self): | 
					
						
							| 
									
										
										
										
											2017-12-30 15:18:06 -05:00
										 |  |  |         package = util.create_package( | 
					
						
							| 
									
										
										
										
											2021-03-04 13:43:00 -05:00
										 |  |  |             file=data01, path=data01.__file__, contents=['A', 'B', 'C', 'D/E', 'D/F'] | 
					
						
							|  |  |  |         ) | 
					
						
							| 
									
										
										
										
											2021-12-18 21:28:49 -05:00
										 |  |  |         self.assertFalse(resources.files(package).joinpath('Z').is_file()) | 
					
						
							| 
									
										
										
										
											2017-12-30 15:18:06 -05:00
										 |  |  | 
 | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | class ResourceCornerCaseTests(unittest.TestCase): | 
					
						
							|  |  |  |     def test_package_has_no_reader_fallback(self): | 
					
						
							| 
									
										
										
										
											2023-02-18 16:29:22 -05:00
										 |  |  |         """
 | 
					
						
							|  |  |  |         Test odd ball packages which: | 
					
						
							| 
									
										
										
										
											2017-12-30 15:18:06 -05:00
										 |  |  |         # 1. Do not have a ResourceReader as a loader | 
					
						
							|  |  |  |         # 2. Are not on the file system | 
					
						
							|  |  |  |         # 3. Are not in a zip file | 
					
						
							| 
									
										
										
										
											2023-02-18 16:29:22 -05:00
										 |  |  |         """
 | 
					
						
							| 
									
										
										
										
											2017-12-30 15:18:06 -05:00
										 |  |  |         module = util.create_package( | 
					
						
							| 
									
										
										
										
											2021-03-04 13:43:00 -05:00
										 |  |  |             file=data01, path=data01.__file__, contents=['A', 'B', 'C'] | 
					
						
							|  |  |  |         ) | 
					
						
							| 
									
										
										
										
											2017-12-30 15:18:06 -05:00
										 |  |  |         # Give the module a dummy loader. | 
					
						
							|  |  |  |         module.__loader__ = object() | 
					
						
							|  |  |  |         # Give the module a dummy origin. | 
					
						
							|  |  |  |         module.__file__ = '/path/which/shall/not/be/named' | 
					
						
							| 
									
										
										
										
											2021-03-04 13:43:00 -05:00
										 |  |  |         module.__spec__.loader = module.__loader__ | 
					
						
							|  |  |  |         module.__spec__.origin = module.__file__ | 
					
						
							| 
									
										
										
										
											2021-12-18 21:28:49 -05:00
										 |  |  |         self.assertFalse(resources.files(module).joinpath('A').is_file()) | 
					
						
							| 
									
										
										
										
											2017-12-30 15:18:06 -05:00
										 |  |  | 
 | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2021-03-04 13:43:00 -05:00
										 |  |  | class ResourceFromZipsTest01(util.ZipSetupBase, unittest.TestCase): | 
					
						
							|  |  |  |     ZIP_MODULE = zipdata01  # type: ignore | 
					
						
							| 
									
										
										
										
											2018-06-05 09:40:45 -07:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2018-03-27 09:59:38 -07:00
										 |  |  |     def test_is_submodule_resource(self): | 
					
						
							|  |  |  |         submodule = import_module('ziptestdata.subdirectory') | 
					
						
							| 
									
										
										
										
											2021-12-18 21:28:49 -05:00
										 |  |  |         self.assertTrue(resources.files(submodule).joinpath('binary.file').is_file()) | 
					
						
							| 
									
										
										
										
											2018-03-27 09:59:38 -07:00
										 |  |  | 
 | 
					
						
							|  |  |  |     def test_read_submodule_resource_by_name(self): | 
					
						
							| 
									
										
										
										
											2021-12-18 21:28:49 -05:00
										 |  |  |         self.assertTrue( | 
					
						
							|  |  |  |             resources.files('ziptestdata.subdirectory') | 
					
						
							|  |  |  |             .joinpath('binary.file') | 
					
						
							|  |  |  |             .is_file() | 
					
						
							|  |  |  |         ) | 
					
						
							| 
									
										
										
										
											2018-03-27 09:59:38 -07:00
										 |  |  | 
 | 
					
						
							|  |  |  |     def test_submodule_contents(self): | 
					
						
							|  |  |  |         submodule = import_module('ziptestdata.subdirectory') | 
					
						
							| 
									
										
										
										
											2021-12-18 21:28:49 -05:00
										 |  |  |         self.assertEqual( | 
					
						
							|  |  |  |             names(resources.files(submodule)), {'__init__.py', 'binary.file'} | 
					
						
							|  |  |  |         ) | 
					
						
							| 
									
										
										
										
											2018-03-27 09:59:38 -07:00
										 |  |  | 
 | 
					
						
							|  |  |  |     def test_submodule_contents_by_name(self): | 
					
						
							| 
									
										
										
										
											2021-12-18 21:28:49 -05:00
										 |  |  |         self.assertEqual( | 
					
						
							|  |  |  |             names(resources.files('ziptestdata.subdirectory')), | 
					
						
							|  |  |  |             {'__init__.py', 'binary.file'}, | 
					
						
							|  |  |  |         ) | 
					
						
							| 
									
										
										
										
											2021-03-04 13:43:00 -05:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2022-10-16 15:00:39 -04:00
										 |  |  |     def test_as_file_directory(self): | 
					
						
							|  |  |  |         with resources.as_file(resources.files('ziptestdata')) as data: | 
					
						
							|  |  |  |             assert data.name == 'ziptestdata' | 
					
						
							|  |  |  |             assert data.is_dir() | 
					
						
							|  |  |  |             assert data.joinpath('subdirectory').is_dir() | 
					
						
							|  |  |  |             assert len(list(data.iterdir())) | 
					
						
							|  |  |  |         assert not data.parent.exists() | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2021-03-04 13:43:00 -05:00
										 |  |  | 
 | 
					
						
							|  |  |  | class ResourceFromZipsTest02(util.ZipSetupBase, unittest.TestCase): | 
					
						
							|  |  |  |     ZIP_MODULE = zipdata02  # type: ignore | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |     def test_unrelated_contents(self): | 
					
						
							|  |  |  |         """
 | 
					
						
							|  |  |  |         Test thata zip with two unrelated subpackages return | 
					
						
							|  |  |  |         distinct resources. Ref python/importlib_resources#44. | 
					
						
							|  |  |  |         """
 | 
					
						
							| 
									
										
										
										
											2021-12-18 21:28:49 -05:00
										 |  |  |         self.assertEqual( | 
					
						
							|  |  |  |             names(resources.files('ziptestdata.one')), | 
					
						
							|  |  |  |             {'__init__.py', 'resource1.txt'}, | 
					
						
							|  |  |  |         ) | 
					
						
							|  |  |  |         self.assertEqual( | 
					
						
							|  |  |  |             names(resources.files('ziptestdata.two')), | 
					
						
							|  |  |  |             {'__init__.py', 'resource2.txt'}, | 
					
						
							|  |  |  |         ) | 
					
						
							| 
									
										
										
										
											2017-12-30 15:18:06 -05:00
										 |  |  | 
 | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2023-02-18 16:29:22 -05:00
										 |  |  | @contextlib.contextmanager | 
					
						
							|  |  |  | def zip_on_path(dir): | 
					
						
							|  |  |  |     data_path = pathlib.Path(zipdata01.__file__) | 
					
						
							|  |  |  |     source_zip_path = data_path.parent.joinpath('ziptestdata.zip') | 
					
						
							|  |  |  |     zip_path = pathlib.Path(dir) / f'{uuid.uuid4()}.zip' | 
					
						
							|  |  |  |     zip_path.write_bytes(source_zip_path.read_bytes()) | 
					
						
							|  |  |  |     sys.path.append(str(zip_path)) | 
					
						
							|  |  |  |     import_module('ziptestdata') | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |     try: | 
					
						
							|  |  |  |         yield | 
					
						
							|  |  |  |     finally: | 
					
						
							|  |  |  |         with contextlib.suppress(ValueError): | 
					
						
							|  |  |  |             sys.path.remove(str(zip_path)) | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |         with contextlib.suppress(KeyError): | 
					
						
							|  |  |  |             del sys.path_importer_cache[str(zip_path)] | 
					
						
							|  |  |  |             del sys.modules['ziptestdata'] | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |         with contextlib.suppress(OSError): | 
					
						
							|  |  |  |             unlink(zip_path) | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2020-10-25 14:21:46 -04:00
										 |  |  | class DeletingZipsTest(unittest.TestCase): | 
					
						
							|  |  |  |     """Having accessed resources in a zip file should not keep an open
 | 
					
						
							|  |  |  |     reference to the zip. | 
					
						
							|  |  |  |     """
 | 
					
						
							| 
									
										
										
										
											2021-03-04 13:43:00 -05:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2020-10-25 14:21:46 -04:00
										 |  |  |     def setUp(self): | 
					
						
							| 
									
										
										
										
											2023-02-18 16:29:22 -05:00
										 |  |  |         self.fixtures = contextlib.ExitStack() | 
					
						
							|  |  |  |         self.addCleanup(self.fixtures.close) | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2020-10-25 14:21:46 -04:00
										 |  |  |         modules = import_helper.modules_setup() | 
					
						
							|  |  |  |         self.addCleanup(import_helper.modules_cleanup, *modules) | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2023-02-18 16:29:22 -05:00
										 |  |  |         temp_dir = self.fixtures.enter_context(os_helper.temp_dir()) | 
					
						
							|  |  |  |         self.fixtures.enter_context(zip_on_path(temp_dir)) | 
					
						
							| 
									
										
										
										
											2020-10-25 14:21:46 -04:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2021-12-18 21:28:49 -05:00
										 |  |  |     def test_iterdir_does_not_keep_open(self): | 
					
						
							| 
									
										
										
										
											2023-02-18 16:29:22 -05:00
										 |  |  |         [item.name for item in resources.files('ziptestdata').iterdir()] | 
					
						
							| 
									
										
										
										
											2020-10-25 14:21:46 -04:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2021-12-18 21:28:49 -05:00
										 |  |  |     def test_is_file_does_not_keep_open(self): | 
					
						
							| 
									
										
										
										
											2023-02-18 16:29:22 -05:00
										 |  |  |         resources.files('ziptestdata').joinpath('binary.file').is_file() | 
					
						
							| 
									
										
										
										
											2020-10-25 14:21:46 -04:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2021-12-18 21:28:49 -05:00
										 |  |  |     def test_is_file_failure_does_not_keep_open(self): | 
					
						
							| 
									
										
										
										
											2023-02-18 16:29:22 -05:00
										 |  |  |         resources.files('ziptestdata').joinpath('not-present').is_file() | 
					
						
							| 
									
										
										
										
											2020-10-25 14:21:46 -04:00
										 |  |  | 
 | 
					
						
							|  |  |  |     @unittest.skip("Desired but not supported.") | 
					
						
							| 
									
										
										
										
											2021-12-18 21:28:49 -05:00
										 |  |  |     def test_as_file_does_not_keep_open(self):  # pragma: no cover | 
					
						
							| 
									
										
										
										
											2023-02-18 16:29:22 -05:00
										 |  |  |         resources.as_file(resources.files('ziptestdata') / 'binary.file') | 
					
						
							| 
									
										
										
										
											2020-10-25 14:21:46 -04:00
										 |  |  | 
 | 
					
						
							|  |  |  |     def test_entered_path_does_not_keep_open(self): | 
					
						
							| 
									
										
										
										
											2023-02-18 16:29:22 -05:00
										 |  |  |         """
 | 
					
						
							|  |  |  |         Mimic what certifi does on import to make its bundle | 
					
						
							|  |  |  |         available for the process duration. | 
					
						
							|  |  |  |         """
 | 
					
						
							|  |  |  |         resources.as_file(resources.files('ziptestdata') / 'binary.file').__enter__() | 
					
						
							| 
									
										
										
										
											2020-10-25 14:21:46 -04:00
										 |  |  | 
 | 
					
						
							|  |  |  |     def test_read_binary_does_not_keep_open(self): | 
					
						
							| 
									
										
										
										
											2023-02-18 16:29:22 -05:00
										 |  |  |         resources.files('ziptestdata').joinpath('binary.file').read_bytes() | 
					
						
							| 
									
										
										
										
											2020-10-25 14:21:46 -04:00
										 |  |  | 
 | 
					
						
							|  |  |  |     def test_read_text_does_not_keep_open(self): | 
					
						
							| 
									
										
										
										
											2023-02-18 16:29:22 -05:00
										 |  |  |         resources.files('ziptestdata').joinpath('utf-8.file').read_text( | 
					
						
							|  |  |  |             encoding='utf-8' | 
					
						
							|  |  |  |         ) | 
					
						
							| 
									
										
										
										
											2020-10-25 14:21:46 -04:00
										 |  |  | 
 | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2021-03-04 13:43:00 -05:00
										 |  |  | class ResourceFromNamespaceTest01(unittest.TestCase): | 
					
						
							|  |  |  |     site_dir = str(pathlib.Path(__file__).parent) | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |     @classmethod | 
					
						
							|  |  |  |     def setUpClass(cls): | 
					
						
							|  |  |  |         sys.path.append(cls.site_dir) | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |     @classmethod | 
					
						
							|  |  |  |     def tearDownClass(cls): | 
					
						
							|  |  |  |         sys.path.remove(cls.site_dir) | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |     def test_is_submodule_resource(self): | 
					
						
							| 
									
										
										
										
											2021-12-18 21:28:49 -05:00
										 |  |  |         self.assertTrue( | 
					
						
							|  |  |  |             resources.files(import_module('namespacedata01')) | 
					
						
							|  |  |  |             .joinpath('binary.file') | 
					
						
							|  |  |  |             .is_file() | 
					
						
							|  |  |  |         ) | 
					
						
							| 
									
										
										
										
											2021-03-04 13:43:00 -05:00
										 |  |  | 
 | 
					
						
							|  |  |  |     def test_read_submodule_resource_by_name(self): | 
					
						
							| 
									
										
										
										
											2021-12-18 21:28:49 -05:00
										 |  |  |         self.assertTrue( | 
					
						
							|  |  |  |             resources.files('namespacedata01').joinpath('binary.file').is_file() | 
					
						
							|  |  |  |         ) | 
					
						
							| 
									
										
										
										
											2021-03-04 13:43:00 -05:00
										 |  |  | 
 | 
					
						
							|  |  |  |     def test_submodule_contents(self): | 
					
						
							| 
									
										
										
										
											2021-12-18 21:28:49 -05:00
										 |  |  |         contents = names(resources.files(import_module('namespacedata01'))) | 
					
						
							| 
									
										
										
										
											2021-03-04 13:43:00 -05:00
										 |  |  |         try: | 
					
						
							|  |  |  |             contents.remove('__pycache__') | 
					
						
							|  |  |  |         except KeyError: | 
					
						
							|  |  |  |             pass | 
					
						
							|  |  |  |         self.assertEqual(contents, {'binary.file', 'utf-8.file', 'utf-16.file'}) | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |     def test_submodule_contents_by_name(self): | 
					
						
							| 
									
										
										
										
											2021-12-18 21:28:49 -05:00
										 |  |  |         contents = names(resources.files('namespacedata01')) | 
					
						
							| 
									
										
										
										
											2021-03-04 13:43:00 -05:00
										 |  |  |         try: | 
					
						
							|  |  |  |             contents.remove('__pycache__') | 
					
						
							|  |  |  |         except KeyError: | 
					
						
							|  |  |  |             pass | 
					
						
							|  |  |  |         self.assertEqual(contents, {'binary.file', 'utf-8.file', 'utf-16.file'}) | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2017-12-30 15:18:06 -05:00
										 |  |  | if __name__ == '__main__': | 
					
						
							|  |  |  |     unittest.main() |