wapdown/tests/regenerate_golden.py
2026-09-26 13:08:50 +03:00

49 lines
1.6 KiB
Python

#!/usr/bin/env python3
"""Rewrite every golden file under tests/golden/.
Run after an intentional change to the renderer, then read the diff --
that diff is the change, stated in output rather than in code:
uv run python tests/regenerate_golden.py
jj diff tests/golden
"""
from __future__ import annotations
import shutil
import sys
from pathlib import Path
sys.path.insert(0, str(Path(__file__).resolve().parent))
from conftest import EXAMPLES, GOLDEN, render_file # noqa: E402
from golden_cases import CASES, MULTI_CASES, XHTMLMP_CASES # noqa: E402
def main() -> int:
GOLDEN.mkdir(parents=True, exist_ok=True)
for name, (source, options) in CASES.items():
output = render_file(EXAMPLES / source, **options)
(GOLDEN / f"{name}.wml").write_text(output.text, encoding="utf-8")
print(f"wrote {name}.wml")
for name, (source, options) in MULTI_CASES.items():
directory = GOLDEN / name
if directory.exists():
shutil.rmtree(directory)
directory.mkdir(parents=True)
for document in render_file(EXAMPLES / source, **options).documents:
(directory / (document.name or "index.wml")).write_text(
document.text, encoding="utf-8"
)
print(f"wrote {name}/ ({len(list(directory.iterdir()))} files)")
for name, (source, options) in XHTMLMP_CASES.items():
output = render_file(EXAMPLES / source, renderer_name="xhtmlmp", **options)
(GOLDEN / f"{name}.xhtml").write_text(output, encoding="utf-8")
print(f"wrote {name}.xhtml")
return 0
if __name__ == "__main__":
raise SystemExit(main())