test_config_cmd.py 3.0 KB

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