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