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
This commit is contained in:
Jakub Piasecki authored and meta-codesync[bot] committed 2026-03-31 10:10:04 -07:00
1 parent 866e55cc8e
commit 3b634bc520
3 files changed
+46 -15

No files matched your search

+1 -1
View File
@@ -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
+19 -4
View File
@@ -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")
+26 -10
View File
@@ -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