test_frank.py 3.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151
  1. from collections import OrderedDict
  2. from frank.frank import parse_yaml, ordered_load, run_action
  3. from frank.frank import Shell
  4. test_yaml = """
  5. commands:
  6. - test
  7. - build_sphinx
  8. - publish
  9. test:
  10. shell:
  11. - pip install -e .
  12. - python setup.py test
  13. build_sphinx:
  14. shell:
  15. cwd: docs
  16. cmd: make html
  17. """
  18. def mk_yaml():
  19. with open('.frank.yaml', 'w') as f:
  20. f.write(test_yaml)
  21. mk_yaml()
  22. def test_parse_yaml():
  23. y = parse_yaml('.')
  24. assert isinstance(y, dict)
  25. def test_ordered_load():
  26. ans = ordered_load("""
  27. commands:
  28. - test
  29. - build_sphinx
  30. - publish""")
  31. assert isinstance(ans['commands'], list)
  32. ans = ordered_load("""
  33. commands:
  34. - test
  35. - build_sphinx
  36. - publish
  37. test:
  38. shell:
  39. - pip install -e .
  40. - python setup.py test
  41. """)
  42. assert isinstance(ans['test'], OrderedDict)
  43. assert isinstance(ans['test']['shell'], list)
  44. # If 'shell' is a list run the commands as ordered
  45. ans = ordered_load("""
  46. commands:
  47. - test
  48. - build_sphinx
  49. - publish
  50. test:
  51. shell:
  52. - pip install -e .
  53. - python setup.py test
  54. build_sphinx:
  55. shell:
  56. cwd: docs
  57. cmd: make html
  58. """)
  59. assert isinstance(ans['test']['shell'], list)
  60. assert isinstance(ans['build_sphinx'], OrderedDict)
  61. assert isinstance(ans['build_sphinx']['shell'], OrderedDict)
  62. # If 'shell' is an OrderedDict run the command with the keywords given
  63. def test_Shell_run():
  64. sh = Shell()
  65. sh.run('ls -l', cwd='/tmp/')
  66. assert not sh.code
  67. assert isinstance(sh.output(), list)
  68. sh = Shell()
  69. sh.run('ls -l', cwd='/no-such-directory/')
  70. assert sh.pid is None
  71. assert isinstance(sh.exception, OSError)
  72. def test_run_action_shell():
  73. # test shell commands
  74. axn = ['shell', 'touch /tmp/testaxn', 'touch /tmp/testaxn2',
  75. 'rm /tmp/testaxn']
  76. results = run_action(axn)
  77. # all these commands should have succeeded
  78. assert not any([result.code for result in results])
  79. axn = ['shell', 'touch /nosuchdir/testaxn', 'touch /tmp/testaxn2',
  80. 'rm /tmp/testaxn']
  81. results = run_action(axn)
  82. assert len(results) == 1
  83. assert results[0].code == 1
  84. axn = ['shell', 'touch /tmp/works', 'touch /nosuchdir/fails',
  85. 'touch /tmp/testaxn2', 'rm /tmp/testaxn']
  86. results = run_action(axn)
  87. assert len(results) == 2
  88. assert results[0].code == 0
  89. assert results[1].code == 1
  90. def test_run_action_shell_with_kwargs():
  91. # test shell commands
  92. axn = OrderedDict([('shell', OrderedDict([('cwd', 'tmp'),
  93. ('cmd', 'touch foo')]))])
  94. results = run_action(axn)
  95. assert len(results) == 1
  96. assert results[0].code == 0
  97. axn = OrderedDict([('shell', OrderedDict([('cwd', 'nosuchdir'),
  98. ('cmd', 'touch foo')]))])
  99. results = run_action(axn)
  100. assert len(results) == 1
  101. assert isinstance(results[0].exception, OSError)
  102. axn = OrderedDict([('shell', OrderedDict([('cwd', '/tmp'),
  103. ('cmd', 'touch /nosuchdir/foo')]))])
  104. results = run_action(axn)
  105. assert len(results) == 1
  106. assert results[0].code == 1
  107. # missing arguments to touch
  108. axn = OrderedDict([('shell', OrderedDict([('cwd', '/tmp'),
  109. ('cmd', 'touch')]))])
  110. assert len(results) == 1
  111. assert results[0].code == 1
  112. assert ['touch: missing file operand',
  113. "Try 'touch --help' for more information."] == \
  114. results[0].errors()
  115. def test_run_python_code():
  116. import sys
  117. import os
  118. sys.path.insert(0, os.path.dirname(__file__))
  119. od = ordered_load("""
  120. run_function_python:
  121. python:
  122. - dummy_code:do_something
  123. - dummy_code:do_something_else
  124. """)
  125. results = run_action(od['run_function_python'])
  126. assert len(results) == 2
  127. assert isinstance(results[-1].code, AttributeError)