ยปCore Development>Code coverage>Lib/distutils/tests/test_cygwinccompiler.py

Python code coverage for Lib/distutils/tests/test_cygwinccompiler.py

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