1import sys 2import unittest 3 4from . import data01 5from . import zipdata01, zipdata02 6from . import util 7from importlib import resources, import_module 8 9 10class ResourceTests: 11 # Subclasses are expected to set the `data` attribute. 12 13 def test_is_resource_good_path(self): 14 self.assertTrue(resources.is_resource(self.data, 'binary.file')) 15 16 def test_is_resource_missing(self): 17 self.assertFalse(resources.is_resource(self.data, 'not-a-file')) 18 19 def test_is_resource_subresource_directory(self): 20 # Directories are not resources. 21 self.assertFalse(resources.is_resource(self.data, 'subdirectory')) 22 23 def test_contents(self): 24 contents = set(resources.contents(self.data)) 25 # There may be cruft in the directory listing of the data directory. 26 # Under Python 3 we could have a __pycache__ directory, and under 27 # Python 2 we could have .pyc files. These are both artifacts of the 28 # test suite importing these modules and writing these caches. They 29 # aren't germane to this test, so just filter them out. 30 contents.discard('__pycache__') 31 contents.discard('__init__.pyc') 32 contents.discard('__init__.pyo') 33 self.assertEqual(contents, { 34 '__init__.py', 35 'subdirectory', 36 'utf-8.file', 37 'binary.file', 38 'utf-16.file', 39 }) 40 41 42class ResourceDiskTests(ResourceTests, unittest.TestCase): 43 def setUp(self): 44 self.data = data01 45 46 47class ResourceZipTests(ResourceTests, util.ZipSetup, unittest.TestCase): 48 pass 49 50 51class ResourceLoaderTests(unittest.TestCase): 52 def test_resource_contents(self): 53 package = util.create_package( 54 file=data01, path=data01.__file__, contents=['A', 'B', 'C']) 55 self.assertEqual( 56 set(resources.contents(package)), 57 {'A', 'B', 'C'}) 58 59 def test_resource_is_resource(self): 60 package = util.create_package( 61 file=data01, path=data01.__file__, 62 contents=['A', 'B', 'C', 'D/E', 'D/F']) 63 self.assertTrue(resources.is_resource(package, 'B')) 64 65 def test_resource_directory_is_not_resource(self): 66 package = util.create_package( 67 file=data01, path=data01.__file__, 68 contents=['A', 'B', 'C', 'D/E', 'D/F']) 69 self.assertFalse(resources.is_resource(package, 'D')) 70 71 def test_resource_missing_is_not_resource(self): 72 package = util.create_package( 73 file=data01, path=data01.__file__, 74 contents=['A', 'B', 'C', 'D/E', 'D/F']) 75 self.assertFalse(resources.is_resource(package, 'Z')) 76 77 78class ResourceCornerCaseTests(unittest.TestCase): 79 def test_package_has_no_reader_fallback(self): 80 # Test odd ball packages which: 81 # 1. Do not have a ResourceReader as a loader 82 # 2. Are not on the file system 83 # 3. Are not in a zip file 84 module = util.create_package( 85 file=data01, path=data01.__file__, contents=['A', 'B', 'C']) 86 # Give the module a dummy loader. 87 module.__loader__ = object() 88 # Give the module a dummy origin. 89 module.__file__ = '/path/which/shall/not/be/named' 90 if sys.version_info >= (3,): 91 module.__spec__.loader = module.__loader__ 92 module.__spec__.origin = module.__file__ 93 self.assertFalse(resources.is_resource(module, 'A')) 94 95 96class ResourceFromZipsTest(util.ZipSetupBase, unittest.TestCase): 97 ZIP_MODULE = zipdata02 # type: ignore 98 99 def test_unrelated_contents(self): 100 # https://gitlab.com/python-devs/importlib_resources/issues/44 101 # 102 # Here we have a zip file with two unrelated subpackages. The bug 103 # reports that getting the contents of a resource returns unrelated 104 # files. 105 self.assertEqual( 106 set(resources.contents('ziptestdata.one')), 107 {'__init__.py', 'resource1.txt'}) 108 self.assertEqual( 109 set(resources.contents('ziptestdata.two')), 110 {'__init__.py', 'resource2.txt'}) 111 112 113class SubdirectoryResourceFromZipsTest(util.ZipSetupBase, unittest.TestCase): 114 ZIP_MODULE = zipdata01 # type: ignore 115 116 def test_is_submodule_resource(self): 117 submodule = import_module('ziptestdata.subdirectory') 118 self.assertTrue( 119 resources.is_resource(submodule, 'binary.file')) 120 121 def test_read_submodule_resource_by_name(self): 122 self.assertTrue( 123 resources.is_resource('ziptestdata.subdirectory', 'binary.file')) 124 125 def test_submodule_contents(self): 126 submodule = import_module('ziptestdata.subdirectory') 127 self.assertEqual( 128 set(resources.contents(submodule)), 129 {'__init__.py', 'binary.file'}) 130 131 def test_submodule_contents_by_name(self): 132 self.assertEqual( 133 set(resources.contents('ziptestdata.subdirectory')), 134 {'__init__.py', 'binary.file'}) 135 136 137class NamespaceTest(unittest.TestCase): 138 def test_namespaces_cannot_have_resources(self): 139 contents = resources.contents('test.test_importlib.data03.namespace') 140 self.assertFalse(list(contents)) 141 # Even though there is a file in the namespace directory, it is not 142 # considered a resource, since namespace packages can't have them. 143 self.assertFalse(resources.is_resource( 144 'test.test_importlib.data03.namespace', 145 'resource1.txt')) 146 # We should get an exception if we try to read it or open it. 147 self.assertRaises( 148 FileNotFoundError, 149 resources.open_text, 150 'test.test_importlib.data03.namespace', 'resource1.txt') 151 self.assertRaises( 152 FileNotFoundError, 153 resources.open_binary, 154 'test.test_importlib.data03.namespace', 'resource1.txt') 155 self.assertRaises( 156 FileNotFoundError, 157 resources.read_text, 158 'test.test_importlib.data03.namespace', 'resource1.txt') 159 self.assertRaises( 160 FileNotFoundError, 161 resources.read_binary, 162 'test.test_importlib.data03.namespace', 'resource1.txt') 163 164 165if __name__ == '__main__': 166 unittest.main() 167