tools/gamedata_sps1.py: validate --json ancestors and --shaders/target types upfront

This commit is contained in:
kirigayakazima
2026-11-26 20:51:12 +08:00
parent a73a57e133
commit 4f930f0dfa
2 changed files with 65 additions and 7 deletions

View File

@ -126,6 +126,46 @@ class GamedataSps1AtomicTests(unittest.TestCase):
doc = json.load(f)
self.assertEqual(doc["payloads"][0]["table"]["records"], 1)
def test_non_dir_json_ancestor_refused_before_reading_target(self):
with tempfile.TemporaryDirectory() as tmp:
bin_path = os.path.join(tmp, "sample.bin")
blocker = os.path.join(tmp, "file_blocker")
with open(bin_path, "wb") as f:
f.write(_sps1_blob([("alpha.sps", "alpha", 0)]))
with open(blocker, "wb") as f:
f.write(b"not a dir")
for bad_out in (os.path.join(blocker, "out.json"), os.path.join(blocker, "nested", "out.json")):
with self.subTest(bad_out=bad_out):
err = io.StringIO()
with mock.patch.object(gamedata_sps1, "read_payloads", side_effect=AssertionError("read target")), \
contextlib.redirect_stderr(err):
rc = gamedata_sps1.main([bin_path, "--json", bad_out])
self.assertEqual(rc, 1)
self.assertIn("error:", err.getvalue())
def test_directory_shaders_and_special_target_fail_upfront_exit_2(self):
with tempfile.TemporaryDirectory() as tmp:
bin_path = os.path.join(tmp, "sample.bin")
shader_dir = os.path.join(tmp, "shaders_dir")
os.makedirs(shader_dir)
with open(bin_path, "wb") as f:
f.write(_sps1_blob([("alpha.sps", "alpha", 0)]))
err = io.StringIO()
with mock.patch.object(gamedata_sps1, "read_payloads", side_effect=AssertionError("read target")), \
contextlib.redirect_stderr(err):
rc = gamedata_sps1.main([bin_path, "--shaders", shader_dir])
self.assertEqual(rc, 2)
self.assertIn("ERROR:", err.getvalue())
err2 = io.StringIO()
with mock.patch.object(gamedata_sps1.os.path, "exists", return_value=True), \
mock.patch.object(gamedata_sps1.os.path, "isfile", return_value=False), \
mock.patch.object(gamedata_sps1.os.path, "isdir", return_value=False), \
contextlib.redirect_stderr(err2):
rc2 = gamedata_sps1.main([bin_path])
self.assertEqual(rc2, 2)
self.assertIn("ERROR:", err2.getvalue())
if __name__ == "__main__":
unittest.main()

View File

@ -311,9 +311,18 @@ def lookup_preset_hash(payloads: list[dict], target_hash: int) -> list[dict]:
def check_report_path(out_path: str, target: str, shaders_rpf: str | None = None,
rows: list[dict] | None = None) -> None:
"""Refuse a --json destination that is a directory or aliases any input file."""
if os.path.isdir(out_path):
raise ValueError(f"--json is a directory: {out_path}")
"""Refuse a --json destination that is a directory, blocked by a non-dir ancestor, or aliases any input file."""
for candidate in (os.path.abspath(out_path), os.path.realpath(out_path)):
if (os.path.exists(candidate) or os.path.islink(candidate)) and os.path.isdir(candidate):
raise ValueError(f"--json is a directory: {out_path}")
probe = os.path.dirname(candidate)
while probe and not (os.path.exists(probe) or os.path.islink(probe)):
nxt = os.path.dirname(probe)
if nxt == probe:
break
probe = nxt
if probe and not os.path.isdir(probe):
raise ValueError(f"--json parent {probe} is not a directory")
if shaders_rpf and same_report_path(out_path, shaders_rpf):
raise ValueError(f"--json would overwrite the input {shaders_rpf}")
if os.path.isdir(target):
@ -377,10 +386,19 @@ def main(argv=None) -> int:
if a.json.endswith(("/", "\\")) or (os.sep and a.json.endswith(os.sep)):
ap.error(f"--json is a directory path: {a.json}")
# A missing input path is a usage error (exit 2), not a failed run, as in BUILD-1046.
for path in (a.target, a.shaders):
if path is not None and not os.path.exists(path):
print(f"ERROR: {path} does not exist", file=sys.stderr)
# A missing or wrong-kind input path is a usage error (exit 2), not a failed run, as in BUILD-1046.
if not os.path.exists(a.target):
print(f"ERROR: {a.target} does not exist", file=sys.stderr)
return 2
if not (os.path.isfile(a.target) or os.path.isdir(a.target)):
print(f"ERROR: {a.target} is not a file or directory", file=sys.stderr)
return 2
if a.shaders is not None:
if not os.path.exists(a.shaders):
print(f"ERROR: {a.shaders} does not exist", file=sys.stderr)
return 2
if not os.path.isfile(a.shaders):
print(f"ERROR: --shaders {a.shaders} is not a file", file=sys.stderr)
return 2
try: