mirror of
https://github.com/eth-p/bat-extras.git
synced 2025-01-20 20:28:37 +01:00
Added testing support.
This commit is contained in:
parent
db50cbe753
commit
b6ba2e1736
10
test/data/file.txt
Normal file
10
test/data/file.txt
Normal file
@ -0,0 +1,10 @@
|
||||
cat
|
||||
dog
|
||||
car
|
||||
frog
|
||||
fox
|
||||
clocks
|
||||
bash
|
||||
$300
|
||||
^$!@
|
||||
|
1
test/data/link.txt
Symbolic link
1
test/data/link.txt
Symbolic link
@ -0,0 +1 @@
|
||||
file.txt
|
21
test/run.sh
Executable file
21
test/run.sh
Executable file
@ -0,0 +1,21 @@
|
||||
#!/usr/bin/env bash
|
||||
HERE="$(cd "$(dirname "${BASH_SOURCE[0]}")" && pwd)"
|
||||
FAIL=false
|
||||
ACTION="snapshot-test"
|
||||
|
||||
if [ -n "$1" ]; then
|
||||
ACTION="$1"
|
||||
fi
|
||||
|
||||
for test in "$HERE"/tests/*.sh; do
|
||||
test_name="$(basename "$test" .sh)"
|
||||
bash "$HERE/util/test.sh" "$ACTION" "$test_name"
|
||||
if [ $? -ne 0 ]; then
|
||||
FAIL=true
|
||||
fi
|
||||
done
|
||||
|
||||
if [ "$FAIL" = true ]; then
|
||||
exit 1
|
||||
fi
|
||||
|
4
test/tests/batgrep.pattern.sh
Normal file
4
test/tests/batgrep.pattern.sh
Normal file
@ -0,0 +1,4 @@
|
||||
set -e
|
||||
"$TEST_RUNNER" batgrep "ca" file.txt
|
||||
"$TEST_RUNNER" batgrep "ca" link.txt
|
||||
|
14
test/tests/batgrep.pattern.snapshot
Normal file
14
test/tests/batgrep.pattern.snapshot
Normal file
@ -0,0 +1,14 @@
|
||||
────────────────────────────────────────────────────────────────────────────────
|
||||
cat
|
||||
dog
|
||||
car
|
||||
frog
|
||||
fox
|
||||
────────────────────────────────────────────────────────────────────────────────
|
||||
────────────────────────────────────────────────────────────────────────────────
|
||||
cat
|
||||
dog
|
||||
car
|
||||
frog
|
||||
fox
|
||||
────────────────────────────────────────────────────────────────────────────────
|
25
test/util/runner.sh
Executable file
25
test/util/runner.sh
Executable file
@ -0,0 +1,25 @@
|
||||
#!/usr/bin/env bash
|
||||
HERE="$(cd "$(dirname "${BASH_SOURCE[0]}")" && pwd)"
|
||||
SCRIPT="$1"
|
||||
DIR_SRC="${HERE}/../../src"
|
||||
DIR_BIN="${HERE}/../../bin"
|
||||
|
||||
shift
|
||||
case "$TEST_RUNNER_USE" in
|
||||
src)
|
||||
bash "${DIR_SRC}/${SCRIPT}.sh" "$@"
|
||||
exit $?
|
||||
;;
|
||||
|
||||
bin|"")
|
||||
"${DIR_BIN}/${SCRIPT}" "$@"
|
||||
exit $?
|
||||
;;
|
||||
|
||||
*)
|
||||
printf "\x1B[31mInvalid TEST_RUNNER_USE variable.\x1B[0m\n"
|
||||
printf "\x1B[31mExpects: \x1B[33msrc\x1B[31m, \x1B[33mbin\x1B[0m\n"
|
||||
exit 1
|
||||
;;
|
||||
esac
|
||||
|
86
test/util/test.sh
Executable file
86
test/util/test.sh
Executable file
@ -0,0 +1,86 @@
|
||||
#!/usr/bin/env bash
|
||||
HERE="$(cd "$(dirname "${BASH_SOURCE[0]}")" && pwd)"
|
||||
RUNNER="${HERE}/runner.sh"
|
||||
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
|
||||
|
||||
printf "\x1B[33mError (%s):\x1B[0m\n" "$c"
|
||||
bat --style=numbers -
|
||||
;;
|
||||
|
||||
DIFF)
|
||||
printf "\x1B[31m%s\x1B[0m\n" "$1"
|
||||
printf "\x1B[33mDifference:\x1B[0m\n"
|
||||
bat --style=plain -l diff -
|
||||
;;
|
||||
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
|
||||
fail DIFF "The current revision does not match the snapshot."
|
||||
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
|
||||
fail DIFF "The current built and executed scripts act differently."
|
||||
fi
|
||||
};;
|
||||
|
||||
*) {
|
||||
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
|
||||
|
Loading…
Reference in New Issue
Block a user