mirror of
https://github.com/ProjectLeonida/mandy.git
synced 2026-12-06 08:58:36 +00:00
gamedata_graph: fix manifest/root summary invariants and --json path guards
This commit is contained in:
@ -426,6 +426,57 @@ class GamedataGraphTest(unittest.TestCase):
|
||||
self.assertEqual(gg.classify_target("../Item[@key='X']"),
|
||||
("relative/locator", "../Item[@key='X']", "relative_locator"))
|
||||
|
||||
def test_unparsed_manifest_and_rootless_payloads_keep_summary_totals_consistent(self):
|
||||
docs = [
|
||||
{"data": ALPHA, "complete": False},
|
||||
{"data": b"<Item/></CDataFileMgr__ContentChangeSetDef>",
|
||||
"complete": False, "root_hint": "CDataFileMgr__ContentChangeSetDef"},
|
||||
{"data": b"not-xml", "complete": False, "root_hint": ""},
|
||||
]
|
||||
s = gg.build_graph(docs)["summary"]
|
||||
self.assertEqual(s["parsed"], 1)
|
||||
self.assertEqual(s["documents"], 1)
|
||||
self.assertEqual(s["manifest_documents"], 1)
|
||||
self.assertEqual(s["categories"]["mount / content manifests"]["manifest_documents"], 1)
|
||||
self.assertEqual(s["roots"]["CDataFileMgr__ContentChangeSetDef"]["manifest_documents"], 1)
|
||||
self.assertEqual(s["root_count"], 1)
|
||||
self.assertEqual(s["rootless"], 1)
|
||||
self.assertEqual(s["categories"]["uncatalogued"]["roots"], 0)
|
||||
self.assertEqual(sum(c["roots"] for c in s["categories"].values()), s["root_count"])
|
||||
|
||||
def test_empty_and_whitespace_json_path_is_refused(self):
|
||||
self.finish()
|
||||
for bad in ("", " "):
|
||||
with self.subTest(bad=bad):
|
||||
r = self.run_tool(self.carve, "--json", bad)
|
||||
self.assertEqual(r.returncode, 2, r.stderr)
|
||||
self.assertIn("must not be empty", r.stderr)
|
||||
self.assertNotIn("Traceback", r.stderr)
|
||||
|
||||
def test_directory_mode_allows_existing_bin_report_and_carve_refuses_bk2(self):
|
||||
directory = os.path.join(self.root, "dir_bin")
|
||||
os.makedirs(directory)
|
||||
with open(os.path.join(directory, "a.xml"), "wb") as f:
|
||||
f.write(PLAIN)
|
||||
existing_bin = os.path.join(directory, "out.bin")
|
||||
with open(existing_bin, "wb") as f:
|
||||
f.write(b"old")
|
||||
r = self.run_tool(directory, "--json", existing_bin)
|
||||
self.assertEqual(r.returncode, 0, r.stderr)
|
||||
with open(existing_bin, encoding="utf-8") as f:
|
||||
self.assertEqual(json.load(f)["summary"]["parsed"], 1)
|
||||
|
||||
self.add(ALPHA)
|
||||
self.finish()
|
||||
bk2 = os.path.join(self.carve, "000000002000.bk2")
|
||||
with open(bk2, "wb") as f:
|
||||
f.write(b"bk2-stream")
|
||||
r2 = self.run_tool(self.carve, "--json", bk2)
|
||||
self.assertEqual(r2.returncode, 2, r2.stderr)
|
||||
self.assertIn("would overwrite an input file", r2.stderr)
|
||||
with open(bk2, "rb") as f:
|
||||
self.assertEqual(f.read(), b"bk2-stream")
|
||||
|
||||
|
||||
INHERIT = (
|
||||
b'<Root><Sets>'
|
||||
|
||||
@ -132,7 +132,7 @@ def scan_document(doc: dict) -> dict:
|
||||
return report
|
||||
report["parsed"] = True
|
||||
report["root"] = root.tag
|
||||
report["complete"] = bool(doc.get("complete", True))
|
||||
report["complete"] = True
|
||||
|
||||
own = _direct_id(root)
|
||||
if own:
|
||||
@ -426,7 +426,7 @@ def build_graph(documents: list) -> dict:
|
||||
stats["include_references"] += len(report["includes"])
|
||||
stats["target_references"] += len(report["refs"])
|
||||
stats["references"] += len(report["refs"]) + len(report["includes"])
|
||||
if root in MANIFEST_ROOT_SET:
|
||||
if report["parsed"] and root in MANIFEST_ROOT_SET:
|
||||
category_stats[category]["manifest_documents"] += 1
|
||||
root_stats[root]["manifest_documents"] += 1
|
||||
|
||||
@ -538,7 +538,7 @@ def build_graph(documents: list) -> dict:
|
||||
# one category, so this also checks that the category totals line up with the 118 named roots.
|
||||
for category, stats in summary["categories"].items():
|
||||
stats["roots"] = sum(1 for root in summary["roots"]
|
||||
if (gx.category_for(root) if root else gx.UNCATALOGUED) == category)
|
||||
if root and gx.category_for(root) == category)
|
||||
stats.setdefault("manifest_documents", 0)
|
||||
for stats in summary["roots"].values():
|
||||
stats.setdefault("manifest_documents", 0)
|
||||
@ -630,19 +630,23 @@ def main(argv=None) -> int:
|
||||
args = parser.parse_args(argv)
|
||||
|
||||
try:
|
||||
if args.json:
|
||||
if args.json is not None:
|
||||
if not args.json.strip():
|
||||
parser.error("--json path must not be empty")
|
||||
if os.path.isdir(args.json):
|
||||
parser.error(f"--json is a directory: {args.json}")
|
||||
for target in args.targets:
|
||||
is_dir = os.path.isdir(target)
|
||||
is_carve = is_dir and os.path.isfile(os.path.join(target, "index.json"))
|
||||
source = os.path.join(target, "index.json") if is_dir else target
|
||||
if gx.same_report_path(args.json, source):
|
||||
parser.error(f"--json would overwrite the input {source}")
|
||||
if is_dir and os.path.isfile(args.json):
|
||||
root = os.path.normcase(os.path.abspath(target))
|
||||
dest = os.path.normcase(os.path.abspath(args.json))
|
||||
if dest.endswith(".bin") and dest.startswith(root + os.sep):
|
||||
parser.error(f"--json would overwrite an input file: {args.json}")
|
||||
if is_carve:
|
||||
root = os.path.normcase(os.path.realpath(target))
|
||||
dest = os.path.normcase(os.path.realpath(args.json))
|
||||
if dest.lower().endswith((".bin", ".bk2")) and dest.startswith(root + os.sep):
|
||||
parser.error(f"--json would overwrite an input file: {args.json}")
|
||||
for path in gx.input_files([target]):
|
||||
if gx.same_report_path(args.json, path):
|
||||
parser.error(f"--json would overwrite the input {path}")
|
||||
@ -663,7 +667,7 @@ def main(argv=None) -> int:
|
||||
graph = build_graph(documents)
|
||||
graph["summary"]["mode"] = gx.document_mode(args.targets)
|
||||
print_summary(graph)
|
||||
if args.json:
|
||||
if args.json is not None:
|
||||
try:
|
||||
write_json(args.json, graph)
|
||||
except OSError as exc:
|
||||
|
||||
Reference in New Issue
Block a user