Code

Merge branch 'ph/rerere-doc' into maint-1.7.8
[git.git] / t / test-lib.sh
index 875d05ed50d0dfd94efd3e23ecb4982fd7ca6b98..bdd9513b84301275330d3dd7e49af05081ef9cd7 100644 (file)
@@ -92,6 +92,10 @@ _x40="$_x05$_x05$_x05$_x05$_x05$_x05$_x05$_x05"
 # Zero SHA-1
 _z40=0000000000000000000000000000000000000000
 
+# Line feed
+LF='
+'
+
 # Each test should start with something like this, after copyright notices:
 #
 # test_description='Description of this test...
@@ -357,6 +361,24 @@ test_chmod () {
        git update-index --add "--chmod=$@"
 }
 
+# Unset a configuration variable, but don't fail if it doesn't exist.
+test_unconfig () {
+       git config --unset-all "$@"
+       config_status=$?
+       case "$config_status" in
+       5) # ok, nothing to unset
+               config_status=0
+               ;;
+       esac
+       return $config_status
+}
+
+# Set git config, automatically unsetting it after the test is over.
+test_config () {
+       test_when_finished "test_unconfig '$1'" &&
+       git config "$@"
+}
+
 # Use test_set_prereq to tell that a particular prerequisite is available.
 # The prerequisite can later be checked for in two ways:
 #
@@ -444,20 +466,26 @@ test_debug () {
        test "$debug" = "" || eval "$1"
 }
 
+test_eval_ () {
+       # This is a separate function because some tests use
+       # "return" to end a test_expect_success block early.
+       eval >&3 2>&4 "$*"
+}
+
 test_run_ () {
        test_cleanup=:
        expecting_failure=$2
-       eval >&3 2>&4 "$1"
+       test_eval_ "$1"
        eval_ret=$?
 
        if test -z "$immediate" || test $eval_ret = 0 || test -n "$expecting_failure"
        then
-               eval >&3 2>&4 "$test_cleanup"
+               test_eval_ "$test_cleanup"
        fi
        if test "$verbose" = "t" && test -n "$HARNESS_ACTIVE"; then
                echo ""
        fi
-       return 0
+       return "$eval_ret"
 }
 
 test_skip () {
@@ -502,8 +530,7 @@ test_expect_failure () {
        if ! test_skip "$@"
        then
                say >&3 "checking known breakage: $2"
-               test_run_ "$2" expecting_failure
-               if [ "$?" = 0 -a "$eval_ret" = 0 ]
+               if test_run_ "$2" expecting_failure
                then
                        test_known_broken_ok_ "$1"
                else
@@ -521,8 +548,7 @@ test_expect_success () {
        if ! test_skip "$@"
        then
                say >&3 "expecting success: $2"
-               test_run_ "$2"
-               if [ "$?" = 0 -a "$eval_ret" = 0 ]
+               if test_run_ "$2"
                then
                        test_ok_ "$1"
                else
@@ -583,7 +609,7 @@ test_external () {
 test_external_without_stderr () {
        # The temporary file has no (and must have no) security
        # implications.
-       tmp="$TMPDIR"; if [ -z "$tmp" ]; then tmp=/tmp; fi
+       tmp=${TMPDIR:-/tmp}
        stderr="$tmp/git-external-stderr.$$.tmp"
        test_external "$@" 4> "$stderr"
        [ -f "$stderr" ] || error "Internal error: $stderr disappeared."
@@ -736,12 +762,11 @@ test_expect_code () {
        exit_code=$?
        if test $exit_code = $want_code
        then
-               echo >&2 "test_expect_code: command exited with $exit_code: $*"
                return 0
-       else
-               echo >&2 "test_expect_code: command exited with $exit_code, we wanted $want_code $*"
-               return 1
        fi
+
+       echo >&2 "test_expect_code: command exited with $exit_code, we wanted $want_code $*"
+       return 1
 }
 
 # test_cmp is a helper function to compare actual and expected output.
@@ -812,12 +837,14 @@ test_done () {
                mkdir -p "$test_results_dir"
                test_results_path="$test_results_dir/${0%.sh}-$$.counts"
 
-               echo "total $test_count" >> $test_results_path
-               echo "success $test_success" >> $test_results_path
-               echo "fixed $test_fixed" >> $test_results_path
-               echo "broken $test_broken" >> $test_results_path
-               echo "failed $test_failure" >> $test_results_path
-               echo "" >> $test_results_path
+               cat >>"$test_results_path" <<-EOF
+               total $test_count
+               success $test_success
+               fixed $test_fixed
+               broken $test_broken
+               failed $test_failure
+
+               EOF
        fi
 
        if test "$test_fixed" != 0
@@ -891,8 +918,13 @@ then
        }
 
        make_valgrind_symlink () {
-               # handle only executables
-               test -x "$1" || return
+               # handle only executables, unless they are shell libraries that
+               # need to be in the exec-path.  We will just use "#!" as a
+               # guess for a shell-script, since we have no idea what the user
+               # may have configured as the shell path.
+               test -x "$1" ||
+               test "#!" = "$(head -c 2 <"$1")" ||
+               return;
 
                base=$(basename "$1")
                symlink_target=$GIT_BUILD_DIR/$base
@@ -918,6 +950,8 @@ then
        do
                make_valgrind_symlink $file
        done
+       # special-case the mergetools loadables
+       make_symlink "$GIT_BUILD_DIR"/mergetools "$GIT_VALGRIND/bin/mergetools"
        OLDIFS=$IFS
        IFS=:
        for path in $PATH
@@ -1078,6 +1112,7 @@ esac
 
 test -z "$NO_PERL" && test_set_prereq PERL
 test -z "$NO_PYTHON" && test_set_prereq PYTHON
+test -n "$USE_LIBPCRE" && test_set_prereq LIBPCRE
 
 # Can we rely on git's output in the C locale?
 if test -n "$GETTEXT_POISON"