test_execute.py 1.7 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546
  1. import logging
  2. import subprocess
  3. import pytest
  4. from flexmock import flexmock
  5. from borgmatic import execute as module
  6. def test_execute_and_log_output_logs_each_line_separately():
  7. flexmock(module.logger).should_receive('log').with_args(logging.INFO, 'hi').once()
  8. flexmock(module.logger).should_receive('log').with_args(logging.INFO, 'there').once()
  9. module.execute_and_log_output(['echo', 'hi'], output_log_level=logging.INFO, shell=False)
  10. module.execute_and_log_output(['echo', 'there'], output_log_level=logging.INFO, shell=False)
  11. def test_execute_and_log_output_includes_borg_error_output_in_exception():
  12. with pytest.raises(subprocess.CalledProcessError) as error:
  13. module.execute_and_log_output(['grep'], output_log_level=logging.INFO, shell=False)
  14. assert error.value.returncode == 2
  15. assert error.value.output
  16. def test_execute_and_log_output_truncates_long_borg_error_output():
  17. flexmock(module).ERROR_OUTPUT_MAX_LINE_COUNT = 0
  18. with pytest.raises(subprocess.CalledProcessError) as error:
  19. module.execute_and_log_output(['grep'], output_log_level=logging.INFO, shell=False)
  20. assert error.value.returncode == 2
  21. assert error.value.output.startswith('...')
  22. def test_execute_and_log_output_with_no_output_logs_nothing():
  23. flexmock(module.logger).should_receive('log').never()
  24. module.execute_and_log_output(['true'], output_log_level=logging.INFO, shell=False)
  25. def test_execute_and_log_output_with_error_exit_status_raises():
  26. flexmock(module.logger).should_receive('log').never()
  27. with pytest.raises(subprocess.CalledProcessError):
  28. module.execute_and_log_output(['false'], output_log_level=logging.INFO, shell=False)