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