Imported Upstream version 2.4.2
[scm/test.git] / test / 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 . "test/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   shutdown
35
36   if [ $failures -gt 0 ]; then
37     exit 1
38   fi
39
40   exit 0
41 }
42
43 # create the trash dir
44 trap "atexit" EXIT
45
46 SHUTDOWN_LFS=yes
47 GITSERVER=undefined
48
49 # if the file exists, assume another process started it, and will clean it up
50 # when it's done
51 if [ -s $LFS_URL_FILE ]; then
52   SHUTDOWN_LFS=no
53 else
54   setup || {
55     failures=$(( failures + 1 ))
56     exit $?
57   }
58 fi
59
60 GITSERVER=$(cat "$LFS_URL_FILE")
61 SSLGITSERVER=$(cat "$LFS_SSL_URL_FILE")
62 CLIENTCERTGITSERVER=$(cat "$LFS_CLIENT_CERT_URL_FILE")
63 cd "$TRASHDIR"
64
65 # Mark the beginning of a test. A subshell should immediately follow this
66 # statement.
67 begin_test () {
68     test_status=$?
69     [ -n "$test_description" ] && end_test $test_status
70     unset test_status
71
72     tests=$(( tests + 1 ))
73     test_description="$1"
74
75     exec 3>&1 4>&2
76     out="$TRASHDIR/out"
77     err="$TRASHDIR/err"
78     trace="$TRASHDIR/trace"
79
80     exec 1>"$out" 2>"$err"
81
82     # enabling GIT_TRACE can cause Windows git to stall, esp with fd 5
83     # other fd numbers like 8/9 don't stall but still don't work, so disable
84     if [ $IS_WINDOWS -eq 0 ]; then
85       exec 5>"$trace"
86       export GIT_TRACE=5
87     fi
88
89     # reset global git config
90     HOME="$TRASHDIR/home"
91     rm -rf "$TRASHDIR/home"
92     mkdir "$HOME"
93     cp "$TESTHOME/.gitconfig" "$HOME/.gitconfig"
94
95     # do not let Git use a different configuration file
96     unset GIT_CONFIG
97     unset XDG_CONFIG_HOME
98
99     # allow the subshell to exit non-zero without exiting this process
100     set -x +e
101 }
102
103 # Mark the end of a test.
104 end_test () {
105     test_status="${1:-$?}"
106     set +x -e
107     exec 1>&3 2>&4
108     # close fd 5 (GIT_TRACE)
109     exec 5>&-
110
111     if [ "$test_status" -eq 0 ]; then
112         printf "test: %-60s OK\n" "$test_description ..."
113     else
114         failures=$(( failures + 1 ))
115         printf "test: %-60s FAILED\n" "$test_description ..."
116         (
117             echo "-- stdout --"
118             sed 's/^/    /' <"$TRASHDIR/out"
119             echo "-- stderr --"
120             grep -v -e '^\+ end_test' -e '^+ set +x' <"$TRASHDIR/err" |
121                 sed 's/^/    /'
122             if [ $IS_WINDOWS -eq 0 ]; then
123                 echo "-- git trace --"
124                 sed 's/^/   /' <"$TRASHDIR/trace"
125             fi
126         ) 1>&2
127         echo
128     fi
129     unset test_description
130 }