Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
52 changes: 52 additions & 0 deletions tests/test_manifest_verifier.py
Original file line number Diff line number Diff line change
Expand Up @@ -896,6 +896,58 @@ def cached_names(self) -> set[str]:
with open(self.cache, encoding="utf-8") as f:
return {os.path.basename(k) for k in json.load(f)["files"]}

def assert_manifest_cache_refused(self) -> None:
with open(self.manifest, "rb") as f:
before = f.read()
with patch.object(mv, "sha256_file", wraps=mv.sha256_file) as hashing:
with self.assertRaisesRegex(mv.ManifestError, "cache must not overwrite the manifest"):
self.verify()
hashing.assert_not_called()
with open(self.manifest, "rb") as f:
self.assertEqual(f.read(), before)
with open(self.cache, "rb") as f:
self.assertEqual(f.read(), before)
self.assertFalse(os.path.exists(self.cache + ".tmp"))

def test_cache_refuses_another_spelling_of_manifest(self) -> None:
self.cache = os.path.join(self.game, "..", "manifest.json")
self.assert_manifest_cache_refused()

def test_cache_refuses_manifest_hard_link(self) -> None:
try:
os.link(self.manifest, self.cache)
except OSError as err:
self.skipTest(f"hard links unavailable: {err}")
self.assert_manifest_cache_refused()

def test_cache_refuses_manifest_symlink(self) -> None:
try:
os.symlink(self.manifest, self.cache)
except OSError as err:
self.skipTest(f"symlinks unavailable: {err}")
self.assert_manifest_cache_refused()

def test_cache_refuses_manifest_case_alias_on_folding_volume(self) -> None:
self.cache = os.path.join(self.tmp.name, "MANIFEST.JSON")
if not os.path.exists(self.cache):
self.skipTest("volume is case-sensitive")
self.assert_manifest_cache_refused()

def test_cli_cache_equal_to_manifest_preserves_input(self) -> None:
with open(self.manifest, "rb") as f:
before = f.read()
result = subprocess.run(
[sys.executable, "-I", TOOL, self.game, "--manifest", self.manifest,
"--cache", self.manifest, "--json"],
capture_output=True, text=True, timeout=15,
)
with open(self.manifest, "rb") as f:
self.assertEqual(f.read(), before)
self.assertEqual(result.returncode, 2)
self.assertIn("cache must not overwrite the manifest", result.stderr)
self.assertNotIn("Traceback", result.stderr)
self.assertEqual(result.stdout, "")

def test_second_run_hashes_nothing(self) -> None:
for jobs in (1, 4):
with self.subTest(jobs=jobs):
Expand Down
5 changes: 5 additions & 0 deletions tools/manifest_verifier.py
Original file line number Diff line number Diff line change
Expand Up @@ -28,6 +28,9 @@
import time
from typing import Callable, Optional, TextIO

sys.path.insert(0, os.path.dirname(os.path.abspath(__file__)))
from report_paths import same_report_path # noqa: E402

CHUNK = 8 * 1024 * 1024
DEFAULT_MANIFEST = os.path.join(os.path.dirname(__file__), "..", "reports", "manifest.json")
CACHE_VERSION = 1
Expand Down Expand Up @@ -326,6 +329,8 @@ def verify_manifest(
cache_path: Optional[str] = None,
) -> ManifestReport:
"""Verifies target_dir against manifest_path, optionally in parallel."""
if cache_path is not None and same_report_path(cache_path, manifest_path):
raise ManifestError(f"cache must not overwrite the manifest: {cache_path}")
try:
mode = os.stat(target_dir).st_mode
except FileNotFoundError:
Expand Down