1 | n/a | import os |
---|
2 | n/a | import errno |
---|
3 | n/a | import importlib.machinery |
---|
4 | n/a | import py_compile |
---|
5 | n/a | import shutil |
---|
6 | n/a | import unittest |
---|
7 | n/a | import tempfile |
---|
8 | n/a | |
---|
9 | n/a | from test import support |
---|
10 | n/a | |
---|
11 | n/a | import modulefinder |
---|
12 | n/a | |
---|
13 | n/a | TEST_DIR = tempfile.mkdtemp() |
---|
14 | n/a | TEST_PATH = [TEST_DIR, os.path.dirname(tempfile.__file__)] |
---|
15 | n/a | |
---|
16 | n/a | # Each test description is a list of 5 items: |
---|
17 | n/a | # |
---|
18 | n/a | # 1. a module name that will be imported by modulefinder |
---|
19 | n/a | # 2. a list of module names that modulefinder is required to find |
---|
20 | n/a | # 3. a list of module names that modulefinder should complain |
---|
21 | n/a | # about because they are not found |
---|
22 | n/a | # 4. a list of module names that modulefinder should complain |
---|
23 | n/a | # about because they MAY be not found |
---|
24 | n/a | # 5. a string specifying packages to create; the format is obvious imo. |
---|
25 | n/a | # |
---|
26 | n/a | # Each package will be created in TEST_DIR, and TEST_DIR will be |
---|
27 | n/a | # removed after the tests again. |
---|
28 | n/a | # Modulefinder searches in a path that contains TEST_DIR, plus |
---|
29 | n/a | # the standard Lib directory. |
---|
30 | n/a | |
---|
31 | n/a | maybe_test = [ |
---|
32 | n/a | "a.module", |
---|
33 | n/a | ["a", "a.module", "sys", |
---|
34 | n/a | "b"], |
---|
35 | n/a | ["c"], ["b.something"], |
---|
36 | n/a | """\ |
---|
37 | n/a | a/__init__.py |
---|
38 | n/a | a/module.py |
---|
39 | n/a | from b import something |
---|
40 | n/a | from c import something |
---|
41 | n/a | b/__init__.py |
---|
42 | n/a | from sys import * |
---|
43 | n/a | """] |
---|
44 | n/a | |
---|
45 | n/a | maybe_test_new = [ |
---|
46 | n/a | "a.module", |
---|
47 | n/a | ["a", "a.module", "sys", |
---|
48 | n/a | "b", "__future__"], |
---|
49 | n/a | ["c"], ["b.something"], |
---|
50 | n/a | """\ |
---|
51 | n/a | a/__init__.py |
---|
52 | n/a | a/module.py |
---|
53 | n/a | from b import something |
---|
54 | n/a | from c import something |
---|
55 | n/a | b/__init__.py |
---|
56 | n/a | from __future__ import absolute_import |
---|
57 | n/a | from sys import * |
---|
58 | n/a | """] |
---|
59 | n/a | |
---|
60 | n/a | package_test = [ |
---|
61 | n/a | "a.module", |
---|
62 | n/a | ["a", "a.b", "a.c", "a.module", "mymodule", "sys"], |
---|
63 | n/a | ["blahblah", "c"], [], |
---|
64 | n/a | """\ |
---|
65 | n/a | mymodule.py |
---|
66 | n/a | a/__init__.py |
---|
67 | n/a | import blahblah |
---|
68 | n/a | from a import b |
---|
69 | n/a | import c |
---|
70 | n/a | a/module.py |
---|
71 | n/a | import sys |
---|
72 | n/a | from a import b as x |
---|
73 | n/a | from a.c import sillyname |
---|
74 | n/a | a/b.py |
---|
75 | n/a | a/c.py |
---|
76 | n/a | from a.module import x |
---|
77 | n/a | import mymodule as sillyname |
---|
78 | n/a | from sys import version_info |
---|
79 | n/a | """] |
---|
80 | n/a | |
---|
81 | n/a | absolute_import_test = [ |
---|
82 | n/a | "a.module", |
---|
83 | n/a | ["a", "a.module", |
---|
84 | n/a | "b", "b.x", "b.y", "b.z", |
---|
85 | n/a | "__future__", "sys", "gc"], |
---|
86 | n/a | ["blahblah", "z"], [], |
---|
87 | n/a | """\ |
---|
88 | n/a | mymodule.py |
---|
89 | n/a | a/__init__.py |
---|
90 | n/a | a/module.py |
---|
91 | n/a | from __future__ import absolute_import |
---|
92 | n/a | import sys # sys |
---|
93 | n/a | import blahblah # fails |
---|
94 | n/a | import gc # gc |
---|
95 | n/a | import b.x # b.x |
---|
96 | n/a | from b import y # b.y |
---|
97 | n/a | from b.z import * # b.z.* |
---|
98 | n/a | a/gc.py |
---|
99 | n/a | a/sys.py |
---|
100 | n/a | import mymodule |
---|
101 | n/a | a/b/__init__.py |
---|
102 | n/a | a/b/x.py |
---|
103 | n/a | a/b/y.py |
---|
104 | n/a | a/b/z.py |
---|
105 | n/a | b/__init__.py |
---|
106 | n/a | import z |
---|
107 | n/a | b/unused.py |
---|
108 | n/a | b/x.py |
---|
109 | n/a | b/y.py |
---|
110 | n/a | b/z.py |
---|
111 | n/a | """] |
---|
112 | n/a | |
---|
113 | n/a | relative_import_test = [ |
---|
114 | n/a | "a.module", |
---|
115 | n/a | ["__future__", |
---|
116 | n/a | "a", "a.module", |
---|
117 | n/a | "a.b", "a.b.y", "a.b.z", |
---|
118 | n/a | "a.b.c", "a.b.c.moduleC", |
---|
119 | n/a | "a.b.c.d", "a.b.c.e", |
---|
120 | n/a | "a.b.x", |
---|
121 | n/a | "gc"], |
---|
122 | n/a | [], [], |
---|
123 | n/a | """\ |
---|
124 | n/a | mymodule.py |
---|
125 | n/a | a/__init__.py |
---|
126 | n/a | from .b import y, z # a.b.y, a.b.z |
---|
127 | n/a | a/module.py |
---|
128 | n/a | from __future__ import absolute_import # __future__ |
---|
129 | n/a | import gc # gc |
---|
130 | n/a | a/gc.py |
---|
131 | n/a | a/sys.py |
---|
132 | n/a | a/b/__init__.py |
---|
133 | n/a | from ..b import x # a.b.x |
---|
134 | n/a | #from a.b.c import moduleC |
---|
135 | n/a | from .c import moduleC # a.b.moduleC |
---|
136 | n/a | a/b/x.py |
---|
137 | n/a | a/b/y.py |
---|
138 | n/a | a/b/z.py |
---|
139 | n/a | a/b/g.py |
---|
140 | n/a | a/b/c/__init__.py |
---|
141 | n/a | from ..c import e # a.b.c.e |
---|
142 | n/a | a/b/c/moduleC.py |
---|
143 | n/a | from ..c import d # a.b.c.d |
---|
144 | n/a | a/b/c/d.py |
---|
145 | n/a | a/b/c/e.py |
---|
146 | n/a | a/b/c/x.py |
---|
147 | n/a | """] |
---|
148 | n/a | |
---|
149 | n/a | relative_import_test_2 = [ |
---|
150 | n/a | "a.module", |
---|
151 | n/a | ["a", "a.module", |
---|
152 | n/a | "a.sys", |
---|
153 | n/a | "a.b", "a.b.y", "a.b.z", |
---|
154 | n/a | "a.b.c", "a.b.c.d", |
---|
155 | n/a | "a.b.c.e", |
---|
156 | n/a | "a.b.c.moduleC", |
---|
157 | n/a | "a.b.c.f", |
---|
158 | n/a | "a.b.x", |
---|
159 | n/a | "a.another"], |
---|
160 | n/a | [], [], |
---|
161 | n/a | """\ |
---|
162 | n/a | mymodule.py |
---|
163 | n/a | a/__init__.py |
---|
164 | n/a | from . import sys # a.sys |
---|
165 | n/a | a/another.py |
---|
166 | n/a | a/module.py |
---|
167 | n/a | from .b import y, z # a.b.y, a.b.z |
---|
168 | n/a | a/gc.py |
---|
169 | n/a | a/sys.py |
---|
170 | n/a | a/b/__init__.py |
---|
171 | n/a | from .c import moduleC # a.b.c.moduleC |
---|
172 | n/a | from .c import d # a.b.c.d |
---|
173 | n/a | a/b/x.py |
---|
174 | n/a | a/b/y.py |
---|
175 | n/a | a/b/z.py |
---|
176 | n/a | a/b/c/__init__.py |
---|
177 | n/a | from . import e # a.b.c.e |
---|
178 | n/a | a/b/c/moduleC.py |
---|
179 | n/a | # |
---|
180 | n/a | from . import f # a.b.c.f |
---|
181 | n/a | from .. import x # a.b.x |
---|
182 | n/a | from ... import another # a.another |
---|
183 | n/a | a/b/c/d.py |
---|
184 | n/a | a/b/c/e.py |
---|
185 | n/a | a/b/c/f.py |
---|
186 | n/a | """] |
---|
187 | n/a | |
---|
188 | n/a | relative_import_test_3 = [ |
---|
189 | n/a | "a.module", |
---|
190 | n/a | ["a", "a.module"], |
---|
191 | n/a | ["a.bar"], |
---|
192 | n/a | [], |
---|
193 | n/a | """\ |
---|
194 | n/a | a/__init__.py |
---|
195 | n/a | def foo(): pass |
---|
196 | n/a | a/module.py |
---|
197 | n/a | from . import foo |
---|
198 | n/a | from . import bar |
---|
199 | n/a | """] |
---|
200 | n/a | |
---|
201 | n/a | relative_import_test_4 = [ |
---|
202 | n/a | "a.module", |
---|
203 | n/a | ["a", "a.module"], |
---|
204 | n/a | [], |
---|
205 | n/a | [], |
---|
206 | n/a | """\ |
---|
207 | n/a | a/__init__.py |
---|
208 | n/a | def foo(): pass |
---|
209 | n/a | a/module.py |
---|
210 | n/a | from . import * |
---|
211 | n/a | """] |
---|
212 | n/a | |
---|
213 | n/a | bytecode_test = [ |
---|
214 | n/a | "a", |
---|
215 | n/a | ["a"], |
---|
216 | n/a | [], |
---|
217 | n/a | [], |
---|
218 | n/a | "" |
---|
219 | n/a | ] |
---|
220 | n/a | |
---|
221 | n/a | |
---|
222 | n/a | def open_file(path): |
---|
223 | n/a | dirname = os.path.dirname(path) |
---|
224 | n/a | try: |
---|
225 | n/a | os.makedirs(dirname) |
---|
226 | n/a | except OSError as e: |
---|
227 | n/a | if e.errno != errno.EEXIST: |
---|
228 | n/a | raise |
---|
229 | n/a | return open(path, "w") |
---|
230 | n/a | |
---|
231 | n/a | |
---|
232 | n/a | def create_package(source): |
---|
233 | n/a | ofi = None |
---|
234 | n/a | try: |
---|
235 | n/a | for line in source.splitlines(): |
---|
236 | n/a | if line.startswith(" ") or line.startswith("\t"): |
---|
237 | n/a | ofi.write(line.strip() + "\n") |
---|
238 | n/a | else: |
---|
239 | n/a | if ofi: |
---|
240 | n/a | ofi.close() |
---|
241 | n/a | ofi = open_file(os.path.join(TEST_DIR, line.strip())) |
---|
242 | n/a | finally: |
---|
243 | n/a | if ofi: |
---|
244 | n/a | ofi.close() |
---|
245 | n/a | |
---|
246 | n/a | |
---|
247 | n/a | class ModuleFinderTest(unittest.TestCase): |
---|
248 | n/a | def _do_test(self, info, report=False, debug=0, replace_paths=[]): |
---|
249 | n/a | import_this, modules, missing, maybe_missing, source = info |
---|
250 | n/a | create_package(source) |
---|
251 | n/a | try: |
---|
252 | n/a | mf = modulefinder.ModuleFinder(path=TEST_PATH, debug=debug, |
---|
253 | n/a | replace_paths=replace_paths) |
---|
254 | n/a | mf.import_hook(import_this) |
---|
255 | n/a | if report: |
---|
256 | n/a | mf.report() |
---|
257 | n/a | ## # This wouldn't work in general when executed several times: |
---|
258 | n/a | ## opath = sys.path[:] |
---|
259 | n/a | ## sys.path = TEST_PATH |
---|
260 | n/a | ## try: |
---|
261 | n/a | ## __import__(import_this) |
---|
262 | n/a | ## except: |
---|
263 | n/a | ## import traceback; traceback.print_exc() |
---|
264 | n/a | ## sys.path = opath |
---|
265 | n/a | ## return |
---|
266 | n/a | modules = sorted(set(modules)) |
---|
267 | n/a | found = sorted(mf.modules) |
---|
268 | n/a | # check if we found what we expected, not more, not less |
---|
269 | n/a | self.assertEqual(found, modules) |
---|
270 | n/a | |
---|
271 | n/a | # check for missing and maybe missing modules |
---|
272 | n/a | bad, maybe = mf.any_missing_maybe() |
---|
273 | n/a | self.assertEqual(bad, missing) |
---|
274 | n/a | self.assertEqual(maybe, maybe_missing) |
---|
275 | n/a | finally: |
---|
276 | n/a | shutil.rmtree(TEST_DIR) |
---|
277 | n/a | |
---|
278 | n/a | def test_package(self): |
---|
279 | n/a | self._do_test(package_test) |
---|
280 | n/a | |
---|
281 | n/a | def test_maybe(self): |
---|
282 | n/a | self._do_test(maybe_test) |
---|
283 | n/a | |
---|
284 | n/a | def test_maybe_new(self): |
---|
285 | n/a | self._do_test(maybe_test_new) |
---|
286 | n/a | |
---|
287 | n/a | def test_absolute_imports(self): |
---|
288 | n/a | self._do_test(absolute_import_test) |
---|
289 | n/a | |
---|
290 | n/a | def test_relative_imports(self): |
---|
291 | n/a | self._do_test(relative_import_test) |
---|
292 | n/a | |
---|
293 | n/a | def test_relative_imports_2(self): |
---|
294 | n/a | self._do_test(relative_import_test_2) |
---|
295 | n/a | |
---|
296 | n/a | def test_relative_imports_3(self): |
---|
297 | n/a | self._do_test(relative_import_test_3) |
---|
298 | n/a | |
---|
299 | n/a | def test_relative_imports_4(self): |
---|
300 | n/a | self._do_test(relative_import_test_4) |
---|
301 | n/a | |
---|
302 | n/a | def test_bytecode(self): |
---|
303 | n/a | base_path = os.path.join(TEST_DIR, 'a') |
---|
304 | n/a | source_path = base_path + importlib.machinery.SOURCE_SUFFIXES[0] |
---|
305 | n/a | bytecode_path = base_path + importlib.machinery.BYTECODE_SUFFIXES[0] |
---|
306 | n/a | with open_file(source_path) as file: |
---|
307 | n/a | file.write('testing_modulefinder = True\n') |
---|
308 | n/a | py_compile.compile(source_path, cfile=bytecode_path) |
---|
309 | n/a | os.remove(source_path) |
---|
310 | n/a | self._do_test(bytecode_test) |
---|
311 | n/a | |
---|
312 | n/a | def test_replace_paths(self): |
---|
313 | n/a | old_path = os.path.join(TEST_DIR, 'a', 'module.py') |
---|
314 | n/a | new_path = os.path.join(TEST_DIR, 'a', 'spam.py') |
---|
315 | n/a | with support.captured_stdout() as output: |
---|
316 | n/a | self._do_test(maybe_test, debug=2, |
---|
317 | n/a | replace_paths=[(old_path, new_path)]) |
---|
318 | n/a | output = output.getvalue() |
---|
319 | n/a | expected = "co_filename %r changed to %r" % (old_path, new_path) |
---|
320 | n/a | self.assertIn(expected, output) |
---|
321 | n/a | |
---|
322 | n/a | def test_extended_opargs(self): |
---|
323 | n/a | extended_opargs_test = [ |
---|
324 | n/a | "a", |
---|
325 | n/a | ["a", "b"], |
---|
326 | n/a | [], [], |
---|
327 | n/a | """\ |
---|
328 | n/a | a.py |
---|
329 | n/a | %r |
---|
330 | n/a | import b |
---|
331 | n/a | b.py |
---|
332 | n/a | """ % list(range(2**16))] # 2**16 constants |
---|
333 | n/a | self._do_test(extended_opargs_test) |
---|
334 | n/a | |
---|
335 | n/a | |
---|
336 | n/a | if __name__ == "__main__": |
---|
337 | n/a | unittest.main() |
---|