test_collect.py 5.5 KB

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