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