t / test-lib.shon commit Merge branch 'jk/ident-ai-canonname-could-be-null' into maint (11738dd)
   1# Test framework for git.  See t/README for usage.
   2#
   3# Copyright (c) 2005 Junio C Hamano
   4#
   5# This program is free software: you can redistribute it and/or modify
   6# it under the terms of the GNU General Public License as published by
   7# the Free Software Foundation, either version 2 of the License, or
   8# (at your option) any later version.
   9#
  10# This program is distributed in the hope that it will be useful,
  11# but WITHOUT ANY WARRANTY; without even the implied warranty of
  12# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
  13# GNU General Public License for more details.
  14#
  15# You should have received a copy of the GNU General Public License
  16# along with this program.  If not, see http://www.gnu.org/licenses/ .
  17
  18# Test the binaries we have just built.  The tests are kept in
  19# t/ subdirectory and are run in 'trash directory' subdirectory.
  20if test -z "$TEST_DIRECTORY"
  21then
  22        # We allow tests to override this, in case they want to run tests
  23        # outside of t/, e.g. for running tests on the test library
  24        # itself.
  25        TEST_DIRECTORY=$(pwd)
  26else
  27        # ensure that TEST_DIRECTORY is an absolute path so that it
  28        # is valid even if the current working directory is changed
  29        TEST_DIRECTORY=$(cd "$TEST_DIRECTORY" && pwd) || exit 1
  30fi
  31if test -z "$TEST_OUTPUT_DIRECTORY"
  32then
  33        # Similarly, override this to store the test-results subdir
  34        # elsewhere
  35        TEST_OUTPUT_DIRECTORY=$TEST_DIRECTORY
  36fi
  37GIT_BUILD_DIR="$TEST_DIRECTORY"/..
  38
  39################################################################
  40# It appears that people try to run tests without building...
  41"$GIT_BUILD_DIR/git" >/dev/null
  42if test $? != 1
  43then
  44        echo >&2 'error: you do not seem to have built git yet.'
  45        exit 1
  46fi
  47
  48. "$GIT_BUILD_DIR"/GIT-BUILD-OPTIONS
  49export PERL_PATH SHELL_PATH
  50
  51# if --tee was passed, write the output not only to the terminal, but
  52# additionally to the file test-results/$BASENAME.out, too.
  53case "$GIT_TEST_TEE_STARTED, $* " in
  54done,*)
  55        # do not redirect again
  56        ;;
  57*' --tee '*|*' --va'*)
  58        mkdir -p "$TEST_OUTPUT_DIRECTORY/test-results"
  59        BASE="$TEST_OUTPUT_DIRECTORY/test-results/$(basename "$0" .sh)"
  60        (GIT_TEST_TEE_STARTED=done ${SHELL_PATH} "$0" "$@" 2>&1;
  61         echo $? > $BASE.exit) | tee $BASE.out
  62        test "$(cat $BASE.exit)" = 0
  63        exit
  64        ;;
  65esac
  66
  67# For repeatability, reset the environment to known value.
  68# TERM is sanitized below, after saving color control sequences.
  69LANG=C
  70LC_ALL=C
  71PAGER=cat
  72TZ=UTC
  73export LANG LC_ALL PAGER TZ
  74EDITOR=:
  75# A call to "unset" with no arguments causes at least Solaris 10
  76# /usr/xpg4/bin/sh and /bin/ksh to bail out.  So keep the unsets
  77# deriving from the command substitution clustered with the other
  78# ones.
  79unset VISUAL EMAIL LANGUAGE COLUMNS $("$PERL_PATH" -e '
  80        my @env = keys %ENV;
  81        my $ok = join("|", qw(
  82                TRACE
  83                DEBUG
  84                USE_LOOKUP
  85                TEST
  86                .*_TEST
  87                PROVE
  88                VALGRIND
  89                UNZIP
  90                PERF_
  91                CURL_VERBOSE
  92                TRACE_CURL
  93        ));
  94        my @vars = grep(/^GIT_/ && !/^GIT_($ok)/o, @env);
  95        print join("\n", @vars);
  96')
  97unset XDG_CONFIG_HOME
  98unset GITPERLLIB
  99GIT_AUTHOR_EMAIL=author@example.com
 100GIT_AUTHOR_NAME='A U Thor'
 101GIT_COMMITTER_EMAIL=committer@example.com
 102GIT_COMMITTER_NAME='C O Mitter'
 103GIT_MERGE_VERBOSITY=5
 104GIT_MERGE_AUTOEDIT=no
 105export GIT_MERGE_VERBOSITY GIT_MERGE_AUTOEDIT
 106export GIT_AUTHOR_EMAIL GIT_AUTHOR_NAME
 107export GIT_COMMITTER_EMAIL GIT_COMMITTER_NAME
 108export EDITOR
 109
 110# Tests using GIT_TRACE typically don't want <timestamp> <file>:<line> output
 111GIT_TRACE_BARE=1
 112export GIT_TRACE_BARE
 113
 114if test -n "${TEST_GIT_INDEX_VERSION:+isset}"
 115then
 116        GIT_INDEX_VERSION="$TEST_GIT_INDEX_VERSION"
 117        export GIT_INDEX_VERSION
 118fi
 119
 120# Add libc MALLOC and MALLOC_PERTURB test
 121# only if we are not executing the test with valgrind
 122if expr " $GIT_TEST_OPTS " : ".* --valgrind " >/dev/null ||
 123   test -n "$TEST_NO_MALLOC_CHECK"
 124then
 125        setup_malloc_check () {
 126                : nothing
 127        }
 128        teardown_malloc_check () {
 129                : nothing
 130        }
 131else
 132        setup_malloc_check () {
 133                MALLOC_CHECK_=3 MALLOC_PERTURB_=165
 134                export MALLOC_CHECK_ MALLOC_PERTURB_
 135        }
 136        teardown_malloc_check () {
 137                unset MALLOC_CHECK_ MALLOC_PERTURB_
 138        }
 139fi
 140
 141: ${ASAN_OPTIONS=detect_leaks=0}
 142export ASAN_OPTIONS
 143
 144# Protect ourselves from common misconfiguration to export
 145# CDPATH into the environment
 146unset CDPATH
 147
 148unset GREP_OPTIONS
 149unset UNZIP
 150
 151case $(echo $GIT_TRACE |tr "[A-Z]" "[a-z]") in
 1521|2|true)
 153        GIT_TRACE=4
 154        ;;
 155esac
 156
 157# Convenience
 158#
 159# A regexp to match 5 and 40 hexdigits
 160_x05='[0-9a-f][0-9a-f][0-9a-f][0-9a-f][0-9a-f]'
 161_x40="$_x05$_x05$_x05$_x05$_x05$_x05$_x05$_x05"
 162
 163# Zero SHA-1
 164_z40=0000000000000000000000000000000000000000
 165
 166EMPTY_TREE=4b825dc642cb6eb9a060e54bf8d69288fbee4904
 167EMPTY_BLOB=e69de29bb2d1d6434b8b29ae775ad8c2e48c5391
 168
 169# Line feed
 170LF='
 171'
 172
 173# UTF-8 ZERO WIDTH NON-JOINER, which HFS+ ignores
 174# when case-folding filenames
 175u200c=$(printf '\342\200\214')
 176
 177export _x05 _x40 _z40 LF u200c EMPTY_TREE EMPTY_BLOB
 178
 179# Each test should start with something like this, after copyright notices:
 180#
 181# test_description='Description of this test...
 182# This test checks if command xyzzy does the right thing...
 183# '
 184# . ./test-lib.sh
 185test "x$TERM" != "xdumb" && (
 186                test -t 1 &&
 187                tput bold >/dev/null 2>&1 &&
 188                tput setaf 1 >/dev/null 2>&1 &&
 189                tput sgr0 >/dev/null 2>&1
 190        ) &&
 191        color=t
 192
 193while test "$#" -ne 0
 194do
 195        case "$1" in
 196        -d|--d|--de|--deb|--debu|--debug)
 197                debug=t; shift ;;
 198        -i|--i|--im|--imm|--imme|--immed|--immedi|--immedia|--immediat|--immediate)
 199                immediate=t; shift ;;
 200        -l|--l|--lo|--lon|--long|--long-|--long-t|--long-te|--long-tes|--long-test|--long-tests)
 201                GIT_TEST_LONG=t; export GIT_TEST_LONG; shift ;;
 202        -r)
 203                shift; test "$#" -ne 0 || {
 204                        echo 'error: -r requires an argument' >&2;
 205                        exit 1;
 206                }
 207                run_list=$1; shift ;;
 208        --run=*)
 209                run_list=${1#--*=}; shift ;;
 210        -h|--h|--he|--hel|--help)
 211                help=t; shift ;;
 212        -v|--v|--ve|--ver|--verb|--verbo|--verbos|--verbose)
 213                verbose=t; shift ;;
 214        --verbose-only=*)
 215                verbose_only=${1#--*=}
 216                shift ;;
 217        -q|--q|--qu|--qui|--quie|--quiet)
 218                # Ignore --quiet under a TAP::Harness. Saying how many tests
 219                # passed without the ok/not ok details is always an error.
 220                test -z "$HARNESS_ACTIVE" && quiet=t; shift ;;
 221        --with-dashes)
 222                with_dashes=t; shift ;;
 223        --no-color)
 224                color=; shift ;;
 225        --va|--val|--valg|--valgr|--valgri|--valgrin|--valgrind)
 226                valgrind=memcheck
 227                shift ;;
 228        --valgrind=*)
 229                valgrind=${1#--*=}
 230                shift ;;
 231        --valgrind-only=*)
 232                valgrind_only=${1#--*=}
 233                shift ;;
 234        --tee)
 235                shift ;; # was handled already
 236        --root=*)
 237                root=${1#--*=}
 238                shift ;;
 239        --chain-lint)
 240                GIT_TEST_CHAIN_LINT=1
 241                shift ;;
 242        --no-chain-lint)
 243                GIT_TEST_CHAIN_LINT=0
 244                shift ;;
 245        -x)
 246                trace=t
 247                verbose=t
 248                shift ;;
 249        *)
 250                echo "error: unknown test option '$1'" >&2; exit 1 ;;
 251        esac
 252done
 253
 254if test -n "$valgrind_only"
 255then
 256        test -z "$valgrind" && valgrind=memcheck
 257        test -z "$verbose" && verbose_only="$valgrind_only"
 258elif test -n "$valgrind"
 259then
 260        verbose=t
 261fi
 262
 263if test -n "$color"
 264then
 265        # Save the color control sequences now rather than run tput
 266        # each time say_color() is called.  This is done for two
 267        # reasons:
 268        #   * TERM will be changed to dumb
 269        #   * HOME will be changed to a temporary directory and tput
 270        #     might need to read ~/.terminfo from the original HOME
 271        #     directory to get the control sequences
 272        # Note:  This approach assumes the control sequences don't end
 273        # in a newline for any terminal of interest (command
 274        # substitutions strip trailing newlines).  Given that most
 275        # (all?) terminals in common use are related to ECMA-48, this
 276        # shouldn't be a problem.
 277        say_color_error=$(tput bold; tput setaf 1) # bold red
 278        say_color_skip=$(tput setaf 4) # blue
 279        say_color_warn=$(tput setaf 3) # brown/yellow
 280        say_color_pass=$(tput setaf 2) # green
 281        say_color_info=$(tput setaf 6) # cyan
 282        say_color_reset=$(tput sgr0)
 283        say_color_="" # no formatting for normal text
 284        say_color () {
 285                test -z "$1" && test -n "$quiet" && return
 286                eval "say_color_color=\$say_color_$1"
 287                shift
 288                printf "%s\\n" "$say_color_color$*$say_color_reset"
 289        }
 290else
 291        say_color() {
 292                test -z "$1" && test -n "$quiet" && return
 293                shift
 294                printf "%s\n" "$*"
 295        }
 296fi
 297
 298TERM=dumb
 299export TERM
 300
 301error () {
 302        say_color error "error: $*"
 303        GIT_EXIT_OK=t
 304        exit 1
 305}
 306
 307say () {
 308        say_color info "$*"
 309}
 310
 311test "${test_description}" != "" ||
 312error "Test script did not set test_description."
 313
 314if test "$help" = "t"
 315then
 316        printf '%s\n' "$test_description"
 317        exit 0
 318fi
 319
 320exec 5>&1
 321exec 6<&0
 322if test "$verbose" = "t"
 323then
 324        exec 4>&2 3>&1
 325else
 326        exec 4>/dev/null 3>/dev/null
 327fi
 328
 329# Send any "-x" output directly to stderr to avoid polluting tests
 330# which capture stderr. We can do this unconditionally since it
 331# has no effect if tracing isn't turned on.
 332#
 333# Note that this sets up the trace fd as soon as we assign the variable, so it
 334# must come after the creation of descriptor 4 above. Likewise, we must never
 335# unset this, as it has the side effect of closing descriptor 4, which we
 336# use to show verbose tests to the user.
 337#
 338# Note also that we don't need or want to export it. The tracing is local to
 339# this shell, and we would not want to influence any shells we exec.
 340BASH_XTRACEFD=4
 341
 342test_failure=0
 343test_count=0
 344test_fixed=0
 345test_broken=0
 346test_success=0
 347
 348test_external_has_tap=0
 349
 350die () {
 351        code=$?
 352        if test -n "$GIT_EXIT_OK"
 353        then
 354                exit $code
 355        else
 356                echo >&5 "FATAL: Unexpected exit with code $code"
 357                exit 1
 358        fi
 359}
 360
 361GIT_EXIT_OK=
 362trap 'die' EXIT
 363trap 'exit $?' INT
 364
 365# The user-facing functions are loaded from a separate file so that
 366# test_perf subshells can have them too
 367. "$TEST_DIRECTORY/test-lib-functions.sh"
 368
 369# You are not expected to call test_ok_ and test_failure_ directly, use
 370# the test_expect_* functions instead.
 371
 372test_ok_ () {
 373        test_success=$(($test_success + 1))
 374        say_color "" "ok $test_count - $@"
 375}
 376
 377test_failure_ () {
 378        test_failure=$(($test_failure + 1))
 379        say_color error "not ok $test_count - $1"
 380        shift
 381        printf '%s\n' "$*" | sed -e 's/^/#      /'
 382        test "$immediate" = "" || { GIT_EXIT_OK=t; exit 1; }
 383}
 384
 385test_known_broken_ok_ () {
 386        test_fixed=$(($test_fixed+1))
 387        say_color error "ok $test_count - $@ # TODO known breakage vanished"
 388}
 389
 390test_known_broken_failure_ () {
 391        test_broken=$(($test_broken+1))
 392        say_color warn "not ok $test_count - $@ # TODO known breakage"
 393}
 394
 395test_debug () {
 396        test "$debug" = "" || eval "$1"
 397}
 398
 399match_pattern_list () {
 400        arg="$1"
 401        shift
 402        test -z "$*" && return 1
 403        for pattern_
 404        do
 405                case "$arg" in
 406                $pattern_)
 407                        return 0
 408                esac
 409        done
 410        return 1
 411}
 412
 413match_test_selector_list () {
 414        title="$1"
 415        shift
 416        arg="$1"
 417        shift
 418        test -z "$1" && return 0
 419
 420        # Both commas and whitespace are accepted as separators.
 421        OLDIFS=$IFS
 422        IFS='   ,'
 423        set -- $1
 424        IFS=$OLDIFS
 425
 426        # If the first selector is negative we include by default.
 427        include=
 428        case "$1" in
 429                !*) include=t ;;
 430        esac
 431
 432        for selector
 433        do
 434                orig_selector=$selector
 435
 436                positive=t
 437                case "$selector" in
 438                        !*)
 439                                positive=
 440                                selector=${selector##?}
 441                                ;;
 442                esac
 443
 444                test -z "$selector" && continue
 445
 446                case "$selector" in
 447                        *-*)
 448                                if expr "z${selector%%-*}" : "z[0-9]*[^0-9]" >/dev/null
 449                                then
 450                                        echo "error: $title: invalid non-numeric in range" \
 451                                                "start: '$orig_selector'" >&2
 452                                        exit 1
 453                                fi
 454                                if expr "z${selector#*-}" : "z[0-9]*[^0-9]" >/dev/null
 455                                then
 456                                        echo "error: $title: invalid non-numeric in range" \
 457                                                "end: '$orig_selector'" >&2
 458                                        exit 1
 459                                fi
 460                                ;;
 461                        *)
 462                                if expr "z$selector" : "z[0-9]*[^0-9]" >/dev/null
 463                                then
 464                                        echo "error: $title: invalid non-numeric in test" \
 465                                                "selector: '$orig_selector'" >&2
 466                                        exit 1
 467                                fi
 468                esac
 469
 470                # Short cut for "obvious" cases
 471                test -z "$include" && test -z "$positive" && continue
 472                test -n "$include" && test -n "$positive" && continue
 473
 474                case "$selector" in
 475                        -*)
 476                                if test $arg -le ${selector#-}
 477                                then
 478                                        include=$positive
 479                                fi
 480                                ;;
 481                        *-)
 482                                if test $arg -ge ${selector%-}
 483                                then
 484                                        include=$positive
 485                                fi
 486                                ;;
 487                        *-*)
 488                                if test ${selector%%-*} -le $arg \
 489                                        && test $arg -le ${selector#*-}
 490                                then
 491                                        include=$positive
 492                                fi
 493                                ;;
 494                        *)
 495                                if test $arg -eq $selector
 496                                then
 497                                        include=$positive
 498                                fi
 499                                ;;
 500                esac
 501        done
 502
 503        test -n "$include"
 504}
 505
 506maybe_teardown_verbose () {
 507        test -z "$verbose_only" && return
 508        exec 4>/dev/null 3>/dev/null
 509        verbose=
 510}
 511
 512last_verbose=t
 513maybe_setup_verbose () {
 514        test -z "$verbose_only" && return
 515        if match_pattern_list $test_count $verbose_only
 516        then
 517                exec 4>&2 3>&1
 518                # Emit a delimiting blank line when going from
 519                # non-verbose to verbose.  Within verbose mode the
 520                # delimiter is printed by test_expect_*.  The choice
 521                # of the initial $last_verbose is such that before
 522                # test 1, we do not print it.
 523                test -z "$last_verbose" && echo >&3 ""
 524                verbose=t
 525        else
 526                exec 4>/dev/null 3>/dev/null
 527                verbose=
 528        fi
 529        last_verbose=$verbose
 530}
 531
 532maybe_teardown_valgrind () {
 533        test -z "$GIT_VALGRIND" && return
 534        GIT_VALGRIND_ENABLED=
 535}
 536
 537maybe_setup_valgrind () {
 538        test -z "$GIT_VALGRIND" && return
 539        if test -z "$valgrind_only"
 540        then
 541                GIT_VALGRIND_ENABLED=t
 542                return
 543        fi
 544        GIT_VALGRIND_ENABLED=
 545        if match_pattern_list $test_count $valgrind_only
 546        then
 547                GIT_VALGRIND_ENABLED=t
 548        fi
 549}
 550
 551want_trace () {
 552        test "$trace" = t && test "$verbose" = t
 553}
 554
 555# This is a separate function because some tests use
 556# "return" to end a test_expect_success block early
 557# (and we want to make sure we run any cleanup like
 558# "set +x").
 559test_eval_inner_ () {
 560        # Do not add anything extra (including LF) after '$*'
 561        eval "
 562                want_trace && set -x
 563                $*"
 564}
 565
 566test_eval_ () {
 567        # We run this block with stderr redirected to avoid extra cruft
 568        # during a "-x" trace. Once in "set -x" mode, we cannot prevent
 569        # the shell from printing the "set +x" to turn it off (nor the saving
 570        # of $? before that). But we can make sure that the output goes to
 571        # /dev/null.
 572        #
 573        # The test itself is run with stderr put back to &4 (so either to
 574        # /dev/null, or to the original stderr if --verbose was used).
 575        {
 576                test_eval_inner_ "$@" </dev/null >&3 2>&4
 577                test_eval_ret_=$?
 578                if want_trace
 579                then
 580                        set +x
 581                        if test "$test_eval_ret_" != 0
 582                        then
 583                                say_color error >&4 "error: last command exited with \$?=$test_eval_ret_"
 584                        fi
 585                fi
 586        } 2>/dev/null
 587        return $test_eval_ret_
 588}
 589
 590test_run_ () {
 591        test_cleanup=:
 592        expecting_failure=$2
 593
 594        if test "${GIT_TEST_CHAIN_LINT:-1}" != 0; then
 595                # turn off tracing for this test-eval, as it simply creates
 596                # confusing noise in the "-x" output
 597                trace_tmp=$trace
 598                trace=
 599                # 117 is magic because it is unlikely to match the exit
 600                # code of other programs
 601                test_eval_ "(exit 117) && $1"
 602                if test "$?" != 117; then
 603                        error "bug in the test script: broken &&-chain: $1"
 604                fi
 605                trace=$trace_tmp
 606        fi
 607
 608        setup_malloc_check
 609        test_eval_ "$1"
 610        eval_ret=$?
 611        teardown_malloc_check
 612
 613        if test -z "$immediate" || test $eval_ret = 0 ||
 614           test -n "$expecting_failure" && test "$test_cleanup" != ":"
 615        then
 616                setup_malloc_check
 617                test_eval_ "$test_cleanup"
 618                teardown_malloc_check
 619        fi
 620        if test "$verbose" = "t" && test -n "$HARNESS_ACTIVE"
 621        then
 622                echo ""
 623        fi
 624        return "$eval_ret"
 625}
 626
 627test_start_ () {
 628        test_count=$(($test_count+1))
 629        maybe_setup_verbose
 630        maybe_setup_valgrind
 631}
 632
 633test_finish_ () {
 634        echo >&3 ""
 635        maybe_teardown_valgrind
 636        maybe_teardown_verbose
 637}
 638
 639test_skip () {
 640        to_skip=
 641        skipped_reason=
 642        if match_pattern_list $this_test.$test_count $GIT_SKIP_TESTS
 643        then
 644                to_skip=t
 645                skipped_reason="GIT_SKIP_TESTS"
 646        fi
 647        if test -z "$to_skip" && test -n "$test_prereq" &&
 648           ! test_have_prereq "$test_prereq"
 649        then
 650                to_skip=t
 651
 652                of_prereq=
 653                if test "$missing_prereq" != "$test_prereq"
 654                then
 655                        of_prereq=" of $test_prereq"
 656                fi
 657                skipped_reason="missing $missing_prereq${of_prereq}"
 658        fi
 659        if test -z "$to_skip" && test -n "$run_list" &&
 660                ! match_test_selector_list '--run' $test_count "$run_list"
 661        then
 662                to_skip=t
 663                skipped_reason="--run"
 664        fi
 665
 666        case "$to_skip" in
 667        t)
 668                say_color skip >&3 "skipping test: $@"
 669                say_color skip "ok $test_count # skip $1 ($skipped_reason)"
 670                : true
 671                ;;
 672        *)
 673                false
 674                ;;
 675        esac
 676}
 677
 678# stub; perf-lib overrides it
 679test_at_end_hook_ () {
 680        :
 681}
 682
 683test_done () {
 684        GIT_EXIT_OK=t
 685
 686        if test -z "$HARNESS_ACTIVE"
 687        then
 688                test_results_dir="$TEST_OUTPUT_DIRECTORY/test-results"
 689                mkdir -p "$test_results_dir"
 690                base=${0##*/}
 691                test_results_path="$test_results_dir/${base%.sh}.counts"
 692
 693                cat >"$test_results_path" <<-EOF
 694                total $test_count
 695                success $test_success
 696                fixed $test_fixed
 697                broken $test_broken
 698                failed $test_failure
 699
 700                EOF
 701        fi
 702
 703        if test "$test_fixed" != 0
 704        then
 705                say_color error "# $test_fixed known breakage(s) vanished; please update test(s)"
 706        fi
 707        if test "$test_broken" != 0
 708        then
 709                say_color warn "# still have $test_broken known breakage(s)"
 710        fi
 711        if test "$test_broken" != 0 || test "$test_fixed" != 0
 712        then
 713                test_remaining=$(( $test_count - $test_broken - $test_fixed ))
 714                msg="remaining $test_remaining test(s)"
 715        else
 716                test_remaining=$test_count
 717                msg="$test_count test(s)"
 718        fi
 719        case "$test_failure" in
 720        0)
 721                # Maybe print SKIP message
 722                if test -n "$skip_all" && test $test_count -gt 0
 723                then
 724                        error "Can't use skip_all after running some tests"
 725                fi
 726                test -z "$skip_all" || skip_all=" # SKIP $skip_all"
 727
 728                if test $test_external_has_tap -eq 0
 729                then
 730                        if test $test_remaining -gt 0
 731                        then
 732                                say_color pass "# passed all $msg"
 733                        fi
 734                        say "1..$test_count$skip_all"
 735                fi
 736
 737                test -d "$remove_trash" &&
 738                cd "$(dirname "$remove_trash")" &&
 739                rm -rf "$(basename "$remove_trash")"
 740
 741                test_at_end_hook_
 742
 743                exit 0 ;;
 744
 745        *)
 746                if test $test_external_has_tap -eq 0
 747                then
 748                        say_color error "# failed $test_failure among $msg"
 749                        say "1..$test_count"
 750                fi
 751
 752                exit 1 ;;
 753
 754        esac
 755}
 756
 757if test -n "$valgrind"
 758then
 759        make_symlink () {
 760                test -h "$2" &&
 761                test "$1" = "$(readlink "$2")" || {
 762                        # be super paranoid
 763                        if mkdir "$2".lock
 764                        then
 765                                rm -f "$2" &&
 766                                ln -s "$1" "$2" &&
 767                                rm -r "$2".lock
 768                        else
 769                                while test -d "$2".lock
 770                                do
 771                                        say "Waiting for lock on $2."
 772                                        sleep 1
 773                                done
 774                        fi
 775                }
 776        }
 777
 778        make_valgrind_symlink () {
 779                # handle only executables, unless they are shell libraries that
 780                # need to be in the exec-path.
 781                test -x "$1" ||
 782                test "# " = "$(head -c 2 <"$1")" ||
 783                return;
 784
 785                base=$(basename "$1")
 786                symlink_target=$GIT_BUILD_DIR/$base
 787                # do not override scripts
 788                if test -x "$symlink_target" &&
 789                    test ! -d "$symlink_target" &&
 790                    test "#!" != "$(head -c 2 < "$symlink_target")"
 791                then
 792                        symlink_target=../valgrind.sh
 793                fi
 794                case "$base" in
 795                *.sh|*.perl)
 796                        symlink_target=../unprocessed-script
 797                esac
 798                # create the link, or replace it if it is out of date
 799                make_symlink "$symlink_target" "$GIT_VALGRIND/bin/$base" || exit
 800        }
 801
 802        # override all git executables in TEST_DIRECTORY/..
 803        GIT_VALGRIND=$TEST_DIRECTORY/valgrind
 804        mkdir -p "$GIT_VALGRIND"/bin
 805        for file in $GIT_BUILD_DIR/git* $GIT_BUILD_DIR/t/helper/test-*
 806        do
 807                make_valgrind_symlink $file
 808        done
 809        # special-case the mergetools loadables
 810        make_symlink "$GIT_BUILD_DIR"/mergetools "$GIT_VALGRIND/bin/mergetools"
 811        OLDIFS=$IFS
 812        IFS=:
 813        for path in $PATH
 814        do
 815                ls "$path"/git-* 2> /dev/null |
 816                while read file
 817                do
 818                        make_valgrind_symlink "$file"
 819                done
 820        done
 821        IFS=$OLDIFS
 822        PATH=$GIT_VALGRIND/bin:$PATH
 823        GIT_EXEC_PATH=$GIT_VALGRIND/bin
 824        export GIT_VALGRIND
 825        GIT_VALGRIND_MODE="$valgrind"
 826        export GIT_VALGRIND_MODE
 827        GIT_VALGRIND_ENABLED=t
 828        test -n "$valgrind_only" && GIT_VALGRIND_ENABLED=
 829        export GIT_VALGRIND_ENABLED
 830elif test -n "$GIT_TEST_INSTALLED"
 831then
 832        GIT_EXEC_PATH=$($GIT_TEST_INSTALLED/git --exec-path)  ||
 833        error "Cannot run git from $GIT_TEST_INSTALLED."
 834        PATH=$GIT_TEST_INSTALLED:$GIT_BUILD_DIR:$PATH
 835        GIT_EXEC_PATH=${GIT_TEST_EXEC_PATH:-$GIT_EXEC_PATH}
 836else # normal case, use ../bin-wrappers only unless $with_dashes:
 837        git_bin_dir="$GIT_BUILD_DIR/bin-wrappers"
 838        if ! test -x "$git_bin_dir/git"
 839        then
 840                if test -z "$with_dashes"
 841                then
 842                        say "$git_bin_dir/git is not executable; using GIT_EXEC_PATH"
 843                fi
 844                with_dashes=t
 845        fi
 846        PATH="$git_bin_dir:$PATH"
 847        GIT_EXEC_PATH=$GIT_BUILD_DIR
 848        if test -n "$with_dashes"
 849        then
 850                PATH="$GIT_BUILD_DIR:$PATH"
 851        fi
 852fi
 853GIT_TEMPLATE_DIR="$GIT_BUILD_DIR"/templates/blt
 854GIT_CONFIG_NOSYSTEM=1
 855GIT_ATTR_NOSYSTEM=1
 856export PATH GIT_EXEC_PATH GIT_TEMPLATE_DIR GIT_CONFIG_NOSYSTEM GIT_ATTR_NOSYSTEM
 857
 858if test -z "$GIT_TEST_CMP"
 859then
 860        if test -n "$GIT_TEST_CMP_USE_COPIED_CONTEXT"
 861        then
 862                GIT_TEST_CMP="$DIFF -c"
 863        else
 864                GIT_TEST_CMP="$DIFF -u"
 865        fi
 866fi
 867
 868GITPERLLIB="$GIT_BUILD_DIR"/perl/blib/lib:"$GIT_BUILD_DIR"/perl/blib/arch/auto/Git
 869export GITPERLLIB
 870test -d "$GIT_BUILD_DIR"/templates/blt || {
 871        error "You haven't built things yet, have you?"
 872}
 873
 874if ! test -x "$GIT_BUILD_DIR"/t/helper/test-chmtime
 875then
 876        echo >&2 'You need to build test-chmtime:'
 877        echo >&2 'Run "make t/helper/test-chmtime" in the source (toplevel) directory'
 878        exit 1
 879fi
 880
 881# Test repository
 882TRASH_DIRECTORY="trash directory.$(basename "$0" .sh)"
 883test -n "$root" && TRASH_DIRECTORY="$root/$TRASH_DIRECTORY"
 884case "$TRASH_DIRECTORY" in
 885/*) ;; # absolute path is good
 886 *) TRASH_DIRECTORY="$TEST_OUTPUT_DIRECTORY/$TRASH_DIRECTORY" ;;
 887esac
 888test ! -z "$debug" || remove_trash=$TRASH_DIRECTORY
 889rm -fr "$TRASH_DIRECTORY" || {
 890        GIT_EXIT_OK=t
 891        echo >&5 "FATAL: Cannot prepare test area"
 892        exit 1
 893}
 894
 895HOME="$TRASH_DIRECTORY"
 896GNUPGHOME="$HOME/gnupg-home-not-used"
 897export HOME GNUPGHOME
 898
 899if test -z "$TEST_NO_CREATE_REPO"
 900then
 901        test_create_repo "$TRASH_DIRECTORY"
 902else
 903        mkdir -p "$TRASH_DIRECTORY"
 904fi
 905# Use -P to resolve symlinks in our working directory so that the cwd
 906# in subprocesses like git equals our $PWD (for pathname comparisons).
 907cd -P "$TRASH_DIRECTORY" || exit 1
 908
 909this_test=${0##*/}
 910this_test=${this_test%%-*}
 911if match_pattern_list "$this_test" $GIT_SKIP_TESTS
 912then
 913        say_color info >&3 "skipping test $this_test altogether"
 914        skip_all="skip all tests in $this_test"
 915        test_done
 916fi
 917
 918# Provide an implementation of the 'yes' utility
 919yes () {
 920        if test $# = 0
 921        then
 922                y=y
 923        else
 924                y="$*"
 925        fi
 926
 927        i=0
 928        while test $i -lt 99
 929        do
 930                echo "$y"
 931                i=$(($i+1))
 932        done
 933}
 934
 935# Fix some commands on Windows
 936case $(uname -s) in
 937*MINGW*)
 938        # Windows has its own (incompatible) sort and find
 939        sort () {
 940                /usr/bin/sort "$@"
 941        }
 942        find () {
 943                /usr/bin/find "$@"
 944        }
 945        sum () {
 946                md5sum "$@"
 947        }
 948        # git sees Windows-style pwd
 949        pwd () {
 950                builtin pwd -W
 951        }
 952        # no POSIX permissions
 953        # backslashes in pathspec are converted to '/'
 954        # exec does not inherit the PID
 955        test_set_prereq MINGW
 956        test_set_prereq NATIVE_CRLF
 957        test_set_prereq SED_STRIPS_CR
 958        test_set_prereq GREP_STRIPS_CR
 959        GIT_TEST_CMP=mingw_test_cmp
 960        ;;
 961*CYGWIN*)
 962        test_set_prereq POSIXPERM
 963        test_set_prereq EXECKEEPSPID
 964        test_set_prereq CYGWIN
 965        test_set_prereq SED_STRIPS_CR
 966        test_set_prereq GREP_STRIPS_CR
 967        ;;
 968*)
 969        test_set_prereq POSIXPERM
 970        test_set_prereq BSLASHPSPEC
 971        test_set_prereq EXECKEEPSPID
 972        ;;
 973esac
 974
 975( COLUMNS=1 && test $COLUMNS = 1 ) && test_set_prereq COLUMNS_CAN_BE_1
 976test -z "$NO_PERL" && test_set_prereq PERL
 977test -z "$NO_PYTHON" && test_set_prereq PYTHON
 978test -n "$USE_LIBPCRE" && test_set_prereq LIBPCRE
 979test -z "$NO_GETTEXT" && test_set_prereq GETTEXT
 980
 981# Can we rely on git's output in the C locale?
 982if test -n "$GETTEXT_POISON"
 983then
 984        GIT_GETTEXT_POISON=YesPlease
 985        export GIT_GETTEXT_POISON
 986        test_set_prereq GETTEXT_POISON
 987else
 988        test_set_prereq C_LOCALE_OUTPUT
 989fi
 990
 991# Use this instead of test_cmp to compare files that contain expected and
 992# actual output from git commands that can be translated.  When running
 993# under GETTEXT_POISON this pretends that the command produced expected
 994# results.
 995test_i18ncmp () {
 996        test -n "$GETTEXT_POISON" || test_cmp "$@"
 997}
 998
 999# Use this instead of "grep expected-string actual" to see if the
1000# output from a git command that can be translated either contains an
1001# expected string, or does not contain an unwanted one.  When running
1002# under GETTEXT_POISON this pretends that the command produced expected
1003# results.
1004test_i18ngrep () {
1005        if test -n "$GETTEXT_POISON"
1006        then
1007            : # pretend success
1008        elif test "x!" = "x$1"
1009        then
1010                shift
1011                ! grep "$@"
1012        else
1013                grep "$@"
1014        fi
1015}
1016
1017test_lazy_prereq PIPE '
1018        # test whether the filesystem supports FIFOs
1019        case $(uname -s) in
1020        CYGWIN*|MINGW*)
1021                false
1022                ;;
1023        *)
1024                rm -f testfifo && mkfifo testfifo
1025                ;;
1026        esac
1027'
1028
1029test_lazy_prereq SYMLINKS '
1030        # test whether the filesystem supports symbolic links
1031        ln -s x y && test -h y
1032'
1033
1034test_lazy_prereq FILEMODE '
1035        test "$(git config --bool core.filemode)" = true
1036'
1037
1038test_lazy_prereq CASE_INSENSITIVE_FS '
1039        echo good >CamelCase &&
1040        echo bad >camelcase &&
1041        test "$(cat CamelCase)" != good
1042'
1043
1044test_lazy_prereq UTF8_NFD_TO_NFC '
1045        # check whether FS converts nfd unicode to nfc
1046        auml=$(printf "\303\244")
1047        aumlcdiar=$(printf "\141\314\210")
1048        >"$auml" &&
1049        case "$(echo *)" in
1050        "$aumlcdiar")
1051                true ;;
1052        *)
1053                false ;;
1054        esac
1055'
1056
1057test_lazy_prereq AUTOIDENT '
1058        sane_unset GIT_AUTHOR_NAME &&
1059        sane_unset GIT_AUTHOR_EMAIL &&
1060        git var GIT_AUTHOR_IDENT
1061'
1062
1063test_lazy_prereq EXPENSIVE '
1064        test -n "$GIT_TEST_LONG"
1065'
1066
1067test_lazy_prereq USR_BIN_TIME '
1068        test -x /usr/bin/time
1069'
1070
1071test_lazy_prereq NOT_ROOT '
1072        uid=$(id -u) &&
1073        test "$uid" != 0
1074'
1075
1076# SANITY is about "can you correctly predict what the filesystem would
1077# do by only looking at the permission bits of the files and
1078# directories?"  A typical example of !SANITY is running the test
1079# suite as root, where a test may expect "chmod -r file && cat file"
1080# to fail because file is supposed to be unreadable after a successful
1081# chmod.  In an environment (i.e. combination of what filesystem is
1082# being used and who is running the tests) that lacks SANITY, you may
1083# be able to delete or create a file when the containing directory
1084# doesn't have write permissions, or access a file even if the
1085# containing directory doesn't have read or execute permissions.
1086
1087test_lazy_prereq SANITY '
1088        mkdir SANETESTD.1 SANETESTD.2 &&
1089
1090        chmod +w SANETESTD.1 SANETESTD.2 &&
1091        >SANETESTD.1/x 2>SANETESTD.2/x &&
1092        chmod -w SANETESTD.1 &&
1093        chmod -r SANETESTD.1/x &&
1094        chmod -rx SANETESTD.2 ||
1095        error "bug in test sript: cannot prepare SANETESTD"
1096
1097        ! test -r SANETESTD.1/x &&
1098        ! rm SANETESTD.1/x && ! test -f SANETESTD.2/x
1099        status=$?
1100
1101        chmod +rwx SANETESTD.1 SANETESTD.2 &&
1102        rm -rf SANETESTD.1 SANETESTD.2 ||
1103        error "bug in test sript: cannot clean SANETESTD"
1104        return $status
1105'
1106
1107GIT_UNZIP=${GIT_UNZIP:-unzip}
1108test_lazy_prereq UNZIP '
1109        "$GIT_UNZIP" -v
1110        test $? -ne 127
1111'
1112
1113run_with_limited_cmdline () {
1114        (ulimit -s 128 && "$@")
1115}
1116
1117test_lazy_prereq CMDLINE_LIMIT 'run_with_limited_cmdline true'
1118
1119build_option () {
1120        git version --build-options |
1121        sed -ne "s/^$1: //p"
1122}
1123
1124test_lazy_prereq LONG_IS_64BIT '
1125        test 8 -le "$(build_option sizeof-long)"
1126'