ยปCore Development>Code coverage>Lib/test/test__osx_support.py

Python code coverage for Lib/test/test__osx_support.py

#countcontent
1n/a"""
2n/aTest suite for _osx_support: shared OS X support functions.
3n/a"""
4n/a
5n/aimport os
6n/aimport platform
7n/aimport stat
8n/aimport sys
9n/aimport unittest
10n/a
11n/aimport test.support
12n/a
13n/aimport _osx_support
14n/a
15n/a@unittest.skipUnless(sys.platform.startswith("darwin"), "requires OS X")
16n/aclass Test_OSXSupport(unittest.TestCase):
17n/a
18n/a def setUp(self):
19n/a self.maxDiff = None
20n/a self.prog_name = 'bogus_program_xxxx'
21n/a self.temp_path_dir = os.path.abspath(os.getcwd())
22n/a self.env = test.support.EnvironmentVarGuard()
23n/a self.addCleanup(self.env.__exit__)
24n/a for cv in ('CFLAGS', 'LDFLAGS', 'CPPFLAGS',
25n/a 'BASECFLAGS', 'BLDSHARED', 'LDSHARED', 'CC',
26n/a 'CXX', 'PY_CFLAGS', 'PY_LDFLAGS', 'PY_CPPFLAGS',
27n/a 'PY_CORE_CFLAGS'):
28n/a if cv in self.env:
29n/a self.env.unset(cv)
30n/a
31n/a def add_expected_saved_initial_values(self, config_vars, expected_vars):
32n/a # Ensure that the initial values for all modified config vars
33n/a # are also saved with modified keys.
34n/a expected_vars.update(('_OSX_SUPPORT_INITIAL_'+ k,
35n/a config_vars[k]) for k in config_vars
36n/a if config_vars[k] != expected_vars[k])
37n/a
38n/a def test__find_executable(self):
39n/a if self.env['PATH']:
40n/a self.env['PATH'] = self.env['PATH'] + ':'
41n/a self.env['PATH'] = self.env['PATH'] + os.path.abspath(self.temp_path_dir)
42n/a test.support.unlink(self.prog_name)
43n/a self.assertIsNone(_osx_support._find_executable(self.prog_name))
44n/a self.addCleanup(test.support.unlink, self.prog_name)
45n/a with open(self.prog_name, 'w') as f:
46n/a f.write("#!/bin/sh\n/bin/echo OK\n")
47n/a os.chmod(self.prog_name, stat.S_IRWXU)
48n/a self.assertEqual(self.prog_name,
49n/a _osx_support._find_executable(self.prog_name))
50n/a
51n/a def test__read_output(self):
52n/a if self.env['PATH']:
53n/a self.env['PATH'] = self.env['PATH'] + ':'
54n/a self.env['PATH'] = self.env['PATH'] + os.path.abspath(self.temp_path_dir)
55n/a test.support.unlink(self.prog_name)
56n/a self.addCleanup(test.support.unlink, self.prog_name)
57n/a with open(self.prog_name, 'w') as f:
58n/a f.write("#!/bin/sh\n/bin/echo ExpectedOutput\n")
59n/a os.chmod(self.prog_name, stat.S_IRWXU)
60n/a self.assertEqual('ExpectedOutput',
61n/a _osx_support._read_output(self.prog_name))
62n/a
63n/a def test__find_build_tool(self):
64n/a out = _osx_support._find_build_tool('cc')
65n/a self.assertTrue(os.path.isfile(out),
66n/a 'cc not found - check xcode-select')
67n/a
68n/a def test__get_system_version(self):
69n/a self.assertTrue(platform.mac_ver()[0].startswith(
70n/a _osx_support._get_system_version()))
71n/a
72n/a def test__remove_original_values(self):
73n/a config_vars = {
74n/a 'CC': 'gcc-test -pthreads',
75n/a }
76n/a expected_vars = {
77n/a 'CC': 'clang -pthreads',
78n/a }
79n/a cv = 'CC'
80n/a newvalue = 'clang -pthreads'
81n/a _osx_support._save_modified_value(config_vars, cv, newvalue)
82n/a self.assertNotEqual(expected_vars, config_vars)
83n/a _osx_support._remove_original_values(config_vars)
84n/a self.assertEqual(expected_vars, config_vars)
85n/a
86n/a def test__save_modified_value(self):
87n/a config_vars = {
88n/a 'CC': 'gcc-test -pthreads',
89n/a }
90n/a expected_vars = {
91n/a 'CC': 'clang -pthreads',
92n/a }
93n/a self.add_expected_saved_initial_values(config_vars, expected_vars)
94n/a cv = 'CC'
95n/a newvalue = 'clang -pthreads'
96n/a _osx_support._save_modified_value(config_vars, cv, newvalue)
97n/a self.assertEqual(expected_vars, config_vars)
98n/a
99n/a def test__save_modified_value_unchanged(self):
100n/a config_vars = {
101n/a 'CC': 'gcc-test -pthreads',
102n/a }
103n/a expected_vars = config_vars.copy()
104n/a cv = 'CC'
105n/a newvalue = 'gcc-test -pthreads'
106n/a _osx_support._save_modified_value(config_vars, cv, newvalue)
107n/a self.assertEqual(expected_vars, config_vars)
108n/a
109n/a def test__supports_universal_builds(self):
110n/a import platform
111n/a mac_ver_tuple = tuple(int(i) for i in
112n/a platform.mac_ver()[0].split('.')[0:2])
113n/a self.assertEqual(mac_ver_tuple >= (10, 4),
114n/a _osx_support._supports_universal_builds())
115n/a
116n/a def test__find_appropriate_compiler(self):
117n/a compilers = (
118n/a ('gcc-test', 'i686-apple-darwin11-llvm-gcc-4.2'),
119n/a ('clang', 'clang version 3.1'),
120n/a )
121n/a config_vars = {
122n/a 'CC': 'gcc-test -pthreads',
123n/a 'CXX': 'cc++-test',
124n/a 'CFLAGS': '-fno-strict-aliasing -g -O3 -arch ppc -arch i386 ',
125n/a 'LDFLAGS': '-arch ppc -arch i386 -g',
126n/a 'CPPFLAGS': '-I. -isysroot /Developer/SDKs/MacOSX10.4u.sdk',
127n/a 'BLDSHARED': 'gcc-test -bundle -arch ppc -arch i386 -g',
128n/a 'LDSHARED': 'gcc-test -bundle -arch ppc -arch i386 '
129n/a '-isysroot /Developer/SDKs/MacOSX10.4u.sdk -g',
130n/a }
131n/a expected_vars = {
132n/a 'CC': 'clang -pthreads',
133n/a 'CXX': 'clang++',
134n/a 'CFLAGS': '-fno-strict-aliasing -g -O3 -arch ppc -arch i386 ',
135n/a 'LDFLAGS': '-arch ppc -arch i386 -g',
136n/a 'CPPFLAGS': '-I. -isysroot /Developer/SDKs/MacOSX10.4u.sdk',
137n/a 'BLDSHARED': 'clang -bundle -arch ppc -arch i386 -g',
138n/a 'LDSHARED': 'clang -bundle -arch ppc -arch i386 '
139n/a '-isysroot /Developer/SDKs/MacOSX10.4u.sdk -g',
140n/a }
141n/a self.add_expected_saved_initial_values(config_vars, expected_vars)
142n/a
143n/a suffix = (':' + self.env['PATH']) if self.env['PATH'] else ''
144n/a self.env['PATH'] = os.path.abspath(self.temp_path_dir) + suffix
145n/a for c_name, c_output in compilers:
146n/a test.support.unlink(c_name)
147n/a self.addCleanup(test.support.unlink, c_name)
148n/a with open(c_name, 'w') as f:
149n/a f.write("#!/bin/sh\n/bin/echo " + c_output)
150n/a os.chmod(c_name, stat.S_IRWXU)
151n/a self.assertEqual(expected_vars,
152n/a _osx_support._find_appropriate_compiler(
153n/a config_vars))
154n/a
155n/a def test__remove_universal_flags(self):
156n/a config_vars = {
157n/a 'CFLAGS': '-fno-strict-aliasing -g -O3 -arch ppc -arch i386 ',
158n/a 'LDFLAGS': '-arch ppc -arch i386 -g',
159n/a 'CPPFLAGS': '-I. -isysroot /Developer/SDKs/MacOSX10.4u.sdk',
160n/a 'BLDSHARED': 'gcc-4.0 -bundle -arch ppc -arch i386 -g',
161n/a 'LDSHARED': 'gcc-4.0 -bundle -arch ppc -arch i386 '
162n/a '-isysroot /Developer/SDKs/MacOSX10.4u.sdk -g',
163n/a }
164n/a expected_vars = {
165n/a 'CFLAGS': '-fno-strict-aliasing -g -O3 ',
166n/a 'LDFLAGS': ' -g',
167n/a 'CPPFLAGS': '-I. ',
168n/a 'BLDSHARED': 'gcc-4.0 -bundle -g',
169n/a 'LDSHARED': 'gcc-4.0 -bundle -g',
170n/a }
171n/a self.add_expected_saved_initial_values(config_vars, expected_vars)
172n/a
173n/a self.assertEqual(expected_vars,
174n/a _osx_support._remove_universal_flags(
175n/a config_vars))
176n/a
177n/a def test__remove_unsupported_archs(self):
178n/a config_vars = {
179n/a 'CC': 'clang',
180n/a 'CFLAGS': '-fno-strict-aliasing -g -O3 -arch ppc -arch i386 ',
181n/a 'LDFLAGS': '-arch ppc -arch i386 -g',
182n/a 'CPPFLAGS': '-I. -isysroot /Developer/SDKs/MacOSX10.4u.sdk',
183n/a 'BLDSHARED': 'gcc-4.0 -bundle -arch ppc -arch i386 -g',
184n/a 'LDSHARED': 'gcc-4.0 -bundle -arch ppc -arch i386 '
185n/a '-isysroot /Developer/SDKs/MacOSX10.4u.sdk -g',
186n/a }
187n/a expected_vars = {
188n/a 'CC': 'clang',
189n/a 'CFLAGS': '-fno-strict-aliasing -g -O3 -arch i386 ',
190n/a 'LDFLAGS': ' -arch i386 -g',
191n/a 'CPPFLAGS': '-I. -isysroot /Developer/SDKs/MacOSX10.4u.sdk',
192n/a 'BLDSHARED': 'gcc-4.0 -bundle -arch i386 -g',
193n/a 'LDSHARED': 'gcc-4.0 -bundle -arch i386 '
194n/a '-isysroot /Developer/SDKs/MacOSX10.4u.sdk -g',
195n/a }
196n/a self.add_expected_saved_initial_values(config_vars, expected_vars)
197n/a
198n/a suffix = (':' + self.env['PATH']) if self.env['PATH'] else ''
199n/a self.env['PATH'] = os.path.abspath(self.temp_path_dir) + suffix
200n/a c_name = 'clang'
201n/a test.support.unlink(c_name)
202n/a self.addCleanup(test.support.unlink, c_name)
203n/a # exit status 255 means no PPC support in this compiler chain
204n/a with open(c_name, 'w') as f:
205n/a f.write("#!/bin/sh\nexit 255")
206n/a os.chmod(c_name, stat.S_IRWXU)
207n/a self.assertEqual(expected_vars,
208n/a _osx_support._remove_unsupported_archs(
209n/a config_vars))
210n/a
211n/a def test__override_all_archs(self):
212n/a self.env['ARCHFLAGS'] = '-arch x86_64'
213n/a config_vars = {
214n/a 'CC': 'clang',
215n/a 'CFLAGS': '-fno-strict-aliasing -g -O3 -arch ppc -arch i386 ',
216n/a 'LDFLAGS': '-arch ppc -arch i386 -g',
217n/a 'CPPFLAGS': '-I. -isysroot /Developer/SDKs/MacOSX10.4u.sdk',
218n/a 'BLDSHARED': 'gcc-4.0 -bundle -arch ppc -arch i386 -g',
219n/a 'LDSHARED': 'gcc-4.0 -bundle -arch ppc -arch i386 '
220n/a '-isysroot /Developer/SDKs/MacOSX10.4u.sdk -g',
221n/a }
222n/a expected_vars = {
223n/a 'CC': 'clang',
224n/a 'CFLAGS': '-fno-strict-aliasing -g -O3 -arch x86_64',
225n/a 'LDFLAGS': ' -g -arch x86_64',
226n/a 'CPPFLAGS': '-I. -isysroot /Developer/SDKs/MacOSX10.4u.sdk',
227n/a 'BLDSHARED': 'gcc-4.0 -bundle -g -arch x86_64',
228n/a 'LDSHARED': 'gcc-4.0 -bundle -isysroot '
229n/a '/Developer/SDKs/MacOSX10.4u.sdk -g -arch x86_64',
230n/a }
231n/a self.add_expected_saved_initial_values(config_vars, expected_vars)
232n/a
233n/a self.assertEqual(expected_vars,
234n/a _osx_support._override_all_archs(
235n/a config_vars))
236n/a
237n/a def test__check_for_unavailable_sdk(self):
238n/a config_vars = {
239n/a 'CC': 'clang',
240n/a 'CFLAGS': '-fno-strict-aliasing -g -O3 -arch ppc -arch i386 '
241n/a '-isysroot /Developer/SDKs/MacOSX10.1.sdk',
242n/a 'LDFLAGS': '-arch ppc -arch i386 -g',
243n/a 'CPPFLAGS': '-I. -isysroot /Developer/SDKs/MacOSX10.1.sdk',
244n/a 'BLDSHARED': 'gcc-4.0 -bundle -arch ppc -arch i386 -g',
245n/a 'LDSHARED': 'gcc-4.0 -bundle -arch ppc -arch i386 '
246n/a '-isysroot /Developer/SDKs/MacOSX10.1.sdk -g',
247n/a }
248n/a expected_vars = {
249n/a 'CC': 'clang',
250n/a 'CFLAGS': '-fno-strict-aliasing -g -O3 -arch ppc -arch i386 '
251n/a ' ',
252n/a 'LDFLAGS': '-arch ppc -arch i386 -g',
253n/a 'CPPFLAGS': '-I. ',
254n/a 'BLDSHARED': 'gcc-4.0 -bundle -arch ppc -arch i386 -g',
255n/a 'LDSHARED': 'gcc-4.0 -bundle -arch ppc -arch i386 '
256n/a ' -g',
257n/a }
258n/a self.add_expected_saved_initial_values(config_vars, expected_vars)
259n/a
260n/a self.assertEqual(expected_vars,
261n/a _osx_support._check_for_unavailable_sdk(
262n/a config_vars))
263n/a
264n/a def test_get_platform_osx(self):
265n/a # Note, get_platform_osx is currently tested more extensively
266n/a # indirectly by test_sysconfig and test_distutils
267n/a config_vars = {
268n/a 'CFLAGS': '-fno-strict-aliasing -g -O3 -arch ppc -arch i386 '
269n/a '-isysroot /Developer/SDKs/MacOSX10.1.sdk',
270n/a 'MACOSX_DEPLOYMENT_TARGET': '10.6',
271n/a }
272n/a result = _osx_support.get_platform_osx(config_vars, ' ', ' ', ' ')
273n/a self.assertEqual(('macosx', '10.6', 'fat'), result)
274n/a
275n/aif __name__ == "__main__":
276n/a unittest.main()