Reorder test directory setup and TEST_OPTS parsing
[tig.git] / test / tools / libtest.sh
blobe341add1051a5a80af2f356b7d9ea3dc80fbf4db
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 # The locale must specify UTF-8 for Ncurses to output correctly. Since C.UTF-8
28 # does not exist on Mac OS X, we end up with en_US as the only sane choice.
29 export LANG=en_US.UTF-8
30 export LC_ALL=en_US.UTF-8
32 export PAGER=cat
33 export TZ=UTC
34 export TERM=dumb
35 export HOME="$output_dir"
36 export EDITOR=:
37 unset CDPATH
39 # Git env
40 export GIT_CONFIG_NOSYSTEM
41 unset GIT_CONFIG GIT_DIR
42 unset GIT_AUTHOR_NAME GIT_AUTHOR_EMAIL GIT_AUTHOR_DATE
43 unset GIT_COMMITTER_NAME GIT_COMMITTER_EMAIL GIT_COMMITTER_DATE
45 # Tig env
46 export TIGRC_SYSTEM=
47 unset TIGRC_USER
49 # Ncurses env
50 export ESCDELAY=200
51 export LINES=30
52 export COLUMNS=80
54 [ -e "$output_dir" ] && rm -rf "$output_dir"
55 mkdir -p "$output_dir"
57 if [ ! -d "$tmp_dir/.git" ]; then
58 # Create a dummy repository to avoid reading .git/config
59 # settings from the tig repository.
60 git init -q "$tmp_dir"
63 cd "$output_dir"
66 # Utilities.
69 die()
71 echo >&2 "$*"
72 exit 1
75 file() {
76 path="$1"; shift
78 mkdir -p "$(dirname "$path")"
79 if [ -z "$1" ]; then
80 case "$path" in
81 stdin|expected*) cat ;;
82 *) sed 's/^[ ]//' ;;
83 esac > "$path"
84 else
85 printf '%s' "$@" > "$path"
89 steps() {
90 # Ensure that the steps finish by quitting
91 printf '%s\n:quit\n' "$@" \
92 | sed -e 's/^[ ]*//' -e '/^$/d' \
93 | sed "s|:save-display\s\+\(\S*\)|:save-display $HOME/\1|" \
94 > steps
95 export TIG_SCRIPT="$HOME/steps"
98 stdin() {
99 file "stdin" "$@"
102 tigrc() {
103 file "$HOME/.tigrc" "$@"
106 gitconfig() {
107 file "$HOME/.gitconfig" "$@"
111 # Parse TEST_OPTS
114 [ -t 1 ] && diff_color_arg=--color
116 indent=' '
117 verbose=
118 debugger=
120 set -- $TEST_OPTS
122 while [ $# -gt 0 ]; do
123 arg="$1"; shift
124 case "$arg" in
125 verbose) verbose=yes ;;
126 no-indent) indent= ;;
127 debugger=*) debugger=$(expr "$arg" : 'debugger=\(.*\)') ;;
128 esac
129 done
132 # Test runners and assertion checking.
135 assert_equals()
137 file="$1"; shift
139 file "expected/$file" "$@"
141 if [ -e "$file" ]; then
142 git diff --no-index $diff_color_arg "expected/$file" "$file" > "$file.diff" || true
143 if [ -s "$file.diff" ]; then
144 echo "[FAIL] $file != expected/$file" >> .test-result
145 cat "$file.diff" >> .test-result
146 else
147 echo " [OK] $file assertion" >> .test-result
149 rm -f "$file.diff"
150 else
151 echo "[FAIL] $file not found" >> .test-result
155 show_test_results()
157 if [ ! -e .test-result ]; then
158 [ -e stderr ] &&
159 sed "s/^/[stderr] /" < stderr
160 [ -e stderr.orig ] &&
161 sed "s/^/[stderr] /" < stderr.orig
162 echo "No test results found"
163 elif grep FAIL -q < .test-result; then
164 failed="$(grep FAIL < .test-result | wc -l)"
165 count="$(sed -n '/\(FAIL\|OK\)/p' < .test-result | wc -l)"
167 printf "Failed %d out of %d test(s)%s\n" $failed $count
169 # Show output from stderr if no output is expected
170 [ -e expected/stderr ] ||
171 sed "s/^/[stderr] /" < stderr
173 [ -e .test-result ] &&
174 cat .test-result
175 elif [ "$verbose" ]; then
176 count="$(sed -n '/\(OK\)/p' < .test-result | wc -l)"
177 printf "Passed %d assertions\n" $count
178 fi | sed "s/^/$indent| /"
181 trap 'show_test_results' EXIT
183 test_tig()
185 export TIG_NO_DISPLAY=
186 touch stdin stderr
187 if [ -n "$debugger" ]; then
188 if [ -s "$work_dir/stdin" ]; then
189 echo "*** This test requires data to be injected via stdin."
190 echo "*** The expected input file is: '$work_dir/stdin'"
192 (cd "$work_dir" && $debugger tig "$@")
193 else
194 (cd "$work_dir" && tig "$@") < stdin > stdout 2> stderr.orig
196 # Normalize paths in stderr output
197 if [ -e stderr.orig ]; then
198 sed "s#$output_dir#HOME#" < stderr.orig > stderr
199 rm -f stderr.orig
203 test_graph()
205 test-graph $@ > stdout 2> stderr.orig