test_cmd.py 3.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126
  1. """Tests for distutils.cmd."""
  2. import unittest
  3. import os
  4. from test.support import captured_stdout, run_unittest
  5. from distutils.cmd import Command
  6. from distutils.dist import Distribution
  7. from distutils.errors import DistutilsOptionError
  8. from distutils import debug
  9. class MyCmd(Command):
  10. def initialize_options(self):
  11. pass
  12. class CommandTestCase(unittest.TestCase):
  13. def setUp(self):
  14. dist = Distribution()
  15. self.cmd = MyCmd(dist)
  16. def test_ensure_string_list(self):
  17. cmd = self.cmd
  18. cmd.not_string_list = ['one', 2, 'three']
  19. cmd.yes_string_list = ['one', 'two', 'three']
  20. cmd.not_string_list2 = object()
  21. cmd.yes_string_list2 = 'ok'
  22. cmd.ensure_string_list('yes_string_list')
  23. cmd.ensure_string_list('yes_string_list2')
  24. self.assertRaises(DistutilsOptionError,
  25. cmd.ensure_string_list, 'not_string_list')
  26. self.assertRaises(DistutilsOptionError,
  27. cmd.ensure_string_list, 'not_string_list2')
  28. cmd.option1 = 'ok,dok'
  29. cmd.ensure_string_list('option1')
  30. self.assertEqual(cmd.option1, ['ok', 'dok'])
  31. cmd.option2 = ['xxx', 'www']
  32. cmd.ensure_string_list('option2')
  33. cmd.option3 = ['ok', 2]
  34. self.assertRaises(DistutilsOptionError, cmd.ensure_string_list,
  35. 'option3')
  36. def test_make_file(self):
  37. cmd = self.cmd
  38. # making sure it raises when infiles is not a string or a list/tuple
  39. self.assertRaises(TypeError, cmd.make_file,
  40. infiles=1, outfile='', func='func', args=())
  41. # making sure execute gets called properly
  42. def _execute(func, args, exec_msg, level):
  43. self.assertEqual(exec_msg, 'generating out from in')
  44. cmd.force = True
  45. cmd.execute = _execute
  46. cmd.make_file(infiles='in', outfile='out', func='func', args=())
  47. def test_dump_options(self):
  48. msgs = []
  49. def _announce(msg, level):
  50. msgs.append(msg)
  51. cmd = self.cmd
  52. cmd.announce = _announce
  53. cmd.option1 = 1
  54. cmd.option2 = 1
  55. cmd.user_options = [('option1', '', ''), ('option2', '', '')]
  56. cmd.dump_options()
  57. wanted = ["command options for 'MyCmd':", ' option1 = 1',
  58. ' option2 = 1']
  59. self.assertEqual(msgs, wanted)
  60. def test_ensure_string(self):
  61. cmd = self.cmd
  62. cmd.option1 = 'ok'
  63. cmd.ensure_string('option1')
  64. cmd.option2 = None
  65. cmd.ensure_string('option2', 'xxx')
  66. self.assertTrue(hasattr(cmd, 'option2'))
  67. cmd.option3 = 1
  68. self.assertRaises(DistutilsOptionError, cmd.ensure_string, 'option3')
  69. def test_ensure_filename(self):
  70. cmd = self.cmd
  71. cmd.option1 = __file__
  72. cmd.ensure_filename('option1')
  73. cmd.option2 = 'xxx'
  74. self.assertRaises(DistutilsOptionError, cmd.ensure_filename, 'option2')
  75. def test_ensure_dirname(self):
  76. cmd = self.cmd
  77. cmd.option1 = os.path.dirname(__file__) or os.curdir
  78. cmd.ensure_dirname('option1')
  79. cmd.option2 = 'xxx'
  80. self.assertRaises(DistutilsOptionError, cmd.ensure_dirname, 'option2')
  81. def test_debug_print(self):
  82. cmd = self.cmd
  83. with captured_stdout() as stdout:
  84. cmd.debug_print('xxx')
  85. stdout.seek(0)
  86. self.assertEqual(stdout.read(), '')
  87. debug.DEBUG = True
  88. try:
  89. with captured_stdout() as stdout:
  90. cmd.debug_print('xxx')
  91. stdout.seek(0)
  92. self.assertEqual(stdout.read(), 'xxx\n')
  93. finally:
  94. debug.DEBUG = False
  95. def test_suite():
  96. return unittest.makeSuite(CommandTestCase)
  97. if __name__ == '__main__':
  98. run_unittest(test_suite())