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