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