|
| 1 | +# Test packages (dotted-name import) |
| 2 | + |
| 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 |
| 7 | +if sys.argv[1:2] == ['-q']: verbose = 0 |
| 8 | + |
| 9 | +# Helpers to create and destroy hierarchies. |
| 10 | + |
| 11 | +def mkhier(root, descr): |
| 12 | + mkdir(root) |
| 13 | + for name, contents in descr: |
| 14 | + comps = string.split(name) |
| 15 | + fullname = root |
| 16 | + for c in comps: |
| 17 | + fullname = os.path.join(fullname, c) |
| 18 | + if contents is None: |
| 19 | + mkdir(fullname) |
| 20 | + else: |
| 21 | + if verbose: print "write", fullname |
| 22 | + f = open(fullname, "w") |
| 23 | + f.write(contents) |
| 24 | + if contents and contents[-1] != '\n': |
| 25 | + f.write('\n') |
| 26 | + f.close() |
| 27 | + |
| 28 | +def mkdir(x): |
| 29 | + if verbose: print "mkdir", x |
| 30 | + os.mkdir(x) |
| 31 | + |
| 32 | +def cleanout(root): |
| 33 | + names = os.listdir(root) |
| 34 | + for name in names: |
| 35 | + fullname = os.path.join(root, name) |
| 36 | + if os.path.isdir(fullname) and not os.path.islink(fullname): |
| 37 | + cleanout(fullname) |
| 38 | + else: |
| 39 | + os.remove(fullname) |
| 40 | + rmdir(root) |
| 41 | + |
| 42 | +def rmdir(x): |
| 43 | + if verbose: print "rmdir", x |
| 44 | + os.rmdir(x) |
| 45 | + |
| 46 | +# Helper to run a test |
| 47 | + |
| 48 | +def runtest(hier, code): |
| 49 | + root = tempfile.mktemp() |
| 50 | + mkhier(root, hier) |
| 51 | + savepath = sys.path[:] |
| 52 | + codefile = tempfile.mktemp() |
| 53 | + f = open(codefile, "w") |
| 54 | + f.write(code) |
| 55 | + f.close() |
| 56 | + try: |
| 57 | + sys.path.insert(0, root) |
| 58 | + if verbose: print "sys.path =", sys.path |
| 59 | + try: |
| 60 | + execfile(codefile, globals(), {}) |
| 61 | + except: |
| 62 | + traceback.print_exc() |
| 63 | + finally: |
| 64 | + sys.path[:] = savepath |
| 65 | + try: |
| 66 | + cleanout(root) |
| 67 | + except (os.error, IOError): |
| 68 | + pass |
| 69 | + os.remove(codefile) |
| 70 | + |
| 71 | +# Test descriptions |
| 72 | + |
| 73 | +tests = [ |
| 74 | + ("t1", [("t1", None)], "import ni"), |
| 75 | + |
| 76 | + ("t2", [ |
| 77 | + ("t2", None), |
| 78 | + ("t2 __init__.py", "'doc for t2'; print __name__, 'loading'"), |
| 79 | + ("t2 sub", None), |
| 80 | + ("t2 sub subsub", None), |
| 81 | + ("t2 sub subsub __init__.py", "print __name__, 'loading'; spam = 1"), |
| 82 | + ], |
| 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 | +"""), |
| 104 | + |
| 105 | + ("t3", [ |
| 106 | + ("t3", None), |
| 107 | + ("t3 __init__.py", "print __name__, 'loading'"), |
| 108 | + ("t3 sub", None), |
| 109 | + ("t3 sub subsub", None), |
| 110 | + ("t3 sub subsub __init__.py", "print __name__, 'loading'; spam = 1"), |
| 111 | + ], |
| 112 | +""" |
| 113 | +import t3.sub.subsub |
| 114 | +print t3.__name__, t3.sub.__name__, t3.sub.subsub.__name__ |
| 115 | +"""), |
| 116 | + |
| 117 | + ("t4", [ |
| 118 | + ("t4.py", "print 'THIS SHOULD NOT BE PRINTED (t4.py)'"), |
| 119 | + ("t4", None), |
| 120 | + ("t4 __init__.py", "print __name__, 'loading'"), |
| 121 | + ("t4 sub.py", "print 'THIS SHOULD NOT BE PRINTED (sub.py)'"), |
| 122 | + ("t4 sub", None), |
| 123 | + ("t4 sub subsub.py", "print 'THIS SHOULD NOT BE PRINTED (subsub.py)'"), |
| 124 | + ("t4 sub subsub", None), |
| 125 | + ("t4 sub subsub __init__.py", "print __name__, 'loading'; spam = 1"), |
| 126 | + ], |
| 127 | +""" |
| 128 | +from t4.sub.subsub import * |
| 129 | +print "t4.sub.subsub.spam =", spam |
| 130 | +"""), |
| 131 | + |
| 132 | + ("t5", [ |
| 133 | + ("t5", None), |
| 134 | + ("t5 __init__.py", "import t5.foo"), |
| 135 | + ("t5 string.py", "print __name__, 'loading'; spam = 1"), |
| 136 | + ("t5 foo.py", |
| 137 | + "print __name__, 'loading'; import string; print string.spam"), |
| 138 | + ], |
| 139 | +""" |
| 140 | +from t5 import * |
| 141 | +print dir() |
| 142 | +import t5 |
| 143 | +print dir(t5) |
| 144 | +print dir(t5.foo) |
| 145 | +print dir(t5.string) |
| 146 | +"""), |
| 147 | + |
| 148 | +] |
| 149 | + |
| 150 | +nontests = [ |
| 151 | + ("x5", [], ("import a" + ".a"*400)), |
| 152 | + ("x6", [], ("import a" + ".a"*499)), |
| 153 | + ("x7", [], ("import a" + ".a"*500)), |
| 154 | + ("x8", [], ("import a" + ".a"*1100)), |
| 155 | + ("x9", [], ("import " + "a"*400)), |
| 156 | + ("x10", [], ("import " + "a"*500)), |
| 157 | + ("x11", [], ("import " + "a"*998)), |
| 158 | + ("x12", [], ("import " + "a"*999)), |
| 159 | + ("x13", [], ("import " + "a"*999)), |
| 160 | + ("x14", [], ("import " + "a"*2000)), |
| 161 | +] |
| 162 | + |
| 163 | +"""XXX Things to test |
| 164 | +
|
| 165 | +import package without __init__ |
| 166 | +import package with __init__ |
| 167 | +__init__ importing submodule |
| 168 | +__init__ importing global module |
| 169 | +__init__ defining variables |
| 170 | +submodule importing other submodule |
| 171 | +submodule importing global module |
| 172 | +submodule import submodule via global name |
| 173 | +from package import submodule |
| 174 | +from package import subpackage |
| 175 | +from package import variable (defined in __init__) |
| 176 | +from package import * (defined in __init__) |
| 177 | +""" |
| 178 | + |
| 179 | +# Run the tests |
| 180 | + |
| 181 | +for name, hier, code in tests: |
| 182 | + print "running test", name |
| 183 | + runtest(hier, code) |
0 commit comments