diff --git a/tools/aws/regen_cfn_schema_surface.py b/tools/aws/regen_cfn_schema_surface.py index 11575b4732..e0f4c68bee 100644 --- a/tools/aws/regen_cfn_schema_surface.py +++ b/tools/aws/regen_cfn_schema_surface.py @@ -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 diff --git a/tools/aws/test_regen_cfn_schema_surface.py b/tools/aws/test_regen_cfn_schema_surface.py index 7a4a38f459..b0e4e304b9 100644 --- a/tools/aws/test_regen_cfn_schema_surface.py +++ b/tools/aws/test_regen_cfn_schema_surface.py @@ -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