1edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoepimport __future__
2edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoepimport os
3edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoepimport unittest
4edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoepimport distutils.dir_util
5edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoepimport tempfile
6edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep
7edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoepfrom test import test_support
8edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep
9edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoeptry: set
10edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoepexcept NameError: from sets import Set as set
11edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep
12edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoepimport modulefinder
13edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep
14edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep# Note: To test modulefinder with Python 2.2, sets.py and
15edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep# modulefinder.py must be available - they are not in the standard
16edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep# library.
17edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep
18edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander StoepTEST_DIR = tempfile.mkdtemp()
19edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander StoepTEST_PATH = [TEST_DIR, os.path.dirname(__future__.__file__)]
20edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep
21edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep# Each test description is a list of 5 items:
22edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep#
23edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep# 1. a module name that will be imported by modulefinder
24edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep# 2. a list of module names that modulefinder is required to find
25edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep# 3. a list of module names that modulefinder should complain
26edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep#    about because they are not found
27edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep# 4. a list of module names that modulefinder should complain
28edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep#    about because they MAY be not found
29edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep# 5. a string specifying packages to create; the format is obvious imo.
30edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep#
31edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep# Each package will be created in TEST_DIR, and TEST_DIR will be
32edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep# removed after the tests again.
33edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep# Modulefinder searches in a path that contains TEST_DIR, plus
34edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep# the standard Lib directory.
35edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep
36edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoepmaybe_test = [
37edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep    "a.module",
38edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep    ["a", "a.module", "sys",
39edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep     "b"],
40edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep    ["c"], ["b.something"],
41edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep    """\
42edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoepa/__init__.py
43edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoepa/module.py
44edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep                                from b import something
45edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep                                from c import something
46edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoepb/__init__.py
47edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep                                from sys import *
48edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep"""]
49edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep
50edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoepmaybe_test_new = [
51edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep    "a.module",
52edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep    ["a", "a.module", "sys",
53edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep     "b", "__future__"],
54edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep    ["c"], ["b.something"],
55edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep    """\
56edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoepa/__init__.py
57edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoepa/module.py
58edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep                                from b import something
59edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep                                from c import something
60edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoepb/__init__.py
61edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep                                from __future__ import absolute_import
62edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep                                from sys import *
63edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep"""]
64edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep
65edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoeppackage_test = [
66edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep    "a.module",
67edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep    ["a", "a.b", "a.c", "a.module", "mymodule", "sys"],
68edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep    ["blahblah"], [],
69edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep    """\
70edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoepmymodule.py
71edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoepa/__init__.py
72edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep                                import blahblah
73edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep                                from a import b
74edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep                                import c
75edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoepa/module.py
76edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep                                import sys
77edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep                                from a import b as x
78edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep                                from a.c import sillyname
79edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoepa/b.py
80edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoepa/c.py
81edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep                                from a.module import x
82edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep                                import mymodule as sillyname
83edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep                                from sys import version_info
84edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep"""]
85edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep
86edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoepabsolute_import_test = [
87edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep    "a.module",
88edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep    ["a", "a.module",
89edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep     "b", "b.x", "b.y", "b.z",
90edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep     "__future__", "sys", "exceptions"],
91edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep    ["blahblah"], [],
92edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep    """\
93edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoepmymodule.py
94edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoepa/__init__.py
95edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoepa/module.py
96edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep                                from __future__ import absolute_import
97edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep                                import sys # sys
98edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep                                import blahblah # fails
99edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep                                import exceptions # exceptions
100edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep                                import b.x # b.x
101edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep                                from b import y # b.y
102edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep                                from b.z import * # b.z.*
103edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoepa/exceptions.py
104edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoepa/sys.py
105edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep                                import mymodule
106edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoepa/b/__init__.py
107edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoepa/b/x.py
108edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoepa/b/y.py
109edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoepa/b/z.py
110edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoepb/__init__.py
111edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep                                import z
112edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoepb/unused.py
113edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoepb/x.py
114edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoepb/y.py
115edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoepb/z.py
116edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep"""]
117edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep
118edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoeprelative_import_test = [
119edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep    "a.module",
120edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep    ["__future__",
121edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep     "a", "a.module",
122edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep     "a.b", "a.b.y", "a.b.z",
123edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep     "a.b.c", "a.b.c.moduleC",
124edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep     "a.b.c.d", "a.b.c.e",
125edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep     "a.b.x",
126edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep     "exceptions"],
127edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep    [], [],
128edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep    """\
129edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoepmymodule.py
130edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoepa/__init__.py
131edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep                                from .b import y, z # a.b.y, a.b.z
132edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoepa/module.py
133edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep                                from __future__ import absolute_import # __future__
134edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep                                import exceptions # exceptions
135edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoepa/exceptions.py
136edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoepa/sys.py
137edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoepa/b/__init__.py
138edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep                                from ..b import x # a.b.x
139edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep                                #from a.b.c import moduleC
140edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep                                from .c import moduleC # a.b.moduleC
141edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoepa/b/x.py
142edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoepa/b/y.py
143edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoepa/b/z.py
144edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoepa/b/g.py
145edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoepa/b/c/__init__.py
146edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep                                from ..c import e # a.b.c.e
147edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoepa/b/c/moduleC.py
148edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep                                from ..c import d # a.b.c.d
149edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoepa/b/c/d.py
150edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoepa/b/c/e.py
151edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoepa/b/c/x.py
152edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep"""]
153edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep
154edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoeprelative_import_test_2 = [
155edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep    "a.module",
156edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep    ["a", "a.module",
157edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep     "a.sys",
158edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep     "a.b", "a.b.y", "a.b.z",
159edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep     "a.b.c", "a.b.c.d",
160edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep     "a.b.c.e",
161edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep     "a.b.c.moduleC",
162edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep     "a.b.c.f",
163edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep     "a.b.x",
164edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep     "a.another"],
165edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep    [], [],
166edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep    """\
167edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoepmymodule.py
168edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoepa/__init__.py
169edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep                                from . import sys # a.sys
170edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoepa/another.py
171edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoepa/module.py
172edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep                                from .b import y, z # a.b.y, a.b.z
173edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoepa/exceptions.py
174edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoepa/sys.py
175edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoepa/b/__init__.py
176edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep                                from .c import moduleC # a.b.c.moduleC
177edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep                                from .c import d # a.b.c.d
178edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoepa/b/x.py
179edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoepa/b/y.py
180edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoepa/b/z.py
181edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoepa/b/c/__init__.py
182edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep                                from . import e # a.b.c.e
183edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoepa/b/c/moduleC.py
184edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep                                #
185edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep                                from . import f   # a.b.c.f
186edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep                                from .. import x  # a.b.x
187edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep                                from ... import another # a.another
188edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoepa/b/c/d.py
189edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoepa/b/c/e.py
190edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoepa/b/c/f.py
191edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep"""]
192edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep
193edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoeprelative_import_test_3 = [
194edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep    "a.module",
195edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep    ["a", "a.module"],
196edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep    ["a.bar"],
197edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep    [],
198edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep    """\
199edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoepa/__init__.py
200edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep                                def foo(): pass
201edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoepa/module.py
202edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep                                from . import foo
203edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep                                from . import bar
204edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep"""]
205edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep
206edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoepdef open_file(path):
207edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep    ##print "#", os.path.abspath(path)
208edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep    dirname = os.path.dirname(path)
209edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep    distutils.dir_util.mkpath(dirname)
210edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep    return open(path, "w")
211edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep
212edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoepdef create_package(source):
213edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep    ofi = None
214edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep    try:
215edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep        for line in source.splitlines():
216edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep            if line.startswith(" ") or line.startswith("\t"):
217edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep                ofi.write(line.strip() + "\n")
218edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep            else:
219edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep                if ofi:
220edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep                    ofi.close()
221edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep                ofi = open_file(os.path.join(TEST_DIR, line.strip()))
222edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep    finally:
223edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep        if ofi:
224edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep            ofi.close()
225edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep
226edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoepclass ModuleFinderTest(unittest.TestCase):
227edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep    def _do_test(self, info, report=False):
228edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep        import_this, modules, missing, maybe_missing, source = info
229edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep        create_package(source)
230edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep        try:
231edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep            mf = modulefinder.ModuleFinder(path=TEST_PATH)
232edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep            mf.import_hook(import_this)
233edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep            if report:
234edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep                mf.report()
235edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep##                # This wouldn't work in general when executed several times:
236edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep##                opath = sys.path[:]
237edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep##                sys.path = TEST_PATH
238edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep##                try:
239edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep##                    __import__(import_this)
240edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep##                except:
241edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep##                    import traceback; traceback.print_exc()
242edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep##                sys.path = opath
243edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep##                return
244edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep            modules = set(modules)
245edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep            found = set(mf.modules.keys())
246edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep            more = list(found - modules)
247edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep            less = list(modules - found)
248edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep            # check if we found what we expected, not more, not less
249edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep            self.assertEqual((more, less), ([], []))
250edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep
251edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep            # check for missing and maybe missing modules
252edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep            bad, maybe = mf.any_missing_maybe()
253edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep            self.assertEqual(bad, missing)
254edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep            self.assertEqual(maybe, maybe_missing)
255edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep        finally:
256edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep            distutils.dir_util.remove_tree(TEST_DIR)
257edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep
258edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep    def test_package(self):
259edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep        self._do_test(package_test)
260edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep
261edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep    def test_maybe(self):
262edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep        self._do_test(maybe_test)
263edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep
264edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep    if getattr(__future__, "absolute_import", None):
265edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep
266edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep        def test_maybe_new(self):
267edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep            self._do_test(maybe_test_new)
268edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep
269edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep        def test_absolute_imports(self):
270edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep            self._do_test(absolute_import_test)
271edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep
272edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep        def test_relative_imports(self):
273edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep            self._do_test(relative_import_test)
274edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep
275edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep        def test_relative_imports_2(self):
276edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep            self._do_test(relative_import_test_2)
277edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep
278edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep        def test_relative_imports_3(self):
279edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep            self._do_test(relative_import_test_3)
280edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep
281edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoepdef test_main():
282edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep    distutils.log.set_threshold(distutils.log.WARN)
283edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep    test_support.run_unittest(ModuleFinderTest)
284edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep
285edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoepif __name__ == "__main__":
286edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep    unittest.main()
287