]> Sergey Matveev's repositories - bfs.git/commitdiff
tests: Fix Bash 3 compatibility
authorTavian Barnes <tavianator@tavianator.com>
Mon, 23 Oct 2023 15:42:33 +0000 (11:42 -0400)
committerTavian Barnes <tavianator@tavianator.com>
Mon, 23 Oct 2023 17:00:21 +0000 (13:00 -0400)
tests/bfs/execdir_plus_nonexistent.sh
tests/common/execdir_nonexistent.sh
tests/posix/L_loops.sh
tests/posix/exec_nonexistent.sh
tests/posix/exec_plus_nonexistent.sh
tests/run.sh
tests/util.sh

index e3b4d2dd887c2b9d030a3d8ae0e9fbc3c554ec98..ed7ed56d9f9c0a18fcc1600d44dc6aeadf4df7bd 100644 (file)
@@ -1,4 +1,2 @@
-! stderr=$(invoke_bfs basic -execdir "$TESTS/nonexistent" {} + 2>&1 >/dev/null)
-[ -n "$stderr" ]
-
-! bfs_diff basic -execdir "$TESTS/nonexistent" {} + -print
+bfs_diff basic -execdir "$TESTS/nonexistent" {} + -print 2>"$TEST/err" && fail
+test -s "$TEST/err"
index 4bb4fdb5612a3ab9d58fda7a4b2441bc71412968..0ec013cee6f28715d8b579afbe1d59cabf3222f1 100644 (file)
@@ -1,4 +1,2 @@
-! stderr=$(invoke_bfs basic -execdir "$TESTS/nonexistent" {} \; 2>&1 >/dev/null)
-[ -n "$stderr" ]
-
-! bfs_diff basic -print -execdir "$TESTS/nonexistent" {} \; -print
+bfs_diff basic -print -execdir "$TESTS/nonexistent" {} \; -print 2>"$TEST/err" && fail
+test -s "$TEST/err"
index 1314401aaf4fb809c795e22ad8023aa31695d011..01b7efc3494e7b79a0a3697e7950bb96a237ae1f 100644 (file)
@@ -1,4 +1,4 @@
 # POSIX says it's okay to either stop or keep going on seeing a filesystem
 # loop, as long as a diagnostic is printed
-! errors=$(invoke_bfs -L loops 2>&1 >/dev/null)
-[ -n "$errors" ]
+invoke_bfs -L loops >/dev/null 2>"$OUT" && fail
+test -s "$OUT"
index 901be861441d02d6cfefb434d700fc55ba59d32f..a9ff052b30dbbcd3210b58cf180fa59f69b7285c 100644 (file)
@@ -1,7 +1,4 @@
 # Failure to execute the command should lead to an error message and
 # non-zero exit status.  See https://unix.stackexchange.com/q/704522/56202
-
-! stderr=$(invoke_bfs basic -exec "$TESTS/nonexistent" {} \; 2>&1 >/dev/null)
-[ -n "$stderr" ]
-
-! bfs_diff basic -print -exec "$TESTS/nonexistent" {} \; -print
+bfs_diff basic -print -exec "$TESTS/nonexistent" {} \; -print 2>"$TEST/err" && fail
+test -s "$TEST/err"
index 6bddc67540c84d5828b1876fced6549e71d92db9..24582a341c35cbde48a15163e177010ad28c1e69 100644 (file)
@@ -1,4 +1,2 @@
-! stderr=$(invoke_bfs basic -exec "$TESTS/nonexistent" {} + 2>&1 >/dev/null)
-[ -n "$stderr" ]
-
-! bfs_diff basic -exec "$TESTS/nonexistent" {} + -print
+bfs_diff basic -exec "$TESTS/nonexistent" {} + -print 2>"$TEST/err" && fail
+test -s "$TEST/err"
index b46fde63b68439a31326bb96d30c05b67d4205ba..d58147642690c1656c41e1c8323fe3ce60c5633d 100644 (file)
@@ -47,7 +47,7 @@ bg_test() {
     if ((VERBOSE_ERRORS)); then
         run_test "$1"
     else
-        run_test "$1" 2>"$TMP/TEST.err"
+        run_test "$1" 2>"$TMP/$TEST.err"
     fi
     ret=$?
 
@@ -59,9 +59,21 @@ bg_test() {
     return $ret
 }
 
-# Reap a background job
-reap() {
-    wait -n
+# 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
     ret=$?
     ((BG--))
 
@@ -118,7 +130,7 @@ run_tests() {
         OUT="$TMP/$TEST.out"
 
         if ((BG >= JOBS)); then
-            reap
+            wait_test
         fi
         ((++BG))
 
@@ -126,7 +138,7 @@ run_tests() {
     done
 
     while ((BG > 0)); do
-        reap
+        wait_test
     done
 
     printf "${BOL}"
@@ -145,6 +157,14 @@ run_tests() {
 
 ## Utilities for the tests themselves
 
+# Default return value for failed tests
+EX_FAIL=1
+
+# Fail the current test
+fail() {
+    exit $EX_FAIL
+}
+
 # Return value when a test is skipped
 EX_SKIP=77
 
@@ -166,9 +186,9 @@ skip() {
 # Run a command and check its exit status
 check_exit() {
     local expected="$1"
-    local actual="0"
+    local actual=0
     shift
-    "$@" || actual="$?"
+    "$@" || actual=$?
     ((actual == expected))
 }
 
@@ -253,9 +273,9 @@ invoke_bfs() {
 
     # Allow bfs to fail, but not crash
     if ((ret > 125)); then
-        exit "$ret"
+        exit $ret
     else
-        return "$ret"
+        return $ret
     fi
 }
 
@@ -275,9 +295,9 @@ bfs_pty() {
     "$UNBUFFER" bash -c 'stty cols 80 rows 24 && "$@"' bash "${BFS[@]}" "$@" || ret=$?
 
     if ((ret > 125)); then
-        exit "$ret"
+        exit $ret
     else
-        return "$ret"
+        return $ret
     fi
 }
 
index 31a7b6c38db66daa3b92f6fa57b430f59f3dba28..bfa5d165633df48bcc3dcff6a885ebba07bfb7fb 100644 (file)
@@ -120,7 +120,7 @@ callers() {
 
 # Print a message including path, line number, and command
 debug() {
-    local file="${1/#*\/tests\//tests\/}"
+    local file="${1/#*\/tests\//tests/}"
     set -- "$file" "${@:2}"
     color printf "${BLD}%s:%d:${RST} %s\n    %s\n" "$@"
 }
@@ -136,14 +136,13 @@ quote() {
     fi
 }
 
+DEFER_LEVEL=-1
+
 # Run a command when this (sub)shell exits
 defer() {
-    # Refresh trap state before trap -p
-    # See https://unix.stackexchange.com/a/556888/56202
-    trap -- KILL
-
     # Check if the EXIT trap is already set
-    if ! trap -p EXIT | grep -q pop_defers; then
+    if ((DEFER_LEVEL != BASH_SUBSHELL)); then
+        DEFER_LEVEL=$BASH_SUBSHELL
         DEFER_CMDS=()
         DEFER_LINES=()
         DEFER_FILES=()