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 @unittest.skipUnless(get_default_compiler() == 'unix', 59 'not testing if default compiler is not unix') 60 def test_customize_compiler(self): 61 os.environ['AR'] = 'my_ar' 62 os.environ['ARFLAGS'] = '-arflags' 63 64 # make sure AR gets caught 65 class compiler: 66 compiler_type = 'unix' 67 68 def set_executables(self, **kw): 69 self.exes = kw 70 71 comp = compiler() 72 customize_compiler(comp) 73 self.assertEqual(comp.exes['archiver'], 'my_ar -arflags') 74 75 def test_suite(): 76 return unittest.makeSuite(CCompilerTestCase) 77 78 if __name__ == "__main__": 79 unittest.main(defaultTest="test_suite") 80