bat-extras/test/util/test-exec.sh

98 lines
2.2 KiB
Bash
Raw Normal View History

2019-06-20 01:54:23 +02:00
#!/usr/bin/env bash
HERE="$(cd "$(dirname "${BASH_SOURCE[0]}")" && pwd)"
RUNNER="${HERE}/test-runner.sh"
2019-06-20 01:54:23 +02:00
TEST_DATA="${HERE}/../data"
# Test data.
TEST_ID="$2"
TEST_SCRIPT="${HERE}/../tests/${TEST_ID}.sh"
TEST_OUT="$(mktemp)"
TEST_OUT2="$(mktemp)"
TEST_OUT_SNAPSHOT="${HERE}/../tests/${TEST_ID}.snapshot"
# Functions.
pass() {
printf "\x1B[33mTest [%s]:\x1B[32m %s\x1B[0m\n" "$TEST_ID" "Passed"
exit 0
}
fail() {
printf "\x1B[33mTest [%s]:\x1B[31m %s\x1B[0m\n" "$TEST_ID" "Failed"
case "$1" in
EXIT)
local c="in packaged script"
if [ "$2" = "bin" ]; then
c="in source script"
fi
if [[ "$TEST_QUIET" != "true" ]]; then
printf "\x1B[33mError (%s):\x1B[0m\n" "$c"
bat --style=numbers --paging=never -
fi
2019-06-20 01:54:23 +02:00
;;
DIFF)
2019-06-20 02:37:16 +02:00
printf "\x1B[33m%s\x1B[0m\n" "$2"
if [[ "$TEST_QUIET" != "true" ]]; then
printf "\x1B[33mDifference:\x1B[0m\n"
bat --style=plain --paging=never -l diff -
fi
2019-06-20 01:54:23 +02:00
;;
esac
exit 1
}
run() {
({
cd "$TEST_DATA"
export TEST_RUNNER="$RUNNER"
export TEST_RUNNER_USE="$1"
bash "$TEST_SCRIPT" >"$2" 2>&1 || exit $?
}) || fail EXIT "$1" < "$2"
}
# Run the test command.
case "$1" in
snapshot-generate) {
run src "$TEST_OUT"
mv "$TEST_OUT" "$TEST_OUT_SNAPSHOT"
printf "\x1B[33mTest [%s]:\x1B[35m %s\x1B[0m\n" "$1" "Updated"
};;
snapshot-test) {
run src "$TEST_OUT"
SNAPSHOT_DIFF="$(diff "$TEST_OUT" "$TEST_OUT_SNAPSHOT")"
if [ -z "$SNAPSHOT_DIFF" ]; then
pass
else
2019-06-20 02:37:16 +02:00
fail DIFF "The current revision does not match the snapshot." <<< "$SNAPSHOT_DIFF"
2019-06-20 01:54:23 +02:00
fi
};;
consistency-test) {
run src "$TEST_OUT"
run bin "$TEST_OUT2"
SNAPSHOT_DIFF="$(diff "$TEST_OUT" "$TEST_OUT2")"
if [ -z "$SNAPSHOT_DIFF" ]; then
pass
else
2019-06-20 02:37:16 +02:00
fail DIFF "The current built and executed scripts act differently." <<< "$SNAPSHOT_DIFF"
2019-06-20 01:54:23 +02:00
fi
};;
2019-10-23 00:21:12 +02:00
--supports) {
echo "snapshot-generate"
echo "snapshot-test"
echo "consistency-test"
exit 0
};;
2019-06-20 01:54:23 +02:00
*) {
printf "\x1B[31mUnknown subcommand.\x1B[0m\n"
printf " - \x1B[33msnapshot-generate\x1B[0m -- generate new snapshots\n"
printf " - \x1B[33msnapshot-test\x1B[0m -- compare current revision with snapshots\n"
printf " - \x1B[33mconsistency-test\x1B[0m -- compare current revision packaged and loose scripts\n"
};;
esac