Skip to content

Commit dc27155

Browse files
committed
Merge branch 'ph/color-test'
* ph/color-test: Support a --quiet option in the test-suite. Add some fancy colors in the test library when terminal supports it.
2 parents 4a21d13 + 1ece127 commit dc27155

File tree

1 file changed

+48
-17
lines changed

1 file changed

+48
-17
lines changed

t/test-lib.sh

Lines changed: 48 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -59,15 +59,11 @@ esac
5959
# '
6060
# . ./test-lib.sh
6161

62-
error () {
63-
echo "* error: $*"
64-
trap - exit
65-
exit 1
66-
}
67-
68-
say () {
69-
echo "* $*"
70-
}
62+
[ "x$TERM" != "xdumb" ] &&
63+
tput bold >/dev/null 2>&1 &&
64+
tput setaf 1 >/dev/null 2>&1 &&
65+
tput sgr0 >/dev/null 2>&1 &&
66+
color=t
7167

7268
test "${test_description}" != "" ||
7369
error "Test script did not set test_description."
@@ -84,6 +80,10 @@ do
8480
exit 0 ;;
8581
-v|--v|--ve|--ver|--verb|--verbo|--verbos|--verbose)
8682
verbose=t; shift ;;
83+
-q|--q|--qu|--qui|--quie|--quiet)
84+
quiet=t; shift ;;
85+
--no-color)
86+
color=; shift ;;
8787
--no-python)
8888
# noop now...
8989
shift ;;
@@ -92,6 +92,37 @@ do
9292
esac
9393
done
9494

95+
if test -n "$color"; then
96+
say_color () {
97+
case "$1" in
98+
error) tput bold; tput setaf 1;; # bold red
99+
skip) tput bold; tput setaf 2;; # bold green
100+
pass) tput setaf 2;; # green
101+
info) tput setaf 3;; # brown
102+
*) test -n "$quiet" && return;;
103+
esac
104+
shift
105+
echo "* $*"
106+
tput sgr0
107+
}
108+
else
109+
say_color() {
110+
test -z "$1" && test -n "$quiet" && return
111+
shift
112+
echo "* $*"
113+
}
114+
fi
115+
116+
error () {
117+
say_color error "error: $*"
118+
trap - exit
119+
exit 1
120+
}
121+
122+
say () {
123+
say_color info "$*"
124+
}
125+
95126
exec 5>&1
96127
if test "$verbose" = "t"
97128
then
@@ -122,13 +153,13 @@ test_tick () {
122153

123154
test_ok_ () {
124155
test_count=$(expr "$test_count" + 1)
125-
say " ok $test_count: $@"
156+
say_color "" " ok $test_count: $@"
126157
}
127158

128159
test_failure_ () {
129160
test_count=$(expr "$test_count" + 1)
130161
test_failure=$(expr "$test_failure" + 1);
131-
say "FAIL $test_count: $1"
162+
say_color error "FAIL $test_count: $1"
132163
shift
133164
echo "$@" | sed -e 's/^/ /'
134165
test "$immediate" = "" || { trap - exit; exit 1; }
@@ -158,9 +189,9 @@ test_skip () {
158189
done
159190
case "$to_skip" in
160191
t)
161-
say >&3 "skipping test: $@"
192+
say_color skip >&3 "skipping test: $@"
162193
test_count=$(expr "$test_count" + 1)
163-
say "skip $test_count: $1"
194+
say_color skip "skip $test_count: $1"
164195
: true
165196
;;
166197
*)
@@ -247,11 +278,11 @@ test_done () {
247278
# The Makefile provided will clean this test area so
248279
# we will leave things as they are.
249280

250-
say "passed all $test_count test(s)"
281+
say_color pass "passed all $test_count test(s)"
251282
exit 0 ;;
252283

253284
*)
254-
say "failed $test_failure among $test_count test(s)"
285+
say_color error "failed $test_failure among $test_count test(s)"
255286
exit 1 ;;
256287

257288
esac
@@ -296,8 +327,8 @@ do
296327
done
297328
case "$to_skip" in
298329
t)
299-
say >&3 "skipping test $this_test altogether"
300-
say "skip all tests in $this_test"
330+
say_color skip >&3 "skipping test $this_test altogether"
331+
say_color skip "skip all tests in $this_test"
301332
test_done
302333
esac
303334
done

0 commit comments

Comments
 (0)