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