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