]> Sergey Matveev's repositories - bfs.git/commitdiff
tests/color: Remove some useless cats
authorTavian Barnes <tavianator@tavianator.com>
Fri, 20 Oct 2023 19:52:45 +0000 (15:52 -0400)
committerTavian Barnes <tavianator@tavianator.com>
Fri, 20 Oct 2023 19:52:45 +0000 (15:52 -0400)
tests/color.sh
tests/getopts.sh
tests/run.sh
tests/util.sh

index 0d6ef68db1eca3e79223b55956ea50c407de5f7c..805d2b88691f870872586b58d28157558f85954a 100644 (file)
@@ -24,20 +24,14 @@ color_fd() {
 color_fd 1 && COLOR_STDOUT=1 || COLOR_STDOUT=0
 color_fd 2 && COLOR_STDERR=1 || COLOR_STDERR=0
 
-# Save these in case the tests unset PATH
-CAT=$(command -v cat)
+# Save this in case the tests unset PATH
 SED=$(command -v sed)
 
 # Filter out escape sequences if necessary
 color() {
     if color_fd 1; then
-        "$CAT"
+        "$@"
     else
-        "$SED" $'s/\e\\[[^m]*m//g'
+        "$@" | "$SED" $'s/\e\\[[^m]*m//g'
     fi
 }
-
-# printf with auto-detected color support
-cprintf() {
-    printf "$@" | color
-}
index 6616a4a23e61ce5876865bf2bbd9049aa5c36399..7d3ef4b056c189f1bc1301726878e6f65b829548 100644 (file)
@@ -8,7 +8,7 @@
 # Print usage information
 usage() {
     local pad=$(printf "%*s" ${#0} "")
-    color <<EOF
+    color cat <<EOF
 Usage: ${GRN}$0${RST} [${BLU}--bfs${RST}=${MAG}path/to/bfs${RST}] [${BLU}--sudo${RST}[=${BLD}COMMAND${RST}]] [${BLU}--stop${RST}]
        $pad [${BLU}--no-clean${RST}] [${BLU}--update${RST}] [${BLU}--verbose${RST}[=${BLD}LEVEL${RST}]] [${BLU}--help${RST}]
        $pad [${BLU}--posix${RST}] [${BLU}--bsd${RST}] [${BLU}--gnu${RST}] [${BLU}--all${RST}] [${BLD}TEST${RST} [${BLD}TEST${RST} ...]]
@@ -117,7 +117,7 @@ parse_args() {
                 exit 0
                 ;;
             -*)
-                cprintf "${RED}error:${RST} Unrecognized option '%s'.\n\n" "$arg" >&2
+                color printf "${RED}error:${RST} Unrecognized option '%s'.\n\n" "$arg" >&2
                 usage >&2
                 exit 1
                 ;;
@@ -149,9 +149,9 @@ parse_args() {
     done
 
     if ((${#TEST_CASES[@]} == 0)); then
-        cprintf "${RED}error:${RST} No tests matched" >&2
-        cprintf " ${BLD}%s${RST}" "${PATTERNS[@]}" >&2
-        cprintf ".\n\n" >&2
+        color printf "${RED}error:${RST} No tests matched" >&2
+        color printf " ${BLD}%s${RST}" "${PATTERNS[@]}" >&2
+        color printf ".\n\n" >&2
         usage >&2
         exit 1
     fi
index 70c9cc2c6ed77f88f35dee830a71ac0019405ac9..5fcccad34db7f0a64e7367486c0a12c64165aa71 100644 (file)
@@ -93,7 +93,7 @@ run_tests() {
         else
             ((++failed))
             ((VERBOSE_ERRORS)) || cat "$TMP/$TEST.err" >&2
-            cprintf "${BOL}${RED}%s failed!${RST}\n" "$TEST"
+            color printf "${BOL}${RED}%s failed!${RST}\n" "$TEST"
             ((STOP)) && break
         fi
     done
@@ -101,13 +101,13 @@ run_tests() {
     printf "${BOL}"
 
     if ((passed > 0)); then
-        cprintf "${GRN}tests passed: %d${RST}\n" "$passed"
+        color printf "${GRN}tests passed: %d${RST}\n" "$passed"
     fi
     if ((skipped > 0)); then
-        cprintf "${CYN}tests skipped: %s${RST}\n" "$skipped"
+        color printf "${CYN}tests skipped: %s${RST}\n" "$skipped"
     fi
     if ((failed > 0)); then
-        cprintf "${RED}tests failed: %s${RST}\n" "$failed"
+        color printf "${RED}tests failed: %s${RST}\n" "$failed"
         exit 1
     fi
 }
@@ -126,7 +126,7 @@ skip() {
             debug "$file" $line "${CYN}$TEST skipped!${RST}" "$(awk "NR == $line" "$file")" >&3
         }
     elif ((VERBOSE_TESTS)); then
-        cprintf "${BOL}${CYN}%s skipped!${RST}\n" "$TEST"
+        color printf "${BOL}${CYN}%s skipped!${RST}\n" "$TEST"
     fi
 
     exit $EX_SKIP
@@ -175,39 +175,42 @@ set_acl() {
 }
 
 # Print a bfs invocation for --verbose=commands
-bfs_verbose() (
-    if ((!VERBOSE_COMMANDS)); then
-        return
+bfs_verbose() {
+    if ((VERBOSE_COMMANDS)); then
+        (
+            # Close some fds to make room for the pipe,
+            # even with extremely low ulimit -n
+            exec >&- 4>&-
+            exec >&3 3>&-
+            color bfs_verbose_impl "$@"
+        )
     fi
+}
 
-    # Free up an fd for the pipe
-    exec 4>&-
-
-    {
-        printf "${GRN}%q${RST} " "${BFS[@]}"
-
-        local expr_started=
-        for arg; do
-            if [[ $arg == -[A-Z]* ]]; then
-                printf "${CYN}%q${RST} " "$arg"
-            elif [[ $arg == [\(!] || $arg == -[ao] || $arg == -and || $arg == -or || $arg == -not ]]; then
-                expr_started=yes
-                printf "${RED}%q${RST} " "$arg"
-            elif [[ $expr_started && $arg == [\),] ]]; then
-                printf "${RED}%q${RST} " "$arg"
-            elif [[ $arg == -?* ]]; then
-                expr_started=yes
-                printf "${BLU}%q${RST} " "$arg"
-            elif [ "$expr_started" ]; then
-                printf "${BLD}%q${RST} " "$arg"
-            else
-                printf "${MAG}%q${RST} " "$arg"
-            fi
-        done
+bfs_verbose_impl() {
+    printf "${GRN}%q${RST} " "${BFS[@]}"
+
+    local expr_started=
+    for arg; do
+        if [[ $arg == -[A-Z]* ]]; then
+            printf "${CYN}%q${RST} " "$arg"
+        elif [[ $arg == [\(!] || $arg == -[ao] || $arg == -and || $arg == -or || $arg == -not ]]; then
+            expr_started=yes
+            printf "${RED}%q${RST} " "$arg"
+        elif [[ $expr_started && $arg == [\),] ]]; then
+            printf "${RED}%q${RST} " "$arg"
+        elif [[ $arg == -?* ]]; then
+            expr_started=yes
+            printf "${BLU}%q${RST} " "$arg"
+        elif [ "$expr_started" ]; then
+            printf "${BLD}%q${RST} " "$arg"
+        else
+            printf "${MAG}%q${RST} " "$arg"
+        fi
+    done
 
-        printf '\n'
-    } | color >&3
-)
+    printf '\n'
+}
 
 # Run the bfs we're testing
 invoke_bfs() {
index efc24b02529269894b2ea1b155f5595b002594cf..31a7b6c38db66daa3b92f6fa57b430f59f3dba28 100644 (file)
@@ -76,14 +76,14 @@ drop_root() {
     if command -v capsh &>/dev/null; then
         if capsh --has-p=cap_dac_override &>/dev/null || capsh --has-p=cap_dac_read_search &>/dev/null; then
            if [ -n "${BFS_TRIED_DROP:-}" ]; then
-                color >&2 <<EOF
+                color cat >&2 <<EOF
 ${RED}error:${RST} Failed to drop capabilities.
 EOF
 
                exit 1
            fi
 
-            color >&2 <<EOF
+            color cat >&2 <<EOF
 ${YLW}warning:${RST} Running as ${BLD}$(id -un)${RST} is not recommended.  Dropping ${BLD}cap_dac_override${RST} and
 ${BLD}cap_dac_read_search${RST}.
 
@@ -100,7 +100,7 @@ EOF
            UNLESS=" unless ${GRN}capsh${RST} is installed"
         fi
 
-        color >&2 <<EOF
+        color cat >&2 <<EOF
 ${RED}error:${RST} These tests expect filesystem permissions to be enforced, and therefore
 will not work when run as ${BLD}$(id -un)${RST}${UNLESS}.
 EOF
@@ -122,7 +122,7 @@ callers() {
 debug() {
     local file="${1/#*\/tests\//tests\/}"
     set -- "$file" "${@:2}"
-    cprintf "${BLD}%s:%d:${RST} %s\n    %s\n" "$@"
+    color printf "${BLD}%s:%d:${RST} %s\n    %s\n" "$@"
 }
 
 ## Deferred cleanup