1# -*- coding: utf-8 -*-
2"""
3Tests for msvc support module (msvc14 unit tests).
4"""
5
6import os
7from distutils.errors import DistutilsPlatformError
8import pytest
9import sys
10
11
12@pytest.mark.skipif(sys.platform != "win32",
13                    reason="These tests are only for win32")
14class TestMSVC14:
15    """Python 3.8 "distutils/tests/test_msvccompiler.py" backport"""
16    def test_no_compiler(self):
17        import setuptools.msvc as _msvccompiler
18        # makes sure query_vcvarsall raises
19        # a DistutilsPlatformError if the compiler
20        # is not found
21
22        def _find_vcvarsall(plat_spec):
23            return None, None
24
25        old_find_vcvarsall = _msvccompiler._msvc14_find_vcvarsall
26        _msvccompiler._msvc14_find_vcvarsall = _find_vcvarsall
27        try:
28            pytest.raises(DistutilsPlatformError,
29                          _msvccompiler._msvc14_get_vc_env,
30                          'wont find this version')
31        finally:
32            _msvccompiler._msvc14_find_vcvarsall = old_find_vcvarsall
33
34    def test_get_vc_env_unicode(self):
35        import setuptools.msvc as _msvccompiler
36
37        test_var = 'ṰḖṤṪ┅ṼẨṜ'
38        test_value = '₃⁴₅'
39
40        # Ensure we don't early exit from _get_vc_env
41        old_distutils_use_sdk = os.environ.pop('DISTUTILS_USE_SDK', None)
42        os.environ[test_var] = test_value
43        try:
44            env = _msvccompiler._msvc14_get_vc_env('x86')
45            assert test_var.lower() in env
46            assert test_value == env[test_var.lower()]
47        finally:
48            os.environ.pop(test_var)
49            if old_distutils_use_sdk:
50                os.environ['DISTUTILS_USE_SDK'] = old_distutils_use_sdk
51
52    def test_get_vc2017(self):
53        import setuptools.msvc as _msvccompiler
54
55        # This function cannot be mocked, so pass it if we find VS 2017
56        # and mark it skipped if we do not.
57        version, path = _msvccompiler._msvc14_find_vc2017()
58        if os.environ.get('APPVEYOR_BUILD_WORKER_IMAGE', '') in [
59            'Visual Studio 2017'
60        ]:
61            assert version
62        if version:
63            assert version >= 15
64            assert os.path.isdir(path)
65        else:
66            pytest.skip("VS 2017 is not installed")
67
68    def test_get_vc2015(self):
69        import setuptools.msvc as _msvccompiler
70
71        # This function cannot be mocked, so pass it if we find VS 2015
72        # and mark it skipped if we do not.
73        version, path = _msvccompiler._msvc14_find_vc2015()
74        if os.environ.get('APPVEYOR_BUILD_WORKER_IMAGE', '') in [
75            'Visual Studio 2015', 'Visual Studio 2017'
76        ]:
77            assert version
78        if version:
79            assert version >= 14
80            assert os.path.isdir(path)
81        else:
82            pytest.skip("VS 2015 is not installed")
83