8553c1ff8764da221b484b43e3c46f6b5d6d5989
   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# If we were built with ASAN, it may complain about leaks
  40# of program-lifetime variables. Disable it by default to lower
  41# the noise level. This needs to happen at the start of the script,
  42# before we even do our "did we build git yet" check (since we don't
  43# want that one to complain to stderr).
  44: ${ASAN_OPTIONS=detect_leaks=0:abort_on_error=1}
  45export ASAN_OPTIONS
  46
  47# If LSAN is in effect we _do_ want leak checking, but we still
  48# want to abort so that we notice the problems.
  49: ${LSAN_OPTIONS=abort_on_error=1}
  50export LSAN_OPTIONS
  51
  52if test ! -f "$GIT_BUILD_DIR"/GIT-BUILD-OPTIONS
  53then
  54        echo >&2 'error: GIT-BUILD-OPTIONS missing (has Git been built?).'
  55        exit 1
  56fi
  57. "$GIT_BUILD_DIR"/GIT-BUILD-OPTIONS
  58export PERL_PATH SHELL_PATH
  59
  60################################################################
  61# It appears that people try to run tests without building...
  62"${GIT_TEST_INSTALLED:-$GIT_BUILD_DIR}/git$X" >/dev/null
  63if test $? != 1
  64then
  65        if test -n "$GIT_TEST_INSTALLED"
  66        then
  67                echo >&2 "error: there is no working Git at '$GIT_TEST_INSTALLED'"
  68        else
  69                echo >&2 'error: you do not seem to have built git yet.'
  70        fi
  71        exit 1
  72fi
  73
  74# Parse options while taking care to leave $@ intact, so we will still
  75# have all the original command line options when executing the test
  76# script again for '--tee' and '--verbose-log' below.
  77store_arg_to=
  78prev_opt=
  79for opt
  80do
  81        if test -n "$store_arg_to"
  82        then
  83                eval $store_arg_to=\$opt
  84                store_arg_to=
  85                prev_opt=
  86                continue
  87        fi
  88
  89        case "$opt" in
  90        -d|--d|--de|--deb|--debu|--debug)
  91                debug=t ;;
  92        -i|--i|--im|--imm|--imme|--immed|--immedi|--immedia|--immediat|--immediate)
  93                immediate=t ;;
  94        -l|--l|--lo|--lon|--long|--long-|--long-t|--long-te|--long-tes|--long-test|--long-tests)
  95                GIT_TEST_LONG=t; export GIT_TEST_LONG ;;
  96        -r)
  97                store_arg_to=run_list
  98                ;;
  99        --run=*)
 100                run_list=${opt#--*=} ;;
 101        -h|--h|--he|--hel|--help)
 102                help=t ;;
 103        -v|--v|--ve|--ver|--verb|--verbo|--verbos|--verbose)
 104                verbose=t ;;
 105        --verbose-only=*)
 106                verbose_only=${opt#--*=}
 107                ;;
 108        -q|--q|--qu|--qui|--quie|--quiet)
 109                # Ignore --quiet under a TAP::Harness. Saying how many tests
 110                # passed without the ok/not ok details is always an error.
 111                test -z "$HARNESS_ACTIVE" && quiet=t ;;
 112        --with-dashes)
 113                with_dashes=t ;;
 114        --no-bin-wrappers)
 115                no_bin_wrappers=t ;;
 116        --no-color)
 117                color= ;;
 118        --va|--val|--valg|--valgr|--valgri|--valgrin|--valgrind)
 119                valgrind=memcheck
 120                tee=t
 121                ;;
 122        --valgrind=*)
 123                valgrind=${opt#--*=}
 124                tee=t
 125                ;;
 126        --valgrind-only=*)
 127                valgrind_only=${opt#--*=}
 128                tee=t
 129                ;;
 130        --tee)
 131                tee=t ;;
 132        --root=*)
 133                root=${opt#--*=} ;;
 134        --chain-lint)
 135                GIT_TEST_CHAIN_LINT=1 ;;
 136        --no-chain-lint)
 137                GIT_TEST_CHAIN_LINT=0 ;;
 138        -x)
 139                trace=t ;;
 140        -V|--verbose-log)
 141                verbose_log=t
 142                tee=t
 143                ;;
 144        --write-junit-xml)
 145                write_junit_xml=t
 146                ;;
 147        --stress)
 148                stress=t ;;
 149        --stress=*)
 150                stress=${opt#--*=}
 151                case "$stress" in
 152                *[!0-9]*|0*|"")
 153                        echo "error: --stress=<N> requires the number of jobs to run" >&2
 154                        exit 1
 155                        ;;
 156                *)      # Good.
 157                        ;;
 158                esac
 159                ;;
 160        --stress-limit=*)
 161                stress=t;
 162                stress_limit=${opt#--*=}
 163                case "$stress_limit" in
 164                *[!0-9]*|0*|"")
 165                        echo "error: --stress-limit=<N> requires the number of repetitions" >&2
 166                        exit 1
 167                        ;;
 168                *)      # Good.
 169                        ;;
 170                esac
 171                ;;
 172        *)
 173                echo "error: unknown test option '$opt'" >&2; exit 1 ;;
 174        esac
 175
 176        prev_opt=$opt
 177done
 178if test -n "$store_arg_to"
 179then
 180        echo "error: $prev_opt requires an argument" >&2
 181        exit 1
 182fi
 183
 184if test -n "$valgrind_only"
 185then
 186        test -z "$valgrind" && valgrind=memcheck
 187        test -z "$verbose" && verbose_only="$valgrind_only"
 188elif test -n "$valgrind"
 189then
 190        test -z "$verbose_log" && verbose=t
 191fi
 192
 193if test -n "$stress"
 194then
 195        verbose=t
 196        trace=t
 197        immediate=t
 198fi
 199
 200TEST_STRESS_JOB_SFX="${GIT_TEST_STRESS_JOB_NR:+.stress-$GIT_TEST_STRESS_JOB_NR}"
 201TEST_NAME="$(basename "$0" .sh)"
 202TEST_RESULTS_DIR="$TEST_OUTPUT_DIRECTORY/test-results"
 203TEST_RESULTS_BASE="$TEST_RESULTS_DIR/$TEST_NAME$TEST_STRESS_JOB_SFX"
 204TRASH_DIRECTORY="trash directory.$TEST_NAME$TEST_STRESS_JOB_SFX"
 205test -n "$root" && TRASH_DIRECTORY="$root/$TRASH_DIRECTORY"
 206case "$TRASH_DIRECTORY" in
 207/*) ;; # absolute path is good
 208 *) TRASH_DIRECTORY="$TEST_OUTPUT_DIRECTORY/$TRASH_DIRECTORY" ;;
 209esac
 210
 211# If --stress was passed, run this test repeatedly in several parallel loops.
 212if test "$GIT_TEST_STRESS_STARTED" = "done"
 213then
 214        : # Don't stress test again.
 215elif test -n "$stress"
 216then
 217        if test "$stress" != t
 218        then
 219                job_count=$stress
 220        elif test -n "$GIT_TEST_STRESS_LOAD"
 221        then
 222                job_count="$GIT_TEST_STRESS_LOAD"
 223        elif job_count=$(getconf _NPROCESSORS_ONLN 2>/dev/null) &&
 224             test -n "$job_count"
 225        then
 226                job_count=$((2 * $job_count))
 227        else
 228                job_count=8
 229        fi
 230
 231        mkdir -p "$TEST_RESULTS_DIR"
 232        stressfail="$TEST_RESULTS_BASE.stress-failed"
 233        rm -f "$stressfail"
 234
 235        stress_exit=0
 236        trap '
 237                kill $job_pids 2>/dev/null
 238                wait
 239                stress_exit=1
 240        ' TERM INT HUP
 241
 242        job_pids=
 243        job_nr=0
 244        while test $job_nr -lt "$job_count"
 245        do
 246                (
 247                        GIT_TEST_STRESS_STARTED=done
 248                        GIT_TEST_STRESS_JOB_NR=$job_nr
 249                        export GIT_TEST_STRESS_STARTED GIT_TEST_STRESS_JOB_NR
 250
 251                        trap '
 252                                kill $test_pid 2>/dev/null
 253                                wait
 254                                exit 1
 255                        ' TERM INT
 256
 257                        cnt=1
 258                        while ! test -e "$stressfail" &&
 259                              { test -z "$stress_limit" ||
 260                                test $cnt -le $stress_limit ; }
 261                        do
 262                                $TEST_SHELL_PATH "$0" "$@" >"$TEST_RESULTS_BASE.stress-$job_nr.out" 2>&1 &
 263                                test_pid=$!
 264
 265                                if wait $test_pid
 266                                then
 267                                        printf "OK   %2d.%d\n" $GIT_TEST_STRESS_JOB_NR $cnt
 268                                else
 269                                        echo $GIT_TEST_STRESS_JOB_NR >>"$stressfail"
 270                                        printf "FAIL %2d.%d\n" $GIT_TEST_STRESS_JOB_NR $cnt
 271                                fi
 272                                cnt=$(($cnt + 1))
 273                        done
 274                ) &
 275                job_pids="$job_pids $!"
 276                job_nr=$(($job_nr + 1))
 277        done
 278
 279        wait
 280
 281        if test -f "$stressfail"
 282        then
 283                stress_exit=1
 284                echo "Log(s) of failed test run(s):"
 285                for failed_job_nr in $(sort -n "$stressfail")
 286                do
 287                        echo "Contents of '$TEST_RESULTS_BASE.stress-$failed_job_nr.out':"
 288                        cat "$TEST_RESULTS_BASE.stress-$failed_job_nr.out"
 289                done
 290                rm -rf "$TRASH_DIRECTORY.stress-failed"
 291                # Move the last one.
 292                mv "$TRASH_DIRECTORY.stress-$failed_job_nr" "$TRASH_DIRECTORY.stress-failed"
 293        fi
 294
 295        exit $stress_exit
 296fi
 297
 298# if --tee was passed, write the output not only to the terminal, but
 299# additionally to the file test-results/$BASENAME.out, too.
 300if test "$GIT_TEST_TEE_STARTED" = "done"
 301then
 302        : # do not redirect again
 303elif test -n "$tee"
 304then
 305        mkdir -p "$TEST_RESULTS_DIR"
 306
 307        # Make this filename available to the sub-process in case it is using
 308        # --verbose-log.
 309        GIT_TEST_TEE_OUTPUT_FILE=$TEST_RESULTS_BASE.out
 310        export GIT_TEST_TEE_OUTPUT_FILE
 311
 312        # Truncate before calling "tee -a" to get rid of the results
 313        # from any previous runs.
 314        >"$GIT_TEST_TEE_OUTPUT_FILE"
 315
 316        (GIT_TEST_TEE_STARTED=done ${TEST_SHELL_PATH} "$0" "$@" 2>&1;
 317         echo $? >"$TEST_RESULTS_BASE.exit") | tee -a "$GIT_TEST_TEE_OUTPUT_FILE"
 318        test "$(cat "$TEST_RESULTS_BASE.exit")" = 0
 319        exit
 320fi
 321
 322if test -n "$trace" && test -n "$test_untraceable"
 323then
 324        # '-x' tracing requested, but this test script can't be reliably
 325        # traced, unless it is run with a Bash version supporting
 326        # BASH_XTRACEFD (introduced in Bash v4.1).
 327        #
 328        # Perform this version check _after_ the test script was
 329        # potentially re-executed with $TEST_SHELL_PATH for '--tee' or
 330        # '--verbose-log', so the right shell is checked and the
 331        # warning is issued only once.
 332        if test -n "$BASH_VERSION" && eval '
 333             test ${BASH_VERSINFO[0]} -gt 4 || {
 334               test ${BASH_VERSINFO[0]} -eq 4 &&
 335               test ${BASH_VERSINFO[1]} -ge 1
 336             }
 337           '
 338        then
 339                : Executed by a Bash version supporting BASH_XTRACEFD.  Good.
 340        else
 341                echo >&2 "warning: ignoring -x; '$0' is untraceable without BASH_XTRACEFD"
 342                trace=
 343        fi
 344fi
 345if test -n "$trace" && test -z "$verbose_log"
 346then
 347        verbose=t
 348fi
 349
 350# For repeatability, reset the environment to known value.
 351# TERM is sanitized below, after saving color control sequences.
 352LANG=C
 353LC_ALL=C
 354PAGER=cat
 355TZ=UTC
 356export LANG LC_ALL PAGER TZ
 357EDITOR=:
 358
 359# GIT_TEST_GETTEXT_POISON should not influence git commands executed
 360# during initialization of test-lib and the test repo. Back it up,
 361# unset and then restore after initialization is finished.
 362if test -n "$GIT_TEST_GETTEXT_POISON"
 363then
 364        GIT_TEST_GETTEXT_POISON_ORIG=$GIT_TEST_GETTEXT_POISON
 365        unset GIT_TEST_GETTEXT_POISON
 366fi
 367
 368# A call to "unset" with no arguments causes at least Solaris 10
 369# /usr/xpg4/bin/sh and /bin/ksh to bail out.  So keep the unsets
 370# deriving from the command substitution clustered with the other
 371# ones.
 372unset VISUAL EMAIL LANGUAGE COLUMNS $("$PERL_PATH" -e '
 373        my @env = keys %ENV;
 374        my $ok = join("|", qw(
 375                TRACE
 376                DEBUG
 377                TEST
 378                .*_TEST
 379                PROVE
 380                VALGRIND
 381                UNZIP
 382                PERF_
 383                CURL_VERBOSE
 384                TRACE_CURL
 385        ));
 386        my @vars = grep(/^GIT_/ && !/^GIT_($ok)/o, @env);
 387        print join("\n", @vars);
 388')
 389unset XDG_CACHE_HOME
 390unset XDG_CONFIG_HOME
 391unset GITPERLLIB
 392GIT_AUTHOR_EMAIL=author@example.com
 393GIT_AUTHOR_NAME='A U Thor'
 394GIT_COMMITTER_EMAIL=committer@example.com
 395GIT_COMMITTER_NAME='C O Mitter'
 396GIT_MERGE_VERBOSITY=5
 397GIT_MERGE_AUTOEDIT=no
 398export GIT_MERGE_VERBOSITY GIT_MERGE_AUTOEDIT
 399export GIT_AUTHOR_EMAIL GIT_AUTHOR_NAME
 400export GIT_COMMITTER_EMAIL GIT_COMMITTER_NAME
 401export EDITOR
 402
 403# Tests using GIT_TRACE typically don't want <timestamp> <file>:<line> output
 404GIT_TRACE_BARE=1
 405export GIT_TRACE_BARE
 406
 407check_var_migration () {
 408        # the warnings and hints given from this helper depends
 409        # on end-user settings, which will disrupt the self-test
 410        # done on the test framework itself.
 411        case "$GIT_TEST_FRAMEWORK_SELFTEST" in
 412        t)      return ;;
 413        esac
 414
 415        old_name=$1 new_name=$2
 416        eval "old_isset=\${${old_name}:+isset}"
 417        eval "new_isset=\${${new_name}:+isset}"
 418
 419        case "$old_isset,$new_isset" in
 420        isset,)
 421                echo >&2 "warning: $old_name is now $new_name"
 422                echo >&2 "hint: set $new_name too during the transition period"
 423                eval "$new_name=\$$old_name"
 424                ;;
 425        isset,isset)
 426                # do this later
 427                # echo >&2 "warning: $old_name is now $new_name"
 428                # echo >&2 "hint: remove $old_name"
 429                ;;
 430        esac
 431}
 432
 433check_var_migration GIT_FSMONITOR_TEST GIT_TEST_FSMONITOR
 434check_var_migration TEST_GIT_INDEX_VERSION GIT_TEST_INDEX_VERSION
 435check_var_migration GIT_FORCE_PRELOAD_TEST GIT_TEST_PRELOAD_INDEX
 436
 437# Use specific version of the index file format
 438if test -n "${GIT_TEST_INDEX_VERSION:+isset}"
 439then
 440        GIT_INDEX_VERSION="$GIT_TEST_INDEX_VERSION"
 441        export GIT_INDEX_VERSION
 442fi
 443
 444# Add libc MALLOC and MALLOC_PERTURB test
 445# only if we are not executing the test with valgrind
 446if test -n "$valgrind" ||
 447   test -n "$TEST_NO_MALLOC_CHECK"
 448then
 449        setup_malloc_check () {
 450                : nothing
 451        }
 452        teardown_malloc_check () {
 453                : nothing
 454        }
 455else
 456        setup_malloc_check () {
 457                MALLOC_CHECK_=3 MALLOC_PERTURB_=165
 458                export MALLOC_CHECK_ MALLOC_PERTURB_
 459        }
 460        teardown_malloc_check () {
 461                unset MALLOC_CHECK_ MALLOC_PERTURB_
 462        }
 463fi
 464
 465# Protect ourselves from common misconfiguration to export
 466# CDPATH into the environment
 467unset CDPATH
 468
 469unset GREP_OPTIONS
 470unset UNZIP
 471
 472case $(echo $GIT_TRACE |tr "[A-Z]" "[a-z]") in
 4731|2|true)
 474        GIT_TRACE=4
 475        ;;
 476esac
 477
 478# Convenience
 479#
 480# A regexp to match 5, 35 and 40 hexdigits
 481_x05='[0-9a-f][0-9a-f][0-9a-f][0-9a-f][0-9a-f]'
 482_x35="$_x05$_x05$_x05$_x05$_x05$_x05$_x05"
 483_x40="$_x35$_x05"
 484
 485# Zero SHA-1
 486_z40=0000000000000000000000000000000000000000
 487
 488OID_REGEX="$_x40"
 489ZERO_OID=$_z40
 490EMPTY_TREE=4b825dc642cb6eb9a060e54bf8d69288fbee4904
 491EMPTY_BLOB=e69de29bb2d1d6434b8b29ae775ad8c2e48c5391
 492
 493# Line feed
 494LF='
 495'
 496
 497# UTF-8 ZERO WIDTH NON-JOINER, which HFS+ ignores
 498# when case-folding filenames
 499u200c=$(printf '\342\200\214')
 500
 501export _x05 _x35 _x40 _z40 LF u200c EMPTY_TREE EMPTY_BLOB ZERO_OID OID_REGEX
 502
 503# Each test should start with something like this, after copyright notices:
 504#
 505# test_description='Description of this test...
 506# This test checks if command xyzzy does the right thing...
 507# '
 508# . ./test-lib.sh
 509test "x$TERM" != "xdumb" && (
 510                test -t 1 &&
 511                tput bold >/dev/null 2>&1 &&
 512                tput setaf 1 >/dev/null 2>&1 &&
 513                tput sgr0 >/dev/null 2>&1
 514        ) &&
 515        color=t
 516
 517if test -n "$color"
 518then
 519        # Save the color control sequences now rather than run tput
 520        # each time say_color() is called.  This is done for two
 521        # reasons:
 522        #   * TERM will be changed to dumb
 523        #   * HOME will be changed to a temporary directory and tput
 524        #     might need to read ~/.terminfo from the original HOME
 525        #     directory to get the control sequences
 526        # Note:  This approach assumes the control sequences don't end
 527        # in a newline for any terminal of interest (command
 528        # substitutions strip trailing newlines).  Given that most
 529        # (all?) terminals in common use are related to ECMA-48, this
 530        # shouldn't be a problem.
 531        say_color_error=$(tput bold; tput setaf 1) # bold red
 532        say_color_skip=$(tput setaf 4) # blue
 533        say_color_warn=$(tput setaf 3) # brown/yellow
 534        say_color_pass=$(tput setaf 2) # green
 535        say_color_info=$(tput setaf 6) # cyan
 536        say_color_reset=$(tput sgr0)
 537        say_color_="" # no formatting for normal text
 538        say_color () {
 539                test -z "$1" && test -n "$quiet" && return
 540                eval "say_color_color=\$say_color_$1"
 541                shift
 542                printf "%s\\n" "$say_color_color$*$say_color_reset"
 543        }
 544else
 545        say_color() {
 546                test -z "$1" && test -n "$quiet" && return
 547                shift
 548                printf "%s\n" "$*"
 549        }
 550fi
 551
 552TERM=dumb
 553export TERM
 554
 555error () {
 556        say_color error "error: $*"
 557        GIT_EXIT_OK=t
 558        exit 1
 559}
 560
 561BUG () {
 562        error >&7 "bug in the test script: $*"
 563}
 564
 565say () {
 566        say_color info "$*"
 567}
 568
 569if test -n "$HARNESS_ACTIVE"
 570then
 571        if test "$verbose" = t || test -n "$verbose_only"
 572        then
 573                printf 'Bail out! %s\n' \
 574                 'verbose mode forbidden under TAP harness; try --verbose-log'
 575                exit 1
 576        fi
 577fi
 578
 579test "${test_description}" != "" ||
 580error "Test script did not set test_description."
 581
 582if test "$help" = "t"
 583then
 584        printf '%s\n' "$test_description"
 585        exit 0
 586fi
 587
 588exec 5>&1
 589exec 6<&0
 590exec 7>&2
 591if test "$verbose_log" = "t"
 592then
 593        exec 3>>"$GIT_TEST_TEE_OUTPUT_FILE" 4>&3
 594elif test "$verbose" = "t"
 595then
 596        exec 4>&2 3>&1
 597else
 598        exec 4>/dev/null 3>/dev/null
 599fi
 600
 601# Send any "-x" output directly to stderr to avoid polluting tests
 602# which capture stderr. We can do this unconditionally since it
 603# has no effect if tracing isn't turned on.
 604#
 605# Note that this sets up the trace fd as soon as we assign the variable, so it
 606# must come after the creation of descriptor 4 above. Likewise, we must never
 607# unset this, as it has the side effect of closing descriptor 4, which we
 608# use to show verbose tests to the user.
 609#
 610# Note also that we don't need or want to export it. The tracing is local to
 611# this shell, and we would not want to influence any shells we exec.
 612BASH_XTRACEFD=4
 613
 614test_failure=0
 615test_count=0
 616test_fixed=0
 617test_broken=0
 618test_success=0
 619
 620test_external_has_tap=0
 621
 622die () {
 623        code=$?
 624        if test -n "$GIT_EXIT_OK"
 625        then
 626                exit $code
 627        else
 628                echo >&5 "FATAL: Unexpected exit with code $code"
 629                exit 1
 630        fi
 631}
 632
 633GIT_EXIT_OK=
 634trap 'die' EXIT
 635trap 'exit $?' INT TERM HUP
 636
 637# The user-facing functions are loaded from a separate file so that
 638# test_perf subshells can have them too
 639. "$TEST_DIRECTORY/test-lib-functions.sh"
 640
 641# You are not expected to call test_ok_ and test_failure_ directly, use
 642# the test_expect_* functions instead.
 643
 644test_ok_ () {
 645        if test -n "$write_junit_xml"
 646        then
 647                write_junit_xml_testcase "$*"
 648        fi
 649        test_success=$(($test_success + 1))
 650        say_color "" "ok $test_count - $@"
 651}
 652
 653test_failure_ () {
 654        if test -n "$write_junit_xml"
 655        then
 656                junit_insert="<failure message=\"not ok $test_count -"
 657                junit_insert="$junit_insert $(xml_attr_encode "$1")\">"
 658                junit_insert="$junit_insert $(xml_attr_encode \
 659                        "$(if test -n "$GIT_TEST_TEE_OUTPUT_FILE"
 660                           then
 661                                test-tool path-utils skip-n-bytes \
 662                                        "$GIT_TEST_TEE_OUTPUT_FILE" $GIT_TEST_TEE_OFFSET
 663                           else
 664                                printf '%s\n' "$@" | sed 1d
 665                           fi)")"
 666                junit_insert="$junit_insert</failure>"
 667                if test -n "$GIT_TEST_TEE_OUTPUT_FILE"
 668                then
 669                        junit_insert="$junit_insert<system-err>$(xml_attr_encode \
 670                                "$(cat "$GIT_TEST_TEE_OUTPUT_FILE")")</system-err>"
 671                fi
 672                write_junit_xml_testcase "$1" "      $junit_insert"
 673        fi
 674        test_failure=$(($test_failure + 1))
 675        say_color error "not ok $test_count - $1"
 676        shift
 677        printf '%s\n' "$*" | sed -e 's/^/#      /'
 678        test "$immediate" = "" || { GIT_EXIT_OK=t; exit 1; }
 679}
 680
 681test_known_broken_ok_ () {
 682        if test -n "$write_junit_xml"
 683        then
 684                write_junit_xml_testcase "$* (breakage fixed)"
 685        fi
 686        test_fixed=$(($test_fixed+1))
 687        say_color error "ok $test_count - $@ # TODO known breakage vanished"
 688}
 689
 690test_known_broken_failure_ () {
 691        if test -n "$write_junit_xml"
 692        then
 693                write_junit_xml_testcase "$* (known breakage)"
 694        fi
 695        test_broken=$(($test_broken+1))
 696        say_color warn "not ok $test_count - $@ # TODO known breakage"
 697}
 698
 699test_debug () {
 700        test "$debug" = "" || eval "$1"
 701}
 702
 703match_pattern_list () {
 704        arg="$1"
 705        shift
 706        test -z "$*" && return 1
 707        for pattern_
 708        do
 709                case "$arg" in
 710                $pattern_)
 711                        return 0
 712                esac
 713        done
 714        return 1
 715}
 716
 717match_test_selector_list () {
 718        title="$1"
 719        shift
 720        arg="$1"
 721        shift
 722        test -z "$1" && return 0
 723
 724        # Both commas and whitespace are accepted as separators.
 725        OLDIFS=$IFS
 726        IFS='   ,'
 727        set -- $1
 728        IFS=$OLDIFS
 729
 730        # If the first selector is negative we include by default.
 731        include=
 732        case "$1" in
 733                !*) include=t ;;
 734        esac
 735
 736        for selector
 737        do
 738                orig_selector=$selector
 739
 740                positive=t
 741                case "$selector" in
 742                        !*)
 743                                positive=
 744                                selector=${selector##?}
 745                                ;;
 746                esac
 747
 748                test -z "$selector" && continue
 749
 750                case "$selector" in
 751                        *-*)
 752                                if expr "z${selector%%-*}" : "z[0-9]*[^0-9]" >/dev/null
 753                                then
 754                                        echo "error: $title: invalid non-numeric in range" \
 755                                                "start: '$orig_selector'" >&2
 756                                        exit 1
 757                                fi
 758                                if expr "z${selector#*-}" : "z[0-9]*[^0-9]" >/dev/null
 759                                then
 760                                        echo "error: $title: invalid non-numeric in range" \
 761                                                "end: '$orig_selector'" >&2
 762                                        exit 1
 763                                fi
 764                                ;;
 765                        *)
 766                                if expr "z$selector" : "z[0-9]*[^0-9]" >/dev/null
 767                                then
 768                                        echo "error: $title: invalid non-numeric in test" \
 769                                                "selector: '$orig_selector'" >&2
 770                                        exit 1
 771                                fi
 772                esac
 773
 774                # Short cut for "obvious" cases
 775                test -z "$include" && test -z "$positive" && continue
 776                test -n "$include" && test -n "$positive" && continue
 777
 778                case "$selector" in
 779                        -*)
 780                                if test $arg -le ${selector#-}
 781                                then
 782                                        include=$positive
 783                                fi
 784                                ;;
 785                        *-)
 786                                if test $arg -ge ${selector%-}
 787                                then
 788                                        include=$positive
 789                                fi
 790                                ;;
 791                        *-*)
 792                                if test ${selector%%-*} -le $arg \
 793                                        && test $arg -le ${selector#*-}
 794                                then
 795                                        include=$positive
 796                                fi
 797                                ;;
 798                        *)
 799                                if test $arg -eq $selector
 800                                then
 801                                        include=$positive
 802                                fi
 803                                ;;
 804                esac
 805        done
 806
 807        test -n "$include"
 808}
 809
 810maybe_teardown_verbose () {
 811        test -z "$verbose_only" && return
 812        exec 4>/dev/null 3>/dev/null
 813        verbose=
 814}
 815
 816last_verbose=t
 817maybe_setup_verbose () {
 818        test -z "$verbose_only" && return
 819        if match_pattern_list $test_count $verbose_only
 820        then
 821                exec 4>&2 3>&1
 822                # Emit a delimiting blank line when going from
 823                # non-verbose to verbose.  Within verbose mode the
 824                # delimiter is printed by test_expect_*.  The choice
 825                # of the initial $last_verbose is such that before
 826                # test 1, we do not print it.
 827                test -z "$last_verbose" && echo >&3 ""
 828                verbose=t
 829        else
 830                exec 4>/dev/null 3>/dev/null
 831                verbose=
 832        fi
 833        last_verbose=$verbose
 834}
 835
 836maybe_teardown_valgrind () {
 837        test -z "$GIT_VALGRIND" && return
 838        GIT_VALGRIND_ENABLED=
 839}
 840
 841maybe_setup_valgrind () {
 842        test -z "$GIT_VALGRIND" && return
 843        if test -z "$valgrind_only"
 844        then
 845                GIT_VALGRIND_ENABLED=t
 846                return
 847        fi
 848        GIT_VALGRIND_ENABLED=
 849        if match_pattern_list $test_count $valgrind_only
 850        then
 851                GIT_VALGRIND_ENABLED=t
 852        fi
 853}
 854
 855want_trace () {
 856        test "$trace" = t && {
 857                test "$verbose" = t || test "$verbose_log" = t
 858        }
 859}
 860
 861# This is a separate function because some tests use
 862# "return" to end a test_expect_success block early
 863# (and we want to make sure we run any cleanup like
 864# "set +x").
 865test_eval_inner_ () {
 866        # Do not add anything extra (including LF) after '$*'
 867        eval "
 868                want_trace && set -x
 869                $*"
 870}
 871
 872test_eval_ () {
 873        # If "-x" tracing is in effect, then we want to avoid polluting stderr
 874        # with non-test commands. But once in "set -x" mode, we cannot prevent
 875        # the shell from printing the "set +x" to turn it off (nor the saving
 876        # of $? before that). But we can make sure that the output goes to
 877        # /dev/null.
 878        #
 879        # There are a few subtleties here:
 880        #
 881        #   - we have to redirect descriptor 4 in addition to 2, to cover
 882        #     BASH_XTRACEFD
 883        #
 884        #   - the actual eval has to come before the redirection block (since
 885        #     it needs to see descriptor 4 to set up its stderr)
 886        #
 887        #   - likewise, any error message we print must be outside the block to
 888        #     access descriptor 4
 889        #
 890        #   - checking $? has to come immediately after the eval, but it must
 891        #     be _inside_ the block to avoid polluting the "set -x" output
 892        #
 893
 894        test_eval_inner_ "$@" </dev/null >&3 2>&4
 895        {
 896                test_eval_ret_=$?
 897                if want_trace
 898                then
 899                        set +x
 900                fi
 901        } 2>/dev/null 4>&2
 902
 903        if test "$test_eval_ret_" != 0 && want_trace
 904        then
 905                say_color error >&4 "error: last command exited with \$?=$test_eval_ret_"
 906        fi
 907        return $test_eval_ret_
 908}
 909
 910test_run_ () {
 911        test_cleanup=:
 912        expecting_failure=$2
 913
 914        if test "${GIT_TEST_CHAIN_LINT:-1}" != 0; then
 915                # turn off tracing for this test-eval, as it simply creates
 916                # confusing noise in the "-x" output
 917                trace_tmp=$trace
 918                trace=
 919                # 117 is magic because it is unlikely to match the exit
 920                # code of other programs
 921                if $(printf '%s\n' "$1" | sed -f "$GIT_BUILD_DIR/t/chainlint.sed" | grep -q '?![A-Z][A-Z]*?!') ||
 922                        test "OK-117" != "$(test_eval_ "(exit 117) && $1${LF}${LF}echo OK-\$?" 3>&1)"
 923                then
 924                        BUG "broken &&-chain or run-away HERE-DOC: $1"
 925                fi
 926                trace=$trace_tmp
 927        fi
 928
 929        setup_malloc_check
 930        test_eval_ "$1"
 931        eval_ret=$?
 932        teardown_malloc_check
 933
 934        if test -z "$immediate" || test $eval_ret = 0 ||
 935           test -n "$expecting_failure" && test "$test_cleanup" != ":"
 936        then
 937                setup_malloc_check
 938                test_eval_ "$test_cleanup"
 939                teardown_malloc_check
 940        fi
 941        if test "$verbose" = "t" && test -n "$HARNESS_ACTIVE"
 942        then
 943                echo ""
 944        fi
 945        return "$eval_ret"
 946}
 947
 948test_start_ () {
 949        test_count=$(($test_count+1))
 950        maybe_setup_verbose
 951        maybe_setup_valgrind
 952        if test -n "$write_junit_xml"
 953        then
 954                junit_start=$(test-tool date getnanos)
 955        fi
 956}
 957
 958test_finish_ () {
 959        echo >&3 ""
 960        maybe_teardown_valgrind
 961        maybe_teardown_verbose
 962        if test -n "$GIT_TEST_TEE_OFFSET"
 963        then
 964                GIT_TEST_TEE_OFFSET=$(test-tool path-utils file-size \
 965                        "$GIT_TEST_TEE_OUTPUT_FILE")
 966        fi
 967}
 968
 969test_skip () {
 970        to_skip=
 971        skipped_reason=
 972        if match_pattern_list $this_test.$test_count $GIT_SKIP_TESTS
 973        then
 974                to_skip=t
 975                skipped_reason="GIT_SKIP_TESTS"
 976        fi
 977        if test -z "$to_skip" && test -n "$test_prereq" &&
 978           ! test_have_prereq "$test_prereq"
 979        then
 980                to_skip=t
 981
 982                of_prereq=
 983                if test "$missing_prereq" != "$test_prereq"
 984                then
 985                        of_prereq=" of $test_prereq"
 986                fi
 987                skipped_reason="missing $missing_prereq${of_prereq}"
 988        fi
 989        if test -z "$to_skip" && test -n "$run_list" &&
 990                ! match_test_selector_list '--run' $test_count "$run_list"
 991        then
 992                to_skip=t
 993                skipped_reason="--run"
 994        fi
 995
 996        case "$to_skip" in
 997        t)
 998                if test -n "$write_junit_xml"
 999                then
1000                        message="$(xml_attr_encode "$skipped_reason")"
1001                        write_junit_xml_testcase "$1" \
1002                                "      <skipped message=\"$message\" />"
1003                fi
1004
1005                say_color skip >&3 "skipping test: $@"
1006                say_color skip "ok $test_count # skip $1 ($skipped_reason)"
1007                : true
1008                ;;
1009        *)
1010                false
1011                ;;
1012        esac
1013}
1014
1015# stub; perf-lib overrides it
1016test_at_end_hook_ () {
1017        :
1018}
1019
1020write_junit_xml () {
1021        case "$1" in
1022        --truncate)
1023                >"$junit_xml_path"
1024                junit_have_testcase=
1025                shift
1026                ;;
1027        esac
1028        printf '%s\n' "$@" >>"$junit_xml_path"
1029}
1030
1031xml_attr_encode () {
1032        printf '%s\n' "$@" | test-tool xml-encode
1033}
1034
1035write_junit_xml_testcase () {
1036        junit_attrs="name=\"$(xml_attr_encode "$this_test.$test_count $1")\""
1037        shift
1038        junit_attrs="$junit_attrs classname=\"$this_test\""
1039        junit_attrs="$junit_attrs time=\"$(test-tool \
1040                date getnanos $junit_start)\""
1041        write_junit_xml "$(printf '%s\n' \
1042                "    <testcase $junit_attrs>" "$@" "    </testcase>")"
1043        junit_have_testcase=t
1044}
1045
1046test_done () {
1047        GIT_EXIT_OK=t
1048
1049        if test -n "$write_junit_xml" && test -n "$junit_xml_path"
1050        then
1051                test -n "$junit_have_testcase" || {
1052                        junit_start=$(test-tool date getnanos)
1053                        write_junit_xml_testcase "all tests skipped"
1054                }
1055
1056                # adjust the overall time
1057                junit_time=$(test-tool date getnanos $junit_suite_start)
1058                sed "s/<testsuite [^>]*/& time=\"$junit_time\"/" \
1059                        <"$junit_xml_path" >"$junit_xml_path.new"
1060                mv "$junit_xml_path.new" "$junit_xml_path"
1061
1062                write_junit_xml "  </testsuite>" "</testsuites>"
1063        fi
1064
1065        if test -z "$HARNESS_ACTIVE"
1066        then
1067                mkdir -p "$TEST_RESULTS_DIR"
1068
1069                cat >"$TEST_RESULTS_BASE.counts" <<-EOF
1070                total $test_count
1071                success $test_success
1072                fixed $test_fixed
1073                broken $test_broken
1074                failed $test_failure
1075
1076                EOF
1077        fi
1078
1079        if test "$test_fixed" != 0
1080        then
1081                say_color error "# $test_fixed known breakage(s) vanished; please update test(s)"
1082        fi
1083        if test "$test_broken" != 0
1084        then
1085                say_color warn "# still have $test_broken known breakage(s)"
1086        fi
1087        if test "$test_broken" != 0 || test "$test_fixed" != 0
1088        then
1089                test_remaining=$(( $test_count - $test_broken - $test_fixed ))
1090                msg="remaining $test_remaining test(s)"
1091        else
1092                test_remaining=$test_count
1093                msg="$test_count test(s)"
1094        fi
1095        case "$test_failure" in
1096        0)
1097                if test $test_external_has_tap -eq 0
1098                then
1099                        if test $test_remaining -gt 0
1100                        then
1101                                say_color pass "# passed all $msg"
1102                        fi
1103
1104                        # Maybe print SKIP message
1105                        test -z "$skip_all" || skip_all="# SKIP $skip_all"
1106                        case "$test_count" in
1107                        0)
1108                                say "1..$test_count${skip_all:+ $skip_all}"
1109                                ;;
1110                        *)
1111                                test -z "$skip_all" ||
1112                                say_color warn "$skip_all"
1113                                say "1..$test_count"
1114                                ;;
1115                        esac
1116                fi
1117
1118                if test -z "$debug"
1119                then
1120                        test -d "$TRASH_DIRECTORY" ||
1121                        error "Tests passed but trash directory already removed before test cleanup; aborting"
1122
1123                        cd "$TRASH_DIRECTORY/.." &&
1124                        rm -fr "$TRASH_DIRECTORY" || {
1125                                # try again in a bit
1126                                sleep 5;
1127                                rm -fr "$TRASH_DIRECTORY"
1128                        } ||
1129                        error "Tests passed but test cleanup failed; aborting"
1130                fi
1131                test_at_end_hook_
1132
1133                exit 0 ;;
1134
1135        *)
1136                if test $test_external_has_tap -eq 0
1137                then
1138                        say_color error "# failed $test_failure among $msg"
1139                        say "1..$test_count"
1140                fi
1141
1142                exit 1 ;;
1143
1144        esac
1145}
1146
1147if test -n "$valgrind"
1148then
1149        make_symlink () {
1150                test -h "$2" &&
1151                test "$1" = "$(readlink "$2")" || {
1152                        # be super paranoid
1153                        if mkdir "$2".lock
1154                        then
1155                                rm -f "$2" &&
1156                                ln -s "$1" "$2" &&
1157                                rm -r "$2".lock
1158                        else
1159                                while test -d "$2".lock
1160                                do
1161                                        say "Waiting for lock on $2."
1162                                        sleep 1
1163                                done
1164                        fi
1165                }
1166        }
1167
1168        make_valgrind_symlink () {
1169                # handle only executables, unless they are shell libraries that
1170                # need to be in the exec-path.
1171                test -x "$1" ||
1172                test "# " = "$(test_copy_bytes 2 <"$1")" ||
1173                return;
1174
1175                base=$(basename "$1")
1176                case "$base" in
1177                test-*)
1178                        symlink_target="$GIT_BUILD_DIR/t/helper/$base"
1179                        ;;
1180                *)
1181                        symlink_target="$GIT_BUILD_DIR/$base"
1182                        ;;
1183                esac
1184                # do not override scripts
1185                if test -x "$symlink_target" &&
1186                    test ! -d "$symlink_target" &&
1187                    test "#!" != "$(test_copy_bytes 2 <"$symlink_target")"
1188                then
1189                        symlink_target=../valgrind.sh
1190                fi
1191                case "$base" in
1192                *.sh|*.perl)
1193                        symlink_target=../unprocessed-script
1194                esac
1195                # create the link, or replace it if it is out of date
1196                make_symlink "$symlink_target" "$GIT_VALGRIND/bin/$base" || exit
1197        }
1198
1199        # override all git executables in TEST_DIRECTORY/..
1200        GIT_VALGRIND=$TEST_DIRECTORY/valgrind
1201        mkdir -p "$GIT_VALGRIND"/bin
1202        for file in $GIT_BUILD_DIR/git* $GIT_BUILD_DIR/t/helper/test-*
1203        do
1204                make_valgrind_symlink $file
1205        done
1206        # special-case the mergetools loadables
1207        make_symlink "$GIT_BUILD_DIR"/mergetools "$GIT_VALGRIND/bin/mergetools"
1208        OLDIFS=$IFS
1209        IFS=:
1210        for path in $PATH
1211        do
1212                ls "$path"/git-* 2> /dev/null |
1213                while read file
1214                do
1215                        make_valgrind_symlink "$file"
1216                done
1217        done
1218        IFS=$OLDIFS
1219        PATH=$GIT_VALGRIND/bin:$PATH
1220        GIT_EXEC_PATH=$GIT_VALGRIND/bin
1221        export GIT_VALGRIND
1222        GIT_VALGRIND_MODE="$valgrind"
1223        export GIT_VALGRIND_MODE
1224        GIT_VALGRIND_ENABLED=t
1225        test -n "$valgrind_only" && GIT_VALGRIND_ENABLED=
1226        export GIT_VALGRIND_ENABLED
1227elif test -n "$GIT_TEST_INSTALLED"
1228then
1229        GIT_EXEC_PATH=$($GIT_TEST_INSTALLED/git --exec-path)  ||
1230        error "Cannot run git from $GIT_TEST_INSTALLED."
1231        PATH=$GIT_TEST_INSTALLED:$GIT_BUILD_DIR/t/helper:$PATH
1232        GIT_EXEC_PATH=${GIT_TEST_EXEC_PATH:-$GIT_EXEC_PATH}
1233else # normal case, use ../bin-wrappers only unless $with_dashes:
1234        if test -n "$no_bin_wrappers"
1235        then
1236                with_dashes=t
1237        else
1238                git_bin_dir="$GIT_BUILD_DIR/bin-wrappers"
1239                if ! test -x "$git_bin_dir/git"
1240                then
1241                        if test -z "$with_dashes"
1242                        then
1243                                say "$git_bin_dir/git is not executable; using GIT_EXEC_PATH"
1244                        fi
1245                        with_dashes=t
1246                fi
1247                PATH="$git_bin_dir:$PATH"
1248        fi
1249        GIT_EXEC_PATH=$GIT_BUILD_DIR
1250        if test -n "$with_dashes"
1251        then
1252                PATH="$GIT_BUILD_DIR:$GIT_BUILD_DIR/t/helper:$PATH"
1253        fi
1254fi
1255GIT_TEMPLATE_DIR="$GIT_BUILD_DIR"/templates/blt
1256GIT_CONFIG_NOSYSTEM=1
1257GIT_ATTR_NOSYSTEM=1
1258export PATH GIT_EXEC_PATH GIT_TEMPLATE_DIR GIT_CONFIG_NOSYSTEM GIT_ATTR_NOSYSTEM
1259
1260if test -z "$GIT_TEST_CMP"
1261then
1262        if test -n "$GIT_TEST_CMP_USE_COPIED_CONTEXT"
1263        then
1264                GIT_TEST_CMP="$DIFF -c"
1265        else
1266                GIT_TEST_CMP="$DIFF -u"
1267        fi
1268fi
1269
1270GITPERLLIB="$GIT_BUILD_DIR"/perl/build/lib
1271export GITPERLLIB
1272test -d "$GIT_BUILD_DIR"/templates/blt || {
1273        error "You haven't built things yet, have you?"
1274}
1275
1276if ! test -x "$GIT_BUILD_DIR"/t/helper/test-tool$X
1277then
1278        echo >&2 'You need to build test-tool:'
1279        echo >&2 'Run "make t/helper/test-tool" in the source (toplevel) directory'
1280        exit 1
1281fi
1282
1283# Test repository
1284rm -fr "$TRASH_DIRECTORY" || {
1285        GIT_EXIT_OK=t
1286        echo >&5 "FATAL: Cannot prepare test area"
1287        exit 1
1288}
1289
1290HOME="$TRASH_DIRECTORY"
1291GNUPGHOME="$HOME/gnupg-home-not-used"
1292export HOME GNUPGHOME
1293
1294if test -z "$TEST_NO_CREATE_REPO"
1295then
1296        test_create_repo "$TRASH_DIRECTORY"
1297else
1298        mkdir -p "$TRASH_DIRECTORY"
1299fi
1300
1301# Use -P to resolve symlinks in our working directory so that the cwd
1302# in subprocesses like git equals our $PWD (for pathname comparisons).
1303cd -P "$TRASH_DIRECTORY" || exit 1
1304
1305this_test=${0##*/}
1306this_test=${this_test%%-*}
1307if match_pattern_list "$this_test" $GIT_SKIP_TESTS
1308then
1309        say_color info >&3 "skipping test $this_test altogether"
1310        skip_all="skip all tests in $this_test"
1311        test_done
1312fi
1313
1314if test -n "$write_junit_xml"
1315then
1316        junit_xml_dir="$TEST_OUTPUT_DIRECTORY/out"
1317        mkdir -p "$junit_xml_dir"
1318        junit_xml_base=${0##*/}
1319        junit_xml_path="$junit_xml_dir/TEST-${junit_xml_base%.sh}.xml"
1320        junit_attrs="name=\"${junit_xml_base%.sh}\""
1321        junit_attrs="$junit_attrs timestamp=\"$(TZ=UTC \
1322                date +%Y-%m-%dT%H:%M:%S)\""
1323        write_junit_xml --truncate "<testsuites>" "  <testsuite $junit_attrs>"
1324        junit_suite_start=$(test-tool date getnanos)
1325        if test -n "$GIT_TEST_TEE_OUTPUT_FILE"
1326        then
1327                GIT_TEST_TEE_OFFSET=0
1328        fi
1329fi
1330
1331# Provide an implementation of the 'yes' utility
1332yes () {
1333        if test $# = 0
1334        then
1335                y=y
1336        else
1337                y="$*"
1338        fi
1339
1340        i=0
1341        while test $i -lt 99
1342        do
1343                echo "$y"
1344                i=$(($i+1))
1345        done
1346}
1347
1348# Fix some commands on Windows
1349uname_s=$(uname -s)
1350case $uname_s in
1351*MINGW*)
1352        # Windows has its own (incompatible) sort and find
1353        sort () {
1354                /usr/bin/sort "$@"
1355        }
1356        find () {
1357                /usr/bin/find "$@"
1358        }
1359        # git sees Windows-style pwd
1360        pwd () {
1361                builtin pwd -W
1362        }
1363        # no POSIX permissions
1364        # backslashes in pathspec are converted to '/'
1365        # exec does not inherit the PID
1366        test_set_prereq MINGW
1367        test_set_prereq NATIVE_CRLF
1368        test_set_prereq SED_STRIPS_CR
1369        test_set_prereq GREP_STRIPS_CR
1370        GIT_TEST_CMP=mingw_test_cmp
1371        ;;
1372*CYGWIN*)
1373        test_set_prereq POSIXPERM
1374        test_set_prereq EXECKEEPSPID
1375        test_set_prereq CYGWIN
1376        test_set_prereq SED_STRIPS_CR
1377        test_set_prereq GREP_STRIPS_CR
1378        ;;
1379*)
1380        test_set_prereq POSIXPERM
1381        test_set_prereq BSLASHPSPEC
1382        test_set_prereq EXECKEEPSPID
1383        ;;
1384esac
1385
1386( COLUMNS=1 && test $COLUMNS = 1 ) && test_set_prereq COLUMNS_CAN_BE_1
1387test -z "$NO_PERL" && test_set_prereq PERL
1388test -z "$NO_PTHREADS" && test_set_prereq PTHREADS
1389test -z "$NO_PYTHON" && test_set_prereq PYTHON
1390test -n "$USE_LIBPCRE1$USE_LIBPCRE2" && test_set_prereq PCRE
1391test -n "$USE_LIBPCRE1" && test_set_prereq LIBPCRE1
1392test -n "$USE_LIBPCRE2" && test_set_prereq LIBPCRE2
1393test -z "$NO_GETTEXT" && test_set_prereq GETTEXT
1394
1395if test -n "$GIT_TEST_GETTEXT_POISON_ORIG"
1396then
1397        GIT_TEST_GETTEXT_POISON=$GIT_TEST_GETTEXT_POISON_ORIG
1398        unset GIT_TEST_GETTEXT_POISON_ORIG
1399fi
1400
1401# Can we rely on git's output in the C locale?
1402if test -z "$GIT_TEST_GETTEXT_POISON"
1403then
1404        test_set_prereq C_LOCALE_OUTPUT
1405fi
1406
1407if test -z "$GIT_TEST_CHECK_CACHE_TREE"
1408then
1409        GIT_TEST_CHECK_CACHE_TREE=true
1410        export GIT_TEST_CHECK_CACHE_TREE
1411fi
1412
1413test_lazy_prereq PIPE '
1414        # test whether the filesystem supports FIFOs
1415        test_have_prereq !MINGW,!CYGWIN &&
1416        rm -f testfifo && mkfifo testfifo
1417'
1418
1419test_lazy_prereq SYMLINKS '
1420        # test whether the filesystem supports symbolic links
1421        ln -s x y && test -h y
1422'
1423
1424test_lazy_prereq FILEMODE '
1425        test "$(git config --bool core.filemode)" = true
1426'
1427
1428test_lazy_prereq CASE_INSENSITIVE_FS '
1429        echo good >CamelCase &&
1430        echo bad >camelcase &&
1431        test "$(cat CamelCase)" != good
1432'
1433
1434test_lazy_prereq FUNNYNAMES '
1435        test_have_prereq !MINGW &&
1436        touch -- \
1437                "FUNNYNAMES tab embedded" \
1438                "FUNNYNAMES \"quote embedded\"" \
1439                "FUNNYNAMES newline
1440embedded" 2>/dev/null &&
1441        rm -- \
1442                "FUNNYNAMES tab embedded" \
1443                "FUNNYNAMES \"quote embedded\"" \
1444                "FUNNYNAMES newline
1445embedded" 2>/dev/null
1446'
1447
1448test_lazy_prereq UTF8_NFD_TO_NFC '
1449        # check whether FS converts nfd unicode to nfc
1450        auml=$(printf "\303\244")
1451        aumlcdiar=$(printf "\141\314\210")
1452        >"$auml" &&
1453        test -f "$aumlcdiar"
1454'
1455
1456test_lazy_prereq AUTOIDENT '
1457        sane_unset GIT_AUTHOR_NAME &&
1458        sane_unset GIT_AUTHOR_EMAIL &&
1459        git var GIT_AUTHOR_IDENT
1460'
1461
1462test_lazy_prereq EXPENSIVE '
1463        test -n "$GIT_TEST_LONG"
1464'
1465
1466test_lazy_prereq EXPENSIVE_ON_WINDOWS '
1467        test_have_prereq EXPENSIVE || test_have_prereq !MINGW,!CYGWIN
1468'
1469
1470test_lazy_prereq USR_BIN_TIME '
1471        test -x /usr/bin/time
1472'
1473
1474test_lazy_prereq NOT_ROOT '
1475        uid=$(id -u) &&
1476        test "$uid" != 0
1477'
1478
1479test_lazy_prereq JGIT '
1480        type jgit
1481'
1482
1483# SANITY is about "can you correctly predict what the filesystem would
1484# do by only looking at the permission bits of the files and
1485# directories?"  A typical example of !SANITY is running the test
1486# suite as root, where a test may expect "chmod -r file && cat file"
1487# to fail because file is supposed to be unreadable after a successful
1488# chmod.  In an environment (i.e. combination of what filesystem is
1489# being used and who is running the tests) that lacks SANITY, you may
1490# be able to delete or create a file when the containing directory
1491# doesn't have write permissions, or access a file even if the
1492# containing directory doesn't have read or execute permissions.
1493
1494test_lazy_prereq SANITY '
1495        mkdir SANETESTD.1 SANETESTD.2 &&
1496
1497        chmod +w SANETESTD.1 SANETESTD.2 &&
1498        >SANETESTD.1/x 2>SANETESTD.2/x &&
1499        chmod -w SANETESTD.1 &&
1500        chmod -r SANETESTD.1/x &&
1501        chmod -rx SANETESTD.2 ||
1502        BUG "cannot prepare SANETESTD"
1503
1504        ! test -r SANETESTD.1/x &&
1505        ! rm SANETESTD.1/x && ! test -f SANETESTD.2/x
1506        status=$?
1507
1508        chmod +rwx SANETESTD.1 SANETESTD.2 &&
1509        rm -rf SANETESTD.1 SANETESTD.2 ||
1510        BUG "cannot clean SANETESTD"
1511        return $status
1512'
1513
1514test FreeBSD != $uname_s || GIT_UNZIP=${GIT_UNZIP:-/usr/local/bin/unzip}
1515GIT_UNZIP=${GIT_UNZIP:-unzip}
1516test_lazy_prereq UNZIP '
1517        "$GIT_UNZIP" -v
1518        test $? -ne 127
1519'
1520
1521run_with_limited_cmdline () {
1522        (ulimit -s 128 && "$@")
1523}
1524
1525test_lazy_prereq CMDLINE_LIMIT '
1526        test_have_prereq !MINGW,!CYGWIN &&
1527        run_with_limited_cmdline true
1528'
1529
1530run_with_limited_stack () {
1531        (ulimit -s 128 && "$@")
1532}
1533
1534test_lazy_prereq ULIMIT_STACK_SIZE '
1535        test_have_prereq !MINGW,!CYGWIN &&
1536        run_with_limited_stack true
1537'
1538
1539build_option () {
1540        git version --build-options |
1541        sed -ne "s/^$1: //p"
1542}
1543
1544test_lazy_prereq LONG_IS_64BIT '
1545        test 8 -le "$(build_option sizeof-long)"
1546'
1547
1548test_lazy_prereq TIME_IS_64BIT 'test-tool date is64bit'
1549test_lazy_prereq TIME_T_IS_64BIT 'test-tool date time_t-is64bit'
1550
1551test_lazy_prereq CURL '
1552        curl --version
1553'
1554
1555# SHA1 is a test if the hash algorithm in use is SHA-1.  This is both for tests
1556# which will not work with other hash algorithms and tests that work but don't
1557# test anything meaningful (e.g. special values which cause short collisions).
1558test_lazy_prereq SHA1 '
1559        test $(git hash-object /dev/null) = e69de29bb2d1d6434b8b29ae775ad8c2e48c5391
1560'
1561
1562test_lazy_prereq REBASE_P '
1563        test -z "$GIT_TEST_SKIP_REBASE_P"
1564'