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