Skip to content

Commit 797c097

Browse files
committed
Port test_resource to traversable API.
1 parent 6f35cd3 commit 797c097

File tree

1 file changed

+68
-97
lines changed

1 file changed

+68
-97
lines changed

importlib_resources/tests/test_resource.py

Lines changed: 68 additions & 97 deletions
Original file line numberDiff line numberDiff line change
@@ -14,38 +14,18 @@
1414
class ResourceTests:
1515
# Subclasses are expected to set the `data` attribute.
1616

17-
def test_is_resource_good_path(self):
18-
with util.suppress_known_deprecation():
19-
self.assertTrue(resources.is_resource(self.data, 'binary.file'))
20-
21-
def test_is_resource_missing(self):
22-
with util.suppress_known_deprecation():
23-
self.assertFalse(resources.is_resource(self.data, 'not-a-file'))
24-
25-
def test_is_resource_subresource_directory(self):
26-
# Directories are not resources.
27-
with util.suppress_known_deprecation():
28-
self.assertFalse(resources.is_resource(self.data, 'subdirectory'))
29-
30-
def test_contents(self):
31-
with util.suppress_known_deprecation():
32-
contents = set(resources.contents(self.data))
33-
# There may be cruft in the directory listing of the data directory.
34-
# It could have a __pycache__ directory,
35-
# an artifact of the
36-
# test suite importing these modules, which
37-
# are not germane to this test, so just filter them out.
38-
contents.discard('__pycache__')
39-
self.assertEqual(
40-
sorted(contents),
41-
[
42-
'__init__.py',
43-
'binary.file',
44-
'subdirectory',
45-
'utf-16.file',
46-
'utf-8.file',
47-
],
48-
)
17+
def test_is_file_exists(self):
18+
target = resources.files(self.data) / 'binary.file'
19+
self.assertTrue(target.is_file())
20+
21+
def test_is_file_missing(self):
22+
target = resources.files(self.data) / 'not-a-file'
23+
self.assertFalse(target.is_file())
24+
25+
def test_is_dir(self):
26+
target = resources.files(self.data) / 'subdirectory'
27+
self.assertFalse(target.is_file())
28+
self.assertTrue(target.is_dir())
4929

5030

5131
class ResourceDiskTests(ResourceTests, unittest.TestCase):
@@ -57,34 +37,34 @@ class ResourceZipTests(ResourceTests, util.ZipSetup, unittest.TestCase):
5737
pass
5838

5939

40+
def names(traversable):
41+
return {item.name for item in traversable.iterdir()}
42+
43+
6044
class ResourceLoaderTests(unittest.TestCase):
6145
def test_resource_contents(self):
6246
package = util.create_package(
6347
file=data01, path=data01.__file__, contents=['A', 'B', 'C']
6448
)
65-
with util.suppress_known_deprecation():
66-
self.assertEqual(set(resources.contents(package)), {'A', 'B', 'C'})
49+
self.assertEqual(names(resources.files(package)), {'A', 'B', 'C'})
6750

68-
def test_resource_is_resource(self):
51+
def test_is_file(self):
6952
package = util.create_package(
7053
file=data01, path=data01.__file__, contents=['A', 'B', 'C', 'D/E', 'D/F']
7154
)
72-
with util.suppress_known_deprecation():
73-
self.assertTrue(resources.is_resource(package, 'B'))
55+
self.assertTrue(resources.files(package).joinpath('B').is_file())
7456

75-
def test_resource_directory_is_not_resource(self):
57+
def test_is_dir(self):
7658
package = util.create_package(
7759
file=data01, path=data01.__file__, contents=['A', 'B', 'C', 'D/E', 'D/F']
7860
)
79-
with util.suppress_known_deprecation():
80-
self.assertFalse(resources.is_resource(package, 'D'))
61+
self.assertTrue(resources.files(package).joinpath('D').is_dir())
8162

82-
def test_resource_missing_is_not_resource(self):
63+
def test_resource_missing(self):
8364
package = util.create_package(
8465
file=data01, path=data01.__file__, contents=['A', 'B', 'C', 'D/E', 'D/F']
8566
)
86-
with util.suppress_known_deprecation():
87-
self.assertFalse(resources.is_resource(package, 'Z'))
67+
self.assertFalse(resources.files(package).joinpath('Z').is_file())
8868

8969

9070
class ResourceCornerCaseTests(unittest.TestCase):
@@ -102,37 +82,34 @@ def test_package_has_no_reader_fallback(self):
10282
module.__file__ = '/path/which/shall/not/be/named'
10383
module.__spec__.loader = module.__loader__
10484
module.__spec__.origin = module.__file__
105-
with util.suppress_known_deprecation():
106-
self.assertFalse(resources.is_resource(module, 'A'))
85+
self.assertFalse(resources.files(module).joinpath('A').is_file())
10786

10887

10988
class ResourceFromZipsTest01(util.ZipSetupBase, unittest.TestCase):
11089
ZIP_MODULE = zipdata01 # type: ignore
11190

11291
def test_is_submodule_resource(self):
11392
submodule = import_module('ziptestdata.subdirectory')
114-
with util.suppress_known_deprecation():
115-
self.assertTrue(resources.is_resource(submodule, 'binary.file'))
93+
self.assertTrue(resources.files(submodule).joinpath('binary.file').is_file())
11694

11795
def test_read_submodule_resource_by_name(self):
118-
with util.suppress_known_deprecation():
119-
self.assertTrue(
120-
resources.is_resource('ziptestdata.subdirectory', 'binary.file')
121-
)
96+
self.assertTrue(
97+
resources.files('ziptestdata.subdirectory')
98+
.joinpath('binary.file')
99+
.is_file()
100+
)
122101

123102
def test_submodule_contents(self):
124103
submodule = import_module('ziptestdata.subdirectory')
125-
with util.suppress_known_deprecation():
126-
self.assertEqual(
127-
set(resources.contents(submodule)), {'__init__.py', 'binary.file'}
128-
)
104+
self.assertEqual(
105+
names(resources.files(submodule)), {'__init__.py', 'binary.file'}
106+
)
129107

130108
def test_submodule_contents_by_name(self):
131-
with util.suppress_known_deprecation():
132-
self.assertEqual(
133-
set(resources.contents('ziptestdata.subdirectory')),
134-
{'__init__.py', 'binary.file'},
135-
)
109+
self.assertEqual(
110+
names(resources.files('ziptestdata.subdirectory')),
111+
{'__init__.py', 'binary.file'},
112+
)
136113

137114

138115
class ResourceFromZipsTest02(util.ZipSetupBase, unittest.TestCase):
@@ -143,16 +120,14 @@ def test_unrelated_contents(self):
143120
Test thata zip with two unrelated subpackages return
144121
distinct resources. Ref python/importlib_resources#44.
145122
"""
146-
with util.suppress_known_deprecation():
147-
self.assertEqual(
148-
set(resources.contents('ziptestdata.one')),
149-
{'__init__.py', 'resource1.txt'},
150-
)
151-
with util.suppress_known_deprecation():
152-
self.assertEqual(
153-
set(resources.contents('ziptestdata.two')),
154-
{'__init__.py', 'resource2.txt'},
155-
)
123+
self.assertEqual(
124+
names(resources.files('ziptestdata.one')),
125+
{'__init__.py', 'resource1.txt'},
126+
)
127+
self.assertEqual(
128+
names(resources.files('ziptestdata.two')),
129+
{'__init__.py', 'resource2.txt'},
130+
)
156131

157132

158133
class DeletingZipsTest(unittest.TestCase):
@@ -192,47 +167,43 @@ def tearDown(self):
192167
# If the test fails, this will probably fail too
193168
pass
194169

195-
def test_contents_does_not_keep_open(self):
196-
with util.suppress_known_deprecation():
197-
c = resources.contents('ziptestdata')
170+
def test_iterdir_does_not_keep_open(self):
171+
c = [item.name for item in resources.files('ziptestdata').iterdir()]
198172
self.zip_path.unlink()
199173
del c
200174

201-
def test_is_resource_does_not_keep_open(self):
202-
with util.suppress_known_deprecation():
203-
c = resources.is_resource('ziptestdata', 'binary.file')
175+
def test_is_file_does_not_keep_open(self):
176+
c = resources.files('ziptestdata').joinpath('binary.file').is_file()
204177
self.zip_path.unlink()
205178
del c
206179

207-
def test_is_resource_failure_does_not_keep_open(self):
208-
with util.suppress_known_deprecation():
209-
c = resources.is_resource('ziptestdata', 'not-present')
180+
def test_is_file_failure_does_not_keep_open(self):
181+
c = resources.files('ziptestdata').joinpath('not-present').is_file()
210182
self.zip_path.unlink()
211183
del c
212184

213185
@unittest.skip("Desired but not supported.")
214-
def test_path_does_not_keep_open(self):
215-
c = resources.path('ziptestdata', 'binary.file')
186+
def test_as_file_does_not_keep_open(self): # pragma: no cover
187+
c = resources.as_file(resources.files('ziptestdata') / 'binary.file')
216188
self.zip_path.unlink()
217189
del c
218190

219191
def test_entered_path_does_not_keep_open(self):
220192
# This is what certifi does on import to make its bundle
221193
# available for the process duration.
222-
with util.suppress_known_deprecation():
223-
c = resources.path('ziptestdata', 'binary.file').__enter__()
194+
c = resources.as_file(
195+
resources.files('ziptestdata') / 'binary.file'
196+
).__enter__()
224197
self.zip_path.unlink()
225198
del c
226199

227200
def test_read_binary_does_not_keep_open(self):
228-
with util.suppress_known_deprecation():
229-
c = resources.read_binary('ziptestdata', 'binary.file')
201+
c = resources.files('ziptestdata').joinpath('binary.file').read_bytes()
230202
self.zip_path.unlink()
231203
del c
232204

233205
def test_read_text_does_not_keep_open(self):
234-
with util.suppress_known_deprecation():
235-
c = resources.read_text('ziptestdata', 'utf-8.file', encoding='utf-8')
206+
c = resources.files('ziptestdata').joinpath('utf-8.file').read_text()
236207
self.zip_path.unlink()
237208
del c
238209

@@ -249,27 +220,27 @@ def tearDownClass(cls):
249220
sys.path.remove(cls.site_dir)
250221

251222
def test_is_submodule_resource(self):
252-
with util.suppress_known_deprecation():
253-
self.assertTrue(
254-
resources.is_resource(import_module('namespacedata01'), 'binary.file')
255-
)
223+
self.assertTrue(
224+
resources.files(import_module('namespacedata01'))
225+
.joinpath('binary.file')
226+
.is_file()
227+
)
256228

257229
def test_read_submodule_resource_by_name(self):
258-
with util.suppress_known_deprecation():
259-
self.assertTrue(resources.is_resource('namespacedata01', 'binary.file'))
230+
self.assertTrue(
231+
resources.files('namespacedata01').joinpath('binary.file').is_file()
232+
)
260233

261234
def test_submodule_contents(self):
262-
with util.suppress_known_deprecation():
263-
contents = set(resources.contents(import_module('namespacedata01')))
235+
contents = names(resources.files(import_module('namespacedata01')))
264236
try:
265237
contents.remove('__pycache__')
266238
except KeyError:
267239
pass
268240
self.assertEqual(contents, {'binary.file', 'utf-8.file', 'utf-16.file'})
269241

270242
def test_submodule_contents_by_name(self):
271-
with util.suppress_known_deprecation():
272-
contents = set(resources.contents('namespacedata01'))
243+
contents = names(resources.files('namespacedata01'))
273244
try:
274245
contents.remove('__pycache__')
275246
except KeyError:

0 commit comments

Comments
 (0)