Imported Upstream version 2.5.1
[scm/test.git] / t / testlib.sh
1 #!/usr/bin/env bash
2 # Usage: . testlib.sh
3 # Simple shell command language test library.
4 #
5 # Tests must follow the basic form:
6 #
7 #   begin_test "the thing"
8 #   (
9 #        set -e
10 #        echo "hello"
11 #        false
12 #   )
13 #   end_test
14 #
15 # When a test fails its stdout and stderr are shown.
16 #
17 # Note that tests must `set -e' within the subshell block or failed assertions
18 # will not cause the test to fail and the result may be misreported.
19 #
20 # Copyright (c) 2011-13 by Ryan Tomayko <http://tomayko.com>
21 # License: MIT
22
23 fullfile="$(pwd)/$0"
24
25 . "$(dirname "$0")/testenv.sh"
26 set -e
27
28 # keep track of num tests and failures
29 tests=0
30 failures=0
31
32 # this runs at process exit
33 atexit () {
34   tap_show_plan "$tests"
35   shutdown
36
37   if [ $failures -gt 0 ]; then
38     exit 1
39   fi
40
41   exit 0
42 }
43
44 # create the trash dir
45 trap "atexit" SIGKILL SIGINT SIGTERM EXIT
46
47 SHUTDOWN_LFS=yes
48 GITSERVER=undefined
49
50 setup
51
52 GITSERVER=$(cat "$LFS_URL_FILE")
53 SSLGITSERVER=$(cat "$LFS_SSL_URL_FILE")
54 CLIENTCERTGITSERVER=$(cat "$LFS_CLIENT_CERT_URL_FILE")
55 cd "$TRASHDIR"
56
57 # Mark the beginning of a test. A subshell should immediately follow this
58 # statement.
59 begin_test () {
60     test_status=$?
61     [ -n "$test_description" ] && end_test $test_status
62     unset test_status
63
64     tests=$(( tests + 1 ))
65     test_description="$1"
66
67     exec 3>&1 4>&2
68     out="$TRASHDIR/out"
69     err="$TRASHDIR/err"
70     trace="$TRASHDIR/trace"
71
72     exec 1>"$out" 2>"$err"
73
74     # enabling GIT_TRACE can cause Windows git to stall, esp with fd 5
75     # other fd numbers like 8/9 don't stall but still don't work, so disable
76     if [ $IS_WINDOWS -eq 0 ]; then
77       exec 5>"$trace"
78       export GIT_TRACE=5
79     fi
80
81     # reset global git config
82     HOME="$TRASHDIR/home"
83     rm -rf "$TRASHDIR/home"
84     mkdir "$HOME"
85     cp "$TESTHOME/.gitconfig" "$HOME/.gitconfig"
86
87     # do not let Git use a different configuration file
88     unset GIT_CONFIG
89     unset XDG_CONFIG_HOME
90
91     # allow the subshell to exit non-zero without exiting this process
92     set -x +e
93 }
94
95 # Mark the end of a test.
96 end_test () {
97     test_status="${1:-$?}"
98     set +x -e
99     exec 1>&3 2>&4
100     # close fd 5 (GIT_TRACE)
101     exec 5>&-
102
103     if [ "$test_status" -eq 0 ]; then
104         printf "ok %d - %-60s\n" "$tests" "$test_description ..."
105     else
106         failures=$(( failures + 1 ))
107         printf "not ok %d - %-60s\n" "$tests" "$test_description ..."
108         (
109             echo "# -- stdout --"
110             sed 's/^/#     /' <"$TRASHDIR/out"
111             echo "# -- stderr --"
112             grep -v -e '^\+ end_test' -e '^+ set +x' <"$TRASHDIR/err" |
113                 sed 's/^/#     /'
114             if [ $IS_WINDOWS -eq 0 ]; then
115                 echo "# -- git trace --"
116                 sed 's/^/#    /' <"$TRASHDIR/trace"
117             fi
118         ) 1>&2
119         echo
120     fi
121     unset test_description
122 }