[LTP] [PATCH v4 2/4] tst_ansi_color.sh: Allow to run with set -e

Petr Vorel pvorel@suse.cz
Mon Aug 8 13:37:54 CEST 2022


set -e (or #!/bin/sh -e or set -o errexit) quits on any non-zero exit
code, therefore any && must be turned into || (or if ...; then ..; fi).
Fix hardens tst_res TINFO to be able to be used on scripts with errexit.

Signed-off-by: Petr Vorel <pvorel@suse.cz>
---
changes v2->v3:
* really fix it.

 testcases/lib/tst_ansi_color.sh | 13 +++++++------
 1 file changed, 7 insertions(+), 6 deletions(-)

diff --git a/testcases/lib/tst_ansi_color.sh b/testcases/lib/tst_ansi_color.sh
index 703df1eb8..517b709d0 100644
--- a/testcases/lib/tst_ansi_color.sh
+++ b/testcases/lib/tst_ansi_color.sh
@@ -24,18 +24,19 @@ tst_flag2color()
 
 tst_color_enabled()
 {
-	[ "$LTP_COLORIZE_OUTPUT" = "n" ] || [ "$LTP_COLORIZE_OUTPUT" = "0" ] && return 0
-	[ "$LTP_COLORIZE_OUTPUT" = "y" ] || [ "$LTP_COLORIZE_OUTPUT" = "1" ] && return 1
+	[ "$LTP_COLORIZE_OUTPUT" = "n" -o "$LTP_COLORIZE_OUTPUT" = "0" ] || return 1
+	[ "$LTP_COLORIZE_OUTPUT" = "y" -o "$LTP_COLORIZE_OUTPUT" = "1" ] || return 0
 	[ -t 1 ] || return 0
 	return 1
 }
 
 tst_print_colored()
 {
-	tst_color_enabled
-	local color=$?
+	local color=0
 
-	[ "$color" = "1" ] && tst_flag2color "$1"
+	tst_color_enabled || color=$?
+
+	[ "$color" != 1 ] || tst_flag2color "$1"
 	printf "$2"
-	[ "$color" = "1" ] && printf '\033[0m'
+	[ "$color" != 1 ] || printf '\033[0m'
 }
-- 
2.37.1



More information about the ltp mailing list