t / test-lib.shon commit git-add--interactive: Improve behavior on bogus input (6a6eb3d)
   1#!/bin/sh
   2#
   3# Copyright (c) 2005 Junio C Hamano
   4#
   5
   6# For repeatability, reset the environment to known value.
   7LANG=C
   8LC_ALL=C
   9PAGER=cat
  10TZ=UTC
  11export LANG LC_ALL PAGER TZ
  12EDITOR=:
  13VISUAL=:
  14unset GIT_EDITOR
  15unset AUTHOR_DATE
  16unset AUTHOR_EMAIL
  17unset AUTHOR_NAME
  18unset COMMIT_AUTHOR_EMAIL
  19unset COMMIT_AUTHOR_NAME
  20unset EMAIL
  21unset GIT_ALTERNATE_OBJECT_DIRECTORIES
  22unset GIT_AUTHOR_DATE
  23GIT_AUTHOR_EMAIL=author@example.com
  24GIT_AUTHOR_NAME='A U Thor'
  25unset GIT_COMMITTER_DATE
  26GIT_COMMITTER_EMAIL=committer@example.com
  27GIT_COMMITTER_NAME='C O Mitter'
  28unset GIT_DIFF_OPTS
  29unset GIT_DIR
  30unset GIT_WORK_TREE
  31unset GIT_EXTERNAL_DIFF
  32unset GIT_INDEX_FILE
  33unset GIT_OBJECT_DIRECTORY
  34unset SHA1_FILE_DIRECTORIES
  35unset SHA1_FILE_DIRECTORY
  36GIT_MERGE_VERBOSITY=5
  37export GIT_MERGE_VERBOSITY
  38export GIT_AUTHOR_EMAIL GIT_AUTHOR_NAME
  39export GIT_COMMITTER_EMAIL GIT_COMMITTER_NAME
  40export EDITOR VISUAL
  41
  42# Protect ourselves from common misconfiguration to export
  43# CDPATH into the environment
  44unset CDPATH
  45
  46case $(echo $GIT_TRACE |tr "[A-Z]" "[a-z]") in
  47        1|2|true)
  48                echo "* warning: Some tests will not work if GIT_TRACE" \
  49                        "is set as to trace on STDERR ! *"
  50                echo "* warning: Please set GIT_TRACE to something" \
  51                        "other than 1, 2 or true ! *"
  52                ;;
  53esac
  54
  55# Each test should start with something like this, after copyright notices:
  56#
  57# test_description='Description of this test...
  58# This test checks if command xyzzy does the right thing...
  59# '
  60# . ./test-lib.sh
  61
  62error () {
  63        echo "* error: $*"
  64        trap - exit
  65        exit 1
  66}
  67
  68say () {
  69        echo "* $*"
  70}
  71
  72test "${test_description}" != "" ||
  73error "Test script did not set test_description."
  74
  75while test "$#" -ne 0
  76do
  77        case "$1" in
  78        -d|--d|--de|--deb|--debu|--debug)
  79                debug=t; shift ;;
  80        -i|--i|--im|--imm|--imme|--immed|--immedi|--immedia|--immediat|--immediate)
  81                immediate=t; shift ;;
  82        -h|--h|--he|--hel|--help)
  83                echo "$test_description"
  84                exit 0 ;;
  85        -v|--v|--ve|--ver|--verb|--verbo|--verbos|--verbose)
  86                verbose=t; shift ;;
  87        --no-python)
  88                # noop now...
  89                shift ;;
  90        *)
  91                break ;;
  92        esac
  93done
  94
  95exec 5>&1
  96if test "$verbose" = "t"
  97then
  98        exec 4>&2 3>&1
  99else
 100        exec 4>/dev/null 3>/dev/null
 101fi
 102
 103test_failure=0
 104test_count=0
 105
 106trap 'echo >&5 "FATAL: Unexpected exit with code $?"; exit 1' exit
 107
 108test_tick () {
 109        if test -z "${test_tick+set}"
 110        then
 111                test_tick=1112911993
 112        else
 113                test_tick=$(($test_tick + 60))
 114        fi
 115        GIT_COMMITTER_DATE="$test_tick -0700"
 116        GIT_AUTHOR_DATE="$test_tick -0700"
 117        export GIT_COMMITTER_DATE GIT_AUTHOR_DATE
 118}
 119
 120# You are not expected to call test_ok_ and test_failure_ directly, use
 121# the text_expect_* functions instead.
 122
 123test_ok_ () {
 124        test_count=$(expr "$test_count" + 1)
 125        say "  ok $test_count: $@"
 126}
 127
 128test_failure_ () {
 129        test_count=$(expr "$test_count" + 1)
 130        test_failure=$(expr "$test_failure" + 1);
 131        say "FAIL $test_count: $1"
 132        shift
 133        echo "$@" | sed -e 's/^/        /'
 134        test "$immediate" = "" || { trap - exit; exit 1; }
 135}
 136
 137
 138test_debug () {
 139        test "$debug" = "" || eval "$1"
 140}
 141
 142test_run_ () {
 143        eval >&3 2>&4 "$1"
 144        eval_ret="$?"
 145        return 0
 146}
 147
 148test_skip () {
 149        this_test=$(expr "./$0" : '.*/\(t[0-9]*\)-[^/]*$')
 150        this_test="$this_test.$(expr "$test_count" + 1)"
 151        to_skip=
 152        for skp in $GIT_SKIP_TESTS
 153        do
 154                case "$this_test" in
 155                $skp)
 156                        to_skip=t
 157                esac
 158        done
 159        case "$to_skip" in
 160        t)
 161                say >&3 "skipping test: $@"
 162                test_count=$(expr "$test_count" + 1)
 163                say "skip $test_count: $1"
 164                : true
 165                ;;
 166        *)
 167                false
 168                ;;
 169        esac
 170}
 171
 172test_expect_failure () {
 173        test "$#" = 2 ||
 174        error "bug in the test script: not 2 parameters to test-expect-failure"
 175        if ! test_skip "$@"
 176        then
 177                say >&3 "expecting failure: $2"
 178                test_run_ "$2"
 179                if [ "$?" = 0 -a "$eval_ret" != 0 -a "$eval_ret" -lt 129 ]
 180                then
 181                        test_ok_ "$1"
 182                else
 183                        test_failure_ "$@"
 184                fi
 185        fi
 186        echo >&3 ""
 187}
 188
 189test_expect_success () {
 190        test "$#" = 2 ||
 191        error "bug in the test script: not 2 parameters to test-expect-success"
 192        if ! test_skip "$@"
 193        then
 194                say >&3 "expecting success: $2"
 195                test_run_ "$2"
 196                if [ "$?" = 0 -a "$eval_ret" = 0 ]
 197                then
 198                        test_ok_ "$1"
 199                else
 200                        test_failure_ "$@"
 201                fi
 202        fi
 203        echo >&3 ""
 204}
 205
 206test_expect_code () {
 207        test "$#" = 3 ||
 208        error "bug in the test script: not 3 parameters to test-expect-code"
 209        if ! test_skip "$@"
 210        then
 211                say >&3 "expecting exit code $1: $3"
 212                test_run_ "$3"
 213                if [ "$?" = 0 -a "$eval_ret" = "$1" ]
 214                then
 215                        test_ok_ "$2"
 216                else
 217                        test_failure_ "$@"
 218                fi
 219        fi
 220        echo >&3 ""
 221}
 222
 223# Most tests can use the created repository, but some amy need to create more.
 224# Usage: test_create_repo <directory>
 225test_create_repo () {
 226        test "$#" = 1 ||
 227        error "bug in the test script: not 1 parameter to test-create-repo"
 228        owd=`pwd`
 229        repo="$1"
 230        mkdir "$repo"
 231        cd "$repo" || error "Cannot setup test environment"
 232        "$GIT_EXEC_PATH/git" init --template=$GIT_EXEC_PATH/templates/blt/ >/dev/null 2>&1 ||
 233        error "cannot run git init -- have you built things yet?"
 234        mv .git/hooks .git/hooks-disabled
 235        cd "$owd"
 236}
 237
 238test_done () {
 239        trap - exit
 240        case "$test_failure" in
 241        0)
 242                # We could:
 243                # cd .. && rm -fr trash
 244                # but that means we forbid any tests that use their own
 245                # subdirectory from calling test_done without coming back
 246                # to where they started from.
 247                # The Makefile provided will clean this test area so
 248                # we will leave things as they are.
 249
 250                say "passed all $test_count test(s)"
 251                exit 0 ;;
 252
 253        *)
 254                say "failed $test_failure among $test_count test(s)"
 255                exit 1 ;;
 256
 257        esac
 258}
 259
 260# Test the binaries we have just built.  The tests are kept in
 261# t/ subdirectory and are run in trash subdirectory.
 262PATH=$(pwd)/..:$PATH
 263GIT_EXEC_PATH=$(pwd)/..
 264GIT_TEMPLATE_DIR=$(pwd)/../templates/blt
 265GIT_CONFIG=.git/config
 266export PATH GIT_EXEC_PATH GIT_TEMPLATE_DIR GIT_CONFIG
 267
 268GITPERLLIB=$(pwd)/../perl/blib/lib:$(pwd)/../perl/blib/arch/auto/Git
 269export GITPERLLIB
 270test -d ../templates/blt || {
 271        error "You haven't built things yet, have you?"
 272}
 273
 274if ! test -x ../test-chmtime; then
 275        echo >&2 'You need to build test-chmtime:'
 276        echo >&2 'Run "make test-chmtime" in the source (toplevel) directory'
 277        exit 1
 278fi
 279
 280# Test repository
 281test=trash
 282rm -fr "$test"
 283test_create_repo $test
 284cd "$test"
 285
 286this_test=$(expr "./$0" : '.*/\(t[0-9]*\)-[^/]*$')
 287for skp in $GIT_SKIP_TESTS
 288do
 289        to_skip=
 290        for skp in $GIT_SKIP_TESTS
 291        do
 292                case "$this_test" in
 293                $skp)
 294                        to_skip=t
 295                esac
 296        done
 297        case "$to_skip" in
 298        t)
 299                say >&3 "skipping test $this_test altogether"
 300                say "skip all tests in $this_test"
 301                test_done
 302        esac
 303done