14
14
class ResourceTests :
15
15
# Subclasses are expected to set the `data` attribute.
16
16
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 ())
49
29
50
30
51
31
class ResourceDiskTests (ResourceTests , unittest .TestCase ):
@@ -57,34 +37,34 @@ class ResourceZipTests(ResourceTests, util.ZipSetup, unittest.TestCase):
57
37
pass
58
38
59
39
40
+ def names (traversable ):
41
+ return {item .name for item in traversable .iterdir ()}
42
+
43
+
60
44
class ResourceLoaderTests (unittest .TestCase ):
61
45
def test_resource_contents (self ):
62
46
package = util .create_package (
63
47
file = data01 , path = data01 .__file__ , contents = ['A' , 'B' , 'C' ]
64
48
)
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' })
67
50
68
- def test_resource_is_resource (self ):
51
+ def test_is_file (self ):
69
52
package = util .create_package (
70
53
file = data01 , path = data01 .__file__ , contents = ['A' , 'B' , 'C' , 'D/E' , 'D/F' ]
71
54
)
72
- with util .suppress_known_deprecation ():
73
- self .assertTrue (resources .is_resource (package , 'B' ))
55
+ self .assertTrue (resources .files (package ).joinpath ('B' ).is_file ())
74
56
75
- def test_resource_directory_is_not_resource (self ):
57
+ def test_is_dir (self ):
76
58
package = util .create_package (
77
59
file = data01 , path = data01 .__file__ , contents = ['A' , 'B' , 'C' , 'D/E' , 'D/F' ]
78
60
)
79
- with util .suppress_known_deprecation ():
80
- self .assertFalse (resources .is_resource (package , 'D' ))
61
+ self .assertTrue (resources .files (package ).joinpath ('D' ).is_dir ())
81
62
82
- def test_resource_missing_is_not_resource (self ):
63
+ def test_resource_missing (self ):
83
64
package = util .create_package (
84
65
file = data01 , path = data01 .__file__ , contents = ['A' , 'B' , 'C' , 'D/E' , 'D/F' ]
85
66
)
86
- with util .suppress_known_deprecation ():
87
- self .assertFalse (resources .is_resource (package , 'Z' ))
67
+ self .assertFalse (resources .files (package ).joinpath ('Z' ).is_file ())
88
68
89
69
90
70
class ResourceCornerCaseTests (unittest .TestCase ):
@@ -102,37 +82,34 @@ def test_package_has_no_reader_fallback(self):
102
82
module .__file__ = '/path/which/shall/not/be/named'
103
83
module .__spec__ .loader = module .__loader__
104
84
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 ())
107
86
108
87
109
88
class ResourceFromZipsTest01 (util .ZipSetupBase , unittest .TestCase ):
110
89
ZIP_MODULE = zipdata01 # type: ignore
111
90
112
91
def test_is_submodule_resource (self ):
113
92
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 ())
116
94
117
95
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
+ )
122
101
123
102
def test_submodule_contents (self ):
124
103
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
+ )
129
107
130
108
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
+ )
136
113
137
114
138
115
class ResourceFromZipsTest02 (util .ZipSetupBase , unittest .TestCase ):
@@ -143,16 +120,14 @@ def test_unrelated_contents(self):
143
120
Test thata zip with two unrelated subpackages return
144
121
distinct resources. Ref python/importlib_resources#44.
145
122
"""
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
+ )
156
131
157
132
158
133
class DeletingZipsTest (unittest .TestCase ):
@@ -192,47 +167,43 @@ def tearDown(self):
192
167
# If the test fails, this will probably fail too
193
168
pass
194
169
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 ()]
198
172
self .zip_path .unlink ()
199
173
del c
200
174
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 ()
204
177
self .zip_path .unlink ()
205
178
del c
206
179
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 ()
210
182
self .zip_path .unlink ()
211
183
del c
212
184
213
185
@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' )
216
188
self .zip_path .unlink ()
217
189
del c
218
190
219
191
def test_entered_path_does_not_keep_open (self ):
220
192
# This is what certifi does on import to make its bundle
221
193
# 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__ ()
224
197
self .zip_path .unlink ()
225
198
del c
226
199
227
200
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 ()
230
202
self .zip_path .unlink ()
231
203
del c
232
204
233
205
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 ()
236
207
self .zip_path .unlink ()
237
208
del c
238
209
@@ -249,27 +220,27 @@ def tearDownClass(cls):
249
220
sys .path .remove (cls .site_dir )
250
221
251
222
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
+ )
256
228
257
229
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
+ )
260
233
261
234
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' )))
264
236
try :
265
237
contents .remove ('__pycache__' )
266
238
except KeyError :
267
239
pass
268
240
self .assertEqual (contents , {'binary.file' , 'utf-8.file' , 'utf-16.file' })
269
241
270
242
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' ))
273
244
try :
274
245
contents .remove ('__pycache__' )
275
246
except KeyError :
0 commit comments