test_msvccompiler.py 2.8 KB

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