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

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

#countcontent
1n/a"""Tests for distutils.command.build_clib."""
2n/aimport unittest
3n/aimport os
4n/aimport sys
5n/a
6n/afrom test.support import run_unittest, missing_compiler_executable
7n/a
8n/afrom distutils.command.build_clib import build_clib
9n/afrom distutils.errors import DistutilsSetupError
10n/afrom distutils.tests import support
11n/afrom distutils.spawn import find_executable
12n/a
13n/aclass BuildCLibTestCase(support.TempdirManager,
14n/a support.LoggingSilencer,
15n/a unittest.TestCase):
16n/a
17n/a def test_check_library_dist(self):
18n/a pkg_dir, dist = self.create_dist()
19n/a cmd = build_clib(dist)
20n/a
21n/a # 'libraries' option must be a list
22n/a self.assertRaises(DistutilsSetupError, cmd.check_library_list, 'foo')
23n/a
24n/a # each element of 'libraries' must a 2-tuple
25n/a self.assertRaises(DistutilsSetupError, cmd.check_library_list,
26n/a ['foo1', 'foo2'])
27n/a
28n/a # first element of each tuple in 'libraries'
29n/a # must be a string (the library name)
30n/a self.assertRaises(DistutilsSetupError, cmd.check_library_list,
31n/a [(1, 'foo1'), ('name', 'foo2')])
32n/a
33n/a # library name may not contain directory separators
34n/a self.assertRaises(DistutilsSetupError, cmd.check_library_list,
35n/a [('name', 'foo1'),
36n/a ('another/name', 'foo2')])
37n/a
38n/a # second element of each tuple must be a dictionary (build info)
39n/a self.assertRaises(DistutilsSetupError, cmd.check_library_list,
40n/a [('name', {}),
41n/a ('another', 'foo2')])
42n/a
43n/a # those work
44n/a libs = [('name', {}), ('name', {'ok': 'good'})]
45n/a cmd.check_library_list(libs)
46n/a
47n/a def test_get_source_files(self):
48n/a pkg_dir, dist = self.create_dist()
49n/a cmd = build_clib(dist)
50n/a
51n/a # "in 'libraries' option 'sources' must be present and must be
52n/a # a list of source filenames
53n/a cmd.libraries = [('name', {})]
54n/a self.assertRaises(DistutilsSetupError, cmd.get_source_files)
55n/a
56n/a cmd.libraries = [('name', {'sources': 1})]
57n/a self.assertRaises(DistutilsSetupError, cmd.get_source_files)
58n/a
59n/a cmd.libraries = [('name', {'sources': ['a', 'b']})]
60n/a self.assertEqual(cmd.get_source_files(), ['a', 'b'])
61n/a
62n/a cmd.libraries = [('name', {'sources': ('a', 'b')})]
63n/a self.assertEqual(cmd.get_source_files(), ['a', 'b'])
64n/a
65n/a cmd.libraries = [('name', {'sources': ('a', 'b')}),
66n/a ('name2', {'sources': ['c', 'd']})]
67n/a self.assertEqual(cmd.get_source_files(), ['a', 'b', 'c', 'd'])
68n/a
69n/a def test_build_libraries(self):
70n/a
71n/a pkg_dir, dist = self.create_dist()
72n/a cmd = build_clib(dist)
73n/a class FakeCompiler:
74n/a def compile(*args, **kw):
75n/a pass
76n/a create_static_lib = compile
77n/a
78n/a cmd.compiler = FakeCompiler()
79n/a
80n/a # build_libraries is also doing a bit of typo checking
81n/a lib = [('name', {'sources': 'notvalid'})]
82n/a self.assertRaises(DistutilsSetupError, cmd.build_libraries, lib)
83n/a
84n/a lib = [('name', {'sources': list()})]
85n/a cmd.build_libraries(lib)
86n/a
87n/a lib = [('name', {'sources': tuple()})]
88n/a cmd.build_libraries(lib)
89n/a
90n/a def test_finalize_options(self):
91n/a pkg_dir, dist = self.create_dist()
92n/a cmd = build_clib(dist)
93n/a
94n/a cmd.include_dirs = 'one-dir'
95n/a cmd.finalize_options()
96n/a self.assertEqual(cmd.include_dirs, ['one-dir'])
97n/a
98n/a cmd.include_dirs = None
99n/a cmd.finalize_options()
100n/a self.assertEqual(cmd.include_dirs, [])
101n/a
102n/a cmd.distribution.libraries = 'WONTWORK'
103n/a self.assertRaises(DistutilsSetupError, cmd.finalize_options)
104n/a
105n/a @unittest.skipIf(sys.platform == 'win32', "can't test on Windows")
106n/a def test_run(self):
107n/a pkg_dir, dist = self.create_dist()
108n/a cmd = build_clib(dist)
109n/a
110n/a foo_c = os.path.join(pkg_dir, 'foo.c')
111n/a self.write_file(foo_c, 'int main(void) { return 1;}\n')
112n/a cmd.libraries = [('foo', {'sources': [foo_c]})]
113n/a
114n/a build_temp = os.path.join(pkg_dir, 'build')
115n/a os.mkdir(build_temp)
116n/a cmd.build_temp = build_temp
117n/a cmd.build_clib = build_temp
118n/a
119n/a # Before we run the command, we want to make sure
120n/a # all commands are present on the system.
121n/a ccmd = missing_compiler_executable()
122n/a if ccmd is not None:
123n/a self.skipTest('The %r command is not found' % ccmd)
124n/a
125n/a # this should work
126n/a cmd.run()
127n/a
128n/a # let's check the result
129n/a self.assertIn('libfoo.a', os.listdir(build_temp))
130n/a
131n/adef test_suite():
132n/a return unittest.makeSuite(BuildCLibTestCase)
133n/a
134n/aif __name__ == "__main__":
135n/a run_unittest(test_suite())