»Core Development>Code coverage>Lib/distutils/tests/test_msvccompiler.py

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

#countcontent
1n/a"""Tests for distutils._msvccompiler."""
2n/aimport sys
3n/aimport unittest
4n/aimport os
5n/a
6n/afrom distutils.errors import DistutilsPlatformError
7n/afrom distutils.tests import support
8n/afrom test.support import run_unittest
9n/a
10n/a
11n/aSKIP_MESSAGE = (None if sys.platform == "win32" else
12n/a "These tests are only for win32")
13n/a
14n/a@unittest.skipUnless(SKIP_MESSAGE is None, SKIP_MESSAGE)
15n/aclass msvccompilerTestCase(support.TempdirManager,
16n/a unittest.TestCase):
17n/a
18n/a def test_no_compiler(self):
19n/a import distutils._msvccompiler as _msvccompiler
20n/a # makes sure query_vcvarsall raises
21n/a # a DistutilsPlatformError if the compiler
22n/a # is not found
23n/a def _find_vcvarsall(plat_spec):
24n/a return None, None
25n/a
26n/a old_find_vcvarsall = _msvccompiler._find_vcvarsall
27n/a _msvccompiler._find_vcvarsall = _find_vcvarsall
28n/a try:
29n/a self.assertRaises(DistutilsPlatformError,
30n/a _msvccompiler._get_vc_env,
31n/a 'wont find this version')
32n/a finally:
33n/a _msvccompiler._find_vcvarsall = old_find_vcvarsall
34n/a
35n/a def test_compiler_options(self):
36n/a import distutils._msvccompiler as _msvccompiler
37n/a # suppress path to vcruntime from _find_vcvarsall to
38n/a # check that /MT is added to compile options
39n/a old_find_vcvarsall = _msvccompiler._find_vcvarsall
40n/a def _find_vcvarsall(plat_spec):
41n/a return old_find_vcvarsall(plat_spec)[0], None
42n/a _msvccompiler._find_vcvarsall = _find_vcvarsall
43n/a try:
44n/a compiler = _msvccompiler.MSVCCompiler()
45n/a compiler.initialize()
46n/a
47n/a self.assertIn('/MT', compiler.compile_options)
48n/a self.assertNotIn('/MD', compiler.compile_options)
49n/a finally:
50n/a _msvccompiler._find_vcvarsall = old_find_vcvarsall
51n/a
52n/a def test_vcruntime_copy(self):
53n/a import distutils._msvccompiler as _msvccompiler
54n/a # force path to a known file - it doesn't matter
55n/a # what we copy as long as its name is not in
56n/a # _msvccompiler._BUNDLED_DLLS
57n/a old_find_vcvarsall = _msvccompiler._find_vcvarsall
58n/a def _find_vcvarsall(plat_spec):
59n/a return old_find_vcvarsall(plat_spec)[0], __file__
60n/a _msvccompiler._find_vcvarsall = _find_vcvarsall
61n/a try:
62n/a tempdir = self.mkdtemp()
63n/a compiler = _msvccompiler.MSVCCompiler()
64n/a compiler.initialize()
65n/a compiler._copy_vcruntime(tempdir)
66n/a
67n/a self.assertTrue(os.path.isfile(os.path.join(
68n/a tempdir, os.path.basename(__file__))))
69n/a finally:
70n/a _msvccompiler._find_vcvarsall = old_find_vcvarsall
71n/a
72n/a def test_vcruntime_skip_copy(self):
73n/a import distutils._msvccompiler as _msvccompiler
74n/a
75n/a tempdir = self.mkdtemp()
76n/a compiler = _msvccompiler.MSVCCompiler()
77n/a compiler.initialize()
78n/a dll = compiler._vcruntime_redist
79n/a self.assertTrue(os.path.isfile(dll))
80n/a
81n/a compiler._copy_vcruntime(tempdir)
82n/a
83n/a self.assertFalse(os.path.isfile(os.path.join(
84n/a tempdir, os.path.basename(dll))))
85n/a
86n/a def test_get_vc_env_unicode(self):
87n/a import distutils._msvccompiler as _msvccompiler
88n/a
89n/a test_var = 'ṰḖṤṪ┅ṼẨṜ'
90n/a test_value = '₃⁴₅'
91n/a
92n/a # Ensure we don't early exit from _get_vc_env
93n/a old_distutils_use_sdk = os.environ.pop('DISTUTILS_USE_SDK', None)
94n/a os.environ[test_var] = test_value
95n/a try:
96n/a env = _msvccompiler._get_vc_env('x86')
97n/a self.assertIn(test_var.lower(), env)
98n/a self.assertEqual(test_value, env[test_var.lower()])
99n/a finally:
100n/a os.environ.pop(test_var)
101n/a if old_distutils_use_sdk:
102n/a os.environ['DISTUTILS_USE_SDK'] = old_distutils_use_sdk
103n/a
104n/adef test_suite():
105n/a return unittest.makeSuite(msvccompilerTestCase)
106n/a
107n/aif __name__ == "__main__":
108n/a run_unittest(test_suite())