| 
									
										
										
										
											2020-05-08 19:20:26 -04:00
										 |  |  | import typing | 
					
						
							| 
									
										
										
										
											2023-01-01 11:07:32 -05:00
										 |  |  | import textwrap | 
					
						
							| 
									
										
										
										
											2020-05-08 19:20:26 -04:00
										 |  |  | import unittest | 
					
						
							| 
									
										
										
										
											2023-01-01 11:07:32 -05:00
										 |  |  | import warnings | 
					
						
							|  |  |  | import importlib | 
					
						
							|  |  |  | import contextlib | 
					
						
							| 
									
										
										
										
											2020-05-08 19:20:26 -04:00
										 |  |  | 
 | 
					
						
							|  |  |  | from importlib import resources | 
					
						
							| 
									
										
										
										
											2022-07-27 04:54:31 -04:00
										 |  |  | from importlib.resources.abc import Traversable | 
					
						
							| 
									
										
										
										
											2022-01-22 21:38:26 -05:00
										 |  |  | from . import data01 | 
					
						
							| 
									
										
										
										
											2022-07-24 20:53:10 -04:00
										 |  |  | from . import util | 
					
						
							| 
									
										
										
										
											2023-01-01 11:07:32 -05:00
										 |  |  | from . import _path | 
					
						
							|  |  |  | from test.support import os_helper | 
					
						
							|  |  |  | from test.support import import_helper | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | @contextlib.contextmanager | 
					
						
							|  |  |  | def suppress_known_deprecation(): | 
					
						
							|  |  |  |     with warnings.catch_warnings(record=True) as ctx: | 
					
						
							|  |  |  |         warnings.simplefilter('default', category=DeprecationWarning) | 
					
						
							|  |  |  |         yield ctx | 
					
						
							| 
									
										
										
										
											2020-05-08 19:20:26 -04:00
										 |  |  | 
 | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | class FilesTests: | 
					
						
							|  |  |  |     def test_read_bytes(self): | 
					
						
							|  |  |  |         files = resources.files(self.data) | 
					
						
							|  |  |  |         actual = files.joinpath('utf-8.file').read_bytes() | 
					
						
							|  |  |  |         assert actual == b'Hello, UTF-8 world!\n' | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |     def test_read_text(self): | 
					
						
							|  |  |  |         files = resources.files(self.data) | 
					
						
							| 
									
										
										
										
											2021-04-05 13:11:23 +09:00
										 |  |  |         actual = files.joinpath('utf-8.file').read_text(encoding='utf-8') | 
					
						
							| 
									
										
										
										
											2020-05-08 19:20:26 -04:00
										 |  |  |         assert actual == 'Hello, UTF-8 world!\n' | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |     @unittest.skipUnless( | 
					
						
							|  |  |  |         hasattr(typing, 'runtime_checkable'), | 
					
						
							|  |  |  |         "Only suitable when typing supports runtime_checkable", | 
					
						
							| 
									
										
										
										
											2021-03-04 13:43:00 -05:00
										 |  |  |     ) | 
					
						
							| 
									
										
										
										
											2020-05-08 19:20:26 -04:00
										 |  |  |     def test_traversable(self): | 
					
						
							|  |  |  |         assert isinstance(resources.files(self.data), Traversable) | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2023-01-01 11:07:32 -05:00
										 |  |  |     def test_old_parameter(self): | 
					
						
							|  |  |  |         """
 | 
					
						
							|  |  |  |         Files used to take a 'package' parameter. Make sure anyone | 
					
						
							|  |  |  |         passing by name is still supported. | 
					
						
							|  |  |  |         """
 | 
					
						
							|  |  |  |         with suppress_known_deprecation(): | 
					
						
							|  |  |  |             resources.files(package=self.data) | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2020-05-08 19:20:26 -04:00
										 |  |  | 
 | 
					
						
							|  |  |  | class OpenDiskTests(FilesTests, unittest.TestCase): | 
					
						
							|  |  |  |     def setUp(self): | 
					
						
							|  |  |  |         self.data = data01 | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | class OpenZipTests(FilesTests, util.ZipSetup, unittest.TestCase): | 
					
						
							|  |  |  |     pass | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2021-07-29 21:05:05 -04:00
										 |  |  | class OpenNamespaceTests(FilesTests, unittest.TestCase): | 
					
						
							|  |  |  |     def setUp(self): | 
					
						
							| 
									
										
										
										
											2022-01-22 21:38:26 -05:00
										 |  |  |         from . import namespacedata01 | 
					
						
							| 
									
										
										
										
											2021-07-29 21:05:05 -04:00
										 |  |  | 
 | 
					
						
							|  |  |  |         self.data = namespacedata01 | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2023-01-01 11:07:32 -05:00
										 |  |  | class SiteDir: | 
					
						
							|  |  |  |     def setUp(self): | 
					
						
							|  |  |  |         self.fixtures = contextlib.ExitStack() | 
					
						
							|  |  |  |         self.addCleanup(self.fixtures.close) | 
					
						
							|  |  |  |         self.site_dir = self.fixtures.enter_context(os_helper.temp_dir()) | 
					
						
							|  |  |  |         self.fixtures.enter_context(import_helper.DirsOnSysPath(self.site_dir)) | 
					
						
							|  |  |  |         self.fixtures.enter_context(import_helper.CleanImport()) | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | class ModulesFilesTests(SiteDir, unittest.TestCase): | 
					
						
							|  |  |  |     def test_module_resources(self): | 
					
						
							|  |  |  |         """
 | 
					
						
							|  |  |  |         A module can have resources found adjacent to the module. | 
					
						
							|  |  |  |         """
 | 
					
						
							|  |  |  |         spec = { | 
					
						
							|  |  |  |             'mod.py': '', | 
					
						
							|  |  |  |             'res.txt': 'resources are the best', | 
					
						
							|  |  |  |         } | 
					
						
							|  |  |  |         _path.build(spec, self.site_dir) | 
					
						
							|  |  |  |         import mod | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2023-02-18 16:29:22 -05:00
										 |  |  |         actual = resources.files(mod).joinpath('res.txt').read_text(encoding='utf-8') | 
					
						
							| 
									
										
										
										
											2023-01-01 11:07:32 -05:00
										 |  |  |         assert actual == spec['res.txt'] | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | class ImplicitContextFilesTests(SiteDir, unittest.TestCase): | 
					
						
							|  |  |  |     def test_implicit_files(self): | 
					
						
							|  |  |  |         """
 | 
					
						
							|  |  |  |         Without any parameter, files() will infer the location as the caller. | 
					
						
							|  |  |  |         """
 | 
					
						
							|  |  |  |         spec = { | 
					
						
							|  |  |  |             'somepkg': { | 
					
						
							|  |  |  |                 '__init__.py': textwrap.dedent( | 
					
						
							|  |  |  |                     """
 | 
					
						
							|  |  |  |                     import importlib.resources as res | 
					
						
							| 
									
										
										
										
											2023-02-18 16:29:22 -05:00
										 |  |  |                     val = res.files().joinpath('res.txt').read_text(encoding='utf-8') | 
					
						
							| 
									
										
										
										
											2023-01-01 11:07:32 -05:00
										 |  |  |                     """
 | 
					
						
							|  |  |  |                 ), | 
					
						
							|  |  |  |                 'res.txt': 'resources are the best', | 
					
						
							|  |  |  |             }, | 
					
						
							|  |  |  |         } | 
					
						
							|  |  |  |         _path.build(spec, self.site_dir) | 
					
						
							|  |  |  |         assert importlib.import_module('somepkg').val == 'resources are the best' | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2020-05-08 19:20:26 -04:00
										 |  |  | if __name__ == '__main__': | 
					
						
							|  |  |  |     unittest.main() |