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