From 6b882a905af7baf75afedc6881448716343b4f4d Mon Sep 17 00:00:00 2001 From: Mateusz Majewski Date: Thu, 16 Jan 2020 08:02:14 +0100 Subject: [PATCH] Add colorful output integration tests Change-Id: I40fa224bf367908a3850875d25c2892e9e09ef0e --- tests/dlog_test.in | 51 +++++++++++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 51 insertions(+) diff --git a/tests/dlog_test.in b/tests/dlog_test.in index 0b9994e..3448015 100644 --- a/tests/dlog_test.in +++ b/tests/dlog_test.in @@ -368,6 +368,57 @@ regex_pidtid="P[0-9[:space:]]{5,},\s{1}T[0-9[:space:]]{5,}" regex_time="[0-9]{2}-[0-9]{2}\s{1}[0-9]{2}:[0-9]{2}:[0-9]{2}" regex_timezone="[\+-]{1}[0-9]{4}" +for PARAM in always auto none never; do + for PRIO in info error; do + for OUTPUT in pipe tty; do + if [[ "$PARAM" == "always" ]]; then + COLOR_EXPECTED=1 + elif [[ "$PARAM" == "never" ]]; then + COLOR_EXPECTED=0 + elif [[ "$OUTPUT" == "tty" ]]; then + COLOR_EXPECTED=1 + else + COLOR_EXPECTED=0 + fi + + if [[ "$PRIO" == "info" ]]; then + REGEX="s/^I\([0-9[:space:]]{5,}\)[[:print:]]*\([[:print:]]*\)@$/1/g" + elif [[ "$COLOR_EXPECTED" -eq 0 ]]; then + REGEX="s/^E\([0-9[:space:]]{5,}\)[[:print:]]*\([[:print:]]*\)@$/1/g" + else + REGEX="s/^~\[31;1mE\([0-9[:space:]]{5,}\) ~\[0m[[:print:]]*~\[31;1m \([[:print:]]*\)@~\[0m$/1/g" + fi + + # -t 1 instead of | head -n 1 because the `script` command can't cope with SIGPIPE. + COMMAND="$cmd_prefix $format -t 1" + + if [[ "$PARAM" == "none" ]]; then + LOG_DETAILS="testing if color output is correct (implicit --color=auto/$PRIO priority/$OUTPUT output)" + else + COMMAND="$COMMAND --color=$PARAM" + LOG_DETAILS="testing if color output is correct (--color=$PARAM/$PRIO priority/$OUTPUT output)" + fi + + if [[ "$PRIO" == "info" ]]; then + COMMAND="$COMMAND '*:=I'" + else + COMMAND="$COMMAND '*:=E'" + fi + + if [[ "$OUTPUT" == "tty" ]]; then + # This emulates a TTY. The sed call is because `script` likes to mess up the output for some reason. + COMMAND="script -qc\"$COMMAND\" /dev/null | sed 's/\r$//'" + fi + + # `eval` is needed since `$COMMAND` may contain `"` + line=`eval $COMMAND | tr '\033\n' '~@'` + [[ `echo "$line" | sed -re "$REGEX"` == "1" ]] && ok || fail + done + done +done +# TODO: It would be cool to also test warning messages (which have a different color), +# but this would require deeper changes to this script, which is already janky enough. + REGEX="s/^$regex_prio\([0-9[:space:]]{5,}\)[[:print:]]*\([[:print:]]*\)$/1/g" LOG_DETAILS="testing if \"$format\" print format works" line=`$cmd_prefix $format | $cmd_postfix` -- 2.7.4