test_cmd.py 3.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107
  1. """Tests for distutils.cmd."""
  2. import os
  3. from distutils import debug
  4. from distutils.cmd import Command
  5. from distutils.dist import Distribution
  6. from distutils.errors import DistutilsOptionError
  7. import pytest
  8. class MyCmd(Command):
  9. def initialize_options(self):
  10. pass
  11. @pytest.fixture
  12. def cmd(request):
  13. return MyCmd(Distribution())
  14. class TestCommand:
  15. def test_ensure_string_list(self, cmd):
  16. cmd.not_string_list = ['one', 2, 'three']
  17. cmd.yes_string_list = ['one', 'two', 'three']
  18. cmd.not_string_list2 = object()
  19. cmd.yes_string_list2 = 'ok'
  20. cmd.ensure_string_list('yes_string_list')
  21. cmd.ensure_string_list('yes_string_list2')
  22. with pytest.raises(DistutilsOptionError):
  23. cmd.ensure_string_list('not_string_list')
  24. with pytest.raises(DistutilsOptionError):
  25. cmd.ensure_string_list('not_string_list2')
  26. cmd.option1 = 'ok,dok'
  27. cmd.ensure_string_list('option1')
  28. assert cmd.option1 == ['ok', 'dok']
  29. cmd.option2 = ['xxx', 'www']
  30. cmd.ensure_string_list('option2')
  31. cmd.option3 = ['ok', 2]
  32. with pytest.raises(DistutilsOptionError):
  33. cmd.ensure_string_list('option3')
  34. def test_make_file(self, cmd):
  35. # making sure it raises when infiles is not a string or a list/tuple
  36. with pytest.raises(TypeError):
  37. cmd.make_file(infiles=True, outfile='', func='func', args=())
  38. # making sure execute gets called properly
  39. def _execute(func, args, exec_msg, level):
  40. assert exec_msg == 'generating out from in'
  41. cmd.force = True
  42. cmd.execute = _execute
  43. cmd.make_file(infiles='in', outfile='out', func='func', args=())
  44. def test_dump_options(self, cmd):
  45. msgs = []
  46. def _announce(msg, level):
  47. msgs.append(msg)
  48. cmd.announce = _announce
  49. cmd.option1 = 1
  50. cmd.option2 = 1
  51. cmd.user_options = [('option1', '', ''), ('option2', '', '')]
  52. cmd.dump_options()
  53. wanted = ["command options for 'MyCmd':", ' option1 = 1', ' option2 = 1']
  54. assert msgs == wanted
  55. def test_ensure_string(self, cmd):
  56. cmd.option1 = 'ok'
  57. cmd.ensure_string('option1')
  58. cmd.option2 = None
  59. cmd.ensure_string('option2', 'xxx')
  60. assert hasattr(cmd, 'option2')
  61. cmd.option3 = 1
  62. with pytest.raises(DistutilsOptionError):
  63. cmd.ensure_string('option3')
  64. def test_ensure_filename(self, cmd):
  65. cmd.option1 = __file__
  66. cmd.ensure_filename('option1')
  67. cmd.option2 = 'xxx'
  68. with pytest.raises(DistutilsOptionError):
  69. cmd.ensure_filename('option2')
  70. def test_ensure_dirname(self, cmd):
  71. cmd.option1 = os.path.dirname(__file__) or os.curdir
  72. cmd.ensure_dirname('option1')
  73. cmd.option2 = 'xxx'
  74. with pytest.raises(DistutilsOptionError):
  75. cmd.ensure_dirname('option2')
  76. def test_debug_print(self, cmd, capsys, monkeypatch):
  77. cmd.debug_print('xxx')
  78. assert capsys.readouterr().out == ''
  79. monkeypatch.setattr(debug, 'DEBUG', True)
  80. cmd.debug_print('xxx')
  81. assert capsys.readouterr().out == 'xxx\n'