Merge pull request #480 from kotekompotek/work/gen-gdk-classes-makedirs

Why merged: --out now creates its missing parent folder instead of failing.
Why it's good: security-reviewed; tested on Windows 11 merged onto main c023a36: the new test errors on main and passes here; a 3-line change.

AI-assisted (Claude Code, Claude Opus 5.5).
This commit is contained in:
QemmHD
2026-11-24 01:59:48 -05:00
committed by GitHub
2 changed files with 35 additions and 0 deletions

View File

@ -0,0 +1,32 @@
"""Regression test for --out parent directory creation in tools/gen_gdk_runtime_classes.py."""
from __future__ import annotations
import json
import sys
import unittest
from pathlib import Path
from tempfile import TemporaryDirectory
from unittest import mock
REPO = Path(__file__).resolve().parent.parent
sys.path.insert(0, str(REPO / "tools"))
import gen_gdk_runtime_classes as gen # noqa: E402
class TestGenGdkOutDir(unittest.TestCase):
def test_main_creates_missing_out_parent_directory(self) -> None:
with TemporaryDirectory() as tmp:
docs = Path(tmp) / "docs"
(docs / "COM").mkdir(parents=True)
out_json = Path(tmp) / "nested" / "dir" / "classes.json"
with mock.patch.object(gen, "revision", return_value="abc123"):
rc = gen.main([str(docs), "--out", str(out_json)])
self.assertEqual(rc, 0)
self.assertTrue(out_json.is_file())
self.assertEqual(json.loads(out_json.read_text(encoding="utf-8"))["classes"], {})
if __name__ == "__main__":
unittest.main()

View File

@ -133,6 +133,9 @@ def main(argv: list[str] | None = None) -> int:
problems = check(table["classes"], table["interfaces"])
for p in problems:
print(f"warning: {p}", file=sys.stderr)
parent = os.path.dirname(os.path.abspath(a.out))
if parent:
os.makedirs(parent, exist_ok=True)
with open(a.out, "w", encoding="utf-8", newline="\n") as f:
json.dump(table, f, indent=1, sort_keys=True)
f.write("\n")