blob: 80c06d7c30d18d5220e1987880cf4d89ac306603 [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
7from test import test_support
8
9try: set
10except NameError: from sets import Set as set
11
12import modulefinder
13
14# Note: To test modulefinder with Python 2.2, sets.py and
15# modulefinder.py must be available - they are not in the standard
16# library.
17
18TEST_DIR = tempfile.mkdtemp()
19TEST_PATH = [TEST_DIR, os.path.dirname(__future__.__file__)]
20
21# Each test description is a list of 5 items:
22#
23# 1. a module name that will be imported by modulefinder
24# 2. a list of module names that modulefinder is required to find
25# 3. a list of module names that modulefinder should complain
26# about because they are not found
27# 4. a list of module names that modulefinder should complain
28# about because they MAY be not found
29# 5. a string specifying packages to create; the format is obvious imo.
30#
31# Each package will be created in TEST_DIR, and TEST_DIR will be
32# removed after the tests again.
33# Modulefinder searches in a path that contains TEST_DIR, plus
34# the standard Lib directory.
35
36maybe_test = [
37 "a.module",
38 ["a", "a.module", "sys",
39 "b"],
40 ["c"], ["b.something"],
41 """\
42a/__init__.py
43a/module.py
44 from b import something
45 from c import something
46b/__init__.py
47 from sys import *
48"""]
49
50maybe_test_new = [
51 "a.module",
52 ["a", "a.module", "sys",
Thomas Wouters89f507f2006-12-13 04:49:30 +000053 "b", "__future__"],
Guido van Rossumfc2a0a82006-10-27 23:06:01 +000054 ["c"], ["b.something"],
55 """\
56a/__init__.py
57a/module.py
58 from b import something
59 from c import something
60b/__init__.py
Thomas Wouters89f507f2006-12-13 04:49:30 +000061 from __future__ import absolute_import
Guido van Rossumfc2a0a82006-10-27 23:06:01 +000062 from sys import *
63"""]
64
65package_test = [
66 "a.module",
67 ["a", "a.b", "a.c", "a.module", "mymodule", "sys"],
68 ["blahblah", "c"], [],
69 """\
70mymodule.py
71a/__init__.py
72 import blahblah
73 from a import b
74 import c
75a/module.py
76 import sys
77 from a import b as x
78 from a.c import sillyname
79a/b.py
80a/c.py
81 from a.module import x
82 import mymodule as sillyname
83 from sys import version_info
84"""]
85
86absolute_import_test = [
87 "a.module",
88 ["a", "a.module",
89 "b", "b.x", "b.y", "b.z",
Neal Norwitz2633c692007-02-26 22:22:47 +000090 "__future__", "sys", "gc"],
Guido van Rossumfc2a0a82006-10-27 23:06:01 +000091 ["blahblah", "z"], [],
92 """\
93mymodule.py
94a/__init__.py
95a/module.py
Thomas Wouters89f507f2006-12-13 04:49:30 +000096 from __future__ import absolute_import
Guido van Rossumfc2a0a82006-10-27 23:06:01 +000097 import sys # sys
98 import blahblah # fails
Neal Norwitz2633c692007-02-26 22:22:47 +000099 import gc # gc
Guido van Rossumfc2a0a82006-10-27 23:06:01 +0000100 import b.x # b.x
101 from b import y # b.y
102 from b.z import * # b.z.*
Neal Norwitz2633c692007-02-26 22:22:47 +0000103a/gc.py
Guido van Rossumfc2a0a82006-10-27 23:06:01 +0000104a/sys.py
105 import mymodule
106a/b/__init__.py
107a/b/x.py
108a/b/y.py
109a/b/z.py
110b/__init__.py
111 import z
112b/unused.py
113b/x.py
114b/y.py
115b/z.py
116"""]
117
118relative_import_test = [
119 "a.module",
Thomas Wouters89f507f2006-12-13 04:49:30 +0000120 ["__future__",
121 "a", "a.module",
Guido van Rossumfc2a0a82006-10-27 23:06:01 +0000122 "a.b", "a.b.y", "a.b.z",
123 "a.b.c", "a.b.c.moduleC",
124 "a.b.c.d", "a.b.c.e",
125 "a.b.x",
Neal Norwitz2633c692007-02-26 22:22:47 +0000126 "gc"],
Guido van Rossumfc2a0a82006-10-27 23:06:01 +0000127 [], [],
128 """\
129mymodule.py
130a/__init__.py
131 from .b import y, z # a.b.y, a.b.z
132a/module.py
Thomas Wouters89f507f2006-12-13 04:49:30 +0000133 from __future__ import absolute_import # __future__
Neal Norwitz2633c692007-02-26 22:22:47 +0000134 import gc # gc
135a/gc.py
Guido van Rossumfc2a0a82006-10-27 23:06:01 +0000136a/sys.py
137a/b/__init__.py
138 from ..b import x # a.b.x
139 #from a.b.c import moduleC
140 from .c import moduleC # a.b.moduleC
141a/b/x.py
142a/b/y.py
143a/b/z.py
144a/b/g.py
145a/b/c/__init__.py
146 from ..c import e # a.b.c.e
147a/b/c/moduleC.py
148 from ..c import d # a.b.c.d
149a/b/c/d.py
150a/b/c/e.py
151a/b/c/x.py
152"""]
153
154relative_import_test_2 = [
155 "a.module",
156 ["a", "a.module",
157 "a.sys",
158 "a.b", "a.b.y", "a.b.z",
159 "a.b.c", "a.b.c.d",
160 "a.b.c.e",
161 "a.b.c.moduleC",
162 "a.b.c.f",
163 "a.b.x",
164 "a.another"],
165 [], [],
166 """\
167mymodule.py
168a/__init__.py
169 from . import sys # a.sys
170a/another.py
171a/module.py
172 from .b import y, z # a.b.y, a.b.z
Neal Norwitz2633c692007-02-26 22:22:47 +0000173a/gc.py
Guido van Rossumfc2a0a82006-10-27 23:06:01 +0000174a/sys.py
175a/b/__init__.py
176 from .c import moduleC # a.b.c.moduleC
177 from .c import d # a.b.c.d
178a/b/x.py
179a/b/y.py
180a/b/z.py
181a/b/c/__init__.py
182 from . import e # a.b.c.e
183a/b/c/moduleC.py
184 #
185 from . import f # a.b.c.f
186 from .. import x # a.b.x
187 from ... import another # a.another
188a/b/c/d.py
189a/b/c/e.py
190a/b/c/f.py
191"""]
192
193def open_file(path):
194 ##print "#", os.path.abspath(path)
195 dirname = os.path.dirname(path)
196 distutils.dir_util.mkpath(dirname)
197 return open(path, "w")
198
199def create_package(source):
200 ofi = None
201 for line in source.splitlines():
202 if line.startswith(" ") or line.startswith("\t"):
203 ofi.write(line.strip() + "\n")
204 else:
205 ofi = open_file(os.path.join(TEST_DIR, line.strip()))
206
207class ModuleFinderTest(unittest.TestCase):
208 def _do_test(self, info, report=False):
209 import_this, modules, missing, maybe_missing, source = info
210 create_package(source)
211 try:
212 mf = modulefinder.ModuleFinder(path=TEST_PATH)
213 mf.import_hook(import_this)
214 if report:
215 mf.report()
216## # This wouldn't work in general when executed several times:
217## opath = sys.path[:]
218## sys.path = TEST_PATH
219## try:
220## __import__(import_this)
221## except:
222## import traceback; traceback.print_exc()
223## sys.path = opath
224## return
225 modules = set(modules)
226 found = set(mf.modules.keys())
227 more = list(found - modules)
228 less = list(modules - found)
229 # check if we found what we expected, not more, not less
230 self.failUnlessEqual((more, less), ([], []))
231
232 # check for missing and maybe missing modules
233 bad, maybe = mf.any_missing_maybe()
234 self.failUnlessEqual(bad, missing)
235 self.failUnlessEqual(maybe, maybe_missing)
236 finally:
237 distutils.dir_util.remove_tree(TEST_DIR)
238
239 def test_package(self):
240 self._do_test(package_test)
241
242 def test_maybe(self):
243 self._do_test(maybe_test)
244
245 if getattr(__future__, "absolute_import", None):
246
247 def test_maybe_new(self):
248 self._do_test(maybe_test_new)
249
250 def test_absolute_imports(self):
251 self._do_test(absolute_import_test)
252
253 def test_relative_imports(self):
254 self._do_test(relative_import_test)
255
256 def test_relative_imports_2(self):
257 self._do_test(relative_import_test_2)
258
259def test_main():
Thomas Wouters89f507f2006-12-13 04:49:30 +0000260 distutils.log.set_threshold(distutils.log.WARN)
Guido van Rossumfc2a0a82006-10-27 23:06:01 +0000261 test_support.run_unittest(ModuleFinderTest)
262
263if __name__ == "__main__":
264 unittest.main()