Created
October 1, 2024 07:16
-
-
Save adamgen/ba40b8b97b03aafbdda68654c32aad78 to your computer and use it in GitHub Desktop.
Snapshot testing for shunit2
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
snapshot() { | |
mkdir -p snapshots | |
local FILE_NAME="$1" | |
local EXPECTED="$2" | |
local FILE_PATH | |
local FILE_CONTENTS | |
FILE_PATH="snapshots/$FILE_NAME" | |
if [ -f "$FILE_PATH" ]; then | |
FILE_CONTENTS=$(cat "$FILE_PATH") | |
if [ "$EXPECTED" = "$FILE_CONTENTS" ]; then | |
_shunit_assertPass | |
else | |
if [ "$SNAPSHOT_UPDATE" = "true" ]; then | |
echo "$EXPECTED" >"$FILE_PATH" | |
fail "Snapshot $FILE_NAME updated, re-run to check" | |
else | |
failNotEquals "$FILE_NAME" "$FILE_CONTENTS" "$EXPECTED" | |
fi | |
fi | |
else | |
echo "$EXPECTED" >"$FILE_PATH" | |
fail "Snapshot $FILE_NAME created, re-run to check" | |
fi | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment