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

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

#countcontent
1n/a"""Tests for distutils.command.install."""
2n/a
3n/aimport os
4n/aimport sys
5n/aimport unittest
6n/aimport site
7n/a
8n/afrom test.support import captured_stdout, run_unittest
9n/a
10n/afrom distutils import sysconfig
11n/afrom distutils.command.install import install
12n/afrom distutils.command import install as install_module
13n/afrom distutils.command.build_ext import build_ext
14n/afrom distutils.command.install import INSTALL_SCHEMES
15n/afrom distutils.core import Distribution
16n/afrom distutils.errors import DistutilsOptionError
17n/afrom distutils.extension import Extension
18n/a
19n/afrom distutils.tests import support
20n/afrom test import support as test_support
21n/a
22n/a
23n/adef _make_ext_name(modname):
24n/a return modname + sysconfig.get_config_var('EXT_SUFFIX')
25n/a
26n/a
27n/aclass InstallTestCase(support.TempdirManager,
28n/a support.EnvironGuard,
29n/a support.LoggingSilencer,
30n/a unittest.TestCase):
31n/a
32n/a def test_home_installation_scheme(self):
33n/a # This ensure two things:
34n/a # - that --home generates the desired set of directory names
35n/a # - test --home is supported on all platforms
36n/a builddir = self.mkdtemp()
37n/a destination = os.path.join(builddir, "installation")
38n/a
39n/a dist = Distribution({"name": "foopkg"})
40n/a # script_name need not exist, it just need to be initialized
41n/a dist.script_name = os.path.join(builddir, "setup.py")
42n/a dist.command_obj["build"] = support.DummyCommand(
43n/a build_base=builddir,
44n/a build_lib=os.path.join(builddir, "lib"),
45n/a )
46n/a
47n/a cmd = install(dist)
48n/a cmd.home = destination
49n/a cmd.ensure_finalized()
50n/a
51n/a self.assertEqual(cmd.install_base, destination)
52n/a self.assertEqual(cmd.install_platbase, destination)
53n/a
54n/a def check_path(got, expected):
55n/a got = os.path.normpath(got)
56n/a expected = os.path.normpath(expected)
57n/a self.assertEqual(got, expected)
58n/a
59n/a libdir = os.path.join(destination, "lib", "python")
60n/a check_path(cmd.install_lib, libdir)
61n/a check_path(cmd.install_platlib, libdir)
62n/a check_path(cmd.install_purelib, libdir)
63n/a check_path(cmd.install_headers,
64n/a os.path.join(destination, "include", "python", "foopkg"))
65n/a check_path(cmd.install_scripts, os.path.join(destination, "bin"))
66n/a check_path(cmd.install_data, destination)
67n/a
68n/a def test_user_site(self):
69n/a # test install with --user
70n/a # preparing the environment for the test
71n/a self.old_user_base = site.USER_BASE
72n/a self.old_user_site = site.USER_SITE
73n/a self.tmpdir = self.mkdtemp()
74n/a self.user_base = os.path.join(self.tmpdir, 'B')
75n/a self.user_site = os.path.join(self.tmpdir, 'S')
76n/a site.USER_BASE = self.user_base
77n/a site.USER_SITE = self.user_site
78n/a install_module.USER_BASE = self.user_base
79n/a install_module.USER_SITE = self.user_site
80n/a
81n/a def _expanduser(path):
82n/a return self.tmpdir
83n/a self.old_expand = os.path.expanduser
84n/a os.path.expanduser = _expanduser
85n/a
86n/a def cleanup():
87n/a site.USER_BASE = self.old_user_base
88n/a site.USER_SITE = self.old_user_site
89n/a install_module.USER_BASE = self.old_user_base
90n/a install_module.USER_SITE = self.old_user_site
91n/a os.path.expanduser = self.old_expand
92n/a
93n/a self.addCleanup(cleanup)
94n/a
95n/a for key in ('nt_user', 'unix_user'):
96n/a self.assertIn(key, INSTALL_SCHEMES)
97n/a
98n/a dist = Distribution({'name': 'xx'})
99n/a cmd = install(dist)
100n/a
101n/a # making sure the user option is there
102n/a options = [name for name, short, lable in
103n/a cmd.user_options]
104n/a self.assertIn('user', options)
105n/a
106n/a # setting a value
107n/a cmd.user = 1
108n/a
109n/a # user base and site shouldn't be created yet
110n/a self.assertFalse(os.path.exists(self.user_base))
111n/a self.assertFalse(os.path.exists(self.user_site))
112n/a
113n/a # let's run finalize
114n/a cmd.ensure_finalized()
115n/a
116n/a # now they should
117n/a self.assertTrue(os.path.exists(self.user_base))
118n/a self.assertTrue(os.path.exists(self.user_site))
119n/a
120n/a self.assertIn('userbase', cmd.config_vars)
121n/a self.assertIn('usersite', cmd.config_vars)
122n/a
123n/a def test_handle_extra_path(self):
124n/a dist = Distribution({'name': 'xx', 'extra_path': 'path,dirs'})
125n/a cmd = install(dist)
126n/a
127n/a # two elements
128n/a cmd.handle_extra_path()
129n/a self.assertEqual(cmd.extra_path, ['path', 'dirs'])
130n/a self.assertEqual(cmd.extra_dirs, 'dirs')
131n/a self.assertEqual(cmd.path_file, 'path')
132n/a
133n/a # one element
134n/a cmd.extra_path = ['path']
135n/a cmd.handle_extra_path()
136n/a self.assertEqual(cmd.extra_path, ['path'])
137n/a self.assertEqual(cmd.extra_dirs, 'path')
138n/a self.assertEqual(cmd.path_file, 'path')
139n/a
140n/a # none
141n/a dist.extra_path = cmd.extra_path = None
142n/a cmd.handle_extra_path()
143n/a self.assertEqual(cmd.extra_path, None)
144n/a self.assertEqual(cmd.extra_dirs, '')
145n/a self.assertEqual(cmd.path_file, None)
146n/a
147n/a # three elements (no way !)
148n/a cmd.extra_path = 'path,dirs,again'
149n/a self.assertRaises(DistutilsOptionError, cmd.handle_extra_path)
150n/a
151n/a def test_finalize_options(self):
152n/a dist = Distribution({'name': 'xx'})
153n/a cmd = install(dist)
154n/a
155n/a # must supply either prefix/exec-prefix/home or
156n/a # install-base/install-platbase -- not both
157n/a cmd.prefix = 'prefix'
158n/a cmd.install_base = 'base'
159n/a self.assertRaises(DistutilsOptionError, cmd.finalize_options)
160n/a
161n/a # must supply either home or prefix/exec-prefix -- not both
162n/a cmd.install_base = None
163n/a cmd.home = 'home'
164n/a self.assertRaises(DistutilsOptionError, cmd.finalize_options)
165n/a
166n/a # can't combine user with prefix/exec_prefix/home or
167n/a # install_(plat)base
168n/a cmd.prefix = None
169n/a cmd.user = 'user'
170n/a self.assertRaises(DistutilsOptionError, cmd.finalize_options)
171n/a
172n/a def test_record(self):
173n/a install_dir = self.mkdtemp()
174n/a project_dir, dist = self.create_dist(py_modules=['hello'],
175n/a scripts=['sayhi'])
176n/a os.chdir(project_dir)
177n/a self.write_file('hello.py', "def main(): print('o hai')")
178n/a self.write_file('sayhi', 'from hello import main; main()')
179n/a
180n/a cmd = install(dist)
181n/a dist.command_obj['install'] = cmd
182n/a cmd.root = install_dir
183n/a cmd.record = os.path.join(project_dir, 'filelist')
184n/a cmd.ensure_finalized()
185n/a cmd.run()
186n/a
187n/a f = open(cmd.record)
188n/a try:
189n/a content = f.read()
190n/a finally:
191n/a f.close()
192n/a
193n/a found = [os.path.basename(line) for line in content.splitlines()]
194n/a expected = ['hello.py', 'hello.%s.pyc' % sys.implementation.cache_tag,
195n/a 'sayhi',
196n/a 'UNKNOWN-0.0.0-py%s.%s.egg-info' % sys.version_info[:2]]
197n/a self.assertEqual(found, expected)
198n/a
199n/a def test_record_extensions(self):
200n/a cmd = test_support.missing_compiler_executable()
201n/a if cmd is not None:
202n/a self.skipTest('The %r command is not found' % cmd)
203n/a install_dir = self.mkdtemp()
204n/a project_dir, dist = self.create_dist(ext_modules=[
205n/a Extension('xx', ['xxmodule.c'])])
206n/a os.chdir(project_dir)
207n/a support.copy_xxmodule_c(project_dir)
208n/a
209n/a buildextcmd = build_ext(dist)
210n/a support.fixup_build_ext(buildextcmd)
211n/a buildextcmd.ensure_finalized()
212n/a
213n/a cmd = install(dist)
214n/a dist.command_obj['install'] = cmd
215n/a dist.command_obj['build_ext'] = buildextcmd
216n/a cmd.root = install_dir
217n/a cmd.record = os.path.join(project_dir, 'filelist')
218n/a cmd.ensure_finalized()
219n/a cmd.run()
220n/a
221n/a f = open(cmd.record)
222n/a try:
223n/a content = f.read()
224n/a finally:
225n/a f.close()
226n/a
227n/a found = [os.path.basename(line) for line in content.splitlines()]
228n/a expected = [_make_ext_name('xx'),
229n/a 'UNKNOWN-0.0.0-py%s.%s.egg-info' % sys.version_info[:2]]
230n/a self.assertEqual(found, expected)
231n/a
232n/a def test_debug_mode(self):
233n/a # this covers the code called when DEBUG is set
234n/a old_logs_len = len(self.logs)
235n/a install_module.DEBUG = True
236n/a try:
237n/a with captured_stdout():
238n/a self.test_record()
239n/a finally:
240n/a install_module.DEBUG = False
241n/a self.assertGreater(len(self.logs), old_logs_len)
242n/a
243n/a
244n/adef test_suite():
245n/a return unittest.makeSuite(InstallTestCase)
246n/a
247n/aif __name__ == "__main__":
248n/a run_unittest(test_suite())