test_resource.py 7.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221
  1. import sys
  2. import unittest
  3. import importlib_resources as resources
  4. import pathlib
  5. from . import data01
  6. from . import util
  7. from importlib import import_module
  8. class ResourceTests:
  9. # Subclasses are expected to set the `data` attribute.
  10. def test_is_file_exists(self):
  11. target = resources.files(self.data) / 'binary.file'
  12. self.assertTrue(target.is_file())
  13. def test_is_file_missing(self):
  14. target = resources.files(self.data) / 'not-a-file'
  15. self.assertFalse(target.is_file())
  16. def test_is_dir(self):
  17. target = resources.files(self.data) / 'subdirectory'
  18. self.assertFalse(target.is_file())
  19. self.assertTrue(target.is_dir())
  20. class ResourceDiskTests(ResourceTests, unittest.TestCase):
  21. def setUp(self):
  22. self.data = data01
  23. class ResourceZipTests(ResourceTests, util.ZipSetup, unittest.TestCase):
  24. pass
  25. def names(traversable):
  26. return {item.name for item in traversable.iterdir()}
  27. class ResourceLoaderTests(unittest.TestCase):
  28. def test_resource_contents(self):
  29. package = util.create_package(
  30. file=data01, path=data01.__file__, contents=['A', 'B', 'C']
  31. )
  32. self.assertEqual(names(resources.files(package)), {'A', 'B', 'C'})
  33. def test_is_file(self):
  34. package = util.create_package(
  35. file=data01, path=data01.__file__, contents=['A', 'B', 'C', 'D/E', 'D/F']
  36. )
  37. self.assertTrue(resources.files(package).joinpath('B').is_file())
  38. def test_is_dir(self):
  39. package = util.create_package(
  40. file=data01, path=data01.__file__, contents=['A', 'B', 'C', 'D/E', 'D/F']
  41. )
  42. self.assertTrue(resources.files(package).joinpath('D').is_dir())
  43. def test_resource_missing(self):
  44. package = util.create_package(
  45. file=data01, path=data01.__file__, contents=['A', 'B', 'C', 'D/E', 'D/F']
  46. )
  47. self.assertFalse(resources.files(package).joinpath('Z').is_file())
  48. class ResourceCornerCaseTests(unittest.TestCase):
  49. def test_package_has_no_reader_fallback(self):
  50. """
  51. Test odd ball packages which:
  52. # 1. Do not have a ResourceReader as a loader
  53. # 2. Are not on the file system
  54. # 3. Are not in a zip file
  55. """
  56. module = util.create_package(
  57. file=data01, path=data01.__file__, contents=['A', 'B', 'C']
  58. )
  59. # Give the module a dummy loader.
  60. module.__loader__ = object()
  61. # Give the module a dummy origin.
  62. module.__file__ = '/path/which/shall/not/be/named'
  63. module.__spec__.loader = module.__loader__
  64. module.__spec__.origin = module.__file__
  65. self.assertFalse(resources.files(module).joinpath('A').is_file())
  66. class ResourceFromZipsTest01(util.ZipSetupBase, unittest.TestCase):
  67. ZIP_MODULE = 'data01'
  68. def test_is_submodule_resource(self):
  69. submodule = import_module('data01.subdirectory')
  70. self.assertTrue(resources.files(submodule).joinpath('binary.file').is_file())
  71. def test_read_submodule_resource_by_name(self):
  72. self.assertTrue(
  73. resources.files('data01.subdirectory').joinpath('binary.file').is_file()
  74. )
  75. def test_submodule_contents(self):
  76. submodule = import_module('data01.subdirectory')
  77. self.assertEqual(
  78. names(resources.files(submodule)), {'__init__.py', 'binary.file'}
  79. )
  80. def test_submodule_contents_by_name(self):
  81. self.assertEqual(
  82. names(resources.files('data01.subdirectory')),
  83. {'__init__.py', 'binary.file'},
  84. )
  85. def test_as_file_directory(self):
  86. with resources.as_file(resources.files('data01')) as data:
  87. assert data.name == 'data01'
  88. assert data.is_dir()
  89. assert data.joinpath('subdirectory').is_dir()
  90. assert len(list(data.iterdir()))
  91. assert not data.parent.exists()
  92. class ResourceFromZipsTest02(util.ZipSetupBase, unittest.TestCase):
  93. ZIP_MODULE = 'data02'
  94. def test_unrelated_contents(self):
  95. """
  96. Test thata zip with two unrelated subpackages return
  97. distinct resources. Ref python/importlib_resources#44.
  98. """
  99. self.assertEqual(
  100. names(resources.files('data02.one')),
  101. {'__init__.py', 'resource1.txt'},
  102. )
  103. self.assertEqual(
  104. names(resources.files('data02.two')),
  105. {'__init__.py', 'resource2.txt'},
  106. )
  107. class DeletingZipsTest(util.ZipSetupBase, unittest.TestCase):
  108. """Having accessed resources in a zip file should not keep an open
  109. reference to the zip.
  110. """
  111. def test_iterdir_does_not_keep_open(self):
  112. [item.name for item in resources.files('data01').iterdir()]
  113. def test_is_file_does_not_keep_open(self):
  114. resources.files('data01').joinpath('binary.file').is_file()
  115. def test_is_file_failure_does_not_keep_open(self):
  116. resources.files('data01').joinpath('not-present').is_file()
  117. @unittest.skip("Desired but not supported.")
  118. def test_as_file_does_not_keep_open(self): # pragma: no cover
  119. resources.as_file(resources.files('data01') / 'binary.file')
  120. def test_entered_path_does_not_keep_open(self):
  121. """
  122. Mimic what certifi does on import to make its bundle
  123. available for the process duration.
  124. """
  125. resources.as_file(resources.files('data01') / 'binary.file').__enter__()
  126. def test_read_binary_does_not_keep_open(self):
  127. resources.files('data01').joinpath('binary.file').read_bytes()
  128. def test_read_text_does_not_keep_open(self):
  129. resources.files('data01').joinpath('utf-8.file').read_text(encoding='utf-8')
  130. class ResourceFromNamespaceTests:
  131. def test_is_submodule_resource(self):
  132. self.assertTrue(
  133. resources.files(import_module('namespacedata01'))
  134. .joinpath('binary.file')
  135. .is_file()
  136. )
  137. def test_read_submodule_resource_by_name(self):
  138. self.assertTrue(
  139. resources.files('namespacedata01').joinpath('binary.file').is_file()
  140. )
  141. def test_submodule_contents(self):
  142. contents = names(resources.files(import_module('namespacedata01')))
  143. try:
  144. contents.remove('__pycache__')
  145. except KeyError:
  146. pass
  147. self.assertEqual(contents, {'binary.file', 'utf-8.file', 'utf-16.file'})
  148. def test_submodule_contents_by_name(self):
  149. contents = names(resources.files('namespacedata01'))
  150. try:
  151. contents.remove('__pycache__')
  152. except KeyError:
  153. pass
  154. self.assertEqual(contents, {'binary.file', 'utf-8.file', 'utf-16.file'})
  155. class ResourceFromNamespaceDiskTests(ResourceFromNamespaceTests, unittest.TestCase):
  156. site_dir = str(pathlib.Path(__file__).parent)
  157. @classmethod
  158. def setUpClass(cls):
  159. sys.path.append(cls.site_dir)
  160. @classmethod
  161. def tearDownClass(cls):
  162. sys.path.remove(cls.site_dir)
  163. class ResourceFromNamespaceZipTests(
  164. util.ZipSetupBase,
  165. ResourceFromNamespaceTests,
  166. unittest.TestCase,
  167. ):
  168. ZIP_MODULE = 'namespacedata01'
  169. if __name__ == '__main__':
  170. unittest.main()