Files
mandy/tests/test_report_path_case.py
undeadhuman37 12d4bcbea7 tools: report paths that differ only in case are one file on macOS
bvh_index, anim_index, gamedata_xml, gamedata_index and navmesh_decode compare
two report paths with os.path.normcase, which folds case only on Windows. On
macOS's case-insensitive APFS volume `--out o/B.json --csv o/b.json` passed and
the CSV replaced the JSON. The comparison now folds case and Unicode
normalization when the volume that will hold the path is case-insensitive
(pathconf _PC_CASE_SENSITIVE); case-sensitive volumes keep two reports.
Same fix as world_index in #1576.

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
2026-11-22 15:30:32 +03:00

97 lines
3.6 KiB
Python

"""Report paths that differ only in letter case, in the tools that compare two report paths before writing.
python -I tests/test_report_path_case.py
On a case-insensitive volume (macOS's default APFS, Windows) `o/B.json` and `o/b.json` are one file, so a tool that
accepted both as `--out` and `--csv` wrote the CSV over the JSON. On a case-sensitive volume they are two reports and
must stay allowed. Everything runs in a temporary folder on hand-built inputs; no game data.
"""
from __future__ import annotations
import importlib.util
import os
import pathlib
import subprocess
import sys
import tempfile
import unittest
ROOT = pathlib.Path(__file__).resolve().parent.parent
TOOLS = ROOT / "tools"
if str(TOOLS) not in sys.path:
sys.path.insert(0, str(TOOLS))
# tool -> name of its two-path comparison helper
HELPERS = {
"bvh_index": "same_output_path",
"anim_index": "same_report_path",
"gamedata_xml": None,
"gamedata_index": None,
"navmesh_decode": "same_report_path",
}
def load(name):
spec = importlib.util.spec_from_file_location(f"case_{name}", TOOLS / f"{name}.py")
module = importlib.util.module_from_spec(spec)
spec.loader.exec_module(module)
return module
def helper(module, name):
if HELPERS[name]:
return getattr(module, HELPERS[name])
found = [v for k, v in vars(module).items()
if callable(v) and k.startswith("same_") and k.endswith("_path")]
assert len(found) == 1, (name, found)
return found[0]
class ReportPathCaseTest(unittest.TestCase):
def setUp(self):
self._tmp = tempfile.TemporaryDirectory()
self.dir = pathlib.Path(self._tmp.name)
probe = self.dir / "Probe"
probe.touch()
self.insensitive = (self.dir / "probe").exists()
probe.unlink()
def tearDown(self):
self._tmp.cleanup()
def test_new_paths_that_differ_only_in_case(self):
upper, lower = self.dir / "new" / "B.json", self.dir / "new" / "b.json"
for name in HELPERS:
with self.subTest(tool=name):
same = helper(load(name), name)
self.assertTrue(same(str(upper), str(upper)))
self.assertEqual(same(str(upper), str(lower)), self.insensitive)
self.assertFalse(same(str(upper), str(self.dir / "new" / "c.json")))
def test_new_paths_that_differ_only_in_accent_form(self):
if sys.platform != "darwin" or not self.insensitive:
self.skipTest("APFS/HFS+ name normalization")
composed, decomposed = self.dir / "café.json", self.dir / "CAFÉ.json"
for name in HELPERS:
with self.subTest(tool=name):
self.assertTrue(helper(load(name), name)(str(composed), str(decomposed)))
def test_bvh_index_refuses_case_only_report_pair(self):
source = self.dir / "in.rpf"
source.write_bytes(bytes(4096))
out = self.dir / "o"
r = subprocess.run([sys.executable, "-I", str(TOOLS / "bvh_index.py"), str(source),
"--out", str(out / "B.json"), "--csv", str(out / "b.json")],
cwd=ROOT, capture_output=True, text=True, timeout=60)
if self.insensitive:
self.assertEqual(r.returncode, 2, r.stderr)
self.assertIn("--out and --csv must use different paths", r.stderr)
self.assertFalse(out.exists() and any(out.iterdir()))
else:
self.assertEqual(r.returncode, 0, r.stderr)
self.assertEqual(sorted(p.name for p in out.iterdir()), ["B.json", "b.json"])
if __name__ == "__main__":
unittest.main()