4 import distutils
.dir_util
7 from test
import test_support
10 except NameError: from sets
import Set
as set
14 # Note: To test modulefinder with Python 2.2, sets.py and
15 # modulefinder.py must be available - they are not in the standard
18 TEST_DIR
= tempfile
.mkdtemp()
19 TEST_PATH
= [TEST_DIR
, os
.path
.dirname(__future__
.__file
__)]
21 # Each test description is a list of 5 items:
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.
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.
38 ["a", "a.module", "sys",
40 ["c"], ["b.something"],
44 from b import something
45 from c import something
52 ["a", "a.module", "sys",
54 ["c"], ["b.something"],
58 from b import something
59 from c import something
61 from __future__ import absolute_import
67 ["a", "a.b", "a.c", "a.module", "mymodule", "sys"],
78 from a.c import sillyname
81 from a.module import x
82 import mymodule as sillyname
83 from sys import version_info
86 absolute_import_test
= [
89 "b", "b.x", "b.y", "b.z",
90 "__future__", "sys", "exceptions"],
96 from __future__ import absolute_import
98 import blahblah # fails
99 import exceptions # exceptions
101 from b import y # b.y
102 from b.z import * # b.z.*
118 relative_import_test
= [
122 "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",
131 from .b import y, z # a.b.y, a.b.z
133 from __future__ import absolute_import # __future__
134 import exceptions # exceptions
138 from ..b import x # a.b.x
139 #from a.b.c import moduleC
140 from .c import moduleC # a.b.moduleC
146 from ..c import e # a.b.c.e
148 from ..c import d # a.b.c.d
154 relative_import_test_2
= [
158 "a.b", "a.b.y", "a.b.z",
169 from . import sys # a.sys
172 from .b import y, z # a.b.y, a.b.z
176 from .c import moduleC # a.b.c.moduleC
177 from .c import d # a.b.c.d
182 from . import e # a.b.c.e
185 from . import f # a.b.c.f
186 from .. import x # a.b.x
187 from ... import another # a.another
193 relative_import_test_3
= [
207 ##print "#", os.path.abspath(path)
208 dirname
= os
.path
.dirname(path
)
209 distutils
.dir_util
.mkpath(dirname
)
210 return open(path
, "w")
212 def create_package(source
):
214 for line
in source
.splitlines():
215 if line
.startswith(" ") or line
.startswith("\t"):
216 ofi
.write(line
.strip() + "\n")
218 ofi
= open_file(os
.path
.join(TEST_DIR
, line
.strip()))
220 class ModuleFinderTest(unittest
.TestCase
):
221 def _do_test(self
, info
, report
=False):
222 import_this
, modules
, missing
, maybe_missing
, source
= info
223 create_package(source
)
225 mf
= modulefinder
.ModuleFinder(path
=TEST_PATH
)
226 mf
.import_hook(import_this
)
229 ## # This wouldn't work in general when executed several times:
230 ## opath = sys.path[:]
231 ## sys.path = TEST_PATH
233 ## __import__(import_this)
235 ## import traceback; traceback.print_exc()
238 modules
= set(modules
)
239 found
= set(mf
.modules
.keys())
240 more
= list(found
- modules
)
241 less
= list(modules
- found
)
242 # check if we found what we expected, not more, not less
243 self
.assertEqual((more
, less
), ([], []))
245 # check for missing and maybe missing modules
246 bad
, maybe
= mf
.any_missing_maybe()
247 self
.assertEqual(bad
, missing
)
248 self
.assertEqual(maybe
, maybe_missing
)
250 distutils
.dir_util
.remove_tree(TEST_DIR
)
252 def test_package(self
):
253 self
._do
_test
(package_test
)
255 def test_maybe(self
):
256 self
._do
_test
(maybe_test
)
258 if getattr(__future__
, "absolute_import", None):
260 def test_maybe_new(self
):
261 self
._do
_test
(maybe_test_new
)
263 def test_absolute_imports(self
):
264 self
._do
_test
(absolute_import_test
)
266 def test_relative_imports(self
):
267 self
._do
_test
(relative_import_test
)
269 def test_relative_imports_2(self
):
270 self
._do
_test
(relative_import_test_2
)
272 def test_relative_imports_3(self
):
273 self
._do
_test
(relative_import_test_3
)
276 distutils
.log
.set_threshold(distutils
.log
.WARN
)
277 test_support
.run_unittest(ModuleFinderTest
)
279 if __name__
== "__main__":