diff: diff.context configuration gives default to -U
[gitweb.git] / t / test-lib.sh
index acda33d177197c4288e8b7a33385f11c63c44698..bff3d7570c675c7b6fbc71342909b0a930aae1a0 100644 (file)
@@ -34,6 +34,35 @@ esac
 # Keep the original TERM for say_color
 ORIGINAL_TERM=$TERM
 
+# Test the binaries we have just built.  The tests are kept in
+# t/ subdirectory and are run in 'trash directory' subdirectory.
+if test -z "$TEST_DIRECTORY"
+then
+       # We allow tests to override this, in case they want to run tests
+       # outside of t/, e.g. for running tests on the test library
+       # itself.
+       TEST_DIRECTORY=$(pwd)
+fi
+if test -z "$TEST_OUTPUT_DIRECTORY"
+then
+       # Similarly, override this to store the test-results subdir
+       # elsewhere
+       TEST_OUTPUT_DIRECTORY=$TEST_DIRECTORY
+fi
+GIT_BUILD_DIR="$TEST_DIRECTORY"/..
+
+################################################################
+# It appears that people try to run tests without building...
+"$GIT_BUILD_DIR/git" >/dev/null
+if test $? != 1
+then
+       echo >&2 'error: you do not seem to have built git yet.'
+       exit 1
+fi
+
+. "$GIT_BUILD_DIR"/GIT-BUILD-OPTIONS
+export PERL_PATH SHELL_PATH
+
 # For repeatability, reset the environment to known value.
 LANG=C
 LC_ALL=C
@@ -46,7 +75,7 @@ EDITOR=:
 # /usr/xpg4/bin/sh and /bin/ksh to bail out.  So keep the unsets
 # deriving from the command substitution clustered with the other
 # ones.
-unset VISUAL EMAIL LANGUAGE COLUMNS $(perl -e '
+unset VISUAL EMAIL LANGUAGE COLUMNS $("$PERL_PATH" -e '
        my @env = keys %ENV;
        my $ok = join("|", qw(
                TRACE
@@ -61,6 +90,7 @@ unset VISUAL EMAIL LANGUAGE COLUMNS $(perl -e '
        my @vars = grep(/^GIT_/ && !/^GIT_($ok)/o, @env);
        print join("\n", @vars);
 ')
+unset XDG_CONFIG_HOME
 GIT_AUTHOR_EMAIL=author@example.com
 GIT_AUTHOR_NAME='A U Thor'
 GIT_COMMITTER_EMAIL=committer@example.com
@@ -72,6 +102,27 @@ export GIT_AUTHOR_EMAIL GIT_AUTHOR_NAME
 export GIT_COMMITTER_EMAIL GIT_COMMITTER_NAME
 export EDITOR
 
+# Add libc MALLOC and MALLOC_PERTURB test
+# only if we are not executing the test with valgrind
+if expr " $GIT_TEST_OPTS " : ".* --valgrind " >/dev/null ||
+   test -n "TEST_NO_MALLOC_"
+then
+       setup_malloc_check () {
+               : nothing
+       }
+       teardown_malloc_check () {
+               : nothing
+       }
+else
+       setup_malloc_check () {
+               MALLOC_CHECK_=3 MALLOC_PERTURB_=165
+               export MALLOC_CHECK_ MALLOC_PERTURB_
+       }
+       teardown_malloc_check () {
+               unset MALLOC_CHECK_ MALLOC_PERTURB_
+       }
+fi
+
 # Protect ourselves from common misconfiguration to export
 # CDPATH into the environment
 unset CDPATH
@@ -79,12 +130,12 @@ unset CDPATH
 unset GREP_OPTIONS
 
 case $(echo $GIT_TRACE |tr "[A-Z]" "[a-z]") in
-       1|2|true)
-               echo "* warning: Some tests will not work if GIT_TRACE" \
-                       "is set as to trace on STDERR ! *"
-               echo "* warning: Please set GIT_TRACE to something" \
-                       "other than 1, 2 or true ! *"
-               ;;
+1|2|true)
+       echo "* warning: Some tests will not work if GIT_TRACE" \
+               "is set as to trace on STDERR ! *"
+       echo "* warning: Please set GIT_TRACE to something" \
+               "other than 1, 2 or true ! *"
+       ;;
 esac
 
 # Convenience
@@ -151,17 +202,23 @@ do
        esac
 done
 
-if test -n "$color"; then
+if test -n "$color"
+then
        say_color () {
                (
                TERM=$ORIGINAL_TERM
                export TERM
                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;;
+               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
                printf "%s" "$*"
@@ -229,7 +286,7 @@ trap 'die' EXIT
 
 # The user-facing functions are loaded from a separate file so that
 # test_perf subshells can have them too
-. "${TEST_DIRECTORY:-.}"/test-lib-functions.sh
+. "$TEST_DIRECTORY/test-lib-functions.sh"
 
 # You are not expected to call test_ok_ and test_failure_ directly, use
 # the text_expect_* functions instead.
@@ -275,9 +332,12 @@ test_run_ () {
 
        if test -z "$immediate" || test $eval_ret = 0 || test -n "$expecting_failure"
        then
+               setup_malloc_check
                test_eval_ "$test_cleanup"
+               teardown_malloc_check
        fi
-       if test "$verbose" = "t" && test -n "$HARNESS_ACTIVE"; then
+       if test "$verbose" = "t" && test -n "$HARNESS_ACTIVE"
+       then
                echo ""
        fi
        return "$eval_ret"
@@ -325,7 +385,8 @@ test_at_end_hook_ () {
 test_done () {
        GIT_EXIT_OK=t
 
-       if test -z "$HARNESS_ACTIVE"; then
+       if test -z "$HARNESS_ACTIVE"
+       then
                test_results_dir="$TEST_OUTPUT_DIRECTORY/test-results"
                mkdir -p "$test_results_dir"
                test_results_path="$test_results_dir/${0%.sh}-$$.counts"
@@ -354,10 +415,18 @@ test_done () {
        case "$test_failure" in
        0)
                # Maybe print SKIP message
+               if test -n "$skip_all" && test $test_count -gt 0
+               then
+                       error "Can't use skip_all after running some tests"
+               fi
                [ -z "$skip_all" ] || skip_all=" # SKIP $skip_all"
 
-               if test $test_external_has_tap -eq 0; then
-                       say_color pass "# passed all $msg"
+               if test $test_external_has_tap -eq 0
+               then
+                       if test $test_count -gt 0
+                       then
+                               say_color pass "# passed all $msg"
+                       fi
                        say "1..$test_count$skip_all"
                fi
 
@@ -370,7 +439,8 @@ test_done () {
                exit 0 ;;
 
        *)
-               if test $test_external_has_tap -eq 0; then
+               if test $test_external_has_tap -eq 0
+               then
                        say_color error "# failed $test_failure among $msg"
                        say "1..$test_count"
                fi
@@ -380,23 +450,6 @@ test_done () {
        esac
 }
 
-# Test the binaries we have just built.  The tests are kept in
-# t/ subdirectory and are run in 'trash directory' subdirectory.
-if test -z "$TEST_DIRECTORY"
-then
-       # We allow tests to override this, in case they want to run tests
-       # outside of t/, e.g. for running tests on the test library
-       # itself.
-       TEST_DIRECTORY=$(pwd)
-fi
-if test -z "$TEST_OUTPUT_DIRECTORY"
-then
-       # Similarly, override this to store the test-results subdir
-       # elsewhere
-       TEST_OUTPUT_DIRECTORY=$TEST_DIRECTORY
-fi
-GIT_BUILD_DIR="$TEST_DIRECTORY"/..
-
 if test -n "$valgrind"
 then
        make_symlink () {
@@ -467,22 +520,26 @@ then
        PATH=$GIT_VALGRIND/bin:$PATH
        GIT_EXEC_PATH=$GIT_VALGRIND/bin
        export GIT_VALGRIND
-elif test -n "$GIT_TEST_INSTALLED" ; then
+elif test -n "$GIT_TEST_INSTALLED"
+then
        GIT_EXEC_PATH=$($GIT_TEST_INSTALLED/git --exec-path)  ||
        error "Cannot run git from $GIT_TEST_INSTALLED."
        PATH=$GIT_TEST_INSTALLED:$GIT_BUILD_DIR:$PATH
        GIT_EXEC_PATH=${GIT_TEST_EXEC_PATH:-$GIT_EXEC_PATH}
 else # normal case, use ../bin-wrappers only unless $with_dashes:
        git_bin_dir="$GIT_BUILD_DIR/bin-wrappers"
-       if ! test -x "$git_bin_dir/git" ; then
-               if test -z "$with_dashes" ; then
+       if ! test -x "$git_bin_dir/git"
+       then
+               if test -z "$with_dashes"
+               then
                        say "$git_bin_dir/git is not executable; using GIT_EXEC_PATH"
                fi
                with_dashes=t
        fi
        PATH="$git_bin_dir:$PATH"
        GIT_EXEC_PATH=$GIT_BUILD_DIR
-       if test -n "$with_dashes" ; then
+       if test -n "$with_dashes"
+       then
                PATH="$GIT_BUILD_DIR:$PATH"
        fi
 fi
@@ -492,10 +549,6 @@ GIT_CONFIG_NOSYSTEM=1
 GIT_ATTR_NOSYSTEM=1
 export PATH GIT_EXEC_PATH GIT_TEMPLATE_DIR GIT_CONFIG_NOSYSTEM GIT_ATTR_NOSYSTEM
 
-. "$GIT_BUILD_DIR"/GIT-BUILD-OPTIONS
-
-export PERL_PATH
-
 if test -z "$GIT_TEST_CMP"
 then
        if test -n "$GIT_TEST_CMP_USE_COPIED_CONTEXT"
@@ -521,7 +574,8 @@ then
        }
 fi
 
-if ! test -x "$GIT_BUILD_DIR"/test-chmtime; then
+if ! test -x "$GIT_BUILD_DIR"/test-chmtime
+then
        echo >&2 'You need to build test-chmtime:'
        echo >&2 'Run "make test-chmtime" in the source (toplevel) directory'
        exit 1
@@ -544,7 +598,8 @@ rm -fr "$test" || {
 HOME="$TRASH_DIRECTORY"
 export HOME
 
-if test -z "$TEST_NO_CREATE_REPO"; then
+if test -z "$TEST_NO_CREATE_REPO"
+then
        test_create_repo "$test"
 else
        mkdir -p "$test"
@@ -659,9 +714,29 @@ test_i18ngrep () {
        fi
 }
 
-# test whether the filesystem supports symbolic links
-ln -s x y 2>/dev/null && test -h y 2>/dev/null && test_set_prereq SYMLINKS
-rm -f y
+test_lazy_prereq SYMLINKS '
+       # test whether the filesystem supports symbolic links
+       ln -s x y && test -h y
+'
+
+test_lazy_prereq CASE_INSENSITIVE_FS '
+       echo good >CamelCase &&
+       echo bad >camelcase &&
+       test "$(cat CamelCase)" != good
+'
+
+test_lazy_prereq UTF8_NFD_TO_NFC '
+       # check whether FS converts nfd unicode to nfc
+       auml=$(printf "\303\244")
+       aumlcdiar=$(printf "\141\314\210")
+       >"$auml" &&
+       case "$(echo *)" in
+       "$aumlcdiar")
+               true ;;
+       *)
+               false ;;
+       esac
+'
 
 # When the tests are run as root, permission tests will report that
 # things are writable when they shouldn't be.