@matheusbsbs / matheus-bagliano / commits / 55db1df0c8
Fork 0

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>

Pierre Habouzit committed Oct 24, 2007 at 22:03 UTC 55db1df0c86b0fded61731647a8f1cd6e7dc9b04
1 file changed +45 -17
t/test-lib.sh
+45 -17
@@ -59,15 +59,11 @@ esac
59 # '
60 # . ./test-lib.sh
61
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
67
68 test "${test_description}" != "" ||
69 error "Test script did not set test_description."
@@ -84,6 +80,8 @@ do
80 exit 0 ;;
81 -v|--v|--ve|--ver|--verb|--verbo|--verbos|--verbose)
82 verbose=t; shift ;;
83 + --no-color)
84 + color=; shift ;;
85 --no-python)
86 # noop now...
87 shift ;;
@@ -92,6 +90,36 @@ do
90 esac
91 done
92
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 +
123 exec 5>&1
124 if test "$verbose" = "t"
125 then
@@ -122,13 +150,13 @@ test_tick () {
150
151 test_ok_ () {
152 test_count=$(expr "$test_count" + 1)
125 - say " ok $test_count: $@"
153 + say_color "" " ok $test_count: $@"
154 }
155
156 test_failure_ () {
157 test_count=$(expr "$test_count" + 1)
158 test_failure=$(expr "$test_failure" + 1);
131 - say "FAIL $test_count: $1"
159 + say_color error "FAIL $test_count: $1"
160 shift
161 echo "$@" | sed -e 's/^/ /'
162 test "$immediate" = "" || { trap - exit; exit 1; }
@@ -158,9 +186,9 @@ test_skip () {
186 done
187 case "$to_skip" in
188 t)
161 - say >&3 "skipping test: $@"
189 + say_color skip >&3 "skipping test: $@"
190 test_count=$(expr "$test_count" + 1)
163 - say "skip $test_count: $1"
191 + say_color skip "skip $test_count: $1"
192 : true
193 ;;
194 *)
@@ -247,11 +275,11 @@ test_done () {
275 # The Makefile provided will clean this test area so
276 # we will leave things as they are.
277
250 - say "passed all $test_count test(s)"
278 + say_color pass "passed all $test_count test(s)"
279 exit 0 ;;
280
281 *)
254 - say "failed $test_failure among $test_count test(s)"
282 + say_color error "failed $test_failure among $test_count test(s)"
283 exit 1 ;;
284
285 esac
@@ -296,8 +324,8 @@ do
324 done
325 case "$to_skip" in
326 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"
329 test_done
330 esac
331 done