annotate Lib/test/test_pkg.py @ 58925:eecf1bef052d 2.6

Merged revisions 78207 via svnmerge from svn+ssh://pythondev@svn.python.org/python/trunk ........ r78207 | ezio.melotti | 2010-02-17 01:26:09 +0200 (Wed, 17 Feb 2010) | 1 line #7930: fix stripid ........
author Ezio Melotti <ezio.melotti@gmail.com>
date Tue, 16 Feb 2010 23:29:44 +0000
parents 5c43159f5ad0
children 6768b2350c57
Ignore whitespace changes - Everywhere: Within whitespace: At end of lines:
rev   line source
6247
1496444e8c8b Test set for package import.
Guido van Rossum <guido@python.org>
parents:
diff changeset
1 # Test packages (dotted-name import)
1496444e8c8b Test set for package import.
Guido van Rossum <guido@python.org>
parents:
diff changeset
2
42789
721c3ea13d84 Convert test_pkg to use unittest.
Collin Winter <collinw@gmail.com>
parents: 24816
diff changeset
3 import sys
721c3ea13d84 Convert test_pkg to use unittest.
Collin Winter <collinw@gmail.com>
parents: 24816
diff changeset
4 import os
721c3ea13d84 Convert test_pkg to use unittest.
Collin Winter <collinw@gmail.com>
parents: 24816
diff changeset
5 import tempfile
721c3ea13d84 Convert test_pkg to use unittest.
Collin Winter <collinw@gmail.com>
parents: 24816
diff changeset
6 import textwrap
721c3ea13d84 Convert test_pkg to use unittest.
Collin Winter <collinw@gmail.com>
parents: 24816
diff changeset
7 import unittest
721c3ea13d84 Convert test_pkg to use unittest.
Collin Winter <collinw@gmail.com>
parents: 24816
diff changeset
8 from test import test_support
721c3ea13d84 Convert test_pkg to use unittest.
Collin Winter <collinw@gmail.com>
parents: 24816
diff changeset
9
6247
1496444e8c8b Test set for package import.
Guido van Rossum <guido@python.org>
parents:
diff changeset
10
1496444e8c8b Test set for package import.
Guido van Rossum <guido@python.org>
parents:
diff changeset
11 # Helpers to create and destroy hierarchies.
1496444e8c8b Test set for package import.
Guido van Rossum <guido@python.org>
parents:
diff changeset
12
1496444e8c8b Test set for package import.
Guido van Rossum <guido@python.org>
parents:
diff changeset
13 def cleanout(root):
1496444e8c8b Test set for package import.
Guido van Rossum <guido@python.org>
parents:
diff changeset
14 names = os.listdir(root)
1496444e8c8b Test set for package import.
Guido van Rossum <guido@python.org>
parents:
diff changeset
15 for name in names:
7747
aedd2d0dcb31 Mass check-in after untabifying all files that need it.
Guido van Rossum <guido@python.org>
parents: 6539
diff changeset
16 fullname = os.path.join(root, name)
aedd2d0dcb31 Mass check-in after untabifying all files that need it.
Guido van Rossum <guido@python.org>
parents: 6539
diff changeset
17 if os.path.isdir(fullname) and not os.path.islink(fullname):
aedd2d0dcb31 Mass check-in after untabifying all files that need it.
Guido van Rossum <guido@python.org>
parents: 6539
diff changeset
18 cleanout(fullname)
aedd2d0dcb31 Mass check-in after untabifying all files that need it.
Guido van Rossum <guido@python.org>
parents: 6539
diff changeset
19 else:
aedd2d0dcb31 Mass check-in after untabifying all files that need it.
Guido van Rossum <guido@python.org>
parents: 6539
diff changeset
20 os.remove(fullname)
42789
721c3ea13d84 Convert test_pkg to use unittest.
Collin Winter <collinw@gmail.com>
parents: 24816
diff changeset
21 os.rmdir(root)
6247
1496444e8c8b Test set for package import.
Guido van Rossum <guido@python.org>
parents:
diff changeset
22
14342
d41c41371dc1 Applying patch #100994 to allow JPython to use more of the standard
Barry Warsaw <barry@python.org>
parents: 14038
diff changeset
23 def fixdir(lst):
42789
721c3ea13d84 Convert test_pkg to use unittest.
Collin Winter <collinw@gmail.com>
parents: 24816
diff changeset
24 if "__builtins__" in lst:
721c3ea13d84 Convert test_pkg to use unittest.
Collin Winter <collinw@gmail.com>
parents: 24816
diff changeset
25 lst.remove("__builtins__")
14342
d41c41371dc1 Applying patch #100994 to allow JPython to use more of the standard
Barry Warsaw <barry@python.org>
parents: 14038
diff changeset
26 return lst
d41c41371dc1 Applying patch #100994 to allow JPython to use more of the standard
Barry Warsaw <barry@python.org>
parents: 14038
diff changeset
27
42789
721c3ea13d84 Convert test_pkg to use unittest.
Collin Winter <collinw@gmail.com>
parents: 24816
diff changeset
28
721c3ea13d84 Convert test_pkg to use unittest.
Collin Winter <collinw@gmail.com>
parents: 24816
diff changeset
29 # XXX Things to test
721c3ea13d84 Convert test_pkg to use unittest.
Collin Winter <collinw@gmail.com>
parents: 24816
diff changeset
30 #
721c3ea13d84 Convert test_pkg to use unittest.
Collin Winter <collinw@gmail.com>
parents: 24816
diff changeset
31 # import package without __init__
721c3ea13d84 Convert test_pkg to use unittest.
Collin Winter <collinw@gmail.com>
parents: 24816
diff changeset
32 # import package with __init__
721c3ea13d84 Convert test_pkg to use unittest.
Collin Winter <collinw@gmail.com>
parents: 24816
diff changeset
33 # __init__ importing submodule
721c3ea13d84 Convert test_pkg to use unittest.
Collin Winter <collinw@gmail.com>
parents: 24816
diff changeset
34 # __init__ importing global module
721c3ea13d84 Convert test_pkg to use unittest.
Collin Winter <collinw@gmail.com>
parents: 24816
diff changeset
35 # __init__ defining variables
721c3ea13d84 Convert test_pkg to use unittest.
Collin Winter <collinw@gmail.com>
parents: 24816
diff changeset
36 # submodule importing other submodule
721c3ea13d84 Convert test_pkg to use unittest.
Collin Winter <collinw@gmail.com>
parents: 24816
diff changeset
37 # submodule importing global module
721c3ea13d84 Convert test_pkg to use unittest.
Collin Winter <collinw@gmail.com>
parents: 24816
diff changeset
38 # submodule import submodule via global name
721c3ea13d84 Convert test_pkg to use unittest.
Collin Winter <collinw@gmail.com>
parents: 24816
diff changeset
39 # from package import submodule
721c3ea13d84 Convert test_pkg to use unittest.
Collin Winter <collinw@gmail.com>
parents: 24816
diff changeset
40 # from package import subpackage
721c3ea13d84 Convert test_pkg to use unittest.
Collin Winter <collinw@gmail.com>
parents: 24816
diff changeset
41 # from package import variable (defined in __init__)
721c3ea13d84 Convert test_pkg to use unittest.
Collin Winter <collinw@gmail.com>
parents: 24816
diff changeset
42 # from package import * (defined in __init__)
6247
1496444e8c8b Test set for package import.
Guido van Rossum <guido@python.org>
parents:
diff changeset
43
42789
721c3ea13d84 Convert test_pkg to use unittest.
Collin Winter <collinw@gmail.com>
parents: 24816
diff changeset
44
721c3ea13d84 Convert test_pkg to use unittest.
Collin Winter <collinw@gmail.com>
parents: 24816
diff changeset
45 class Test(unittest.TestCase):
721c3ea13d84 Convert test_pkg to use unittest.
Collin Winter <collinw@gmail.com>
parents: 24816
diff changeset
46
721c3ea13d84 Convert test_pkg to use unittest.
Collin Winter <collinw@gmail.com>
parents: 24816
diff changeset
47 def setUp(self):
721c3ea13d84 Convert test_pkg to use unittest.
Collin Winter <collinw@gmail.com>
parents: 24816
diff changeset
48 self.root = None
46474
5c43159f5ad0 Some tests did not pass on repeated calls (regrtest -R::)
Amaury Forgeot d'Arc <amauryfa@gmail.com>
parents: 45468
diff changeset
49 self.pkgname = None
42789
721c3ea13d84 Convert test_pkg to use unittest.
Collin Winter <collinw@gmail.com>
parents: 24816
diff changeset
50 self.syspath = list(sys.path)
721c3ea13d84 Convert test_pkg to use unittest.
Collin Winter <collinw@gmail.com>
parents: 24816
diff changeset
51
721c3ea13d84 Convert test_pkg to use unittest.
Collin Winter <collinw@gmail.com>
parents: 24816
diff changeset
52 def tearDown(self):
721c3ea13d84 Convert test_pkg to use unittest.
Collin Winter <collinw@gmail.com>
parents: 24816
diff changeset
53 sys.path[:] = self.syspath
721c3ea13d84 Convert test_pkg to use unittest.
Collin Winter <collinw@gmail.com>
parents: 24816
diff changeset
54 cleanout(self.root)
721c3ea13d84 Convert test_pkg to use unittest.
Collin Winter <collinw@gmail.com>
parents: 24816
diff changeset
55
46474
5c43159f5ad0 Some tests did not pass on repeated calls (regrtest -R::)
Amaury Forgeot d'Arc <amauryfa@gmail.com>
parents: 45468
diff changeset
56 # delete all modules concerning the tested hiearchy
5c43159f5ad0 Some tests did not pass on repeated calls (regrtest -R::)
Amaury Forgeot d'Arc <amauryfa@gmail.com>
parents: 45468
diff changeset
57 if self.pkgname:
5c43159f5ad0 Some tests did not pass on repeated calls (regrtest -R::)
Amaury Forgeot d'Arc <amauryfa@gmail.com>
parents: 45468
diff changeset
58 modules = [name for name in sys.modules
5c43159f5ad0 Some tests did not pass on repeated calls (regrtest -R::)
Amaury Forgeot d'Arc <amauryfa@gmail.com>
parents: 45468
diff changeset
59 if self.pkgname in name.split('.')]
5c43159f5ad0 Some tests did not pass on repeated calls (regrtest -R::)
Amaury Forgeot d'Arc <amauryfa@gmail.com>
parents: 45468
diff changeset
60 for name in modules:
5c43159f5ad0 Some tests did not pass on repeated calls (regrtest -R::)
Amaury Forgeot d'Arc <amauryfa@gmail.com>
parents: 45468
diff changeset
61 del sys.modules[name]
5c43159f5ad0 Some tests did not pass on repeated calls (regrtest -R::)
Amaury Forgeot d'Arc <amauryfa@gmail.com>
parents: 45468
diff changeset
62
42789
721c3ea13d84 Convert test_pkg to use unittest.
Collin Winter <collinw@gmail.com>
parents: 24816
diff changeset
63 def run_code(self, code):
721c3ea13d84 Convert test_pkg to use unittest.
Collin Winter <collinw@gmail.com>
parents: 24816
diff changeset
64 exec(textwrap.dedent(code), globals(), {"self": self})
721c3ea13d84 Convert test_pkg to use unittest.
Collin Winter <collinw@gmail.com>
parents: 24816
diff changeset
65
721c3ea13d84 Convert test_pkg to use unittest.
Collin Winter <collinw@gmail.com>
parents: 24816
diff changeset
66 def mkhier(self, descr):
721c3ea13d84 Convert test_pkg to use unittest.
Collin Winter <collinw@gmail.com>
parents: 24816
diff changeset
67 root = tempfile.mkdtemp()
7747
aedd2d0dcb31 Mass check-in after untabifying all files that need it.
Guido van Rossum <guido@python.org>
parents: 6539
diff changeset
68 sys.path.insert(0, root)
42789
721c3ea13d84 Convert test_pkg to use unittest.
Collin Winter <collinw@gmail.com>
parents: 24816
diff changeset
69 if not os.path.isdir(root):
721c3ea13d84 Convert test_pkg to use unittest.
Collin Winter <collinw@gmail.com>
parents: 24816
diff changeset
70 os.mkdir(root)
721c3ea13d84 Convert test_pkg to use unittest.
Collin Winter <collinw@gmail.com>
parents: 24816
diff changeset
71 for name, contents in descr:
721c3ea13d84 Convert test_pkg to use unittest.
Collin Winter <collinw@gmail.com>
parents: 24816
diff changeset
72 comps = name.split()
721c3ea13d84 Convert test_pkg to use unittest.
Collin Winter <collinw@gmail.com>
parents: 24816
diff changeset
73 fullname = root
721c3ea13d84 Convert test_pkg to use unittest.
Collin Winter <collinw@gmail.com>
parents: 24816
diff changeset
74 for c in comps:
721c3ea13d84 Convert test_pkg to use unittest.
Collin Winter <collinw@gmail.com>
parents: 24816
diff changeset
75 fullname = os.path.join(fullname, c)
721c3ea13d84 Convert test_pkg to use unittest.
Collin Winter <collinw@gmail.com>
parents: 24816
diff changeset
76 if contents is None:
721c3ea13d84 Convert test_pkg to use unittest.
Collin Winter <collinw@gmail.com>
parents: 24816
diff changeset
77 os.mkdir(fullname)
721c3ea13d84 Convert test_pkg to use unittest.
Collin Winter <collinw@gmail.com>
parents: 24816
diff changeset
78 else:
721c3ea13d84 Convert test_pkg to use unittest.
Collin Winter <collinw@gmail.com>
parents: 24816
diff changeset
79 f = open(fullname, "w")
721c3ea13d84 Convert test_pkg to use unittest.
Collin Winter <collinw@gmail.com>
parents: 24816
diff changeset
80 f.write(contents)
721c3ea13d84 Convert test_pkg to use unittest.
Collin Winter <collinw@gmail.com>
parents: 24816
diff changeset
81 if contents and contents[-1] != '\n':
721c3ea13d84 Convert test_pkg to use unittest.
Collin Winter <collinw@gmail.com>
parents: 24816
diff changeset
82 f.write('\n')
721c3ea13d84 Convert test_pkg to use unittest.
Collin Winter <collinw@gmail.com>
parents: 24816
diff changeset
83 f.close()
721c3ea13d84 Convert test_pkg to use unittest.
Collin Winter <collinw@gmail.com>
parents: 24816
diff changeset
84 self.root = root
46474
5c43159f5ad0 Some tests did not pass on repeated calls (regrtest -R::)
Amaury Forgeot d'Arc <amauryfa@gmail.com>
parents: 45468
diff changeset
85 # package name is the name of the first item
5c43159f5ad0 Some tests did not pass on repeated calls (regrtest -R::)
Amaury Forgeot d'Arc <amauryfa@gmail.com>
parents: 45468
diff changeset
86 self.pkgname = descr[0][0]
6247
1496444e8c8b Test set for package import.
Guido van Rossum <guido@python.org>
parents:
diff changeset
87
42789
721c3ea13d84 Convert test_pkg to use unittest.
Collin Winter <collinw@gmail.com>
parents: 24816
diff changeset
88 def test_1(self):
721c3ea13d84 Convert test_pkg to use unittest.
Collin Winter <collinw@gmail.com>
parents: 24816
diff changeset
89 hier = [("t1", None), ("t1 __init__"+os.extsep+"py", "")]
721c3ea13d84 Convert test_pkg to use unittest.
Collin Winter <collinw@gmail.com>
parents: 24816
diff changeset
90 self.mkhier(hier)
721c3ea13d84 Convert test_pkg to use unittest.
Collin Winter <collinw@gmail.com>
parents: 24816
diff changeset
91 import t1
15305
07f24261c6e6 Make reindent.py happy (convert everything to 4-space indents!).
Fred Drake <fdrake@acm.org>
parents: 14342
diff changeset
92
42789
721c3ea13d84 Convert test_pkg to use unittest.
Collin Winter <collinw@gmail.com>
parents: 24816
diff changeset
93 def test_2(self):
721c3ea13d84 Convert test_pkg to use unittest.
Collin Winter <collinw@gmail.com>
parents: 24816
diff changeset
94 hier = [
721c3ea13d84 Convert test_pkg to use unittest.
Collin Winter <collinw@gmail.com>
parents: 24816
diff changeset
95 ("t2", None),
721c3ea13d84 Convert test_pkg to use unittest.
Collin Winter <collinw@gmail.com>
parents: 24816
diff changeset
96 ("t2 __init__"+os.extsep+"py", "'doc for t2'"),
721c3ea13d84 Convert test_pkg to use unittest.
Collin Winter <collinw@gmail.com>
parents: 24816
diff changeset
97 ("t2 sub", None),
721c3ea13d84 Convert test_pkg to use unittest.
Collin Winter <collinw@gmail.com>
parents: 24816
diff changeset
98 ("t2 sub __init__"+os.extsep+"py", ""),
721c3ea13d84 Convert test_pkg to use unittest.
Collin Winter <collinw@gmail.com>
parents: 24816
diff changeset
99 ("t2 sub subsub", None),
721c3ea13d84 Convert test_pkg to use unittest.
Collin Winter <collinw@gmail.com>
parents: 24816
diff changeset
100 ("t2 sub subsub __init__"+os.extsep+"py", "spam = 1"),
721c3ea13d84 Convert test_pkg to use unittest.
Collin Winter <collinw@gmail.com>
parents: 24816
diff changeset
101 ]
721c3ea13d84 Convert test_pkg to use unittest.
Collin Winter <collinw@gmail.com>
parents: 24816
diff changeset
102 self.mkhier(hier)
721c3ea13d84 Convert test_pkg to use unittest.
Collin Winter <collinw@gmail.com>
parents: 24816
diff changeset
103
721c3ea13d84 Convert test_pkg to use unittest.
Collin Winter <collinw@gmail.com>
parents: 24816
diff changeset
104 import t2
721c3ea13d84 Convert test_pkg to use unittest.
Collin Winter <collinw@gmail.com>
parents: 24816
diff changeset
105 self.assertEqual(t2.__doc__, "doc for t2")
721c3ea13d84 Convert test_pkg to use unittest.
Collin Winter <collinw@gmail.com>
parents: 24816
diff changeset
106
721c3ea13d84 Convert test_pkg to use unittest.
Collin Winter <collinw@gmail.com>
parents: 24816
diff changeset
107 import t2.sub
721c3ea13d84 Convert test_pkg to use unittest.
Collin Winter <collinw@gmail.com>
parents: 24816
diff changeset
108 import t2.sub.subsub
721c3ea13d84 Convert test_pkg to use unittest.
Collin Winter <collinw@gmail.com>
parents: 24816
diff changeset
109 self.assertEqual(t2.__name__, "t2")
721c3ea13d84 Convert test_pkg to use unittest.
Collin Winter <collinw@gmail.com>
parents: 24816
diff changeset
110 self.assertEqual(t2.sub.__name__, "t2.sub")
721c3ea13d84 Convert test_pkg to use unittest.
Collin Winter <collinw@gmail.com>
parents: 24816
diff changeset
111 self.assertEqual(t2.sub.subsub.__name__, "t2.sub.subsub")
721c3ea13d84 Convert test_pkg to use unittest.
Collin Winter <collinw@gmail.com>
parents: 24816
diff changeset
112
721c3ea13d84 Convert test_pkg to use unittest.
Collin Winter <collinw@gmail.com>
parents: 24816
diff changeset
113 # This exec crap is needed because Py3k forbids 'import *' outside
721c3ea13d84 Convert test_pkg to use unittest.
Collin Winter <collinw@gmail.com>
parents: 24816
diff changeset
114 # of module-scope and __import__() is insufficient for what we need.
721c3ea13d84 Convert test_pkg to use unittest.
Collin Winter <collinw@gmail.com>
parents: 24816
diff changeset
115 s = """
721c3ea13d84 Convert test_pkg to use unittest.
Collin Winter <collinw@gmail.com>
parents: 24816
diff changeset
116 import t2
721c3ea13d84 Convert test_pkg to use unittest.
Collin Winter <collinw@gmail.com>
parents: 24816
diff changeset
117 from t2 import *
721c3ea13d84 Convert test_pkg to use unittest.
Collin Winter <collinw@gmail.com>
parents: 24816
diff changeset
118 self.assertEqual(dir(), ['self', 'sub', 't2'])
721c3ea13d84 Convert test_pkg to use unittest.
Collin Winter <collinw@gmail.com>
parents: 24816
diff changeset
119 """
721c3ea13d84 Convert test_pkg to use unittest.
Collin Winter <collinw@gmail.com>
parents: 24816
diff changeset
120 self.run_code(s)
15305
07f24261c6e6 Make reindent.py happy (convert everything to 4-space indents!).
Fred Drake <fdrake@acm.org>
parents: 14342
diff changeset
121
42789
721c3ea13d84 Convert test_pkg to use unittest.
Collin Winter <collinw@gmail.com>
parents: 24816
diff changeset
122 from t2 import sub
721c3ea13d84 Convert test_pkg to use unittest.
Collin Winter <collinw@gmail.com>
parents: 24816
diff changeset
123 from t2.sub import subsub
721c3ea13d84 Convert test_pkg to use unittest.
Collin Winter <collinw@gmail.com>
parents: 24816
diff changeset
124 from t2.sub.subsub import spam
721c3ea13d84 Convert test_pkg to use unittest.
Collin Winter <collinw@gmail.com>
parents: 24816
diff changeset
125 self.assertEqual(sub.__name__, "t2.sub")
721c3ea13d84 Convert test_pkg to use unittest.
Collin Winter <collinw@gmail.com>
parents: 24816
diff changeset
126 self.assertEqual(subsub.__name__, "t2.sub.subsub")
721c3ea13d84 Convert test_pkg to use unittest.
Collin Winter <collinw@gmail.com>
parents: 24816
diff changeset
127 self.assertEqual(sub.subsub.__name__, "t2.sub.subsub")
721c3ea13d84 Convert test_pkg to use unittest.
Collin Winter <collinw@gmail.com>
parents: 24816
diff changeset
128 for name in ['spam', 'sub', 'subsub', 't2']:
721c3ea13d84 Convert test_pkg to use unittest.
Collin Winter <collinw@gmail.com>
parents: 24816
diff changeset
129 self.failUnless(locals()["name"], "Failed to import %s" % name)
721c3ea13d84 Convert test_pkg to use unittest.
Collin Winter <collinw@gmail.com>
parents: 24816
diff changeset
130
721c3ea13d84 Convert test_pkg to use unittest.
Collin Winter <collinw@gmail.com>
parents: 24816
diff changeset
131 import t2.sub
721c3ea13d84 Convert test_pkg to use unittest.
Collin Winter <collinw@gmail.com>
parents: 24816
diff changeset
132 import t2.sub.subsub
721c3ea13d84 Convert test_pkg to use unittest.
Collin Winter <collinw@gmail.com>
parents: 24816
diff changeset
133 self.assertEqual(t2.__name__, "t2")
721c3ea13d84 Convert test_pkg to use unittest.
Collin Winter <collinw@gmail.com>
parents: 24816
diff changeset
134 self.assertEqual(t2.sub.__name__, "t2.sub")
721c3ea13d84 Convert test_pkg to use unittest.
Collin Winter <collinw@gmail.com>
parents: 24816
diff changeset
135 self.assertEqual(t2.sub.subsub.__name__, "t2.sub.subsub")
15305
07f24261c6e6 Make reindent.py happy (convert everything to 4-space indents!).
Fred Drake <fdrake@acm.org>
parents: 14342
diff changeset
136
42789
721c3ea13d84 Convert test_pkg to use unittest.
Collin Winter <collinw@gmail.com>
parents: 24816
diff changeset
137 s = """
721c3ea13d84 Convert test_pkg to use unittest.
Collin Winter <collinw@gmail.com>
parents: 24816
diff changeset
138 from t2 import *
721c3ea13d84 Convert test_pkg to use unittest.
Collin Winter <collinw@gmail.com>
parents: 24816
diff changeset
139 self.failUnless(dir(), ['self', 'sub'])
721c3ea13d84 Convert test_pkg to use unittest.
Collin Winter <collinw@gmail.com>
parents: 24816
diff changeset
140 """
721c3ea13d84 Convert test_pkg to use unittest.
Collin Winter <collinw@gmail.com>
parents: 24816
diff changeset
141 self.run_code(s)
721c3ea13d84 Convert test_pkg to use unittest.
Collin Winter <collinw@gmail.com>
parents: 24816
diff changeset
142
721c3ea13d84 Convert test_pkg to use unittest.
Collin Winter <collinw@gmail.com>
parents: 24816
diff changeset
143 def test_3(self):
721c3ea13d84 Convert test_pkg to use unittest.
Collin Winter <collinw@gmail.com>
parents: 24816
diff changeset
144 hier = [
721c3ea13d84 Convert test_pkg to use unittest.
Collin Winter <collinw@gmail.com>
parents: 24816
diff changeset
145 ("t3", None),
721c3ea13d84 Convert test_pkg to use unittest.
Collin Winter <collinw@gmail.com>
parents: 24816
diff changeset
146 ("t3 __init__"+os.extsep+"py", ""),
721c3ea13d84 Convert test_pkg to use unittest.
Collin Winter <collinw@gmail.com>
parents: 24816
diff changeset
147 ("t3 sub", None),
721c3ea13d84 Convert test_pkg to use unittest.
Collin Winter <collinw@gmail.com>
parents: 24816
diff changeset
148 ("t3 sub __init__"+os.extsep+"py", ""),
721c3ea13d84 Convert test_pkg to use unittest.
Collin Winter <collinw@gmail.com>
parents: 24816
diff changeset
149 ("t3 sub subsub", None),
721c3ea13d84 Convert test_pkg to use unittest.
Collin Winter <collinw@gmail.com>
parents: 24816
diff changeset
150 ("t3 sub subsub __init__"+os.extsep+"py", "spam = 1"),
721c3ea13d84 Convert test_pkg to use unittest.
Collin Winter <collinw@gmail.com>
parents: 24816
diff changeset
151 ]
721c3ea13d84 Convert test_pkg to use unittest.
Collin Winter <collinw@gmail.com>
parents: 24816
diff changeset
152 self.mkhier(hier)
721c3ea13d84 Convert test_pkg to use unittest.
Collin Winter <collinw@gmail.com>
parents: 24816
diff changeset
153
721c3ea13d84 Convert test_pkg to use unittest.
Collin Winter <collinw@gmail.com>
parents: 24816
diff changeset
154 import t3.sub.subsub
721c3ea13d84 Convert test_pkg to use unittest.
Collin Winter <collinw@gmail.com>
parents: 24816
diff changeset
155 self.assertEqual(t3.__name__, "t3")
721c3ea13d84 Convert test_pkg to use unittest.
Collin Winter <collinw@gmail.com>
parents: 24816
diff changeset
156 self.assertEqual(t3.sub.__name__, "t3.sub")
721c3ea13d84 Convert test_pkg to use unittest.
Collin Winter <collinw@gmail.com>
parents: 24816
diff changeset
157 self.assertEqual(t3.sub.subsub.__name__, "t3.sub.subsub")
6247
1496444e8c8b Test set for package import.
Guido van Rossum <guido@python.org>
parents:
diff changeset
158
42789
721c3ea13d84 Convert test_pkg to use unittest.
Collin Winter <collinw@gmail.com>
parents: 24816
diff changeset
159 def test_4(self):
721c3ea13d84 Convert test_pkg to use unittest.
Collin Winter <collinw@gmail.com>
parents: 24816
diff changeset
160 hier = [
721c3ea13d84 Convert test_pkg to use unittest.
Collin Winter <collinw@gmail.com>
parents: 24816
diff changeset
161 ("t4.py", "raise RuntimeError('Shouldnt load t4.py')"),
721c3ea13d84 Convert test_pkg to use unittest.
Collin Winter <collinw@gmail.com>
parents: 24816
diff changeset
162 ("t4", None),
721c3ea13d84 Convert test_pkg to use unittest.
Collin Winter <collinw@gmail.com>
parents: 24816
diff changeset
163 ("t4 __init__"+os.extsep+"py", ""),
721c3ea13d84 Convert test_pkg to use unittest.
Collin Winter <collinw@gmail.com>
parents: 24816
diff changeset
164 ("t4 sub.py", "raise RuntimeError('Shouldnt load sub.py')"),
721c3ea13d84 Convert test_pkg to use unittest.
Collin Winter <collinw@gmail.com>
parents: 24816
diff changeset
165 ("t4 sub", None),
721c3ea13d84 Convert test_pkg to use unittest.
Collin Winter <collinw@gmail.com>
parents: 24816
diff changeset
166 ("t4 sub __init__"+os.extsep+"py", ""),
721c3ea13d84 Convert test_pkg to use unittest.
Collin Winter <collinw@gmail.com>
parents: 24816
diff changeset
167 ("t4 sub subsub"+os.extsep+"py",
721c3ea13d84 Convert test_pkg to use unittest.
Collin Winter <collinw@gmail.com>
parents: 24816
diff changeset
168 "raise RuntimeError('Shouldnt load subsub.py')"),
721c3ea13d84 Convert test_pkg to use unittest.
Collin Winter <collinw@gmail.com>
parents: 24816
diff changeset
169 ("t4 sub subsub", None),
721c3ea13d84 Convert test_pkg to use unittest.
Collin Winter <collinw@gmail.com>
parents: 24816
diff changeset
170 ("t4 sub subsub __init__"+os.extsep+"py", "spam = 1"),
721c3ea13d84 Convert test_pkg to use unittest.
Collin Winter <collinw@gmail.com>
parents: 24816
diff changeset
171 ]
721c3ea13d84 Convert test_pkg to use unittest.
Collin Winter <collinw@gmail.com>
parents: 24816
diff changeset
172 self.mkhier(hier)
721c3ea13d84 Convert test_pkg to use unittest.
Collin Winter <collinw@gmail.com>
parents: 24816
diff changeset
173
721c3ea13d84 Convert test_pkg to use unittest.
Collin Winter <collinw@gmail.com>
parents: 24816
diff changeset
174 s = """
721c3ea13d84 Convert test_pkg to use unittest.
Collin Winter <collinw@gmail.com>
parents: 24816
diff changeset
175 from t4.sub.subsub import *
721c3ea13d84 Convert test_pkg to use unittest.
Collin Winter <collinw@gmail.com>
parents: 24816
diff changeset
176 self.assertEqual(spam, 1)
721c3ea13d84 Convert test_pkg to use unittest.
Collin Winter <collinw@gmail.com>
parents: 24816
diff changeset
177 """
721c3ea13d84 Convert test_pkg to use unittest.
Collin Winter <collinw@gmail.com>
parents: 24816
diff changeset
178 self.run_code(s)
6247
1496444e8c8b Test set for package import.
Guido van Rossum <guido@python.org>
parents:
diff changeset
179
42789
721c3ea13d84 Convert test_pkg to use unittest.
Collin Winter <collinw@gmail.com>
parents: 24816
diff changeset
180 def test_5(self):
721c3ea13d84 Convert test_pkg to use unittest.
Collin Winter <collinw@gmail.com>
parents: 24816
diff changeset
181 hier = [
721c3ea13d84 Convert test_pkg to use unittest.
Collin Winter <collinw@gmail.com>
parents: 24816
diff changeset
182 ("t5", None),
721c3ea13d84 Convert test_pkg to use unittest.
Collin Winter <collinw@gmail.com>
parents: 24816
diff changeset
183 ("t5 __init__"+os.extsep+"py", "import t5.foo"),
721c3ea13d84 Convert test_pkg to use unittest.
Collin Winter <collinw@gmail.com>
parents: 24816
diff changeset
184 ("t5 string"+os.extsep+"py", "spam = 1"),
721c3ea13d84 Convert test_pkg to use unittest.
Collin Winter <collinw@gmail.com>
parents: 24816
diff changeset
185 ("t5 foo"+os.extsep+"py",
721c3ea13d84 Convert test_pkg to use unittest.
Collin Winter <collinw@gmail.com>
parents: 24816
diff changeset
186 "from . import string; assert string.spam == 1"),
721c3ea13d84 Convert test_pkg to use unittest.
Collin Winter <collinw@gmail.com>
parents: 24816
diff changeset
187 ]
721c3ea13d84 Convert test_pkg to use unittest.
Collin Winter <collinw@gmail.com>
parents: 24816
diff changeset
188 self.mkhier(hier)
721c3ea13d84 Convert test_pkg to use unittest.
Collin Winter <collinw@gmail.com>
parents: 24816
diff changeset
189
721c3ea13d84 Convert test_pkg to use unittest.
Collin Winter <collinw@gmail.com>
parents: 24816
diff changeset
190 import t5
721c3ea13d84 Convert test_pkg to use unittest.
Collin Winter <collinw@gmail.com>
parents: 24816
diff changeset
191 s = """
721c3ea13d84 Convert test_pkg to use unittest.
Collin Winter <collinw@gmail.com>
parents: 24816
diff changeset
192 from t5 import *
721c3ea13d84 Convert test_pkg to use unittest.
Collin Winter <collinw@gmail.com>
parents: 24816
diff changeset
193 self.assertEqual(dir(), ['foo', 'self', 'string', 't5'])
721c3ea13d84 Convert test_pkg to use unittest.
Collin Winter <collinw@gmail.com>
parents: 24816
diff changeset
194 """
721c3ea13d84 Convert test_pkg to use unittest.
Collin Winter <collinw@gmail.com>
parents: 24816
diff changeset
195 self.run_code(s)
721c3ea13d84 Convert test_pkg to use unittest.
Collin Winter <collinw@gmail.com>
parents: 24816
diff changeset
196
721c3ea13d84 Convert test_pkg to use unittest.
Collin Winter <collinw@gmail.com>
parents: 24816
diff changeset
197 import t5
721c3ea13d84 Convert test_pkg to use unittest.
Collin Winter <collinw@gmail.com>
parents: 24816
diff changeset
198 self.assertEqual(fixdir(dir(t5)),
721c3ea13d84 Convert test_pkg to use unittest.
Collin Winter <collinw@gmail.com>
parents: 24816
diff changeset
199 ['__doc__', '__file__', '__name__',
44116
803edf9b1dd9 Implement PEP 366
Nick Coghlan <ncoghlan@gmail.com>
parents: 42789
diff changeset
200 '__package__', '__path__', 'foo', 'string', 't5'])
42789
721c3ea13d84 Convert test_pkg to use unittest.
Collin Winter <collinw@gmail.com>
parents: 24816
diff changeset
201 self.assertEqual(fixdir(dir(t5.foo)),
44116
803edf9b1dd9 Implement PEP 366
Nick Coghlan <ncoghlan@gmail.com>
parents: 42789
diff changeset
202 ['__doc__', '__file__', '__name__', '__package__',
803edf9b1dd9 Implement PEP 366
Nick Coghlan <ncoghlan@gmail.com>
parents: 42789
diff changeset
203 'string'])
42789
721c3ea13d84 Convert test_pkg to use unittest.
Collin Winter <collinw@gmail.com>
parents: 24816
diff changeset
204 self.assertEqual(fixdir(dir(t5.string)),
44116
803edf9b1dd9 Implement PEP 366
Nick Coghlan <ncoghlan@gmail.com>
parents: 42789
diff changeset
205 ['__doc__', '__file__', '__name__','__package__',
803edf9b1dd9 Implement PEP 366
Nick Coghlan <ncoghlan@gmail.com>
parents: 42789
diff changeset
206 'spam'])
42789
721c3ea13d84 Convert test_pkg to use unittest.
Collin Winter <collinw@gmail.com>
parents: 24816
diff changeset
207
721c3ea13d84 Convert test_pkg to use unittest.
Collin Winter <collinw@gmail.com>
parents: 24816
diff changeset
208 def test_6(self):
721c3ea13d84 Convert test_pkg to use unittest.
Collin Winter <collinw@gmail.com>
parents: 24816
diff changeset
209 hier = [
721c3ea13d84 Convert test_pkg to use unittest.
Collin Winter <collinw@gmail.com>
parents: 24816
diff changeset
210 ("t6", None),
721c3ea13d84 Convert test_pkg to use unittest.
Collin Winter <collinw@gmail.com>
parents: 24816
diff changeset
211 ("t6 __init__"+os.extsep+"py",
721c3ea13d84 Convert test_pkg to use unittest.
Collin Winter <collinw@gmail.com>
parents: 24816
diff changeset
212 "__all__ = ['spam', 'ham', 'eggs']"),
721c3ea13d84 Convert test_pkg to use unittest.
Collin Winter <collinw@gmail.com>
parents: 24816
diff changeset
213 ("t6 spam"+os.extsep+"py", ""),
721c3ea13d84 Convert test_pkg to use unittest.
Collin Winter <collinw@gmail.com>
parents: 24816
diff changeset
214 ("t6 ham"+os.extsep+"py", ""),
721c3ea13d84 Convert test_pkg to use unittest.
Collin Winter <collinw@gmail.com>
parents: 24816
diff changeset
215 ("t6 eggs"+os.extsep+"py", ""),
721c3ea13d84 Convert test_pkg to use unittest.
Collin Winter <collinw@gmail.com>
parents: 24816
diff changeset
216 ]
721c3ea13d84 Convert test_pkg to use unittest.
Collin Winter <collinw@gmail.com>
parents: 24816
diff changeset
217 self.mkhier(hier)
15305
07f24261c6e6 Make reindent.py happy (convert everything to 4-space indents!).
Fred Drake <fdrake@acm.org>
parents: 14342
diff changeset
218
42789
721c3ea13d84 Convert test_pkg to use unittest.
Collin Winter <collinw@gmail.com>
parents: 24816
diff changeset
219 import t6
721c3ea13d84 Convert test_pkg to use unittest.
Collin Winter <collinw@gmail.com>
parents: 24816
diff changeset
220 self.assertEqual(fixdir(dir(t6)),
721c3ea13d84 Convert test_pkg to use unittest.
Collin Winter <collinw@gmail.com>
parents: 24816
diff changeset
221 ['__all__', '__doc__', '__file__',
44116
803edf9b1dd9 Implement PEP 366
Nick Coghlan <ncoghlan@gmail.com>
parents: 42789
diff changeset
222 '__name__', '__package__', '__path__'])
42789
721c3ea13d84 Convert test_pkg to use unittest.
Collin Winter <collinw@gmail.com>
parents: 24816
diff changeset
223 s = """
721c3ea13d84 Convert test_pkg to use unittest.
Collin Winter <collinw@gmail.com>
parents: 24816
diff changeset
224 import t6
721c3ea13d84 Convert test_pkg to use unittest.
Collin Winter <collinw@gmail.com>
parents: 24816
diff changeset
225 from t6 import *
721c3ea13d84 Convert test_pkg to use unittest.
Collin Winter <collinw@gmail.com>
parents: 24816
diff changeset
226 self.assertEqual(fixdir(dir(t6)),
721c3ea13d84 Convert test_pkg to use unittest.
Collin Winter <collinw@gmail.com>
parents: 24816
diff changeset
227 ['__all__', '__doc__', '__file__',
44116
803edf9b1dd9 Implement PEP 366
Nick Coghlan <ncoghlan@gmail.com>
parents: 42789
diff changeset
228 '__name__', '__package__', '__path__',
803edf9b1dd9 Implement PEP 366
Nick Coghlan <ncoghlan@gmail.com>
parents: 42789
diff changeset
229 'eggs', 'ham', 'spam'])
42789
721c3ea13d84 Convert test_pkg to use unittest.
Collin Winter <collinw@gmail.com>
parents: 24816
diff changeset
230 self.assertEqual(dir(), ['eggs', 'ham', 'self', 'spam', 't6'])
721c3ea13d84 Convert test_pkg to use unittest.
Collin Winter <collinw@gmail.com>
parents: 24816
diff changeset
231 """
721c3ea13d84 Convert test_pkg to use unittest.
Collin Winter <collinw@gmail.com>
parents: 24816
diff changeset
232 self.run_code(s)
6284
daa62c2264b5 Added test for __all__.
Guido van Rossum <guido@python.org>
parents: 6255
diff changeset
233
42789
721c3ea13d84 Convert test_pkg to use unittest.
Collin Winter <collinw@gmail.com>
parents: 24816
diff changeset
234 def test_7(self):
721c3ea13d84 Convert test_pkg to use unittest.
Collin Winter <collinw@gmail.com>
parents: 24816
diff changeset
235 hier = [
46474
5c43159f5ad0 Some tests did not pass on repeated calls (regrtest -R::)
Amaury Forgeot d'Arc <amauryfa@gmail.com>
parents: 45468
diff changeset
236 ("t7", None),
42789
721c3ea13d84 Convert test_pkg to use unittest.
Collin Winter <collinw@gmail.com>
parents: 24816
diff changeset
237 ("t7"+os.extsep+"py", ""),
721c3ea13d84 Convert test_pkg to use unittest.
Collin Winter <collinw@gmail.com>
parents: 24816
diff changeset
238 ("t7 __init__"+os.extsep+"py", ""),
721c3ea13d84 Convert test_pkg to use unittest.
Collin Winter <collinw@gmail.com>
parents: 24816
diff changeset
239 ("t7 sub"+os.extsep+"py",
721c3ea13d84 Convert test_pkg to use unittest.
Collin Winter <collinw@gmail.com>
parents: 24816
diff changeset
240 "raise RuntimeError('Shouldnt load sub.py')"),
721c3ea13d84 Convert test_pkg to use unittest.
Collin Winter <collinw@gmail.com>
parents: 24816
diff changeset
241 ("t7 sub", None),
721c3ea13d84 Convert test_pkg to use unittest.
Collin Winter <collinw@gmail.com>
parents: 24816
diff changeset
242 ("t7 sub __init__"+os.extsep+"py", ""),
721c3ea13d84 Convert test_pkg to use unittest.
Collin Winter <collinw@gmail.com>
parents: 24816
diff changeset
243 ("t7 sub "+os.extsep+"py",
721c3ea13d84 Convert test_pkg to use unittest.
Collin Winter <collinw@gmail.com>
parents: 24816
diff changeset
244 "raise RuntimeError('Shouldnt load subsub.py')"),
721c3ea13d84 Convert test_pkg to use unittest.
Collin Winter <collinw@gmail.com>
parents: 24816
diff changeset
245 ("t7 sub subsub", None),
721c3ea13d84 Convert test_pkg to use unittest.
Collin Winter <collinw@gmail.com>
parents: 24816
diff changeset
246 ("t7 sub subsub __init__"+os.extsep+"py",
721c3ea13d84 Convert test_pkg to use unittest.
Collin Winter <collinw@gmail.com>
parents: 24816
diff changeset
247 "spam = 1"),
721c3ea13d84 Convert test_pkg to use unittest.
Collin Winter <collinw@gmail.com>
parents: 24816
diff changeset
248 ]
721c3ea13d84 Convert test_pkg to use unittest.
Collin Winter <collinw@gmail.com>
parents: 24816
diff changeset
249 self.mkhier(hier)
6247
1496444e8c8b Test set for package import.
Guido van Rossum <guido@python.org>
parents:
diff changeset
250
1496444e8c8b Test set for package import.
Guido van Rossum <guido@python.org>
parents:
diff changeset
251
42789
721c3ea13d84 Convert test_pkg to use unittest.
Collin Winter <collinw@gmail.com>
parents: 24816
diff changeset
252 t7, sub, subsub = None, None, None
721c3ea13d84 Convert test_pkg to use unittest.
Collin Winter <collinw@gmail.com>
parents: 24816
diff changeset
253 import t7 as tas
721c3ea13d84 Convert test_pkg to use unittest.
Collin Winter <collinw@gmail.com>
parents: 24816
diff changeset
254 self.assertEqual(fixdir(dir(tas)),
44116
803edf9b1dd9 Implement PEP 366
Nick Coghlan <ncoghlan@gmail.com>
parents: 42789
diff changeset
255 ['__doc__', '__file__', '__name__',
803edf9b1dd9 Implement PEP 366
Nick Coghlan <ncoghlan@gmail.com>
parents: 42789
diff changeset
256 '__package__', '__path__'])
42789
721c3ea13d84 Convert test_pkg to use unittest.
Collin Winter <collinw@gmail.com>
parents: 24816
diff changeset
257 self.failIf(t7)
721c3ea13d84 Convert test_pkg to use unittest.
Collin Winter <collinw@gmail.com>
parents: 24816
diff changeset
258 from t7 import sub as subpar
721c3ea13d84 Convert test_pkg to use unittest.
Collin Winter <collinw@gmail.com>
parents: 24816
diff changeset
259 self.assertEqual(fixdir(dir(subpar)),
44116
803edf9b1dd9 Implement PEP 366
Nick Coghlan <ncoghlan@gmail.com>
parents: 42789
diff changeset
260 ['__doc__', '__file__', '__name__',
803edf9b1dd9 Implement PEP 366
Nick Coghlan <ncoghlan@gmail.com>
parents: 42789
diff changeset
261 '__package__', '__path__'])
42789
721c3ea13d84 Convert test_pkg to use unittest.
Collin Winter <collinw@gmail.com>
parents: 24816
diff changeset
262 self.failIf(t7)
721c3ea13d84 Convert test_pkg to use unittest.
Collin Winter <collinw@gmail.com>
parents: 24816
diff changeset
263 self.failIf(sub)
721c3ea13d84 Convert test_pkg to use unittest.
Collin Winter <collinw@gmail.com>
parents: 24816
diff changeset
264 from t7.sub import subsub as subsubsub
721c3ea13d84 Convert test_pkg to use unittest.
Collin Winter <collinw@gmail.com>
parents: 24816
diff changeset
265 self.assertEqual(fixdir(dir(subsubsub)),
44116
803edf9b1dd9 Implement PEP 366
Nick Coghlan <ncoghlan@gmail.com>
parents: 42789
diff changeset
266 ['__doc__', '__file__', '__name__',
803edf9b1dd9 Implement PEP 366
Nick Coghlan <ncoghlan@gmail.com>
parents: 42789
diff changeset
267 '__package__', '__path__', 'spam'])
42789
721c3ea13d84 Convert test_pkg to use unittest.
Collin Winter <collinw@gmail.com>
parents: 24816
diff changeset
268 self.failIf(t7)
721c3ea13d84 Convert test_pkg to use unittest.
Collin Winter <collinw@gmail.com>
parents: 24816
diff changeset
269 self.failIf(sub)
721c3ea13d84 Convert test_pkg to use unittest.
Collin Winter <collinw@gmail.com>
parents: 24816
diff changeset
270 self.failIf(subsub)
721c3ea13d84 Convert test_pkg to use unittest.
Collin Winter <collinw@gmail.com>
parents: 24816
diff changeset
271 from t7.sub.subsub import spam as ham
721c3ea13d84 Convert test_pkg to use unittest.
Collin Winter <collinw@gmail.com>
parents: 24816
diff changeset
272 self.assertEqual(ham, 1)
721c3ea13d84 Convert test_pkg to use unittest.
Collin Winter <collinw@gmail.com>
parents: 24816
diff changeset
273 self.failIf(t7)
721c3ea13d84 Convert test_pkg to use unittest.
Collin Winter <collinw@gmail.com>
parents: 24816
diff changeset
274 self.failIf(sub)
721c3ea13d84 Convert test_pkg to use unittest.
Collin Winter <collinw@gmail.com>
parents: 24816
diff changeset
275 self.failIf(subsub)
6255
f446e08ac302 Fix details in the test:
Guido van Rossum <guido@python.org>
parents: 6251
diff changeset
276
42789
721c3ea13d84 Convert test_pkg to use unittest.
Collin Winter <collinw@gmail.com>
parents: 24816
diff changeset
277
721c3ea13d84 Convert test_pkg to use unittest.
Collin Winter <collinw@gmail.com>
parents: 24816
diff changeset
278 def test_main():
721c3ea13d84 Convert test_pkg to use unittest.
Collin Winter <collinw@gmail.com>
parents: 24816
diff changeset
279 test_support.run_unittest(__name__)
8375
242b3c9c5bec Test that "import sys.imp" fails as it should.
Guido van Rossum <guido@python.org>
parents: 7747
diff changeset
280
42789
721c3ea13d84 Convert test_pkg to use unittest.
Collin Winter <collinw@gmail.com>
parents: 24816
diff changeset
281
721c3ea13d84 Convert test_pkg to use unittest.
Collin Winter <collinw@gmail.com>
parents: 24816
diff changeset
282 if __name__ == "__main__":
721c3ea13d84 Convert test_pkg to use unittest.
Collin Winter <collinw@gmail.com>
parents: 24816
diff changeset
283 test_main()