Guido van Rossum | 6c61242 | 1997-09-06 18:42:57 +0000 | [diff] [blame] | 1 | # Test packages (dotted-name import) |
| 2 | |
Collin Winter | 11e065b | 2007-08-24 19:15:12 +0000 | [diff] [blame] | 3 | import sys |
| 4 | import os |
| 5 | import tempfile |
| 6 | import textwrap |
Collin Winter | 11e065b | 2007-08-24 19:15:12 +0000 | [diff] [blame] | 7 | import unittest |
Benjamin Peterson | ee8712c | 2008-05-20 21:35:26 +0000 | [diff] [blame] | 8 | from test import support |
Collin Winter | 11e065b | 2007-08-24 19:15:12 +0000 | [diff] [blame] | 9 | |
Guido van Rossum | 6c61242 | 1997-09-06 18:42:57 +0000 | [diff] [blame] | 10 | |
| 11 | # Helpers to create and destroy hierarchies. |
| 12 | |
Guido van Rossum | 6c61242 | 1997-09-06 18:42:57 +0000 | [diff] [blame] | 13 | def cleanout(root): |
| 14 | names = os.listdir(root) |
| 15 | for name in names: |
Guido van Rossum | 41360a4 | 1998-03-26 19:42:58 +0000 | [diff] [blame] | 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) |
Collin Winter | 11e065b | 2007-08-24 19:15:12 +0000 | [diff] [blame] | 21 | os.rmdir(root) |
Guido van Rossum | 6c61242 | 1997-09-06 18:42:57 +0000 | [diff] [blame] | 22 | |
Barry Warsaw | 3a9d061 | 2000-09-01 06:53:52 +0000 | [diff] [blame] | 23 | def fixdir(lst): |
Collin Winter | 11e065b | 2007-08-24 19:15:12 +0000 | [diff] [blame] | 24 | if "__builtins__" in lst: |
| 25 | lst.remove("__builtins__") |
Barry Warsaw | 3a9d061 | 2000-09-01 06:53:52 +0000 | [diff] [blame] | 26 | return lst |
| 27 | |
Guido van Rossum | 6c61242 | 1997-09-06 18:42:57 +0000 | [diff] [blame] | 28 | |
Collin Winter | 11e065b | 2007-08-24 19:15:12 +0000 | [diff] [blame] | 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 | |
Christian Heimes | db4a2ef | 2007-11-12 19:53:03 +0000 | [diff] [blame] | 45 | class TestPkg(unittest.TestCase): |
Collin Winter | 11e065b | 2007-08-24 19:15:12 +0000 | [diff] [blame] | 46 | |
| 47 | def setUp(self): |
| 48 | self.root = None |
Christian Heimes | dae2a89 | 2008-04-19 00:55:37 +0000 | [diff] [blame] | 49 | self.pkgname = None |
Collin Winter | 11e065b | 2007-08-24 19:15:12 +0000 | [diff] [blame] | 50 | self.syspath = list(sys.path) |
Christian Heimes | db4a2ef | 2007-11-12 19:53:03 +0000 | [diff] [blame] | 51 | self.sysmodules = sys.modules.copy() |
Collin Winter | 11e065b | 2007-08-24 19:15:12 +0000 | [diff] [blame] | 52 | |
| 53 | def tearDown(self): |
| 54 | sys.path[:] = self.syspath |
Christian Heimes | db4a2ef | 2007-11-12 19:53:03 +0000 | [diff] [blame] | 55 | sys.modules.clear() |
| 56 | sys.modules.update(self.sysmodules) |
| 57 | del self.sysmodules |
Collin Winter | 11e065b | 2007-08-24 19:15:12 +0000 | [diff] [blame] | 58 | cleanout(self.root) |
| 59 | |
Christian Heimes | dae2a89 | 2008-04-19 00:55:37 +0000 | [diff] [blame] | 60 | # delete all modules concerning the tested hiearchy |
| 61 | if self.pkgname: |
| 62 | modules = [name for name in sys.modules |
| 63 | if self.pkgname in name.split('.')] |
| 64 | for name in modules: |
| 65 | del sys.modules[name] |
| 66 | |
Collin Winter | 11e065b | 2007-08-24 19:15:12 +0000 | [diff] [blame] | 67 | def run_code(self, code): |
| 68 | exec(textwrap.dedent(code), globals(), {"self": self}) |
| 69 | |
| 70 | def mkhier(self, descr): |
| 71 | root = tempfile.mkdtemp() |
Guido van Rossum | 41360a4 | 1998-03-26 19:42:58 +0000 | [diff] [blame] | 72 | sys.path.insert(0, root) |
Collin Winter | 11e065b | 2007-08-24 19:15:12 +0000 | [diff] [blame] | 73 | if not os.path.isdir(root): |
| 74 | os.mkdir(root) |
| 75 | for name, contents in descr: |
| 76 | comps = name.split() |
| 77 | fullname = root |
| 78 | for c in comps: |
| 79 | fullname = os.path.join(fullname, c) |
| 80 | if contents is None: |
| 81 | os.mkdir(fullname) |
| 82 | else: |
| 83 | f = open(fullname, "w") |
| 84 | f.write(contents) |
| 85 | if contents and contents[-1] != '\n': |
| 86 | f.write('\n') |
| 87 | f.close() |
| 88 | self.root = root |
Christian Heimes | dae2a89 | 2008-04-19 00:55:37 +0000 | [diff] [blame] | 89 | # package name is the name of the first item |
| 90 | self.pkgname = descr[0][0] |
Guido van Rossum | 6c61242 | 1997-09-06 18:42:57 +0000 | [diff] [blame] | 91 | |
Collin Winter | 11e065b | 2007-08-24 19:15:12 +0000 | [diff] [blame] | 92 | def test_1(self): |
| 93 | hier = [("t1", None), ("t1 __init__.py", "")] |
| 94 | self.mkhier(hier) |
| 95 | import t1 |
Guido van Rossum | 6c61242 | 1997-09-06 18:42:57 +0000 | [diff] [blame] | 96 | |
Collin Winter | 11e065b | 2007-08-24 19:15:12 +0000 | [diff] [blame] | 97 | def test_2(self): |
| 98 | hier = [ |
| 99 | ("t2", None), |
| 100 | ("t2 __init__.py", "'doc for t2'"), |
| 101 | ("t2 sub", None), |
| 102 | ("t2 sub __init__.py", ""), |
| 103 | ("t2 sub subsub", None), |
| 104 | ("t2 sub subsub __init__.py", "spam = 1"), |
| 105 | ] |
| 106 | self.mkhier(hier) |
Fred Drake | 004d5e6 | 2000-10-23 17:22:08 +0000 | [diff] [blame] | 107 | |
Collin Winter | 11e065b | 2007-08-24 19:15:12 +0000 | [diff] [blame] | 108 | import t2 |
| 109 | self.assertEqual(t2.__doc__, "doc for t2") |
Fred Drake | 004d5e6 | 2000-10-23 17:22:08 +0000 | [diff] [blame] | 110 | |
Collin Winter | 11e065b | 2007-08-24 19:15:12 +0000 | [diff] [blame] | 111 | import t2.sub |
| 112 | import t2.sub.subsub |
| 113 | self.assertEqual(t2.__name__, "t2") |
| 114 | self.assertEqual(t2.sub.__name__, "t2.sub") |
| 115 | self.assertEqual(t2.sub.subsub.__name__, "t2.sub.subsub") |
Fred Drake | 004d5e6 | 2000-10-23 17:22:08 +0000 | [diff] [blame] | 116 | |
Collin Winter | 11e065b | 2007-08-24 19:15:12 +0000 | [diff] [blame] | 117 | # This exec crap is needed because Py3k forbids 'import *' outside |
| 118 | # of module-scope and __import__() is insufficient for what we need. |
| 119 | s = """ |
| 120 | import t2 |
| 121 | from t2 import * |
| 122 | self.assertEqual(dir(), ['self', 'sub', 't2']) |
| 123 | """ |
| 124 | self.run_code(s) |
Guido van Rossum | 6c61242 | 1997-09-06 18:42:57 +0000 | [diff] [blame] | 125 | |
Collin Winter | 11e065b | 2007-08-24 19:15:12 +0000 | [diff] [blame] | 126 | from t2 import sub |
| 127 | from t2.sub import subsub |
| 128 | from t2.sub.subsub import spam |
| 129 | self.assertEqual(sub.__name__, "t2.sub") |
| 130 | self.assertEqual(subsub.__name__, "t2.sub.subsub") |
| 131 | self.assertEqual(sub.subsub.__name__, "t2.sub.subsub") |
| 132 | for name in ['spam', 'sub', 'subsub', 't2']: |
| 133 | self.failUnless(locals()["name"], "Failed to import %s" % name) |
Guido van Rossum | 6c61242 | 1997-09-06 18:42:57 +0000 | [diff] [blame] | 134 | |
Collin Winter | 11e065b | 2007-08-24 19:15:12 +0000 | [diff] [blame] | 135 | import t2.sub |
| 136 | import t2.sub.subsub |
| 137 | self.assertEqual(t2.__name__, "t2") |
| 138 | self.assertEqual(t2.sub.__name__, "t2.sub") |
| 139 | self.assertEqual(t2.sub.subsub.__name__, "t2.sub.subsub") |
Fred Drake | 004d5e6 | 2000-10-23 17:22:08 +0000 | [diff] [blame] | 140 | |
Collin Winter | 11e065b | 2007-08-24 19:15:12 +0000 | [diff] [blame] | 141 | s = """ |
| 142 | from t2 import * |
| 143 | self.failUnless(dir(), ['self', 'sub']) |
| 144 | """ |
| 145 | self.run_code(s) |
Guido van Rossum | c8bf884 | 1997-09-08 16:06:20 +0000 | [diff] [blame] | 146 | |
Collin Winter | 11e065b | 2007-08-24 19:15:12 +0000 | [diff] [blame] | 147 | def test_3(self): |
| 148 | hier = [ |
| 149 | ("t3", None), |
| 150 | ("t3 __init__.py", ""), |
| 151 | ("t3 sub", None), |
| 152 | ("t3 sub __init__.py", ""), |
| 153 | ("t3 sub subsub", None), |
| 154 | ("t3 sub subsub __init__.py", "spam = 1"), |
| 155 | ] |
| 156 | self.mkhier(hier) |
Guido van Rossum | 6c61242 | 1997-09-06 18:42:57 +0000 | [diff] [blame] | 157 | |
Collin Winter | 11e065b | 2007-08-24 19:15:12 +0000 | [diff] [blame] | 158 | import t3.sub.subsub |
| 159 | self.assertEqual(t3.__name__, "t3") |
| 160 | self.assertEqual(t3.sub.__name__, "t3.sub") |
| 161 | self.assertEqual(t3.sub.subsub.__name__, "t3.sub.subsub") |
Guido van Rossum | 6c61242 | 1997-09-06 18:42:57 +0000 | [diff] [blame] | 162 | |
Collin Winter | 11e065b | 2007-08-24 19:15:12 +0000 | [diff] [blame] | 163 | def test_4(self): |
| 164 | hier = [ |
| 165 | ("t4.py", "raise RuntimeError('Shouldnt load t4.py')"), |
| 166 | ("t4", None), |
| 167 | ("t4 __init__.py", ""), |
| 168 | ("t4 sub.py", "raise RuntimeError('Shouldnt load sub.py')"), |
| 169 | ("t4 sub", None), |
| 170 | ("t4 sub __init__.py", ""), |
Thomas Wouters | ed03b41 | 2007-08-28 21:37:11 +0000 | [diff] [blame] | 171 | ("t4 sub subsub.py", |
| 172 | "raise RuntimeError('Shouldnt load subsub.py')"), |
Collin Winter | 11e065b | 2007-08-24 19:15:12 +0000 | [diff] [blame] | 173 | ("t4 sub subsub", None), |
| 174 | ("t4 sub subsub __init__.py", "spam = 1"), |
| 175 | ] |
| 176 | self.mkhier(hier) |
Guido van Rossum | 6c61242 | 1997-09-06 18:42:57 +0000 | [diff] [blame] | 177 | |
Collin Winter | 11e065b | 2007-08-24 19:15:12 +0000 | [diff] [blame] | 178 | s = """ |
| 179 | from t4.sub.subsub import * |
| 180 | self.assertEqual(spam, 1) |
| 181 | """ |
| 182 | self.run_code(s) |
Guido van Rossum | 6c61242 | 1997-09-06 18:42:57 +0000 | [diff] [blame] | 183 | |
Collin Winter | 11e065b | 2007-08-24 19:15:12 +0000 | [diff] [blame] | 184 | def test_5(self): |
| 185 | hier = [ |
| 186 | ("t5", None), |
| 187 | ("t5 __init__.py", "import t5.foo"), |
| 188 | ("t5 string.py", "spam = 1"), |
| 189 | ("t5 foo.py", |
| 190 | "from . import string; assert string.spam == 1"), |
| 191 | ] |
| 192 | self.mkhier(hier) |
Guido van Rossum | 6c61242 | 1997-09-06 18:42:57 +0000 | [diff] [blame] | 193 | |
Collin Winter | 11e065b | 2007-08-24 19:15:12 +0000 | [diff] [blame] | 194 | import t5 |
| 195 | s = """ |
| 196 | from t5 import * |
| 197 | self.assertEqual(dir(), ['foo', 'self', 'string', 't5']) |
| 198 | """ |
| 199 | self.run_code(s) |
Guido van Rossum | 10887a3 | 1997-09-07 06:12:11 +0000 | [diff] [blame] | 200 | |
Collin Winter | 11e065b | 2007-08-24 19:15:12 +0000 | [diff] [blame] | 201 | import t5 |
| 202 | self.assertEqual(fixdir(dir(t5)), |
| 203 | ['__doc__', '__file__', '__name__', |
Christian Heimes | cbf3b5c | 2007-12-03 21:02:03 +0000 | [diff] [blame] | 204 | '__package__', '__path__', 'foo', 'string', 't5']) |
Collin Winter | 11e065b | 2007-08-24 19:15:12 +0000 | [diff] [blame] | 205 | self.assertEqual(fixdir(dir(t5.foo)), |
Christian Heimes | cbf3b5c | 2007-12-03 21:02:03 +0000 | [diff] [blame] | 206 | ['__doc__', '__file__', '__name__', '__package__', |
| 207 | 'string']) |
Collin Winter | 11e065b | 2007-08-24 19:15:12 +0000 | [diff] [blame] | 208 | self.assertEqual(fixdir(dir(t5.string)), |
Christian Heimes | cbf3b5c | 2007-12-03 21:02:03 +0000 | [diff] [blame] | 209 | ['__doc__', '__file__', '__name__','__package__', |
| 210 | 'spam']) |
Guido van Rossum | 5f4fb91 | 1998-05-19 15:09:42 +0000 | [diff] [blame] | 211 | |
Collin Winter | 11e065b | 2007-08-24 19:15:12 +0000 | [diff] [blame] | 212 | def test_6(self): |
| 213 | hier = [ |
| 214 | ("t6", None), |
Thomas Wouters | ed03b41 | 2007-08-28 21:37:11 +0000 | [diff] [blame] | 215 | ("t6 __init__.py", |
| 216 | "__all__ = ['spam', 'ham', 'eggs']"), |
Collin Winter | 11e065b | 2007-08-24 19:15:12 +0000 | [diff] [blame] | 217 | ("t6 spam.py", ""), |
| 218 | ("t6 ham.py", ""), |
| 219 | ("t6 eggs.py", ""), |
| 220 | ] |
| 221 | self.mkhier(hier) |
| 222 | |
| 223 | import t6 |
| 224 | self.assertEqual(fixdir(dir(t6)), |
| 225 | ['__all__', '__doc__', '__file__', |
Christian Heimes | cbf3b5c | 2007-12-03 21:02:03 +0000 | [diff] [blame] | 226 | '__name__', '__package__', '__path__']) |
Collin Winter | 11e065b | 2007-08-24 19:15:12 +0000 | [diff] [blame] | 227 | s = """ |
| 228 | import t6 |
| 229 | from t6 import * |
| 230 | self.assertEqual(fixdir(dir(t6)), |
| 231 | ['__all__', '__doc__', '__file__', |
Christian Heimes | cbf3b5c | 2007-12-03 21:02:03 +0000 | [diff] [blame] | 232 | '__name__', '__package__', '__path__', |
| 233 | 'eggs', 'ham', 'spam']) |
Collin Winter | 11e065b | 2007-08-24 19:15:12 +0000 | [diff] [blame] | 234 | self.assertEqual(dir(), ['eggs', 'ham', 'self', 'spam', 't6']) |
| 235 | """ |
| 236 | self.run_code(s) |
| 237 | |
| 238 | def test_7(self): |
| 239 | hier = [ |
| 240 | ("t7.py", ""), |
| 241 | ("t7", None), |
| 242 | ("t7 __init__.py", ""), |
Thomas Wouters | ed03b41 | 2007-08-28 21:37:11 +0000 | [diff] [blame] | 243 | ("t7 sub.py", |
| 244 | "raise RuntimeError('Shouldnt load sub.py')"), |
Collin Winter | 11e065b | 2007-08-24 19:15:12 +0000 | [diff] [blame] | 245 | ("t7 sub", None), |
| 246 | ("t7 sub __init__.py", ""), |
Thomas Wouters | ed03b41 | 2007-08-28 21:37:11 +0000 | [diff] [blame] | 247 | ("t7 sub .py", |
Collin Winter | 11e065b | 2007-08-24 19:15:12 +0000 | [diff] [blame] | 248 | "raise RuntimeError('Shouldnt load subsub.py')"), |
| 249 | ("t7 sub subsub", None), |
| 250 | ("t7 sub subsub __init__.py", |
| 251 | "spam = 1"), |
| 252 | ] |
| 253 | self.mkhier(hier) |
| 254 | |
| 255 | |
| 256 | t7, sub, subsub = None, None, None |
| 257 | import t7 as tas |
| 258 | self.assertEqual(fixdir(dir(tas)), |
Christian Heimes | cbf3b5c | 2007-12-03 21:02:03 +0000 | [diff] [blame] | 259 | ['__doc__', '__file__', '__name__', |
| 260 | '__package__', '__path__']) |
Collin Winter | 11e065b | 2007-08-24 19:15:12 +0000 | [diff] [blame] | 261 | self.failIf(t7) |
| 262 | from t7 import sub as subpar |
| 263 | self.assertEqual(fixdir(dir(subpar)), |
Christian Heimes | cbf3b5c | 2007-12-03 21:02:03 +0000 | [diff] [blame] | 264 | ['__doc__', '__file__', '__name__', |
| 265 | '__package__', '__path__']) |
Collin Winter | 11e065b | 2007-08-24 19:15:12 +0000 | [diff] [blame] | 266 | self.failIf(t7) |
| 267 | self.failIf(sub) |
| 268 | from t7.sub import subsub as subsubsub |
| 269 | self.assertEqual(fixdir(dir(subsubsub)), |
Christian Heimes | cbf3b5c | 2007-12-03 21:02:03 +0000 | [diff] [blame] | 270 | ['__doc__', '__file__', '__name__', |
| 271 | '__package__', '__path__', 'spam']) |
Collin Winter | 11e065b | 2007-08-24 19:15:12 +0000 | [diff] [blame] | 272 | self.failIf(t7) |
| 273 | self.failIf(sub) |
| 274 | self.failIf(subsub) |
| 275 | from t7.sub.subsub import spam as ham |
| 276 | self.assertEqual(ham, 1) |
| 277 | self.failIf(t7) |
| 278 | self.failIf(sub) |
| 279 | self.failIf(subsub) |
| 280 | |
| 281 | |
| 282 | def test_main(): |
Benjamin Peterson | ee8712c | 2008-05-20 21:35:26 +0000 | [diff] [blame] | 283 | support.run_unittest(__name__) |
Collin Winter | 11e065b | 2007-08-24 19:15:12 +0000 | [diff] [blame] | 284 | |
| 285 | |
| 286 | if __name__ == "__main__": |
| 287 | test_main() |