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 | |
| 6 | from distutils.command.build_clib import build_clib |
| 7 | from distutils.errors import DistutilsSetupError |
| 8 | from distutils.tests import support |
Tarek Ziadé | 5662d3e | 2009-05-07 21:24:43 +0000 | [diff] [blame] | 9 | from distutils.spawn import find_executable |
Tarek Ziadé | dd9f65f | 2009-05-06 07:26:24 +0000 | [diff] [blame] | 10 | |
| 11 | class BuildCLibTestCase(support.TempdirManager, |
| 12 | support.LoggingSilencer, |
| 13 | unittest.TestCase): |
| 14 | |
| 15 | def test_check_library_dist(self): |
| 16 | pkg_dir, dist = self.create_dist() |
| 17 | cmd = build_clib(dist) |
| 18 | |
| 19 | # 'libraries' option must be a list |
| 20 | self.assertRaises(DistutilsSetupError, cmd.check_library_list, 'foo') |
| 21 | |
| 22 | # each element of 'libraries' must a 2-tuple |
| 23 | self.assertRaises(DistutilsSetupError, cmd.check_library_list, |
| 24 | ['foo1', 'foo2']) |
| 25 | |
| 26 | # first element of each tuple in 'libraries' |
| 27 | # must be a string (the library name) |
| 28 | self.assertRaises(DistutilsSetupError, cmd.check_library_list, |
| 29 | [(1, 'foo1'), ('name', 'foo2')]) |
| 30 | |
| 31 | # library name may not contain directory separators |
| 32 | self.assertRaises(DistutilsSetupError, cmd.check_library_list, |
| 33 | [('name', 'foo1'), |
| 34 | ('another/name', 'foo2')]) |
| 35 | |
| 36 | # second element of each tuple must be a dictionary (build info) |
| 37 | self.assertRaises(DistutilsSetupError, cmd.check_library_list, |
| 38 | [('name', {}), |
| 39 | ('another', 'foo2')]) |
| 40 | |
| 41 | # those work |
| 42 | libs = [('name', {}), ('name', {'ok': 'good'})] |
| 43 | cmd.check_library_list(libs) |
| 44 | |
Tarek Ziadé | 0e53397 | 2009-05-06 08:08:26 +0000 | [diff] [blame] | 45 | def test_get_source_files(self): |
| 46 | pkg_dir, dist = self.create_dist() |
| 47 | cmd = build_clib(dist) |
| 48 | |
| 49 | # "in 'libraries' option 'sources' must be present and must be |
| 50 | # a list of source filenames |
| 51 | cmd.libraries = [('name', {})] |
| 52 | self.assertRaises(DistutilsSetupError, cmd.get_source_files) |
| 53 | |
| 54 | cmd.libraries = [('name', {'sources': 1})] |
| 55 | self.assertRaises(DistutilsSetupError, cmd.get_source_files) |
| 56 | |
| 57 | cmd.libraries = [('name', {'sources': ['a', 'b']})] |
| 58 | self.assertEquals(cmd.get_source_files(), ['a', 'b']) |
| 59 | |
| 60 | cmd.libraries = [('name', {'sources': ('a', 'b')})] |
| 61 | self.assertEquals(cmd.get_source_files(), ['a', 'b']) |
| 62 | |
| 63 | cmd.libraries = [('name', {'sources': ('a', 'b')}), |
| 64 | ('name2', {'sources': ['c', 'd']})] |
| 65 | self.assertEquals(cmd.get_source_files(), ['a', 'b', 'c', 'd']) |
| 66 | |
| 67 | def test_build_libraries(self): |
| 68 | |
| 69 | pkg_dir, dist = self.create_dist() |
| 70 | cmd = build_clib(dist) |
| 71 | class FakeCompiler: |
| 72 | def compile(*args, **kw): |
| 73 | pass |
| 74 | create_static_lib = compile |
| 75 | |
| 76 | cmd.compiler = FakeCompiler() |
| 77 | |
| 78 | # build_libraries is also doing a bit of typoe checking |
| 79 | lib = [('name', {'sources': 'notvalid'})] |
| 80 | self.assertRaises(DistutilsSetupError, cmd.build_libraries, lib) |
| 81 | |
| 82 | lib = [('name', {'sources': list()})] |
| 83 | cmd.build_libraries(lib) |
| 84 | |
| 85 | lib = [('name', {'sources': tuple()})] |
| 86 | cmd.build_libraries(lib) |
| 87 | |
| 88 | def test_finalize_options(self): |
| 89 | pkg_dir, dist = self.create_dist() |
| 90 | cmd = build_clib(dist) |
| 91 | |
| 92 | cmd.include_dirs = 'one-dir' |
| 93 | cmd.finalize_options() |
| 94 | self.assertEquals(cmd.include_dirs, ['one-dir']) |
| 95 | |
| 96 | cmd.include_dirs = None |
| 97 | cmd.finalize_options() |
| 98 | self.assertEquals(cmd.include_dirs, []) |
| 99 | |
| 100 | cmd.distribution.libraries = 'WONTWORK' |
| 101 | self.assertRaises(DistutilsSetupError, cmd.finalize_options) |
Tarek Ziadé | dd9f65f | 2009-05-06 07:26:24 +0000 | [diff] [blame] | 102 | |
Tarek Ziadé | 5662d3e | 2009-05-07 21:24:43 +0000 | [diff] [blame] | 103 | def test_run(self): |
| 104 | # can't test on windows |
| 105 | if sys.platform == 'win32': |
| 106 | return |
| 107 | |
| 108 | pkg_dir, dist = self.create_dist() |
| 109 | cmd = build_clib(dist) |
| 110 | |
| 111 | foo_c = os.path.join(pkg_dir, 'foo.c') |
Tarek Ziadé | 3bd31a2 | 2009-05-11 08:49:17 +0000 | [diff] [blame] | 112 | self.write_file(foo_c, 'int main(void) { return 1;}\n') |
Tarek Ziadé | 5662d3e | 2009-05-07 21:24:43 +0000 | [diff] [blame] | 113 | cmd.libraries = [('foo', {'sources': [foo_c]})] |
| 114 | |
| 115 | build_temp = os.path.join(pkg_dir, 'build') |
| 116 | os.mkdir(build_temp) |
| 117 | cmd.build_temp = build_temp |
| 118 | cmd.build_clib = build_temp |
| 119 | |
| 120 | # before we run the command, we want to make sure |
| 121 | # all commands are present on the system |
| 122 | # by creating a compiler and checking its executables |
Tarek Ziadé | 3679727 | 2010-07-22 12:50:05 +0000 | [diff] [blame^] | 123 | from distutils.ccompiler import new_compiler |
| 124 | from distutils.sysconfig import customize_compiler |
Tarek Ziadé | 5662d3e | 2009-05-07 21:24:43 +0000 | [diff] [blame] | 125 | |
| 126 | compiler = new_compiler() |
| 127 | customize_compiler(compiler) |
| 128 | for ccmd in compiler.executables.values(): |
| 129 | if ccmd is None: |
| 130 | continue |
| 131 | if find_executable(ccmd[0]) is None: |
| 132 | return # can't test |
| 133 | |
| 134 | # this should work |
| 135 | cmd.run() |
| 136 | |
| 137 | # let's check the result |
Benjamin Peterson | c9c0f20 | 2009-06-30 23:06:06 +0000 | [diff] [blame] | 138 | self.assertTrue('libfoo.a' in os.listdir(build_temp)) |
Tarek Ziadé | 5662d3e | 2009-05-07 21:24:43 +0000 | [diff] [blame] | 139 | |
Tarek Ziadé | dd9f65f | 2009-05-06 07:26:24 +0000 | [diff] [blame] | 140 | def test_suite(): |
| 141 | return unittest.makeSuite(BuildCLibTestCase) |
| 142 | |
| 143 | if __name__ == "__main__": |
| 144 | unittest.main(defaultTest="test_suite") |