• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
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 typo 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    @unittest.skipIf(sys.platform == 'win32', "can't test on Windows")
106    def test_run(self):
107        pkg_dir, dist = self.create_dist()
108        cmd = build_clib(dist)
109
110        foo_c = os.path.join(pkg_dir, 'foo.c')
111        self.write_file(foo_c, 'int main(void) { return 1;}\n')
112        cmd.libraries = [('foo', {'sources': [foo_c]})]
113
114        build_temp = os.path.join(pkg_dir, 'build')
115        os.mkdir(build_temp)
116        cmd.build_temp = build_temp
117        cmd.build_clib = build_temp
118
119        # before we run the command, we want to make sure
120        # all commands are present on the system
121        # by creating a compiler and checking its executables
122        from distutils.ccompiler import new_compiler
123        from distutils.sysconfig import customize_compiler
124
125        compiler = new_compiler()
126        customize_compiler(compiler)
127        for ccmd in compiler.executables.values():
128            if ccmd is None:
129                continue
130            if find_executable(ccmd[0]) is None:
131                self.skipTest('The %r command is not found' % ccmd[0])
132
133        # this should work
134        cmd.run()
135
136        # let's check the result
137        self.assertIn('libfoo.a', os.listdir(build_temp))
138
139def test_suite():
140    return unittest.makeSuite(BuildCLibTestCase)
141
142if __name__ == "__main__":
143    run_unittest(test_suite())
144