test_config_cmd.py 2.7 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192
  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. # simple pattern searches
  40. match = cmd.search_cpp(pattern='xxx', body='/* xxx */')
  41. self.assertEqual(match, 0)
  42. match = cmd.search_cpp(pattern='_configtest', body='/* xxx */')
  43. self.assertEqual(match, 1)
  44. def test_finalize_options(self):
  45. # finalize_options does a bit of transformation
  46. # on options
  47. pkg_dir, dist = self.create_dist()
  48. cmd = config(dist)
  49. cmd.include_dirs = 'one%stwo' % os.pathsep
  50. cmd.libraries = 'one'
  51. cmd.library_dirs = 'three%sfour' % os.pathsep
  52. cmd.ensure_finalized()
  53. self.assertEqual(cmd.include_dirs, ['one', 'two'])
  54. self.assertEqual(cmd.libraries, ['one'])
  55. self.assertEqual(cmd.library_dirs, ['three', 'four'])
  56. def test_clean(self):
  57. # _clean removes files
  58. tmp_dir = self.mkdtemp()
  59. f1 = os.path.join(tmp_dir, 'one')
  60. f2 = os.path.join(tmp_dir, 'two')
  61. self.write_file(f1, 'xxx')
  62. self.write_file(f2, 'xxx')
  63. for f in (f1, f2):
  64. self.assertTrue(os.path.exists(f))
  65. pkg_dir, dist = self.create_dist()
  66. cmd = config(dist)
  67. cmd._clean(f1, f2)
  68. for f in (f1, f2):
  69. self.assertFalse(os.path.exists(f))
  70. def test_suite():
  71. return unittest.makeSuite(ConfigTestCase)
  72. if __name__ == "__main__":
  73. run_unittest(test_suite())