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

View File

@@ -7,8 +7,10 @@ src="$PWD"
rc=0 rc=0
cp ../../{rgbfix,contrib/gbdiff.bash} "$tmpdir" cp ../../{rgbfix,contrib/gbdiff.bash} "$tmpdir"
cd "$tmpdir" cd "$tmpdir" || exit
trap "cd; rm -rf '$tmpdir'" EXIT # Immediate expansion is the desired behaviour.
# shellcheck disable=SC2064
trap "cd; rm -rf ${tmpdir@Q}" EXIT
bold="$(tput bold)" bold="$(tput bold)"
resbold="$(tput sgr0)" resbold="$(tput sgr0)"
@@ -43,7 +45,7 @@ runTest () {
fi fi
if [[ -z "$variant" ]]; then if [[ -z "$variant" ]]; then
cp "$2/$1.bin" out.gb cp "$2/$1.bin" out.gb
if [[ -n "$(eval $RGBFIX $flags out.gb '2>out.err')" ]]; then if [[ -n "$(eval "$RGBFIX" $flags out.gb '2>out.err')" ]]; then
echo "${bold}${red}Fixing $1 in-place shouldn't output anything on stdout!${rescolors}${resbold}" echo "${bold}${red}Fixing $1 in-place shouldn't output anything on stdout!${rescolors}${resbold}"
our_rc=1 our_rc=1
fi fi
@@ -52,18 +54,19 @@ runTest () {
# Stop! This is not a Useless Use Of Cat. Using cat instead of # Stop! This is not a Useless Use Of Cat. Using cat instead of
# stdin redirection makes the input an unseekable pipe - a scenario # stdin redirection makes the input an unseekable pipe - a scenario
# that's harder to deal with. # that's harder to deal with.
# shellcheck disable=SC2002
cat "$2/$1.bin" | eval $RGBFIX "$flags" - '>out.gb' '2>out.err' cat "$2/$1.bin" | eval $RGBFIX "$flags" - '>out.gb' '2>out.err'
subst='<stdin>' subst='<stdin>'
fi fi
sed "s/$subst/<filename>/g" "out.err" | tryDiff "$2/$1.err" - "$1.err${variant}" sed "s/$subst/<filename>/g" "out.err" | tryDiff "$2/$1.err" - "$1.err${variant}"
our_rc=$(($? || $our_rc)) (( our_rc = our_rc || $? ))
if [[ -r "$2/$1.gb" ]]; then if [[ -r "$2/$1.gb" ]]; then
tryCmp "$2/$1.gb" "out.gb" "$1.gb${variant}" tryCmp "$2/$1.gb" "out.gb" "$1.gb${variant}"
our_rc=$(($? || $our_rc)) (( our_rc = our_rc || $? ))
fi fi
rc=$(($rc || $our_rc)) (( rc = rc || our_rc ))
if [[ $our_rc -ne 0 ]]; then break; fi if [[ $our_rc -ne 0 ]]; then break; fi
done done
} }

View File

@@ -9,7 +9,9 @@ gbtemp2="$(mktemp)"
outtemp="$(mktemp)" outtemp="$(mktemp)"
rc=0 rc=0
trap "rm -f '$otemp' '$gbtemp' '$gbtemp2' '$outtemp'" EXIT # Immediate expansion is the desired behaviour.
# shellcheck disable=SC2064
trap "rm -f ${otemp@Q} ${gbtemp@Q} ${gbtemp2@Q} ${outtemp@Q}" EXIT
bold="$(tput bold)" bold="$(tput bold)"
resbold="$(tput sgr0)" resbold="$(tput sgr0)"
@@ -38,6 +40,12 @@ tryCmp () {
false false
fi fi
} }
tryCmpRom () {
# `printf` lets us keep only the first returned word.
rom_size=$(printf %s $(wc -c <"$1"))
dd if="$gbtemp" count=1 bs="$rom_size" >"$otemp" 2>/dev/null
tryCmp "$1" "$otemp"
}
rgblinkQuiet () { rgblinkQuiet () {
out="$(env $RGBLINK "$@")" || return $? out="$(env $RGBLINK "$@")" || return $?
@@ -49,51 +57,50 @@ rgblinkQuiet () {
for i in *.asm; do for i in *.asm; do
startTest startTest
$RGBASM -o $otemp $i "$RGBASM" -o "$otemp" "$i"
# Some tests have variants depending on flags # Some tests have variants depending on flags
ran_flag= ran_flag=false
for flag in '-d' '-t' '-w'; do for flag in '-d' '-t' '-w'; do
if [ -f ${i%.asm}-no${flag}.out ]; then if [ -f "${i%.asm}-no${flag}.out" ]; then
rgblinkQuiet -o $gbtemp $otemp > $outtemp 2>&1 rgblinkQuiet -o "$gbtemp" "$otemp" >"$outtemp" 2>&1
tryDiff ${i%.asm}-no${flag}.out $outtemp tryDiff "${i%.asm}-no${flag}.out" "$outtemp"
rc=$(($? || $rc)) (( rc = rc || $? ))
ran_flag=1 ran_flag=true
fi fi
if [ -f ${i%.asm}${flag}.out ]; then if [ -f "${i%.asm}${flag}.out" ]; then
rgblinkQuiet ${flag} -o $gbtemp $otemp > $outtemp 2>&1 rgblinkQuiet ${flag} -o "$gbtemp" "$otemp" >"$outtemp" 2>&1
tryDiff ${i%.asm}${flag}.out $outtemp tryDiff "${i%.asm}${flag}.out" "$outtemp"
rc=$(($? || $rc)) (( rc = rc || $? ))
ran_flag=1 ran_flag=true
fi fi
done done
if [ -n "$ran_flag" ]; then if "$ran_flag"; then
continue continue
fi fi
# Other tests have several linker scripts # Other tests have several linker scripts
find . -name "${i%.asm}*.link" | while read script; do while read -rd '' script; do
rgblinkQuiet -l $script -o $gbtemp $otemp > $outtemp 2>&1 rgblinkQuiet -l "$script" -o "$gbtemp" "$otemp" >"$outtemp" 2>&1
tryDiff ${script%.link}.out $outtemp tryDiff "${script%.link}.out" "$outtemp"
rc=$(($? || $rc)) (( rc = rc || $? ))
ran_flag=1 ran_flag=1
done done < <(find . -name "${i%.asm}*.link" -print0)
if [ -n "$ran_flag" ]; then if [ -n "$ran_flag" ]; then
continue continue
fi fi
# The rest of the tests just links a file, and maybe checks the binary # The rest of the tests just links a file, and maybe checks the binary
rgblinkQuiet -o $gbtemp $otemp > $outtemp 2>&1 rgblinkQuiet -o "$gbtemp" "$otemp" >"$outtemp" 2>&1
if [ -f ${i%.asm}.out ]; then if [ -f "${i%.asm}.out" ]; then
tryDiff ${i%.asm}.out $outtemp tryDiff "${i%.asm}.out" "$outtemp"
rc=$(($? || $rc)) (( rc = rc || $? ))
fi fi
bin=${i%.asm}.out.bin bin=${i%.asm}.out.bin
if [ -f $bin ]; then if [ -f "$bin" ]; then
dd if=$gbtemp count=1 bs=$(printf %s $(wc -c < $bin)) > $otemp 2>/dev/null tryCmpRom "$bin"
tryCmp $bin $otemp (( rc = rc || $? ))
rc=$(($? || $rc))
fi fi
done done
@@ -101,85 +108,81 @@ done
i="bank-const.asm" i="bank-const.asm"
startTest startTest
$RGBASM -o $otemp bank-const/a.asm "$RGBASM" -o "$otemp" bank-const/a.asm
$RGBASM -o $gbtemp2 bank-const/b.asm "$RGBASM" -o "$gbtemp2" bank-const/b.asm
rgblinkQuiet -o $gbtemp $gbtemp2 $otemp > $outtemp 2>&1 rgblinkQuiet -o "$gbtemp" "$gbtemp2" "$otemp" >"$outtemp" 2>&1
tryDiff bank-const/out.err $outtemp tryDiff bank-const/out.err "$outtemp"
rc=$(($? || $rc)) (( rc = rc || $? ))
for i in fragment-align/*; do for i in fragment-align/*; do
startTest startTest
$RGBASM -o $otemp $i/a.asm "$RGBASM" -o "$otemp" "$i"/a.asm
$RGBASM -o $gbtemp2 $i/b.asm "$RGBASM" -o "$gbtemp2" "$i"/b.asm
rgblinkQuiet -o $gbtemp $otemp $gbtemp2 2>$outtemp rgblinkQuiet -o "$gbtemp" "$otemp" "$gbtemp2" 2>"$outtemp"
tryDiff $i/out.err $outtemp tryDiff "$i"/out.err "$outtemp"
rc=$(($? || $rc)) (( rc = rc || $? ))
if [[ -f $i/out.gb ]]; then if [[ -f "$i"/out.gb ]]; then
dd if=$gbtemp count=1 bs=$(printf %s $(wc -c < $i/out.gb)) > $otemp 2>/dev/null tryCmpRom "$i"/out.gb
tryCmp $i/out.gb $otemp (( rc = rc || $? ))
rc=$(($? || $rc))
fi fi
done done
i="high-low.asm" i="high-low.asm"
startTest startTest
$RGBASM -o $otemp high-low/a.asm "$RGBASM" -o "$otemp" high-low/a.asm
rgblinkQuiet -o $gbtemp $otemp rgblinkQuiet -o "$gbtemp" "$otemp"
$RGBASM -o $otemp high-low/b.asm "$RGBASM" -o "$otemp" high-low/b.asm
rgblinkQuiet -o $gbtemp2 $otemp rgblinkQuiet -o "$gbtemp2" "$otemp"
tryCmp $gbtemp $gbtemp2 tryCmp "$gbtemp" "$gbtemp2"
rc=$(($? || $rc)) (( rc = rc || $? ))
i="overlay.asm" i="overlay.asm"
startTest startTest
$RGBASM -o $otemp overlay/a.asm "$RGBASM" -o "$otemp" overlay/a.asm
rgblinkQuiet -o $gbtemp -t -O overlay/overlay.gb $otemp > $outtemp 2>&1 rgblinkQuiet -o "$gbtemp" -t -O overlay/overlay.gb "$otemp" >"$outtemp" 2>&1
tryDiff overlay/out.err $outtemp tryDiff overlay/out.err "$outtemp"
rc=$(($? || $rc)) (( rc = rc || $? ))
# This test does not trim its output with 'dd' because it needs to verify the correct output size # This test does not trim its output with 'dd' because it needs to verify the correct output size
tryCmp overlay/out.gb $gbtemp tryCmp overlay/out.gb "$gbtemp"
rc=$(($? || $rc)) (( rc = rc || $? ))
i="section-fragment/jr-offset.asm" i="section-fragment/jr-offset.asm"
startTest startTest
$RGBASM -o $otemp section-fragment/jr-offset/a.asm "$RGBASM" -o "$otemp" section-fragment/jr-offset/a.asm
$RGBASM -o $gbtemp2 section-fragment/jr-offset/b.asm "$RGBASM" -o "$gbtemp2" section-fragment/jr-offset/b.asm
rgblinkQuiet -o $gbtemp $otemp $gbtemp2 rgblinkQuiet -o "$gbtemp" "$otemp" "$gbtemp2"
dd if=$gbtemp count=1 bs=$(printf %s $(wc -c < section-fragment/jr-offset/ref.out.bin)) > $otemp 2>/dev/null tryCmpRom section-fragment/jr-offset/ref.out.bin
tryCmp section-fragment/jr-offset/ref.out.bin $otemp (( rc = rc || $? ))
rc=$(($? || $rc))
i="section-union/good.asm" i="section-union/good.asm"
startTest startTest
$RGBASM -o $otemp section-union/good/a.asm "$RGBASM" -o "$otemp" section-union/good/a.asm
$RGBASM -o $gbtemp2 section-union/good/b.asm "$RGBASM" -o "$gbtemp2" section-union/good/b.asm
rgblinkQuiet -o $gbtemp -l section-union/good/script.link $otemp $gbtemp2 rgblinkQuiet -o "$gbtemp" -l section-union/good/script.link "$otemp" "$gbtemp2"
dd if=$gbtemp count=1 bs=$(printf %s $(wc -c < section-union/good/ref.out.bin)) > $otemp 2>/dev/null tryCmpRom section-union/good/ref.out.bin
tryCmp section-union/good/ref.out.bin $otemp (( rc = rc || $? ))
rc=$(($? || $rc))
i="section-union/fragments.asm" i="section-union/fragments.asm"
startTest startTest
$RGBASM -o $otemp section-union/fragments/a.asm "$RGBASM" -o "$otemp" section-union/fragments/a.asm
$RGBASM -o $gbtemp2 section-union/fragments/b.asm "$RGBASM" -o "$gbtemp2" section-union/fragments/b.asm
rgblinkQuiet -o $gbtemp $otemp $gbtemp2 rgblinkQuiet -o "$gbtemp" "$otemp" "$gbtemp2"
dd if=$gbtemp count=1 bs=$(printf %s $(wc -c < section-union/fragments/ref.out.bin)) > $otemp 2>/dev/null tryCmpRom section-union/fragments/ref.out.bin
tryCmp section-union/fragments/ref.out.bin $otemp (( rc = rc || $? ))
rc=$(($? || $rc))
for i in section-union/*.asm; do for i in section-union/*.asm; do
startTest startTest
$RGBASM -o $otemp $i "$RGBASM" -o "$otemp" "$i"
$RGBASM -o $gbtemp2 $i -DSECOND "$RGBASM" -o "$gbtemp2" "$i" -DSECOND
if rgblinkQuiet $otemp $gbtemp2 2>$outtemp; then if rgblinkQuiet "$otemp" "$gbtemp2" 2>"$outtemp"; then
echo -e "${bold}${red}$i didn't fail to link!${rescolors}${resbold}" echo -e "${bold}${red}$i didn't fail to link!${rescolors}${resbold}"
rc=1 rc=1
fi fi
echo --- >> $outtemp echo --- >>"$outtemp"
# Ensure RGBASM also errors out # Ensure RGBASM also errors out
cat $i - $i <<<'def SECOND equs "1"' | $RGBASM - 2>> $outtemp cat "$i" - "$i" <<<'def SECOND equs "1"' | "$RGBASM" - 2>>"$outtemp"
tryDiff ${i%.asm}.out $outtemp tryDiff "${i%.asm}.out" "$outtemp"
rc=$(($? || $rc)) (( rc = rc || $? ))
done done
exit $rc exit $rc