test_execute.py 2.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081
  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. flexmock(module).should_receive('exit_code_indicates_error').and_return(False)
  10. module.execute_and_log_output(
  11. ['echo', 'hi'],
  12. output_log_level=logging.INFO,
  13. shell=False,
  14. environment=None,
  15. working_directory=None,
  16. error_on_warnings=False,
  17. )
  18. module.execute_and_log_output(
  19. ['echo', 'there'],
  20. output_log_level=logging.INFO,
  21. shell=False,
  22. environment=None,
  23. working_directory=None,
  24. error_on_warnings=False,
  25. )
  26. def test_execute_and_log_output_includes_error_output_in_exception():
  27. flexmock(module.logger).should_receive('log')
  28. flexmock(module).should_receive('exit_code_indicates_error').and_return(True)
  29. with pytest.raises(subprocess.CalledProcessError) as error:
  30. module.execute_and_log_output(
  31. ['grep'],
  32. output_log_level=logging.INFO,
  33. shell=False,
  34. environment=None,
  35. working_directory=None,
  36. error_on_warnings=False,
  37. )
  38. assert error.value.returncode == 2
  39. assert error.value.output
  40. def test_execute_and_log_output_truncates_long_error_output():
  41. flexmock(module).ERROR_OUTPUT_MAX_LINE_COUNT = 0
  42. flexmock(module.logger).should_receive('log')
  43. flexmock(module).should_receive('exit_code_indicates_error').and_return(True)
  44. with pytest.raises(subprocess.CalledProcessError) as error:
  45. module.execute_and_log_output(
  46. ['grep'],
  47. output_log_level=logging.INFO,
  48. shell=False,
  49. environment=None,
  50. working_directory=None,
  51. error_on_warnings=False,
  52. )
  53. assert error.value.returncode == 2
  54. assert error.value.output.startswith('...')
  55. def test_execute_and_log_output_with_no_output_logs_nothing():
  56. flexmock(module.logger).should_receive('log').never()
  57. flexmock(module).should_receive('exit_code_indicates_error').and_return(False)
  58. module.execute_and_log_output(
  59. ['true'],
  60. output_log_level=logging.INFO,
  61. shell=False,
  62. environment=None,
  63. working_directory=None,
  64. error_on_warnings=False,
  65. )