Document the test framework and improve the test output
[tig.git] / test / tools / libtest.sh
blob0b6291d86963353d0dae4bb137b309a7ce710264
1 #!/bin/sh
3 # Setup test environment.
5 # Copyright (c) 2014 Jonas Fonseca <jonas.fonseca@gmail.com>
7 # This program is free software; you can redistribute it and/or
8 # modify it under the terms of the GNU General Public License as
9 # published by the Free Software Foundation; either version 2 of
10 # the License, or (at your option) any later version.
12 # This program is distributed in the hope that it will be useful,
13 # but WITHOUT ANY WARRANTY; without even the implied warranty of
14 # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
15 # GNU General Public License for more details.
17 set -e
19 test="$(basename "$0")"
20 source_dir="$(cd "$(dirname "$0")" && pwd)"
21 base_dir="$(echo "$source_dir" | sed -n 's#\(.*/test\)\([/].*\)*#\1#p')"
22 prefix_dir="$(echo "$source_dir" | sed -n 's#\(.*/test/\)\([/].*\)*#\2#p')"
23 output_dir="$base_dir/tmp/$prefix_dir/$test"
24 tmp_dir="$base_dir/tmp"
25 output_dir="$tmp_dir/$prefix_dir/$test"
27 [ -t 1 ] && diff_color_arg=--color
29 indent=' '
30 verbose=
32 set -- $TEST_OPTS
34 while [ $# -gt 0 ]; do
35 arg="$1"; shift
36 case "$arg" in
37 verbose) verbose=yes ;;
38 no-indent) indent= ;;
39 esac
40 done
42 [ -e "$output_dir" ] && rm -rf "$output_dir"
43 mkdir -p "$output_dir"
45 if [ ! -d "$tmp_dir/.git" ]; then
46 # Create a dummy repository to avoid reading .git/config
47 # settings from the tig repository.
48 git init -q "$tmp_dir"
51 # The locale must specify UTF-8 for Ncurses to output correctly. Since C.UTF-8
52 # does not exist on Mac OS X, we end up with en_US as the only sane choice.
53 export LANG=en_US.UTF-8
54 export LC_ALL=en_US.UTF-8
56 export PAGER=cat
57 export TZ=UTC
58 export TERM=dumb
59 export HOME="$output_dir"
60 export EDITOR=:
61 unset CDPATH
63 # Git env
64 export GIT_CONFIG_NOSYSTEM
65 unset GIT_CONFIG GIT_DIR
66 unset GIT_AUTHOR_NAME GIT_AUTHOR_EMAIL GIT_AUTHOR_DATE
67 unset GIT_COMMITTER_NAME GIT_COMMITTER_EMAIL GIT_COMMITTER_DATE
69 # Tig env
70 export TIGRC_SYSTEM=
71 unset TIGRC_USER
73 # Ncurses env
74 export ESCDELAY=200
75 export LINES=30
76 export COLUMNS=80
78 cd "$output_dir"
81 # Utilities.
84 die()
86 echo >&2 "$*"
87 exit 1
90 file() {
91 path="$1"; shift
93 mkdir -p "$(dirname "$path")"
94 if [ -z "$1" ]; then
95 case "$path" in
96 stdin|expected*) cat ;;
97 *) sed 's/^[ ]//' ;;
98 esac > "$path"
99 else
100 printf '%s' "$@" > "$path"
104 steps() {
105 # Ensure that the steps finish by quitting
106 printf '%s\n:quit\n' "$@" \
107 | sed -e 's/^[ ]*//' -e '/^$/d' \
108 | sed "s|:save-display\s\+\(\S*\)|:save-display $HOME/\1|" \
109 > steps
110 export TIG_SCRIPT="$HOME/steps"
113 stdin() {
114 file "stdin" "$@"
117 tigrc() {
118 file "$HOME/.tigrc" "$@"
121 gitconfig() {
122 file "$HOME/.gitconfig" "$@"
126 # Test runners and assertion checking.
129 assert_equals()
131 file="$1"; shift
133 file "expected/$file" "$@"
135 if [ -e "$file" ]; then
136 git diff --no-index $diff_color_arg "expected/$file" "$file" > "$file.diff" || true
137 if [ -s "$file.diff" ]; then
138 echo "[FAIL] $file != expected/$file" >> .test-result
139 cat "$file.diff" >> .test-result
140 else
141 echo " [OK] $file assertion" >> .test-result
143 rm -f "$file.diff"
144 else
145 echo "[FAIL] $file not found" >> .test-result
149 show_test_results()
151 if [ ! -e .test-result ]; then
152 [ -e stderr ] &&
153 sed "s/^/[stderr] /" < stderr
154 [ -e stderr.orig ] &&
155 sed "s/^/[stderr] /" < stderr.orig
156 echo "No test results found"
157 elif grep FAIL -q < .test-result; then
158 failed="$(grep FAIL < .test-result | wc -l)"
159 count="$(sed -n '/\(FAIL\|OK\)/p' < .test-result | wc -l)"
161 printf "Failed %d out of %d test(s)%s\n" $failed $count
163 # Show output from stderr if no output is expected
164 [ -e expected/stderr ] ||
165 sed "s/^/[stderr] /" < stderr
167 [ -e .test-result ] &&
168 cat .test-result
169 elif [ "$verbose" ]; then
170 count="$(sed -n '/\(OK\)/p' < .test-result | wc -l)"
171 printf "Passed %d assertions\n" $count
172 fi | sed "s/^/$indent| /"
175 trap 'show_test_results' EXIT
177 test_tig()
179 export TIG_NO_DISPLAY=
180 touch stdin
181 (cd "$work_dir" && tig "$@") < stdin > stdout 2> stderr.orig
182 # Normalize paths in stderr output
183 sed "s#$output_dir#HOME#" < stderr.orig > stderr
184 rm -f stderr.orig
187 test_graph()
189 test-graph $@ > stdout 2> stderr.orig