test_collect.py 5.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115
  1. from flexmock import flexmock
  2. from borgmatic.config import collect as module
  3. def test_get_default_config_paths_includes_absolute_user_config_path():
  4. flexmock(module.os, environ={'XDG_CONFIG_HOME': None, 'HOME': '/home/user'})
  5. config_paths = module.get_default_config_paths()
  6. assert '/home/user/.config/borgmatic/config.yaml' in config_paths
  7. def test_get_default_config_paths_prefers_xdg_config_home_for_user_config_path():
  8. flexmock(module.os, environ={'XDG_CONFIG_HOME': '/home/user/.etc', 'HOME': '/home/user'})
  9. config_paths = module.get_default_config_paths()
  10. assert '/home/user/.etc/borgmatic/config.yaml' in config_paths
  11. def test_collect_config_filenames_collects_given_files():
  12. config_paths = ('config.yaml', 'other.yaml')
  13. flexmock(module.os.path).should_receive('isdir').and_return(False)
  14. config_filenames = tuple(module.collect_config_filenames(config_paths))
  15. assert config_filenames == config_paths
  16. def test_collect_config_filenames_collects_files_from_given_directories_and_ignores_sub_directories():
  17. config_paths = ('config.yaml', '/etc/borgmatic.d')
  18. mock_path = flexmock(module.os.path)
  19. mock_path.should_receive('exists').and_return(True)
  20. mock_path.should_receive('isdir').with_args('config.yaml').and_return(False)
  21. mock_path.should_receive('isdir').with_args('/etc/borgmatic.d').and_return(True)
  22. mock_path.should_receive('isdir').with_args('/etc/borgmatic.d/foo.yaml').and_return(False)
  23. mock_path.should_receive('isdir').with_args('/etc/borgmatic.d/bar').and_return(True)
  24. mock_path.should_receive('isdir').with_args('/etc/borgmatic.d/baz.yaml').and_return(False)
  25. flexmock(module.os).should_receive('listdir').and_return(['foo.yaml', 'bar', 'baz.yaml'])
  26. config_filenames = tuple(module.collect_config_filenames(config_paths))
  27. assert config_filenames == (
  28. 'config.yaml',
  29. '/etc/borgmatic.d/foo.yaml',
  30. '/etc/borgmatic.d/baz.yaml',
  31. )
  32. def test_collect_config_filenames_collects_files_from_given_directories_and_ignores_non_yaml_filenames():
  33. config_paths = ('/etc/borgmatic.d',)
  34. mock_path = flexmock(module.os.path)
  35. mock_path.should_receive('exists').and_return(True)
  36. mock_path.should_receive('isdir').with_args('/etc/borgmatic.d').and_return(True)
  37. mock_path.should_receive('isdir').with_args('/etc/borgmatic.d/foo.yaml').and_return(False)
  38. mock_path.should_receive('isdir').with_args('/etc/borgmatic.d/bar.yaml~').and_return(False)
  39. mock_path.should_receive('isdir').with_args('/etc/borgmatic.d/baz.txt').and_return(False)
  40. flexmock(module.os).should_receive('listdir').and_return(['foo.yaml', 'bar.yaml~', 'baz.txt'])
  41. config_filenames = tuple(module.collect_config_filenames(config_paths))
  42. assert config_filenames == ('/etc/borgmatic.d/foo.yaml',)
  43. def test_collect_config_filenames_skips_etc_borgmatic_config_dot_yaml_if_it_does_not_exist():
  44. config_paths = ('config.yaml', '/etc/borgmatic/config.yaml')
  45. mock_path = flexmock(module.os.path)
  46. mock_path.should_receive('exists').with_args('config.yaml').and_return(True)
  47. mock_path.should_receive('exists').with_args('/etc/borgmatic/config.yaml').and_return(False)
  48. mock_path.should_receive('isdir').with_args('config.yaml').and_return(False)
  49. mock_path.should_receive('isdir').with_args('/etc/borgmatic/config.yaml').and_return(True)
  50. config_filenames = tuple(module.collect_config_filenames(config_paths))
  51. assert config_filenames == ('config.yaml',)
  52. def test_collect_config_filenames_skips_etc_borgmatic_dot_d_if_it_does_not_exist():
  53. config_paths = ('config.yaml', '/etc/borgmatic.d')
  54. mock_path = flexmock(module.os.path)
  55. mock_path.should_receive('exists').with_args('config.yaml').and_return(True)
  56. mock_path.should_receive('exists').with_args('/etc/borgmatic.d').and_return(False)
  57. mock_path.should_receive('isdir').with_args('config.yaml').and_return(False)
  58. mock_path.should_receive('isdir').with_args('/etc/borgmatic.d').and_return(True)
  59. config_filenames = tuple(module.collect_config_filenames(config_paths))
  60. assert config_filenames == ('config.yaml',)
  61. def test_collect_config_filenames_skips_non_canonical_etc_borgmatic_dot_d_if_it_does_not_exist():
  62. config_paths = ('config.yaml', '/etc/../etc/borgmatic.d')
  63. mock_path = flexmock(module.os.path)
  64. mock_path.should_receive('exists').with_args('config.yaml').and_return(True)
  65. mock_path.should_receive('exists').with_args('/etc/../etc/borgmatic.d').and_return(False)
  66. mock_path.should_receive('isdir').with_args('config.yaml').and_return(False)
  67. mock_path.should_receive('isdir').with_args('/etc/../etc/borgmatic.d').and_return(True)
  68. config_filenames = tuple(module.collect_config_filenames(config_paths))
  69. assert config_filenames == ('config.yaml',)
  70. def test_collect_config_filenames_includes_other_directory_if_it_does_not_exist():
  71. config_paths = ('config.yaml', '/my/directory')
  72. mock_path = flexmock(module.os.path)
  73. mock_path.should_receive('exists').with_args('config.yaml').and_return(True)
  74. mock_path.should_receive('exists').with_args('/my/directory').and_return(False)
  75. mock_path.should_receive('isdir').with_args('config.yaml').and_return(False)
  76. mock_path.should_receive('isdir').with_args('/my/directory').and_return(True)
  77. config_filenames = tuple(module.collect_config_filenames(config_paths))
  78. assert config_filenames == config_paths