test_paths.py 3.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687
  1. from flexmock import flexmock
  2. from borgmatic.config import paths as module
  3. def test_expand_user_in_path_passes_through_plain_directory():
  4. flexmock(module.os.path).should_receive('expanduser').and_return('/home/foo')
  5. assert module.expand_user_in_path('/home/foo') == '/home/foo'
  6. def test_expand_user_in_path_expands_tildes():
  7. flexmock(module.os.path).should_receive('expanduser').and_return('/home/foo')
  8. assert module.expand_user_in_path('~/foo') == '/home/foo'
  9. def test_expand_user_in_path_handles_empty_directory():
  10. assert module.expand_user_in_path('') is None
  11. def test_expand_user_in_path_handles_none_directory():
  12. assert module.expand_user_in_path(None) is None
  13. def test_get_borgmatic_source_directory_uses_config_option():
  14. flexmock(module).should_receive('expand_user_in_path').replace_with(lambda path: path)
  15. assert module.get_borgmatic_source_directory({'borgmatic_source_directory': '/tmp'}) == '/tmp'
  16. def test_get_borgmatic_source_directory_without_config_option_uses_default():
  17. flexmock(module).should_receive('expand_user_in_path').replace_with(lambda path: path)
  18. assert module.get_borgmatic_source_directory({}) == '~/.borgmatic'
  19. def test_get_borgmatic_runtime_directory_uses_config_option():
  20. flexmock(module).should_receive('expand_user_in_path').replace_with(lambda path: path)
  21. assert (
  22. module.get_borgmatic_runtime_directory(
  23. {'user_runtime_directory': '/tmp', 'borgmatic_source_directory': '/nope'}
  24. )
  25. == '/tmp/./borgmatic'
  26. )
  27. def test_get_borgmatic_runtime_directory_falls_back_to_environment_variable():
  28. flexmock(module).should_receive('expand_user_in_path').replace_with(lambda path: path)
  29. flexmock(module.os.environ).should_receive('get').with_args(
  30. 'XDG_RUNTIME_DIR', object
  31. ).and_return('/tmp')
  32. assert module.get_borgmatic_runtime_directory({}) == '/tmp/./borgmatic'
  33. def test_get_borgmatic_runtime_directory_defaults_to_hard_coded_path():
  34. flexmock(module).should_receive('expand_user_in_path').replace_with(lambda path: path)
  35. flexmock(module.os.environ).should_receive('get').and_return('/run/user/0')
  36. flexmock(module.os).should_receive('getuid').and_return(0)
  37. assert module.get_borgmatic_runtime_directory({}) == '/run/user/0/./borgmatic'
  38. def test_get_borgmatic_state_directory_uses_config_option():
  39. flexmock(module).should_receive('expand_user_in_path').replace_with(lambda path: path)
  40. assert (
  41. module.get_borgmatic_state_directory(
  42. {'user_state_directory': '/tmp', 'borgmatic_source_directory': '/nope'}
  43. )
  44. == '/tmp/borgmatic'
  45. )
  46. def test_get_borgmatic_state_directory_falls_back_to_environment_variable():
  47. flexmock(module).should_receive('expand_user_in_path').replace_with(lambda path: path)
  48. flexmock(module.os.environ).should_receive('get').with_args(
  49. 'XDG_STATE_HOME', object
  50. ).and_return('/tmp')
  51. assert module.get_borgmatic_state_directory({}) == '/tmp/borgmatic'
  52. def test_get_borgmatic_state_directory_defaults_to_hard_coded_path():
  53. flexmock(module).should_receive('expand_user_in_path').replace_with(lambda path: path)
  54. flexmock(module.os.environ).should_receive('get').and_return('/root/.local/state')
  55. assert module.get_borgmatic_state_directory({}) == '/root/.local/state/borgmatic'