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