blob: d4a1e693ce71ec4f13f27526cca33f8f556eda20 [file] [log] [blame]
Tarek Ziadéd2bb1a52009-05-06 07:17:52 +00001"""Tests for distutils.command.build_clib."""
2import unittest
Tarek Ziadé99f660a2009-05-07 21:20:34 +00003import os
4import sys
Tarek Ziadéd2bb1a52009-05-06 07:17:52 +00005
Éric Araujo54274ad2011-02-03 00:12:18 +00006from test.test_support import run_unittest
7
Tarek Ziadéd2bb1a52009-05-06 07:17:52 +00008from distutils.command.build_clib import build_clib
9from distutils.errors import DistutilsSetupError
10from distutils.tests import support
Tarek Ziadé99f660a2009-05-07 21:20:34 +000011from distutils.spawn import find_executable
Tarek Ziadéd2bb1a52009-05-06 07:17:52 +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éccf608c2009-05-06 08:05:47 +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']})]
Ezio Melotti2623a372010-11-21 13:34:58 +000060 self.assertEqual(cmd.get_source_files(), ['a', 'b'])
Tarek Ziadéccf608c2009-05-06 08:05:47 +000061
62 cmd.libraries = [('name', {'sources': ('a', 'b')})]
Ezio Melotti2623a372010-11-21 13:34:58 +000063 self.assertEqual(cmd.get_source_files(), ['a', 'b'])
Tarek Ziadéccf608c2009-05-06 08:05:47 +000064
65 cmd.libraries = [('name', {'sources': ('a', 'b')}),
66 ('name2', {'sources': ['c', 'd']})]
Ezio Melotti2623a372010-11-21 13:34:58 +000067 self.assertEqual(cmd.get_source_files(), ['a', 'b', 'c', 'd'])
Tarek Ziadéccf608c2009-05-06 08:05:47 +000068
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
Ezio Melotti5dd99eb2013-08-17 16:07:38 +030080 # build_libraries is also doing a bit of typo checking
Tarek Ziadéccf608c2009-05-06 08:05:47 +000081 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()
Ezio Melotti2623a372010-11-21 13:34:58 +000096 self.assertEqual(cmd.include_dirs, ['one-dir'])
Tarek Ziadéccf608c2009-05-06 08:05:47 +000097
98 cmd.include_dirs = None
99 cmd.finalize_options()
Ezio Melotti2623a372010-11-21 13:34:58 +0000100 self.assertEqual(cmd.include_dirs, [])
Tarek Ziadéccf608c2009-05-06 08:05:47 +0000101
102 cmd.distribution.libraries = 'WONTWORK'
103 self.assertRaises(DistutilsSetupError, cmd.finalize_options)
Tarek Ziadéd2bb1a52009-05-06 07:17:52 +0000104
Serhiy Storchaka57bc6da2013-12-18 16:45:37 +0200105 @unittest.skipIf(sys.platform == 'win32', "can't test on Windows")
Tarek Ziadé99f660a2009-05-07 21:20:34 +0000106 def test_run(self):
Tarek Ziadé99f660a2009-05-07 21:20:34 +0000107 pkg_dir, dist = self.create_dist()
108 cmd = build_clib(dist)
109
110 foo_c = os.path.join(pkg_dir, 'foo.c')
Tarek Ziadé68e27eb2009-05-11 08:45:17 +0000111 self.write_file(foo_c, 'int main(void) { return 1;}\n')
Tarek Ziadé99f660a2009-05-07 21:20:34 +0000112 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
Ned Deilyc47a4592012-02-11 20:40:24 +0100122 from distutils.ccompiler import new_compiler
123 from distutils.sysconfig import customize_compiler
Tarek Ziadé99f660a2009-05-07 21:20:34 +0000124
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:
Serhiy Storchaka57bc6da2013-12-18 16:45:37 +0200131 self.skipTest('The %r command is not found' % ccmd[0])
Tarek Ziadé99f660a2009-05-07 21:20:34 +0000132
133 # this should work
134 cmd.run()
135
136 # let's check the result
Serhiy Storchaka25a23ef2013-11-17 00:29:27 +0200137 self.assertIn('libfoo.a', os.listdir(build_temp))
Tarek Ziadé99f660a2009-05-07 21:20:34 +0000138
Tarek Ziadéd2bb1a52009-05-06 07:17:52 +0000139def test_suite():
140 return unittest.makeSuite(BuildCLibTestCase)
141
142if __name__ == "__main__":
Éric Araujo54274ad2011-02-03 00:12:18 +0000143 run_unittest(test_suite())