blob: a1842178df1a38490e5ea90db2d43674738e70a8 [file] [log] [blame]
Guido van Rossumfc2a0a82006-10-27 23:06:01 +00001import __future__
Christian Heimes05e8be12008-02-23 18:30:17 +00002import os
Guido van Rossumfc2a0a82006-10-27 23:06:01 +00003import unittest
4import distutils.dir_util
5import tempfile
6
Benjamin Petersonee8712c2008-05-20 21:35:26 +00007from test import support
Guido van Rossumfc2a0a82006-10-27 23:06:01 +00008
Guido van Rossumfc2a0a82006-10-27 23:06:01 +00009import modulefinder
10
Guido van Rossumfc2a0a82006-10-27 23:06:01 +000011TEST_DIR = tempfile.mkdtemp()
12TEST_PATH = [TEST_DIR, os.path.dirname(__future__.__file__)]
13
14# Each test description is a list of 5 items:
15#
16# 1. a module name that will be imported by modulefinder
17# 2. a list of module names that modulefinder is required to find
18# 3. a list of module names that modulefinder should complain
19# about because they are not found
20# 4. a list of module names that modulefinder should complain
21# about because they MAY be not found
22# 5. a string specifying packages to create; the format is obvious imo.
23#
24# Each package will be created in TEST_DIR, and TEST_DIR will be
25# removed after the tests again.
26# Modulefinder searches in a path that contains TEST_DIR, plus
27# the standard Lib directory.
28
29maybe_test = [
30 "a.module",
31 ["a", "a.module", "sys",
32 "b"],
33 ["c"], ["b.something"],
34 """\
35a/__init__.py
36a/module.py
37 from b import something
38 from c import something
39b/__init__.py
40 from sys import *
41"""]
42
43maybe_test_new = [
44 "a.module",
45 ["a", "a.module", "sys",
Thomas Wouters89f507f2006-12-13 04:49:30 +000046 "b", "__future__"],
Guido van Rossumfc2a0a82006-10-27 23:06:01 +000047 ["c"], ["b.something"],
48 """\
49a/__init__.py
50a/module.py
51 from b import something
52 from c import something
53b/__init__.py
Thomas Wouters89f507f2006-12-13 04:49:30 +000054 from __future__ import absolute_import
Guido van Rossumfc2a0a82006-10-27 23:06:01 +000055 from sys import *
56"""]
57
58package_test = [
59 "a.module",
60 ["a", "a.b", "a.c", "a.module", "mymodule", "sys"],
61 ["blahblah", "c"], [],
62 """\
63mymodule.py
64a/__init__.py
65 import blahblah
66 from a import b
67 import c
68a/module.py
69 import sys
70 from a import b as x
71 from a.c import sillyname
72a/b.py
73a/c.py
74 from a.module import x
75 import mymodule as sillyname
76 from sys import version_info
77"""]
78
79absolute_import_test = [
80 "a.module",
81 ["a", "a.module",
82 "b", "b.x", "b.y", "b.z",
Neal Norwitz2633c692007-02-26 22:22:47 +000083 "__future__", "sys", "gc"],
Guido van Rossumfc2a0a82006-10-27 23:06:01 +000084 ["blahblah", "z"], [],
85 """\
86mymodule.py
87a/__init__.py
88a/module.py
Thomas Wouters89f507f2006-12-13 04:49:30 +000089 from __future__ import absolute_import
Guido van Rossumfc2a0a82006-10-27 23:06:01 +000090 import sys # sys
91 import blahblah # fails
Neal Norwitz2633c692007-02-26 22:22:47 +000092 import gc # gc
Guido van Rossumfc2a0a82006-10-27 23:06:01 +000093 import b.x # b.x
94 from b import y # b.y
95 from b.z import * # b.z.*
Neal Norwitz2633c692007-02-26 22:22:47 +000096a/gc.py
Guido van Rossumfc2a0a82006-10-27 23:06:01 +000097a/sys.py
98 import mymodule
99a/b/__init__.py
100a/b/x.py
101a/b/y.py
102a/b/z.py
103b/__init__.py
104 import z
105b/unused.py
106b/x.py
107b/y.py
108b/z.py
109"""]
110
111relative_import_test = [
112 "a.module",
Thomas Wouters89f507f2006-12-13 04:49:30 +0000113 ["__future__",
114 "a", "a.module",
Guido van Rossumfc2a0a82006-10-27 23:06:01 +0000115 "a.b", "a.b.y", "a.b.z",
116 "a.b.c", "a.b.c.moduleC",
117 "a.b.c.d", "a.b.c.e",
118 "a.b.x",
Neal Norwitz2633c692007-02-26 22:22:47 +0000119 "gc"],
Guido van Rossumfc2a0a82006-10-27 23:06:01 +0000120 [], [],
121 """\
122mymodule.py
123a/__init__.py
124 from .b import y, z # a.b.y, a.b.z
125a/module.py
Thomas Wouters89f507f2006-12-13 04:49:30 +0000126 from __future__ import absolute_import # __future__
Neal Norwitz2633c692007-02-26 22:22:47 +0000127 import gc # gc
128a/gc.py
Guido van Rossumfc2a0a82006-10-27 23:06:01 +0000129a/sys.py
130a/b/__init__.py
131 from ..b import x # a.b.x
132 #from a.b.c import moduleC
133 from .c import moduleC # a.b.moduleC
134a/b/x.py
135a/b/y.py
136a/b/z.py
137a/b/g.py
138a/b/c/__init__.py
139 from ..c import e # a.b.c.e
140a/b/c/moduleC.py
141 from ..c import d # a.b.c.d
142a/b/c/d.py
143a/b/c/e.py
144a/b/c/x.py
145"""]
146
147relative_import_test_2 = [
148 "a.module",
149 ["a", "a.module",
150 "a.sys",
151 "a.b", "a.b.y", "a.b.z",
152 "a.b.c", "a.b.c.d",
153 "a.b.c.e",
154 "a.b.c.moduleC",
155 "a.b.c.f",
156 "a.b.x",
157 "a.another"],
158 [], [],
159 """\
160mymodule.py
161a/__init__.py
162 from . import sys # a.sys
163a/another.py
164a/module.py
165 from .b import y, z # a.b.y, a.b.z
Neal Norwitz2633c692007-02-26 22:22:47 +0000166a/gc.py
Guido van Rossumfc2a0a82006-10-27 23:06:01 +0000167a/sys.py
168a/b/__init__.py
169 from .c import moduleC # a.b.c.moduleC
170 from .c import d # a.b.c.d
171a/b/x.py
172a/b/y.py
173a/b/z.py
174a/b/c/__init__.py
175 from . import e # a.b.c.e
176a/b/c/moduleC.py
177 #
178 from . import f # a.b.c.f
179 from .. import x # a.b.x
180 from ... import another # a.another
181a/b/c/d.py
182a/b/c/e.py
183a/b/c/f.py
184"""]
185
Benjamin Petersonc0747cf2008-11-03 20:31:38 +0000186relative_import_test_3 = [
187 "a.module",
188 ["a", "a.module"],
189 ["a.bar"],
190 [],
191 """\
192a/__init__.py
193 def foo(): pass
194a/module.py
195 from . import foo
196 from . import bar
197"""]
198
Guido van Rossumfc2a0a82006-10-27 23:06:01 +0000199def open_file(path):
200 ##print "#", os.path.abspath(path)
201 dirname = os.path.dirname(path)
202 distutils.dir_util.mkpath(dirname)
203 return open(path, "w")
204
205def create_package(source):
206 ofi = None
Antoine Pitrou92f60ed2010-10-14 22:11:44 +0000207 try:
208 for line in source.splitlines():
209 if line.startswith(" ") or line.startswith("\t"):
210 ofi.write(line.strip() + "\n")
211 else:
212 if ofi:
213 ofi.close()
214 ofi = open_file(os.path.join(TEST_DIR, line.strip()))
215 finally:
216 if ofi:
217 ofi.close()
Guido van Rossumfc2a0a82006-10-27 23:06:01 +0000218
219class ModuleFinderTest(unittest.TestCase):
220 def _do_test(self, info, report=False):
221 import_this, modules, missing, maybe_missing, source = info
222 create_package(source)
223 try:
224 mf = modulefinder.ModuleFinder(path=TEST_PATH)
225 mf.import_hook(import_this)
226 if report:
227 mf.report()
228## # This wouldn't work in general when executed several times:
229## opath = sys.path[:]
230## sys.path = TEST_PATH
231## try:
232## __import__(import_this)
233## except:
234## import traceback; traceback.print_exc()
235## sys.path = opath
236## return
237 modules = set(modules)
238 found = set(mf.modules.keys())
239 more = list(found - modules)
240 less = list(modules - found)
241 # check if we found what we expected, not more, not less
Benjamin Petersonc9c0f202009-06-30 23:06:06 +0000242 self.assertEqual((more, less), ([], []))
Guido van Rossumfc2a0a82006-10-27 23:06:01 +0000243
244 # check for missing and maybe missing modules
245 bad, maybe = mf.any_missing_maybe()
Benjamin Petersonc9c0f202009-06-30 23:06:06 +0000246 self.assertEqual(bad, missing)
247 self.assertEqual(maybe, maybe_missing)
Guido van Rossumfc2a0a82006-10-27 23:06:01 +0000248 finally:
249 distutils.dir_util.remove_tree(TEST_DIR)
250
251 def test_package(self):
252 self._do_test(package_test)
253
254 def test_maybe(self):
255 self._do_test(maybe_test)
256
257 if getattr(__future__, "absolute_import", None):
258
259 def test_maybe_new(self):
260 self._do_test(maybe_test_new)
261
262 def test_absolute_imports(self):
263 self._do_test(absolute_import_test)
264
265 def test_relative_imports(self):
266 self._do_test(relative_import_test)
267
268 def test_relative_imports_2(self):
269 self._do_test(relative_import_test_2)
270
Benjamin Petersonc0747cf2008-11-03 20:31:38 +0000271 def test_relative_imports_3(self):
272 self._do_test(relative_import_test_3)
273
Guido van Rossumfc2a0a82006-10-27 23:06:01 +0000274def test_main():
Thomas Wouters89f507f2006-12-13 04:49:30 +0000275 distutils.log.set_threshold(distutils.log.WARN)
Benjamin Petersonee8712c2008-05-20 21:35:26 +0000276 support.run_unittest(ModuleFinderTest)
Guido van Rossumfc2a0a82006-10-27 23:06:01 +0000277
278if __name__ == "__main__":
279 unittest.main()