From f8b22f20147d872aef9fa1037139c39e4dd0e687 Mon Sep 17 00:00:00 2001 From: Tavian Barnes Date: Thu, 1 Feb 2024 16:03:05 -0500 Subject: tests: Use variable redirections to dup std{out,err} Previously, we hardcoded file descriptors 3 and 4 for duplicating stdandard output/error respectively. In preparation for keeping inherited FDs open, switch to using bash's variable redirection feature to dynamically assign FDs. This feature is only available from bash 4.1 onwards, so this marks the end of our support for bash 3. macOS users will need to install a modern bash version to run our tests. --- tests/run.sh | 26 +++++++------------------- tests/util.sh | 17 ++++++++--------- 2 files changed, 15 insertions(+), 28 deletions(-) (limited to 'tests') diff --git a/tests/run.sh b/tests/run.sh index 4a159f9..85d961f 100644 --- a/tests/run.sh +++ b/tests/run.sh @@ -28,7 +28,7 @@ debug_err() { callers | while read -r line func file; do if [ "$func" = source ]; then local cmd="$(awk "NR == $line" "$file" 2>/dev/null)" || : - debug "$file" $line "${RED}error $ret${RST}" "$cmd" >&4 + debug "$file" $line "${RED}error $ret${RST}" "$cmd" >&$DUPERR break fi done @@ -73,21 +73,9 @@ bg_test() { return $ret } -# Wait for any background job to complete -if ((BASH_VERSINFO[0] > 4 || (BASH_VERSINFO[0] == 4 && BASH_VERSINFO[1] >= 3))); then - wait_any() { - wait -n - } -else - wait_any() { - read -ra jobs < <(jobs -p) - wait ${jobs[0]} - } -fi - # Wait for a background test to finish wait_test() { - wait_any + wait -n ret=$? ((BG--)) @@ -197,7 +185,7 @@ skip() { caller | { read -r line file printf "${BOL}" - debug "$file" $line "" "$(awk "NR == $line" "$file")" >&3 + debug "$file" $line "" "$(awk "NR == $line" "$file")" >&$DUPOUT } fi @@ -252,8 +240,8 @@ bfs_verbose() { ( # Close some fds to make room for the pipe, # even with extremely low ulimit -n - exec >&- 4>&- - exec >&3 3>&- + exec >&- {DUPERR}>&- + exec >&$DUPOUT {DUPOUT}>&- color bfs_verbose_impl "$@" ) fi @@ -306,7 +294,7 @@ invoke_bfs() { local ret=0 # Close the logging fds - "${BFS[@]}" "$@" 3>&- 4>&- || ret=$? + "${BFS[@]}" "$@" {DUPOUT}>&- {DUPERR}>&- || ret=$? # Allow bfs to fail, but not crash if ((ret > 125)); then @@ -391,7 +379,7 @@ diff_output() { if ((UPDATE)); then cp "$OUT" "$GOLD" else - $DIFF -u "$GOLD" "$OUT" >&4 + $DIFF -u "$GOLD" "$OUT" >&$DUPERR fi } diff --git a/tests/util.sh b/tests/util.sh index 5bd3328..686cc77 100644 --- a/tests/util.sh +++ b/tests/util.sh @@ -68,7 +68,7 @@ stdenv() { # Close stdin so bfs doesn't think we're interactive # dup() the standard fds for logging even when redirected - exec &1 4>&2 + exec &1 {DUPERR}>&2 } # Drop root priviliges or bail @@ -159,19 +159,18 @@ defer() { # Pop a single command from the defer stack and run it pop_defer() { - local i=$((${#DEFER_CMDS[@]} - 1)) - local cmd="${DEFER_CMDS[$i]}" - local file="${DEFER_FILES[$i]}" - local line="${DEFER_LINES[$i]}" - unset "DEFER_CMDS[$i]" - unset "DEFER_FILES[$i]" - unset "DEFER_LINES[$i]" + local cmd="${DEFER_CMDS[-1]}" + local file="${DEFER_FILES[-1]}" + local line="${DEFER_LINES[-1]}" + unset "DEFER_CMDS[-1]" + unset "DEFER_FILES[-1]" + unset "DEFER_LINES[-1]" local ret=0 eval "$cmd" || ret=$? if ((ret != 0)); then - debug "$file" $line "${RED}error $ret${RST}" "defer $cmd" >&4 + debug "$file" $line "${RED}error $ret${RST}" "defer $cmd" >&$DUPERR fi return $ret -- cgit v1.2.3