blob: e59b8f9a19325a36e2d08adc53b8c6efa80b9749 [file] [log] [blame]
Tarek Ziadédd9f65f2009-05-06 07:26:24 +00001"""Tests for distutils.command.build_clib."""
2import unittest
Tarek Ziadé5662d3e2009-05-07 21:24:43 +00003import os
4import sys
Tarek Ziadédd9f65f2009-05-06 07:26:24 +00005
Éric Araujo70ec44a2010-11-06 02:44:43 +00006from test.support import run_unittest
7
Tarek Ziadédd9f65f2009-05-06 07:26:24 +00008from distutils.command.build_clib import build_clib
9from distutils.errors import DistutilsSetupError
10from distutils.tests import support
Tarek Ziadé5662d3e2009-05-07 21:24:43 +000011from distutils.spawn import find_executable
Tarek Ziadédd9f65f2009-05-06 07:26:24 +000012
13class BuildCLibTestCase(support.TempdirManager,
14 support.LoggingSilencer,
15 unittest.TestCase):
16
17 def test_check_library_dist(self):
18 pkg_dir, dist = self.create_dist()
19 cmd = build_clib(dist)
20
21 # 'libraries' option must be a list
22 self.assertRaises(DistutilsSetupError, cmd.check_library_list, 'foo')
23
24 # each element of 'libraries' must a 2-tuple
25 self.assertRaises(DistutilsSetupError, cmd.check_library_list,
26 ['foo1', 'foo2'])
27
28 # first element of each tuple in 'libraries'
29 # must be a string (the library name)
30 self.assertRaises(DistutilsSetupError, cmd.check_library_list,
31 [(1, 'foo1'), ('name', 'foo2')])
32
33 # library name may not contain directory separators
34 self.assertRaises(DistutilsSetupError, cmd.check_library_list,
35 [('name', 'foo1'),
36 ('another/name', 'foo2')])
37
38 # second element of each tuple must be a dictionary (build info)
39 self.assertRaises(DistutilsSetupError, cmd.check_library_list,
40 [('name', {}),
41 ('another', 'foo2')])
42
43 # those work
44 libs = [('name', {}), ('name', {'ok': 'good'})]
45 cmd.check_library_list(libs)
46
Tarek Ziadé0e533972009-05-06 08:08:26 +000047 def test_get_source_files(self):
48 pkg_dir, dist = self.create_dist()
49 cmd = build_clib(dist)
50
51 # "in 'libraries' option 'sources' must be present and must be
52 # a list of source filenames
53 cmd.libraries = [('name', {})]
54 self.assertRaises(DistutilsSetupError, cmd.get_source_files)
55
56 cmd.libraries = [('name', {'sources': 1})]
57 self.assertRaises(DistutilsSetupError, cmd.get_source_files)
58
59 cmd.libraries = [('name', {'sources': ['a', 'b']})]
60 self.assertEquals(cmd.get_source_files(), ['a', 'b'])
61
62 cmd.libraries = [('name', {'sources': ('a', 'b')})]
63 self.assertEquals(cmd.get_source_files(), ['a', 'b'])
64
65 cmd.libraries = [('name', {'sources': ('a', 'b')}),
66 ('name2', {'sources': ['c', 'd']})]
67 self.assertEquals(cmd.get_source_files(), ['a', 'b', 'c', 'd'])
68
69 def test_build_libraries(self):
70
71 pkg_dir, dist = self.create_dist()
72 cmd = build_clib(dist)
73 class FakeCompiler:
74 def compile(*args, **kw):
75 pass
76 create_static_lib = compile
77
78 cmd.compiler = FakeCompiler()
79
80 # build_libraries is also doing a bit of typoe checking
81 lib = [('name', {'sources': 'notvalid'})]
82 self.assertRaises(DistutilsSetupError, cmd.build_libraries, lib)
83
84 lib = [('name', {'sources': list()})]
85 cmd.build_libraries(lib)
86
87 lib = [('name', {'sources': tuple()})]
88 cmd.build_libraries(lib)
89
90 def test_finalize_options(self):
91 pkg_dir, dist = self.create_dist()
92 cmd = build_clib(dist)
93
94 cmd.include_dirs = 'one-dir'
95 cmd.finalize_options()
96 self.assertEquals(cmd.include_dirs, ['one-dir'])
97
98 cmd.include_dirs = None
99 cmd.finalize_options()
100 self.assertEquals(cmd.include_dirs, [])
101
102 cmd.distribution.libraries = 'WONTWORK'
103 self.assertRaises(DistutilsSetupError, cmd.finalize_options)
Tarek Ziadédd9f65f2009-05-06 07:26:24 +0000104
Tarek Ziadé5662d3e2009-05-07 21:24:43 +0000105 def test_run(self):
106 # can't test on windows
107 if sys.platform == 'win32':
108 return
109
110 pkg_dir, dist = self.create_dist()
111 cmd = build_clib(dist)
112
113 foo_c = os.path.join(pkg_dir, 'foo.c')
Tarek Ziadé3bd31a22009-05-11 08:49:17 +0000114 self.write_file(foo_c, 'int main(void) { return 1;}\n')
Tarek Ziadé5662d3e2009-05-07 21:24:43 +0000115 cmd.libraries = [('foo', {'sources': [foo_c]})]
116
117 build_temp = os.path.join(pkg_dir, 'build')
118 os.mkdir(build_temp)
119 cmd.build_temp = build_temp
120 cmd.build_clib = build_temp
121
122 # before we run the command, we want to make sure
123 # all commands are present on the system
124 # by creating a compiler and checking its executables
Tarek Ziadé36797272010-07-22 12:50:05 +0000125 from distutils.ccompiler import new_compiler
126 from distutils.sysconfig import customize_compiler
Tarek Ziadé5662d3e2009-05-07 21:24:43 +0000127
128 compiler = new_compiler()
129 customize_compiler(compiler)
130 for ccmd in compiler.executables.values():
131 if ccmd is None:
132 continue
133 if find_executable(ccmd[0]) is None:
134 return # can't test
135
136 # this should work
137 cmd.run()
138
139 # let's check the result
Benjamin Petersonc9c0f202009-06-30 23:06:06 +0000140 self.assertTrue('libfoo.a' in os.listdir(build_temp))
Tarek Ziadé5662d3e2009-05-07 21:24:43 +0000141
Tarek Ziadédd9f65f2009-05-06 07:26:24 +0000142def test_suite():
143 return unittest.makeSuite(BuildCLibTestCase)
144
145if __name__ == "__main__":
Éric Araujo70ec44a2010-11-06 02:44:43 +0000146 run_unittest(test_suite())