| 
									
										
										
										
											2009-05-06 07:26:24 +00:00
										 |  |  | """Tests for distutils.command.build_clib.""" | 
					
						
							|  |  |  | import unittest | 
					
						
							| 
									
										
										
										
											2009-05-07 21:24:43 +00:00
										 |  |  | import os | 
					
						
							|  |  |  | import sys | 
					
						
							| 
									
										
										
										
											2009-05-06 07:26:24 +00:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2010-11-06 02:44:43 +00:00
										 |  |  | from test.support import run_unittest | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2009-05-06 07:26:24 +00:00
										 |  |  | from distutils.command.build_clib import build_clib | 
					
						
							|  |  |  | from distutils.errors import DistutilsSetupError | 
					
						
							|  |  |  | from distutils.tests import support | 
					
						
							| 
									
										
										
										
											2009-05-07 21:24:43 +00:00
										 |  |  | from distutils.spawn import find_executable | 
					
						
							| 
									
										
										
										
											2009-05-06 07:26:24 +00:00
										 |  |  | 
 | 
					
						
							|  |  |  | class BuildCLibTestCase(support.TempdirManager, | 
					
						
							|  |  |  |                         support.LoggingSilencer, | 
					
						
							|  |  |  |                         unittest.TestCase): | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |     def test_check_library_dist(self): | 
					
						
							|  |  |  |         pkg_dir, dist = self.create_dist() | 
					
						
							|  |  |  |         cmd = build_clib(dist) | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |         # 'libraries' option must be a list | 
					
						
							|  |  |  |         self.assertRaises(DistutilsSetupError, cmd.check_library_list, 'foo') | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |         # each element of 'libraries' must a 2-tuple | 
					
						
							|  |  |  |         self.assertRaises(DistutilsSetupError, cmd.check_library_list, | 
					
						
							|  |  |  |                           ['foo1', 'foo2']) | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |         # first element of each tuple in 'libraries' | 
					
						
							|  |  |  |         # must be a string (the library name) | 
					
						
							|  |  |  |         self.assertRaises(DistutilsSetupError, cmd.check_library_list, | 
					
						
							|  |  |  |                           [(1, 'foo1'), ('name', 'foo2')]) | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |         # library name may not contain directory separators | 
					
						
							|  |  |  |         self.assertRaises(DistutilsSetupError, cmd.check_library_list, | 
					
						
							|  |  |  |                           [('name', 'foo1'), | 
					
						
							|  |  |  |                            ('another/name', 'foo2')]) | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |         # second element of each tuple must be a dictionary (build info) | 
					
						
							|  |  |  |         self.assertRaises(DistutilsSetupError, cmd.check_library_list, | 
					
						
							|  |  |  |                           [('name', {}), | 
					
						
							|  |  |  |                            ('another', 'foo2')]) | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |         # those work | 
					
						
							|  |  |  |         libs = [('name', {}), ('name', {'ok': 'good'})] | 
					
						
							|  |  |  |         cmd.check_library_list(libs) | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2009-05-06 08:08:26 +00:00
										 |  |  |     def test_get_source_files(self): | 
					
						
							|  |  |  |         pkg_dir, dist = self.create_dist() | 
					
						
							|  |  |  |         cmd = build_clib(dist) | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |         # "in 'libraries' option 'sources' must be present and must be | 
					
						
							|  |  |  |         # a list of source filenames | 
					
						
							|  |  |  |         cmd.libraries = [('name', {})] | 
					
						
							|  |  |  |         self.assertRaises(DistutilsSetupError, cmd.get_source_files) | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |         cmd.libraries = [('name', {'sources': 1})] | 
					
						
							|  |  |  |         self.assertRaises(DistutilsSetupError, cmd.get_source_files) | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |         cmd.libraries = [('name', {'sources': ['a', 'b']})] | 
					
						
							| 
									
										
										
										
											2010-11-20 19:04:17 +00:00
										 |  |  |         self.assertEqual(cmd.get_source_files(), ['a', 'b']) | 
					
						
							| 
									
										
										
										
											2009-05-06 08:08:26 +00:00
										 |  |  | 
 | 
					
						
							|  |  |  |         cmd.libraries = [('name', {'sources': ('a', 'b')})] | 
					
						
							| 
									
										
										
										
											2010-11-20 19:04:17 +00:00
										 |  |  |         self.assertEqual(cmd.get_source_files(), ['a', 'b']) | 
					
						
							| 
									
										
										
										
											2009-05-06 08:08:26 +00:00
										 |  |  | 
 | 
					
						
							|  |  |  |         cmd.libraries = [('name', {'sources': ('a', 'b')}), | 
					
						
							|  |  |  |                          ('name2', {'sources': ['c', 'd']})] | 
					
						
							| 
									
										
										
										
											2010-11-20 19:04:17 +00:00
										 |  |  |         self.assertEqual(cmd.get_source_files(), ['a', 'b', 'c', 'd']) | 
					
						
							| 
									
										
										
										
											2009-05-06 08:08:26 +00:00
										 |  |  | 
 | 
					
						
							|  |  |  |     def test_build_libraries(self): | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |         pkg_dir, dist = self.create_dist() | 
					
						
							|  |  |  |         cmd = build_clib(dist) | 
					
						
							|  |  |  |         class FakeCompiler: | 
					
						
							|  |  |  |             def compile(*args, **kw): | 
					
						
							|  |  |  |                 pass | 
					
						
							|  |  |  |             create_static_lib = compile | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |         cmd.compiler = FakeCompiler() | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2013-08-17 16:11:40 +03:00
										 |  |  |         # build_libraries is also doing a bit of typo checking | 
					
						
							| 
									
										
										
										
											2009-05-06 08:08:26 +00:00
										 |  |  |         lib = [('name', {'sources': 'notvalid'})] | 
					
						
							|  |  |  |         self.assertRaises(DistutilsSetupError, cmd.build_libraries, lib) | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |         lib = [('name', {'sources': list()})] | 
					
						
							|  |  |  |         cmd.build_libraries(lib) | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |         lib = [('name', {'sources': tuple()})] | 
					
						
							|  |  |  |         cmd.build_libraries(lib) | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |     def test_finalize_options(self): | 
					
						
							|  |  |  |         pkg_dir, dist = self.create_dist() | 
					
						
							|  |  |  |         cmd = build_clib(dist) | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |         cmd.include_dirs = 'one-dir' | 
					
						
							|  |  |  |         cmd.finalize_options() | 
					
						
							| 
									
										
										
										
											2010-11-20 19:04:17 +00:00
										 |  |  |         self.assertEqual(cmd.include_dirs, ['one-dir']) | 
					
						
							| 
									
										
										
										
											2009-05-06 08:08:26 +00:00
										 |  |  | 
 | 
					
						
							|  |  |  |         cmd.include_dirs = None | 
					
						
							|  |  |  |         cmd.finalize_options() | 
					
						
							| 
									
										
										
										
											2010-11-20 19:04:17 +00:00
										 |  |  |         self.assertEqual(cmd.include_dirs, []) | 
					
						
							| 
									
										
										
										
											2009-05-06 08:08:26 +00:00
										 |  |  | 
 | 
					
						
							|  |  |  |         cmd.distribution.libraries = 'WONTWORK' | 
					
						
							|  |  |  |         self.assertRaises(DistutilsSetupError, cmd.finalize_options) | 
					
						
							| 
									
										
										
										
											2009-05-06 07:26:24 +00:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2013-12-18 16:41:01 +02:00
										 |  |  |     @unittest.skipIf(sys.platform == 'win32', "can't test on Windows") | 
					
						
							| 
									
										
										
										
											2009-05-07 21:24:43 +00:00
										 |  |  |     def test_run(self): | 
					
						
							|  |  |  |         pkg_dir, dist = self.create_dist() | 
					
						
							|  |  |  |         cmd = build_clib(dist) | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |         foo_c = os.path.join(pkg_dir, 'foo.c') | 
					
						
							| 
									
										
										
										
											2009-05-11 08:49:17 +00:00
										 |  |  |         self.write_file(foo_c, 'int main(void) { return 1;}\n') | 
					
						
							| 
									
										
										
										
											2009-05-07 21:24:43 +00:00
										 |  |  |         cmd.libraries = [('foo', {'sources': [foo_c]})] | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |         build_temp = os.path.join(pkg_dir, 'build') | 
					
						
							|  |  |  |         os.mkdir(build_temp) | 
					
						
							|  |  |  |         cmd.build_temp = build_temp | 
					
						
							|  |  |  |         cmd.build_clib = build_temp | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |         # before we run the command, we want to make sure | 
					
						
							|  |  |  |         # all commands are present on the system | 
					
						
							|  |  |  |         # by creating a compiler and checking its executables | 
					
						
							| 
									
										
										
										
											2010-07-22 12:50:05 +00:00
										 |  |  |         from distutils.ccompiler import new_compiler | 
					
						
							|  |  |  |         from distutils.sysconfig import customize_compiler | 
					
						
							| 
									
										
										
										
											2009-05-07 21:24:43 +00:00
										 |  |  | 
 | 
					
						
							|  |  |  |         compiler = new_compiler() | 
					
						
							|  |  |  |         customize_compiler(compiler) | 
					
						
							|  |  |  |         for ccmd in compiler.executables.values(): | 
					
						
							|  |  |  |             if ccmd is None: | 
					
						
							|  |  |  |                 continue | 
					
						
							|  |  |  |             if find_executable(ccmd[0]) is None: | 
					
						
							| 
									
										
										
										
											2013-12-18 16:41:01 +02:00
										 |  |  |                 self.skipTest('The %r command is not found' % ccmd[0]) | 
					
						
							| 
									
										
										
										
											2009-05-07 21:24:43 +00:00
										 |  |  | 
 | 
					
						
							|  |  |  |         # this should work | 
					
						
							|  |  |  |         cmd.run() | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |         # let's check the result | 
					
						
							| 
									
										
										
										
											2013-11-17 00:17:46 +02:00
										 |  |  |         self.assertIn('libfoo.a', os.listdir(build_temp)) | 
					
						
							| 
									
										
										
										
											2009-05-07 21:24:43 +00:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2009-05-06 07:26:24 +00:00
										 |  |  | def test_suite(): | 
					
						
							|  |  |  |     return unittest.makeSuite(BuildCLibTestCase) | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | if __name__ == "__main__": | 
					
						
							| 
									
										
										
										
											2010-11-06 02:44:43 +00:00
										 |  |  |     run_unittest(test_suite()) |