From 0132eee1d48b9a11700ab856e53f2e1ea8191b4f Mon Sep 17 00:00:00 2001 From: Andrey Bienkowski Date: Sun, 20 Feb 2022 13:27:44 +0000 Subject: [PATCH] Write a test for safe_unlink (#6311) write a test for safe_unlink --- src/borg/testsuite/helpers.py | 31 +++++++++++++++++++++++++++++++ 1 file changed, 31 insertions(+) diff --git a/src/borg/testsuite/helpers.py b/src/borg/testsuite/helpers.py index 63dae7804..2e30e36be 100644 --- a/src/borg/testsuite/helpers.py +++ b/src/borg/testsuite/helpers.py @@ -1,3 +1,4 @@ +import errno import hashlib import os import shutil @@ -30,6 +31,7 @@ from ..helpers import dash_open from ..helpers import iter_separated from ..helpers import eval_escapes +from ..helpers import safe_unlink from . import BaseTestCase, FakeInputs @@ -1133,3 +1135,32 @@ def test_iter_separated(): def test_eval_escapes(): assert eval_escapes('\\n\\0\\x23') == '\n\0#' assert eval_escapes('äç\\n') == 'äç\n' + + +def test_safe_unlink_is_safe(tmpdir): + contents = b"Hello, world\n" + victim = tmpdir / 'victim' + victim.write_binary(contents) + hard_link = tmpdir / 'hardlink' + hard_link.mklinkto(victim) + + safe_unlink(hard_link) + + assert victim.read_binary() == contents + + +def test_safe_unlink_is_safe_ENOSPC(tmpdir, monkeypatch): + contents = b"Hello, world\n" + victim = tmpdir / 'victim' + victim.write_binary(contents) + hard_link = tmpdir / 'hardlink' + hard_link.mklinkto(victim) + + def os_unlink(_): + raise OSError(errno.ENOSPC, "Pretend that we ran out of space") + monkeypatch.setattr(os, "unlink", os_unlink) + + with pytest.raises(OSError): + safe_unlink(hard_link) + + assert victim.read_binary() == contents