test_collect.py 6.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138
  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_yml_file_endings():
  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.yml').and_return(False)
  24. flexmock(module.os).should_receive('listdir')
  25. flexmock(sys.modules['builtins']).should_receive('sorted').and_return(['foo.yml'])
  26. config_filenames = tuple(module.collect_config_filenames(config_paths))
  27. assert config_filenames == ('config.yaml', '/etc/borgmatic.d/foo.yml')
  28. def test_collect_config_filenames_collects_files_from_given_directories_and_ignores_sub_directories():
  29. config_paths = ('config.yaml', '/etc/borgmatic.d')
  30. mock_path = flexmock(module.os.path)
  31. mock_path.should_receive('exists').and_return(True)
  32. mock_path.should_receive('isdir').with_args('config.yaml').and_return(False)
  33. mock_path.should_receive('isdir').with_args('/etc/borgmatic.d').and_return(True)
  34. mock_path.should_receive('isdir').with_args('/etc/borgmatic.d/foo.yaml').and_return(False)
  35. mock_path.should_receive('isdir').with_args('/etc/borgmatic.d/bar').and_return(True)
  36. mock_path.should_receive('isdir').with_args('/etc/borgmatic.d/baz.yaml').and_return(False)
  37. flexmock(module.os).should_receive('listdir')
  38. flexmock(sys.modules['builtins']).should_receive('sorted').and_return(
  39. ['foo.yaml', 'bar', 'baz.yaml']
  40. )
  41. config_filenames = tuple(module.collect_config_filenames(config_paths))
  42. assert config_filenames == (
  43. 'config.yaml',
  44. '/etc/borgmatic.d/foo.yaml',
  45. '/etc/borgmatic.d/baz.yaml',
  46. )
  47. def test_collect_config_filenames_collects_files_from_given_directories_and_ignores_non_yaml_filenames():
  48. config_paths = ('/etc/borgmatic.d',)
  49. mock_path = flexmock(module.os.path)
  50. mock_path.should_receive('exists').and_return(True)
  51. mock_path.should_receive('isdir').with_args('/etc/borgmatic.d').and_return(True)
  52. mock_path.should_receive('isdir').with_args('/etc/borgmatic.d/foo.yaml').and_return(False)
  53. mock_path.should_receive('isdir').with_args('/etc/borgmatic.d/bar.yaml~').and_return(False)
  54. mock_path.should_receive('isdir').with_args('/etc/borgmatic.d/baz.txt').and_return(False)
  55. flexmock(module.os).should_receive('listdir')
  56. flexmock(sys.modules['builtins']).should_receive('sorted').and_return(
  57. ['foo.yaml', 'bar.yaml~', 'baz.txt']
  58. )
  59. config_filenames = tuple(module.collect_config_filenames(config_paths))
  60. assert config_filenames == ('/etc/borgmatic.d/foo.yaml',)
  61. def test_collect_config_filenames_skips_etc_borgmatic_config_dot_yaml_if_it_does_not_exist():
  62. config_paths = ('config.yaml', '/etc/borgmatic/config.yaml')
  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/borgmatic/config.yaml').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/borgmatic/config.yaml').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_skips_etc_borgmatic_dot_d_if_it_does_not_exist():
  71. config_paths = ('config.yaml', '/etc/borgmatic.d')
  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('/etc/borgmatic.d').and_return(False)
  75. mock_path.should_receive('isdir').with_args('config.yaml').and_return(False)
  76. mock_path.should_receive('isdir').with_args('/etc/borgmatic.d').and_return(True)
  77. config_filenames = tuple(module.collect_config_filenames(config_paths))
  78. assert config_filenames == ('config.yaml',)
  79. def test_collect_config_filenames_skips_non_canonical_etc_borgmatic_dot_d_if_it_does_not_exist():
  80. config_paths = ('config.yaml', '/etc/../etc/borgmatic.d')
  81. mock_path = flexmock(module.os.path)
  82. mock_path.should_receive('exists').with_args('config.yaml').and_return(True)
  83. mock_path.should_receive('exists').with_args('/etc/../etc/borgmatic.d').and_return(False)
  84. mock_path.should_receive('isdir').with_args('config.yaml').and_return(False)
  85. mock_path.should_receive('isdir').with_args('/etc/../etc/borgmatic.d').and_return(True)
  86. config_filenames = tuple(module.collect_config_filenames(config_paths))
  87. assert config_filenames == ('config.yaml',)
  88. def test_collect_config_filenames_includes_other_directory_if_it_does_not_exist():
  89. config_paths = ('config.yaml', '/my/directory')
  90. mock_path = flexmock(module.os.path)
  91. mock_path.should_receive('exists').with_args('config.yaml').and_return(True)
  92. mock_path.should_receive('exists').with_args('/my/directory').and_return(False)
  93. mock_path.should_receive('isdir').with_args('config.yaml').and_return(False)
  94. mock_path.should_receive('isdir').with_args('/my/directory').and_return(True)
  95. config_filenames = tuple(module.collect_config_filenames(config_paths))
  96. assert config_filenames == config_paths