test_collect.py 6.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146
  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_get_default_config_paths_does_not_expand_home_when_false():
  13. flexmock(module.os, environ={'HOME': '/home/user'})
  14. config_paths = module.get_default_config_paths(expand_home=False)
  15. assert '$HOME/.config/borgmatic/config.yaml' in config_paths
  16. def test_collect_config_filenames_collects_given_files():
  17. config_paths = ('config.yaml', 'other.yaml')
  18. flexmock(module.os.path).should_receive('isdir').and_return(False)
  19. config_filenames = tuple(module.collect_config_filenames(config_paths))
  20. assert config_filenames == config_paths
  21. def test_collect_config_filenames_collects_yml_file_endings():
  22. config_paths = ('config.yaml', '/etc/borgmatic.d')
  23. mock_path = flexmock(module.os.path)
  24. mock_path.should_receive('exists').and_return(True)
  25. mock_path.should_receive('isdir').with_args('config.yaml').and_return(False)
  26. mock_path.should_receive('isdir').with_args('/etc/borgmatic.d').and_return(True)
  27. mock_path.should_receive('isdir').with_args('/etc/borgmatic.d/foo.yml').and_return(False)
  28. flexmock(module.os).should_receive('listdir')
  29. flexmock(sys.modules['builtins']).should_receive('sorted').and_return(['foo.yml'])
  30. config_filenames = tuple(module.collect_config_filenames(config_paths))
  31. assert config_filenames == ('config.yaml', '/etc/borgmatic.d/foo.yml')
  32. def test_collect_config_filenames_collects_files_from_given_directories_and_ignores_sub_directories():
  33. config_paths = ('config.yaml', '/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('config.yaml').and_return(False)
  37. mock_path.should_receive('isdir').with_args('/etc/borgmatic.d').and_return(True)
  38. mock_path.should_receive('isdir').with_args('/etc/borgmatic.d/foo.yaml').and_return(False)
  39. mock_path.should_receive('isdir').with_args('/etc/borgmatic.d/bar').and_return(True)
  40. mock_path.should_receive('isdir').with_args('/etc/borgmatic.d/baz.yaml').and_return(False)
  41. flexmock(module.os).should_receive('listdir')
  42. flexmock(sys.modules['builtins']).should_receive('sorted').and_return(
  43. ['foo.yaml', 'bar', 'baz.yaml']
  44. )
  45. config_filenames = tuple(module.collect_config_filenames(config_paths))
  46. assert config_filenames == (
  47. 'config.yaml',
  48. '/etc/borgmatic.d/foo.yaml',
  49. '/etc/borgmatic.d/baz.yaml',
  50. )
  51. def test_collect_config_filenames_collects_files_from_given_directories_and_ignores_non_yaml_filenames():
  52. config_paths = ('/etc/borgmatic.d',)
  53. mock_path = flexmock(module.os.path)
  54. mock_path.should_receive('exists').and_return(True)
  55. mock_path.should_receive('isdir').with_args('/etc/borgmatic.d').and_return(True)
  56. mock_path.should_receive('isdir').with_args('/etc/borgmatic.d/foo.yaml').and_return(False)
  57. mock_path.should_receive('isdir').with_args('/etc/borgmatic.d/bar.yaml~').and_return(False)
  58. mock_path.should_receive('isdir').with_args('/etc/borgmatic.d/baz.txt').and_return(False)
  59. flexmock(module.os).should_receive('listdir')
  60. flexmock(sys.modules['builtins']).should_receive('sorted').and_return(
  61. ['foo.yaml', 'bar.yaml~', 'baz.txt']
  62. )
  63. config_filenames = tuple(module.collect_config_filenames(config_paths))
  64. assert config_filenames == ('/etc/borgmatic.d/foo.yaml',)
  65. def test_collect_config_filenames_skips_etc_borgmatic_config_dot_yaml_if_it_does_not_exist():
  66. config_paths = ('config.yaml', '/etc/borgmatic/config.yaml')
  67. mock_path = flexmock(module.os.path)
  68. mock_path.should_receive('exists').with_args('config.yaml').and_return(True)
  69. mock_path.should_receive('exists').with_args('/etc/borgmatic/config.yaml').and_return(False)
  70. mock_path.should_receive('isdir').with_args('config.yaml').and_return(False)
  71. mock_path.should_receive('isdir').with_args('/etc/borgmatic/config.yaml').and_return(True)
  72. config_filenames = tuple(module.collect_config_filenames(config_paths))
  73. assert config_filenames == ('config.yaml',)
  74. def test_collect_config_filenames_skips_etc_borgmatic_dot_d_if_it_does_not_exist():
  75. config_paths = ('config.yaml', '/etc/borgmatic.d')
  76. mock_path = flexmock(module.os.path)
  77. mock_path.should_receive('exists').with_args('config.yaml').and_return(True)
  78. mock_path.should_receive('exists').with_args('/etc/borgmatic.d').and_return(False)
  79. mock_path.should_receive('isdir').with_args('config.yaml').and_return(False)
  80. mock_path.should_receive('isdir').with_args('/etc/borgmatic.d').and_return(True)
  81. config_filenames = tuple(module.collect_config_filenames(config_paths))
  82. assert config_filenames == ('config.yaml',)
  83. def test_collect_config_filenames_skips_non_canonical_etc_borgmatic_dot_d_if_it_does_not_exist():
  84. config_paths = ('config.yaml', '/etc/../etc/borgmatic.d')
  85. mock_path = flexmock(module.os.path)
  86. mock_path.should_receive('exists').with_args('config.yaml').and_return(True)
  87. mock_path.should_receive('exists').with_args('/etc/../etc/borgmatic.d').and_return(False)
  88. mock_path.should_receive('isdir').with_args('config.yaml').and_return(False)
  89. mock_path.should_receive('isdir').with_args('/etc/../etc/borgmatic.d').and_return(True)
  90. config_filenames = tuple(module.collect_config_filenames(config_paths))
  91. assert config_filenames == ('config.yaml',)
  92. def test_collect_config_filenames_includes_other_directory_if_it_does_not_exist():
  93. config_paths = ('config.yaml', '/my/directory')
  94. mock_path = flexmock(module.os.path)
  95. mock_path.should_receive('exists').with_args('config.yaml').and_return(True)
  96. mock_path.should_receive('exists').with_args('/my/directory').and_return(False)
  97. mock_path.should_receive('isdir').with_args('config.yaml').and_return(False)
  98. mock_path.should_receive('isdir').with_args('/my/directory').and_return(True)
  99. config_filenames = tuple(module.collect_config_filenames(config_paths))
  100. assert config_filenames == config_paths