mirror of
https://github.com/react/react-native.git
synced 2026-09-28 13:23:09 +08:00
Summary: Pull Request resolved: https://github.com/facebook/react-native/pull/55906 Changelog: [Internal] Adds a simple snapshot diffing mode to the snapshot generation (`--check` argument). Removes `RUN_ON_REACT_NATIVE` constant in favor of `--test` argument. Reviewed By: cipolleschi Differential Revision: D93407648 fbshipit-source-id: 7860bca27ba6784b9b8517ec86bd16b41af48d9b
74 lines
2.4 KiB
Python
74 lines
2.4 KiB
Python
# Copyright (c) Meta Platforms, Inc. and affiliates.
|
|
#
|
|
# This source code is licensed under the MIT license found in the
|
|
# LICENSE file in the root directory of this source tree.
|
|
|
|
"""
|
|
Utilities for comparing API snapshots.
|
|
"""
|
|
|
|
import difflib
|
|
import os
|
|
|
|
|
|
def check_snapshots(generated_dir: str, committed_dir: str) -> bool:
|
|
"""Compare generated snapshots against committed ones.
|
|
|
|
Returns True if check passes (snapshots match or no committed snapshots).
|
|
Returns False if snapshots differ.
|
|
"""
|
|
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)")
|
|
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)")
|
|
return True
|
|
|
|
committed_set = set(committed_files)
|
|
generated_set = set(generated_files)
|
|
all_passed = True
|
|
|
|
for filename in sorted(committed_set | generated_set):
|
|
committed_path = os.path.join(committed_dir, filename)
|
|
generated_path = os.path.join(generated_dir, filename)
|
|
|
|
if filename not in generated_set:
|
|
print(
|
|
f"FAIL: {filename} exists in committed snapshots but was not generated"
|
|
)
|
|
all_passed = False
|
|
continue
|
|
|
|
if filename not in committed_set:
|
|
print(f"OK: {filename} generated (no committed baseline)")
|
|
continue
|
|
|
|
with open(committed_path) as f:
|
|
committed_content = f.read()
|
|
with open(generated_path) as f:
|
|
generated_content = f.read()
|
|
|
|
if committed_content == generated_content:
|
|
print(f"OK: {filename} matches committed snapshot")
|
|
else:
|
|
print(f"FAIL: {filename} differs from committed snapshot")
|
|
diff = "\n".join(
|
|
difflib.unified_diff(
|
|
committed_content.splitlines(),
|
|
generated_content.splitlines(),
|
|
fromfile=f"committed/{filename}",
|
|
tofile=f"generated/{filename}",
|
|
lineterm="",
|
|
)
|
|
)
|
|
print(diff)
|
|
all_passed = False
|
|
|
|
return all_passed
|