t / test-lib-functions.shon commit test: resurrect q_to_tab (329b26e)
   1#!/bin/sh
   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# The semantics of the editor variables are that of invoking
  19# sh -c "$EDITOR \"$@\"" files ...
  20#
  21# If our trash directory contains shell metacharacters, they will be
  22# interpreted if we just set $EDITOR directly, so do a little dance with
  23# environment variables to work around this.
  24#
  25# In particular, quoting isn't enough, as the path may contain the same quote
  26# that we're using.
  27test_set_editor () {
  28        FAKE_EDITOR="$1"
  29        export FAKE_EDITOR
  30        EDITOR='"$FAKE_EDITOR"'
  31        export EDITOR
  32}
  33
  34test_decode_color () {
  35        awk '
  36                function name(n) {
  37                        if (n == 0) return "RESET";
  38                        if (n == 1) return "BOLD";
  39                        if (n == 30) return "BLACK";
  40                        if (n == 31) return "RED";
  41                        if (n == 32) return "GREEN";
  42                        if (n == 33) return "YELLOW";
  43                        if (n == 34) return "BLUE";
  44                        if (n == 35) return "MAGENTA";
  45                        if (n == 36) return "CYAN";
  46                        if (n == 37) return "WHITE";
  47                        if (n == 40) return "BLACK";
  48                        if (n == 41) return "BRED";
  49                        if (n == 42) return "BGREEN";
  50                        if (n == 43) return "BYELLOW";
  51                        if (n == 44) return "BBLUE";
  52                        if (n == 45) return "BMAGENTA";
  53                        if (n == 46) return "BCYAN";
  54                        if (n == 47) return "BWHITE";
  55                }
  56                {
  57                        while (match($0, /\033\[[0-9;]*m/) != 0) {
  58                                printf "%s<", substr($0, 1, RSTART-1);
  59                                codes = substr($0, RSTART+2, RLENGTH-3);
  60                                if (length(codes) == 0)
  61                                        printf "%s", name(0)
  62                                else {
  63                                        n = split(codes, ary, ";");
  64                                        sep = "";
  65                                        for (i = 1; i <= n; i++) {
  66                                                printf "%s%s", sep, name(ary[i]);
  67                                                sep = ";"
  68                                        }
  69                                }
  70                                printf ">";
  71                                $0 = substr($0, RSTART + RLENGTH, length($0) - RSTART - RLENGTH + 1);
  72                        }
  73                        print
  74                }
  75        '
  76}
  77
  78nul_to_q () {
  79        "$PERL_PATH" -pe 'y/\000/Q/'
  80}
  81
  82q_to_nul () {
  83        "$PERL_PATH" -pe 'y/Q/\000/'
  84}
  85
  86q_to_cr () {
  87        tr Q '\015'
  88}
  89
  90q_to_tab () {
  91        tr Q '\011'
  92}
  93
  94qz_to_tab_space () {
  95        tr QZ '\011\040'
  96}
  97
  98append_cr () {
  99        sed -e 's/$/Q/' | tr Q '\015'
 100}
 101
 102remove_cr () {
 103        tr '\015' Q | sed -e 's/Q$//'
 104}
 105
 106# In some bourne shell implementations, the "unset" builtin returns
 107# nonzero status when a variable to be unset was not set in the first
 108# place.
 109#
 110# Use sane_unset when that should not be considered an error.
 111
 112sane_unset () {
 113        unset "$@"
 114        return 0
 115}
 116
 117test_tick () {
 118        if test -z "${test_tick+set}"
 119        then
 120                test_tick=1112911993
 121        else
 122                test_tick=$(($test_tick + 60))
 123        fi
 124        GIT_COMMITTER_DATE="$test_tick -0700"
 125        GIT_AUTHOR_DATE="$test_tick -0700"
 126        export GIT_COMMITTER_DATE GIT_AUTHOR_DATE
 127}
 128
 129# Stop execution and start a shell. This is useful for debugging tests and
 130# only makes sense together with "-v".
 131#
 132# Be sure to remove all invocations of this command before submitting.
 133
 134test_pause () {
 135        if test "$verbose" = t; then
 136                "$SHELL_PATH" <&6 >&3 2>&4
 137        else
 138                error >&5 "test_pause requires --verbose"
 139        fi
 140}
 141
 142# Call test_commit with the arguments "<message> [<file> [<contents>]]"
 143#
 144# This will commit a file with the given contents and the given commit
 145# message.  It will also add a tag with <message> as name.
 146#
 147# Both <file> and <contents> default to <message>.
 148
 149test_commit () {
 150        notick= &&
 151        signoff= &&
 152        while test $# != 0
 153        do
 154                case "$1" in
 155                --notick)
 156                        notick=yes
 157                        ;;
 158                --signoff)
 159                        signoff="$1"
 160                        ;;
 161                *)
 162                        break
 163                        ;;
 164                esac
 165                shift
 166        done &&
 167        file=${2:-"$1.t"} &&
 168        echo "${3-$1}" > "$file" &&
 169        git add "$file" &&
 170        if test -z "$notick"
 171        then
 172                test_tick
 173        fi &&
 174        git commit $signoff -m "$1" &&
 175        git tag "$1"
 176}
 177
 178# Call test_merge with the arguments "<message> <commit>", where <commit>
 179# can be a tag pointing to the commit-to-merge.
 180
 181test_merge () {
 182        test_tick &&
 183        git merge -m "$1" "$2" &&
 184        git tag "$1"
 185}
 186
 187# This function helps systems where core.filemode=false is set.
 188# Use it instead of plain 'chmod +x' to set or unset the executable bit
 189# of a file in the working directory and add it to the index.
 190
 191test_chmod () {
 192        chmod "$@" &&
 193        git update-index --add "--chmod=$@"
 194}
 195
 196# Unset a configuration variable, but don't fail if it doesn't exist.
 197test_unconfig () {
 198        git config --unset-all "$@"
 199        config_status=$?
 200        case "$config_status" in
 201        5) # ok, nothing to unset
 202                config_status=0
 203                ;;
 204        esac
 205        return $config_status
 206}
 207
 208# Set git config, automatically unsetting it after the test is over.
 209test_config () {
 210        test_when_finished "test_unconfig '$1'" &&
 211        git config "$@"
 212}
 213
 214test_config_global () {
 215        test_when_finished "test_unconfig --global '$1'" &&
 216        git config --global "$@"
 217}
 218
 219write_script () {
 220        {
 221                echo "#!${2-"$SHELL_PATH"}" &&
 222                cat
 223        } >"$1" &&
 224        chmod +x "$1"
 225}
 226
 227# Use test_set_prereq to tell that a particular prerequisite is available.
 228# The prerequisite can later be checked for in two ways:
 229#
 230# - Explicitly using test_have_prereq.
 231#
 232# - Implicitly by specifying the prerequisite tag in the calls to
 233#   test_expect_{success,failure,code}.
 234#
 235# The single parameter is the prerequisite tag (a simple word, in all
 236# capital letters by convention).
 237
 238test_set_prereq () {
 239        satisfied_prereq="$satisfied_prereq$1 "
 240}
 241satisfied_prereq=" "
 242lazily_testable_prereq= lazily_tested_prereq=
 243
 244# Usage: test_lazy_prereq PREREQ 'script'
 245test_lazy_prereq () {
 246        lazily_testable_prereq="$lazily_testable_prereq$1 "
 247        eval test_prereq_lazily_$1=\$2
 248}
 249
 250test_run_lazy_prereq_ () {
 251        script='
 252mkdir -p "$TRASH_DIRECTORY/prereq-test-dir" &&
 253(
 254        cd "$TRASH_DIRECTORY/prereq-test-dir" &&'"$2"'
 255)'
 256        say >&3 "checking prerequisite: $1"
 257        say >&3 "$script"
 258        test_eval_ "$script"
 259        eval_ret=$?
 260        rm -rf "$TRASH_DIRECTORY/prereq-test-dir"
 261        if test "$eval_ret" = 0; then
 262                say >&3 "prerequisite $1 ok"
 263        else
 264                say >&3 "prerequisite $1 not satisfied"
 265        fi
 266        return $eval_ret
 267}
 268
 269test_have_prereq () {
 270        # prerequisites can be concatenated with ','
 271        save_IFS=$IFS
 272        IFS=,
 273        set -- $*
 274        IFS=$save_IFS
 275
 276        total_prereq=0
 277        ok_prereq=0
 278        missing_prereq=
 279
 280        for prerequisite
 281        do
 282                case " $lazily_tested_prereq " in
 283                *" $prerequisite "*)
 284                        ;;
 285                *)
 286                        case " $lazily_testable_prereq " in
 287                        *" $prerequisite "*)
 288                                eval "script=\$test_prereq_lazily_$prerequisite" &&
 289                                if test_run_lazy_prereq_ "$prerequisite" "$script"
 290                                then
 291                                        test_set_prereq $prerequisite
 292                                fi
 293                                lazily_tested_prereq="$lazily_tested_prereq$prerequisite "
 294                        esac
 295                        ;;
 296                esac
 297
 298                total_prereq=$(($total_prereq + 1))
 299                case "$satisfied_prereq" in
 300                *" $prerequisite "*)
 301                        ok_prereq=$(($ok_prereq + 1))
 302                        ;;
 303                *)
 304                        # Keep a list of missing prerequisites
 305                        if test -z "$missing_prereq"
 306                        then
 307                                missing_prereq=$prerequisite
 308                        else
 309                                missing_prereq="$prerequisite,$missing_prereq"
 310                        fi
 311                esac
 312        done
 313
 314        test $total_prereq = $ok_prereq
 315}
 316
 317test_declared_prereq () {
 318        case ",$test_prereq," in
 319        *,$1,*)
 320                return 0
 321                ;;
 322        esac
 323        return 1
 324}
 325
 326test_expect_failure () {
 327        test "$#" = 3 && { test_prereq=$1; shift; } || test_prereq=
 328        test "$#" = 2 ||
 329        error "bug in the test script: not 2 or 3 parameters to test-expect-failure"
 330        export test_prereq
 331        if ! test_skip "$@"
 332        then
 333                say >&3 "checking known breakage: $2"
 334                if test_run_ "$2" expecting_failure
 335                then
 336                        test_known_broken_ok_ "$1"
 337                else
 338                        test_known_broken_failure_ "$1"
 339                fi
 340        fi
 341        echo >&3 ""
 342}
 343
 344test_expect_success () {
 345        test "$#" = 3 && { test_prereq=$1; shift; } || test_prereq=
 346        test "$#" = 2 ||
 347        error "bug in the test script: not 2 or 3 parameters to test-expect-success"
 348        export test_prereq
 349        if ! test_skip "$@"
 350        then
 351                say >&3 "expecting success: $2"
 352                if test_run_ "$2"
 353                then
 354                        test_ok_ "$1"
 355                else
 356                        test_failure_ "$@"
 357                fi
 358        fi
 359        echo >&3 ""
 360}
 361
 362# test_external runs external test scripts that provide continuous
 363# test output about their progress, and succeeds/fails on
 364# zero/non-zero exit code.  It outputs the test output on stdout even
 365# in non-verbose mode, and announces the external script with "# run
 366# <n>: ..." before running it.  When providing relative paths, keep in
 367# mind that all scripts run in "trash directory".
 368# Usage: test_external description command arguments...
 369# Example: test_external 'Perl API' perl ../path/to/test.pl
 370test_external () {
 371        test "$#" = 4 && { test_prereq=$1; shift; } || test_prereq=
 372        test "$#" = 3 ||
 373        error >&5 "bug in the test script: not 3 or 4 parameters to test_external"
 374        descr="$1"
 375        shift
 376        export test_prereq
 377        if ! test_skip "$descr" "$@"
 378        then
 379                # Announce the script to reduce confusion about the
 380                # test output that follows.
 381                say_color "" "# run $test_count: $descr ($*)"
 382                # Export TEST_DIRECTORY, TRASH_DIRECTORY and GIT_TEST_LONG
 383                # to be able to use them in script
 384                export TEST_DIRECTORY TRASH_DIRECTORY GIT_TEST_LONG
 385                # Run command; redirect its stderr to &4 as in
 386                # test_run_, but keep its stdout on our stdout even in
 387                # non-verbose mode.
 388                "$@" 2>&4
 389                if [ "$?" = 0 ]
 390                then
 391                        if test $test_external_has_tap -eq 0; then
 392                                test_ok_ "$descr"
 393                        else
 394                                say_color "" "# test_external test $descr was ok"
 395                                test_success=$(($test_success + 1))
 396                        fi
 397                else
 398                        if test $test_external_has_tap -eq 0; then
 399                                test_failure_ "$descr" "$@"
 400                        else
 401                                say_color error "# test_external test $descr failed: $@"
 402                                test_failure=$(($test_failure + 1))
 403                        fi
 404                fi
 405        fi
 406}
 407
 408# Like test_external, but in addition tests that the command generated
 409# no output on stderr.
 410test_external_without_stderr () {
 411        # The temporary file has no (and must have no) security
 412        # implications.
 413        tmp=${TMPDIR:-/tmp}
 414        stderr="$tmp/git-external-stderr.$$.tmp"
 415        test_external "$@" 4> "$stderr"
 416        [ -f "$stderr" ] || error "Internal error: $stderr disappeared."
 417        descr="no stderr: $1"
 418        shift
 419        say >&3 "# expecting no stderr from previous command"
 420        if [ ! -s "$stderr" ]; then
 421                rm "$stderr"
 422
 423                if test $test_external_has_tap -eq 0; then
 424                        test_ok_ "$descr"
 425                else
 426                        say_color "" "# test_external_without_stderr test $descr was ok"
 427                        test_success=$(($test_success + 1))
 428                fi
 429        else
 430                if [ "$verbose" = t ]; then
 431                        output=`echo; echo "# Stderr is:"; cat "$stderr"`
 432                else
 433                        output=
 434                fi
 435                # rm first in case test_failure exits.
 436                rm "$stderr"
 437                if test $test_external_has_tap -eq 0; then
 438                        test_failure_ "$descr" "$@" "$output"
 439                else
 440                        say_color error "# test_external_without_stderr test $descr failed: $@: $output"
 441                        test_failure=$(($test_failure + 1))
 442                fi
 443        fi
 444}
 445
 446# debugging-friendly alternatives to "test [-f|-d|-e]"
 447# The commands test the existence or non-existence of $1. $2 can be
 448# given to provide a more precise diagnosis.
 449test_path_is_file () {
 450        if ! [ -f "$1" ]
 451        then
 452                echo "File $1 doesn't exist. $*"
 453                false
 454        fi
 455}
 456
 457test_path_is_dir () {
 458        if ! [ -d "$1" ]
 459        then
 460                echo "Directory $1 doesn't exist. $*"
 461                false
 462        fi
 463}
 464
 465test_path_is_missing () {
 466        if [ -e "$1" ]
 467        then
 468                echo "Path exists:"
 469                ls -ld "$1"
 470                if [ $# -ge 1 ]; then
 471                        echo "$*"
 472                fi
 473                false
 474        fi
 475}
 476
 477# test_line_count checks that a file has the number of lines it
 478# ought to. For example:
 479#
 480#       test_expect_success 'produce exactly one line of output' '
 481#               do something >output &&
 482#               test_line_count = 1 output
 483#       '
 484#
 485# is like "test $(wc -l <output) = 1" except that it passes the
 486# output through when the number of lines is wrong.
 487
 488test_line_count () {
 489        if test $# != 3
 490        then
 491                error "bug in the test script: not 3 parameters to test_line_count"
 492        elif ! test $(wc -l <"$3") "$1" "$2"
 493        then
 494                echo "test_line_count: line count for $3 !$1 $2"
 495                cat "$3"
 496                return 1
 497        fi
 498}
 499
 500# This is not among top-level (test_expect_success | test_expect_failure)
 501# but is a prefix that can be used in the test script, like:
 502#
 503#       test_expect_success 'complain and die' '
 504#           do something &&
 505#           do something else &&
 506#           test_must_fail git checkout ../outerspace
 507#       '
 508#
 509# Writing this as "! git checkout ../outerspace" is wrong, because
 510# the failure could be due to a segv.  We want a controlled failure.
 511
 512test_must_fail () {
 513        "$@"
 514        exit_code=$?
 515        if test $exit_code = 0; then
 516                echo >&2 "test_must_fail: command succeeded: $*"
 517                return 1
 518        elif test $exit_code -gt 129 -a $exit_code -le 192; then
 519                echo >&2 "test_must_fail: died by signal: $*"
 520                return 1
 521        elif test $exit_code = 127; then
 522                echo >&2 "test_must_fail: command not found: $*"
 523                return 1
 524        fi
 525        return 0
 526}
 527
 528# Similar to test_must_fail, but tolerates success, too.  This is
 529# meant to be used in contexts like:
 530#
 531#       test_expect_success 'some command works without configuration' '
 532#               test_might_fail git config --unset all.configuration &&
 533#               do something
 534#       '
 535#
 536# Writing "git config --unset all.configuration || :" would be wrong,
 537# because we want to notice if it fails due to segv.
 538
 539test_might_fail () {
 540        "$@"
 541        exit_code=$?
 542        if test $exit_code -gt 129 -a $exit_code -le 192; then
 543                echo >&2 "test_might_fail: died by signal: $*"
 544                return 1
 545        elif test $exit_code = 127; then
 546                echo >&2 "test_might_fail: command not found: $*"
 547                return 1
 548        fi
 549        return 0
 550}
 551
 552# Similar to test_must_fail and test_might_fail, but check that a
 553# given command exited with a given exit code. Meant to be used as:
 554#
 555#       test_expect_success 'Merge with d/f conflicts' '
 556#               test_expect_code 1 git merge "merge msg" B master
 557#       '
 558
 559test_expect_code () {
 560        want_code=$1
 561        shift
 562        "$@"
 563        exit_code=$?
 564        if test $exit_code = $want_code
 565        then
 566                return 0
 567        fi
 568
 569        echo >&2 "test_expect_code: command exited with $exit_code, we wanted $want_code $*"
 570        return 1
 571}
 572
 573# test_cmp is a helper function to compare actual and expected output.
 574# You can use it like:
 575#
 576#       test_expect_success 'foo works' '
 577#               echo expected >expected &&
 578#               foo >actual &&
 579#               test_cmp expected actual
 580#       '
 581#
 582# This could be written as either "cmp" or "diff -u", but:
 583# - cmp's output is not nearly as easy to read as diff -u
 584# - not all diff versions understand "-u"
 585
 586test_cmp() {
 587        $GIT_TEST_CMP "$@"
 588}
 589
 590# Print a sequence of numbers or letters in increasing order.  This is
 591# similar to GNU seq(1), but the latter might not be available
 592# everywhere (and does not do letters).  It may be used like:
 593#
 594#       for i in `test_seq 100`; do
 595#               for j in `test_seq 10 20`; do
 596#                       for k in `test_seq a z`; do
 597#                               echo $i-$j-$k
 598#                       done
 599#               done
 600#       done
 601
 602test_seq () {
 603        case $# in
 604        1)      set 1 "$@" ;;
 605        2)      ;;
 606        *)      error "bug in the test script: not 1 or 2 parameters to test_seq" ;;
 607        esac
 608        "$PERL_PATH" -le 'print for $ARGV[0]..$ARGV[1]' -- "$@"
 609}
 610
 611# This function can be used to schedule some commands to be run
 612# unconditionally at the end of the test to restore sanity:
 613#
 614#       test_expect_success 'test core.capslock' '
 615#               git config core.capslock true &&
 616#               test_when_finished "git config --unset core.capslock" &&
 617#               hello world
 618#       '
 619#
 620# That would be roughly equivalent to
 621#
 622#       test_expect_success 'test core.capslock' '
 623#               git config core.capslock true &&
 624#               hello world
 625#               git config --unset core.capslock
 626#       '
 627#
 628# except that the greeting and config --unset must both succeed for
 629# the test to pass.
 630#
 631# Note that under --immediate mode, no clean-up is done to help diagnose
 632# what went wrong.
 633
 634test_when_finished () {
 635        test_cleanup="{ $*
 636                } && (exit \"\$eval_ret\"); eval_ret=\$?; $test_cleanup"
 637}
 638
 639# Most tests can use the created repository, but some may need to create more.
 640# Usage: test_create_repo <directory>
 641test_create_repo () {
 642        test "$#" = 1 ||
 643        error "bug in the test script: not 1 parameter to test-create-repo"
 644        repo="$1"
 645        mkdir -p "$repo"
 646        (
 647                cd "$repo" || error "Cannot setup test environment"
 648                "$GIT_EXEC_PATH/git-init" "--template=$GIT_BUILD_DIR/templates/blt/" >&3 2>&4 ||
 649                error "cannot run git init -- have you built things yet?"
 650                mv .git/hooks .git/hooks-disabled
 651        ) || exit
 652}