diff --git a/borg/helpers.py b/borg/helpers.py index 62694e0a1..28231a275 100644 --- a/borg/helpers.py +++ b/borg/helpers.py @@ -210,8 +210,8 @@ class Statistics: def get_keys_dir(): """Determine where to repository keys and cache""" - return os.environ.get('BORG_KEYS_DIR', - os.path.join(os.path.expanduser('~'), '.borg', 'keys')) + xdg_config = os.environ.get('XDG_CONFIG_HOME', os.path.join(os.path.expanduser('~'), '.config')) + return os.environ.get('BORG_KEYS_DIR', os.path.join(xdg_config, 'borg', 'keys')) def get_cache_dir(): diff --git a/borg/testsuite/helpers.py b/borg/testsuite/helpers.py index 53b2ed5b9..0d68fe97b 100644 --- a/borg/testsuite/helpers.py +++ b/borg/testsuite/helpers.py @@ -10,7 +10,8 @@ import msgpack import msgpack.fallback from ..helpers import Location, format_file_size, format_timedelta, make_path_safe, \ - prune_within, prune_split, get_cache_dir, Statistics, is_slow_msgpack, yes, TRUISH, FALSISH, DEFAULTISH, \ + prune_within, prune_split, get_cache_dir, get_keys_dir, Statistics, is_slow_msgpack, \ + yes, TRUISH, FALSISH, DEFAULTISH, \ StableDict, int_to_bigint, bigint_to_int, parse_timestamp, CompressionSpec, ChunkerParams, \ ProgressIndicatorPercent, ProgressIndicatorEndless, load_excludes, parse_pattern, \ PatternMatcher, RegexPattern, PathPrefixPattern, FnmatchPattern, ShellPattern @@ -579,7 +580,7 @@ class TestParseTimestamp(BaseTestCase): def test_get_cache_dir(): - """test that get_cache_dir respects environement""" + """test that get_cache_dir respects environment""" # reset BORG_CACHE_DIR in order to test default old_env = None if os.environ.get('BORG_CACHE_DIR'): @@ -595,6 +596,23 @@ def test_get_cache_dir(): os.environ['BORG_CACHE_DIR'] = old_env +def test_get_keys_dir(): + """test that get_keys_dir respects environment""" + # reset BORG_KEYS_DIR in order to test default + old_env = None + if os.environ.get('BORG_KEYS_DIR'): + old_env = os.environ['BORG_KEYS_DIR'] + del(os.environ['BORG_KEYS_DIR']) + assert get_keys_dir() == os.path.join(os.path.expanduser('~'), '.config', 'borg', 'keys') + os.environ['XDG_CONFIG_HOME'] = '/var/tmp/.config' + assert get_keys_dir() == os.path.join('/var/tmp/.config', 'borg', 'keys') + os.environ['BORG_KEYS_DIR'] = '/var/tmp' + assert get_keys_dir() == '/var/tmp' + # reset old env + if old_env is not None: + os.environ['BORG_KEYS_DIR'] = old_env + + @pytest.fixture() def stats(): stats = Statistics()