Replace --'s so they do not get translated to —
[elinks.git] / test / libtest.sh
blob12de5c9201535cde8f40940ccc8a53443b32438c
1 #!/bin/sh
3 # Copyright (c) 2005 Junio C Hamano
6 # For repeatability, reset the environment to known value.
7 LANG=C
8 LC_ALL=C
9 PAGER=cat
10 TZ=UTC
11 export LANG LC_ALL PAGER TZ
13 # Each test should start with something like this, after copyright notices:
15 # test_description='Description of this test...
16 # This test checks if command xyzzy does the right thing...
17 # '
18 # . ./test-lib.sh
20 error () {
21 echo "* error: $*"
22 trap - exit
23 exit 1
26 say () {
27 echo "* $*"
30 test "${test_description}" != "" ||
31 error "Test script did not set test_description."
33 while test "$#" -ne 0
35 case "$1" in
36 -d|--d|--de|--deb|--debu|--debug)
37 debug=t; shift ;;
38 -i|--i|--im|--imm|--imme|--immed|--immedi|--immedia|--immediat|--immediate)
39 immediate=t; shift ;;
40 -h|--h|--he|--hel|--help)
41 echo "$test_description"
42 exit 0 ;;
43 -v|--v|--ve|--ver|--verb|--verbo|--verbos|--verbose)
44 verbose=t; shift ;;
46 break ;;
47 esac
48 done
50 exec 5>&1
51 if test "$verbose" = "t"
52 then
53 exec 4>&2 3>&1
54 else
55 exec 4>/dev/null 3>/dev/null
58 test_failure=0
59 test_count=0
61 trap 'echo >&5 "FATAL: Unexpected exit with code $?"; exit 1' exit
63 # Test the binaries we have just built. The tests are kept in
64 # test/ subdirectory and are run in test/trash subdirectory.
65 export PATH="$(pwd):$PATH"
67 for dep in $DEPS; do
68 test -e "$dep" || error "You haven't built things yet, have you?"
69 done
72 # You are not expected to call test_ok_ and test_failure_ directly, use
73 # the text_expect_* functions instead.
75 test_ok_ () {
76 test_count=$(expr "$test_count" + 1)
77 say " ok $test_count: $@"
80 test_failure_ () {
81 test_count=$(expr "$test_count" + 1)
82 test_failure=$(expr "$test_failure" + 1);
83 say "FAIL $test_count: $1"
84 shift
85 echo "$@" | sed -e 's/^/ /'
86 test "$immediate" = "" || { trap - exit; exit 1; }
90 test_debug () {
91 test "$debug" = "" || eval "$1"
94 test_run_ () {
95 eval >&3 2>&4 "$1"
96 eval_ret="$?"
97 return 0
100 test_expect_failure () {
101 test "$#" = 2 ||
102 error "bug in the test script: not 2 parameters to test-expect-failure"
103 say >&3 "expecting failure: $2"
104 test_run_ "$2"
105 if [ "$?" = 0 -a "$eval_ret" != 0 ]
106 then
107 test_ok_ "$1"
108 else
109 test_failure_ "$@"
113 test_expect_success () {
114 test "$#" = 2 ||
115 error "bug in the test script: not 2 parameters to test-expect-success"
116 say >&3 "expecting success: $2"
117 test_run_ "$2"
118 if [ "$?" = 0 -a "$eval_ret" = 0 ]
119 then
120 test_ok_ "$1"
121 else
122 test_failure_ "$@"
126 test_expect_code () {
127 test "$#" = 3 ||
128 error "bug in the test script: not 3 parameters to test-expect-code"
129 say >&3 "expecting exit code $1: $3"
130 test_run_ "$3"
131 if [ "$?" = 0 -a "$eval_ret" = "$1" ]
132 then
133 test_ok_ "$2"
134 else
135 test_failure_ "$@"
139 test_done () {
140 trap - exit
141 case "$test_failure" in
143 # We could:
144 # cd .. && rm -fr trash
145 # but that means we forbid any tests that use their own
146 # subdirectory from calling test_done without coming back
147 # to where they started from.
148 # The Makefile provided will clean this test area so
149 # we will leave things as they are.
151 say "passed all $test_count test(s)"
152 exit 0 ;;
155 say "failed $test_failure among $test_count test(s)"
156 exit 1 ;;
158 esac
161 # Test repository
162 test=trash
163 rm -fr "$test"
164 mkdir "$test"
165 cd "$test" || error "Cannot setup test environment"