|
| 1 | +"""Tests for utils.filesystem helpers.""" |
| 2 | + |
| 3 | +import errno |
| 4 | +import os |
| 5 | +from unittest.mock import patch |
| 6 | + |
| 7 | +import pytest |
| 8 | + |
| 9 | +from utils.filesystem import link_or_copy_file |
| 10 | + |
| 11 | + |
| 12 | +class TestLinkOrCopyFile: |
| 13 | + """Test the hardlink-with-copy-fallback helper used by importers and exporters.""" |
| 14 | + |
| 15 | + def test_same_filesystem_creates_hardlink(self, tmp_path): |
| 16 | + """When source and dest are on the same filesystem, the helper creates a |
| 17 | + hardlink — source and dest share an inode and st_nlink reflects both.""" |
| 18 | + source = tmp_path / "source.bin" |
| 19 | + source.write_bytes(b"payload") |
| 20 | + dest = tmp_path / "dest.bin" |
| 21 | + |
| 22 | + link_or_copy_file(source, dest) |
| 23 | + |
| 24 | + assert dest.read_bytes() == b"payload" |
| 25 | + # Hardlink: shared inode, link count >= 2 |
| 26 | + assert source.stat().st_ino == dest.stat().st_ino |
| 27 | + assert source.stat().st_nlink >= 2 |
| 28 | + |
| 29 | + def test_falls_back_to_copy_on_exdev(self, tmp_path): |
| 30 | + """When os.link raises EXDEV (cross-device), the helper falls back to |
| 31 | + shutil.copy2 — content matches but inodes differ.""" |
| 32 | + source = tmp_path / "source.bin" |
| 33 | + source.write_bytes(b"payload") |
| 34 | + dest = tmp_path / "dest.bin" |
| 35 | + |
| 36 | + exdev = OSError(errno.EXDEV, "Cross-device link") |
| 37 | + |
| 38 | + with patch("utils.filesystem.os.link", side_effect=exdev): |
| 39 | + link_or_copy_file(source, dest) |
| 40 | + |
| 41 | + assert dest.read_bytes() == b"payload" |
| 42 | + # Real copy: separate inodes |
| 43 | + assert source.stat().st_ino != dest.stat().st_ino |
| 44 | + |
| 45 | + def test_falls_back_to_copy_on_eperm(self, tmp_path): |
| 46 | + """EPERM (filesystem doesn't permit hardlinks, e.g. FAT32) must also |
| 47 | + trigger the copy fallback.""" |
| 48 | + source = tmp_path / "source.bin" |
| 49 | + source.write_bytes(b"payload") |
| 50 | + dest = tmp_path / "dest.bin" |
| 51 | + |
| 52 | + eperm = OSError(errno.EPERM, "Operation not permitted") |
| 53 | + |
| 54 | + with patch("utils.filesystem.os.link", side_effect=eperm): |
| 55 | + link_or_copy_file(source, dest) |
| 56 | + |
| 57 | + assert dest.read_bytes() == b"payload" |
| 58 | + assert source.stat().st_ino != dest.stat().st_ino |
| 59 | + |
| 60 | + def test_reraises_non_fallback_oserror(self, tmp_path): |
| 61 | + """An OSError that isn't in the fallback set (e.g. ENOSPC — disk full) |
| 62 | + must propagate; we don't want to mask real disk errors.""" |
| 63 | + source = tmp_path / "source.bin" |
| 64 | + source.write_bytes(b"payload") |
| 65 | + dest = tmp_path / "dest.bin" |
| 66 | + |
| 67 | + enospc = OSError(errno.ENOSPC, "No space left on device") |
| 68 | + |
| 69 | + with patch("utils.filesystem.os.link", side_effect=enospc): |
| 70 | + with pytest.raises(OSError) as excinfo: |
| 71 | + link_or_copy_file(source, dest) |
| 72 | + |
| 73 | + assert excinfo.value.errno == errno.ENOSPC |
| 74 | + assert not dest.exists() |
| 75 | + |
| 76 | + def test_mutation_after_link_affects_source(self, tmp_path): |
| 77 | + """Document hardlink semantics: writing through the dest path with |
| 78 | + O_TRUNC truncates the shared inode and therefore mutates the source. |
| 79 | + This is the exact hazard `_store_cover` avoids with allow_link=False.""" |
| 80 | + source = tmp_path / "source.bin" |
| 81 | + source.write_bytes(b"original") |
| 82 | + dest = tmp_path / "dest.bin" |
| 83 | + |
| 84 | + link_or_copy_file(source, dest) |
| 85 | + |
| 86 | + # Truncating-write through dest affects source (same inode). |
| 87 | + with open(dest, "wb") as f: |
| 88 | + f.write(b"mutated") |
| 89 | + |
| 90 | + assert source.read_bytes() == b"mutated" |
| 91 | + |
| 92 | + def test_mutation_after_copy_does_not_affect_source(self, tmp_path): |
| 93 | + """When the helper falls back to copy, the inodes are independent — |
| 94 | + mutation through dest leaves the source untouched.""" |
| 95 | + source = tmp_path / "source.bin" |
| 96 | + source.write_bytes(b"original") |
| 97 | + dest = tmp_path / "dest.bin" |
| 98 | + |
| 99 | + with patch( |
| 100 | + "utils.filesystem.os.link", |
| 101 | + side_effect=OSError(errno.EXDEV, "Cross-device link"), |
| 102 | + ): |
| 103 | + link_or_copy_file(source, dest) |
| 104 | + |
| 105 | + with open(dest, "wb") as f: |
| 106 | + f.write(b"mutated") |
| 107 | + |
| 108 | + assert source.read_bytes() == b"original" |
| 109 | + |
| 110 | + def test_dest_already_exists_raises(self, tmp_path): |
| 111 | + """os.link raises EEXIST when dest already exists; that's not in the |
| 112 | + fallback set, so it propagates. Callers are responsible for handling |
| 113 | + the already-exists case before calling this helper.""" |
| 114 | + source = tmp_path / "source.bin" |
| 115 | + source.write_bytes(b"payload") |
| 116 | + dest = tmp_path / "dest.bin" |
| 117 | + dest.write_bytes(b"existing") |
| 118 | + |
| 119 | + with pytest.raises(OSError) as excinfo: |
| 120 | + link_or_copy_file(source, dest) |
| 121 | + |
| 122 | + assert excinfo.value.errno == errno.EEXIST |
| 123 | + |
| 124 | + def test_helper_uses_os_link_first(self, tmp_path): |
| 125 | + """Sanity-check that the helper calls os.link before any copy logic — |
| 126 | + guards against a future refactor regressing to copy-only.""" |
| 127 | + source = tmp_path / "source.bin" |
| 128 | + source.write_bytes(b"payload") |
| 129 | + dest = tmp_path / "dest.bin" |
| 130 | + |
| 131 | + with patch("utils.filesystem.os.link", wraps=os.link) as link_spy: |
| 132 | + link_or_copy_file(source, dest) |
| 133 | + |
| 134 | + link_spy.assert_called_once_with(source, dest) |
0 commit comments