test_pkg.py 6.74 KB
Newer Older
1 2
# Test packages (dotted-name import)

3
import sys, os, tempfile, traceback
4
from os import mkdir, rmdir, extsep          # Can't test if these fail
5
del mkdir, rmdir
6
from test_support import verify, verbose, TestFailed
7 8 9 10 11 12

# Helpers to create and destroy hierarchies.

def mkhier(root, descr):
    mkdir(root)
    for name, contents in descr:
13
        comps = name.split()
14 15 16 17 18 19 20 21 22 23 24 25
        fullname = root
        for c in comps:
            fullname = os.path.join(fullname, c)
        if contents is None:
            mkdir(fullname)
        else:
            if verbose: print "write", fullname
            f = open(fullname, "w")
            f.write(contents)
            if contents and contents[-1] != '\n':
                f.write('\n')
            f.close()
26 27 28 29 30 31 32 33

def mkdir(x):
    if verbose: print "mkdir", x
    os.mkdir(x)

def cleanout(root):
    names = os.listdir(root)
    for name in names:
34 35 36 37 38
        fullname = os.path.join(root, name)
        if os.path.isdir(fullname) and not os.path.islink(fullname):
            cleanout(fullname)
        else:
            os.remove(fullname)
39 40 41 42 43 44
    rmdir(root)

def rmdir(x):
    if verbose: print "rmdir", x
    os.rmdir(x)

45 46 47 48 49 50 51
def fixdir(lst):
    try:
        lst.remove('__builtins__')
    except ValueError:
        pass
    return lst

52 53 54 55 56 57 58 59 60 61 62
# Helper to run a test

def runtest(hier, code):
    root = tempfile.mktemp()
    mkhier(root, hier)
    savepath = sys.path[:]
    codefile = tempfile.mktemp()
    f = open(codefile, "w")
    f.write(code)
    f.close()
    try:
63 64 65 66 67 68
        sys.path.insert(0, root)
        if verbose: print "sys.path =", sys.path
        try:
            execfile(codefile, globals(), {})
        except:
            traceback.print_exc(file=sys.stdout)
69
    finally:
70 71 72 73 74 75
        sys.path[:] = savepath
        try:
            cleanout(root)
        except (os.error, IOError):
            pass
        os.remove(codefile)
76 77 78 79

# Test descriptions

tests = [
80
    ("t1", [("t1", None), ("t1 __init__"+os.extsep+"py", "")], "import t1"),
81

82 83
    ("t2", [
    ("t2", None),
84
    ("t2 __init__"+os.extsep+"py", "'doc for t2'; print __name__, 'loading'"),
85
    ("t2 sub", None),
86
    ("t2 sub __init__"+os.extsep+"py", ""),
87
    ("t2 sub subsub", None),
88
    ("t2 sub subsub __init__"+os.extsep+"py", "print __name__, 'loading'; spam = 1"),
89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110
    ],
"""
import t2
print t2.__doc__
import t2.sub
import t2.sub.subsub
print t2.__name__, t2.sub.__name__, t2.sub.subsub.__name__
import t2
from t2 import *
print dir()
from t2 import sub
from t2.sub import subsub
from t2.sub.subsub import spam
print sub.__name__, subsub.__name__
print sub.subsub.__name__
print dir()
import t2.sub
import t2.sub.subsub
print t2.__name__, t2.sub.__name__, t2.sub.subsub.__name__
from t2 import *
print dir()
"""),
111

112 113
    ("t3", [
    ("t3", None),
114
    ("t3 __init__"+os.extsep+"py", "print __name__, 'loading'"),
115
    ("t3 sub", None),
116
    ("t3 sub __init__"+os.extsep+"py", ""),
117
    ("t3 sub subsub", None),
118
    ("t3 sub subsub __init__"+os.extsep+"py", "print __name__, 'loading'; spam = 1"),
119 120 121 122
    ],
"""
import t3.sub.subsub
print t3.__name__, t3.sub.__name__, t3.sub.subsub.__name__
123 124 125
reload(t3)
reload(t3.sub)
reload(t3.sub.subsub)
126
"""),
127

128
    ("t4", [
129
    ("t4"+os.extsep+"py", "print 'THIS SHOULD NOT BE PRINTED (t4"+os.extsep+"py)'"),
130
    ("t4", None),
131 132
    ("t4 __init__"+os.extsep+"py", "print __name__, 'loading'"),
    ("t4 sub"+os.extsep+"py", "print 'THIS SHOULD NOT BE PRINTED (sub"+os.extsep+"py)'"),
133
    ("t4 sub", None),
134 135
    ("t4 sub __init__"+os.extsep+"py", ""),
    ("t4 sub subsub"+os.extsep+"py", "print 'THIS SHOULD NOT BE PRINTED (subsub"+os.extsep+"py)'"),
136
    ("t4 sub subsub", None),
137
    ("t4 sub subsub __init__"+os.extsep+"py", "print __name__, 'loading'; spam = 1"),
138 139 140 141 142 143 144 145
    ],
"""
from t4.sub.subsub import *
print "t4.sub.subsub.spam =", spam
"""),

    ("t5", [
    ("t5", None),
146 147 148
    ("t5 __init__"+os.extsep+"py", "import t5.foo"),
    ("t5 string"+os.extsep+"py", "print __name__, 'loading'; spam = 1"),
    ("t5 foo"+os.extsep+"py",
149 150 151
     "print __name__, 'loading'; import string; print string.spam"),
     ],
"""
152
import t5
153 154 155
from t5 import *
print dir()
import t5
156 157 158
print fixdir(dir(t5))
print fixdir(dir(t5.foo))
print fixdir(dir(t5.string))
159 160 161 162
"""),

    ("t6", [
    ("t6", None),
163 164 165 166
    ("t6 __init__"+os.extsep+"py", "__all__ = ['spam', 'ham', 'eggs']"),
    ("t6 spam"+os.extsep+"py", "print __name__, 'loading'"),
    ("t6 ham"+os.extsep+"py", "print __name__, 'loading'"),
    ("t6 eggs"+os.extsep+"py", "print __name__, 'loading'"),
167 168 169
    ],
"""
import t6
170
print fixdir(dir(t6))
171
from t6 import *
172
print fixdir(dir(t6))
173
print dir()
174
"""),
175

176
    ("t7", [
177
    ("t7"+os.extsep+"py", "print 'Importing t7"+os.extsep+"py'"),
178
    ("t7", None),
179 180
    ("t7 __init__"+os.extsep+"py", "print __name__, 'loading'"),
    ("t7 sub"+os.extsep+"py", "print 'THIS SHOULD NOT BE PRINTED (sub"+os.extsep+"py)'"),
181
    ("t7 sub", None),
182 183
    ("t7 sub __init__"+os.extsep+"py", ""),
    ("t7 sub subsub"+os.extsep+"py", "print 'THIS SHOULD NOT BE PRINTED (subsub"+os.extsep+"py)'"),
184
    ("t7 sub subsub", None),
185
    ("t7 sub subsub __init__"+os.extsep+"py", "print __name__, 'loading'; spam = 1"),
186 187 188 189
    ],
"""
t7, sub, subsub = None, None, None
import t7 as tas
190
print fixdir(dir(tas))
191
verify(not t7)
192
from t7 import sub as subpar
193
print fixdir(dir(subpar))
194
verify(not t7 and not sub)
195
from t7.sub import subsub as subsubsub
196
print fixdir(dir(subsubsub))
197
verify(not t7 and not sub and not subsub)
198 199
from t7.sub.subsub import spam as ham
print "t7.sub.subsub.spam =", ham
200
verify(not t7 and not sub and not subsub)
201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235
"""),

]

nontests = [
    ("x5", [], ("import a" + ".a"*400)),
    ("x6", [], ("import a" + ".a"*499)),
    ("x7", [], ("import a" + ".a"*500)),
    ("x8", [], ("import a" + ".a"*1100)),
    ("x9", [], ("import " + "a"*400)),
    ("x10", [], ("import " + "a"*500)),
    ("x11", [], ("import " + "a"*998)),
    ("x12", [], ("import " + "a"*999)),
    ("x13", [], ("import " + "a"*999)),
    ("x14", [], ("import " + "a"*2000)),
]

"""XXX Things to test

import package without __init__
import package with __init__
__init__ importing submodule
__init__ importing global module
__init__ defining variables
submodule importing other submodule
submodule importing global module
submodule import submodule via global name
from package import submodule
from package import subpackage
from package import variable (defined in __init__)
from package import * (defined in __init__)
"""

# Run the tests

236 237 238 239
args = []
if __name__ == '__main__':
    args = sys.argv[1:]
    if args and args[0] == '-q':
240 241
        verbose = 0
        del args[0]
242

243
for name, hier, code in tests:
244
    if args and name not in args:
245 246
        print "skipping test", name
        continue
247 248
    print "running test", name
    runtest(hier, code)
249 250 251 252 253 254 255 256 257 258 259

# Test
import sys
import imp
try:
    import sys.imp
except ImportError:
    # This is what we expect
    pass
else:
    raise TestFailed, "No ImportError exception on 'import sys.imp'"