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