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

Python code coverage for Lib/test/test_modulefinder.py

#countcontent
1n/aimport os
2n/aimport errno
3n/aimport importlib.machinery
4n/aimport py_compile
5n/aimport shutil
6n/aimport unittest
7n/aimport tempfile
8n/a
9n/afrom test import support
10n/a
11n/aimport modulefinder
12n/a
13n/aTEST_DIR = tempfile.mkdtemp()
14n/aTEST_PATH = [TEST_DIR, os.path.dirname(tempfile.__file__)]
15n/a
16n/a# Each test description is a list of 5 items:
17n/a#
18n/a# 1. a module name that will be imported by modulefinder
19n/a# 2. a list of module names that modulefinder is required to find
20n/a# 3. a list of module names that modulefinder should complain
21n/a# about because they are not found
22n/a# 4. a list of module names that modulefinder should complain
23n/a# about because they MAY be not found
24n/a# 5. a string specifying packages to create; the format is obvious imo.
25n/a#
26n/a# Each package will be created in TEST_DIR, and TEST_DIR will be
27n/a# removed after the tests again.
28n/a# Modulefinder searches in a path that contains TEST_DIR, plus
29n/a# the standard Lib directory.
30n/a
31n/amaybe_test = [
32n/a "a.module",
33n/a ["a", "a.module", "sys",
34n/a "b"],
35n/a ["c"], ["b.something"],
36n/a """\
37n/aa/__init__.py
38n/aa/module.py
39n/a from b import something
40n/a from c import something
41n/ab/__init__.py
42n/a from sys import *
43n/a"""]
44n/a
45n/amaybe_test_new = [
46n/a "a.module",
47n/a ["a", "a.module", "sys",
48n/a "b", "__future__"],
49n/a ["c"], ["b.something"],
50n/a """\
51n/aa/__init__.py
52n/aa/module.py
53n/a from b import something
54n/a from c import something
55n/ab/__init__.py
56n/a from __future__ import absolute_import
57n/a from sys import *
58n/a"""]
59n/a
60n/apackage_test = [
61n/a "a.module",
62n/a ["a", "a.b", "a.c", "a.module", "mymodule", "sys"],
63n/a ["blahblah", "c"], [],
64n/a """\
65n/amymodule.py
66n/aa/__init__.py
67n/a import blahblah
68n/a from a import b
69n/a import c
70n/aa/module.py
71n/a import sys
72n/a from a import b as x
73n/a from a.c import sillyname
74n/aa/b.py
75n/aa/c.py
76n/a from a.module import x
77n/a import mymodule as sillyname
78n/a from sys import version_info
79n/a"""]
80n/a
81n/aabsolute_import_test = [
82n/a "a.module",
83n/a ["a", "a.module",
84n/a "b", "b.x", "b.y", "b.z",
85n/a "__future__", "sys", "gc"],
86n/a ["blahblah", "z"], [],
87n/a """\
88n/amymodule.py
89n/aa/__init__.py
90n/aa/module.py
91n/a from __future__ import absolute_import
92n/a import sys # sys
93n/a import blahblah # fails
94n/a import gc # gc
95n/a import b.x # b.x
96n/a from b import y # b.y
97n/a from b.z import * # b.z.*
98n/aa/gc.py
99n/aa/sys.py
100n/a import mymodule
101n/aa/b/__init__.py
102n/aa/b/x.py
103n/aa/b/y.py
104n/aa/b/z.py
105n/ab/__init__.py
106n/a import z
107n/ab/unused.py
108n/ab/x.py
109n/ab/y.py
110n/ab/z.py
111n/a"""]
112n/a
113n/arelative_import_test = [
114n/a "a.module",
115n/a ["__future__",
116n/a "a", "a.module",
117n/a "a.b", "a.b.y", "a.b.z",
118n/a "a.b.c", "a.b.c.moduleC",
119n/a "a.b.c.d", "a.b.c.e",
120n/a "a.b.x",
121n/a "gc"],
122n/a [], [],
123n/a """\
124n/amymodule.py
125n/aa/__init__.py
126n/a from .b import y, z # a.b.y, a.b.z
127n/aa/module.py
128n/a from __future__ import absolute_import # __future__
129n/a import gc # gc
130n/aa/gc.py
131n/aa/sys.py
132n/aa/b/__init__.py
133n/a from ..b import x # a.b.x
134n/a #from a.b.c import moduleC
135n/a from .c import moduleC # a.b.moduleC
136n/aa/b/x.py
137n/aa/b/y.py
138n/aa/b/z.py
139n/aa/b/g.py
140n/aa/b/c/__init__.py
141n/a from ..c import e # a.b.c.e
142n/aa/b/c/moduleC.py
143n/a from ..c import d # a.b.c.d
144n/aa/b/c/d.py
145n/aa/b/c/e.py
146n/aa/b/c/x.py
147n/a"""]
148n/a
149n/arelative_import_test_2 = [
150n/a "a.module",
151n/a ["a", "a.module",
152n/a "a.sys",
153n/a "a.b", "a.b.y", "a.b.z",
154n/a "a.b.c", "a.b.c.d",
155n/a "a.b.c.e",
156n/a "a.b.c.moduleC",
157n/a "a.b.c.f",
158n/a "a.b.x",
159n/a "a.another"],
160n/a [], [],
161n/a """\
162n/amymodule.py
163n/aa/__init__.py
164n/a from . import sys # a.sys
165n/aa/another.py
166n/aa/module.py
167n/a from .b import y, z # a.b.y, a.b.z
168n/aa/gc.py
169n/aa/sys.py
170n/aa/b/__init__.py
171n/a from .c import moduleC # a.b.c.moduleC
172n/a from .c import d # a.b.c.d
173n/aa/b/x.py
174n/aa/b/y.py
175n/aa/b/z.py
176n/aa/b/c/__init__.py
177n/a from . import e # a.b.c.e
178n/aa/b/c/moduleC.py
179n/a #
180n/a from . import f # a.b.c.f
181n/a from .. import x # a.b.x
182n/a from ... import another # a.another
183n/aa/b/c/d.py
184n/aa/b/c/e.py
185n/aa/b/c/f.py
186n/a"""]
187n/a
188n/arelative_import_test_3 = [
189n/a "a.module",
190n/a ["a", "a.module"],
191n/a ["a.bar"],
192n/a [],
193n/a """\
194n/aa/__init__.py
195n/a def foo(): pass
196n/aa/module.py
197n/a from . import foo
198n/a from . import bar
199n/a"""]
200n/a
201n/arelative_import_test_4 = [
202n/a "a.module",
203n/a ["a", "a.module"],
204n/a [],
205n/a [],
206n/a """\
207n/aa/__init__.py
208n/a def foo(): pass
209n/aa/module.py
210n/a from . import *
211n/a"""]
212n/a
213n/abytecode_test = [
214n/a "a",
215n/a ["a"],
216n/a [],
217n/a [],
218n/a ""
219n/a]
220n/a
221n/a
222n/adef open_file(path):
223n/a dirname = os.path.dirname(path)
224n/a try:
225n/a os.makedirs(dirname)
226n/a except OSError as e:
227n/a if e.errno != errno.EEXIST:
228n/a raise
229n/a return open(path, "w")
230n/a
231n/a
232n/adef create_package(source):
233n/a ofi = None
234n/a try:
235n/a for line in source.splitlines():
236n/a if line.startswith(" ") or line.startswith("\t"):
237n/a ofi.write(line.strip() + "\n")
238n/a else:
239n/a if ofi:
240n/a ofi.close()
241n/a ofi = open_file(os.path.join(TEST_DIR, line.strip()))
242n/a finally:
243n/a if ofi:
244n/a ofi.close()
245n/a
246n/a
247n/aclass ModuleFinderTest(unittest.TestCase):
248n/a def _do_test(self, info, report=False, debug=0, replace_paths=[]):
249n/a import_this, modules, missing, maybe_missing, source = info
250n/a create_package(source)
251n/a try:
252n/a mf = modulefinder.ModuleFinder(path=TEST_PATH, debug=debug,
253n/a replace_paths=replace_paths)
254n/a mf.import_hook(import_this)
255n/a if report:
256n/a mf.report()
257n/a## # This wouldn't work in general when executed several times:
258n/a## opath = sys.path[:]
259n/a## sys.path = TEST_PATH
260n/a## try:
261n/a## __import__(import_this)
262n/a## except:
263n/a## import traceback; traceback.print_exc()
264n/a## sys.path = opath
265n/a## return
266n/a modules = sorted(set(modules))
267n/a found = sorted(mf.modules)
268n/a # check if we found what we expected, not more, not less
269n/a self.assertEqual(found, modules)
270n/a
271n/a # check for missing and maybe missing modules
272n/a bad, maybe = mf.any_missing_maybe()
273n/a self.assertEqual(bad, missing)
274n/a self.assertEqual(maybe, maybe_missing)
275n/a finally:
276n/a shutil.rmtree(TEST_DIR)
277n/a
278n/a def test_package(self):
279n/a self._do_test(package_test)
280n/a
281n/a def test_maybe(self):
282n/a self._do_test(maybe_test)
283n/a
284n/a def test_maybe_new(self):
285n/a self._do_test(maybe_test_new)
286n/a
287n/a def test_absolute_imports(self):
288n/a self._do_test(absolute_import_test)
289n/a
290n/a def test_relative_imports(self):
291n/a self._do_test(relative_import_test)
292n/a
293n/a def test_relative_imports_2(self):
294n/a self._do_test(relative_import_test_2)
295n/a
296n/a def test_relative_imports_3(self):
297n/a self._do_test(relative_import_test_3)
298n/a
299n/a def test_relative_imports_4(self):
300n/a self._do_test(relative_import_test_4)
301n/a
302n/a def test_bytecode(self):
303n/a base_path = os.path.join(TEST_DIR, 'a')
304n/a source_path = base_path + importlib.machinery.SOURCE_SUFFIXES[0]
305n/a bytecode_path = base_path + importlib.machinery.BYTECODE_SUFFIXES[0]
306n/a with open_file(source_path) as file:
307n/a file.write('testing_modulefinder = True\n')
308n/a py_compile.compile(source_path, cfile=bytecode_path)
309n/a os.remove(source_path)
310n/a self._do_test(bytecode_test)
311n/a
312n/a def test_replace_paths(self):
313n/a old_path = os.path.join(TEST_DIR, 'a', 'module.py')
314n/a new_path = os.path.join(TEST_DIR, 'a', 'spam.py')
315n/a with support.captured_stdout() as output:
316n/a self._do_test(maybe_test, debug=2,
317n/a replace_paths=[(old_path, new_path)])
318n/a output = output.getvalue()
319n/a expected = "co_filename %r changed to %r" % (old_path, new_path)
320n/a self.assertIn(expected, output)
321n/a
322n/a def test_extended_opargs(self):
323n/a extended_opargs_test = [
324n/a "a",
325n/a ["a", "b"],
326n/a [], [],
327n/a """\
328n/aa.py
329n/a %r
330n/a import b
331n/ab.py
332n/a""" % list(range(2**16))] # 2**16 constants
333n/a self._do_test(extended_opargs_test)
334n/a
335n/a
336n/aif __name__ == "__main__":
337n/a unittest.main()