From 4aa63a78660fbc8e8efbc553e5f14a269316f358 Mon Sep 17 00:00:00 2001 From: Thomas Waldmann Date: Wed, 19 Nov 2025 20:16:41 +0100 Subject: [PATCH 1/2] tests: add some ctx managers for better cleanup --- src/borg/testsuite/archiver/__init__.py | 12 +++++++----- src/borg/testsuite/platform/freebsd_test.py | 10 +++++----- src/borg/testsuite/platform/linux_test.py | 10 +++++----- 3 files changed, 17 insertions(+), 15 deletions(-) diff --git a/src/borg/testsuite/archiver/__init__.py b/src/borg/testsuite/archiver/__init__.py index e140d829d..df50b20d1 100644 --- a/src/borg/testsuite/archiver/__init__.py +++ b/src/borg/testsuite/archiver/__init__.py @@ -514,10 +514,12 @@ def fuse_mount(archiver, mountpoint=None, *options, fork=True, os_fork=False, ** # with the call to `cmd`, above. yield return - wait_for_mountstate(mountpoint, mounted=True) - yield - umount(mountpoint) - wait_for_mountstate(mountpoint, mounted=False) - os.rmdir(mountpoint) + try: + wait_for_mountstate(mountpoint, mounted=True) + yield + finally: + umount(mountpoint) + wait_for_mountstate(mountpoint, mounted=False) + os.rmdir(mountpoint) # Give the daemon some time to exit time.sleep(0.2) diff --git a/src/borg/testsuite/platform/freebsd_test.py b/src/borg/testsuite/platform/freebsd_test.py index c64c544ee..8d313fd6e 100644 --- a/src/borg/testsuite/platform/freebsd_test.py +++ b/src/borg/testsuite/platform/freebsd_test.py @@ -86,11 +86,11 @@ def test_access_acl(): @skipif_acls_not_working def test_default_acl(): - tmpdir = tempfile.mkdtemp() - assert get_acl(tmpdir) == {} - set_acl(tmpdir, access=ACCESS_ACL, default=DEFAULT_ACL) - assert get_acl(tmpdir)["acl_access"] == ACCESS_ACL - assert get_acl(tmpdir)["acl_default"] == DEFAULT_ACL + with tempfile.TemporaryDirectory() as tmpdir: + assert get_acl(tmpdir) == {} + set_acl(tmpdir, access=ACCESS_ACL, default=DEFAULT_ACL) + assert get_acl(tmpdir)["acl_access"] == ACCESS_ACL + assert get_acl(tmpdir)["acl_default"] == DEFAULT_ACL # nfs4 acls testing not implemented. diff --git a/src/borg/testsuite/platform/linux_test.py b/src/borg/testsuite/platform/linux_test.py index 605dc35b7..04d5be525 100644 --- a/src/borg/testsuite/platform/linux_test.py +++ b/src/borg/testsuite/platform/linux_test.py @@ -72,11 +72,11 @@ def test_access_acl(): @skipif_acls_not_working def test_default_acl(): - tmpdir = tempfile.mkdtemp() - assert get_acl(tmpdir) == {} - set_acl(tmpdir, access=ACCESS_ACL, default=DEFAULT_ACL) - assert get_acl(tmpdir)["acl_access"] == ACCESS_ACL - assert get_acl(tmpdir)["acl_default"] == DEFAULT_ACL + with tempfile.TemporaryDirectory() as tmpdir: + assert get_acl(tmpdir) == {} + set_acl(tmpdir, access=ACCESS_ACL, default=DEFAULT_ACL) + assert get_acl(tmpdir)["acl_access"] == ACCESS_ACL + assert get_acl(tmpdir)["acl_default"] == DEFAULT_ACL @skipif_acls_not_working From 1244e0d23a38269f86a3770283307cfbaf31d3b5 Mon Sep 17 00:00:00 2001 From: Thomas Waldmann Date: Wed, 19 Nov 2025 21:58:56 +0100 Subject: [PATCH 2/2] conftest.py: clean up directly after tests --- src/borg/conftest.py | 7 ++++++- 1 file changed, 6 insertions(+), 1 deletion(-) diff --git a/src/borg/conftest.py b/src/borg/conftest.py index e9a749c1f..540a6b3f9 100644 --- a/src/borg/conftest.py +++ b/src/borg/conftest.py @@ -1,4 +1,5 @@ import os +import shutil import pytest @@ -24,10 +25,13 @@ def clean_env(tmpdir_factory, monkeypatch): 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"))) + base_dir = tmpdir_factory.mktemp("borg-base-dir") + monkeypatch.setenv("BORG_BASE_DIR", str(base_dir)) # Speed up tests monkeypatch.setenv("BORG_TESTONLY_WEAKEN_KDF", "1") monkeypatch.setenv("BORG_STORE_DATA_LEVELS", "0") # flat storage for few objects + yield + shutil.rmtree(str(base_dir), ignore_errors=True) # clean up def pytest_report_header(config, start_path): @@ -116,6 +120,7 @@ def archiver(tmp_path, set_env_variables): os.chdir(archiver.tmpdir) yield archiver os.chdir(old_wd) + shutil.rmtree(archiver.tmpdir, ignore_errors=True) # clean up @pytest.fixture()