python (3.11.7)
       1  """Tests for distutils._msvccompiler."""
       2  import sys
       3  import unittest
       4  import os
       5  
       6  from distutils.errors import DistutilsPlatformError
       7  from distutils.tests import support
       8  
       9  
      10  SKIP_MESSAGE = (None if sys.platform == "win32" else
      11                  "These tests are only for win32")
      12  
      13  @unittest.skipUnless(SKIP_MESSAGE is None, SKIP_MESSAGE)
      14  class ESC[4;38;5;81mmsvccompilerTestCase(ESC[4;38;5;149msupportESC[4;38;5;149m.ESC[4;38;5;149mTempdirManager,
      15                              ESC[4;38;5;149munittestESC[4;38;5;149m.ESC[4;38;5;149mTestCase):
      16  
      17      def test_no_compiler(self):
      18          import distutils._msvccompiler as _msvccompiler
      19          # makes sure query_vcvarsall raises
      20          # a DistutilsPlatformError if the compiler
      21          # is not found
      22          def _find_vcvarsall(plat_spec):
      23              return None, None
      24  
      25          old_find_vcvarsall = _msvccompiler._find_vcvarsall
      26          _msvccompiler._find_vcvarsall = _find_vcvarsall
      27          try:
      28              self.assertRaises(DistutilsPlatformError,
      29                                _msvccompiler._get_vc_env,
      30                               'wont find this version')
      31          finally:
      32              _msvccompiler._find_vcvarsall = old_find_vcvarsall
      33  
      34      def test_get_vc_env_unicode(self):
      35          import distutils._msvccompiler 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._get_vc_env('x86')
      45              self.assertIn(test_var.lower(), env)
      46              self.assertEqual(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 distutils._msvccompiler 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._find_vc2017()
      58          if version:
      59              self.assertGreaterEqual(version, 15)
      60              self.assertTrue(os.path.isdir(path))
      61          else:
      62              raise unittest.SkipTest("VS 2017 is not installed")
      63  
      64      def test_get_vc2015(self):
      65          import distutils._msvccompiler as _msvccompiler
      66  
      67          # This function cannot be mocked, so pass it if we find VS 2015
      68          # and mark it skipped if we do not.
      69          version, path = _msvccompiler._find_vc2015()
      70          if version:
      71              self.assertGreaterEqual(version, 14)
      72              self.assertTrue(os.path.isdir(path))
      73          else:
      74              raise unittest.SkipTest("VS 2015 is not installed")
      75  
      76  if __name__ == "__main__":
      77      unittest.main()