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

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

#countcontent
1n/a"""Tests for distutils.core."""
2n/a
3n/aimport io
4n/aimport distutils.core
5n/aimport os
6n/aimport shutil
7n/aimport sys
8n/aimport test.support
9n/afrom test.support import captured_stdout, run_unittest
10n/aimport unittest
11n/afrom distutils.tests import support
12n/afrom distutils import log
13n/a
14n/a# setup script that uses __file__
15n/asetup_using___file__ = """\
16n/a
17n/a__file__
18n/a
19n/afrom distutils.core import setup
20n/asetup()
21n/a"""
22n/a
23n/asetup_prints_cwd = """\
24n/a
25n/aimport os
26n/aprint(os.getcwd())
27n/a
28n/afrom distutils.core import setup
29n/asetup()
30n/a"""
31n/a
32n/asetup_does_nothing = """\
33n/afrom distutils.core import setup
34n/asetup()
35n/a"""
36n/a
37n/a
38n/asetup_defines_subclass = """\
39n/afrom distutils.core import setup
40n/afrom distutils.command.install import install as _install
41n/a
42n/aclass install(_install):
43n/a sub_commands = _install.sub_commands + ['cmd']
44n/a
45n/asetup(cmdclass={'install': install})
46n/a"""
47n/a
48n/aclass CoreTestCase(support.EnvironGuard, unittest.TestCase):
49n/a
50n/a def setUp(self):
51n/a super(CoreTestCase, self).setUp()
52n/a self.old_stdout = sys.stdout
53n/a self.cleanup_testfn()
54n/a self.old_argv = sys.argv, sys.argv[:]
55n/a self.addCleanup(log.set_threshold, log._global_log.threshold)
56n/a
57n/a def tearDown(self):
58n/a sys.stdout = self.old_stdout
59n/a self.cleanup_testfn()
60n/a sys.argv = self.old_argv[0]
61n/a sys.argv[:] = self.old_argv[1]
62n/a super(CoreTestCase, self).tearDown()
63n/a
64n/a def cleanup_testfn(self):
65n/a path = test.support.TESTFN
66n/a if os.path.isfile(path):
67n/a os.remove(path)
68n/a elif os.path.isdir(path):
69n/a shutil.rmtree(path)
70n/a
71n/a def write_setup(self, text, path=test.support.TESTFN):
72n/a f = open(path, "w")
73n/a try:
74n/a f.write(text)
75n/a finally:
76n/a f.close()
77n/a return path
78n/a
79n/a def test_run_setup_provides_file(self):
80n/a # Make sure the script can use __file__; if that's missing, the test
81n/a # setup.py script will raise NameError.
82n/a distutils.core.run_setup(
83n/a self.write_setup(setup_using___file__))
84n/a
85n/a def test_run_setup_preserves_sys_argv(self):
86n/a # Make sure run_setup does not clobber sys.argv
87n/a argv_copy = sys.argv.copy()
88n/a distutils.core.run_setup(
89n/a self.write_setup(setup_does_nothing))
90n/a self.assertEqual(sys.argv, argv_copy)
91n/a
92n/a def test_run_setup_defines_subclass(self):
93n/a # Make sure the script can use __file__; if that's missing, the test
94n/a # setup.py script will raise NameError.
95n/a dist = distutils.core.run_setup(
96n/a self.write_setup(setup_defines_subclass))
97n/a install = dist.get_command_obj('install')
98n/a self.assertIn('cmd', install.sub_commands)
99n/a
100n/a def test_run_setup_uses_current_dir(self):
101n/a # This tests that the setup script is run with the current directory
102n/a # as its own current directory; this was temporarily broken by a
103n/a # previous patch when TESTFN did not use the current directory.
104n/a sys.stdout = io.StringIO()
105n/a cwd = os.getcwd()
106n/a
107n/a # Create a directory and write the setup.py file there:
108n/a os.mkdir(test.support.TESTFN)
109n/a setup_py = os.path.join(test.support.TESTFN, "setup.py")
110n/a distutils.core.run_setup(
111n/a self.write_setup(setup_prints_cwd, path=setup_py))
112n/a
113n/a output = sys.stdout.getvalue()
114n/a if output.endswith("\n"):
115n/a output = output[:-1]
116n/a self.assertEqual(cwd, output)
117n/a
118n/a def test_debug_mode(self):
119n/a # this covers the code called when DEBUG is set
120n/a sys.argv = ['setup.py', '--name']
121n/a with captured_stdout() as stdout:
122n/a distutils.core.setup(name='bar')
123n/a stdout.seek(0)
124n/a self.assertEqual(stdout.read(), 'bar\n')
125n/a
126n/a distutils.core.DEBUG = True
127n/a try:
128n/a with captured_stdout() as stdout:
129n/a distutils.core.setup(name='bar')
130n/a finally:
131n/a distutils.core.DEBUG = False
132n/a stdout.seek(0)
133n/a wanted = "options (after parsing config files):\n"
134n/a self.assertEqual(stdout.readlines()[0], wanted)
135n/a
136n/adef test_suite():
137n/a return unittest.makeSuite(CoreTestCase)
138n/a
139n/aif __name__ == "__main__":
140n/a run_unittest(test_suite())