Refactor test.sh scripts for consistency

This commit is contained in:
Rangi42
2024-08-18 21:13:48 -04:00
committed by Sylvie
parent 7cf4156003
commit 731715ff36
4 changed files with 40 additions and 31 deletions

View File

@@ -10,14 +10,15 @@ gb="$(mktemp)"
input="$(mktemp)" input="$(mktemp)"
output="$(mktemp)" output="$(mktemp)"
errput="$(mktemp)" errput="$(mktemp)"
tests=0
failed=0
rc=0
# Immediate expansion is the desired behavior. # Immediate expansion is the desired behavior.
# shellcheck disable=SC2064 # shellcheck disable=SC2064
trap "rm -f ${o@Q} ${gb@Q} ${input@Q} ${output@Q} ${errput@Q}" EXIT trap "rm -f ${o@Q} ${gb@Q} ${input@Q} ${output@Q} ${errput@Q}" EXIT
tests=0
failed=0
rc=0
bold="$(tput bold)" bold="$(tput bold)"
resbold="$(tput sgr0)" resbold="$(tput sgr0)"
red="$(tput setaf 1)" red="$(tput setaf 1)"

View File

@@ -3,17 +3,19 @@
export LC_ALL=C export LC_ALL=C
tmpdir="$(mktemp -d)" tmpdir="$(mktemp -d)"
src="$PWD"
tests=0
failed=0
rc=0
cp ../../{rgbfix,contrib/gbdiff.bash} "$tmpdir" cp ../../{rgbfix,contrib/gbdiff.bash} "$tmpdir"
src="$PWD"
cd "$tmpdir" || exit cd "$tmpdir" || exit
# Immediate expansion is the desired behavior. # Immediate expansion is the desired behavior.
# shellcheck disable=SC2064 # shellcheck disable=SC2064
trap "cd; rm -rf ${tmpdir@Q}" EXIT trap "cd; rm -rf ${tmpdir@Q}" EXIT
tests=0
failed=0
rc=0
bold="$(tput bold)" bold="$(tput bold)"
resbold="$(tput sgr0)" resbold="$(tput sgr0)"
red="$(tput setaf 1)" red="$(tput setaf 1)"
@@ -111,7 +113,6 @@ rc=$((rc || $? != 1))
tryDiff "$src/noexist.err" out.err noexist.err tryDiff "$src/noexist.err" out.err noexist.err
rc=$((rc || $?)) rc=$((rc || $?))
if [[ "$failed" -eq 0 ]]; then if [[ "$failed" -eq 0 ]]; then
echo "${bold}${green}All ${tests} tests passed!${rescolors}${resbold}" echo "${bold}${green}All ${tests} tests passed!${rescolors}${resbold}"
else else

View File

@@ -3,9 +3,16 @@
[[ -e ./rgbgfx_test ]] || make -C ../.. test/gfx/rgbgfx_test Q= ${CXX:+"CXX=$CXX"} || exit [[ -e ./rgbgfx_test ]] || make -C ../.. test/gfx/rgbgfx_test Q= ${CXX:+"CXX=$CXX"} || exit
[[ -e ./randtilegen ]] || make -C ../.. test/gfx/randtilegen Q= ${CXX:+"CXX=$CXX"} || exit [[ -e ./randtilegen ]] || make -C ../.. test/gfx/randtilegen Q= ${CXX:+"CXX=$CXX"} || exit
trap 'rm -f "$errtmp"' EXIT
errtmp="$(mktemp)" errtmp="$(mktemp)"
# Immediate expansion is the desired behavior.
# shellcheck disable=SC2064
trap "rm -f ${errtmp@Q}" EXIT
tests=0
failed=0
rc=0
bold="$(tput bold)" bold="$(tput bold)"
resbold="$(tput sgr0)" resbold="$(tput sgr0)"
red="$(tput setaf 1)" red="$(tput setaf 1)"
@@ -14,19 +21,17 @@ rescolors="$(tput op)"
RGBGFX=../../rgbgfx RGBGFX=../../rgbgfx
tests=0 newTest () {
failed=0
rc=0
new_test() {
cmdline="$*" cmdline="$*"
echo "${bold}${green}Testing: ${cmdline}${rescolors}${resbold}" >&2 echo "${bold}${green}Testing: ${cmdline}${rescolors}${resbold}" >&2
} }
test() {
runTest () {
(( tests++ )) (( tests++ ))
eval "$cmdline" eval "$cmdline"
} }
fail() {
failTest () {
rc=1 rc=1
(( failed++ )) (( failed++ ))
echo "${bold}${red}Test ${cmdline} failed!${1:+ (RC=$1)}${rescolors}${resbold}" echo "${bold}${red}Test ${cmdline} failed!${1:+ (RC=$1)}${rescolors}${resbold}"
@@ -39,8 +44,8 @@ while [[ "$ofs" -eq 0 ]]; do (( ofs = RANDOM % 256 )); done
while [[ "$size" -eq 0 ]]; do (( size = RANDOM % 256 )); done while [[ "$size" -eq 0 ]]; do (( size = RANDOM % 256 )); done
for f in *.bin; do for f in *.bin; do
for flags in ""{," -b $ofs"}{," -N $size,256"}; do for flags in ""{," -b $ofs"}{," -N $size,256"}; do
new_test ./rgbgfx_test "$f" $flags newTest ./rgbgfx_test "$f" $flags
test || fail $? runTest || failTest $?
done done
done done
@@ -48,8 +53,8 @@ done
reverse_cmd="$RGBGFX -c#none,#fff,#000 -o none_round_trip.2bpp -r 1 out.png" reverse_cmd="$RGBGFX -c#none,#fff,#000 -o none_round_trip.2bpp -r 1 out.png"
reconvert_cmd="$RGBGFX -c#none,#fff,#000 -o result.2bpp out.png" reconvert_cmd="$RGBGFX -c#none,#fff,#000 -o result.2bpp out.png"
compare_cmd="cmp none_round_trip.2bpp result.2bpp" compare_cmd="cmp none_round_trip.2bpp result.2bpp"
new_test "$reverse_cmd && $reconvert_cmd && $compare_cmd" newTest "$reverse_cmd && $reconvert_cmd && $compare_cmd"
test || fail $? runTest || failTest $?
# Remove temporaries (also ignored by Git) created by the above tests # Remove temporaries (also ignored by Git) created by the above tests
rm -f out*.png result.png result.2bpp rm -f out*.png result.png result.2bpp
@@ -57,20 +62,20 @@ rm -f out*.png result.png result.2bpp
for f in *.png; do for f in *.png; do
flags="$([[ -e "${f%.png}.flags" ]] && echo "@${f%.png}.flags")" flags="$([[ -e "${f%.png}.flags" ]] && echo "@${f%.png}.flags")"
new_test "$RGBGFX" $flags "$f" newTest "$RGBGFX" $flags "$f"
if [[ -e "${f%.png}.err" ]]; then if [[ -e "${f%.png}.err" ]]; then
test 2>"$errtmp" runTest 2>"$errtmp"
diff -u --strip-trailing-cr "${f%.png}.err" "$errtmp" || fail diff -u --strip-trailing-cr "${f%.png}.err" "$errtmp" || failTest
else else
test || fail $? runTest || failTest $?
fi fi
new_test "$RGBGFX" $flags - "<$f" newTest "$RGBGFX" $flags - "<$f"
if [[ -e "${f%.png}.err" ]]; then if [[ -e "${f%.png}.err" ]]; then
test 2>"$errtmp" runTest 2>"$errtmp"
diff -u --strip-trailing-cr <(sed "s/$f/<stdin>/g" "${f%.png}.err") "$errtmp" || fail diff -u --strip-trailing-cr <(sed "s/$f/<stdin>/g" "${f%.png}.err") "$errtmp" || failTest
else else
test || fail $? runTest || failTest $?
fi fi
done done

View File

@@ -10,14 +10,15 @@ gbtemp="$(mktemp)"
gbtemp2="$(mktemp)" gbtemp2="$(mktemp)"
outtemp="$(mktemp)" outtemp="$(mktemp)"
outtemp2="$(mktemp)" outtemp2="$(mktemp)"
tests=0
failed=0
rc=0
# Immediate expansion is the desired behavior. # Immediate expansion is the desired behavior.
# shellcheck disable=SC2064 # shellcheck disable=SC2064
trap "rm -f ${otemp@Q} ${gbtemp@Q} ${gbtemp2@Q} ${outtemp@Q} ${outtemp2@Q}" EXIT trap "rm -f ${otemp@Q} ${gbtemp@Q} ${gbtemp2@Q} ${outtemp@Q} ${outtemp2@Q}" EXIT
tests=0
failed=0
rc=0
bold="$(tput bold)" bold="$(tput bold)"
resbold="$(tput sgr0)" resbold="$(tput sgr0)"
red="$(tput setaf 1)" red="$(tput setaf 1)"
@@ -53,6 +54,7 @@ tryCmp () {
fi fi
(( our_rc = our_rc || $? )) (( our_rc = our_rc || $? ))
} }
tryCmpRom () { tryCmpRom () {
# `printf` lets us keep only the first returned word from `wc`. # `printf` lets us keep only the first returned word from `wc`.
rom_size=$(printf %s $(wc -c <"$1")) rom_size=$(printf %s $(wc -c <"$1"))