/Lib/test/test_pkg.py
Python | 287 lines | 254 code | 15 blank | 18 comment | 24 complexity | 8ac1ad3f2028aaae770e2265cd919f2b MD5 | raw file
1# Test packages (dotted-name import) 2 3import sys 4import os 5import tempfile 6import textwrap 7import unittest 8from test import test_support 9 10 11# Helpers to create and destroy hierarchies. 12 13def cleanout(root): 14 names = os.listdir(root) 15 for name in names: 16 fullname = os.path.join(root, name) 17 if os.path.isdir(fullname) and not os.path.islink(fullname): 18 cleanout(fullname) 19 else: 20 os.remove(fullname) 21 os.rmdir(root) 22 23def fixdir(lst): 24 if "__builtins__" in lst: 25 lst.remove("__builtins__") 26 return lst 27 28 29# XXX Things to test 30# 31# import package without __init__ 32# import package with __init__ 33# __init__ importing submodule 34# __init__ importing global module 35# __init__ defining variables 36# submodule importing other submodule 37# submodule importing global module 38# submodule import submodule via global name 39# from package import submodule 40# from package import subpackage 41# from package import variable (defined in __init__) 42# from package import * (defined in __init__) 43 44 45class Test(unittest.TestCase): 46 47 def setUp(self): 48 self.root = None 49 self.pkgname = None 50 self.syspath = list(sys.path) 51 52 def tearDown(self): 53 sys.path[:] = self.syspath 54 cleanout(self.root) 55 56 # delete all modules concerning the tested hiearchy 57 if self.pkgname: 58 modules = [name for name in sys.modules 59 if self.pkgname in name.split('.')] 60 for name in modules: 61 del sys.modules[name] 62 63 def run_code(self, code): 64 exec(textwrap.dedent(code), globals(), {"self": self}) 65 66 def mkhier(self, descr): 67 root = tempfile.mkdtemp() 68 sys.path.insert(0, root) 69 if not os.path.isdir(root): 70 os.mkdir(root) 71 for name, contents in descr: 72 comps = name.split() 73 fullname = root 74 for c in comps: 75 fullname = os.path.join(fullname, c) 76 if contents is None: 77 os.mkdir(fullname) 78 else: 79 f = open(fullname, "w") 80 f.write(contents) 81 if contents and contents[-1] != '\n': 82 f.write('\n') 83 f.close() 84 self.root = root 85 # package name is the name of the first item 86 self.pkgname = descr[0][0] 87 88 def test_1(self): 89 hier = [("t1", None), ("t1 __init__"+os.extsep+"py", "")] 90 self.mkhier(hier) 91 import t1 92 93 def test_2(self): 94 hier = [ 95 ("t2", None), 96 ("t2 __init__"+os.extsep+"py", "'doc for t2'"), 97 ("t2 sub", None), 98 ("t2 sub __init__"+os.extsep+"py", ""), 99 ("t2 sub subsub", None), 100 ("t2 sub subsub __init__"+os.extsep+"py", "spam = 1"), 101 ] 102 self.mkhier(hier) 103 104 import t2 105 if sys.flags.optimize <= 1: 106 self.assertEqual(t2.__doc__, "doc for t2") 107 108 import t2.sub 109 import t2.sub.subsub 110 self.assertEqual(t2.__name__, "t2") 111 self.assertEqual(t2.sub.__name__, "t2.sub") 112 self.assertEqual(t2.sub.subsub.__name__, "t2.sub.subsub") 113 114 # This exec crap is needed because Py3k forbids 'import *' outside 115 # of module-scope and __import__() is insufficient for what we need. 116 s = """ 117 import t2 118 from t2 import * 119 self.assertEqual(dir(), ['self', 'sub', 't2']) 120 """ 121 self.run_code(s) 122 123 from t2 import sub 124 from t2.sub import subsub 125 from t2.sub.subsub import spam 126 self.assertEqual(sub.__name__, "t2.sub") 127 self.assertEqual(subsub.__name__, "t2.sub.subsub") 128 self.assertEqual(sub.subsub.__name__, "t2.sub.subsub") 129 for name in ['spam', 'sub', 'subsub', 't2']: 130 self.failUnless(locals()["name"], "Failed to import %s" % name) 131 132 import t2.sub 133 import t2.sub.subsub 134 self.assertEqual(t2.__name__, "t2") 135 self.assertEqual(t2.sub.__name__, "t2.sub") 136 self.assertEqual(t2.sub.subsub.__name__, "t2.sub.subsub") 137 138 s = """ 139 from t2 import * 140 self.failUnless(dir(), ['self', 'sub']) 141 """ 142 self.run_code(s) 143 144 def test_3(self): 145 hier = [ 146 ("t3", None), 147 ("t3 __init__"+os.extsep+"py", ""), 148 ("t3 sub", None), 149 ("t3 sub __init__"+os.extsep+"py", ""), 150 ("t3 sub subsub", None), 151 ("t3 sub subsub __init__"+os.extsep+"py", "spam = 1"), 152 ] 153 self.mkhier(hier) 154 155 import t3.sub.subsub 156 self.assertEqual(t3.__name__, "t3") 157 self.assertEqual(t3.sub.__name__, "t3.sub") 158 self.assertEqual(t3.sub.subsub.__name__, "t3.sub.subsub") 159 160 def test_4(self): 161 hier = [ 162 ("t4.py", "raise RuntimeError('Shouldnt load t4.py')"), 163 ("t4", None), 164 ("t4 __init__"+os.extsep+"py", ""), 165 ("t4 sub.py", "raise RuntimeError('Shouldnt load sub.py')"), 166 ("t4 sub", None), 167 ("t4 sub __init__"+os.extsep+"py", ""), 168 ("t4 sub subsub"+os.extsep+"py", 169 "raise RuntimeError('Shouldnt load subsub.py')"), 170 ("t4 sub subsub", None), 171 ("t4 sub subsub __init__"+os.extsep+"py", "spam = 1"), 172 ] 173 self.mkhier(hier) 174 175 s = """ 176 from t4.sub.subsub import * 177 self.assertEqual(spam, 1) 178 """ 179 self.run_code(s) 180 181 def test_5(self): 182 hier = [ 183 ("t5", None), 184 ("t5 __init__"+os.extsep+"py", "import t5.foo"), 185 ("t5 string"+os.extsep+"py", "spam = 1"), 186 ("t5 foo"+os.extsep+"py", 187 "from . import string; assert string.spam == 1"), 188 ] 189 self.mkhier(hier) 190 191 import t5 192 s = """ 193 from t5 import * 194 self.assertEqual(dir(), ['foo', 'self', 'string', 't5']) 195 """ 196 self.run_code(s) 197 198 import t5 199 self.assertEqual(fixdir(dir(t5)), 200 ['__doc__', '__file__', '__name__', 201 '__package__', '__path__', 'foo', 'string', 't5']) 202 self.assertEqual(fixdir(dir(t5.foo)), 203 ['__doc__', '__file__', '__name__', '__package__', 204 'string']) 205 self.assertEqual(fixdir(dir(t5.string)), 206 ['__doc__', '__file__', '__name__','__package__', 207 'spam']) 208 209 def test_6(self): 210 hier = [ 211 ("t6", None), 212 ("t6 __init__"+os.extsep+"py", 213 "__all__ = ['spam', 'ham', 'eggs']"), 214 ("t6 spam"+os.extsep+"py", ""), 215 ("t6 ham"+os.extsep+"py", ""), 216 ("t6 eggs"+os.extsep+"py", ""), 217 ] 218 self.mkhier(hier) 219 220 import t6 221 self.assertEqual(fixdir(dir(t6)), 222 ['__all__', '__doc__', '__file__', 223 '__name__', '__package__', '__path__']) 224 s = """ 225 import t6 226 from t6 import * 227 self.assertEqual(fixdir(dir(t6)), 228 ['__all__', '__doc__', '__file__', 229 '__name__', '__package__', '__path__', 230 'eggs', 'ham', 'spam']) 231 self.assertEqual(dir(), ['eggs', 'ham', 'self', 'spam', 't6']) 232 """ 233 self.run_code(s) 234 235 def test_7(self): 236 hier = [ 237 ("t7", None), 238 ("t7"+os.extsep+"py", ""), 239 ("t7 __init__"+os.extsep+"py", ""), 240 ("t7 sub"+os.extsep+"py", 241 "raise RuntimeError('Shouldnt load sub.py')"), 242 ("t7 sub", None), 243 ("t7 sub __init__"+os.extsep+"py", ""), 244 ("t7 sub "+os.extsep+"py", 245 "raise RuntimeError('Shouldnt load subsub.py')"), 246 ("t7 sub subsub", None), 247 ("t7 sub subsub __init__"+os.extsep+"py", 248 "spam = 1"), 249 ] 250 self.mkhier(hier) 251 252 253 t7, sub, subsub = None, None, None 254 import t7 as tas 255 self.assertEqual(fixdir(dir(tas)), 256 ['__doc__', '__file__', '__name__', 257 '__package__', '__path__']) 258 self.failIf(t7) 259 from t7 import sub as subpar 260 self.assertEqual(fixdir(dir(subpar)), 261 ['__doc__', '__file__', '__name__', 262 '__package__', '__path__']) 263 self.failIf(t7) 264 self.failIf(sub) 265 from t7.sub import subsub as subsubsub 266 self.assertEqual(fixdir(dir(subsubsub)), 267 ['__doc__', '__file__', '__name__', 268 '__package__', '__path__', 'spam']) 269 self.failIf(t7) 270 self.failIf(sub) 271 self.failIf(subsub) 272 from t7.sub.subsub import spam as ham 273 self.assertEqual(ham, 1) 274 self.failIf(t7) 275 self.failIf(sub) 276 self.failIf(subsub) 277 278 279def test_main(): 280 if sys.flags.optimize >= 2: 281 print >>sys.stderr, "test_pkg -- skipping some tests due to -O flag." 282 sys.stderr.flush() 283 test_support.run_unittest(__name__) 284 285 286if __name__ == "__main__": 287 test_main()