1"""Tests for distutils.command.config.""" 2import unittest 3import os 4import sys 5from test.support import run_unittest, missing_compiler_executable 6 7from distutils.command.config import dump_file, config 8from distutils.tests import support 9from distutils import log 10 11class ConfigTestCase(support.LoggingSilencer, 12 support.TempdirManager, 13 unittest.TestCase): 14 15 def _info(self, msg, *args): 16 for line in msg.splitlines(): 17 self._logs.append(line) 18 19 def setUp(self): 20 super(ConfigTestCase, self).setUp() 21 self._logs = [] 22 self.old_log = log.info 23 log.info = self._info 24 25 def tearDown(self): 26 log.info = self.old_log 27 super(ConfigTestCase, self).tearDown() 28 29 def test_dump_file(self): 30 this_file = os.path.splitext(__file__)[0] + '.py' 31 f = open(this_file) 32 try: 33 numlines = len(f.readlines()) 34 finally: 35 f.close() 36 37 dump_file(this_file, 'I am the header') 38 self.assertEqual(len(self._logs), numlines+1) 39 40 @unittest.skipIf(sys.platform == 'win32', "can't test on Windows") 41 def test_search_cpp(self): 42 import shutil 43 cmd = missing_compiler_executable(['preprocessor']) 44 if cmd is not None: 45 self.skipTest('The %r command is not found' % cmd) 46 pkg_dir, dist = self.create_dist() 47 cmd = config(dist) 48 cmd._check_compiler() 49 compiler = cmd.compiler 50 if sys.platform[:3] == "aix" and "xlc" in compiler.preprocessor[0].lower(): 51 self.skipTest('xlc: The -E option overrides the -P, -o, and -qsyntaxonly options') 52 53 # simple pattern searches 54 match = cmd.search_cpp(pattern='xxx', body='/* xxx */') 55 self.assertEqual(match, 0) 56 57 match = cmd.search_cpp(pattern='_configtest', body='/* xxx */') 58 self.assertEqual(match, 1) 59 60 def test_finalize_options(self): 61 # finalize_options does a bit of transformation 62 # on options 63 pkg_dir, dist = self.create_dist() 64 cmd = config(dist) 65 cmd.include_dirs = 'one%stwo' % os.pathsep 66 cmd.libraries = 'one' 67 cmd.library_dirs = 'three%sfour' % os.pathsep 68 cmd.ensure_finalized() 69 70 self.assertEqual(cmd.include_dirs, ['one', 'two']) 71 self.assertEqual(cmd.libraries, ['one']) 72 self.assertEqual(cmd.library_dirs, ['three', 'four']) 73 74 def test_clean(self): 75 # _clean removes files 76 tmp_dir = self.mkdtemp() 77 f1 = os.path.join(tmp_dir, 'one') 78 f2 = os.path.join(tmp_dir, 'two') 79 80 self.write_file(f1, 'xxx') 81 self.write_file(f2, 'xxx') 82 83 for f in (f1, f2): 84 self.assertTrue(os.path.exists(f)) 85 86 pkg_dir, dist = self.create_dist() 87 cmd = config(dist) 88 cmd._clean(f1, f2) 89 90 for f in (f1, f2): 91 self.assertFalse(os.path.exists(f)) 92 93def test_suite(): 94 return unittest.makeSuite(ConfigTestCase) 95 96if __name__ == "__main__": 97 run_unittest(test_suite()) 98