test_collect.py 7.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164
  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('access').and_return(True)
  29. flexmock(module.os).should_receive('listdir')
  30. flexmock(sys.modules['builtins']).should_receive('sorted').and_return(['foo.yml'])
  31. config_filenames = tuple(module.collect_config_filenames(config_paths))
  32. assert config_filenames == ('config.yaml', '/etc/borgmatic.d/foo.yml')
  33. def test_collect_config_filenames_collects_files_from_given_directories_and_ignores_sub_directories():
  34. config_paths = ('config.yaml', '/etc/borgmatic.d')
  35. mock_path = flexmock(module.os.path)
  36. mock_path.should_receive('exists').and_return(True)
  37. mock_path.should_receive('isdir').with_args('config.yaml').and_return(False)
  38. mock_path.should_receive('isdir').with_args('/etc/borgmatic.d').and_return(True)
  39. mock_path.should_receive('isdir').with_args('/etc/borgmatic.d/foo.yaml').and_return(False)
  40. mock_path.should_receive('isdir').with_args('/etc/borgmatic.d/bar').and_return(True)
  41. mock_path.should_receive('isdir').with_args('/etc/borgmatic.d/baz.yaml').and_return(False)
  42. flexmock(module.os).should_receive('access').and_return(True)
  43. flexmock(module.os).should_receive('listdir')
  44. flexmock(sys.modules['builtins']).should_receive('sorted').and_return(
  45. ['foo.yaml', 'bar', 'baz.yaml']
  46. )
  47. config_filenames = tuple(module.collect_config_filenames(config_paths))
  48. assert config_filenames == (
  49. 'config.yaml',
  50. '/etc/borgmatic.d/foo.yaml',
  51. '/etc/borgmatic.d/baz.yaml',
  52. )
  53. def test_collect_config_filenames_collects_files_from_given_directories_and_ignores_non_yaml_filenames():
  54. config_paths = ('/etc/borgmatic.d',)
  55. mock_path = flexmock(module.os.path)
  56. mock_path.should_receive('exists').and_return(True)
  57. mock_path.should_receive('isdir').with_args('/etc/borgmatic.d').and_return(True)
  58. mock_path.should_receive('isdir').with_args('/etc/borgmatic.d/foo.yaml').and_return(False)
  59. mock_path.should_receive('isdir').with_args('/etc/borgmatic.d/bar.yaml~').and_return(False)
  60. mock_path.should_receive('isdir').with_args('/etc/borgmatic.d/baz.txt').and_return(False)
  61. flexmock(module.os).should_receive('access').and_return(True)
  62. flexmock(module.os).should_receive('listdir')
  63. flexmock(sys.modules['builtins']).should_receive('sorted').and_return(
  64. ['foo.yaml', 'bar.yaml~', 'baz.txt']
  65. )
  66. config_filenames = tuple(module.collect_config_filenames(config_paths))
  67. assert config_filenames == ('/etc/borgmatic.d/foo.yaml',)
  68. def test_collect_config_filenames_skips_permission_denied_directories():
  69. config_paths = ('config.yaml', '/etc/borgmatic.d')
  70. mock_path = flexmock(module.os.path)
  71. mock_path.should_receive('exists').and_return(True)
  72. mock_path.should_receive('isdir').with_args('config.yaml').and_return(False)
  73. mock_path.should_receive('isdir').with_args('/etc/borgmatic.d').and_return(True)
  74. flexmock(module.os).should_receive('access').and_return(False)
  75. flexmock(module.os).should_receive('listdir')
  76. flexmock(sys.modules['builtins']).should_receive('sorted').and_return(['config.yaml'])
  77. config_filenames = tuple(module.collect_config_filenames(config_paths))
  78. assert config_filenames == ('config.yaml',)
  79. def test_collect_config_filenames_skips_etc_borgmatic_config_dot_yaml_if_it_does_not_exist():
  80. config_paths = ('config.yaml', '/etc/borgmatic/config.yaml')
  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/borgmatic/config.yaml').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/borgmatic/config.yaml').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_skips_etc_borgmatic_dot_d_if_it_does_not_exist():
  89. config_paths = ('config.yaml', '/etc/borgmatic.d')
  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('/etc/borgmatic.d').and_return(False)
  93. mock_path.should_receive('isdir').with_args('config.yaml').and_return(False)
  94. mock_path.should_receive('isdir').with_args('/etc/borgmatic.d').and_return(True)
  95. config_filenames = tuple(module.collect_config_filenames(config_paths))
  96. assert config_filenames == ('config.yaml',)
  97. def test_collect_config_filenames_skips_non_canonical_etc_borgmatic_dot_d_if_it_does_not_exist():
  98. config_paths = ('config.yaml', '/etc/../etc/borgmatic.d')
  99. mock_path = flexmock(module.os.path)
  100. mock_path.should_receive('exists').with_args('config.yaml').and_return(True)
  101. mock_path.should_receive('exists').with_args('/etc/../etc/borgmatic.d').and_return(False)
  102. mock_path.should_receive('isdir').with_args('config.yaml').and_return(False)
  103. mock_path.should_receive('isdir').with_args('/etc/../etc/borgmatic.d').and_return(True)
  104. config_filenames = tuple(module.collect_config_filenames(config_paths))
  105. assert config_filenames == ('config.yaml',)
  106. def test_collect_config_filenames_includes_other_directory_if_it_does_not_exist():
  107. config_paths = ('config.yaml', '/my/directory')
  108. mock_path = flexmock(module.os.path)
  109. mock_path.should_receive('exists').with_args('config.yaml').and_return(True)
  110. mock_path.should_receive('exists').with_args('/my/directory').and_return(False)
  111. mock_path.should_receive('isdir').with_args('config.yaml').and_return(False)
  112. mock_path.should_receive('isdir').with_args('/my/directory').and_return(True)
  113. config_filenames = tuple(module.collect_config_filenames(config_paths))
  114. assert config_filenames == config_paths