test_version.py 2.5 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970
  1. import logging
  2. import pytest
  3. from flexmock import flexmock
  4. from borgmatic.borg import version as module
  5. from ..test_verbosity import insert_logging_mock
  6. VERSION = '1.2.3'
  7. def insert_execute_command_and_capture_output_mock(
  8. command, borg_local_path='borg', borg_exit_codes=None, version_output=f'borg {VERSION}'
  9. ):
  10. flexmock(module.environment).should_receive('make_environment')
  11. flexmock(module).should_receive('execute_command_and_capture_output').with_args(
  12. command,
  13. extra_environment=None,
  14. borg_local_path=borg_local_path,
  15. borg_exit_codes=borg_exit_codes,
  16. ).once().and_return(version_output)
  17. def test_local_borg_version_calls_borg_with_required_parameters():
  18. insert_execute_command_and_capture_output_mock(('borg', '--version'))
  19. flexmock(module.environment).should_receive('make_environment')
  20. assert module.local_borg_version({}) == VERSION
  21. def test_local_borg_version_with_log_info_calls_borg_with_info_parameter():
  22. insert_execute_command_and_capture_output_mock(('borg', '--version', '--info'))
  23. insert_logging_mock(logging.INFO)
  24. flexmock(module.environment).should_receive('make_environment')
  25. assert module.local_borg_version({}) == VERSION
  26. def test_local_borg_version_with_log_debug_calls_borg_with_debug_parameters():
  27. insert_execute_command_and_capture_output_mock(('borg', '--version', '--debug', '--show-rc'))
  28. insert_logging_mock(logging.DEBUG)
  29. flexmock(module.environment).should_receive('make_environment')
  30. assert module.local_borg_version({}) == VERSION
  31. def test_local_borg_version_with_local_borg_path_calls_borg_with_it():
  32. insert_execute_command_and_capture_output_mock(('borg1', '--version'), borg_local_path='borg1')
  33. flexmock(module.environment).should_receive('make_environment')
  34. assert module.local_borg_version({}, 'borg1') == VERSION
  35. def test_local_borg_version_with_borg_exit_codes_calls_using_with_them():
  36. borg_exit_codes = flexmock()
  37. insert_execute_command_and_capture_output_mock(
  38. ('borg', '--version'), borg_exit_codes=borg_exit_codes
  39. )
  40. flexmock(module.environment).should_receive('make_environment')
  41. assert module.local_borg_version({'borg_exit_codes': borg_exit_codes}) == VERSION
  42. def test_local_borg_version_with_invalid_version_raises():
  43. insert_execute_command_and_capture_output_mock(('borg', '--version'), version_output='wtf')
  44. flexmock(module.environment).should_receive('make_environment')
  45. with pytest.raises(ValueError):
  46. module.local_borg_version({})