1"""Tests for distutils.ccompiler.""" 2import os 3import unittest 4from test.test_support import captured_stdout 5 6from distutils.ccompiler import (gen_lib_options, CCompiler, 7 get_default_compiler, customize_compiler) 8from distutils import debug 9from distutils.tests import support 10 11class FakeCompiler(object): 12 def library_dir_option(self, dir): 13 return "-L" + dir 14 15 def runtime_library_dir_option(self, dir): 16 return ["-cool", "-R" + dir] 17 18 def find_library_file(self, dirs, lib, debug=0): 19 return 'found' 20 21 def library_option(self, lib): 22 return "-l" + lib 23 24class CCompilerTestCase(support.EnvironGuard, unittest.TestCase): 25 26 def test_gen_lib_options(self): 27 compiler = FakeCompiler() 28 libdirs = ['lib1', 'lib2'] 29 runlibdirs = ['runlib1'] 30 libs = [os.path.join('dir', 'name'), 'name2'] 31 32 opts = gen_lib_options(compiler, libdirs, runlibdirs, libs) 33 wanted = ['-Llib1', '-Llib2', '-cool', '-Rrunlib1', 'found', 34 '-lname2'] 35 self.assertEqual(opts, wanted) 36 37 def test_debug_print(self): 38 39 class MyCCompiler(CCompiler): 40 executables = {} 41 42 compiler = MyCCompiler() 43 with captured_stdout() as stdout: 44 compiler.debug_print('xxx') 45 stdout.seek(0) 46 self.assertEqual(stdout.read(), '') 47 48 debug.DEBUG = True 49 try: 50 with captured_stdout() as stdout: 51 compiler.debug_print('xxx') 52 stdout.seek(0) 53 self.assertEqual(stdout.read(), 'xxx\n') 54 finally: 55 debug.DEBUG = False 56 57 def test_customize_compiler(self): 58 59 # not testing if default compiler is not unix 60 if get_default_compiler() != 'unix': 61 return 62 63 os.environ['AR'] = 'my_ar' 64 os.environ['ARFLAGS'] = '-arflags' 65 66 # make sure AR gets caught 67 class compiler: 68 compiler_type = 'unix' 69 70 def set_executables(self, **kw): 71 self.exes = kw 72 73 comp = compiler() 74 customize_compiler(comp) 75 self.assertEqual(comp.exes['archiver'], 'my_ar -arflags') 76 77def test_suite(): 78 return unittest.makeSuite(CCompilerTestCase) 79 80if __name__ == "__main__": 81 unittest.main(defaultTest="test_suite") 82