Code

Bisect run: "skip" current commit if script exit code is 125.
[git.git] / t / test-lib.sh
index dee3ad76212ea95c75c8287c8d8c0f312125bd06..714de6e5753e200d8685ef244a428d099a3ae95d 100644 (file)
@@ -11,6 +11,7 @@ TZ=UTC
 export LANG LC_ALL PAGER TZ
 EDITOR=:
 VISUAL=:
+unset GIT_EDITOR
 unset AUTHOR_DATE
 unset AUTHOR_EMAIL
 unset AUTHOR_NAME
@@ -26,6 +27,7 @@ GIT_COMMITTER_EMAIL=committer@example.com
 GIT_COMMITTER_NAME='C O Mitter'
 unset GIT_DIFF_OPTS
 unset GIT_DIR
+unset GIT_WORK_TREE
 unset GIT_EXTERNAL_DIFF
 unset GIT_INDEX_FILE
 unset GIT_OBJECT_DIRECTORY
@@ -57,15 +59,11 @@ esac
 # '
 # . ./test-lib.sh
 
-error () {
-       echo "* error: $*"
-       trap - exit
-       exit 1
-}
-
-say () {
-       echo "* $*"
-}
+[ "x$TERM" != "xdumb" ] &&
+       tput bold >/dev/null 2>&1 &&
+       tput setaf 1 >/dev/null 2>&1 &&
+       tput sgr0 >/dev/null 2>&1 &&
+       color=t
 
 test "${test_description}" != "" ||
 error "Test script did not set test_description."
@@ -82,6 +80,10 @@ do
                exit 0 ;;
        -v|--v|--ve|--ver|--verb|--verbo|--verbos|--verbose)
                verbose=t; shift ;;
+       -q|--q|--qu|--qui|--quie|--quiet)
+               quiet=t; shift ;;
+       --no-color)
+           color=; shift ;;
        --no-python)
                # noop now...
                shift ;;
@@ -90,6 +92,37 @@ do
        esac
 done
 
+if test -n "$color"; then
+       say_color () {
+               case "$1" in
+                       error) tput bold; tput setaf 1;; # bold red
+                       skip)  tput bold; tput setaf 2;; # bold green
+                       pass)  tput setaf 2;;            # green
+                       info)  tput setaf 3;;            # brown
+                       *) test -n "$quiet" && return;;
+               esac
+               shift
+               echo "* $*"
+               tput sgr0
+       }
+else
+       say_color() {
+               test -z "$1" && test -n "$quiet" && return
+               shift
+               echo "* $*"
+       }
+fi
+
+error () {
+       say_color error "error: $*"
+       trap - exit
+       exit 1
+}
+
+say () {
+       say_color info "$*"
+}
+
 exec 5>&1
 if test "$verbose" = "t"
 then
@@ -120,13 +153,13 @@ test_tick () {
 
 test_ok_ () {
        test_count=$(expr "$test_count" + 1)
-       say "  ok $test_count: $@"
+       say_color "" "  ok $test_count: $@"
 }
 
 test_failure_ () {
        test_count=$(expr "$test_count" + 1)
        test_failure=$(expr "$test_failure" + 1);
-       say "FAIL $test_count: $1"
+       say_color error "FAIL $test_count: $1"
        shift
        echo "$@" | sed -e 's/^/        /'
        test "$immediate" = "" || { trap - exit; exit 1; }
@@ -156,9 +189,9 @@ test_skip () {
        done
        case "$to_skip" in
        t)
-               say >&3 "skipping test: $@"
+               say_color skip >&3 "skipping test: $@"
                test_count=$(expr "$test_count" + 1)
-               say "skip $test_count: $1"
+               say_color skip "skip $test_count: $1"
                : true
                ;;
        *)
@@ -232,7 +265,7 @@ test_create_repo () {
        mv .git/hooks .git/hooks-disabled
        cd "$owd"
 }
-       
+
 test_done () {
        trap - exit
        case "$test_failure" in
@@ -245,11 +278,11 @@ test_done () {
                # The Makefile provided will clean this test area so
                # we will leave things as they are.
 
-               say "passed all $test_count test(s)"
+               say_color pass "passed all $test_count test(s)"
                exit 0 ;;
 
        *)
-               say "failed $test_failure among $test_count test(s)"
+               say_color error "failed $test_failure among $test_count test(s)"
                exit 1 ;;
 
        esac
@@ -294,8 +327,8 @@ do
        done
        case "$to_skip" in
        t)
-               say >&3 "skipping test $this_test altogether"
-               say "skip all tests in $this_test"
+               say_color skip >&3 "skipping test $this_test altogether"
+               say_color skip "skip all tests in $this_test"
                test_done
        esac
 done