test: a missing golden file fails instead of being written

assert_golden used to write a golden that didn't exist yet whenever CI was unset, so a deleted or misnamed golden re-recorded whatever the code did and the test passed locally. It now writes only with UPDATE_GOLDEN=1 and otherwise fails with the same message CI already gave. Space bunny flagged this earlier; the public-API guards were hardened the same way in b62806f5.
This commit is contained in:
agessaman committed 2026-10-03 22:46:30 -07:00
1 parent 853f86d8a5
commit 36ab4f983c
1 file changed
+5 -3
+5 -3
View File
@@ -20,11 +20,13 @@ GOLDEN_DIR = Path(__file__).parent / "golden"
def assert_golden(name: str, data: Any) -> None:
path = GOLDEN_DIR / f"{name}.json"
rendered = json.dumps(data, indent=1, sort_keys=True, ensure_ascii=False, default=repr) + "\n"
if os.environ.get("UPDATE_GOLDEN") == "1" or not path.exists():
if not path.exists() and os.environ.get("CI"):
raise AssertionError(f"golden file {path} missing; generate it with UPDATE_GOLDEN=1")
if os.environ.get("UPDATE_GOLDEN") == "1":
path.write_text(rendered, encoding="utf-8")
return
if not path.exists():
# Never write one implicitly: a deleted or misnamed golden would silently
# re-record whatever the code does now, and the test would pass.
raise AssertionError(f"golden file {path} missing; generate it with UPDATE_GOLDEN=1")
expected = path.read_text(encoding="utf-8")
if rendered != expected:
exp = json.loads(expected)