t / test-lib.shon commit test-lib: unset GIT_TRACE (1d0361e)
   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
  12unset AUTHOR_DATE
  13unset AUTHOR_EMAIL
  14unset AUTHOR_NAME
  15unset COMMIT_AUTHOR_EMAIL
  16unset COMMIT_AUTHOR_NAME
  17unset GIT_ALTERNATE_OBJECT_DIRECTORIES
  18unset GIT_AUTHOR_DATE
  19GIT_AUTHOR_EMAIL=author@example.com
  20GIT_AUTHOR_NAME='A U Thor'
  21unset GIT_COMMITTER_DATE
  22GIT_COMMITTER_EMAIL=committer@example.com
  23GIT_COMMITTER_NAME='C O Mitter'
  24unset GIT_DIFF_OPTS
  25unset GIT_DIR
  26unset GIT_EXTERNAL_DIFF
  27unset GIT_INDEX_FILE
  28unset GIT_OBJECT_DIRECTORY
  29unset GIT_TRACE
  30unset SHA1_FILE_DIRECTORIES
  31unset SHA1_FILE_DIRECTORY
  32export GIT_AUTHOR_EMAIL GIT_AUTHOR_NAME
  33export GIT_COMMITTER_EMAIL GIT_COMMITTER_NAME
  34
  35# Each test should start with something like this, after copyright notices:
  36#
  37# test_description='Description of this test...
  38# This test checks if command xyzzy does the right thing...
  39# '
  40# . ./test-lib.sh
  41
  42error () {
  43        echo "* error: $*"
  44        trap - exit
  45        exit 1
  46}
  47
  48say () {
  49        echo "* $*"
  50}
  51
  52test "${test_description}" != "" ||
  53error "Test script did not set test_description."
  54
  55while test "$#" -ne 0
  56do
  57        case "$1" in
  58        -d|--d|--de|--deb|--debu|--debug)
  59                debug=t; shift ;;
  60        -i|--i|--im|--imm|--imme|--immed|--immedi|--immedia|--immediat|--immediate)
  61                immediate=t; shift ;;
  62        -h|--h|--he|--hel|--help)
  63                echo "$test_description"
  64                exit 0 ;;
  65        -v|--v|--ve|--ver|--verb|--verbo|--verbos|--verbose)
  66                verbose=t; shift ;;
  67        --no-python)
  68                no_python=t; shift ;;
  69        *)
  70                break ;;
  71        esac
  72done
  73
  74exec 5>&1
  75if test "$verbose" = "t"
  76then
  77        exec 4>&2 3>&1
  78else
  79        exec 4>/dev/null 3>/dev/null
  80fi
  81
  82test_failure=0
  83test_count=0
  84
  85trap 'echo >&5 "FATAL: Unexpected exit with code $?"; exit 1' exit
  86
  87
  88# You are not expected to call test_ok_ and test_failure_ directly, use
  89# the text_expect_* functions instead.
  90
  91test_ok_ () {
  92        test_count=$(expr "$test_count" + 1)
  93        say "  ok $test_count: $@"
  94}
  95
  96test_failure_ () {
  97        test_count=$(expr "$test_count" + 1)
  98        test_failure=$(expr "$test_failure" + 1);
  99        say "FAIL $test_count: $1"
 100        shift
 101        echo "$@" | sed -e 's/^/        /'
 102        test "$immediate" = "" || { trap - exit; exit 1; }
 103}
 104
 105
 106test_debug () {
 107        test "$debug" = "" || eval "$1"
 108}
 109
 110test_run_ () {
 111        eval >&3 2>&4 "$1"
 112        eval_ret="$?"
 113        return 0
 114}
 115
 116test_expect_failure () {
 117        test "$#" = 2 ||
 118        error "bug in the test script: not 2 parameters to test-expect-failure"
 119        say >&3 "expecting failure: $2"
 120        test_run_ "$2"
 121        if [ "$?" = 0 -a "$eval_ret" != 0 ]
 122        then
 123                test_ok_ "$1"
 124        else
 125                test_failure_ "$@"
 126        fi
 127}
 128
 129test_expect_success () {
 130        test "$#" = 2 ||
 131        error "bug in the test script: not 2 parameters to test-expect-success"
 132        say >&3 "expecting success: $2"
 133        test_run_ "$2"
 134        if [ "$?" = 0 -a "$eval_ret" = 0 ]
 135        then
 136                test_ok_ "$1"
 137        else
 138                test_failure_ "$@"
 139        fi
 140}
 141
 142test_expect_code () {
 143        test "$#" = 3 ||
 144        error "bug in the test script: not 3 parameters to test-expect-code"
 145        say >&3 "expecting exit code $1: $3"
 146        test_run_ "$3"
 147        if [ "$?" = 0 -a "$eval_ret" = "$1" ]
 148        then
 149                test_ok_ "$2"
 150        else
 151                test_failure_ "$@"
 152        fi
 153}
 154
 155# Most tests can use the created repository, but some amy need to create more.
 156# Usage: test_create_repo <directory>
 157test_create_repo () {
 158        test "$#" = 1 ||
 159        error "bug in the test script: not 1 parameter to test-create-repo"
 160        owd=`pwd`
 161        repo="$1"
 162        mkdir "$repo"
 163        cd "$repo" || error "Cannot setup test environment"
 164        "$GIT_EXEC_PATH/git" init-db --template=$GIT_EXEC_PATH/templates/blt/ 2>/dev/null ||
 165        error "cannot run git init-db -- have you built things yet?"
 166        mv .git/hooks .git/hooks-disabled
 167        cd "$owd"
 168}
 169        
 170test_done () {
 171        trap - exit
 172        case "$test_failure" in
 173        0)
 174                # We could:
 175                # cd .. && rm -fr trash
 176                # but that means we forbid any tests that use their own
 177                # subdirectory from calling test_done without coming back
 178                # to where they started from.
 179                # The Makefile provided will clean this test area so
 180                # we will leave things as they are.
 181
 182                say "passed all $test_count test(s)"
 183                exit 0 ;;
 184
 185        *)
 186                say "failed $test_failure among $test_count test(s)"
 187                exit 1 ;;
 188
 189        esac
 190}
 191
 192# Test the binaries we have just built.  The tests are kept in
 193# t/ subdirectory and are run in trash subdirectory.
 194PATH=$(pwd)/..:$PATH
 195GIT_EXEC_PATH=$(pwd)/..
 196export PATH GIT_EXEC_PATH
 197
 198# Similarly use ../compat/subprocess.py if our python does not
 199# have subprocess.py on its own.
 200PYTHON=`sed -e '1{
 201        s/^#!//
 202        q
 203}' ../git-merge-recursive` || {
 204        error "You haven't built things yet, have you?"
 205}
 206"$PYTHON" -c 'import subprocess' 2>/dev/null || {
 207        PYTHONPATH=$(pwd)/../compat
 208        export PYTHONPATH
 209}
 210test -d ../templates/blt || {
 211        error "You haven't built things yet, have you?"
 212}
 213
 214# Test repository
 215test=trash
 216rm -fr "$test"
 217test_create_repo $test
 218cd "$test"