1"""Tests for distutils._msvccompiler."""
2import sys
3import unittest
4import os
5
6from distutils.errors import DistutilsPlatformError
7from distutils.tests import support
8from test.support import run_unittest
9
10
11SKIP_MESSAGE = (None if sys.platform == "win32" else
12                "These tests are only for win32")
13
14@unittest.skipUnless(SKIP_MESSAGE is None, SKIP_MESSAGE)
15class msvccompilerTestCase(support.TempdirManager,
16                            unittest.TestCase):
17
18    def test_no_compiler(self):
19        import distutils._msvccompiler as _msvccompiler
20        # makes sure query_vcvarsall raises
21        # a DistutilsPlatformError if the compiler
22        # is not found
23        def _find_vcvarsall(plat_spec):
24            return None, None
25
26        old_find_vcvarsall = _msvccompiler._find_vcvarsall
27        _msvccompiler._find_vcvarsall = _find_vcvarsall
28        try:
29            self.assertRaises(DistutilsPlatformError,
30                              _msvccompiler._get_vc_env,
31                             'wont find this version')
32        finally:
33            _msvccompiler._find_vcvarsall = old_find_vcvarsall
34
35    def test_get_vc_env_unicode(self):
36        import distutils._msvccompiler as _msvccompiler
37
38        test_var = 'ṰḖṤṪ┅ṼẨṜ'
39        test_value = '₃⁴₅'
40
41        # Ensure we don't early exit from _get_vc_env
42        old_distutils_use_sdk = os.environ.pop('DISTUTILS_USE_SDK', None)
43        os.environ[test_var] = test_value
44        try:
45            env = _msvccompiler._get_vc_env('x86')
46            self.assertIn(test_var.lower(), env)
47            self.assertEqual(test_value, env[test_var.lower()])
48        finally:
49            os.environ.pop(test_var)
50            if old_distutils_use_sdk:
51                os.environ['DISTUTILS_USE_SDK'] = old_distutils_use_sdk
52
53    def test_get_vc2017(self):
54        import distutils._msvccompiler as _msvccompiler
55
56        # This function cannot be mocked, so pass it if we find VS 2017
57        # and mark it skipped if we do not.
58        version, path = _msvccompiler._find_vc2017()
59        if version:
60            self.assertGreaterEqual(version, 15)
61            self.assertTrue(os.path.isdir(path))
62        else:
63            raise unittest.SkipTest("VS 2017 is not installed")
64
65    def test_get_vc2015(self):
66        import distutils._msvccompiler as _msvccompiler
67
68        # This function cannot be mocked, so pass it if we find VS 2015
69        # and mark it skipped if we do not.
70        version, path = _msvccompiler._find_vc2015()
71        if version:
72            self.assertGreaterEqual(version, 14)
73            self.assertTrue(os.path.isdir(path))
74        else:
75            raise unittest.SkipTest("VS 2015 is not installed")
76
77def test_suite():
78    return unittest.makeSuite(msvccompilerTestCase)
79
80if __name__ == "__main__":
81    run_unittest(test_suite())
82