From f2452aef2ae247ef478b4be2703041c14bfeea72 Mon Sep 17 00:00:00 2001 From: Rayyan Ansari Date: Tue, 24 Jan 2023 18:10:49 +0000 Subject: [PATCH 01/20] helpers: use platformdirs on win32 --- src/borg/helpers/fs.py | 50 +++++++++++++++---------- src/borg/testsuite/helpers.py | 69 +++++++++++++++++++++++------------ 2 files changed, 76 insertions(+), 43 deletions(-) diff --git a/src/borg/helpers/fs.py b/src/borg/helpers/fs.py index d69a88715..ef135877c 100644 --- a/src/borg/helpers/fs.py +++ b/src/borg/helpers/fs.py @@ -8,6 +8,8 @@ import subprocess import sys import textwrap +import platformdirs + from .errors import Error from .process import prepare_subprocess_env @@ -58,37 +60,42 @@ def get_base_dir(): return base_dir -def get_keys_dir(): +def get_keys_dir(legacy=True): """Determine where to repository keys and cache""" keys_dir = os.environ.get("BORG_KEYS_DIR") if keys_dir is None: # note: do not just give this as default to the environment.get(), see issue #5979. - keys_dir = os.path.join(get_config_dir(), "keys") + keys_dir = os.path.join(get_config_dir(legacy), "keys") ensure_dir(keys_dir) return keys_dir -def get_security_dir(repository_id=None): +def get_security_dir(repository_id=None, legacy=True): """Determine where to store local security information.""" security_dir = os.environ.get("BORG_SECURITY_DIR") if security_dir is None: # note: do not just give this as default to the environment.get(), see issue #5979. - security_dir = os.path.join(get_config_dir(), "security") + security_dir = os.path.join(get_config_dir(legacy), "security") if repository_id: security_dir = os.path.join(security_dir, repository_id) ensure_dir(security_dir) return security_dir -def get_cache_dir(): +def get_cache_dir(legacy=True): """Determine where to repository keys and cache""" - # Get cache home path - cache_home = os.path.join(get_base_dir(), ".cache") - # Try to use XDG_CACHE_HOME instead if BORG_BASE_DIR isn't explicitly set - if not os.environ.get("BORG_BASE_DIR"): - cache_home = os.environ.get("XDG_CACHE_HOME", cache_home) - # Use BORG_CACHE_DIR if set, otherwise assemble final path from cache home path - cache_dir = os.environ.get("BORG_CACHE_DIR", os.path.join(cache_home, "borg")) + + if legacy: + # Get cache home path + cache_home = os.path.join(get_base_dir(), ".cache") + # Try to use XDG_CACHE_HOME instead if BORG_BASE_DIR isn't explicitly set + if not os.environ.get("BORG_BASE_DIR"): + cache_home = os.environ.get("XDG_CACHE_HOME", cache_home) + # Use BORG_CACHE_DIR if set, otherwise assemble final path from cache home path + cache_dir = os.environ.get("BORG_CACHE_DIR", os.path.join(cache_home, "borg")) + else: + cache_dir = os.environ.get("BORG_CACHE_DIR", platformdirs.user_cache_dir("borg")) + # Create path if it doesn't exist yet ensure_dir(cache_dir) cache_tag_fn = os.path.join(cache_dir, CACHE_TAG_NAME) @@ -110,15 +117,20 @@ def get_cache_dir(): return cache_dir -def get_config_dir(): +def get_config_dir(legacy=True): """Determine where to store whole config""" + # Get config home path - config_home = os.path.join(get_base_dir(), ".config") - # Try to use XDG_CONFIG_HOME instead if BORG_BASE_DIR isn't explicitly set - if not os.environ.get("BORG_BASE_DIR"): - config_home = os.environ.get("XDG_CONFIG_HOME", config_home) - # Use BORG_CONFIG_DIR if set, otherwise assemble final path from config home path - config_dir = os.environ.get("BORG_CONFIG_DIR", os.path.join(config_home, "borg")) + if legacy: + config_home = os.path.join(get_base_dir(), ".config") + # Try to use XDG_CONFIG_HOME instead if BORG_BASE_DIR isn't explicitly set + if not os.environ.get("BORG_BASE_DIR"): + config_home = os.environ.get("XDG_CONFIG_HOME", config_home) + # Use BORG_CONFIG_DIR if set, otherwise assemble final path from config home path + config_dir = os.environ.get("BORG_CONFIG_DIR", os.path.join(config_home, "borg")) + else: + config_dir = os.environ.get("BORG_CONFIG_DIR", platformdirs.user_config_dir("borg")) + # Create path if it doesn't exist yet ensure_dir(config_dir) return config_dir diff --git a/src/borg/testsuite/helpers.py b/src/borg/testsuite/helpers.py index 5cf1900d9..a9c0d6e34 100644 --- a/src/borg/testsuite/helpers.py +++ b/src/borg/testsuite/helpers.py @@ -45,7 +45,7 @@ from ..helpers import eval_escapes from ..helpers import safe_unlink from ..helpers import text_to_json, binary_to_json from ..helpers.passphrase import Passphrase, PasswordRetriesExceeded -from ..platform import is_cygwin +from ..platform import is_cygwin, is_win32 from . import BaseTestCase, FakeInputs, are_hardlinks_supported @@ -597,47 +597,68 @@ def test_get_config_dir(monkeypatch): """test that get_config_dir respects environment""" monkeypatch.delenv("BORG_CONFIG_DIR", raising=False) monkeypatch.delenv("XDG_CONFIG_HOME", raising=False) - assert get_config_dir() == os.path.join(os.path.expanduser("~"), ".config", "borg") - monkeypatch.setenv("XDG_CONFIG_HOME", "/var/tmp/.config") - assert get_config_dir() == os.path.join("/var/tmp/.config", "borg") - monkeypatch.setenv("BORG_CONFIG_DIR", "/var/tmp") - assert get_config_dir() == "/var/tmp" + if is_win32: + assert get_config_dir(legacy=False) == os.path.join(os.path.expanduser("~"), "AppData", "Local", "borg", "borg") + else: + assert get_config_dir() == os.path.join(os.path.expanduser("~"), ".config", "borg") + monkeypatch.setenv("XDG_CONFIG_HOME", "/var/tmp/.config") + assert get_config_dir() == os.path.join("/var/tmp/.config", "borg") + monkeypatch.setenv("BORG_CONFIG_DIR", "/var/tmp") + assert get_config_dir() == "/var/tmp" def test_get_cache_dir(monkeypatch): """test that get_cache_dir respects environment""" monkeypatch.delenv("BORG_CACHE_DIR", raising=False) monkeypatch.delenv("XDG_CACHE_HOME", raising=False) - assert get_cache_dir() == os.path.join(os.path.expanduser("~"), ".cache", "borg") - monkeypatch.setenv("XDG_CACHE_HOME", "/var/tmp/.cache") - assert get_cache_dir() == os.path.join("/var/tmp/.cache", "borg") - monkeypatch.setenv("BORG_CACHE_DIR", "/var/tmp") - assert get_cache_dir() == "/var/tmp" + if is_win32: + assert get_cache_dir(legacy=False) == os.path.join( + os.path.expanduser("~"), "AppData", "Local", "borg", "borg", "Cache" + ) + else: + assert get_cache_dir() == os.path.join(os.path.expanduser("~"), ".cache", "borg") + monkeypatch.setenv("XDG_CACHE_HOME", "/var/tmp/.cache") + assert get_cache_dir() == os.path.join("/var/tmp/.cache", "borg") + monkeypatch.setenv("BORG_CACHE_DIR", "/var/tmp") + assert get_cache_dir() == "/var/tmp" def test_get_keys_dir(monkeypatch): """test that get_keys_dir respects environment""" monkeypatch.delenv("BORG_KEYS_DIR", raising=False) monkeypatch.delenv("XDG_CONFIG_HOME", raising=False) - assert get_keys_dir() == os.path.join(os.path.expanduser("~"), ".config", "borg", "keys") - monkeypatch.setenv("XDG_CONFIG_HOME", "/var/tmp/.config") - assert get_keys_dir() == os.path.join("/var/tmp/.config", "borg", "keys") - monkeypatch.setenv("BORG_KEYS_DIR", "/var/tmp") - assert get_keys_dir() == "/var/tmp" + if is_win32: + assert get_keys_dir(legacy=False) == os.path.join( + os.path.expanduser("~"), "AppData", "Local", "borg", "borg", "keys" + ) + else: + assert get_keys_dir() == os.path.join(os.path.expanduser("~"), ".config", "borg", "keys") + monkeypatch.setenv("XDG_CONFIG_HOME", "/var/tmp/.config") + assert get_keys_dir() == os.path.join("/var/tmp/.config", "borg", "keys") + monkeypatch.setenv("BORG_KEYS_DIR", "/var/tmp") + assert get_keys_dir() == "/var/tmp" def test_get_security_dir(monkeypatch): """test that get_security_dir respects environment""" monkeypatch.delenv("BORG_SECURITY_DIR", raising=False) monkeypatch.delenv("XDG_CONFIG_HOME", raising=False) - assert get_security_dir() == os.path.join(os.path.expanduser("~"), ".config", "borg", "security") - assert get_security_dir(repository_id="1234") == os.path.join( - os.path.expanduser("~"), ".config", "borg", "security", "1234" - ) - monkeypatch.setenv("XDG_CONFIG_HOME", "/var/tmp/.config") - assert get_security_dir() == os.path.join("/var/tmp/.config", "borg", "security") - monkeypatch.setenv("BORG_SECURITY_DIR", "/var/tmp") - assert get_security_dir() == "/var/tmp" + if is_win32: + assert get_security_dir(legacy=False) == os.path.join( + os.path.expanduser("~"), "AppData", "Local", "borg", "borg", "security" + ) + assert get_security_dir(repository_id="1234", legacy=False) == os.path.join( + os.path.expanduser("~"), "AppData", "Local", "borg", "borg", "security", "1234" + ) + else: + assert get_security_dir() == os.path.join(os.path.expanduser("~"), ".config", "borg", "security") + assert get_security_dir(repository_id="1234") == os.path.join( + os.path.expanduser("~"), ".config", "borg", "security", "1234" + ) + monkeypatch.setenv("XDG_CONFIG_HOME", "/var/tmp/.config") + assert get_security_dir() == os.path.join("/var/tmp/.config", "borg", "security") + monkeypatch.setenv("BORG_SECURITY_DIR", "/var/tmp") + assert get_security_dir() == "/var/tmp" def test_file_size(): From 6c7efbe128bddca6f593bd4a17dc5cc84016f251 Mon Sep 17 00:00:00 2001 From: Rayyan Ansari Date: Tue, 24 Jan 2023 18:14:28 +0000 Subject: [PATCH 02/20] add platformdirs to dependencies --- scripts/msys2-install-deps | 2 +- setup.cfg | 1 + 2 files changed, 2 insertions(+), 1 deletion(-) diff --git a/scripts/msys2-install-deps b/scripts/msys2-install-deps index 39fae4c70..227f505d4 100644 --- a/scripts/msys2-install-deps +++ b/scripts/msys2-install-deps @@ -1,6 +1,6 @@ #!/bin/bash -pacman -S --needed --noconfirm git mingw-w64-ucrt-x86_64-{toolchain,pkgconf,zstd,lz4,xxhash,openssl,python,cython,python-setuptools,python-wheel,python-pkgconfig,python-packaging,python-msgpack,python-argon2_cffi,python-pip} +pacman -S --needed --noconfirm git mingw-w64-ucrt-x86_64-{toolchain,pkgconf,zstd,lz4,xxhash,openssl,python-msgpack,python-argon2_cffi,python-platformdirs,python,cython,python-setuptools,python-wheel,python-pkgconfig,python-packaging,python-pip} pip install pyinstaller if [ "$1" = "development" ]; then diff --git a/setup.cfg b/setup.cfg index 127e86356..6984f610d 100644 --- a/setup.cfg +++ b/setup.cfg @@ -41,6 +41,7 @@ setup_requires = install_requires = msgpack >=1.0.3, <=1.0.4 packaging + platformdirs argon2-cffi tests_require = pytest From d3d909ad3172f56eb38c1b484e531053970bb54f Mon Sep 17 00:00:00 2001 From: Thomas Waldmann Date: Fri, 3 Feb 2023 18:22:51 +0100 Subject: [PATCH 03/20] add compatibility test: legacy vs. non-legacy platformdirs --- src/borg/testsuite/helpers.py | 13 +++++++++++++ 1 file changed, 13 insertions(+) diff --git a/src/borg/testsuite/helpers.py b/src/borg/testsuite/helpers.py index a9c0d6e34..ca10d9980 100644 --- a/src/borg/testsuite/helpers.py +++ b/src/borg/testsuite/helpers.py @@ -607,6 +607,19 @@ def test_get_config_dir(monkeypatch): assert get_config_dir() == "/var/tmp" +def test_get_config_dir_compat(monkeypatch): + """test that it works the same for legacy and for non-legacy implementation""" + monkeypatch.delenv("BORG_CONFIG_DIR", raising=False) + monkeypatch.delenv("XDG_CONFIG_HOME", raising=False) + assert get_config_dir(legacy=False) == get_config_dir(legacy=True) + # TODO fails on macOS: assert '/Users/tw/Library/Preferences/borg' == '/Users/tw/.config/borg' + monkeypatch.setenv("XDG_CONFIG_HOME", "/var/tmp/.config1") + assert get_config_dir(legacy=False) == get_config_dir(legacy=True) + # TODO fails on macOS: assert '/Users/tw/Library/Preferences/borg' == '/var/tmp/.config1/borg' + monkeypatch.setenv("BORG_CONFIG_DIR", "/var/tmp/.config2") + assert get_config_dir(legacy=False) == get_config_dir(legacy=True) + + def test_get_cache_dir(monkeypatch): """test that get_cache_dir respects environment""" monkeypatch.delenv("BORG_CACHE_DIR", raising=False) From 8379ecefaa68862b1e60a6ee9fca6bbcdf126fea Mon Sep 17 00:00:00 2001 From: Thomas Waldmann Date: Fri, 3 Feb 2023 20:36:43 +0100 Subject: [PATCH 04/20] deal with BORG_BASE_DIR --- src/borg/helpers/__init__.py | 2 +- src/borg/helpers/fs.py | 39 ++++++++++++++++++++++++----------- src/borg/testsuite/helpers.py | 11 ++++++++++ 3 files changed, 39 insertions(+), 13 deletions(-) diff --git a/src/borg/helpers/__init__.py b/src/borg/helpers/__init__.py index 82cd027a5..e42d78415 100644 --- a/src/borg/helpers/__init__.py +++ b/src/borg/helpers/__init__.py @@ -11,7 +11,7 @@ from ..constants import * # NOQA from .checks import check_extension_modules, check_python from .datastruct import StableDict, Buffer, EfficientCollectionQueue from .errors import Error, ErrorWithTraceback, IntegrityError, DecompressionError -from .fs import ensure_dir, get_security_dir, get_keys_dir, get_base_dir, get_cache_dir, get_config_dir +from .fs import ensure_dir, get_security_dir, get_keys_dir, get_base_dir, join_base_dir, get_cache_dir, get_config_dir from .fs import dir_is_tagged, dir_is_cachedir, make_path_safe, scandir_inorder from .fs import secure_erase, safe_unlink, dash_open, os_open, os_stat, umount from .fs import O_, flags_root, flags_dir, flags_special_follow, flags_special, flags_base, flags_normal, flags_noatime diff --git a/src/borg/helpers/fs.py b/src/borg/helpers/fs.py index ef135877c..ee2ae1aaa 100644 --- a/src/borg/helpers/fs.py +++ b/src/borg/helpers/fs.py @@ -42,7 +42,7 @@ def ensure_dir(path, mode=stat.S_IRWXU | stat.S_IRWXG | stat.S_IRWXO, pretty_dea raise -def get_base_dir(): +def get_base_dir(legacy=True): """Get home directory / base directory for borg: - BORG_BASE_DIR, if set @@ -50,16 +50,27 @@ def get_base_dir(): - ~$USER, if USER is set - ~ """ - base_dir = os.environ.get("BORG_BASE_DIR") or os.environ.get("HOME") - # os.path.expanduser() behaves differently for '~' and '~someuser' as - # parameters: when called with an explicit username, the possibly set - # environment variable HOME is no longer respected. So we have to check if - # it is set and only expand the user's home directory if HOME is unset. - if not base_dir: - base_dir = os.path.expanduser("~%s" % os.environ.get("USER", "")) + if legacy: + base_dir = os.environ.get("BORG_BASE_DIR") or os.environ.get("HOME") + # os.path.expanduser() behaves differently for '~' and '~someuser' as + # parameters: when called with an explicit username, the possibly set + # environment variable HOME is no longer respected. So we have to check if + # it is set and only expand the user's home directory if HOME is unset. + if not base_dir: + base_dir = os.path.expanduser("~%s" % os.environ.get("USER", "")) + else: + # we only care for BORG_BASE_DIR here, as it can be used to override the base dir + # and not use any more or less platform specific way to determine the base dir. + base_dir = os.environ.get("BORG_BASE_DIR") return base_dir +def join_base_dir(*paths, **kw): + legacy = kw.get("legacy", True) + base_dir = get_base_dir(legacy=legacy) + return None if base_dir is None else os.path.join(base_dir, *paths) + + def get_keys_dir(legacy=True): """Determine where to repository keys and cache""" keys_dir = os.environ.get("BORG_KEYS_DIR") @@ -87,14 +98,16 @@ def get_cache_dir(legacy=True): if legacy: # Get cache home path - cache_home = os.path.join(get_base_dir(), ".cache") + cache_home = join_base_dir(".cache", legacy=legacy) # Try to use XDG_CACHE_HOME instead if BORG_BASE_DIR isn't explicitly set if not os.environ.get("BORG_BASE_DIR"): cache_home = os.environ.get("XDG_CACHE_HOME", cache_home) # Use BORG_CACHE_DIR if set, otherwise assemble final path from cache home path cache_dir = os.environ.get("BORG_CACHE_DIR", os.path.join(cache_home, "borg")) else: - cache_dir = os.environ.get("BORG_CACHE_DIR", platformdirs.user_cache_dir("borg")) + cache_dir = os.environ.get( + "BORG_CACHE_DIR", join_base_dir(".cache", legacy=legacy) or platformdirs.user_cache_dir("borg") + ) # Create path if it doesn't exist yet ensure_dir(cache_dir) @@ -122,14 +135,16 @@ def get_config_dir(legacy=True): # Get config home path if legacy: - config_home = os.path.join(get_base_dir(), ".config") + config_home = join_base_dir(".config", legacy=legacy) # Try to use XDG_CONFIG_HOME instead if BORG_BASE_DIR isn't explicitly set if not os.environ.get("BORG_BASE_DIR"): config_home = os.environ.get("XDG_CONFIG_HOME", config_home) # Use BORG_CONFIG_DIR if set, otherwise assemble final path from config home path config_dir = os.environ.get("BORG_CONFIG_DIR", os.path.join(config_home, "borg")) else: - config_dir = os.environ.get("BORG_CONFIG_DIR", platformdirs.user_config_dir("borg")) + config_dir = os.environ.get( + "BORG_CONFIG_DIR", join_base_dir(".config", legacy=legacy) or platformdirs.user_config_dir("borg") + ) # Create path if it doesn't exist yet ensure_dir(config_dir) diff --git a/src/borg/testsuite/helpers.py b/src/borg/testsuite/helpers.py index ca10d9980..8a4fb6cc2 100644 --- a/src/borg/testsuite/helpers.py +++ b/src/borg/testsuite/helpers.py @@ -593,6 +593,17 @@ def test_get_base_dir(monkeypatch): assert get_base_dir() == "/var/tmp/base" +def test_get_base_dir_compat(monkeypatch): + """test that it works the same for legacy and for non-legacy implementation""" + monkeypatch.delenv("BORG_BASE_DIR", raising=False) + # old way: if BORG_BASE_DIR is not set, make something up with HOME/USER/~ + # new way: if BORG_BASE_DIR is not set, return None and let caller deal with it. + assert get_base_dir(legacy=False) is None + # new and old way: BORG_BASE_DIR overrides all other "base path determination". + monkeypatch.setenv("BORG_BASE_DIR", "/var/tmp/base") + assert get_base_dir(legacy=False) == get_base_dir(legacy=True) + + def test_get_config_dir(monkeypatch): """test that get_config_dir respects environment""" monkeypatch.delenv("BORG_CONFIG_DIR", raising=False) From 437b5fa859cd108b5c37b162f9da63badb0e8a9d Mon Sep 17 00:00:00 2001 From: Thomas Waldmann Date: Fri, 3 Feb 2023 22:20:53 +0100 Subject: [PATCH 05/20] do not fail on macOS due to different dirs from platformdirs --- src/borg/testsuite/helpers.py | 18 ++++++++++-------- 1 file changed, 10 insertions(+), 8 deletions(-) diff --git a/src/borg/testsuite/helpers.py b/src/borg/testsuite/helpers.py index 8a4fb6cc2..fffe7652a 100644 --- a/src/borg/testsuite/helpers.py +++ b/src/borg/testsuite/helpers.py @@ -45,7 +45,7 @@ from ..helpers import eval_escapes from ..helpers import safe_unlink from ..helpers import text_to_json, binary_to_json from ..helpers.passphrase import Passphrase, PasswordRetriesExceeded -from ..platform import is_cygwin, is_win32 +from ..platform import is_cygwin, is_win32, is_darwin from . import BaseTestCase, FakeInputs, are_hardlinks_supported @@ -620,13 +620,15 @@ def test_get_config_dir(monkeypatch): def test_get_config_dir_compat(monkeypatch): """test that it works the same for legacy and for non-legacy implementation""" - monkeypatch.delenv("BORG_CONFIG_DIR", raising=False) - monkeypatch.delenv("XDG_CONFIG_HOME", raising=False) - assert get_config_dir(legacy=False) == get_config_dir(legacy=True) - # TODO fails on macOS: assert '/Users/tw/Library/Preferences/borg' == '/Users/tw/.config/borg' - monkeypatch.setenv("XDG_CONFIG_HOME", "/var/tmp/.config1") - assert get_config_dir(legacy=False) == get_config_dir(legacy=True) - # TODO fails on macOS: assert '/Users/tw/Library/Preferences/borg' == '/var/tmp/.config1/borg' + if not is_darwin: + monkeypatch.delenv("BORG_CONFIG_DIR", raising=False) + monkeypatch.delenv("XDG_CONFIG_HOME", raising=False) + # fails on macOS: assert '/Users/tw/Library/Preferences/borg' == '/Users/tw/.config/borg' + assert get_config_dir(legacy=False) == get_config_dir(legacy=True) + if not is_darwin: + monkeypatch.setenv("XDG_CONFIG_HOME", "/var/tmp/.config1") + # fails on macOS: assert '/Users/tw/Library/Preferences/borg' == '/var/tmp/.config1/borg' + assert get_config_dir(legacy=False) == get_config_dir(legacy=True) monkeypatch.setenv("BORG_CONFIG_DIR", "/var/tmp/.config2") assert get_config_dir(legacy=False) == get_config_dir(legacy=True) From fddf6afded98e96bf34fa334b2dd750f1c876401 Mon Sep 17 00:00:00 2001 From: Thomas Waldmann Date: Fri, 3 Feb 2023 22:35:42 +0100 Subject: [PATCH 06/20] get_*_dir: make legacy=... kwarg only it's better readable. --- src/borg/helpers/fs.py | 14 +++++++------- 1 file changed, 7 insertions(+), 7 deletions(-) diff --git a/src/borg/helpers/fs.py b/src/borg/helpers/fs.py index ee2ae1aaa..220283d9b 100644 --- a/src/borg/helpers/fs.py +++ b/src/borg/helpers/fs.py @@ -42,7 +42,7 @@ def ensure_dir(path, mode=stat.S_IRWXU | stat.S_IRWXG | stat.S_IRWXO, pretty_dea raise -def get_base_dir(legacy=True): +def get_base_dir(*, legacy=True): """Get home directory / base directory for borg: - BORG_BASE_DIR, if set @@ -71,29 +71,29 @@ def join_base_dir(*paths, **kw): return None if base_dir is None else os.path.join(base_dir, *paths) -def get_keys_dir(legacy=True): +def get_keys_dir(*, legacy=True): """Determine where to repository keys and cache""" keys_dir = os.environ.get("BORG_KEYS_DIR") if keys_dir is None: # note: do not just give this as default to the environment.get(), see issue #5979. - keys_dir = os.path.join(get_config_dir(legacy), "keys") + keys_dir = os.path.join(get_config_dir(legacy=legacy), "keys") ensure_dir(keys_dir) return keys_dir -def get_security_dir(repository_id=None, legacy=True): +def get_security_dir(repository_id=None, *, legacy=True): """Determine where to store local security information.""" security_dir = os.environ.get("BORG_SECURITY_DIR") if security_dir is None: # note: do not just give this as default to the environment.get(), see issue #5979. - security_dir = os.path.join(get_config_dir(legacy), "security") + security_dir = os.path.join(get_config_dir(legacy=legacy), "security") if repository_id: security_dir = os.path.join(security_dir, repository_id) ensure_dir(security_dir) return security_dir -def get_cache_dir(legacy=True): +def get_cache_dir(*, legacy=True): """Determine where to repository keys and cache""" if legacy: @@ -130,7 +130,7 @@ def get_cache_dir(legacy=True): return cache_dir -def get_config_dir(legacy=True): +def get_config_dir(*, legacy=True): """Determine where to store whole config""" # Get config home path From 81595a9ca0df9f1d43b9d8bf761baf9f04533071 Mon Sep 17 00:00:00 2001 From: Thomas Waldmann Date: Fri, 3 Feb 2023 23:47:28 +0100 Subject: [PATCH 07/20] conftest.py: use BORG_BASE_DIR to redirect borg testing .config/.cache into a temp dir XDG_*_HOME is not honoured on macOS and on Windows if we use platformdirs. --- conftest.py | 5 ++--- src/borg/testsuite/helpers.py | 25 +++++++++++++++++-------- 2 files changed, 19 insertions(+), 11 deletions(-) diff --git a/conftest.py b/conftest.py index 1c08cbbda..362ee0de5 100644 --- a/conftest.py +++ b/conftest.py @@ -20,13 +20,12 @@ from borg.testsuite.platform import fakeroot_detected # noqa: E402 @pytest.fixture(autouse=True) def clean_env(tmpdir_factory, monkeypatch): - # avoid that we access / modify the user's normal .config / .cache directory: - monkeypatch.setenv("XDG_CONFIG_HOME", str(tmpdir_factory.mktemp("xdg-config-home"))) - monkeypatch.setenv("XDG_CACHE_HOME", str(tmpdir_factory.mktemp("xdg-cache-home"))) # also avoid to use anything from the outside environment: keys = [key for key in os.environ if key.startswith("BORG_") and key not in ("BORG_FUSE_IMPL",)] for key in keys: monkeypatch.delenv(key, raising=False) + # avoid that we access / modify the user's normal .config / .cache directory: + monkeypatch.setenv("BORG_BASE_DIR", str(tmpdir_factory.mktemp("borg-base-dir"))) # Speed up tests monkeypatch.setenv("BORG_TESTONLY_WEAKEN_KDF", "1") diff --git a/src/borg/testsuite/helpers.py b/src/borg/testsuite/helpers.py index fffe7652a..01e9f4167 100644 --- a/src/borg/testsuite/helpers.py +++ b/src/borg/testsuite/helpers.py @@ -606,11 +606,13 @@ def test_get_base_dir_compat(monkeypatch): def test_get_config_dir(monkeypatch): """test that get_config_dir respects environment""" - monkeypatch.delenv("BORG_CONFIG_DIR", raising=False) - monkeypatch.delenv("XDG_CONFIG_HOME", raising=False) + monkeypatch.delenv("BORG_BASE_DIR", raising=False) if is_win32: + monkeypatch.delenv("BORG_CONFIG_DIR", raising=False) assert get_config_dir(legacy=False) == os.path.join(os.path.expanduser("~"), "AppData", "Local", "borg", "borg") else: + monkeypatch.delenv("XDG_CONFIG_HOME", raising=False) + monkeypatch.delenv("BORG_CONFIG_DIR", raising=False) assert get_config_dir() == os.path.join(os.path.expanduser("~"), ".config", "borg") monkeypatch.setenv("XDG_CONFIG_HOME", "/var/tmp/.config") assert get_config_dir() == os.path.join("/var/tmp/.config", "borg") @@ -620,6 +622,7 @@ def test_get_config_dir(monkeypatch): def test_get_config_dir_compat(monkeypatch): """test that it works the same for legacy and for non-legacy implementation""" + monkeypatch.delenv("BORG_BASE_DIR", raising=False) if not is_darwin: monkeypatch.delenv("BORG_CONFIG_DIR", raising=False) monkeypatch.delenv("XDG_CONFIG_HOME", raising=False) @@ -635,13 +638,15 @@ def test_get_config_dir_compat(monkeypatch): def test_get_cache_dir(monkeypatch): """test that get_cache_dir respects environment""" - monkeypatch.delenv("BORG_CACHE_DIR", raising=False) - monkeypatch.delenv("XDG_CACHE_HOME", raising=False) + monkeypatch.delenv("BORG_BASE_DIR", raising=False) if is_win32: + monkeypatch.delenv("BORG_CACHE_DIR", raising=False) assert get_cache_dir(legacy=False) == os.path.join( os.path.expanduser("~"), "AppData", "Local", "borg", "borg", "Cache" ) else: + monkeypatch.delenv("XDG_CACHE_HOME", raising=False) + monkeypatch.delenv("BORG_CACHE_DIR", raising=False) assert get_cache_dir() == os.path.join(os.path.expanduser("~"), ".cache", "borg") monkeypatch.setenv("XDG_CACHE_HOME", "/var/tmp/.cache") assert get_cache_dir() == os.path.join("/var/tmp/.cache", "borg") @@ -651,13 +656,15 @@ def test_get_cache_dir(monkeypatch): def test_get_keys_dir(monkeypatch): """test that get_keys_dir respects environment""" - monkeypatch.delenv("BORG_KEYS_DIR", raising=False) - monkeypatch.delenv("XDG_CONFIG_HOME", raising=False) + monkeypatch.delenv("BORG_BASE_DIR", raising=False) if is_win32: + monkeypatch.delenv("BORG_KEYS_DIR", raising=False) assert get_keys_dir(legacy=False) == os.path.join( os.path.expanduser("~"), "AppData", "Local", "borg", "borg", "keys" ) else: + monkeypatch.delenv("XDG_CONFIG_HOME", raising=False) + monkeypatch.delenv("BORG_KEYS_DIR", raising=False) assert get_keys_dir() == os.path.join(os.path.expanduser("~"), ".config", "borg", "keys") monkeypatch.setenv("XDG_CONFIG_HOME", "/var/tmp/.config") assert get_keys_dir() == os.path.join("/var/tmp/.config", "borg", "keys") @@ -667,9 +674,9 @@ def test_get_keys_dir(monkeypatch): def test_get_security_dir(monkeypatch): """test that get_security_dir respects environment""" - monkeypatch.delenv("BORG_SECURITY_DIR", raising=False) - monkeypatch.delenv("XDG_CONFIG_HOME", raising=False) + monkeypatch.delenv("BORG_BASE_DIR", raising=False) if is_win32: + monkeypatch.delenv("BORG_SECURITY_DIR", raising=False) assert get_security_dir(legacy=False) == os.path.join( os.path.expanduser("~"), "AppData", "Local", "borg", "borg", "security" ) @@ -677,6 +684,8 @@ def test_get_security_dir(monkeypatch): os.path.expanduser("~"), "AppData", "Local", "borg", "borg", "security", "1234" ) else: + monkeypatch.delenv("XDG_CONFIG_HOME", raising=False) + monkeypatch.delenv("BORG_SECURITY_DIR", raising=False) assert get_security_dir() == os.path.join(os.path.expanduser("~"), ".config", "borg", "security") assert get_security_dir(repository_id="1234") == os.path.join( os.path.expanduser("~"), ".config", "borg", "security", "1234" From 424be763b22d94fc044e4b4412cd95565b68fef5 Mon Sep 17 00:00:00 2001 From: Thomas Waldmann Date: Fri, 3 Feb 2023 23:54:26 +0100 Subject: [PATCH 08/20] get_base_dir: legacy=False default --- src/borg/helpers/fs.py | 2 +- src/borg/testsuite/helpers.py | 13 +++++++++---- 2 files changed, 10 insertions(+), 5 deletions(-) diff --git a/src/borg/helpers/fs.py b/src/borg/helpers/fs.py index 220283d9b..29e4bb8e0 100644 --- a/src/borg/helpers/fs.py +++ b/src/borg/helpers/fs.py @@ -42,7 +42,7 @@ def ensure_dir(path, mode=stat.S_IRWXU | stat.S_IRWXG | stat.S_IRWXO, pretty_dea raise -def get_base_dir(*, legacy=True): +def get_base_dir(*, legacy=False): """Get home directory / base directory for borg: - BORG_BASE_DIR, if set diff --git a/src/borg/testsuite/helpers.py b/src/borg/testsuite/helpers.py index 01e9f4167..6b1447da7 100644 --- a/src/borg/testsuite/helpers.py +++ b/src/borg/testsuite/helpers.py @@ -584,13 +584,18 @@ def test_get_base_dir(monkeypatch): monkeypatch.delenv("BORG_BASE_DIR", raising=False) monkeypatch.delenv("HOME", raising=False) monkeypatch.delenv("USER", raising=False) - assert get_base_dir() == os.path.expanduser("~") + assert get_base_dir(legacy=True) == os.path.expanduser("~") monkeypatch.setenv("USER", "root") - assert get_base_dir() == os.path.expanduser("~root") + assert get_base_dir(legacy=True) == os.path.expanduser("~root") monkeypatch.setenv("HOME", "/var/tmp/home") - assert get_base_dir() == "/var/tmp/home" + assert get_base_dir(legacy=True) == "/var/tmp/home" monkeypatch.setenv("BORG_BASE_DIR", "/var/tmp/base") - assert get_base_dir() == "/var/tmp/base" + assert get_base_dir(legacy=True) == "/var/tmp/base" + # non-legacy is much easier: + monkeypatch.delenv("BORG_BASE_DIR", raising=False) + assert get_base_dir(legacy=False) is None + monkeypatch.setenv("BORG_BASE_DIR", "/var/tmp/base") + assert get_base_dir(legacy=False) == "/var/tmp/base" def test_get_base_dir_compat(monkeypatch): From ebcda6f0a5054311055af95f9f7fd900ef0d2f84 Mon Sep 17 00:00:00 2001 From: Thomas Waldmann Date: Sat, 4 Feb 2023 00:03:23 +0100 Subject: [PATCH 09/20] get_cache_dir: legacy=False default --- src/borg/helpers/fs.py | 2 +- src/borg/testsuite/helpers.py | 9 ++++++--- 2 files changed, 7 insertions(+), 4 deletions(-) diff --git a/src/borg/helpers/fs.py b/src/borg/helpers/fs.py index 29e4bb8e0..7e2e5efe2 100644 --- a/src/borg/helpers/fs.py +++ b/src/borg/helpers/fs.py @@ -93,7 +93,7 @@ def get_security_dir(repository_id=None, *, legacy=True): return security_dir -def get_cache_dir(*, legacy=True): +def get_cache_dir(*, legacy=False): """Determine where to repository keys and cache""" if legacy: diff --git a/src/borg/testsuite/helpers.py b/src/borg/testsuite/helpers.py index 6b1447da7..a1ae9c51c 100644 --- a/src/borg/testsuite/helpers.py +++ b/src/borg/testsuite/helpers.py @@ -646,9 +646,12 @@ def test_get_cache_dir(monkeypatch): monkeypatch.delenv("BORG_BASE_DIR", raising=False) if is_win32: monkeypatch.delenv("BORG_CACHE_DIR", raising=False) - assert get_cache_dir(legacy=False) == os.path.join( - os.path.expanduser("~"), "AppData", "Local", "borg", "borg", "Cache" - ) + assert get_cache_dir() == os.path.join(os.path.expanduser("~"), "AppData", "Local", "borg", "borg", "Cache") + elif is_darwin: + monkeypatch.delenv("BORG_CACHE_DIR", raising=False) + assert get_cache_dir() == os.path.join(os.path.expanduser("~"), "Library", "Caches", "borg") + monkeypatch.setenv("BORG_CACHE_DIR", "/var/tmp") + assert get_cache_dir() == "/var/tmp" else: monkeypatch.delenv("XDG_CACHE_HOME", raising=False) monkeypatch.delenv("BORG_CACHE_DIR", raising=False) From b06dd1c66cb1e2ada3a7b9b0f814f5f176afb8ed Mon Sep 17 00:00:00 2001 From: Thomas Waldmann Date: Sat, 4 Feb 2023 00:06:31 +0100 Subject: [PATCH 10/20] get_config_dir: legacy=False default --- src/borg/helpers/fs.py | 2 +- src/borg/testsuite/helpers.py | 7 ++++++- 2 files changed, 7 insertions(+), 2 deletions(-) diff --git a/src/borg/helpers/fs.py b/src/borg/helpers/fs.py index 7e2e5efe2..7b402802c 100644 --- a/src/borg/helpers/fs.py +++ b/src/borg/helpers/fs.py @@ -130,7 +130,7 @@ def get_cache_dir(*, legacy=False): return cache_dir -def get_config_dir(*, legacy=True): +def get_config_dir(*, legacy=False): """Determine where to store whole config""" # Get config home path diff --git a/src/borg/testsuite/helpers.py b/src/borg/testsuite/helpers.py index a1ae9c51c..b6ddd58ee 100644 --- a/src/borg/testsuite/helpers.py +++ b/src/borg/testsuite/helpers.py @@ -614,7 +614,12 @@ def test_get_config_dir(monkeypatch): monkeypatch.delenv("BORG_BASE_DIR", raising=False) if is_win32: monkeypatch.delenv("BORG_CONFIG_DIR", raising=False) - assert get_config_dir(legacy=False) == os.path.join(os.path.expanduser("~"), "AppData", "Local", "borg", "borg") + assert get_config_dir() == os.path.join(os.path.expanduser("~"), "AppData", "Local", "borg", "borg") + elif is_darwin: + monkeypatch.delenv("BORG_CONFIG_DIR", raising=False) + assert get_config_dir() == os.path.join(os.path.expanduser("~"), "Library", "Preferences", "borg") + monkeypatch.setenv("BORG_CONFIG_DIR", "/var/tmp") + assert get_config_dir() == "/var/tmp" else: monkeypatch.delenv("XDG_CONFIG_HOME", raising=False) monkeypatch.delenv("BORG_CONFIG_DIR", raising=False) From 1d4810af23a28c5534564ef9fde0cdd38d05c275 Mon Sep 17 00:00:00 2001 From: Thomas Waldmann Date: Sat, 4 Feb 2023 00:11:10 +0100 Subject: [PATCH 11/20] get_keys_dir: legacy=False default --- src/borg/helpers/fs.py | 2 +- src/borg/testsuite/helpers.py | 9 ++++++--- 2 files changed, 7 insertions(+), 4 deletions(-) diff --git a/src/borg/helpers/fs.py b/src/borg/helpers/fs.py index 7b402802c..495210d0c 100644 --- a/src/borg/helpers/fs.py +++ b/src/borg/helpers/fs.py @@ -71,7 +71,7 @@ def join_base_dir(*paths, **kw): return None if base_dir is None else os.path.join(base_dir, *paths) -def get_keys_dir(*, legacy=True): +def get_keys_dir(*, legacy=False): """Determine where to repository keys and cache""" keys_dir = os.environ.get("BORG_KEYS_DIR") if keys_dir is None: diff --git a/src/borg/testsuite/helpers.py b/src/borg/testsuite/helpers.py index b6ddd58ee..4dce31c99 100644 --- a/src/borg/testsuite/helpers.py +++ b/src/borg/testsuite/helpers.py @@ -672,9 +672,12 @@ def test_get_keys_dir(monkeypatch): monkeypatch.delenv("BORG_BASE_DIR", raising=False) if is_win32: monkeypatch.delenv("BORG_KEYS_DIR", raising=False) - assert get_keys_dir(legacy=False) == os.path.join( - os.path.expanduser("~"), "AppData", "Local", "borg", "borg", "keys" - ) + assert get_keys_dir() == os.path.join(os.path.expanduser("~"), "AppData", "Local", "borg", "borg", "keys") + elif is_darwin: + monkeypatch.delenv("BORG_KEYS_DIR", raising=False) + assert get_keys_dir() == os.path.join(os.path.expanduser("~"), "Library", "Preferences", "borg", "keys") + monkeypatch.setenv("BORG_KEYS_DIR", "/var/tmp") + assert get_keys_dir() == "/var/tmp" else: monkeypatch.delenv("XDG_CONFIG_HOME", raising=False) monkeypatch.delenv("BORG_KEYS_DIR", raising=False) From bde0f11e1ec82f3343e8eba90e5afc02432c000c Mon Sep 17 00:00:00 2001 From: Thomas Waldmann Date: Sat, 4 Feb 2023 00:14:40 +0100 Subject: [PATCH 12/20] get_security_dir: legacy=False default --- src/borg/helpers/fs.py | 2 +- src/borg/testsuite/helpers.py | 12 ++++++++++-- 2 files changed, 11 insertions(+), 3 deletions(-) diff --git a/src/borg/helpers/fs.py b/src/borg/helpers/fs.py index 495210d0c..805746557 100644 --- a/src/borg/helpers/fs.py +++ b/src/borg/helpers/fs.py @@ -81,7 +81,7 @@ def get_keys_dir(*, legacy=False): return keys_dir -def get_security_dir(repository_id=None, *, legacy=True): +def get_security_dir(repository_id=None, *, legacy=False): """Determine where to store local security information.""" security_dir = os.environ.get("BORG_SECURITY_DIR") if security_dir is None: diff --git a/src/borg/testsuite/helpers.py b/src/borg/testsuite/helpers.py index 4dce31c99..7a237484e 100644 --- a/src/borg/testsuite/helpers.py +++ b/src/borg/testsuite/helpers.py @@ -693,12 +693,20 @@ def test_get_security_dir(monkeypatch): monkeypatch.delenv("BORG_BASE_DIR", raising=False) if is_win32: monkeypatch.delenv("BORG_SECURITY_DIR", raising=False) - assert get_security_dir(legacy=False) == os.path.join( + assert get_security_dir() == os.path.join( os.path.expanduser("~"), "AppData", "Local", "borg", "borg", "security" ) - assert get_security_dir(repository_id="1234", legacy=False) == os.path.join( + assert get_security_dir(repository_id="1234") == os.path.join( os.path.expanduser("~"), "AppData", "Local", "borg", "borg", "security", "1234" ) + elif is_darwin: + monkeypatch.delenv("BORG_SECURITY_DIR", raising=False) + assert get_security_dir() == os.path.join(os.path.expanduser("~"), "Library", "Preferences", "borg", "security") + assert get_security_dir(repository_id="1234") == os.path.join( + os.path.expanduser("~"), "Library", "Preferences", "borg", "security", "1234" + ) + monkeypatch.setenv("BORG_SECURITY_DIR", "/var/tmp") + assert get_security_dir() == "/var/tmp" else: monkeypatch.delenv("XDG_CONFIG_HOME", raising=False) monkeypatch.delenv("BORG_SECURITY_DIR", raising=False) From 499e5133b5d5c9bf1fb1c9556663e359e446a096 Mon Sep 17 00:00:00 2001 From: Thomas Waldmann Date: Sat, 4 Feb 2023 01:18:16 +0100 Subject: [PATCH 13/20] test_config_dir_compat: don't test on win32 there is no old borg < 2.0 there anyway. --- src/borg/testsuite/helpers.py | 6 ++++-- 1 file changed, 4 insertions(+), 2 deletions(-) diff --git a/src/borg/testsuite/helpers.py b/src/borg/testsuite/helpers.py index 7a237484e..a6771e377 100644 --- a/src/borg/testsuite/helpers.py +++ b/src/borg/testsuite/helpers.py @@ -633,14 +633,16 @@ def test_get_config_dir(monkeypatch): def test_get_config_dir_compat(monkeypatch): """test that it works the same for legacy and for non-legacy implementation""" monkeypatch.delenv("BORG_BASE_DIR", raising=False) - if not is_darwin: + if not is_darwin and not is_win32: monkeypatch.delenv("BORG_CONFIG_DIR", raising=False) monkeypatch.delenv("XDG_CONFIG_HOME", raising=False) # fails on macOS: assert '/Users/tw/Library/Preferences/borg' == '/Users/tw/.config/borg' + # fails on win2 MSYS2 (but we do not need legacy compat there). assert get_config_dir(legacy=False) == get_config_dir(legacy=True) - if not is_darwin: + if not is_darwin and not is_win32: monkeypatch.setenv("XDG_CONFIG_HOME", "/var/tmp/.config1") # fails on macOS: assert '/Users/tw/Library/Preferences/borg' == '/var/tmp/.config1/borg' + # fails on win2 MSYS2 (but we do not need legacy compat there). assert get_config_dir(legacy=False) == get_config_dir(legacy=True) monkeypatch.setenv("BORG_CONFIG_DIR", "/var/tmp/.config2") assert get_config_dir(legacy=False) == get_config_dir(legacy=True) From bb73e8682fedd5af2c813831035ce87e563f33b0 Mon Sep 17 00:00:00 2001 From: Thomas Waldmann Date: Sat, 4 Feb 2023 18:11:28 +0100 Subject: [PATCH 14/20] fix win2 typo --- src/borg/testsuite/helpers.py | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/src/borg/testsuite/helpers.py b/src/borg/testsuite/helpers.py index a6771e377..0980b950b 100644 --- a/src/borg/testsuite/helpers.py +++ b/src/borg/testsuite/helpers.py @@ -637,12 +637,12 @@ def test_get_config_dir_compat(monkeypatch): monkeypatch.delenv("BORG_CONFIG_DIR", raising=False) monkeypatch.delenv("XDG_CONFIG_HOME", raising=False) # fails on macOS: assert '/Users/tw/Library/Preferences/borg' == '/Users/tw/.config/borg' - # fails on win2 MSYS2 (but we do not need legacy compat there). + # fails on win32 MSYS2 (but we do not need legacy compat there). assert get_config_dir(legacy=False) == get_config_dir(legacy=True) if not is_darwin and not is_win32: monkeypatch.setenv("XDG_CONFIG_HOME", "/var/tmp/.config1") # fails on macOS: assert '/Users/tw/Library/Preferences/borg' == '/var/tmp/.config1/borg' - # fails on win2 MSYS2 (but we do not need legacy compat there). + # fails on win32 MSYS2 (but we do not need legacy compat there). assert get_config_dir(legacy=False) == get_config_dir(legacy=True) monkeypatch.setenv("BORG_CONFIG_DIR", "/var/tmp/.config2") assert get_config_dir(legacy=False) == get_config_dir(legacy=True) From 4bacd0b722fdc34f85d24688d4132c7bdd5b93d4 Mon Sep 17 00:00:00 2001 From: Thomas Waldmann Date: Sat, 4 Feb 2023 18:15:57 +0100 Subject: [PATCH 15/20] improve test_get_config_dir --- src/borg/testsuite/helpers.py | 9 ++++++--- 1 file changed, 6 insertions(+), 3 deletions(-) diff --git a/src/borg/testsuite/helpers.py b/src/borg/testsuite/helpers.py index 0980b950b..47e38679a 100644 --- a/src/borg/testsuite/helpers.py +++ b/src/borg/testsuite/helpers.py @@ -612,18 +612,21 @@ def test_get_base_dir_compat(monkeypatch): def test_get_config_dir(monkeypatch): """test that get_config_dir respects environment""" monkeypatch.delenv("BORG_BASE_DIR", raising=False) + home_dir = os.path.expanduser("~") if is_win32: monkeypatch.delenv("BORG_CONFIG_DIR", raising=False) - assert get_config_dir() == os.path.join(os.path.expanduser("~"), "AppData", "Local", "borg", "borg") + assert get_config_dir() == os.path.join(home_dir, "AppData", "Local", "borg", "borg") + monkeypatch.setenv("BORG_CONFIG_DIR", home_dir) + assert get_config_dir() == home_dir elif is_darwin: monkeypatch.delenv("BORG_CONFIG_DIR", raising=False) - assert get_config_dir() == os.path.join(os.path.expanduser("~"), "Library", "Preferences", "borg") + assert get_config_dir() == os.path.join(home_dir, "Library", "Preferences", "borg") monkeypatch.setenv("BORG_CONFIG_DIR", "/var/tmp") assert get_config_dir() == "/var/tmp" else: monkeypatch.delenv("XDG_CONFIG_HOME", raising=False) monkeypatch.delenv("BORG_CONFIG_DIR", raising=False) - assert get_config_dir() == os.path.join(os.path.expanduser("~"), ".config", "borg") + assert get_config_dir() == os.path.join(home_dir, ".config", "borg") monkeypatch.setenv("XDG_CONFIG_HOME", "/var/tmp/.config") assert get_config_dir() == os.path.join("/var/tmp/.config", "borg") monkeypatch.setenv("BORG_CONFIG_DIR", "/var/tmp") From 6ab424598ddd11caae16ae33a7589a75d49908b5 Mon Sep 17 00:00:00 2001 From: Thomas Waldmann Date: Sat, 4 Feb 2023 18:17:43 +0100 Subject: [PATCH 16/20] improve test_get_cache_dir --- src/borg/testsuite/helpers.py | 9 ++++++--- 1 file changed, 6 insertions(+), 3 deletions(-) diff --git a/src/borg/testsuite/helpers.py b/src/borg/testsuite/helpers.py index 47e38679a..f0a696a1d 100644 --- a/src/borg/testsuite/helpers.py +++ b/src/borg/testsuite/helpers.py @@ -654,18 +654,21 @@ def test_get_config_dir_compat(monkeypatch): def test_get_cache_dir(monkeypatch): """test that get_cache_dir respects environment""" monkeypatch.delenv("BORG_BASE_DIR", raising=False) + home_dir = os.path.expanduser("~") if is_win32: monkeypatch.delenv("BORG_CACHE_DIR", raising=False) - assert get_cache_dir() == os.path.join(os.path.expanduser("~"), "AppData", "Local", "borg", "borg", "Cache") + assert get_cache_dir() == os.path.join(home_dir, "AppData", "Local", "borg", "borg", "Cache") + monkeypatch.setenv("BORG_CACHE_DIR", home_dir) + assert get_cache_dir() == home_dir elif is_darwin: monkeypatch.delenv("BORG_CACHE_DIR", raising=False) - assert get_cache_dir() == os.path.join(os.path.expanduser("~"), "Library", "Caches", "borg") + assert get_cache_dir() == os.path.join(home_dir, "Library", "Caches", "borg") monkeypatch.setenv("BORG_CACHE_DIR", "/var/tmp") assert get_cache_dir() == "/var/tmp" else: monkeypatch.delenv("XDG_CACHE_HOME", raising=False) monkeypatch.delenv("BORG_CACHE_DIR", raising=False) - assert get_cache_dir() == os.path.join(os.path.expanduser("~"), ".cache", "borg") + assert get_cache_dir() == os.path.join(home_dir, ".cache", "borg") monkeypatch.setenv("XDG_CACHE_HOME", "/var/tmp/.cache") assert get_cache_dir() == os.path.join("/var/tmp/.cache", "borg") monkeypatch.setenv("BORG_CACHE_DIR", "/var/tmp") From 078bb7ee02a931c8a06e10236b660edcef0442fb Mon Sep 17 00:00:00 2001 From: Thomas Waldmann Date: Sat, 4 Feb 2023 18:19:15 +0100 Subject: [PATCH 17/20] improve test_get_keys_dir --- src/borg/testsuite/helpers.py | 9 ++++++--- 1 file changed, 6 insertions(+), 3 deletions(-) diff --git a/src/borg/testsuite/helpers.py b/src/borg/testsuite/helpers.py index f0a696a1d..a61302375 100644 --- a/src/borg/testsuite/helpers.py +++ b/src/borg/testsuite/helpers.py @@ -678,18 +678,21 @@ def test_get_cache_dir(monkeypatch): def test_get_keys_dir(monkeypatch): """test that get_keys_dir respects environment""" monkeypatch.delenv("BORG_BASE_DIR", raising=False) + home_dir = os.path.expanduser("~") if is_win32: monkeypatch.delenv("BORG_KEYS_DIR", raising=False) - assert get_keys_dir() == os.path.join(os.path.expanduser("~"), "AppData", "Local", "borg", "borg", "keys") + assert get_keys_dir() == os.path.join(home_dir, "AppData", "Local", "borg", "borg", "keys") + monkeypatch.setenv("BORG_KEYS_DIR", home_dir) + assert get_keys_dir() == home_dir elif is_darwin: monkeypatch.delenv("BORG_KEYS_DIR", raising=False) - assert get_keys_dir() == os.path.join(os.path.expanduser("~"), "Library", "Preferences", "borg", "keys") + assert get_keys_dir() == os.path.join(home_dir, "Library", "Preferences", "borg", "keys") monkeypatch.setenv("BORG_KEYS_DIR", "/var/tmp") assert get_keys_dir() == "/var/tmp" else: monkeypatch.delenv("XDG_CONFIG_HOME", raising=False) monkeypatch.delenv("BORG_KEYS_DIR", raising=False) - assert get_keys_dir() == os.path.join(os.path.expanduser("~"), ".config", "borg", "keys") + assert get_keys_dir() == os.path.join(home_dir, ".config", "borg", "keys") monkeypatch.setenv("XDG_CONFIG_HOME", "/var/tmp/.config") assert get_keys_dir() == os.path.join("/var/tmp/.config", "borg", "keys") monkeypatch.setenv("BORG_KEYS_DIR", "/var/tmp") From d9875a4d7e5370aed386fcffc8b575daf5557b77 Mon Sep 17 00:00:00 2001 From: Thomas Waldmann Date: Sat, 4 Feb 2023 18:22:01 +0100 Subject: [PATCH 18/20] improve test_get_security_dir --- src/borg/testsuite/helpers.py | 19 +++++++++---------- 1 file changed, 9 insertions(+), 10 deletions(-) diff --git a/src/borg/testsuite/helpers.py b/src/borg/testsuite/helpers.py index a61302375..4293d421d 100644 --- a/src/borg/testsuite/helpers.py +++ b/src/borg/testsuite/helpers.py @@ -702,29 +702,28 @@ def test_get_keys_dir(monkeypatch): def test_get_security_dir(monkeypatch): """test that get_security_dir respects environment""" monkeypatch.delenv("BORG_BASE_DIR", raising=False) + home_dir = os.path.expanduser("~") if is_win32: monkeypatch.delenv("BORG_SECURITY_DIR", raising=False) - assert get_security_dir() == os.path.join( - os.path.expanduser("~"), "AppData", "Local", "borg", "borg", "security" - ) + assert get_security_dir() == os.path.join(home_dir, "AppData", "Local", "borg", "borg", "security") assert get_security_dir(repository_id="1234") == os.path.join( - os.path.expanduser("~"), "AppData", "Local", "borg", "borg", "security", "1234" + home_dir, "AppData", "Local", "borg", "borg", "security", "1234" ) + monkeypatch.setenv("BORG_SECURITY_DIR", home_dir) + assert get_security_dir() == home_dir elif is_darwin: monkeypatch.delenv("BORG_SECURITY_DIR", raising=False) - assert get_security_dir() == os.path.join(os.path.expanduser("~"), "Library", "Preferences", "borg", "security") + assert get_security_dir() == os.path.join(home_dir, "Library", "Preferences", "borg", "security") assert get_security_dir(repository_id="1234") == os.path.join( - os.path.expanduser("~"), "Library", "Preferences", "borg", "security", "1234" + home_dir, "Library", "Preferences", "borg", "security", "1234" ) monkeypatch.setenv("BORG_SECURITY_DIR", "/var/tmp") assert get_security_dir() == "/var/tmp" else: monkeypatch.delenv("XDG_CONFIG_HOME", raising=False) monkeypatch.delenv("BORG_SECURITY_DIR", raising=False) - assert get_security_dir() == os.path.join(os.path.expanduser("~"), ".config", "borg", "security") - assert get_security_dir(repository_id="1234") == os.path.join( - os.path.expanduser("~"), ".config", "borg", "security", "1234" - ) + assert get_security_dir() == os.path.join(home_dir, ".config", "borg", "security") + assert get_security_dir(repository_id="1234") == os.path.join(home_dir, ".config", "borg", "security", "1234") monkeypatch.setenv("XDG_CONFIG_HOME", "/var/tmp/.config") assert get_security_dir() == os.path.join("/var/tmp/.config", "borg", "security") monkeypatch.setenv("BORG_SECURITY_DIR", "/var/tmp") From f29ff128fb2d7936574db29b2d3a2a38960d9eb6 Mon Sep 17 00:00:00 2001 From: Thomas Waldmann Date: Mon, 6 Feb 2023 23:24:56 +0100 Subject: [PATCH 19/20] require 3.x.x releases of platformdirs we don't want to suddenly/unexpectedly break stuff for borg users just because platformdirs does a breaking release. at platformdirs 2.0.0 macOS config dir changed. at platformdirs 3.0.0 macOS config dir changed again. at platformdirs 4.0.0 (future) - who knows? --- setup.cfg | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/setup.cfg b/setup.cfg index 6984f610d..47cf3119c 100644 --- a/setup.cfg +++ b/setup.cfg @@ -41,7 +41,7 @@ setup_requires = install_requires = msgpack >=1.0.3, <=1.0.4 packaging - platformdirs + platformdirs >=3.0.0, <4.0.0 argon2-cffi tests_require = pytest From 516c070c7ccac77dd73066a84f28201c8b394fe3 Mon Sep 17 00:00:00 2001 From: Thomas Waldmann Date: Tue, 7 Feb 2023 21:11:40 +0100 Subject: [PATCH 20/20] fix tests for platformdirs 3.x.x --- src/borg/testsuite/helpers.py | 12 ++++++------ 1 file changed, 6 insertions(+), 6 deletions(-) diff --git a/src/borg/testsuite/helpers.py b/src/borg/testsuite/helpers.py index 4293d421d..70cc6139e 100644 --- a/src/borg/testsuite/helpers.py +++ b/src/borg/testsuite/helpers.py @@ -620,7 +620,7 @@ def test_get_config_dir(monkeypatch): assert get_config_dir() == home_dir elif is_darwin: monkeypatch.delenv("BORG_CONFIG_DIR", raising=False) - assert get_config_dir() == os.path.join(home_dir, "Library", "Preferences", "borg") + assert get_config_dir() == os.path.join(home_dir, "Library", "Application Support", "borg") monkeypatch.setenv("BORG_CONFIG_DIR", "/var/tmp") assert get_config_dir() == "/var/tmp" else: @@ -639,12 +639,12 @@ def test_get_config_dir_compat(monkeypatch): if not is_darwin and not is_win32: monkeypatch.delenv("BORG_CONFIG_DIR", raising=False) monkeypatch.delenv("XDG_CONFIG_HOME", raising=False) - # fails on macOS: assert '/Users/tw/Library/Preferences/borg' == '/Users/tw/.config/borg' + # fails on macOS: assert '/Users/tw/Library/Application Support/borg' == '/Users/tw/.config/borg' # fails on win32 MSYS2 (but we do not need legacy compat there). assert get_config_dir(legacy=False) == get_config_dir(legacy=True) if not is_darwin and not is_win32: monkeypatch.setenv("XDG_CONFIG_HOME", "/var/tmp/.config1") - # fails on macOS: assert '/Users/tw/Library/Preferences/borg' == '/var/tmp/.config1/borg' + # fails on macOS: assert '/Users/tw/Library/Application Support/borg' == '/var/tmp/.config1/borg' # fails on win32 MSYS2 (but we do not need legacy compat there). assert get_config_dir(legacy=False) == get_config_dir(legacy=True) monkeypatch.setenv("BORG_CONFIG_DIR", "/var/tmp/.config2") @@ -686,7 +686,7 @@ def test_get_keys_dir(monkeypatch): assert get_keys_dir() == home_dir elif is_darwin: monkeypatch.delenv("BORG_KEYS_DIR", raising=False) - assert get_keys_dir() == os.path.join(home_dir, "Library", "Preferences", "borg", "keys") + assert get_keys_dir() == os.path.join(home_dir, "Library", "Application Support", "borg", "keys") monkeypatch.setenv("BORG_KEYS_DIR", "/var/tmp") assert get_keys_dir() == "/var/tmp" else: @@ -713,9 +713,9 @@ def test_get_security_dir(monkeypatch): assert get_security_dir() == home_dir elif is_darwin: monkeypatch.delenv("BORG_SECURITY_DIR", raising=False) - assert get_security_dir() == os.path.join(home_dir, "Library", "Preferences", "borg", "security") + assert get_security_dir() == os.path.join(home_dir, "Library", "Application Support", "borg", "security") assert get_security_dir(repository_id="1234") == os.path.join( - home_dir, "Library", "Preferences", "borg", "security", "1234" + home_dir, "Library", "Application Support", "borg", "security", "1234" ) monkeypatch.setenv("BORG_SECURITY_DIR", "/var/tmp") assert get_security_dir() == "/var/tmp"