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