test_execute.py 2.4 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677
  1. import logging
  2. import pytest
  3. from flexmock import flexmock
  4. from borgmatic import execute as module
  5. def test_execute_command_calls_full_command():
  6. full_command = ['foo', 'bar']
  7. flexmock(module).should_receive('execute_and_log_output').with_args(
  8. full_command, output_log_level=logging.INFO, shell=False
  9. ).once()
  10. output = module.execute_command(full_command)
  11. assert output is None
  12. def test_execute_command_calls_full_command_with_shell():
  13. full_command = ['foo', 'bar']
  14. flexmock(module).should_receive('execute_and_log_output').with_args(
  15. full_command, output_log_level=logging.INFO, shell=True
  16. ).once()
  17. output = module.execute_command(full_command, shell=True)
  18. assert output is None
  19. def test_execute_command_captures_output():
  20. full_command = ['foo', 'bar']
  21. expected_output = '[]'
  22. flexmock(module.subprocess).should_receive('check_output').with_args(
  23. full_command, shell=False
  24. ).and_return(flexmock(decode=lambda: expected_output)).once()
  25. output = module.execute_command(full_command, output_log_level=None)
  26. assert output == expected_output
  27. def test_execute_command_captures_output_with_shell():
  28. full_command = ['foo', 'bar']
  29. expected_output = '[]'
  30. flexmock(module.subprocess).should_receive('check_output').with_args(
  31. full_command, shell=True
  32. ).and_return(flexmock(decode=lambda: expected_output)).once()
  33. output = module.execute_command(full_command, output_log_level=None, shell=True)
  34. assert output == expected_output
  35. def test_execute_command_without_capture_does_not_raise_on_success():
  36. flexmock(module.subprocess).should_receive('check_call').and_raise(
  37. module.subprocess.CalledProcessError(0, 'borg init')
  38. )
  39. module.execute_command_without_capture(('borg', 'init'))
  40. def test_execute_command_without_capture_does_not_raise_on_warning():
  41. flexmock(module.subprocess).should_receive('check_call').and_raise(
  42. module.subprocess.CalledProcessError(1, 'borg init')
  43. )
  44. module.execute_command_without_capture(('borg', 'init'))
  45. def test_execute_command_without_capture_raises_on_error():
  46. flexmock(module.subprocess).should_receive('check_call').and_raise(
  47. module.subprocess.CalledProcessError(2, 'borg init')
  48. )
  49. with pytest.raises(module.subprocess.CalledProcessError):
  50. module.execute_command_without_capture(('borg', 'init'))