1"""Tests for distutils.command.build_clib."""
2import unittest
3import os
4import sys
5
6from test.test_support import run_unittest
7
8from distutils.command.build_clib import build_clib
9from distutils.errors import DistutilsSetupError
10from distutils.tests import support
11from distutils.spawn import find_executable
12
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
47    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.assertEqual(cmd.get_source_files(), ['a', 'b'])
61
62        cmd.libraries = [('name', {'sources': ('a', 'b')})]
63        self.assertEqual(cmd.get_source_files(), ['a', 'b'])
64
65        cmd.libraries = [('name', {'sources': ('a', 'b')}),
66                         ('name2', {'sources': ['c', 'd']})]
67        self.assertEqual(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.assertEqual(cmd.include_dirs, ['one-dir'])
97
98        cmd.include_dirs = None
99        cmd.finalize_options()
100        self.assertEqual(cmd.include_dirs, [])
101
102        cmd.distribution.libraries = 'WONTWORK'
103        self.assertRaises(DistutilsSetupError, cmd.finalize_options)
104
105    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')
114        self.write_file(foo_c, 'int main(void) { return 1;}\n')
115        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
125        from distutils.ccompiler import new_compiler
126        from distutils.sysconfig import customize_compiler
127
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
140        self.assertTrue('libfoo.a' in os.listdir(build_temp))
141
142def test_suite():
143    return unittest.makeSuite(BuildCLibTestCase)
144
145if __name__ == "__main__":
146    run_unittest(test_suite())
147