Fix shellcheck warnings in the test scripts

Making them more robust to changes.
We ought to automate this some day.

My version of ShellCheck (v0.9.0) errors on test/gfx/test.sh, though...
This commit is contained in:
ISSOtm
2023-11-24 20:52:46 +01:00
parent f4463b1708
commit 6f0defbfe5
3 changed files with 121 additions and 110 deletions

View File

@@ -12,7 +12,9 @@ output="$(mktemp)"
errput="$(mktemp)"
rc=0
trap "rm -f '$o' '$gb' '$input' '$output' '$errput'" EXIT
# Immediate expansion is the desired behaviour.
# shellcheck disable=SC2064
trap "rm -f ${o@Q} ${gb@Q} ${input@Q} ${output@Q} ${errput@Q}" EXIT
bold="$(tput bold)"
resbold="$(tput sgr0)"
@@ -40,9 +42,9 @@ tryCmp () {
}
# Add the version constants test, outputting the closest tag to the HEAD
if git describe --tags --abbrev=0 > version.out; then
$RGBASM --version >> version.out
cat > version.asm <<EOF
if git describe --tags --abbrev=0 >version.out; then
$RGBASM --version >>version.out
cat >version.asm <<EOF
IF !DEF(__RGBDS_RC__)
PRINTLN "v{d:__RGBDS_MAJOR__}.{d:__RGBDS_MINOR__}.{d:__RGBDS_PATCH__}"
ELSE
@@ -57,9 +59,9 @@ fi
# Check whether to use '.simple.err' files if they exist
# (rgbasm with pre-3.0 Bison just reports "syntax error")
$RGBASM -Weverything -o $o syntax-error.asm > $output 2> $errput
$RGBASM -Weverything -o "$o" syntax-error.asm >"$output" 2>"$errput"
simple_error=0
if ! diff --strip-trailing-cr syntax-error.err $errput; then
if ! diff --strip-trailing-cr syntax-error.err "$errput"; then
echo "${bold}${orange}Warning: using .simple.err files when available.${rescolors}${resbold}"
simple_error=1
fi
@@ -67,19 +69,19 @@ fi
for i in *.asm; do
flags=${i%.asm}.flags
RGBASMFLAGS=-Weverything
if [ -f $flags ]; then
if [ -f "$flags" ]; then
RGBASMFLAGS="$(head -n 1 "$flags")" # Allow other lines to serve as comments
fi
for variant in '' '.pipe'; do
echo "${bold}${green}${i%.asm}${variant}...${rescolors}${resbold}"
desired_errname=${i%.asm}.err
if [ "$simple_error" -eq 1 ] && [ -e ${i%.asm}.simple.err ]; then
desired_errname=${i%.asm}.simple.err
if [ "$simple_error" -eq 1 ] && [ -e "${i%.asm}.simple.err" ]; then
desired_errname="${i%.asm}.simple.err"
fi
if [ -z "$variant" ]; then
$RGBASM $RGBASMFLAGS -o $o $i > $output 2> $errput
desired_output=${i%.asm}.out
desired_errput=$desired_errname
$RGBASM $RGBASMFLAGS -o "$o" "$i" >"$output" 2>"$errput"
desired_output="${i%.asm}.out"
desired_errput="$desired_errname"
else
# `include-recursion.asm` refers to its own name inside the test code.
# Skip testing with stdin input for that file.
@@ -91,32 +93,35 @@ for i in *.asm; do
# stdin redirection makes the input an unseekable pipe - a scenario
# that's harder to deal with and was broken when the feature was
# first implemented.
cat $i | $RGBASM $RGBASMFLAGS -o $o - > $output 2> $errput
# shellcheck disable=SC2002
cat "$i" | "$RGBASM" $RGBASMFLAGS -o "$o" - >"$output" 2>"$errput"
# Use two otherwise unused files for temp storage
desired_output=$input
desired_errput=$gb
desired_output="$input"
desired_errput="$gb"
# Escape regex metacharacters
subst="$(printf '%s\n' "$i" | sed 's:[][\/.^$*]:\\&:g')"
# Replace the file name with a dash to match changed output
sed "s/$subst/<stdin>/g" ${i%.asm}.out > $desired_output
sed "s/$subst/<stdin>/g" $desired_errname > $desired_errput
sed "s/$subst/<stdin>/g" "${i%.asm}.out" >"$desired_output"
sed "s/$subst/<stdin>/g" "$desired_errname" >"$desired_errput"
fi
tryDiff $desired_output $output out
tryDiff "$desired_output" "$output" out
our_rc=$?
tryDiff $desired_errput $errput err
our_rc=$(($? || $our_rc))
tryDiff "$desired_errput" "$errput" err
(( our_rc = our_rc || $? ))
bin=${i%.asm}.out.bin
if [ -f $bin ]; then
$RGBLINK -o $gb $o
dd if=$gb count=1 bs=$(printf %s $(wc -c < $bin)) > $output 2>/dev/null
tryCmp $bin $output
our_rc=$(($? || $our_rc))
if [ -f "$bin" ]; then
"$RGBLINK" -o "$gb" "$o"
# `printf` ensures we only capture the first word.
bin_size=$(printf %s $(wc -c <"$bin"))
dd "if=$gb" count=1 "bs=$bin_size" >"$output" 2>/dev/null
tryCmp "$bin" "$output"
(( our_rc = our_rc || $? ))
fi
rc=$(($rc || $our_rc))
(( rc = rc || our_rc ))
if [ $our_rc -ne 0 ]; then break; fi
done
done