test_ccompiler.py (2312B)
1 #Embedded file name: C:\BuildAgent\work\20d73f1b24eed06e\eve\release\V24.01\carbon\common\stdlib\distutils\tests\test_ccompiler.py 2 import os 3 import unittest 4 from test.test_support import captured_stdout 5 from distutils.ccompiler import gen_lib_options, CCompiler, get_default_compiler, customize_compiler 6 from distutils import debug 7 from distutils.tests import support 8 9 class FakeCompiler(object): 10 11 def library_dir_option(self, dir): 12 return '-L' + dir 13 14 def runtime_library_dir_option(self, dir): 15 return ['-cool', '-R' + dir] 16 17 def find_library_file(self, dirs, lib, debug = 0): 18 return 'found' 19 20 def library_option(self, lib): 21 return '-l' + lib 22 23 24 class 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 opts = gen_lib_options(compiler, libdirs, runlibdirs, libs) 32 wanted = ['-Llib1', 33 '-Llib2', 34 '-cool', 35 '-Rrunlib1', 36 'found', 37 '-lname2'] 38 self.assertEqual(opts, wanted) 39 40 def test_debug_print(self): 41 42 class MyCCompiler(CCompiler): 43 executables = {} 44 45 compiler = MyCCompiler() 46 with captured_stdout() as stdout: 47 compiler.debug_print('xxx') 48 stdout.seek(0) 49 self.assertEqual(stdout.read(), '') 50 debug.DEBUG = True 51 try: 52 with captured_stdout() as stdout: 53 compiler.debug_print('xxx') 54 stdout.seek(0) 55 self.assertEqual(stdout.read(), 'xxx\n') 56 finally: 57 debug.DEBUG = False 58 59 def test_customize_compiler(self): 60 if get_default_compiler() != 'unix': 61 return 62 os.environ['AR'] = 'my_ar' 63 os.environ['ARFLAGS'] = '-arflags' 64 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 76 def test_suite(): 77 return unittest.makeSuite(CCompilerTestCase) 78 79 80 if __name__ == '__main__': 81 unittest.main(defaultTest='test_suite')