test_cygwinccompiler.py 5.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155
  1. """Tests for distutils.cygwinccompiler."""
  2. import unittest
  3. import sys
  4. import os
  5. from io import BytesIO
  6. import subprocess
  7. from test.support import run_unittest
  8. from distutils import cygwinccompiler
  9. from distutils.cygwinccompiler import (CygwinCCompiler, check_config_h,
  10. CONFIG_H_OK, CONFIG_H_NOTOK,
  11. CONFIG_H_UNCERTAIN, get_versions,
  12. get_msvcr)
  13. from distutils.tests import support
  14. class FakePopen(object):
  15. test_class = None
  16. def __init__(self, cmd, shell, stdout):
  17. self.cmd = cmd.split()[0]
  18. exes = self.test_class._exes
  19. if self.cmd in exes:
  20. # issue #6438 in Python 3.x, Popen returns bytes
  21. self.stdout = BytesIO(exes[self.cmd])
  22. else:
  23. self.stdout = os.popen(cmd, 'r')
  24. class CygwinCCompilerTestCase(support.TempdirManager,
  25. unittest.TestCase):
  26. def setUp(self):
  27. super(CygwinCCompilerTestCase, self).setUp()
  28. self.version = sys.version
  29. self.python_h = os.path.join(self.mkdtemp(), 'python.h')
  30. from distutils import sysconfig
  31. self.old_get_config_h_filename = sysconfig.get_config_h_filename
  32. sysconfig.get_config_h_filename = self._get_config_h_filename
  33. self.old_find_executable = cygwinccompiler.find_executable
  34. cygwinccompiler.find_executable = self._find_executable
  35. self._exes = {}
  36. self.old_popen = cygwinccompiler.Popen
  37. FakePopen.test_class = self
  38. cygwinccompiler.Popen = FakePopen
  39. def tearDown(self):
  40. sys.version = self.version
  41. from distutils import sysconfig
  42. sysconfig.get_config_h_filename = self.old_get_config_h_filename
  43. cygwinccompiler.find_executable = self.old_find_executable
  44. cygwinccompiler.Popen = self.old_popen
  45. super(CygwinCCompilerTestCase, self).tearDown()
  46. def _get_config_h_filename(self):
  47. return self.python_h
  48. def _find_executable(self, name):
  49. if name in self._exes:
  50. return name
  51. return None
  52. def test_check_config_h(self):
  53. # check_config_h looks for "GCC" in sys.version first
  54. # returns CONFIG_H_OK if found
  55. sys.version = ('2.6.1 (r261:67515, Dec 6 2008, 16:42:21) \n[GCC '
  56. '4.0.1 (Apple Computer, Inc. build 5370)]')
  57. self.assertEqual(check_config_h()[0], CONFIG_H_OK)
  58. # then it tries to see if it can find "__GNUC__" in pyconfig.h
  59. sys.version = 'something without the *CC word'
  60. # if the file doesn't exist it returns CONFIG_H_UNCERTAIN
  61. self.assertEqual(check_config_h()[0], CONFIG_H_UNCERTAIN)
  62. # if it exists but does not contain __GNUC__, it returns CONFIG_H_NOTOK
  63. self.write_file(self.python_h, 'xxx')
  64. self.assertEqual(check_config_h()[0], CONFIG_H_NOTOK)
  65. # and CONFIG_H_OK if __GNUC__ is found
  66. self.write_file(self.python_h, 'xxx __GNUC__ xxx')
  67. self.assertEqual(check_config_h()[0], CONFIG_H_OK)
  68. def test_get_versions(self):
  69. # get_versions calls distutils.spawn.find_executable on
  70. # 'gcc', 'ld' and 'dllwrap'
  71. self.assertEqual(get_versions(), (None, None, None))
  72. # Let's fake we have 'gcc' and it returns '3.4.5'
  73. self._exes['gcc'] = b'gcc (GCC) 3.4.5 (mingw special)\nFSF'
  74. res = get_versions()
  75. self.assertEqual(str(res[0]), '3.4.5')
  76. # and let's see what happens when the version
  77. # doesn't match the regular expression
  78. # (\d+\.\d+(\.\d+)*)
  79. self._exes['gcc'] = b'very strange output'
  80. res = get_versions()
  81. self.assertEqual(res[0], None)
  82. # same thing for ld
  83. self._exes['ld'] = b'GNU ld version 2.17.50 20060824'
  84. res = get_versions()
  85. self.assertEqual(str(res[1]), '2.17.50')
  86. self._exes['ld'] = b'@(#)PROGRAM:ld PROJECT:ld64-77'
  87. res = get_versions()
  88. self.assertEqual(res[1], None)
  89. # and dllwrap
  90. self._exes['dllwrap'] = b'GNU dllwrap 2.17.50 20060824\nFSF'
  91. res = get_versions()
  92. self.assertEqual(str(res[2]), '2.17.50')
  93. self._exes['dllwrap'] = b'Cheese Wrap'
  94. res = get_versions()
  95. self.assertEqual(res[2], None)
  96. def test_get_msvcr(self):
  97. # none
  98. sys.version = ('2.6.1 (r261:67515, Dec 6 2008, 16:42:21) '
  99. '\n[GCC 4.0.1 (Apple Computer, Inc. build 5370)]')
  100. self.assertEqual(get_msvcr(), None)
  101. # MSVC 7.0
  102. sys.version = ('2.5.1 (r251:54863, Apr 18 2007, 08:51:08) '
  103. '[MSC v.1300 32 bits (Intel)]')
  104. self.assertEqual(get_msvcr(), ['msvcr70'])
  105. # MSVC 7.1
  106. sys.version = ('2.5.1 (r251:54863, Apr 18 2007, 08:51:08) '
  107. '[MSC v.1310 32 bits (Intel)]')
  108. self.assertEqual(get_msvcr(), ['msvcr71'])
  109. # VS2005 / MSVC 8.0
  110. sys.version = ('2.5.1 (r251:54863, Apr 18 2007, 08:51:08) '
  111. '[MSC v.1400 32 bits (Intel)]')
  112. self.assertEqual(get_msvcr(), ['msvcr80'])
  113. # VS2008 / MSVC 9.0
  114. sys.version = ('2.5.1 (r251:54863, Apr 18 2007, 08:51:08) '
  115. '[MSC v.1500 32 bits (Intel)]')
  116. self.assertEqual(get_msvcr(), ['msvcr90'])
  117. # unknown
  118. sys.version = ('2.5.1 (r251:54863, Apr 18 2007, 08:51:08) '
  119. '[MSC v.1999 32 bits (Intel)]')
  120. self.assertRaises(ValueError, get_msvcr)
  121. def test_suite():
  122. return unittest.makeSuite(CygwinCCompilerTestCase)
  123. if __name__ == '__main__':
  124. run_unittest(test_suite())