Added 'description' class attribute to every command class (to help the
[python/dscho.git] / Lib / test / test_pkg.py
blobfd226126449acd660fc91549b5c651b0ab29b9d2
1 # Test packages (dotted-name import)
3 import sys, os, string, tempfile, traceback
4 from os import mkdir, rmdir # Can't test if these fail
5 del mkdir, rmdir
6 from test_support import verbose, TestFailed
8 # Helpers to create and destroy hierarchies.
10 def 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()
27 def mkdir(x):
28 if verbose: print "mkdir", x
29 os.mkdir(x)
31 def 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)
41 def rmdir(x):
42 if verbose: print "rmdir", x
43 os.rmdir(x)
45 # Helper to run a test
47 def 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:
61 traceback.print_exc(file=sys.stdout)
62 finally:
63 sys.path[:] = savepath
64 try:
65 cleanout(root)
66 except (os.error, IOError):
67 pass
68 os.remove(codefile)
70 # Test descriptions
72 tests = [
73 ("t1", [("t1", None), ("t1 __init__.py", "")], "import t1"),
75 ("t2", [
76 ("t2", None),
77 ("t2 __init__.py", "'doc for t2'; print __name__, 'loading'"),
78 ("t2 sub", None),
79 ("t2 sub __init__.py", ""),
80 ("t2 sub subsub", None),
81 ("t2 sub subsub __init__.py", "print __name__, 'loading'; spam = 1"),
83 """
84 import t2
85 print t2.__doc__
86 import t2.sub
87 import t2.sub.subsub
88 print t2.__name__, t2.sub.__name__, t2.sub.subsub.__name__
89 import t2
90 from t2 import *
91 print dir()
92 from t2 import sub
93 from t2.sub import subsub
94 from t2.sub.subsub import spam
95 print sub.__name__, subsub.__name__
96 print sub.subsub.__name__
97 print dir()
98 import t2.sub
99 import t2.sub.subsub
100 print t2.__name__, t2.sub.__name__, t2.sub.subsub.__name__
101 from t2 import *
102 print dir()
103 """),
105 ("t3", [
106 ("t3", None),
107 ("t3 __init__.py", "print __name__, 'loading'"),
108 ("t3 sub", None),
109 ("t3 sub __init__.py", ""),
110 ("t3 sub subsub", None),
111 ("t3 sub subsub __init__.py", "print __name__, 'loading'; spam = 1"),
114 import t3.sub.subsub
115 print t3.__name__, t3.sub.__name__, t3.sub.subsub.__name__
116 reload(t3)
117 reload(t3.sub)
118 reload(t3.sub.subsub)
119 """),
121 ("t4", [
122 ("t4.py", "print 'THIS SHOULD NOT BE PRINTED (t4.py)'"),
123 ("t4", None),
124 ("t4 __init__.py", "print __name__, 'loading'"),
125 ("t4 sub.py", "print 'THIS SHOULD NOT BE PRINTED (sub.py)'"),
126 ("t4 sub", None),
127 ("t4 sub __init__.py", ""),
128 ("t4 sub subsub.py", "print 'THIS SHOULD NOT BE PRINTED (subsub.py)'"),
129 ("t4 sub subsub", None),
130 ("t4 sub subsub __init__.py", "print __name__, 'loading'; spam = 1"),
133 from t4.sub.subsub import *
134 print "t4.sub.subsub.spam =", spam
135 """),
137 ("t5", [
138 ("t5", None),
139 ("t5 __init__.py", "import t5.foo"),
140 ("t5 string.py", "print __name__, 'loading'; spam = 1"),
141 ("t5 foo.py",
142 "print __name__, 'loading'; import string; print string.spam"),
145 import t5
146 from t5 import *
147 print dir()
148 import t5
149 print dir(t5)
150 print dir(t5.foo)
151 print dir(t5.string)
152 """),
154 ("t6", [
155 ("t6", None),
156 ("t6 __init__.py", "__all__ = ['spam', 'ham', 'eggs']"),
157 ("t6 spam.py", "print __name__, 'loading'"),
158 ("t6 ham.py", "print __name__, 'loading'"),
159 ("t6 eggs.py", "print __name__, 'loading'"),
162 import t6
163 print dir(t6)
164 from t6 import *
165 print dir(t6)
166 print dir()
167 """),
171 nontests = [
172 ("x5", [], ("import a" + ".a"*400)),
173 ("x6", [], ("import a" + ".a"*499)),
174 ("x7", [], ("import a" + ".a"*500)),
175 ("x8", [], ("import a" + ".a"*1100)),
176 ("x9", [], ("import " + "a"*400)),
177 ("x10", [], ("import " + "a"*500)),
178 ("x11", [], ("import " + "a"*998)),
179 ("x12", [], ("import " + "a"*999)),
180 ("x13", [], ("import " + "a"*999)),
181 ("x14", [], ("import " + "a"*2000)),
184 """XXX Things to test
186 import package without __init__
187 import package with __init__
188 __init__ importing submodule
189 __init__ importing global module
190 __init__ defining variables
191 submodule importing other submodule
192 submodule importing global module
193 submodule import submodule via global name
194 from package import submodule
195 from package import subpackage
196 from package import variable (defined in __init__)
197 from package import * (defined in __init__)
200 # Run the tests
202 args = []
203 if __name__ == '__main__':
204 args = sys.argv[1:]
205 if args and args[0] == '-q':
206 verbose = 0
207 del args[0]
209 for name, hier, code in tests:
210 if args and name not in args:
211 print "skipping test", name
212 continue
213 print "running test", name
214 runtest(hier, code)
216 # Test
217 import sys
218 import imp
219 try:
220 import sys.imp
221 except ImportError:
222 # This is what we expect
223 pass
224 else:
225 raise TestFailed, "No ImportError exception on 'import sys.imp'"