Tarek Ziadé | dd9f65f | 2009-05-06 07:26:24 +0000 | [diff] [blame] | 1 | """Tests for distutils.command.build_clib.""" |
| 2 | import unittest |
Tarek Ziadé | 5662d3e | 2009-05-07 21:24:43 +0000 | [diff] [blame] | 3 | import os |
| 4 | import sys |
Tarek Ziadé | dd9f65f | 2009-05-06 07:26:24 +0000 | [diff] [blame] | 5 | |
Éric Araujo | 70ec44a | 2010-11-06 02:44:43 +0000 | [diff] [blame] | 6 | from test.support import run_unittest |
| 7 | |
Tarek Ziadé | dd9f65f | 2009-05-06 07:26:24 +0000 | [diff] [blame] | 8 | from distutils.command.build_clib import build_clib |
| 9 | from distutils.errors import DistutilsSetupError |
| 10 | from distutils.tests import support |
Tarek Ziadé | 5662d3e | 2009-05-07 21:24:43 +0000 | [diff] [blame] | 11 | from distutils.spawn import find_executable |
Tarek Ziadé | dd9f65f | 2009-05-06 07:26:24 +0000 | [diff] [blame] | 12 | |
| 13 | class 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é | 0e53397 | 2009-05-06 08:08:26 +0000 | [diff] [blame] | 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.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é | dd9f65f | 2009-05-06 07:26:24 +0000 | [diff] [blame] | 104 | |
Tarek Ziadé | 5662d3e | 2009-05-07 21:24:43 +0000 | [diff] [blame] | 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') |
Tarek Ziadé | 3bd31a2 | 2009-05-11 08:49:17 +0000 | [diff] [blame] | 114 | self.write_file(foo_c, 'int main(void) { return 1;}\n') |
Tarek Ziadé | 5662d3e | 2009-05-07 21:24:43 +0000 | [diff] [blame] | 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 |
Tarek Ziadé | 3679727 | 2010-07-22 12:50:05 +0000 | [diff] [blame] | 125 | from distutils.ccompiler import new_compiler |
| 126 | from distutils.sysconfig import customize_compiler |
Tarek Ziadé | 5662d3e | 2009-05-07 21:24:43 +0000 | [diff] [blame] | 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 |
Benjamin Peterson | c9c0f20 | 2009-06-30 23:06:06 +0000 | [diff] [blame] | 140 | self.assertTrue('libfoo.a' in os.listdir(build_temp)) |
Tarek Ziadé | 5662d3e | 2009-05-07 21:24:43 +0000 | [diff] [blame] | 141 | |
Tarek Ziadé | dd9f65f | 2009-05-06 07:26:24 +0000 | [diff] [blame] | 142 | def test_suite(): |
| 143 | return unittest.makeSuite(BuildCLibTestCase) |
| 144 | |
| 145 | if __name__ == "__main__": |
Éric Araujo | 70ec44a | 2010-11-06 02:44:43 +0000 | [diff] [blame] | 146 | run_unittest(test_suite()) |