test_cygwinccompiler.py 5.5 KB

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