t / test-lib.shon commit [PATCH] Need to set PAGER in tests (d9bdd39)
   1#!/bin/sh
   2#
   3# Copyright (c) 2005 Junio C Hamano
   4#
   5
   6# For repeatability, reset the environment to known value.
   7LANG=C
   8PAGER=cat
   9TZ=UTC
  10export LANG PAGER TZ
  11unset AUTHOR_DATE
  12unset AUTHOR_EMAIL
  13unset AUTHOR_NAME
  14unset COMMIT_AUTHOR_EMAIL
  15unset COMMIT_AUTHOR_NAME
  16unset GIT_ALTERNATE_OBJECT_DIRECTORIES
  17unset GIT_AUTHOR_DATE
  18unset GIT_AUTHOR_EMAIL
  19unset GIT_AUTHOR_NAME
  20unset GIT_COMMITTER_EMAIL
  21unset GIT_COMMITTER_NAME
  22unset GIT_DIFF_OPTS
  23unset GIT_DIR
  24unset GIT_EXTERNAL_DIFF
  25unset GIT_INDEX_FILE
  26unset GIT_OBJECT_DIRECTORY
  27unset SHA1_FILE_DIRECTORIES
  28unset SHA1_FILE_DIRECTORY
  29
  30# Each test should start with something like this, after copyright notices:
  31#
  32# test_description='Description of this test...
  33# This test checks if command xyzzy does the right thing...
  34# '
  35# . ./test-lib.sh
  36
  37error () {
  38        echo "* error: $*"
  39        exit 1
  40}
  41
  42say () {
  43        echo "* $*"
  44}
  45
  46test "${test_description}" != "" ||
  47error "Test script did not set test_description."
  48
  49while test "$#" -ne 0
  50do
  51        case "$1" in
  52        -d|--d|--de|--deb|--debu|--debug)
  53                debug=t; shift ;;
  54        -i|--i|--im|--imm|--imme|--immed|--immedi|--immedia|--immediat|--immediate)
  55                immediate=t; shift ;;
  56        -h|--h|--he|--hel|--help)
  57                echo "$test_description"
  58                exit 0 ;;
  59        -v|--v|--ve|--ver|--verb|--verbo|--verbos|--verbose)
  60                verbose=t; shift ;;
  61        *)
  62                break ;;
  63        esac
  64done
  65
  66if test "$verbose" = "t"
  67then
  68        exec 4>&2 3>&1
  69else
  70        exec 4>/dev/null 3>/dev/null
  71fi
  72
  73test_failure=0
  74test_count=0
  75
  76
  77# You are not expected to call test_ok_ and test_failure_ directly, use
  78# the text_expect_* functions instead.
  79
  80test_ok_ () {
  81        test_count=$(expr "$test_count" + 1)
  82        say "  ok $test_count: $@"
  83}
  84
  85test_failure_ () {
  86        test_count=$(expr "$test_count" + 1)
  87        test_failure=$(expr "$test_failure" + 1);
  88        say "FAIL $test_count: $1"
  89        shift
  90        echo "$@" | sed -e 's/^/        /'
  91        test "$immediate" = "" || exit 1
  92}
  93
  94
  95test_debug () {
  96        test "$debug" = "" || eval "$1"
  97}
  98
  99test_expect_failure () {
 100        test "$#" = 2 ||
 101        error "bug in the test script: not 2 parameters to test-expect-failure"
 102        say >&3 "expecting failure: $2"
 103        if eval >&3 2>&4 "$2"
 104        then
 105                test_failure_ "$@"
 106        else
 107                test_ok_ "$1"
 108        fi
 109}
 110
 111test_expect_success () {
 112        test "$#" = 2 ||
 113        error "bug in the test script: not 2 parameters to test-expect-success"
 114        say >&3 "expecting success: $2"
 115        if eval >&3 2>&4 "$2"
 116        then
 117                test_ok_ "$1"
 118        else
 119                test_failure_ "$@"
 120        fi
 121}
 122
 123test_done () {
 124        case "$test_failure" in
 125        0)      
 126                # We could:
 127                # cd .. && rm -fr trash
 128                # but that means we forbid any tests that use their own
 129                # subdirectory from calling test_done without coming back
 130                # to where they started from.
 131                # The Makefile provided will clean this test area so
 132                # we will leave things as they are.
 133
 134                say "passed all $test_count test(s)"
 135                exit 0 ;;
 136
 137        *)
 138                say "failed $test_failure among $test_count test(s)"
 139                exit 1 ;;
 140
 141        esac
 142}
 143
 144# Test the binaries we have just built.  The tests are kept in
 145# t/ subdirectory and are run in trash subdirectory.
 146PATH=$(pwd)/..:$PATH
 147
 148# Test repository
 149test=trash
 150rm -fr "$test"
 151mkdir "$test"
 152cd "$test"
 153git-init-db 2>/dev/null || error "cannot run git-init-db"