ยปCore Development>Code coverage>Lib/test/test_pkg.py

Python code coverage for Lib/test/test_pkg.py

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