ยปCore Development>Code coverage>Lib/distutils/tests/test_build_py.py

Python code coverage for Lib/distutils/tests/test_build_py.py

#countcontent
1n/a"""Tests for distutils.command.build_py."""
2n/a
3n/aimport os
4n/aimport sys
5n/aimport unittest
6n/a
7n/afrom distutils.command.build_py import build_py
8n/afrom distutils.core import Distribution
9n/afrom distutils.errors import DistutilsFileError
10n/a
11n/afrom distutils.tests import support
12n/afrom test.support import run_unittest
13n/a
14n/a
15n/aclass BuildPyTestCase(support.TempdirManager,
16n/a support.LoggingSilencer,
17n/a unittest.TestCase):
18n/a
19n/a def test_package_data(self):
20n/a sources = self.mkdtemp()
21n/a f = open(os.path.join(sources, "__init__.py"), "w")
22n/a try:
23n/a f.write("# Pretend this is a package.")
24n/a finally:
25n/a f.close()
26n/a f = open(os.path.join(sources, "README.txt"), "w")
27n/a try:
28n/a f.write("Info about this package")
29n/a finally:
30n/a f.close()
31n/a
32n/a destination = self.mkdtemp()
33n/a
34n/a dist = Distribution({"packages": ["pkg"],
35n/a "package_dir": {"pkg": sources}})
36n/a # script_name need not exist, it just need to be initialized
37n/a dist.script_name = os.path.join(sources, "setup.py")
38n/a dist.command_obj["build"] = support.DummyCommand(
39n/a force=0,
40n/a build_lib=destination)
41n/a dist.packages = ["pkg"]
42n/a dist.package_data = {"pkg": ["README.txt"]}
43n/a dist.package_dir = {"pkg": sources}
44n/a
45n/a cmd = build_py(dist)
46n/a cmd.compile = 1
47n/a cmd.ensure_finalized()
48n/a self.assertEqual(cmd.package_data, dist.package_data)
49n/a
50n/a cmd.run()
51n/a
52n/a # This makes sure the list of outputs includes byte-compiled
53n/a # files for Python modules but not for package data files
54n/a # (there shouldn't *be* byte-code files for those!).
55n/a self.assertEqual(len(cmd.get_outputs()), 3)
56n/a pkgdest = os.path.join(destination, "pkg")
57n/a files = os.listdir(pkgdest)
58n/a pycache_dir = os.path.join(pkgdest, "__pycache__")
59n/a self.assertIn("__init__.py", files)
60n/a self.assertIn("README.txt", files)
61n/a if sys.dont_write_bytecode:
62n/a self.assertFalse(os.path.exists(pycache_dir))
63n/a else:
64n/a pyc_files = os.listdir(pycache_dir)
65n/a self.assertIn("__init__.%s.pyc" % sys.implementation.cache_tag,
66n/a pyc_files)
67n/a
68n/a def test_empty_package_dir(self):
69n/a # See bugs #1668596/#1720897
70n/a sources = self.mkdtemp()
71n/a open(os.path.join(sources, "__init__.py"), "w").close()
72n/a
73n/a testdir = os.path.join(sources, "doc")
74n/a os.mkdir(testdir)
75n/a open(os.path.join(testdir, "testfile"), "w").close()
76n/a
77n/a os.chdir(sources)
78n/a dist = Distribution({"packages": ["pkg"],
79n/a "package_dir": {"pkg": ""},
80n/a "package_data": {"pkg": ["doc/*"]}})
81n/a # script_name need not exist, it just need to be initialized
82n/a dist.script_name = os.path.join(sources, "setup.py")
83n/a dist.script_args = ["build"]
84n/a dist.parse_command_line()
85n/a
86n/a try:
87n/a dist.run_commands()
88n/a except DistutilsFileError:
89n/a self.fail("failed package_data test when package_dir is ''")
90n/a
91n/a @unittest.skipIf(sys.dont_write_bytecode, 'byte-compile disabled')
92n/a def test_byte_compile(self):
93n/a project_dir, dist = self.create_dist(py_modules=['boiledeggs'])
94n/a os.chdir(project_dir)
95n/a self.write_file('boiledeggs.py', 'import antigravity')
96n/a cmd = build_py(dist)
97n/a cmd.compile = 1
98n/a cmd.build_lib = 'here'
99n/a cmd.finalize_options()
100n/a cmd.run()
101n/a
102n/a found = os.listdir(cmd.build_lib)
103n/a self.assertEqual(sorted(found), ['__pycache__', 'boiledeggs.py'])
104n/a found = os.listdir(os.path.join(cmd.build_lib, '__pycache__'))
105n/a self.assertEqual(found,
106n/a ['boiledeggs.%s.pyc' % sys.implementation.cache_tag])
107n/a
108n/a @unittest.skipIf(sys.dont_write_bytecode, 'byte-compile disabled')
109n/a def test_byte_compile_optimized(self):
110n/a project_dir, dist = self.create_dist(py_modules=['boiledeggs'])
111n/a os.chdir(project_dir)
112n/a self.write_file('boiledeggs.py', 'import antigravity')
113n/a cmd = build_py(dist)
114n/a cmd.compile = 0
115n/a cmd.optimize = 1
116n/a cmd.build_lib = 'here'
117n/a cmd.finalize_options()
118n/a cmd.run()
119n/a
120n/a found = os.listdir(cmd.build_lib)
121n/a self.assertEqual(sorted(found), ['__pycache__', 'boiledeggs.py'])
122n/a found = os.listdir(os.path.join(cmd.build_lib, '__pycache__'))
123n/a expect = 'boiledeggs.{}.opt-1.pyc'.format(sys.implementation.cache_tag)
124n/a self.assertEqual(sorted(found), [expect])
125n/a
126n/a def test_dir_in_package_data(self):
127n/a """
128n/a A directory in package_data should not be added to the filelist.
129n/a """
130n/a # See bug 19286
131n/a sources = self.mkdtemp()
132n/a pkg_dir = os.path.join(sources, "pkg")
133n/a
134n/a os.mkdir(pkg_dir)
135n/a open(os.path.join(pkg_dir, "__init__.py"), "w").close()
136n/a
137n/a docdir = os.path.join(pkg_dir, "doc")
138n/a os.mkdir(docdir)
139n/a open(os.path.join(docdir, "testfile"), "w").close()
140n/a
141n/a # create the directory that could be incorrectly detected as a file
142n/a os.mkdir(os.path.join(docdir, 'otherdir'))
143n/a
144n/a os.chdir(sources)
145n/a dist = Distribution({"packages": ["pkg"],
146n/a "package_data": {"pkg": ["doc/*"]}})
147n/a # script_name need not exist, it just need to be initialized
148n/a dist.script_name = os.path.join(sources, "setup.py")
149n/a dist.script_args = ["build"]
150n/a dist.parse_command_line()
151n/a
152n/a try:
153n/a dist.run_commands()
154n/a except DistutilsFileError:
155n/a self.fail("failed package_data when data dir includes a dir")
156n/a
157n/a def test_dont_write_bytecode(self):
158n/a # makes sure byte_compile is not used
159n/a dist = self.create_dist()[1]
160n/a cmd = build_py(dist)
161n/a cmd.compile = 1
162n/a cmd.optimize = 1
163n/a
164n/a old_dont_write_bytecode = sys.dont_write_bytecode
165n/a sys.dont_write_bytecode = True
166n/a try:
167n/a cmd.byte_compile([])
168n/a finally:
169n/a sys.dont_write_bytecode = old_dont_write_bytecode
170n/a
171n/a self.assertIn('byte-compiling is disabled',
172n/a self.logs[0][1] % self.logs[0][2])
173n/a
174n/a
175n/adef test_suite():
176n/a return unittest.makeSuite(BuildPyTestCase)
177n/a
178n/aif __name__ == "__main__":
179n/a run_unittest(test_suite())