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.
This commit is contained in:
Thomas Waldmann 2023-02-03 23:47:28 +01:00
parent fddf6afded
commit 81595a9ca0
No known key found for this signature in database
GPG Key ID: 243ACFA951F78E01
2 changed files with 19 additions and 11 deletions

View File

@ -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")

View File

@ -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"