Guido van Rossum | 6c61242 | 1997-09-06 18:42:57 +0000 | [diff] [blame] | 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__ |
Guido van Rossum | 81da02e | 1997-09-06 19:58:53 +0000 | [diff] [blame^] | 115 | reload(t3) |
| 116 | reload(t3.sub) |
| 117 | reload(t3.sub.subsub) |
Guido van Rossum | 6c61242 | 1997-09-06 18:42:57 +0000 | [diff] [blame] | 118 | """), |
| 119 | |
| 120 | ("t4", [ |
| 121 | ("t4.py", "print 'THIS SHOULD NOT BE PRINTED (t4.py)'"), |
| 122 | ("t4", None), |
| 123 | ("t4 __init__.py", "print __name__, 'loading'"), |
| 124 | ("t4 sub.py", "print 'THIS SHOULD NOT BE PRINTED (sub.py)'"), |
| 125 | ("t4 sub", None), |
| 126 | ("t4 sub subsub.py", "print 'THIS SHOULD NOT BE PRINTED (subsub.py)'"), |
| 127 | ("t4 sub subsub", None), |
| 128 | ("t4 sub subsub __init__.py", "print __name__, 'loading'; spam = 1"), |
| 129 | ], |
| 130 | """ |
| 131 | from t4.sub.subsub import * |
| 132 | print "t4.sub.subsub.spam =", spam |
| 133 | """), |
| 134 | |
| 135 | ("t5", [ |
| 136 | ("t5", None), |
| 137 | ("t5 __init__.py", "import t5.foo"), |
| 138 | ("t5 string.py", "print __name__, 'loading'; spam = 1"), |
| 139 | ("t5 foo.py", |
| 140 | "print __name__, 'loading'; import string; print string.spam"), |
| 141 | ], |
| 142 | """ |
| 143 | from t5 import * |
| 144 | print dir() |
| 145 | import t5 |
| 146 | print dir(t5) |
| 147 | print dir(t5.foo) |
| 148 | print dir(t5.string) |
| 149 | """), |
| 150 | |
| 151 | ] |
| 152 | |
| 153 | nontests = [ |
| 154 | ("x5", [], ("import a" + ".a"*400)), |
| 155 | ("x6", [], ("import a" + ".a"*499)), |
| 156 | ("x7", [], ("import a" + ".a"*500)), |
| 157 | ("x8", [], ("import a" + ".a"*1100)), |
| 158 | ("x9", [], ("import " + "a"*400)), |
| 159 | ("x10", [], ("import " + "a"*500)), |
| 160 | ("x11", [], ("import " + "a"*998)), |
| 161 | ("x12", [], ("import " + "a"*999)), |
| 162 | ("x13", [], ("import " + "a"*999)), |
| 163 | ("x14", [], ("import " + "a"*2000)), |
| 164 | ] |
| 165 | |
| 166 | """XXX Things to test |
| 167 | |
| 168 | import package without __init__ |
| 169 | import package with __init__ |
| 170 | __init__ importing submodule |
| 171 | __init__ importing global module |
| 172 | __init__ defining variables |
| 173 | submodule importing other submodule |
| 174 | submodule importing global module |
| 175 | submodule import submodule via global name |
| 176 | from package import submodule |
| 177 | from package import subpackage |
| 178 | from package import variable (defined in __init__) |
| 179 | from package import * (defined in __init__) |
| 180 | """ |
| 181 | |
| 182 | # Run the tests |
| 183 | |
| 184 | for name, hier, code in tests: |
| 185 | print "running test", name |
| 186 | runtest(hier, code) |