blob: 0356cfa986e1991401b765b73530764fa7406011 [file] [log] [blame]
Guido van Rossum6c612421997-09-06 18:42:57 +00001# Test packages (dotted-name import)
2
3import sys, os, string, tempfile, traceback
4from os import mkdir, rmdir # Can't test if these fail
5del mkdir, rmdir
6from test_support import verbose
Guido van Rossum6c612421997-09-06 18:42:57 +00007
8# Helpers to create and destroy hierarchies.
9
10def mkhier(root, descr):
11 mkdir(root)
12 for name, contents in descr:
13 comps = string.split(name)
14 fullname = root
15 for c in comps:
16 fullname = os.path.join(fullname, c)
17 if contents is None:
18 mkdir(fullname)
19 else:
20 if verbose: print "write", fullname
21 f = open(fullname, "w")
22 f.write(contents)
23 if contents and contents[-1] != '\n':
24 f.write('\n')
25 f.close()
26
27def mkdir(x):
28 if verbose: print "mkdir", x
29 os.mkdir(x)
30
31def cleanout(root):
32 names = os.listdir(root)
33 for name in names:
34 fullname = os.path.join(root, name)
35 if os.path.isdir(fullname) and not os.path.islink(fullname):
36 cleanout(fullname)
37 else:
38 os.remove(fullname)
39 rmdir(root)
40
41def rmdir(x):
42 if verbose: print "rmdir", x
43 os.rmdir(x)
44
45# Helper to run a test
46
47def runtest(hier, code):
48 root = tempfile.mktemp()
49 mkhier(root, hier)
50 savepath = sys.path[:]
51 codefile = tempfile.mktemp()
52 f = open(codefile, "w")
53 f.write(code)
54 f.close()
55 try:
56 sys.path.insert(0, root)
57 if verbose: print "sys.path =", sys.path
58 try:
59 execfile(codefile, globals(), {})
60 except:
Guido van Rossum10887a31997-09-07 06:12:11 +000061 traceback.print_exc(file=sys.stdout)
Guido van Rossum6c612421997-09-06 18:42:57 +000062 finally:
63 sys.path[:] = savepath
64 try:
65 cleanout(root)
66 except (os.error, IOError):
67 pass
68 os.remove(codefile)
69
70# Test descriptions
71
72tests = [
Guido van Rossum10887a31997-09-07 06:12:11 +000073 ("t1", [("t1", None)], "import t1"),
Guido van Rossum6c612421997-09-06 18:42:57 +000074
75 ("t2", [
76 ("t2", None),
77 ("t2 __init__.py", "'doc for t2'; print __name__, 'loading'"),
78 ("t2 sub", None),
79 ("t2 sub subsub", None),
80 ("t2 sub subsub __init__.py", "print __name__, 'loading'; spam = 1"),
81 ],
82"""
83import t2
84print t2.__doc__
85import t2.sub
86import t2.sub.subsub
87print t2.__name__, t2.sub.__name__, t2.sub.subsub.__name__
88import t2
89from t2 import *
90print dir()
91from t2 import sub
92from t2.sub import subsub
93from t2.sub.subsub import spam
94print sub.__name__, subsub.__name__
95print sub.subsub.__name__
96print dir()
97import t2.sub
98import t2.sub.subsub
99print t2.__name__, t2.sub.__name__, t2.sub.subsub.__name__
100from t2 import *
101print dir()
102"""),
103
104 ("t3", [
105 ("t3", None),
106 ("t3 __init__.py", "print __name__, 'loading'"),
107 ("t3 sub", None),
108 ("t3 sub subsub", None),
109 ("t3 sub subsub __init__.py", "print __name__, 'loading'; spam = 1"),
110 ],
111"""
112import t3.sub.subsub
113print t3.__name__, t3.sub.__name__, t3.sub.subsub.__name__
Guido van Rossum81da02e1997-09-06 19:58:53 +0000114reload(t3)
115reload(t3.sub)
116reload(t3.sub.subsub)
Guido van Rossum6c612421997-09-06 18:42:57 +0000117"""),
118
119 ("t4", [
120 ("t4.py", "print 'THIS SHOULD NOT BE PRINTED (t4.py)'"),
121 ("t4", None),
122 ("t4 __init__.py", "print __name__, 'loading'"),
123 ("t4 sub.py", "print 'THIS SHOULD NOT BE PRINTED (sub.py)'"),
124 ("t4 sub", None),
125 ("t4 sub subsub.py", "print 'THIS SHOULD NOT BE PRINTED (subsub.py)'"),
126 ("t4 sub subsub", None),
127 ("t4 sub subsub __init__.py", "print __name__, 'loading'; spam = 1"),
128 ],
129"""
130from t4.sub.subsub import *
131print "t4.sub.subsub.spam =", spam
132"""),
133
134 ("t5", [
135 ("t5", None),
136 ("t5 __init__.py", "import t5.foo"),
137 ("t5 string.py", "print __name__, 'loading'; spam = 1"),
138 ("t5 foo.py",
139 "print __name__, 'loading'; import string; print string.spam"),
140 ],
141"""
Guido van Rossum10887a31997-09-07 06:12:11 +0000142import t5
Guido van Rossum6c612421997-09-06 18:42:57 +0000143from t5 import *
144print dir()
145import t5
146print dir(t5)
147print dir(t5.foo)
148print dir(t5.string)
149"""),
150
Guido van Rossumc8bf8841997-09-08 16:06:20 +0000151 ("t6", [
152 ("t6", None),
153 ("t6 __init__.py", "__all__ = ['spam', 'ham', 'eggs']"),
154 ("t6 spam.py", "print __name__, 'loading'"),
155 ("t6 ham.py", "print __name__, 'loading'"),
156 ("t6 eggs.py", "print __name__, 'loading'"),
157 ],
158"""
159import t6
160print dir(t6)
161from t6 import *
162print dir(t6)
163print dir()
164"""),
165
Guido van Rossum6c612421997-09-06 18:42:57 +0000166]
167
168nontests = [
169 ("x5", [], ("import a" + ".a"*400)),
170 ("x6", [], ("import a" + ".a"*499)),
171 ("x7", [], ("import a" + ".a"*500)),
172 ("x8", [], ("import a" + ".a"*1100)),
173 ("x9", [], ("import " + "a"*400)),
174 ("x10", [], ("import " + "a"*500)),
175 ("x11", [], ("import " + "a"*998)),
176 ("x12", [], ("import " + "a"*999)),
177 ("x13", [], ("import " + "a"*999)),
178 ("x14", [], ("import " + "a"*2000)),
179]
180
181"""XXX Things to test
182
183import package without __init__
184import package with __init__
185__init__ importing submodule
186__init__ importing global module
187__init__ defining variables
188submodule importing other submodule
189submodule importing global module
190submodule import submodule via global name
191from package import submodule
192from package import subpackage
193from package import variable (defined in __init__)
194from package import * (defined in __init__)
195"""
196
197# Run the tests
198
Guido van Rossum10887a31997-09-07 06:12:11 +0000199args = []
200if __name__ == '__main__':
201 args = sys.argv[1:]
202 if args and args[0] == '-q':
203 verbose = 0
204 del args[0]
205
Guido van Rossum6c612421997-09-06 18:42:57 +0000206for name, hier, code in tests:
Guido van Rossum10887a31997-09-07 06:12:11 +0000207 if args and name not in args:
208 print "skipping test", name
209 continue
Guido van Rossum6c612421997-09-06 18:42:57 +0000210 print "running test", name
211 runtest(hier, code)