fix(cfn): show changed rows when schema verification fails (#9380)

Print a unified diff of the committed schema surface and the document
already used for verification. Keep verification read-only and retain its
nonzero result for changed or missing inventory files. Line endings are
kept in the diff and a missing final newline is marked explicitly.

Co-authored-by: Andre Kurait <AndreKurait@gmail.com>
Signed-off-by: Andre Kurait <akurait@amazon.com>
Signed-off-by: Jackson Price <jxnprice@amazon.com>
This commit is contained in:
Jackson PriceandAndre Kurait authored and GitHub committed 2026-11-24 16:12:40 -04:00
1 parent 7af9847813
commit f5849990e4
2 files changed
+40 -1

No files matched your search

+9 -1
View File
@@ -29,6 +29,7 @@ Run from anywhere in the repo:
from __future__ import annotations
import argparse
import difflib
import http.client
import io
import json
@@ -147,9 +148,16 @@ def main(argv: list[str] | None = None) -> int:
return 2
if args.verify:
if not args.output.exists() or args.output.read_text(encoding="utf-8") != document:
committed = args.output.read_text(encoding="utf-8") if args.output.exists() else None
if committed is None or committed != document:
print(f"error: {_shown(args.output)} no longer matches {provenance}; run "
f"'make cfn-schema-surface-sync' and commit the result.", file=sys.stderr)
for line in difflib.unified_diff(
(committed or "").splitlines(keepends=True), document.splitlines(keepends=True),
fromfile=str(_shown(args.output)), tofile=provenance):
print(line, end="", file=sys.stderr)
if not line.endswith("\n"):
print("\n\\ No newline at end of file", file=sys.stderr)
return 1
print(f"{_shown(args.output)} matches {provenance}.")
return 0
@@ -123,3 +123,34 @@ def test_an_incomplete_read_is_a_read_failure(tmp_path, monkeypatch, capsys):
args, _ = _setup(tmp_path, {})
assert r.main(args[2:]) == 2
assert "could not read" in capsys.readouterr().err
@pytest.mark.parametrize("committed", [
"AWS::Test::Thing\tArn\tName\n",
None,
"AWS::Test::Thing\tArn\tName,Nested,Secret",
], ids=["changed", "missing", "missing-final-newline"])
def test_verify_shows_the_actual_difference_without_writing(tmp_path, capsys, committed):
source = tmp_path / "schemas"
source.mkdir()
(source / "aws-test-thing.json").write_text(json.dumps(SCHEMA), encoding="utf-8")
inventory = tmp_path / "inventory.tsv"
inventory.write_text("AWS::Test::Thing\tThingProvisioner\n", encoding="utf-8")
output = tmp_path / "surface.tsv"
if committed is not None:
output.write_text(committed, encoding="utf-8")
args = ["--source-dir", str(source), "--inventory", str(inventory), "--output", str(output), "--verify"]
assert r.main(args) == 1
error = capsys.readouterr().err
assert f"--- {output}" in error
assert f"+++ --source-dir {source}" in error
assert "+AWS::Test::Thing\tArn\tName,Nested,Secret" in error
if committed is None:
assert not output.exists()
else:
assert "-AWS::Test::Thing\tArn\tName" in error
assert output.read_text(encoding="utf-8") == committed
if not committed.endswith("\n"):
assert f"-{committed}\n\\ No newline at end of file\n+{committed}\n" in error