Allow tests to invoke tig with view arguments
[tig.git] / test / tools / libtest.sh
blob5ca0f1bce9e485ca81b8420098a641f92e6ec9a1
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 -eu
18 if [ -n "${BASH_VERSION:-}" ]; then
19 set -o pipefail
20 IFS=$'\n\t'
23 test="$(basename "$0")"
24 source_dir="$(cd "$(dirname "$0")" && pwd)"
25 base_dir="$(echo "$source_dir" | sed -n 's#\(.*/test\)\([/].*\)*#\1#p')"
26 prefix_dir="$(echo "$source_dir" | sed -n 's#\(.*/test/\)\([/].*\)*#\2#p')"
27 output_dir="$base_dir/tmp/$prefix_dir/$test"
28 tmp_dir="$base_dir/tmp"
29 output_dir="$tmp_dir/$prefix_dir/$test"
30 work_dir="work dir"
32 # The locale must specify UTF-8 for Ncurses to output correctly. Since C.UTF-8
33 # does not exist on Mac OS X, we end up with en_US as the only sane choice.
34 export LANG=en_US.UTF-8
35 export LC_ALL=en_US.UTF-8
37 export PAGER=cat
38 export TZ=UTC
39 export TERM=dumb
40 export HOME="$output_dir"
41 export EDITOR=:
42 unset CDPATH
44 # Git env
45 export GIT_CONFIG_NOSYSTEM
46 unset GIT_CONFIG GIT_DIR
47 unset GIT_AUTHOR_NAME GIT_AUTHOR_EMAIL GIT_AUTHOR_DATE
48 unset GIT_COMMITTER_NAME GIT_COMMITTER_EMAIL GIT_COMMITTER_DATE
50 # Tig env
51 export TIG_TRACE=
52 export TIGRC_SYSTEM=
53 unset TIGRC_USER
55 # Ncurses env
56 export ESCDELAY=200
57 export LINES=30
58 export COLUMNS=80
60 [ -e "$output_dir" ] && rm -rf "$output_dir"
61 mkdir -p "$output_dir/$work_dir"
63 if [ ! -d "$tmp_dir/.git" ]; then
64 # Create a dummy repository to avoid reading .git/config
65 # settings from the tig repository.
66 git init -q "$tmp_dir"
69 cd "$output_dir"
72 # Utilities.
75 die()
77 echo >&2 "$*"
78 exit 1
81 file() {
82 path="$1"; shift
84 mkdir -p "$(dirname "$path")"
85 if [ -z "$@" ]; then
86 case "$path" in
87 stdin|expected*) cat ;;
88 *) sed 's/^[ ]//' ;;
89 esac > "$path"
90 else
91 printf '%s' "$@" > "$path"
95 tig_script() {
96 export TIG_SCRIPT="$HOME/$1"; shift
98 # Ensure that the steps finish by quitting
99 printf '%s\n:quit\n' "$@" \
100 | sed -e 's/^[ ]*//' -e '/^$/d' \
101 | sed "s|:save-display\s\+\(\S*\)|:save-display $HOME/\1|" \
102 > "$TIG_SCRIPT"
105 steps() {
106 tig_script 'steps' "$@"
109 stdin() {
110 file "stdin" "$@"
113 tigrc() {
114 file "$HOME/.tigrc" "$@"
117 gitconfig() {
118 file "$HOME/.gitconfig" "$@"
122 # Parse TEST_OPTS
125 diff_color_arg=
126 [ -t 1 ] && diff_color_arg=--color
128 indent=' '
129 verbose=
130 debugger=
131 trace=
133 set -- $TEST_OPTS
135 while [ $# -gt 0 ]; do
136 arg="$1"; shift
137 case "$arg" in
138 verbose) verbose=yes ;;
139 no-indent) indent= ;;
140 debugger=*) debugger=$(expr "$arg" : 'debugger=\(.*\)') ;;
141 trace) trace=yes ;;
142 esac
143 done
146 # Test runners and assertion checking.
149 assert_equals()
151 file="$1"; shift
153 file "expected/$file" "$@"
155 if [ -e "$file" ]; then
156 git diff -w --no-index $diff_color_arg "expected/$file" "$file" > "$file.diff" || true
157 if [ -s "$file.diff" ]; then
158 echo "[FAIL] $file != expected/$file" >> .test-result
159 cat "$file.diff" >> .test-result
160 else
161 echo " [OK] $file assertion" >> .test-result
163 rm -f "$file.diff"
164 else
165 echo "[FAIL] $file not found" >> .test-result
169 show_test_results()
171 if [ -n "$trace" -a -n "$TIG_TRACE" -a -e "$TIG_TRACE" ]; then
172 sed "s/^/$indent[trace] /" < "$TIG_TRACE"
174 if [ ! -d "$HOME" ]; then
175 echo "Skipped"
176 elif [ ! -e .test-result ]; then
177 [ -e stderr ] &&
178 sed "s/^/[stderr] /" < stderr
179 [ -e stderr.orig ] &&
180 sed "s/^/[stderr] /" < stderr.orig
181 echo "No test results found"
182 elif grep FAIL -q < .test-result; then
183 failed="$(grep FAIL < .test-result | wc -l)"
184 count="$(sed -n '/\(FAIL\|OK\)/p' < .test-result | wc -l)"
186 printf "Failed %d out of %d test(s)%s\n" $failed $count
188 # Show output from stderr if no output is expected
189 [ -e expected/stderr ] ||
190 sed "s/^/[stderr] /" < stderr
192 [ -e .test-result ] &&
193 cat .test-result
194 elif [ "$verbose" ]; then
195 count="$(sed -n '/\(OK\)/p' < .test-result | wc -l)"
196 printf "Passed %d assertions\n" $count
197 fi | sed "s/^/$indent| /"
200 trap 'show_test_results' EXIT
202 test_tig()
204 export TIG_NO_DISPLAY=
205 if [ -n "$trace" ]; then
206 export TIG_TRACE="$HOME/.tig-trace"
208 touch stdin stderr
209 if [ -n "$debugger" ]; then
210 echo "*** Running tests in '$(pwd)/$work_dir'"
211 if [ -s "$work_dir/stdin" ]; then
212 echo "*** This test requires data to be injected via stdin."
213 echo "*** The expected input file is: '../stdin'"
215 (cd "$work_dir" && $debugger tig "$@")
216 elif [ -s stdin ]; then
217 (cd "$work_dir" && tig "$@") < stdin > stdout 2> stderr.orig
218 else
219 (cd "$work_dir" && tig "$@") > stdout 2> stderr.orig
221 # Normalize paths in stderr output
222 if [ -e stderr.orig ]; then
223 sed "s#$output_dir#HOME#" < stderr.orig > stderr
224 rm -f stderr.orig
228 test_graph()
230 test-graph $@ > stdout 2> stderr.orig