Skip to content

Commit 55db1df

Browse files
MadCodergitster
authored andcommitted
Add some fancy colors in the test library when terminal supports it.
Signed-off-by: Pierre Habouzit <madcoder@debian.org> Signed-off-by: Junio C Hamano <gitster@pobox.com>
1 parent d90a7fd commit 55db1df

File tree

1 file changed

+45
-17
lines changed

1 file changed

+45
-17
lines changed

t/test-lib.sh

Lines changed: 45 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,8 @@ do
8480
exit 0 ;;
8581
-v|--v|--ve|--ver|--verb|--verbo|--verbos|--verbose)
8682
verbose=t; shift ;;
83+
--no-color)
84+
color=; shift ;;
8785
--no-python)
8886
# noop now...
8987
shift ;;
@@ -92,6 +90,36 @@ do
9290
esac
9391
done
9492

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

123151
test_ok_ () {
124152
test_count=$(expr "$test_count" + 1)
125-
say " ok $test_count: $@"
153+
say_color "" " ok $test_count: $@"
126154
}
127155

128156
test_failure_ () {
129157
test_count=$(expr "$test_count" + 1)
130158
test_failure=$(expr "$test_failure" + 1);
131-
say "FAIL $test_count: $1"
159+
say_color error "FAIL $test_count: $1"
132160
shift
133161
echo "$@" | sed -e 's/^/ /'
134162
test "$immediate" = "" || { trap - exit; exit 1; }
@@ -158,9 +186,9 @@ test_skip () {
158186
done
159187
case "$to_skip" in
160188
t)
161-
say >&3 "skipping test: $@"
189+
say_color skip >&3 "skipping test: $@"
162190
test_count=$(expr "$test_count" + 1)
163-
say "skip $test_count: $1"
191+
say_color skip "skip $test_count: $1"
164192
: true
165193
;;
166194
*)
@@ -247,11 +275,11 @@ test_done () {
247275
# The Makefile provided will clean this test area so
248276
# we will leave things as they are.
249277

250-
say "passed all $test_count test(s)"
278+
say_color pass "passed all $test_count test(s)"
251279
exit 0 ;;
252280

253281
*)
254-
say "failed $test_failure among $test_count test(s)"
282+
say_color error "failed $test_failure among $test_count test(s)"
255283
exit 1 ;;
256284

257285
esac
@@ -296,8 +324,8 @@ do
296324
done
297325
case "$to_skip" in
298326
t)
299-
say >&3 "skipping test $this_test altogether"
300-
say "skip all tests in $this_test"
327+
say_color skip >&3 "skipping test $this_test altogether"
328+
say_color skip "skip all tests in $this_test"
301329
test_done
302330
esac
303331
done

0 commit comments

Comments
 (0)