| 123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306 | import pytestfrom flexmock import flexmockfrom borgmatic.config import paths as moduledef test_expand_user_in_path_passes_through_plain_directory():    flexmock(module.os.path).should_receive('expanduser').and_return('/home/foo')    assert module.expand_user_in_path('/home/foo') == '/home/foo'def test_expand_user_in_path_expands_tildes():    flexmock(module.os.path).should_receive('expanduser').and_return('/home/foo')    assert module.expand_user_in_path('~/foo') == '/home/foo'def test_expand_user_in_path_handles_empty_directory():    assert module.expand_user_in_path('') is Nonedef test_expand_user_in_path_handles_none_directory():    assert module.expand_user_in_path(None) is Nonedef test_expand_user_in_path_handles_incorrectly_typed_directory():    assert module.expand_user_in_path(3) is Nonedef test_get_borgmatic_source_directory_uses_config_option():    flexmock(module).should_receive('expand_user_in_path').replace_with(lambda path: path)    assert module.get_borgmatic_source_directory({'borgmatic_source_directory': '/tmp'}) == '/tmp'def test_get_borgmatic_source_directory_without_config_option_uses_default():    flexmock(module).should_receive('expand_user_in_path').replace_with(lambda path: path)    assert module.get_borgmatic_source_directory({}) == '~/.borgmatic'def test_replace_temporary_subdirectory_with_glob_transforms_path():    assert (        module.replace_temporary_subdirectory_with_glob('/tmp/borgmatic-aet8kn93/borgmatic')        == '/tmp/borgmatic-*/borgmatic'    )def test_replace_temporary_subdirectory_with_glob_passes_through_non_matching_path():    assert (        module.replace_temporary_subdirectory_with_glob('/tmp/foo-aet8kn93/borgmatic')        == '/tmp/foo-aet8kn93/borgmatic'    )def test_replace_temporary_subdirectory_with_glob_uses_custom_temporary_directory_prefix():    assert (        module.replace_temporary_subdirectory_with_glob(            '/tmp/.borgmatic-aet8kn93/borgmatic', temporary_directory_prefix='.borgmatic-'        )        == '/tmp/.borgmatic-*/borgmatic'    )def test_runtime_directory_uses_config_option():    flexmock(module).should_receive('expand_user_in_path').replace_with(lambda path: path)    flexmock(module.os).should_receive('makedirs')    config = {'user_runtime_directory': '/run', 'borgmatic_source_directory': '/nope'}    with module.Runtime_directory(config) as borgmatic_runtime_directory:        assert borgmatic_runtime_directory == '/run/./borgmatic'def test_runtime_directory_uses_config_option_without_adding_duplicate_borgmatic_subdirectory():    flexmock(module).should_receive('expand_user_in_path').replace_with(lambda path: path)    flexmock(module.os).should_receive('makedirs')    config = {'user_runtime_directory': '/run/borgmatic', 'borgmatic_source_directory': '/nope'}    with module.Runtime_directory(config) as borgmatic_runtime_directory:        assert borgmatic_runtime_directory == '/run/./borgmatic'def test_runtime_directory_with_relative_config_option_errors():    flexmock(module.os).should_receive('makedirs').never()    config = {'user_runtime_directory': 'run', 'borgmatic_source_directory': '/nope'}    with pytest.raises(ValueError):        with module.Runtime_directory(config) as borgmatic_runtime_directory:  # noqa: F841            passdef test_runtime_directory_falls_back_to_xdg_runtime_dir():    flexmock(module).should_receive('expand_user_in_path').replace_with(lambda path: path)    flexmock(module.os.environ).should_receive('get').with_args('XDG_RUNTIME_DIR').and_return(        '/run'    )    flexmock(module.os).should_receive('makedirs')    with module.Runtime_directory({}) as borgmatic_runtime_directory:        assert borgmatic_runtime_directory == '/run/./borgmatic'def test_runtime_directory_falls_back_to_xdg_runtime_dir_without_adding_duplicate_borgmatic_subdirectory():    flexmock(module).should_receive('expand_user_in_path').replace_with(lambda path: path)    flexmock(module.os.environ).should_receive('get').with_args('XDG_RUNTIME_DIR').and_return(        '/run/borgmatic'    )    flexmock(module.os).should_receive('makedirs')    with module.Runtime_directory({}) as borgmatic_runtime_directory:        assert borgmatic_runtime_directory == '/run/./borgmatic'def test_runtime_directory_with_relative_xdg_runtime_dir_errors():    flexmock(module.os.environ).should_receive('get').with_args('XDG_RUNTIME_DIR').and_return('run')    flexmock(module.os).should_receive('makedirs').never()    with pytest.raises(ValueError):        with module.Runtime_directory({}) as borgmatic_runtime_directory:  # noqa: F841            passdef test_runtime_directory_falls_back_to_runtime_directory():    flexmock(module).should_receive('expand_user_in_path').replace_with(lambda path: path)    flexmock(module.os.environ).should_receive('get').with_args('XDG_RUNTIME_DIR').and_return(None)    flexmock(module.os.environ).should_receive('get').with_args('RUNTIME_DIRECTORY').and_return(        '/run'    )    flexmock(module.os).should_receive('makedirs')    with module.Runtime_directory({}) as borgmatic_runtime_directory:        assert borgmatic_runtime_directory == '/run/./borgmatic'def test_runtime_directory_falls_back_to_runtime_directory_without_adding_duplicate_borgmatic_subdirectory():    flexmock(module).should_receive('expand_user_in_path').replace_with(lambda path: path)    flexmock(module.os.environ).should_receive('get').with_args('XDG_RUNTIME_DIR').and_return(None)    flexmock(module.os.environ).should_receive('get').with_args('RUNTIME_DIRECTORY').and_return(        '/run/borgmatic'    )    flexmock(module.os).should_receive('makedirs')    with module.Runtime_directory({}) as borgmatic_runtime_directory:        assert borgmatic_runtime_directory == '/run/./borgmatic'def test_runtime_directory_with_relative_runtime_directory_errors():    flexmock(module.os.environ).should_receive('get').with_args('XDG_RUNTIME_DIR').and_return(None)    flexmock(module.os.environ).should_receive('get').with_args('RUNTIME_DIRECTORY').and_return(        'run'    )    flexmock(module.os).should_receive('makedirs').never()    with pytest.raises(ValueError):        with module.Runtime_directory({}) as borgmatic_runtime_directory:  # noqa: F841            passdef test_runtime_directory_falls_back_to_tmpdir_and_adds_temporary_subdirectory_that_get_cleaned_up():    flexmock(module).should_receive('expand_user_in_path').replace_with(lambda path: path)    flexmock(module.os.environ).should_receive('get').with_args('XDG_RUNTIME_DIR').and_return(None)    flexmock(module.os.environ).should_receive('get').with_args('RUNTIME_DIRECTORY').and_return(        None    )    flexmock(module.os.environ).should_receive('get').with_args('TMPDIR').and_return('/run')    temporary_directory = flexmock(name='/run/borgmatic-1234')    temporary_directory.should_receive('cleanup').once()    flexmock(module.tempfile).should_receive('TemporaryDirectory').with_args(        prefix='borgmatic-', dir='/run'    ).and_return(temporary_directory)    flexmock(module.os).should_receive('makedirs')    with module.Runtime_directory({}) as borgmatic_runtime_directory:        assert borgmatic_runtime_directory == '/run/borgmatic-1234/./borgmatic'def test_runtime_directory_with_relative_tmpdir_errors():    flexmock(module.os.environ).should_receive('get').with_args('XDG_RUNTIME_DIR').and_return(None)    flexmock(module.os.environ).should_receive('get').with_args('RUNTIME_DIRECTORY').and_return(        None    )    flexmock(module.os.environ).should_receive('get').with_args('TMPDIR').and_return('run')    flexmock(module.tempfile).should_receive('TemporaryDirectory').never()    flexmock(module.os).should_receive('makedirs').never()    with pytest.raises(ValueError):        with module.Runtime_directory({}) as borgmatic_runtime_directory:  # noqa: F841            passdef test_runtime_directory_falls_back_to_temp_and_adds_temporary_subdirectory_that_get_cleaned_up():    flexmock(module).should_receive('expand_user_in_path').replace_with(lambda path: path)    flexmock(module.os.environ).should_receive('get').with_args('XDG_RUNTIME_DIR').and_return(None)    flexmock(module.os.environ).should_receive('get').with_args('RUNTIME_DIRECTORY').and_return(        None    )    flexmock(module.os.environ).should_receive('get').with_args('TMPDIR').and_return(None)    flexmock(module.os.environ).should_receive('get').with_args('TEMP').and_return('/run')    temporary_directory = flexmock(name='/run/borgmatic-1234')    temporary_directory.should_receive('cleanup').once()    flexmock(module.tempfile).should_receive('TemporaryDirectory').with_args(        prefix='borgmatic-', dir='/run'    ).and_return(temporary_directory)    flexmock(module.os).should_receive('makedirs')    with module.Runtime_directory({}) as borgmatic_runtime_directory:        assert borgmatic_runtime_directory == '/run/borgmatic-1234/./borgmatic'def test_runtime_directory_with_relative_temp_errors():    flexmock(module.os.environ).should_receive('get').with_args('XDG_RUNTIME_DIR').and_return(None)    flexmock(module.os.environ).should_receive('get').with_args('RUNTIME_DIRECTORY').and_return(        None    )    flexmock(module.os.environ).should_receive('get').with_args('TMPDIR').and_return(None)    flexmock(module.os.environ).should_receive('get').with_args('TEMP').and_return('run')    flexmock(module.tempfile).should_receive('TemporaryDirectory').never()    flexmock(module.os).should_receive('makedirs')    with pytest.raises(ValueError):        with module.Runtime_directory({}) as borgmatic_runtime_directory:  # noqa: F841            passdef test_runtime_directory_falls_back_to_hard_coded_tmp_path_and_adds_temporary_subdirectory_that_get_cleaned_up():    flexmock(module).should_receive('expand_user_in_path').replace_with(lambda path: path)    flexmock(module.os.environ).should_receive('get').with_args('XDG_RUNTIME_DIR').and_return(None)    flexmock(module.os.environ).should_receive('get').with_args('RUNTIME_DIRECTORY').and_return(        None    )    flexmock(module.os.environ).should_receive('get').with_args('TMPDIR').and_return(None)    flexmock(module.os.environ).should_receive('get').with_args('TEMP').and_return(None)    temporary_directory = flexmock(name='/tmp/borgmatic-1234')    temporary_directory.should_receive('cleanup').once()    flexmock(module.tempfile).should_receive('TemporaryDirectory').with_args(        prefix='borgmatic-', dir='/tmp'    ).and_return(temporary_directory)    flexmock(module.os).should_receive('makedirs')    with module.Runtime_directory({}) as borgmatic_runtime_directory:        assert borgmatic_runtime_directory == '/tmp/borgmatic-1234/./borgmatic'def test_runtime_directory_with_erroring_cleanup_does_not_raise():    flexmock(module).should_receive('expand_user_in_path').replace_with(lambda path: path)    flexmock(module.os.environ).should_receive('get').with_args('XDG_RUNTIME_DIR').and_return(None)    flexmock(module.os.environ).should_receive('get').with_args('RUNTIME_DIRECTORY').and_return(        None    )    flexmock(module.os.environ).should_receive('get').with_args('TMPDIR').and_return(None)    flexmock(module.os.environ).should_receive('get').with_args('TEMP').and_return(None)    temporary_directory = flexmock(name='/tmp/borgmatic-1234')    temporary_directory.should_receive('cleanup').and_raise(OSError).once()    flexmock(module.tempfile).should_receive('TemporaryDirectory').with_args(        prefix='borgmatic-', dir='/tmp'    ).and_return(temporary_directory)    flexmock(module.os).should_receive('makedirs')    with module.Runtime_directory({}) as borgmatic_runtime_directory:        assert borgmatic_runtime_directory == '/tmp/borgmatic-1234/./borgmatic'@pytest.mark.parametrize(    'borgmatic_runtime_directory,expected_glob',    (        ('/foo/bar/baz/./borgmatic', 'foo/bar/baz/borgmatic'),        ('/foo/borgmatic/baz/./borgmatic', 'foo/borgmatic/baz/borgmatic'),        ('/foo/borgmatic-jti8idds/./borgmatic', 'foo/*/borgmatic'),    ),)def test_make_runtime_directory_glob(borgmatic_runtime_directory, expected_glob):    assert module.make_runtime_directory_glob(borgmatic_runtime_directory) == expected_globdef test_get_borgmatic_state_directory_uses_config_option():    flexmock(module).should_receive('expand_user_in_path').replace_with(lambda path: path)    flexmock(module.os.environ).should_receive('get').never()    assert (        module.get_borgmatic_state_directory(            {'user_state_directory': '/tmp', 'borgmatic_source_directory': '/nope'}        )        == '/tmp/borgmatic'    )def test_get_borgmatic_state_directory_falls_back_to_xdg_state_home():    flexmock(module).should_receive('expand_user_in_path').replace_with(lambda path: path)    flexmock(module.os.environ).should_receive('get').with_args('XDG_STATE_HOME').and_return('/tmp')    assert module.get_borgmatic_state_directory({}) == '/tmp/borgmatic'def test_get_borgmatic_state_directory_falls_back_to_state_directory():    flexmock(module).should_receive('expand_user_in_path').replace_with(lambda path: path)    flexmock(module.os.environ).should_receive('get').with_args('XDG_STATE_HOME').and_return(None)    flexmock(module.os.environ).should_receive('get').with_args('STATE_DIRECTORY').and_return(        '/tmp'    )    assert module.get_borgmatic_state_directory({}) == '/tmp/borgmatic'def test_get_borgmatic_state_directory_defaults_to_hard_coded_path():    flexmock(module).should_receive('expand_user_in_path').replace_with(lambda path: path)    flexmock(module.os.environ).should_receive('get').and_return(None)    assert module.get_borgmatic_state_directory({}) == '~/.local/state/borgmatic'
 |