Update to use 'best' for testing

This commit is contained in:
Ethan P 2020-02-19 20:49:20 -08:00
parent 5cbbbb6ebc
commit 88e71bbb38
No known key found for this signature in database
GPG Key ID: 6963FD04F6CF35EA
40 changed files with 238 additions and 502 deletions

View File

@ -61,7 +61,7 @@ jobs:
minify: "all"
- run:
name: "Test: Snapshots"
command: test/run.sh snapshot-test
command: ./test.sh
test-consistency:
executor: linux_alpine
@ -72,7 +72,7 @@ jobs:
minify: "all"
- run:
name: "Test: Consistency"
command: test/run.sh consistency-test
command: ./test.sh --compiled
test-symlink:
executor: linux_alpine

4
.gitmodules vendored Normal file
View File

@ -0,0 +1,4 @@
[submodule "best"]
path = .test-framework
url = https://github.com/eth-p/best.git

1
.test-framework Submodule

@ -0,0 +1 @@
Subproject commit 723fe4479b6efea7e4dd99aaace287b21e7178ab

View File

@ -309,7 +309,7 @@ done
if "$OPT_VERIFY"; then
printc "\n%{YELLOW}Verifying scripts...%{CLEAR}\n" 1>&2
TEST_QUIET=true "$HERE/test/run.sh" consistency-test
"${HERE}/test.sh" --compiled
exit $?
fi

39
test.sh Executable file
View File

@ -0,0 +1,39 @@
#!/usr/bin/env bash
# -----------------------------------------------------------------------------
# bat-extras | Copyright (C) 2019 eth-p | MIT License
#
# Repository: https://github.com/eth-p/bat-extras
# Issues: https://github.com/eth-p/bat-extras/issues
# -----------------------------------------------------------------------------
HERE="$(cd "$(dirname "${BASH_SOURCE[0]}")" && pwd)"
LIB="${HERE}/lib"
source "${LIB}/opt.sh"
# -----------------------------------------------------------------------------
cd "$HERE"
# -----------------------------------------------------------------------------
export TEST_ENV_LIB="${HERE}/lib"
export TEST_ENV_BIN_DIR="${HERE}/src"
export TEST_ENV_BIN_SUFFIX=".sh"
export TEST_DIR="${HERE}/test/suite"
export TEST_PWD="${HERE}/test/data"
export TEST_SHIM_PATH="${HERE}/test/shim"
export SNAPSHOT_DIR="${HERE}/test/snapshot"
OPT_ARGV=()
while shiftopt; do
case "$OPT" in
--compiled) TEST_ENV_BIN_DIR="${HERE}/bin"; TEST_ENV_BIN_SUFFIX="" ;;
*) OPT_ARGV+=("$OPT") ;;
esac
done
# -----------------------------------------------------------------------------
# Initialize submodule if it isn't already.
if ! [[ -d "${HERE}/.test-framework" ]]; then
git submodule init '.test-framework'
git submodule update
fi
# Run best.
exec "${HERE}/.test-framework/bin/best.sh" "${OPT_ARGV[@]}"

4
test/data/file.sh Normal file
View File

@ -0,0 +1,4 @@
echo Hello, test world!
if true; then
! true
fi

View File

@ -1,112 +0,0 @@
#!/usr/bin/env bash
# -----------------------------------------------------------------------------
# bat-extras | Copyright (C) 2019 eth-p | MIT License
#
# Repository: https://github.com/eth-p/bat-extras
# Issues: https://github.com/eth-p/bat-extras/issues
# -----------------------------------------------------------------------------
HERE="$(cd "$(dirname "${BASH_SOURCE[0]}")" && pwd)"
LIB="${HERE}/../lib"
source "${LIB}/print.sh"
# -----------------------------------------------------------------------------
# Runs a test.
#
# Arguments:
# 1 -- The test name.
# 2 -- The test action.
#
# Returns:
# 0 -- The test passed.
# 1 -- The test failed.
run_test() {
local test_name="$1"
local test_action="$2"
export DIR_SRC="${HERE}/../src"
export DIR_BIN="${HERE}/../bin"
export DIR_LIB="${HERE}/../lib"
bash "$HERE/util/test-exec.sh" "$test_action" "$test_name"
return $?
}
# Runs all tests.
#
# Arguments:
# 1 -- The test action.
#
# Variables:
# RESULT -- "pass" or "fail".
# RESULT_PASS -- The number of tests that passed.
# RESULT_FAIL -- The number of tests that failed.
run_all_tests() {
local test_action="$1"
RESULT="pass"
RESULT_PASS=0
RESULT_FAIL=0
for test in "$HERE"/tests/*.sh; do
if run_test "$(basename "$test" .sh)" "$test_action"; then
((RESULT_PASS++))
else
((RESULT_FAIL++))
RESULT="fail"
fi
done
if [ "$RESULT" = "fail" ]; then
return 1
else
return 0
fi
}
# Displays a summary of the tests run.
display_test_summary() {
local tpc
local tfc
[[ "$RESULT_PASS" -gt 0 ]] && tpc="GREEN" || tpc="CLEAR";
[[ "$RESULT_FAIL" -gt 0 ]] && tfc="RED" || tfc="CLEAR";
printc "%{YELLOW}RESULT: %{${tpc}}%s%{YELLOW} passed, %{${tfc}}%s%{YELLOW} failed.%{CLEAR}\n" \
"$RESULT_PASS" \
"$RESULT_FAIL"
}
# -----------------------------------------------------------------------------
if [ -n "$1" ]; then
test_types="$("$HERE/util/test-exec.sh" --supports)"
if ! grep -F "$1" <<< "$test_types" &>/dev/null; then
printc "%{RED}Unknown test type: %s%{CLEAR}\n" "$1"
printc "%{RED}Supported types:%{CLEAR}\n%s\n" "$test_types"
exit 2
fi
run_all_tests "$1"
exit_status=$?
display_test_summary
exit $exit_status
fi
# Run all actions.
FINAL_RESULT="pass"
for action in "snapshot-test" "consistency-test"; do
printc "%{CYAN}Running %{CLEAR}%s%{CYAN} tests:%{CLEAR}\n" "$action"
run_all_tests "$action"
display_test_summary
printf "\n"
if [[ "$RESULT" = "fail" ]]; then
FINAL_RESULT=fail
fi
done
case "$FINAL_RESULT" in
pass) exit 0;;
fail) exit 1;;
esac

3
test/shim/batgrep.sh Normal file
View File

@ -0,0 +1,3 @@
batgrep() {
"${BIN_DIR}/batgrep${BIN_SUFFIX}" "$@" || return $?
}

View File

@ -0,0 +1,4 @@
────────────────────────────────────────────────────────────────────────────────
$300
^$!@
────────────────────────────────────────────────────────────────────────────────

View File

@ -0,0 +1,8 @@
────────────────────────────────────────────────────────────────────────────────
File: file.txt
 1 cat 
 2 dog
 3 car 
 4 frog
 5 fox
────────────────────────────────────────────────────────────────────────────────

View File

@ -0,0 +1,7 @@
────────────────────────────────────────────────────────────────────────────────
clocks
bash
$300
^$!@
────────────────────────────────────────────────────────────────────────────────

View File

@ -0,0 +1,11 @@
────────────────────────────────────────────────────────────────────────────────
cat
dog
car
frog
fox
clocks
bash
$300
^$!@
────────────────────────────────────────────────────────────────────────────────

View File

@ -0,0 +1,7 @@
────────────────────────────────────────────────────────────────────────────────
cat
dog
car
frog
fox
────────────────────────────────────────────────────────────────────────────────

59
test/suite/batgrep.sh Normal file
View File

@ -0,0 +1,59 @@
setup() {
use_shim 'batgrep'
}
test:regular_file() {
description "Search for a pattern in a regular file."
snapshot stdout
snapshot stderr
batgrep "ca" file.txt
}
test:symlink_file() {
description "Search for a pattern in a symlinked file."
snapshot stdout
snapshot stderr
batgrep "ca" link.txt
}
test:output_with_color() {
description "Snapshot test for colored output."
snapshot stdout
snapshot stderr
batgrep "ca" file.txt --color=always
}
test:output_without_color() {
description "Snapshot test for colored output."
snapshot stdout
snapshot stderr
batgrep "ca" file.txt --color=never
}
test:search_regex() {
description "Search for a regex pattern."
snapshot stdout
snapshot stderr
batgrep "^[cb]" file.txt
}
test:search_fixed() {
description "Search for fixed strings."
snapshot stdout
snapshot stderr
batgrep --fixed-strings '$' file.txt
}
test:option_context() {
description "Search and use the context option."
snapshot stdout
snapshot stderr
batgrep -C 0 '\$' file.txt
}

88
test/suite/lib_opt.sh Normal file
View File

@ -0,0 +1,88 @@
setup() {
set - pos1 \
--val1 for_val1 \
--val2=for_val2 \
pos2 \
--flag1 \
-v3=for_val3 \
-v4 for_val4 \
--flag2
source "${LIB}/opt.sh"
}
assert_opt_value() {
assert [ "$OPT_VAL" = "$1" ]
}
assert_opt_valueless() {
assert [ -z "$OPT_VAL" ]
}
test:long() {
description "Parse long options."
while shiftopt; do
if [[ "$OPT" = "--flag1" ]]; then
assert_opt_valueless
return
fi
done
fail 'Failed to find option.'
}
test:long_value_implicit() {
description "Parse long options in '--long value' syntax."
while shiftopt; do
if [[ "$OPT" = "--val1" ]]; then
shiftval
assert_opt_value 'for_val1'
return
fi
done
fail 'Failed to find option.'
}
test:long_value_explicit() {
description "Parse long options in '--long=value' syntax."
while shiftopt; do
if [[ "$OPT" = "--val2" ]]; then
shiftval
assert_opt_value 'for_val2'
return
fi
done
fail 'Failed to find option.'
}
test:short_value_implicit() {
description "Parse short options in '-k value' syntax."
while shiftopt; do
if [[ "$OPT" = "-v4" ]]; then
shiftval
assert_opt_value 'for_val4'
return
fi
done
fail 'Failed to find option.'
}
test:short_value_explicit() {
description "Parse short options in '-k=value' syntax."
while shiftopt; do
if [[ "$OPT" =~ ^-v3 ]]; then
assert_equal "$OPT" "-v3=for_val3"
return
fi
done
fail 'Failed to find option.'
}

View File

@ -1,3 +0,0 @@
set -e
"$TEST_RUNNER" batgrep "ca" file.txt

View File

@ -1,3 +0,0 @@
set -e
"$TEST_RUNNER" batgrep "ca" link.txt

View File

@ -1,3 +0,0 @@
set -e
"$TEST_RUNNER" batman ./batman.1 --color=always

View File

@ -1,21 +0,0 @@
────────────────────────────────────────────────────────────────────────────────
batman(1) batman(1)



NAME
 batman - Display manpages using bat


SYNOPSIS
 batman page [section]


DESCRIPTION
 A wrapper script that uses bat to highlight the output of a manpage.




 22 October 2019 batman(1)
────────────────────────────────────────────────────────────────────────────────

View File

@ -1,3 +0,0 @@
set -e
"$TEST_RUNNER" batman ./batman.1 --color=never

View File

@ -1,19 +0,0 @@
batman(1) batman(1)
NAME
batman - Display manpages using bat
SYNOPSIS
batman page [section]
DESCRIPTION
A wrapper script that uses bat to highlight the output of a manpage.
22 October 2019 batman(1)

View File

@ -1,23 +0,0 @@
set -e
set - pos1 \
--val1 for_val1 \
--val2=for_val2 \
pos2 \
--flag1 \
-v4 for_val4 \
--flag2 \
source "${DIR_LIB}/opt.sh"
# Run a standard option parsing loop.
echo "PROGRAM: $PROGRAM"
while shiftopt; do
case "$OPT" in
--val*) shiftval; printf "LONG_OPTION: \"%s\" with value \"%s\"\n" "${OPT}" "${OPT_VAL}" ;;
--*) printf "LONG_FLAG: \"%s\"\n" "${OPT}" ;;
-v*) shiftval; printf "SHORT_OPTION: \"%s\" with value \"%s\"\n" "${OPT}" "${OPT_VAL}" ;;
-*) printf "SHORT_FLAG: \"%s\"\n" "${OPT}" ;;
*) printf "ARGUMENT: \"%s\"\n" "${OPT}" ;;
esac
done

View File

@ -1,8 +0,0 @@
PROGRAM: lib.opt
ARGUMENT: "pos1"
LONG_OPTION: "--val1" with value "for_val1"
LONG_OPTION: "--val2" with value "for_val2"
ARGUMENT: "pos2"
LONG_FLAG: "--flag1"
SHORT_OPTION: "-v4" with value "for_val4"
LONG_FLAG: "--flag2"

View File

@ -1,77 +0,0 @@
pager_info() {
source "${DIR_LIB}/pager.sh"
printf "\n"
printf "PAGER=%q\n" "$PAGER"
printf "BAT_PAGER=%q\n" "$BAT_PAGER"
printf "SCRIPT_PAGER_CMD=("
if [[ "${#SCRIPT_PAGER_CMD[@]}" -gt 0 ]]; then
printf "%q" "${SCRIPT_PAGER_CMD[0]}"
if [[ "${#SCRIPT_PAGER_CMD[@]}" -gt 1 ]]; then
printf " %q" "${SCRIPT_PAGER_CMD[@]:1}"
fi
fi
printf ")\n"
printf "SCRIPT_PAGER_ARGS=("
if [[ "${#SCRIPT_PAGER_ARGS[@]}" -gt 0 ]]; then
printf "%q" "${SCRIPT_PAGER_ARGS[0]}"
if [[ "${#SCRIPT_PAGER_ARGS[@]}" -gt 1 ]]; then
printf " %q" "${SCRIPT_PAGER_ARGS[@]:1}"
fi
fi
printf ")\n"
}
pager_test() {
if [[ "$1" = "TTY" ]]; then
if [[ "$(uname -s)" = "Darwin" ]]; then
script -q /dev/null bash "${BASH_SOURCE[0]}" --execute
else
script -q -c "bash $(printf "%q" "${BASH_SOURCE[0]}") --execute" /dev/null
fi
elif [[ "$1" = "FILE" ]]; then
bash "${BASH_SOURCE[0]}" --execute | cat
fi
}
if [[ "$1" = "--execute" ]]; then
pager_info
exit
fi
# First test.
unset PAGER
unset BAT_PAGER
pager_test TTY
# Second test.
unset PAGER
export BAT_PAGER='less'
pager_test TTY
# Third test.
unset PAGER
export BAT_PAGER='less -R -F'
pager_test TTY
# Forth test.
export PAGER='less'
unset BAT_PAGER
pager_test TTY
# Fifth test.
export PAGER='less -R -F'
unset BAT_PAGER
pager_test TTY
# Sixth test.
export PAGER='less -R -F'
export BAT_PAGER='more'
pager_test TTY
# Final test.
export PAGER='less -R -F'
export BAT_PAGER='more'
pager_test FILE

View File

@ -1,35 +0,0 @@
PAGER=''
BAT_PAGER=''
SCRIPT_PAGER_CMD=('')
SCRIPT_PAGER_ARGS=()
PAGER=''
BAT_PAGER=less
SCRIPT_PAGER_CMD=(less)
SCRIPT_PAGER_ARGS=()
PAGER=''
BAT_PAGER=less\ -R\ -F
SCRIPT_PAGER_CMD=(less -R -F)
SCRIPT_PAGER_ARGS=()
PAGER=less
BAT_PAGER=''
SCRIPT_PAGER_CMD=(less)
SCRIPT_PAGER_ARGS=(-R)
PAGER=less\ -R\ -F
BAT_PAGER=''
SCRIPT_PAGER_CMD=(less\ -R\ -F)
SCRIPT_PAGER_ARGS=()
PAGER=less\ -R\ -F
BAT_PAGER=more
SCRIPT_PAGER_CMD=(more)
SCRIPT_PAGER_ARGS=()
PAGER=less\ -R\ -F
BAT_PAGER=more
SCRIPT_PAGER_CMD=()
SCRIPT_PAGER_ARGS=()

View File

@ -1,14 +0,0 @@
set -e
# Create symlink.
templink="$(mktemp -t "TEMP_symlink_XXXX")"
rm "$templink"
ln -s "$(pwd)/../../src/batgrep.sh" "$templink"
chmod +x "$templink"
# Run symlink.
"$(dirname "$templink")/$(basename "$templink")" "templink" "${BASH_SOURCE[0]}" -C 0 | sed '1d;$d' || true
# Cleanup.
rm "$templink"

View File

@ -1,6 +0,0 @@
templink="$(mktemp -t "TEMP_symlink_XXXX")"
rm "$templink"
ln -s "$(pwd)/../../src/batgrep.sh" "$templink"
chmod +x "$templink"
"$(dirname "$templink")/$(basename "$templink")" "templink" "${BASH_SOURCE[0]}" -C 0 | sed '1d;$d' || true
rm "$templink"

View File

@ -1,16 +0,0 @@
set -e
# Create symlink.
templink="$(basename "$0" ".sh")._temp"
tempabs="$(pwd)/${templink}"
ln -s "../../src/batgrep.sh" "$templink"
chmod +x "$templink"
# Run symlink.
export PATH="$(pwd):$PATH"
cd /tmp
"$(basename "$templink")" "templink" "${BASH_SOURCE[0]}" -C 0 | sed '1d;$d' || true
# Cleanup.
rm "$tempabs"

View File

@ -1,5 +0,0 @@
templink="$(basename "$0" ".sh")._temp"
tempabs="$(pwd)/${templink}"
ln -s "../../src/batgrep.sh" "$templink"
chmod +x "$templink"
"$(basename "$templink")" "templink" "${BASH_SOURCE[0]}" -C 0 | sed '1d;$d' || true

View File

@ -1,104 +0,0 @@
#!/usr/bin/env bash
HERE="$(cd "$(dirname "${BASH_SOURCE[0]}")" && pwd)"
RUNNER="${HERE}/test-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
if [[ "$TEST_QUIET" != "true" ]]; then
printf "\x1B[33mError (%s):\x1B[0m\n" "$c"
bat --style=numbers --paging=never -
fi
;;
DIFF)
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
;;
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" "$TEST_ID" "Updated"
};;
snapshot-display) {
run src "$TEST_OUT"
printf "\x1B[33mTest [%s]:\x1B[35m %s\x1B[0m\n" "$TEST_ID" "Output:"
cat "$TEST_OUT"
};;
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." <<< "$SNAPSHOT_DIFF"
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." <<< "$SNAPSHOT_DIFF"
fi
};;
--supports) {
echo "snapshot-generate"
echo "snapshot-display"
echo "snapshot-test"
echo "consistency-test"
exit 0
};;
*) {
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

View File

@ -1,47 +0,0 @@
#!/usr/bin/env bash
# -----------------------------------------------------------------------------
# bat-extras | Copyright (C) 2019 eth-p | MIT License
#
# Repository: https://github.com/eth-p/bat-extras
# Issues: https://github.com/eth-p/bat-extras/issues
# -----------------------------------------------------------------------------
# This is the test runner.
#
# It creates and sets up an environment that acts consistently and in a way
# that is optimal for testing the output of bat.
#
# It can be executed in a test script through the "$TEST_RUNNER" variable.
#
# Arguments:
# 1 -- The script to execute.
# ... -- The arguments to pass to the script.
#
# -----------------------------------------------------------------------------
HERE="$(cd "$(dirname "${BASH_SOURCE[0]}")" && pwd)"
SCRIPT="$1"
DIR_SRC="${HERE}/../../src"
DIR_BIN="${HERE}/../../bin"
export BAT_PAGER=""
export PAGER="cat"
export TERM="xterm"
export LANG="en_US.UTF-8"
shift
case "$TEST_RUNNER_USE" in
src)
bash "${DIR_SRC}/${SCRIPT}.sh" "$@" 2>&1 | cat
exit $?
;;
bin|"")
"${DIR_BIN}/${SCRIPT}" "$@" 2>&1 | cat
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