test_paths.py 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231
  1. import pytest
  2. from flexmock import flexmock
  3. from borgmatic.config import paths as module
  4. def test_expand_user_in_path_passes_through_plain_directory():
  5. flexmock(module.os.path).should_receive('expanduser').and_return('/home/foo')
  6. assert module.expand_user_in_path('/home/foo') == '/home/foo'
  7. def test_expand_user_in_path_expands_tildes():
  8. flexmock(module.os.path).should_receive('expanduser').and_return('/home/foo')
  9. assert module.expand_user_in_path('~/foo') == '/home/foo'
  10. def test_expand_user_in_path_handles_empty_directory():
  11. assert module.expand_user_in_path('') is None
  12. def test_expand_user_in_path_handles_none_directory():
  13. assert module.expand_user_in_path(None) is None
  14. def test_expand_user_in_path_handles_incorrectly_typed_directory():
  15. assert module.expand_user_in_path(3) is None
  16. def test_get_borgmatic_source_directory_uses_config_option():
  17. flexmock(module).should_receive('expand_user_in_path').replace_with(lambda path: path)
  18. assert module.get_borgmatic_source_directory({'borgmatic_source_directory': '/tmp'}) == '/tmp'
  19. def test_get_borgmatic_source_directory_without_config_option_uses_default():
  20. flexmock(module).should_receive('expand_user_in_path').replace_with(lambda path: path)
  21. assert module.get_borgmatic_source_directory({}) == '~/.borgmatic'
  22. def test_replace_temporary_subdirectory_with_glob_transforms_path():
  23. assert (
  24. module.replace_temporary_subdirectory_with_glob('/tmp/borgmatic-aet8kn93/borgmatic')
  25. == '/tmp/borgmatic-*/borgmatic'
  26. )
  27. def test_runtime_directory_uses_config_option():
  28. flexmock(module).should_receive('expand_user_in_path').replace_with(lambda path: path)
  29. flexmock(module.os).should_receive('makedirs')
  30. config = {'user_runtime_directory': '/run', 'borgmatic_source_directory': '/nope'}
  31. with module.Runtime_directory(config, 'prefix') as borgmatic_runtime_directory:
  32. assert borgmatic_runtime_directory == '/run/./borgmatic'
  33. def test_runtime_directory_uses_config_option_without_adding_duplicate_borgmatic_subdirectory():
  34. flexmock(module).should_receive('expand_user_in_path').replace_with(lambda path: path)
  35. flexmock(module.os).should_receive('makedirs')
  36. config = {'user_runtime_directory': '/run/borgmatic', 'borgmatic_source_directory': '/nope'}
  37. with module.Runtime_directory(config, 'prefix') as borgmatic_runtime_directory:
  38. assert borgmatic_runtime_directory == '/run/./borgmatic'
  39. def test_runtime_directory_falls_back_to_xdg_runtime_dir():
  40. flexmock(module).should_receive('expand_user_in_path').replace_with(lambda path: path)
  41. flexmock(module.os.environ).should_receive('get').with_args('XDG_RUNTIME_DIR').and_return(
  42. '/run'
  43. )
  44. flexmock(module.os).should_receive('makedirs')
  45. with module.Runtime_directory({}, 'prefix') as borgmatic_runtime_directory:
  46. assert borgmatic_runtime_directory == '/run/./borgmatic'
  47. def test_runtime_directory_falls_back_to_xdg_runtime_dir_without_adding_duplicate_borgmatic_subdirectory():
  48. flexmock(module).should_receive('expand_user_in_path').replace_with(lambda path: path)
  49. flexmock(module.os.environ).should_receive('get').with_args('XDG_RUNTIME_DIR').and_return(
  50. '/run/borgmatic'
  51. )
  52. flexmock(module.os).should_receive('makedirs')
  53. with module.Runtime_directory({}, 'prefix') as borgmatic_runtime_directory:
  54. assert borgmatic_runtime_directory == '/run/./borgmatic'
  55. def test_runtime_directory_falls_back_to_runtime_directory():
  56. flexmock(module).should_receive('expand_user_in_path').replace_with(lambda path: path)
  57. flexmock(module.os.environ).should_receive('get').with_args('XDG_RUNTIME_DIR').and_return(None)
  58. flexmock(module.os.environ).should_receive('get').with_args('RUNTIME_DIRECTORY').and_return(
  59. '/run'
  60. )
  61. flexmock(module.os).should_receive('makedirs')
  62. with module.Runtime_directory({}, 'prefix') as borgmatic_runtime_directory:
  63. assert borgmatic_runtime_directory == '/run/./borgmatic'
  64. def test_runtime_directory_falls_back_to_runtime_directory_without_adding_duplicate_borgmatic_subdirectory():
  65. flexmock(module).should_receive('expand_user_in_path').replace_with(lambda path: path)
  66. flexmock(module.os.environ).should_receive('get').with_args('XDG_RUNTIME_DIR').and_return(None)
  67. flexmock(module.os.environ).should_receive('get').with_args('RUNTIME_DIRECTORY').and_return(
  68. '/run/borgmatic'
  69. )
  70. flexmock(module.os).should_receive('makedirs')
  71. with module.Runtime_directory({}, 'prefix') as borgmatic_runtime_directory:
  72. assert borgmatic_runtime_directory == '/run/./borgmatic'
  73. def test_runtime_directory_falls_back_to_tmpdir_and_adds_temporary_subdirectory_that_get_cleaned_up():
  74. flexmock(module).should_receive('expand_user_in_path').replace_with(lambda path: path)
  75. flexmock(module.os.environ).should_receive('get').with_args('XDG_RUNTIME_DIR').and_return(None)
  76. flexmock(module.os.environ).should_receive('get').with_args('RUNTIME_DIRECTORY').and_return(
  77. None
  78. )
  79. flexmock(module.os.environ).should_receive('get').with_args('TMPDIR').and_return('/run')
  80. temporary_directory = flexmock(name='/run/borgmatic-1234')
  81. temporary_directory.should_receive('cleanup').once()
  82. flexmock(module.tempfile).should_receive('TemporaryDirectory').with_args(
  83. prefix='borgmatic-', dir='/run'
  84. ).and_return(temporary_directory)
  85. flexmock(module.os).should_receive('makedirs')
  86. with module.Runtime_directory({}, 'prefix') as borgmatic_runtime_directory:
  87. assert borgmatic_runtime_directory == '/run/borgmatic-1234/./borgmatic'
  88. def test_runtime_directory_falls_back_to_temp_and_adds_temporary_subdirectory_that_get_cleaned_up():
  89. flexmock(module).should_receive('expand_user_in_path').replace_with(lambda path: path)
  90. flexmock(module.os.environ).should_receive('get').with_args('XDG_RUNTIME_DIR').and_return(None)
  91. flexmock(module.os.environ).should_receive('get').with_args('RUNTIME_DIRECTORY').and_return(
  92. None
  93. )
  94. flexmock(module.os.environ).should_receive('get').with_args('TMPDIR').and_return(None)
  95. flexmock(module.os.environ).should_receive('get').with_args('TEMP').and_return('/run')
  96. temporary_directory = flexmock(name='/run/borgmatic-1234')
  97. temporary_directory.should_receive('cleanup').once()
  98. flexmock(module.tempfile).should_receive('TemporaryDirectory').with_args(
  99. prefix='borgmatic-', dir='/run'
  100. ).and_return(temporary_directory)
  101. flexmock(module.os).should_receive('makedirs')
  102. with module.Runtime_directory({}, 'prefix') as borgmatic_runtime_directory:
  103. assert borgmatic_runtime_directory == '/run/borgmatic-1234/./borgmatic'
  104. def test_runtime_directory_falls_back_to_hard_coded_tmp_path_and_adds_temporary_subdirectory_that_get_cleaned_up():
  105. flexmock(module).should_receive('expand_user_in_path').replace_with(lambda path: path)
  106. flexmock(module.os.environ).should_receive('get').with_args('XDG_RUNTIME_DIR').and_return(None)
  107. flexmock(module.os.environ).should_receive('get').with_args('RUNTIME_DIRECTORY').and_return(
  108. None
  109. )
  110. flexmock(module.os.environ).should_receive('get').with_args('TMPDIR').and_return(None)
  111. flexmock(module.os.environ).should_receive('get').with_args('TEMP').and_return(None)
  112. temporary_directory = flexmock(name='/tmp/borgmatic-1234')
  113. temporary_directory.should_receive('cleanup').once()
  114. flexmock(module.tempfile).should_receive('TemporaryDirectory').with_args(
  115. prefix='borgmatic-', dir='/tmp'
  116. ).and_return(temporary_directory)
  117. flexmock(module.os).should_receive('makedirs')
  118. with module.Runtime_directory({}, 'prefix') as borgmatic_runtime_directory:
  119. assert borgmatic_runtime_directory == '/tmp/borgmatic-1234/./borgmatic'
  120. def test_runtime_directory_with_erroring_cleanup_does_not_raise():
  121. flexmock(module).should_receive('expand_user_in_path').replace_with(lambda path: path)
  122. flexmock(module.os.environ).should_receive('get').with_args('XDG_RUNTIME_DIR').and_return(None)
  123. flexmock(module.os.environ).should_receive('get').with_args('RUNTIME_DIRECTORY').and_return(
  124. None
  125. )
  126. flexmock(module.os.environ).should_receive('get').with_args('TMPDIR').and_return(None)
  127. flexmock(module.os.environ).should_receive('get').with_args('TEMP').and_return(None)
  128. temporary_directory = flexmock(name='/tmp/borgmatic-1234')
  129. temporary_directory.should_receive('cleanup').and_raise(OSError).once()
  130. flexmock(module.tempfile).should_receive('TemporaryDirectory').with_args(
  131. prefix='borgmatic-', dir='/tmp'
  132. ).and_return(temporary_directory)
  133. flexmock(module.os).should_receive('makedirs')
  134. with module.Runtime_directory({}, 'prefix') as borgmatic_runtime_directory:
  135. assert borgmatic_runtime_directory == '/tmp/borgmatic-1234/./borgmatic'
  136. @pytest.mark.parametrize(
  137. 'borgmatic_runtime_directory,expected_glob',
  138. (
  139. ('/foo/bar/baz/./borgmatic', 'foo/bar/baz/borgmatic'),
  140. ('/foo/borgmatic/baz/./borgmatic', 'foo/borgmatic/baz/borgmatic'),
  141. ('/foo/borgmatic-jti8idds/./borgmatic', 'foo/*/borgmatic'),
  142. ),
  143. )
  144. def test_make_runtime_directory_glob(borgmatic_runtime_directory, expected_glob):
  145. assert module.make_runtime_directory_glob(borgmatic_runtime_directory) == expected_glob
  146. def test_get_borgmatic_state_directory_uses_config_option():
  147. flexmock(module).should_receive('expand_user_in_path').replace_with(lambda path: path)
  148. flexmock(module.os.environ).should_receive('get').never()
  149. assert (
  150. module.get_borgmatic_state_directory(
  151. {'user_state_directory': '/tmp', 'borgmatic_source_directory': '/nope'}
  152. )
  153. == '/tmp/borgmatic'
  154. )
  155. def test_get_borgmatic_state_directory_falls_back_to_xdg_state_home():
  156. flexmock(module).should_receive('expand_user_in_path').replace_with(lambda path: path)
  157. flexmock(module.os.environ).should_receive('get').with_args('XDG_STATE_HOME').and_return('/tmp')
  158. assert module.get_borgmatic_state_directory({}) == '/tmp/borgmatic'
  159. def test_get_borgmatic_state_directory_falls_back_to_state_directory():
  160. flexmock(module).should_receive('expand_user_in_path').replace_with(lambda path: path)
  161. flexmock(module.os.environ).should_receive('get').with_args('XDG_STATE_HOME').and_return(None)
  162. flexmock(module.os.environ).should_receive('get').with_args('STATE_DIRECTORY').and_return(
  163. '/tmp'
  164. )
  165. assert module.get_borgmatic_state_directory({}) == '/tmp/borgmatic'
  166. def test_get_borgmatic_state_directory_defaults_to_hard_coded_path():
  167. flexmock(module).should_receive('expand_user_in_path').replace_with(lambda path: path)
  168. flexmock(module.os.environ).should_receive('get').and_return(None)
  169. assert module.get_borgmatic_state_directory({}) == '~/.local/state/borgmatic'