From 3b634bc52073c8870eebb53cadb6a8771157070d Mon Sep 17 00:00:00 2001 From: Jakub Piasecki Date: Tue, 31 Mar 2026 10:10:04 -0700 Subject: [PATCH] Run the snapshot comparison on CI (#56207) Summary: Pull Request resolved: https://github.com/facebook/react-native/pull/56207 Changelog: [Internal] Reviewed By: cipolleschi Differential Revision: D96101927 fbshipit-source-id: 06c6dd11c145be44bcdd97f14683a222c66c96c6 --- .gitignore | 2 +- scripts/cxx-api/parser/__main__.py | 23 +++++++++++++--- scripts/cxx-api/parser/snapshot_diff.py | 36 ++++++++++++++++++------- 3 files changed, 46 insertions(+), 15 deletions(-) diff --git a/.gitignore b/.gitignore index 4cd31cacb03..d1e403fa6aa 100644 --- a/.gitignore +++ b/.gitignore @@ -183,5 +183,5 @@ __pycache__/ # Doxygen XML output used for C++ API tracking /packages/react-native/**/api/xml /packages/react-native/**/api/codegen -/packages/react-native/**/.doxygen.config.generated +/packages/react-native/**/.doxygen.config.*.generated /scripts/cxx-api/codegen diff --git a/scripts/cxx-api/parser/__main__.py b/scripts/cxx-api/parser/__main__.py index df051850786..574b54d1bfa 100644 --- a/scripts/cxx-api/parser/__main__.py +++ b/scripts/cxx-api/parser/__main__.py @@ -36,8 +36,12 @@ def run_command( """Run a subprocess command with consistent error handling.""" result = subprocess.run(cmd, **kwargs) if result.returncode != 0: - if verbose: - print(f"{label} finished with error: {result.stderr}") + stderr_output = result.stderr or "" + if isinstance(stderr_output, bytes): + stderr_output = stderr_output.decode("utf-8", errors="replace") + print(f"{label} failed (exit code {result.returncode})", file=sys.stderr) + if stderr_output: + print(stderr_output, file=sys.stderr) raise RuntimeError( f"{label} finished with error (exit code {result.returncode})" ) @@ -54,9 +58,11 @@ def build_codegen( ) -> str: react_native_dir = os.path.join(get_react_native_dir(), "packages", "react-native") + node_bin = os.environ.get("NODE_BIN", "node") + run_command( [ - "node", + node_bin, "./scripts/generate-codegen-artifacts.js", "--path", "./", @@ -254,6 +260,11 @@ def main(): action="store_true", help="Generate snapshots to a temp directory and compare against committed ones", ) + parser.add_argument( + "--validate-output", + type=str, + help="File path to write validate results to (used with --validate)", + ) parser.add_argument( "--snapshot-dir", type=str, @@ -337,7 +348,11 @@ def main(): if args.validate: snapshot_dir = args.snapshot_dir or get_default_snapshot_dir() - if not validate_snapshots(snapshot_output_dir, snapshot_dir): + if not validate_snapshots( + snapshot_output_dir, + snapshot_dir, + output_file=args.validate_output, + ): sys.exit(1) print("All snapshot validations passed") diff --git a/scripts/cxx-api/parser/snapshot_diff.py b/scripts/cxx-api/parser/snapshot_diff.py index c503a8d43a3..c4d2cbe6118 100644 --- a/scripts/cxx-api/parser/snapshot_diff.py +++ b/scripts/cxx-api/parser/snapshot_diff.py @@ -9,25 +9,40 @@ Utilities for comparing API snapshots. import difflib import os +import sys -def validate_snapshots(generated_dir: str, committed_dir: str) -> bool: +def validate_snapshots( + generated_dir: str, committed_dir: str, output_file: str | None = None +) -> bool: """Compare generated snapshots against committed ones. Returns True if validation passes (snapshots match or no committed snapshots). Returns False if snapshots differ. + + If output_file is provided, writes comparison results to that file + instead of stdout. """ + out = open(output_file, "w") if output_file else sys.stdout + try: + return _check_snapshots_impl(generated_dir, committed_dir, out) + finally: + if output_file: + out.close() + + +def _check_snapshots_impl(generated_dir: str, committed_dir: str, out) -> bool: if not os.path.isdir(committed_dir): - print(f"No committed snapshots directory found at: {committed_dir}") - print("Skipping comparison (no baseline to compare against)") + print(f"No committed snapshots directory found at: {committed_dir}", file=out) + print("Skipping comparison (no baseline to compare against)", file=out) return True committed_files = sorted(f for f in os.listdir(committed_dir) if f.endswith(".api")) generated_files = sorted(f for f in os.listdir(generated_dir) if f.endswith(".api")) if not committed_files: - print("No committed snapshot files found") - print("Skipping comparison (no baseline to compare against)") + print("No committed snapshot files found", file=out) + print("Skipping comparison (no baseline to compare against)", file=out) return True committed_set = set(committed_files) @@ -40,13 +55,14 @@ def validate_snapshots(generated_dir: str, committed_dir: str) -> bool: if filename not in generated_set: print( - f"FAIL: {filename} exists in committed snapshots but was not generated" + f"FAIL: {filename} exists in committed snapshots but was not generated", + file=out, ) all_passed = False continue if filename not in committed_set: - print(f"OK: {filename} generated (no committed baseline)") + print(f"OK: {filename} generated (no committed baseline)", file=out) continue with open(committed_path) as f: @@ -55,9 +71,9 @@ def validate_snapshots(generated_dir: str, committed_dir: str) -> bool: generated_content = f.read() if committed_content == generated_content: - print(f"OK: {filename} matches committed snapshot") + print(f"OK: {filename} matches committed snapshot", file=out) else: - print(f"FAIL: {filename} differs from committed snapshot") + print(f"FAIL: {filename} differs from committed snapshot", file=out) diff = "\n".join( difflib.unified_diff( committed_content.splitlines(), @@ -67,7 +83,7 @@ def validate_snapshots(generated_dir: str, committed_dir: str) -> bool: lineterm="", ) ) - print(diff) + print(diff, file=out) all_passed = False return all_passed