1
0
Fork 0
mirror of https://github.com/borgbackup/borg.git synced 2024-12-23 08:16:54 +00:00

Write a test for safe_unlink (#6311)

write a test for safe_unlink
This commit is contained in:
Andrey Bienkowski 2022-02-20 13:27:44 +00:00 committed by GitHub
parent 8a409ec1fb
commit 0132eee1d4
No known key found for this signature in database
GPG key ID: 4AEE18F83AFDEB23

View file

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