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

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

#countcontent
1n/a"""Tests for distutils.command.install_data."""
2n/aimport sys
3n/aimport os
4n/aimport importlib.util
5n/aimport unittest
6n/a
7n/afrom distutils.command.install_lib import install_lib
8n/afrom distutils.extension import Extension
9n/afrom distutils.tests import support
10n/afrom distutils.errors import DistutilsOptionError
11n/afrom test.support import run_unittest
12n/a
13n/a
14n/aclass InstallLibTestCase(support.TempdirManager,
15n/a support.LoggingSilencer,
16n/a support.EnvironGuard,
17n/a unittest.TestCase):
18n/a
19n/a def test_finalize_options(self):
20n/a dist = self.create_dist()[1]
21n/a cmd = install_lib(dist)
22n/a
23n/a cmd.finalize_options()
24n/a self.assertEqual(cmd.compile, 1)
25n/a self.assertEqual(cmd.optimize, 0)
26n/a
27n/a # optimize must be 0, 1, or 2
28n/a cmd.optimize = 'foo'
29n/a self.assertRaises(DistutilsOptionError, cmd.finalize_options)
30n/a cmd.optimize = '4'
31n/a self.assertRaises(DistutilsOptionError, cmd.finalize_options)
32n/a
33n/a cmd.optimize = '2'
34n/a cmd.finalize_options()
35n/a self.assertEqual(cmd.optimize, 2)
36n/a
37n/a @unittest.skipIf(sys.dont_write_bytecode, 'byte-compile disabled')
38n/a def test_byte_compile(self):
39n/a project_dir, dist = self.create_dist()
40n/a os.chdir(project_dir)
41n/a cmd = install_lib(dist)
42n/a cmd.compile = cmd.optimize = 1
43n/a
44n/a f = os.path.join(project_dir, 'foo.py')
45n/a self.write_file(f, '# python file')
46n/a cmd.byte_compile([f])
47n/a pyc_file = importlib.util.cache_from_source('foo.py', optimization='')
48n/a pyc_opt_file = importlib.util.cache_from_source('foo.py',
49n/a optimization=cmd.optimize)
50n/a self.assertTrue(os.path.exists(pyc_file))
51n/a self.assertTrue(os.path.exists(pyc_opt_file))
52n/a
53n/a def test_get_outputs(self):
54n/a project_dir, dist = self.create_dist()
55n/a os.chdir(project_dir)
56n/a os.mkdir('spam')
57n/a cmd = install_lib(dist)
58n/a
59n/a # setting up a dist environment
60n/a cmd.compile = cmd.optimize = 1
61n/a cmd.install_dir = self.mkdtemp()
62n/a f = os.path.join(project_dir, 'spam', '__init__.py')
63n/a self.write_file(f, '# python package')
64n/a cmd.distribution.ext_modules = [Extension('foo', ['xxx'])]
65n/a cmd.distribution.packages = ['spam']
66n/a cmd.distribution.script_name = 'setup.py'
67n/a
68n/a # get_outputs should return 4 elements: spam/__init__.py and .pyc,
69n/a # foo.import-tag-abiflags.so / foo.pyd
70n/a outputs = cmd.get_outputs()
71n/a self.assertEqual(len(outputs), 4, outputs)
72n/a
73n/a def test_get_inputs(self):
74n/a project_dir, dist = self.create_dist()
75n/a os.chdir(project_dir)
76n/a os.mkdir('spam')
77n/a cmd = install_lib(dist)
78n/a
79n/a # setting up a dist environment
80n/a cmd.compile = cmd.optimize = 1
81n/a cmd.install_dir = self.mkdtemp()
82n/a f = os.path.join(project_dir, 'spam', '__init__.py')
83n/a self.write_file(f, '# python package')
84n/a cmd.distribution.ext_modules = [Extension('foo', ['xxx'])]
85n/a cmd.distribution.packages = ['spam']
86n/a cmd.distribution.script_name = 'setup.py'
87n/a
88n/a # get_inputs should return 2 elements: spam/__init__.py and
89n/a # foo.import-tag-abiflags.so / foo.pyd
90n/a inputs = cmd.get_inputs()
91n/a self.assertEqual(len(inputs), 2, inputs)
92n/a
93n/a def test_dont_write_bytecode(self):
94n/a # makes sure byte_compile is not used
95n/a dist = self.create_dist()[1]
96n/a cmd = install_lib(dist)
97n/a cmd.compile = 1
98n/a cmd.optimize = 1
99n/a
100n/a old_dont_write_bytecode = sys.dont_write_bytecode
101n/a sys.dont_write_bytecode = True
102n/a try:
103n/a cmd.byte_compile([])
104n/a finally:
105n/a sys.dont_write_bytecode = old_dont_write_bytecode
106n/a
107n/a self.assertIn('byte-compiling is disabled',
108n/a self.logs[0][1] % self.logs[0][2])
109n/a
110n/a
111n/adef test_suite():
112n/a return unittest.makeSuite(InstallLibTestCase)
113n/a
114n/aif __name__ == "__main__":
115n/a run_unittest(test_suite())