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

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

#countcontent
1n/a"""Tests for distutils.sysconfig."""
2n/aimport os
3n/aimport shutil
4n/aimport subprocess
5n/aimport sys
6n/aimport textwrap
7n/aimport unittest
8n/a
9n/afrom distutils import sysconfig
10n/afrom distutils.ccompiler import get_default_compiler
11n/afrom distutils.tests import support
12n/afrom test.support import TESTFN, run_unittest, check_warnings
13n/a
14n/aclass SysconfigTestCase(support.EnvironGuard, unittest.TestCase):
15n/a def setUp(self):
16n/a super(SysconfigTestCase, self).setUp()
17n/a self.makefile = None
18n/a
19n/a def tearDown(self):
20n/a if self.makefile is not None:
21n/a os.unlink(self.makefile)
22n/a self.cleanup_testfn()
23n/a super(SysconfigTestCase, self).tearDown()
24n/a
25n/a def cleanup_testfn(self):
26n/a if os.path.isfile(TESTFN):
27n/a os.remove(TESTFN)
28n/a elif os.path.isdir(TESTFN):
29n/a shutil.rmtree(TESTFN)
30n/a
31n/a def test_get_config_h_filename(self):
32n/a config_h = sysconfig.get_config_h_filename()
33n/a self.assertTrue(os.path.isfile(config_h), config_h)
34n/a
35n/a def test_get_python_lib(self):
36n/a # XXX doesn't work on Linux when Python was never installed before
37n/a #self.assertTrue(os.path.isdir(lib_dir), lib_dir)
38n/a # test for pythonxx.lib?
39n/a self.assertNotEqual(sysconfig.get_python_lib(),
40n/a sysconfig.get_python_lib(prefix=TESTFN))
41n/a
42n/a def test_get_config_vars(self):
43n/a cvars = sysconfig.get_config_vars()
44n/a self.assertIsInstance(cvars, dict)
45n/a self.assertTrue(cvars)
46n/a
47n/a def test_srcdir(self):
48n/a # See Issues #15322, #15364.
49n/a srcdir = sysconfig.get_config_var('srcdir')
50n/a
51n/a self.assertTrue(os.path.isabs(srcdir), srcdir)
52n/a self.assertTrue(os.path.isdir(srcdir), srcdir)
53n/a
54n/a if sysconfig.python_build:
55n/a # The python executable has not been installed so srcdir
56n/a # should be a full source checkout.
57n/a Python_h = os.path.join(srcdir, 'Include', 'Python.h')
58n/a self.assertTrue(os.path.exists(Python_h), Python_h)
59n/a self.assertTrue(sysconfig._is_python_source_dir(srcdir))
60n/a elif os.name == 'posix':
61n/a self.assertEqual(
62n/a os.path.dirname(sysconfig.get_makefile_filename()),
63n/a srcdir)
64n/a
65n/a def test_srcdir_independent_of_cwd(self):
66n/a # srcdir should be independent of the current working directory
67n/a # See Issues #15322, #15364.
68n/a srcdir = sysconfig.get_config_var('srcdir')
69n/a cwd = os.getcwd()
70n/a try:
71n/a os.chdir('..')
72n/a srcdir2 = sysconfig.get_config_var('srcdir')
73n/a finally:
74n/a os.chdir(cwd)
75n/a self.assertEqual(srcdir, srcdir2)
76n/a
77n/a @unittest.skipUnless(get_default_compiler() == 'unix',
78n/a 'not testing if default compiler is not unix')
79n/a def test_customize_compiler(self):
80n/a os.environ['AR'] = 'my_ar'
81n/a os.environ['ARFLAGS'] = '-arflags'
82n/a
83n/a # make sure AR gets caught
84n/a class compiler:
85n/a compiler_type = 'unix'
86n/a
87n/a def set_executables(self, **kw):
88n/a self.exes = kw
89n/a
90n/a comp = compiler()
91n/a sysconfig.customize_compiler(comp)
92n/a self.assertEqual(comp.exes['archiver'], 'my_ar -arflags')
93n/a
94n/a def test_parse_makefile_base(self):
95n/a self.makefile = TESTFN
96n/a fd = open(self.makefile, 'w')
97n/a try:
98n/a fd.write(r"CONFIG_ARGS= '--arg1=optarg1' 'ENV=LIB'" '\n')
99n/a fd.write('VAR=$OTHER\nOTHER=foo')
100n/a finally:
101n/a fd.close()
102n/a d = sysconfig.parse_makefile(self.makefile)
103n/a self.assertEqual(d, {'CONFIG_ARGS': "'--arg1=optarg1' 'ENV=LIB'",
104n/a 'OTHER': 'foo'})
105n/a
106n/a def test_parse_makefile_literal_dollar(self):
107n/a self.makefile = TESTFN
108n/a fd = open(self.makefile, 'w')
109n/a try:
110n/a fd.write(r"CONFIG_ARGS= '--arg1=optarg1' 'ENV=\$$LIB'" '\n')
111n/a fd.write('VAR=$OTHER\nOTHER=foo')
112n/a finally:
113n/a fd.close()
114n/a d = sysconfig.parse_makefile(self.makefile)
115n/a self.assertEqual(d, {'CONFIG_ARGS': r"'--arg1=optarg1' 'ENV=\$LIB'",
116n/a 'OTHER': 'foo'})
117n/a
118n/a
119n/a def test_sysconfig_module(self):
120n/a import sysconfig as global_sysconfig
121n/a self.assertEqual(global_sysconfig.get_config_var('CFLAGS'),
122n/a sysconfig.get_config_var('CFLAGS'))
123n/a self.assertEqual(global_sysconfig.get_config_var('LDFLAGS'),
124n/a sysconfig.get_config_var('LDFLAGS'))
125n/a
126n/a @unittest.skipIf(sysconfig.get_config_var('CUSTOMIZED_OSX_COMPILER'),
127n/a 'compiler flags customized')
128n/a def test_sysconfig_compiler_vars(self):
129n/a # On OS X, binary installers support extension module building on
130n/a # various levels of the operating system with differing Xcode
131n/a # configurations. This requires customization of some of the
132n/a # compiler configuration directives to suit the environment on
133n/a # the installed machine. Some of these customizations may require
134n/a # running external programs and, so, are deferred until needed by
135n/a # the first extension module build. With Python 3.3, only
136n/a # the Distutils version of sysconfig is used for extension module
137n/a # builds, which happens earlier in the Distutils tests. This may
138n/a # cause the following tests to fail since no tests have caused
139n/a # the global version of sysconfig to call the customization yet.
140n/a # The solution for now is to simply skip this test in this case.
141n/a # The longer-term solution is to only have one version of sysconfig.
142n/a
143n/a import sysconfig as global_sysconfig
144n/a if sysconfig.get_config_var('CUSTOMIZED_OSX_COMPILER'):
145n/a self.skipTest('compiler flags customized')
146n/a self.assertEqual(global_sysconfig.get_config_var('LDSHARED'),
147n/a sysconfig.get_config_var('LDSHARED'))
148n/a self.assertEqual(global_sysconfig.get_config_var('CC'),
149n/a sysconfig.get_config_var('CC'))
150n/a
151n/a @unittest.skipIf(sysconfig.get_config_var('EXT_SUFFIX') is None,
152n/a 'EXT_SUFFIX required for this test')
153n/a def test_SO_deprecation(self):
154n/a self.assertWarns(DeprecationWarning,
155n/a sysconfig.get_config_var, 'SO')
156n/a
157n/a @unittest.skipIf(sysconfig.get_config_var('EXT_SUFFIX') is None,
158n/a 'EXT_SUFFIX required for this test')
159n/a def test_SO_value(self):
160n/a with check_warnings(('', DeprecationWarning)):
161n/a self.assertEqual(sysconfig.get_config_var('SO'),
162n/a sysconfig.get_config_var('EXT_SUFFIX'))
163n/a
164n/a @unittest.skipIf(sysconfig.get_config_var('EXT_SUFFIX') is None,
165n/a 'EXT_SUFFIX required for this test')
166n/a def test_SO_in_vars(self):
167n/a vars = sysconfig.get_config_vars()
168n/a self.assertIsNotNone(vars['SO'])
169n/a self.assertEqual(vars['SO'], vars['EXT_SUFFIX'])
170n/a
171n/a def test_customize_compiler_before_get_config_vars(self):
172n/a # Issue #21923: test that a Distribution compiler
173n/a # instance can be called without an explicit call to
174n/a # get_config_vars().
175n/a with open(TESTFN, 'w') as f:
176n/a f.writelines(textwrap.dedent('''\
177n/a from distutils.core import Distribution
178n/a config = Distribution().get_command_obj('config')
179n/a # try_compile may pass or it may fail if no compiler
180n/a # is found but it should not raise an exception.
181n/a rc = config.try_compile('int x;')
182n/a '''))
183n/a p = subprocess.Popen([str(sys.executable), TESTFN],
184n/a stdout=subprocess.PIPE,
185n/a stderr=subprocess.STDOUT,
186n/a universal_newlines=True)
187n/a outs, errs = p.communicate()
188n/a self.assertEqual(0, p.returncode, "Subprocess failed: " + outs)
189n/a
190n/a
191n/adef test_suite():
192n/a suite = unittest.TestSuite()
193n/a suite.addTest(unittest.makeSuite(SysconfigTestCase))
194n/a return suite
195n/a
196n/a
197n/aif __name__ == '__main__':
198n/a run_unittest(test_suite())