t / test-lib-functions.shon commit Merge branch 'tr/diff-submodule-no-reuse-worktree' (481e6aa)
   1# Library of functions shared by all tests scripts, included by
   2# test-lib.sh.
   3#
   4# Copyright (c) 2005 Junio C Hamano
   5#
   6# This program is free software: you can redistribute it and/or modify
   7# it under the terms of the GNU General Public License as published by
   8# the Free Software Foundation, either version 2 of the License, or
   9# (at your option) any later version.
  10#
  11# This program is distributed in the hope that it will be useful,
  12# but WITHOUT ANY WARRANTY; without even the implied warranty of
  13# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
  14# GNU General Public License for more details.
  15#
  16# You should have received a copy of the GNU General Public License
  17# along with this program.  If not, see http://www.gnu.org/licenses/ .
  18
  19# The semantics of the editor variables are that of invoking
  20# sh -c "$EDITOR \"$@\"" files ...
  21#
  22# If our trash directory contains shell metacharacters, they will be
  23# interpreted if we just set $EDITOR directly, so do a little dance with
  24# environment variables to work around this.
  25#
  26# In particular, quoting isn't enough, as the path may contain the same quote
  27# that we're using.
  28test_set_editor () {
  29        FAKE_EDITOR="$1"
  30        export FAKE_EDITOR
  31        EDITOR='"$FAKE_EDITOR"'
  32        export EDITOR
  33}
  34
  35test_decode_color () {
  36        awk '
  37                function name(n) {
  38                        if (n == 0) return "RESET";
  39                        if (n == 1) return "BOLD";
  40                        if (n == 30) return "BLACK";
  41                        if (n == 31) return "RED";
  42                        if (n == 32) return "GREEN";
  43                        if (n == 33) return "YELLOW";
  44                        if (n == 34) return "BLUE";
  45                        if (n == 35) return "MAGENTA";
  46                        if (n == 36) return "CYAN";
  47                        if (n == 37) return "WHITE";
  48                        if (n == 40) return "BLACK";
  49                        if (n == 41) return "BRED";
  50                        if (n == 42) return "BGREEN";
  51                        if (n == 43) return "BYELLOW";
  52                        if (n == 44) return "BBLUE";
  53                        if (n == 45) return "BMAGENTA";
  54                        if (n == 46) return "BCYAN";
  55                        if (n == 47) return "BWHITE";
  56                }
  57                {
  58                        while (match($0, /\033\[[0-9;]*m/) != 0) {
  59                                printf "%s<", substr($0, 1, RSTART-1);
  60                                codes = substr($0, RSTART+2, RLENGTH-3);
  61                                if (length(codes) == 0)
  62                                        printf "%s", name(0)
  63                                else {
  64                                        n = split(codes, ary, ";");
  65                                        sep = "";
  66                                        for (i = 1; i <= n; i++) {
  67                                                printf "%s%s", sep, name(ary[i]);
  68                                                sep = ";"
  69                                        }
  70                                }
  71                                printf ">";
  72                                $0 = substr($0, RSTART + RLENGTH, length($0) - RSTART - RLENGTH + 1);
  73                        }
  74                        print
  75                }
  76        '
  77}
  78
  79nul_to_q () {
  80        perl -pe 'y/\000/Q/'
  81}
  82
  83q_to_nul () {
  84        perl -pe 'y/Q/\000/'
  85}
  86
  87q_to_cr () {
  88        tr Q '\015'
  89}
  90
  91q_to_tab () {
  92        tr Q '\011'
  93}
  94
  95qz_to_tab_space () {
  96        tr QZ '\011\040'
  97}
  98
  99append_cr () {
 100        sed -e 's/$/Q/' | tr Q '\015'
 101}
 102
 103remove_cr () {
 104        tr '\015' Q | sed -e 's/Q$//'
 105}
 106
 107# In some bourne shell implementations, the "unset" builtin returns
 108# nonzero status when a variable to be unset was not set in the first
 109# place.
 110#
 111# Use sane_unset when that should not be considered an error.
 112
 113sane_unset () {
 114        unset "$@"
 115        return 0
 116}
 117
 118test_tick () {
 119        if test -z "${test_tick+set}"
 120        then
 121                test_tick=1112911993
 122        else
 123                test_tick=$(($test_tick + 60))
 124        fi
 125        GIT_COMMITTER_DATE="$test_tick -0700"
 126        GIT_AUTHOR_DATE="$test_tick -0700"
 127        export GIT_COMMITTER_DATE GIT_AUTHOR_DATE
 128}
 129
 130# Stop execution and start a shell. This is useful for debugging tests and
 131# only makes sense together with "-v".
 132#
 133# Be sure to remove all invocations of this command before submitting.
 134
 135test_pause () {
 136        if test "$verbose" = t; then
 137                "$SHELL_PATH" <&6 >&3 2>&4
 138        else
 139                error >&5 "test_pause requires --verbose"
 140        fi
 141}
 142
 143# Call test_commit with the arguments "<message> [<file> [<contents> [<tag>]]]"
 144#
 145# This will commit a file with the given contents and the given commit
 146# message, and tag the resulting commit with the given tag name.
 147#
 148# <file>, <contents>, and <tag> all default to <message>.
 149
 150test_commit () {
 151        notick= &&
 152        signoff= &&
 153        while test $# != 0
 154        do
 155                case "$1" in
 156                --notick)
 157                        notick=yes
 158                        ;;
 159                --signoff)
 160                        signoff="$1"
 161                        ;;
 162                *)
 163                        break
 164                        ;;
 165                esac
 166                shift
 167        done &&
 168        file=${2:-"$1.t"} &&
 169        echo "${3-$1}" > "$file" &&
 170        git add "$file" &&
 171        if test -z "$notick"
 172        then
 173                test_tick
 174        fi &&
 175        git commit $signoff -m "$1" &&
 176        git tag "${4:-$1}"
 177}
 178
 179# Call test_merge with the arguments "<message> <commit>", where <commit>
 180# can be a tag pointing to the commit-to-merge.
 181
 182test_merge () {
 183        test_tick &&
 184        git merge -m "$1" "$2" &&
 185        git tag "$1"
 186}
 187
 188# This function helps systems where core.filemode=false is set.
 189# Use it instead of plain 'chmod +x' to set or unset the executable bit
 190# of a file in the working directory and add it to the index.
 191
 192test_chmod () {
 193        chmod "$@" &&
 194        git update-index --add "--chmod=$@"
 195}
 196
 197# Unset a configuration variable, but don't fail if it doesn't exist.
 198test_unconfig () {
 199        git config --unset-all "$@"
 200        config_status=$?
 201        case "$config_status" in
 202        5) # ok, nothing to unset
 203                config_status=0
 204                ;;
 205        esac
 206        return $config_status
 207}
 208
 209# Set git config, automatically unsetting it after the test is over.
 210test_config () {
 211        test_when_finished "test_unconfig '$1'" &&
 212        git config "$@"
 213}
 214
 215test_config_global () {
 216        test_when_finished "test_unconfig --global '$1'" &&
 217        git config --global "$@"
 218}
 219
 220write_script () {
 221        {
 222                echo "#!${2-"$SHELL_PATH"}" &&
 223                cat
 224        } >"$1" &&
 225        chmod +x "$1"
 226}
 227
 228# Use test_set_prereq to tell that a particular prerequisite is available.
 229# The prerequisite can later be checked for in two ways:
 230#
 231# - Explicitly using test_have_prereq.
 232#
 233# - Implicitly by specifying the prerequisite tag in the calls to
 234#   test_expect_{success,failure,code}.
 235#
 236# The single parameter is the prerequisite tag (a simple word, in all
 237# capital letters by convention).
 238
 239test_set_prereq () {
 240        satisfied_prereq="$satisfied_prereq$1 "
 241}
 242satisfied_prereq=" "
 243lazily_testable_prereq= lazily_tested_prereq=
 244
 245# Usage: test_lazy_prereq PREREQ 'script'
 246test_lazy_prereq () {
 247        lazily_testable_prereq="$lazily_testable_prereq$1 "
 248        eval test_prereq_lazily_$1=\$2
 249}
 250
 251test_run_lazy_prereq_ () {
 252        script='
 253mkdir -p "$TRASH_DIRECTORY/prereq-test-dir" &&
 254(
 255        cd "$TRASH_DIRECTORY/prereq-test-dir" &&'"$2"'
 256)'
 257        say >&3 "checking prerequisite: $1"
 258        say >&3 "$script"
 259        test_eval_ "$script"
 260        eval_ret=$?
 261        rm -rf "$TRASH_DIRECTORY/prereq-test-dir"
 262        if test "$eval_ret" = 0; then
 263                say >&3 "prerequisite $1 ok"
 264        else
 265                say >&3 "prerequisite $1 not satisfied"
 266        fi
 267        return $eval_ret
 268}
 269
 270test_have_prereq () {
 271        # prerequisites can be concatenated with ','
 272        save_IFS=$IFS
 273        IFS=,
 274        set -- $*
 275        IFS=$save_IFS
 276
 277        total_prereq=0
 278        ok_prereq=0
 279        missing_prereq=
 280
 281        for prerequisite
 282        do
 283                case "$prerequisite" in
 284                !*)
 285                        negative_prereq=t
 286                        prerequisite=${prerequisite#!}
 287                        ;;
 288                *)
 289                        negative_prereq=
 290                esac
 291
 292                case " $lazily_tested_prereq " in
 293                *" $prerequisite "*)
 294                        ;;
 295                *)
 296                        case " $lazily_testable_prereq " in
 297                        *" $prerequisite "*)
 298                                eval "script=\$test_prereq_lazily_$prerequisite" &&
 299                                if test_run_lazy_prereq_ "$prerequisite" "$script"
 300                                then
 301                                        test_set_prereq $prerequisite
 302                                fi
 303                                lazily_tested_prereq="$lazily_tested_prereq$prerequisite "
 304                        esac
 305                        ;;
 306                esac
 307
 308                total_prereq=$(($total_prereq + 1))
 309                case "$satisfied_prereq" in
 310                *" $prerequisite "*)
 311                        satisfied_this_prereq=t
 312                        ;;
 313                *)
 314                        satisfied_this_prereq=
 315                esac
 316
 317                case "$satisfied_this_prereq,$negative_prereq" in
 318                t,|,t)
 319                        ok_prereq=$(($ok_prereq + 1))
 320                        ;;
 321                *)
 322                        # Keep a list of missing prerequisites; restore
 323                        # the negative marker if necessary.
 324                        prerequisite=${negative_prereq:+!}$prerequisite
 325                        if test -z "$missing_prereq"
 326                        then
 327                                missing_prereq=$prerequisite
 328                        else
 329                                missing_prereq="$prerequisite,$missing_prereq"
 330                        fi
 331                esac
 332        done
 333
 334        test $total_prereq = $ok_prereq
 335}
 336
 337test_declared_prereq () {
 338        case ",$test_prereq," in
 339        *,$1,*)
 340                return 0
 341                ;;
 342        esac
 343        return 1
 344}
 345
 346test_expect_failure () {
 347        test_start_
 348        test "$#" = 3 && { test_prereq=$1; shift; } || test_prereq=
 349        test "$#" = 2 ||
 350        error "bug in the test script: not 2 or 3 parameters to test-expect-failure"
 351        export test_prereq
 352        if ! test_skip "$@"
 353        then
 354                say >&3 "checking known breakage: $2"
 355                if test_run_ "$2" expecting_failure
 356                then
 357                        test_known_broken_ok_ "$1"
 358                else
 359                        test_known_broken_failure_ "$1"
 360                fi
 361        fi
 362        test_finish_
 363}
 364
 365test_expect_success () {
 366        test_start_
 367        test "$#" = 3 && { test_prereq=$1; shift; } || test_prereq=
 368        test "$#" = 2 ||
 369        error "bug in the test script: not 2 or 3 parameters to test-expect-success"
 370        export test_prereq
 371        if ! test_skip "$@"
 372        then
 373                say >&3 "expecting success: $2"
 374                if test_run_ "$2"
 375                then
 376                        test_ok_ "$1"
 377                else
 378                        test_failure_ "$@"
 379                fi
 380        fi
 381        test_finish_
 382}
 383
 384# test_external runs external test scripts that provide continuous
 385# test output about their progress, and succeeds/fails on
 386# zero/non-zero exit code.  It outputs the test output on stdout even
 387# in non-verbose mode, and announces the external script with "# run
 388# <n>: ..." before running it.  When providing relative paths, keep in
 389# mind that all scripts run in "trash directory".
 390# Usage: test_external description command arguments...
 391# Example: test_external 'Perl API' perl ../path/to/test.pl
 392test_external () {
 393        test "$#" = 4 && { test_prereq=$1; shift; } || test_prereq=
 394        test "$#" = 3 ||
 395        error >&5 "bug in the test script: not 3 or 4 parameters to test_external"
 396        descr="$1"
 397        shift
 398        export test_prereq
 399        if ! test_skip "$descr" "$@"
 400        then
 401                # Announce the script to reduce confusion about the
 402                # test output that follows.
 403                say_color "" "# run $test_count: $descr ($*)"
 404                # Export TEST_DIRECTORY, TRASH_DIRECTORY and GIT_TEST_LONG
 405                # to be able to use them in script
 406                export TEST_DIRECTORY TRASH_DIRECTORY GIT_TEST_LONG
 407                # Run command; redirect its stderr to &4 as in
 408                # test_run_, but keep its stdout on our stdout even in
 409                # non-verbose mode.
 410                "$@" 2>&4
 411                if [ "$?" = 0 ]
 412                then
 413                        if test $test_external_has_tap -eq 0; then
 414                                test_ok_ "$descr"
 415                        else
 416                                say_color "" "# test_external test $descr was ok"
 417                                test_success=$(($test_success + 1))
 418                        fi
 419                else
 420                        if test $test_external_has_tap -eq 0; then
 421                                test_failure_ "$descr" "$@"
 422                        else
 423                                say_color error "# test_external test $descr failed: $@"
 424                                test_failure=$(($test_failure + 1))
 425                        fi
 426                fi
 427        fi
 428}
 429
 430# Like test_external, but in addition tests that the command generated
 431# no output on stderr.
 432test_external_without_stderr () {
 433        # The temporary file has no (and must have no) security
 434        # implications.
 435        tmp=${TMPDIR:-/tmp}
 436        stderr="$tmp/git-external-stderr.$$.tmp"
 437        test_external "$@" 4> "$stderr"
 438        [ -f "$stderr" ] || error "Internal error: $stderr disappeared."
 439        descr="no stderr: $1"
 440        shift
 441        say >&3 "# expecting no stderr from previous command"
 442        if [ ! -s "$stderr" ]; then
 443                rm "$stderr"
 444
 445                if test $test_external_has_tap -eq 0; then
 446                        test_ok_ "$descr"
 447                else
 448                        say_color "" "# test_external_without_stderr test $descr was ok"
 449                        test_success=$(($test_success + 1))
 450                fi
 451        else
 452                if [ "$verbose" = t ]; then
 453                        output=`echo; echo "# Stderr is:"; cat "$stderr"`
 454                else
 455                        output=
 456                fi
 457                # rm first in case test_failure exits.
 458                rm "$stderr"
 459                if test $test_external_has_tap -eq 0; then
 460                        test_failure_ "$descr" "$@" "$output"
 461                else
 462                        say_color error "# test_external_without_stderr test $descr failed: $@: $output"
 463                        test_failure=$(($test_failure + 1))
 464                fi
 465        fi
 466}
 467
 468# debugging-friendly alternatives to "test [-f|-d|-e]"
 469# The commands test the existence or non-existence of $1. $2 can be
 470# given to provide a more precise diagnosis.
 471test_path_is_file () {
 472        if ! [ -f "$1" ]
 473        then
 474                echo "File $1 doesn't exist. $*"
 475                false
 476        fi
 477}
 478
 479test_path_is_dir () {
 480        if ! [ -d "$1" ]
 481        then
 482                echo "Directory $1 doesn't exist. $*"
 483                false
 484        fi
 485}
 486
 487test_path_is_missing () {
 488        if [ -e "$1" ]
 489        then
 490                echo "Path exists:"
 491                ls -ld "$1"
 492                if [ $# -ge 1 ]; then
 493                        echo "$*"
 494                fi
 495                false
 496        fi
 497}
 498
 499# test_line_count checks that a file has the number of lines it
 500# ought to. For example:
 501#
 502#       test_expect_success 'produce exactly one line of output' '
 503#               do something >output &&
 504#               test_line_count = 1 output
 505#       '
 506#
 507# is like "test $(wc -l <output) = 1" except that it passes the
 508# output through when the number of lines is wrong.
 509
 510test_line_count () {
 511        if test $# != 3
 512        then
 513                error "bug in the test script: not 3 parameters to test_line_count"
 514        elif ! test $(wc -l <"$3") "$1" "$2"
 515        then
 516                echo "test_line_count: line count for $3 !$1 $2"
 517                cat "$3"
 518                return 1
 519        fi
 520}
 521
 522# This is not among top-level (test_expect_success | test_expect_failure)
 523# but is a prefix that can be used in the test script, like:
 524#
 525#       test_expect_success 'complain and die' '
 526#           do something &&
 527#           do something else &&
 528#           test_must_fail git checkout ../outerspace
 529#       '
 530#
 531# Writing this as "! git checkout ../outerspace" is wrong, because
 532# the failure could be due to a segv.  We want a controlled failure.
 533
 534test_must_fail () {
 535        "$@"
 536        exit_code=$?
 537        if test $exit_code = 0; then
 538                echo >&2 "test_must_fail: command succeeded: $*"
 539                return 1
 540        elif test $exit_code -gt 129 -a $exit_code -le 192; then
 541                echo >&2 "test_must_fail: died by signal: $*"
 542                return 1
 543        elif test $exit_code = 127; then
 544                echo >&2 "test_must_fail: command not found: $*"
 545                return 1
 546        elif test $exit_code = 126; then
 547                echo >&2 "test_must_fail: valgrind error: $*"
 548                return 1
 549        fi
 550        return 0
 551}
 552
 553# Similar to test_must_fail, but tolerates success, too.  This is
 554# meant to be used in contexts like:
 555#
 556#       test_expect_success 'some command works without configuration' '
 557#               test_might_fail git config --unset all.configuration &&
 558#               do something
 559#       '
 560#
 561# Writing "git config --unset all.configuration || :" would be wrong,
 562# because we want to notice if it fails due to segv.
 563
 564test_might_fail () {
 565        "$@"
 566        exit_code=$?
 567        if test $exit_code -gt 129 -a $exit_code -le 192; then
 568                echo >&2 "test_might_fail: died by signal: $*"
 569                return 1
 570        elif test $exit_code = 127; then
 571                echo >&2 "test_might_fail: command not found: $*"
 572                return 1
 573        fi
 574        return 0
 575}
 576
 577# Similar to test_must_fail and test_might_fail, but check that a
 578# given command exited with a given exit code. Meant to be used as:
 579#
 580#       test_expect_success 'Merge with d/f conflicts' '
 581#               test_expect_code 1 git merge "merge msg" B master
 582#       '
 583
 584test_expect_code () {
 585        want_code=$1
 586        shift
 587        "$@"
 588        exit_code=$?
 589        if test $exit_code = $want_code
 590        then
 591                return 0
 592        fi
 593
 594        echo >&2 "test_expect_code: command exited with $exit_code, we wanted $want_code $*"
 595        return 1
 596}
 597
 598# test_cmp is a helper function to compare actual and expected output.
 599# You can use it like:
 600#
 601#       test_expect_success 'foo works' '
 602#               echo expected >expected &&
 603#               foo >actual &&
 604#               test_cmp expected actual
 605#       '
 606#
 607# This could be written as either "cmp" or "diff -u", but:
 608# - cmp's output is not nearly as easy to read as diff -u
 609# - not all diff versions understand "-u"
 610
 611test_cmp() {
 612        $GIT_TEST_CMP "$@"
 613}
 614
 615# Check if the file expected to be empty is indeed empty, and barfs
 616# otherwise.
 617
 618test_must_be_empty () {
 619        if test -s "$1"
 620        then
 621                echo "'$1' is not empty, it contains:"
 622                cat "$1"
 623                return 1
 624        fi
 625}
 626
 627# Tests that its two parameters refer to the same revision
 628test_cmp_rev () {
 629        git rev-parse --verify "$1" >expect.rev &&
 630        git rev-parse --verify "$2" >actual.rev &&
 631        test_cmp expect.rev actual.rev
 632}
 633
 634# Print a sequence of numbers or letters in increasing order.  This is
 635# similar to GNU seq(1), but the latter might not be available
 636# everywhere (and does not do letters).  It may be used like:
 637#
 638#       for i in `test_seq 100`; do
 639#               for j in `test_seq 10 20`; do
 640#                       for k in `test_seq a z`; do
 641#                               echo $i-$j-$k
 642#                       done
 643#               done
 644#       done
 645
 646test_seq () {
 647        case $# in
 648        1)      set 1 "$@" ;;
 649        2)      ;;
 650        *)      error "bug in the test script: not 1 or 2 parameters to test_seq" ;;
 651        esac
 652        perl -le 'print for $ARGV[0]..$ARGV[1]' -- "$@"
 653}
 654
 655# This function can be used to schedule some commands to be run
 656# unconditionally at the end of the test to restore sanity:
 657#
 658#       test_expect_success 'test core.capslock' '
 659#               git config core.capslock true &&
 660#               test_when_finished "git config --unset core.capslock" &&
 661#               hello world
 662#       '
 663#
 664# That would be roughly equivalent to
 665#
 666#       test_expect_success 'test core.capslock' '
 667#               git config core.capslock true &&
 668#               hello world
 669#               git config --unset core.capslock
 670#       '
 671#
 672# except that the greeting and config --unset must both succeed for
 673# the test to pass.
 674#
 675# Note that under --immediate mode, no clean-up is done to help diagnose
 676# what went wrong.
 677
 678test_when_finished () {
 679        test_cleanup="{ $*
 680                } && (exit \"\$eval_ret\"); eval_ret=\$?; $test_cleanup"
 681}
 682
 683# Most tests can use the created repository, but some may need to create more.
 684# Usage: test_create_repo <directory>
 685test_create_repo () {
 686        test "$#" = 1 ||
 687        error "bug in the test script: not 1 parameter to test-create-repo"
 688        repo="$1"
 689        mkdir -p "$repo"
 690        (
 691                cd "$repo" || error "Cannot setup test environment"
 692                "$GIT_EXEC_PATH/git-init" "--template=$GIT_BUILD_DIR/templates/blt/" >&3 2>&4 ||
 693                error "cannot run git init -- have you built things yet?"
 694                mv .git/hooks .git/hooks-disabled
 695        ) || exit
 696}
 697
 698# This function helps on symlink challenged file systems when it is not
 699# important that the file system entry is a symbolic link.
 700# Use test_ln_s_add instead of "ln -s x y && git add y" to add a
 701# symbolic link entry y to the index.
 702
 703test_ln_s_add () {
 704        if test_have_prereq SYMLINKS
 705        then
 706                ln -s "$1" "$2" &&
 707                git update-index --add "$2"
 708        else
 709                printf '%s' "$1" >"$2" &&
 710                ln_s_obj=$(git hash-object -w "$2") &&
 711                git update-index --add --cacheinfo 120000 $ln_s_obj "$2"
 712        fi
 713}
 714
 715perl () {
 716        command "$PERL_PATH" "$@"
 717}
 718
 719# Is the value one of the various ways to spell a boolean true/false?
 720test_normalize_bool () {
 721        git -c magic.variable="$1" config --bool magic.variable 2>/dev/null
 722}
 723
 724# Given a variable $1, normalize the value of it to one of "true",
 725# "false", or "auto" and store the result to it.
 726#
 727#     test_tristate GIT_TEST_HTTPD
 728#
 729# A variable set to an empty string is set to 'false'.
 730# A variable set to 'false' or 'auto' keeps its value.
 731# Anything else is set to 'true'.
 732# An unset variable defaults to 'auto'.
 733#
 734# The last rule is to allow people to set the variable to an empty
 735# string and export it to decline testing the particular feature
 736# for versions both before and after this change.  We used to treat
 737# both unset and empty variable as a signal for "do not test" and
 738# took any non-empty string as "please test".
 739
 740test_tristate () {
 741        if eval "test x\"\${$1+isset}\" = xisset"
 742        then
 743                # explicitly set
 744                eval "
 745                        case \"\$$1\" in
 746                        '')     $1=false ;;
 747                        auto)   ;;
 748                        *)      $1=\$(test_normalize_bool \$$1 || echo true) ;;
 749                        esac
 750                "
 751        else
 752                eval "$1=auto"
 753        fi
 754}
 755
 756# Exit the test suite, either by skipping all remaining tests or by
 757# exiting with an error. If "$1" is "auto", we then we assume we were
 758# opportunistically trying to set up some tests and we skip. If it is
 759# "true", then we report a failure.
 760#
 761# The error/skip message should be given by $2.
 762#
 763test_skip_or_die () {
 764        case "$1" in
 765        auto)
 766                skip_all=$2
 767                test_done
 768                ;;
 769        true)
 770                error "$2"
 771                ;;
 772        *)
 773                error "BUG: test tristate is '$1' (real error: $2)"
 774        esac
 775}
 776
 777# The following mingw_* functions obey POSIX shell syntax, but are actually
 778# bash scripts, and are meant to be used only with bash on Windows.
 779
 780# A test_cmp function that treats LF and CRLF equal and avoids to fork
 781# diff when possible.
 782mingw_test_cmp () {
 783        # Read text into shell variables and compare them. If the results
 784        # are different, use regular diff to report the difference.
 785        local test_cmp_a= test_cmp_b=
 786
 787        # When text came from stdin (one argument is '-') we must feed it
 788        # to diff.
 789        local stdin_for_diff=
 790
 791        # Since it is difficult to detect the difference between an
 792        # empty input file and a failure to read the files, we go straight
 793        # to diff if one of the inputs is empty.
 794        if test -s "$1" && test -s "$2"
 795        then
 796                # regular case: both files non-empty
 797                mingw_read_file_strip_cr_ test_cmp_a <"$1"
 798                mingw_read_file_strip_cr_ test_cmp_b <"$2"
 799        elif test -s "$1" && test "$2" = -
 800        then
 801                # read 2nd file from stdin
 802                mingw_read_file_strip_cr_ test_cmp_a <"$1"
 803                mingw_read_file_strip_cr_ test_cmp_b
 804                stdin_for_diff='<<<"$test_cmp_b"'
 805        elif test "$1" = - && test -s "$2"
 806        then
 807                # read 1st file from stdin
 808                mingw_read_file_strip_cr_ test_cmp_a
 809                mingw_read_file_strip_cr_ test_cmp_b <"$2"
 810                stdin_for_diff='<<<"$test_cmp_a"'
 811        fi
 812        test -n "$test_cmp_a" &&
 813        test -n "$test_cmp_b" &&
 814        test "$test_cmp_a" = "$test_cmp_b" ||
 815        eval "diff -u \"\$@\" $stdin_for_diff"
 816}
 817
 818# $1 is the name of the shell variable to fill in
 819mingw_read_file_strip_cr_ () {
 820        # Read line-wise using LF as the line separator
 821        # and use IFS to strip CR.
 822        local line
 823        while :
 824        do
 825                if IFS=$'\r' read -r -d $'\n' line
 826                then
 827                        # good
 828                        line=$line$'\n'
 829                else
 830                        # we get here at EOF, but also if the last line
 831                        # was not terminated by LF; in the latter case,
 832                        # some text was read
 833                        if test -z "$line"
 834                        then
 835                                # EOF, really
 836                                break
 837                        fi
 838                fi
 839                eval "$1=\$$1\$line"
 840        done
 841}