blob: 1064617f2d8588ab3a45f2c8272d0d490d646704 [file] [log] [blame]
Guido van Rossum6c612421997-09-06 18:42:57 +00001# Test packages (dotted-name import)
2
Collin Winter11e065b2007-08-24 19:15:12 +00003import sys
4import os
5import tempfile
6import textwrap
Collin Winter11e065b2007-08-24 19:15:12 +00007import unittest
Benjamin Petersonee8712c2008-05-20 21:35:26 +00008from test import support
Collin Winter11e065b2007-08-24 19:15:12 +00009
Guido van Rossum6c612421997-09-06 18:42:57 +000010
11# Helpers to create and destroy hierarchies.
12
Guido van Rossum6c612421997-09-06 18:42:57 +000013def cleanout(root):
14 names = os.listdir(root)
15 for name in names:
Guido van Rossum41360a41998-03-26 19:42:58 +000016 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 Winter11e065b2007-08-24 19:15:12 +000021 os.rmdir(root)
Guido van Rossum6c612421997-09-06 18:42:57 +000022
Barry Warsaw3a9d0612000-09-01 06:53:52 +000023def fixdir(lst):
Collin Winter11e065b2007-08-24 19:15:12 +000024 if "__builtins__" in lst:
25 lst.remove("__builtins__")
Barry Warsaw3a9d0612000-09-01 06:53:52 +000026 return lst
27
Guido van Rossum6c612421997-09-06 18:42:57 +000028
Collin Winter11e065b2007-08-24 19:15:12 +000029# 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 Heimesdb4a2ef2007-11-12 19:53:03 +000045class TestPkg(unittest.TestCase):
Collin Winter11e065b2007-08-24 19:15:12 +000046
47 def setUp(self):
48 self.root = None
Christian Heimesdae2a892008-04-19 00:55:37 +000049 self.pkgname = None
Collin Winter11e065b2007-08-24 19:15:12 +000050 self.syspath = list(sys.path)
Christian Heimesdb4a2ef2007-11-12 19:53:03 +000051 self.sysmodules = sys.modules.copy()
Collin Winter11e065b2007-08-24 19:15:12 +000052
53 def tearDown(self):
54 sys.path[:] = self.syspath
Christian Heimesdb4a2ef2007-11-12 19:53:03 +000055 sys.modules.clear()
56 sys.modules.update(self.sysmodules)
57 del self.sysmodules
Collin Winter11e065b2007-08-24 19:15:12 +000058 cleanout(self.root)
59
Christian Heimesdae2a892008-04-19 00:55:37 +000060 # 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 Winter11e065b2007-08-24 19:15:12 +000067 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 Rossum41360a41998-03-26 19:42:58 +000072 sys.path.insert(0, root)
Collin Winter11e065b2007-08-24 19:15:12 +000073 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 Heimesdae2a892008-04-19 00:55:37 +000089 # package name is the name of the first item
90 self.pkgname = descr[0][0]
Guido van Rossum6c612421997-09-06 18:42:57 +000091
Collin Winter11e065b2007-08-24 19:15:12 +000092 def test_1(self):
93 hier = [("t1", None), ("t1 __init__.py", "")]
94 self.mkhier(hier)
95 import t1
Guido van Rossum6c612421997-09-06 18:42:57 +000096
Collin Winter11e065b2007-08-24 19:15:12 +000097 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 Drake004d5e62000-10-23 17:22:08 +0000107
Collin Winter11e065b2007-08-24 19:15:12 +0000108 import t2
109 self.assertEqual(t2.__doc__, "doc for t2")
Fred Drake004d5e62000-10-23 17:22:08 +0000110
Collin Winter11e065b2007-08-24 19:15:12 +0000111 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 Drake004d5e62000-10-23 17:22:08 +0000116
Collin Winter11e065b2007-08-24 19:15:12 +0000117 # 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 Rossum6c612421997-09-06 18:42:57 +0000125
Collin Winter11e065b2007-08-24 19:15:12 +0000126 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 Rossum6c612421997-09-06 18:42:57 +0000134
Collin Winter11e065b2007-08-24 19:15:12 +0000135 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 Drake004d5e62000-10-23 17:22:08 +0000140
Collin Winter11e065b2007-08-24 19:15:12 +0000141 s = """
142 from t2 import *
143 self.failUnless(dir(), ['self', 'sub'])
144 """
145 self.run_code(s)
Guido van Rossumc8bf8841997-09-08 16:06:20 +0000146
Collin Winter11e065b2007-08-24 19:15:12 +0000147 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 Rossum6c612421997-09-06 18:42:57 +0000157
Collin Winter11e065b2007-08-24 19:15:12 +0000158 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 Rossum6c612421997-09-06 18:42:57 +0000162
Collin Winter11e065b2007-08-24 19:15:12 +0000163 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 Woutersed03b412007-08-28 21:37:11 +0000171 ("t4 sub subsub.py",
172 "raise RuntimeError('Shouldnt load subsub.py')"),
Collin Winter11e065b2007-08-24 19:15:12 +0000173 ("t4 sub subsub", None),
174 ("t4 sub subsub __init__.py", "spam = 1"),
175 ]
176 self.mkhier(hier)
Guido van Rossum6c612421997-09-06 18:42:57 +0000177
Collin Winter11e065b2007-08-24 19:15:12 +0000178 s = """
179 from t4.sub.subsub import *
180 self.assertEqual(spam, 1)
181 """
182 self.run_code(s)
Guido van Rossum6c612421997-09-06 18:42:57 +0000183
Collin Winter11e065b2007-08-24 19:15:12 +0000184 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 Rossum6c612421997-09-06 18:42:57 +0000193
Collin Winter11e065b2007-08-24 19:15:12 +0000194 import t5
195 s = """
196 from t5 import *
197 self.assertEqual(dir(), ['foo', 'self', 'string', 't5'])
198 """
199 self.run_code(s)
Guido van Rossum10887a31997-09-07 06:12:11 +0000200
Collin Winter11e065b2007-08-24 19:15:12 +0000201 import t5
202 self.assertEqual(fixdir(dir(t5)),
203 ['__doc__', '__file__', '__name__',
Christian Heimescbf3b5c2007-12-03 21:02:03 +0000204 '__package__', '__path__', 'foo', 'string', 't5'])
Collin Winter11e065b2007-08-24 19:15:12 +0000205 self.assertEqual(fixdir(dir(t5.foo)),
Christian Heimescbf3b5c2007-12-03 21:02:03 +0000206 ['__doc__', '__file__', '__name__', '__package__',
207 'string'])
Collin Winter11e065b2007-08-24 19:15:12 +0000208 self.assertEqual(fixdir(dir(t5.string)),
Christian Heimescbf3b5c2007-12-03 21:02:03 +0000209 ['__doc__', '__file__', '__name__','__package__',
210 'spam'])
Guido van Rossum5f4fb911998-05-19 15:09:42 +0000211
Collin Winter11e065b2007-08-24 19:15:12 +0000212 def test_6(self):
213 hier = [
214 ("t6", None),
Thomas Woutersed03b412007-08-28 21:37:11 +0000215 ("t6 __init__.py",
216 "__all__ = ['spam', 'ham', 'eggs']"),
Collin Winter11e065b2007-08-24 19:15:12 +0000217 ("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 Heimescbf3b5c2007-12-03 21:02:03 +0000226 '__name__', '__package__', '__path__'])
Collin Winter11e065b2007-08-24 19:15:12 +0000227 s = """
228 import t6
229 from t6 import *
230 self.assertEqual(fixdir(dir(t6)),
231 ['__all__', '__doc__', '__file__',
Christian Heimescbf3b5c2007-12-03 21:02:03 +0000232 '__name__', '__package__', '__path__',
233 'eggs', 'ham', 'spam'])
Collin Winter11e065b2007-08-24 19:15:12 +0000234 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 Woutersed03b412007-08-28 21:37:11 +0000243 ("t7 sub.py",
244 "raise RuntimeError('Shouldnt load sub.py')"),
Collin Winter11e065b2007-08-24 19:15:12 +0000245 ("t7 sub", None),
246 ("t7 sub __init__.py", ""),
Thomas Woutersed03b412007-08-28 21:37:11 +0000247 ("t7 sub .py",
Collin Winter11e065b2007-08-24 19:15:12 +0000248 "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 Heimescbf3b5c2007-12-03 21:02:03 +0000259 ['__doc__', '__file__', '__name__',
260 '__package__', '__path__'])
Collin Winter11e065b2007-08-24 19:15:12 +0000261 self.failIf(t7)
262 from t7 import sub as subpar
263 self.assertEqual(fixdir(dir(subpar)),
Christian Heimescbf3b5c2007-12-03 21:02:03 +0000264 ['__doc__', '__file__', '__name__',
265 '__package__', '__path__'])
Collin Winter11e065b2007-08-24 19:15:12 +0000266 self.failIf(t7)
267 self.failIf(sub)
268 from t7.sub import subsub as subsubsub
269 self.assertEqual(fixdir(dir(subsubsub)),
Christian Heimescbf3b5c2007-12-03 21:02:03 +0000270 ['__doc__', '__file__', '__name__',
271 '__package__', '__path__', 'spam'])
Collin Winter11e065b2007-08-24 19:15:12 +0000272 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
282def test_main():
Benjamin Petersonee8712c2008-05-20 21:35:26 +0000283 support.run_unittest(__name__)
Collin Winter11e065b2007-08-24 19:15:12 +0000284
285
286if __name__ == "__main__":
287 test_main()