Debian Codegraph @

Lib/test/test_importlib/resources/test_functional.py

raw
1 import unittest
2 import os
3 import importlib
4
5 from test.support import warnings_helper
6
7 from importlib import resources
8
9 from . import util
10
11 # Since the functional API forwards to Traversable, we only test
12 # filesystem resources here -- not zip files, namespace packages etc.
13 # We do test for two kinds of Anchor, though.
14
15
16 class StringAnchorMixin:
17 anchor01 = 'data01'
18 anchor02 = 'data02'
19
20
21 class ModuleAnchorMixin:
22 @property
23 def anchor01(self):
24 return importlib.import_module('data01')
25
26 @property
27 def anchor02(self):
28 return importlib.import_module('data02')
29
30
31 class FunctionalAPIBase(util.DiskSetup):
32 def setUp(self):
33 super().setUp()
34 self.load_fixture('data02')
35
36 def _gen_resourcetxt_path_parts(self):
37 """Yield various names of a text file in anchor02, each in a subTest"""
38 for path_parts in (
39 ('subdirectory', 'subsubdir', 'resource.txt'),
40 ('subdirectory/subsubdir/resource.txt',),
41 ('subdirectory/subsubdir', 'resource.txt'),
42 ):
43 with self.subTest(path_parts=path_parts):
44 yield path_parts
45
46 def test_read_text(self):
47 self.assertEqual(
48 resources.read_text(self.anchor01, 'utf-8.file'),
49 'Hello, UTF-8 world!\n',
50 )
51 self.assertEqual(
52 resources.read_text(
53 self.anchor02,
54 'subdirectory',
55 'subsubdir',
56 'resource.txt',
57 encoding='utf-8',
58 ),
59 'a resource',
60 )
61 for path_parts in self._gen_resourcetxt_path_parts():
62 self.assertEqual(
63 resources.read_text(
64 self.anchor02,
65 *path_parts,
66 encoding='utf-8',
67 ),
68 'a resource',
69 )
70 # Use generic OSError, since e.g. attempting to read a directory can
71 # fail with PermissionError rather than IsADirectoryError
72 with self.assertRaises(OSError):
73 resources.read_text(self.anchor01)
74 with self.assertRaises(OSError):
75 resources.read_text(self.anchor01, 'no-such-file')
76 with self.assertRaises(UnicodeDecodeError):
77 resources.read_text(self.anchor01, 'utf-16.file')
78 self.assertEqual(
79 resources.read_text(
80 self.anchor01,
81 'binary.file',
82 encoding='latin1',
83 ),
84 '\x00\x01\x02\x03',
85 )
86 self.assertEndsWith( # ignore the BOM
87 resources.read_text(
88 self.anchor01,
89 'utf-16.file',
90 errors='backslashreplace',
91 ),
92 'Hello, UTF-16 world!\n'.encode('utf-16-le').decode(
93 errors='backslashreplace',
94 ),
95 )
96
97 def test_read_binary(self):
98 self.assertEqual(
99 resources.read_binary(self.anchor01, 'utf-8.file'),
100 b'Hello, UTF-8 world!\n',
101 )
102 for path_parts in self._gen_resourcetxt_path_parts():
103 self.assertEqual(
104 resources.read_binary(self.anchor02, *path_parts),
105 b'a resource',
106 )
107
108 def test_open_text(self):
109 with resources.open_text(self.anchor01, 'utf-8.file') as f:
110 self.assertEqual(f.read(), 'Hello, UTF-8 world!\n')
111 for path_parts in self._gen_resourcetxt_path_parts():
112 with resources.open_text(
113 self.anchor02,
114 *path_parts,
115 encoding='utf-8',
116 ) as f:
117 self.assertEqual(f.read(), 'a resource')
118 # Use generic OSError, since e.g. attempting to read a directory can
119 # fail with PermissionError rather than IsADirectoryError
120 with self.assertRaises(OSError):
121 resources.open_text(self.anchor01)
122 with self.assertRaises(OSError):
123 resources.open_text(self.anchor01, 'no-such-file')
124 with resources.open_text(self.anchor01, 'utf-16.file') as f:
125 with self.assertRaises(UnicodeDecodeError):
126 f.read()
127 with resources.open_text(
128 self.anchor01,
129 'binary.file',
130 encoding='latin1',
131 ) as f:
132 self.assertEqual(f.read(), '\x00\x01\x02\x03')
133 with resources.open_text(
134 self.anchor01,
135 'utf-16.file',
136 errors='backslashreplace',
137 ) as f:
138 self.assertEndsWith( # ignore the BOM
139 f.read(),
140 'Hello, UTF-16 world!\n'.encode('utf-16-le').decode(
141 errors='backslashreplace',
142 ),
143 )
144
145 def test_open_binary(self):
146 with resources.open_binary(self.anchor01, 'utf-8.file') as f:
147 self.assertEqual(f.read(), b'Hello, UTF-8 world!\n')
148 for path_parts in self._gen_resourcetxt_path_parts():
149 with resources.open_binary(
150 self.anchor02,
151 *path_parts,
152 ) as f:
153 self.assertEqual(f.read(), b'a resource')
154
155 def test_path(self):
156 with resources.path(self.anchor01, 'utf-8.file') as path:
157 with open(str(path), encoding='utf-8') as f:
158 self.assertEqual(f.read(), 'Hello, UTF-8 world!\n')
159 with resources.path(self.anchor01) as path:
160 with open(os.path.join(path, 'utf-8.file'), encoding='utf-8') as f:
161 self.assertEqual(f.read(), 'Hello, UTF-8 world!\n')
162
163 def test_is_resource(self):
164 is_resource = resources.is_resource
165 self.assertTrue(is_resource(self.anchor01, 'utf-8.file'))
166 self.assertFalse(is_resource(self.anchor01, 'no_such_file'))
167 self.assertFalse(is_resource(self.anchor01))
168 self.assertFalse(is_resource(self.anchor01, 'subdirectory'))
169 for path_parts in self._gen_resourcetxt_path_parts():
170 self.assertTrue(is_resource(self.anchor02, *path_parts))
171
172 def test_contents(self):
173 with warnings_helper.check_warnings((".*contents.*", DeprecationWarning)):
174 c = resources.contents(self.anchor01)
175 self.assertGreaterEqual(
176 set(c),
177 {'utf-8.file', 'utf-16.file', 'binary.file', 'subdirectory'},
178 )
179 with self.assertRaises(OSError), warnings_helper.check_warnings((
180 ".*contents.*",
181 DeprecationWarning,
182 )):
183 list(resources.contents(self.anchor01, 'utf-8.file'))
184
185 for path_parts in self._gen_resourcetxt_path_parts():
186 with self.assertRaises(OSError), warnings_helper.check_warnings((
187 ".*contents.*",
188 DeprecationWarning,
189 )):
190 list(resources.contents(self.anchor01, *path_parts))
191 with warnings_helper.check_warnings((".*contents.*", DeprecationWarning)):
192 c = resources.contents(self.anchor01, 'subdirectory')
193 self.assertGreaterEqual(
194 set(c),
195 {'binary.file'},
196 )
197
198 @warnings_helper.ignore_warnings(category=DeprecationWarning)
199 def test_common_errors(self):
200 for func in (
201 resources.read_text,
202 resources.read_binary,
203 resources.open_text,
204 resources.open_binary,
205 resources.path,
206 resources.is_resource,
207 resources.contents,
208 ):
209 with self.subTest(func=func):
210 # Rejecting None anchor
211 with self.assertRaises(TypeError):
212 func(None)
213 # Rejecting invalid anchor type
214 with self.assertRaises((TypeError, AttributeError)):
215 func(1234)
216 # Unknown module
217 with self.assertRaises(ModuleNotFoundError):
218 func('$missing module$')
219
220 def test_text_errors(self):
221 for func in (
222 resources.read_text,
223 resources.open_text,
224 ):
225 with self.subTest(func=func):
226 # Multiple path arguments need explicit encoding argument.
227 with self.assertRaises(TypeError):
228 func(
229 self.anchor02,
230 'subdirectory',
231 'subsubdir',
232 'resource.txt',
233 )
234
235
236 class FunctionalAPITest_StringAnchor(
237 StringAnchorMixin,
238 FunctionalAPIBase,
239 unittest.TestCase,
240 ):
241 pass
242
243
244 class FunctionalAPITest_ModuleAnchor(
245 ModuleAnchorMixin,
246 FunctionalAPIBase,
247 unittest.TestCase,
248 ):
249 pass