diff --git a/docs/gamedata_graph.md b/docs/gamedata_graph.md index 7b58ffef5..de247ed7c 100644 --- a/docs/gamedata_graph.md +++ b/docs/gamedata_graph.md @@ -8,7 +8,8 @@ name or target path is committed. `tools/gamedata_graph.py` rebuilds the documents with `gamedata_index.py` and `gamedata_xml.load_documents`, then keeps the reference strings long enough to join them. It is -standard-library only and reads the same carve as the index and catalog. +standard-library only and reads the same carve as the index and catalog. XML with an unsupported +encoding counts as an unparsed payload; it supplies no edges and does not stop the other documents. ## Graph model diff --git a/tests/test_gamedata_graph.py b/tests/test_gamedata_graph.py index 9e53d4b89..688dd6077 100644 --- a/tests/test_gamedata_graph.py +++ b/tests/test_gamedata_graph.py @@ -97,6 +97,24 @@ class GamedataGraphTest(unittest.TestCase): return subprocess.run([sys.executable, "-I", TOOL, *args], cwd=self.root, capture_output=True, text=True, errors="replace") + def test_unknown_encoding_counts_as_unparsed_beside_valid_document(self): + unsupported = os.path.join(self.root, "unsupported.xml") + valid = os.path.join(self.root, "valid.xml") + out = os.path.join(self.root, "graph.json") + with open(unsupported, "wb") as f: + f.write(b'') + with open(valid, "wb") as f: + f.write(ALPHA) + result = self.run_tool(unsupported, valid, "--json", out) + self.assertEqual(result.returncode, 0, result.stderr) + with open(out, encoding="utf-8") as f: + summary = json.load(f)["summary"] + self.assertEqual(summary["payloads"], 2) + self.assertEqual(summary["parsed"], 1) + self.assertEqual(summary["unparsed"], 1) + self.assertEqual(summary["rootless"], 0) + self.assertEqual(summary["include_sites"], 2) + def test_cli_reports_unwritable_graph_report(self): self.finish() out = os.path.join(self.root, "missing", "graph.json") diff --git a/tools/gamedata_graph.py b/tools/gamedata_graph.py index f277a6e5a..081912549 100644 --- a/tools/gamedata_graph.py +++ b/tools/gamedata_graph.py @@ -118,9 +118,10 @@ def scan_document(doc: dict) -> dict: "refs": [], "includes": [], } + data = doc["data"] try: - root = ET.fromstring(doc["data"]) - except (ET.ParseError, ValueError): + root = ET.fromstring(data) + except (ET.ParseError, ValueError, LookupError): # A truncated tail can still carry a closing-root hint from the index. It is a named but # unparsed document, not a rootless fragment; only an empty hint is rootless. report["root"] = doc.get("root_hint") or ""