test_pattern.py 2.0 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152
  1. import pytest
  2. from flexmock import flexmock
  3. from borgmatic.borg import pattern as module
  4. from borgmatic.borg.pattern import Pattern, Pattern_style, Pattern_type
  5. def test_write_patterns_file_writes_pattern_lines():
  6. temporary_file = flexmock(name='filename', flush=lambda: None)
  7. temporary_file.should_receive('write').with_args('R /foo\n+ sh:/foo/bar')
  8. flexmock(module.tempfile).should_receive('NamedTemporaryFile').and_return(temporary_file)
  9. module.write_patterns_file(
  10. [Pattern('/foo'), Pattern('/foo/bar', Pattern_type.INCLUDE, Pattern_style.SHELL)],
  11. borgmatic_runtime_directory='/run/user/0',
  12. )
  13. def test_write_patterns_file_with_empty_exclude_patterns_does_not_raise():
  14. module.write_patterns_file([], borgmatic_runtime_directory='/run/user/0')
  15. def test_write_patterns_file_appends_to_existing():
  16. patterns_file = flexmock(name='filename', flush=lambda: None)
  17. patterns_file.should_receive('write').with_args('\n')
  18. patterns_file.should_receive('write').with_args('R /foo\n+ /foo/bar')
  19. flexmock(module.tempfile).should_receive('NamedTemporaryFile').never()
  20. module.write_patterns_file(
  21. [Pattern('/foo'), Pattern('/foo/bar', Pattern_type.INCLUDE)],
  22. borgmatic_runtime_directory='/run/user/0',
  23. patterns_file=patterns_file,
  24. )
  25. def test_check_all_root_patterns_exist_with_existent_pattern_path_does_not_raise():
  26. flexmock(module.os.path).should_receive('exists').and_return(True)
  27. module.check_all_root_patterns_exist([Pattern('foo')])
  28. def test_check_all_root_patterns_exist_with_non_root_pattern_skips_existence_check():
  29. flexmock(module.os.path).should_receive('exists').never()
  30. module.check_all_root_patterns_exist([Pattern('foo', Pattern_type.INCLUDE)])
  31. def test_check_all_root_patterns_exist_with_non_existent_pattern_path_raises():
  32. flexmock(module.os.path).should_receive('exists').and_return(False)
  33. with pytest.raises(ValueError):
  34. module.check_all_root_patterns_exist([Pattern('foo')])