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