Print expected arguments when debugging a test
[tig.git] / test / tools / libtest.sh
blobb1e12be9aaaa749ad744f792f32101ab54e13f77
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 unset CDPATH
43 # Git env
44 export GIT_CONFIG_NOSYSTEM
45 unset GIT_CONFIG GIT_DIR
46 unset GIT_AUTHOR_NAME GIT_AUTHOR_EMAIL GIT_AUTHOR_DATE
47 unset GIT_COMMITTER_NAME GIT_COMMITTER_EMAIL GIT_COMMITTER_DATE
49 # Tig env
50 export TIG_TRACE=
51 export TIGRC_SYSTEM=
52 unset TIGRC_USER
54 # Ncurses env
55 export ESCDELAY=200
56 export LINES=30
57 export COLUMNS=80
59 [ -e "$output_dir" ] && rm -rf "$output_dir"
60 mkdir -p "$output_dir/$work_dir"
62 if [ ! -d "$tmp_dir/.git" ]; then
63 # Create a dummy repository to avoid reading .git/config
64 # settings from the tig repository.
65 git init -q "$tmp_dir"
68 mkdir -p "$tmp_dir/bin"
70 # Setup fake editor
71 fake_editor="$tmp_dir/bin/vim"
72 cat > "$fake_editor" <<EOF
73 #!/bin/sh
75 lineno="\$(printf "%d" "\$1")"
76 file="\$2"
78 echo "\$@" >> "$HOME/editor.log"
79 sed -n -e "\${lineno}p" "\$file" >> "$HOME/editor.log"
80 EOF
82 chmod +x "$fake_editor"
83 export EDITOR="$(basename "$fake_editor")"
84 export PATH="$(dirname "$fake_editor"):$PATH"
86 cd "$output_dir"
89 # Utilities.
92 die()
94 echo >&2 "$*"
95 exit 1
98 file() {
99 path="$1"; shift
101 mkdir -p "$(dirname "$path")"
102 if [ -z "$@" ]; then
103 case "$path" in
104 stdin|expected*) cat ;;
105 *) sed 's/^[ ]//' ;;
106 esac > "$path"
107 else
108 printf '%s' "$@" > "$path"
112 tig_script() {
113 export TIG_SCRIPT="$HOME/$1"; shift
115 # Ensure that the steps finish by quitting
116 printf '%s\n:quit\n' "$@" \
117 | sed -e 's/^[ ]*//' -e '/^$/d' \
118 | sed "s|:save-display\s\+\(\S*\)|:save-display $HOME/\1|" \
119 > "$TIG_SCRIPT"
122 steps() {
123 tig_script 'steps' "$@"
126 stdin() {
127 file "stdin" "$@"
130 tigrc() {
131 file "$HOME/.tigrc" "$@"
134 gitconfig() {
135 file "$HOME/.gitconfig" "$@"
139 # Parse TEST_OPTS
142 diff_color_arg=
143 [ -t 1 ] && diff_color_arg=--color
145 indent=' '
146 verbose=
147 debugger=
148 trace=
150 set -- $TEST_OPTS
152 while [ $# -gt 0 ]; do
153 arg="$1"; shift
154 case "$arg" in
155 verbose) verbose=yes ;;
156 no-indent) indent= ;;
157 debugger=*) debugger=$(expr "$arg" : 'debugger=\(.*\)') ;;
158 trace) trace=yes ;;
159 esac
160 done
163 # Test runners and assertion checking.
166 assert_equals()
168 file="$1"; shift
170 file "expected/$file" "$@"
172 if [ -e "$file" ]; then
173 git diff -w --no-index $diff_color_arg "expected/$file" "$file" > "$file.diff" || true
174 if [ -s "$file.diff" ]; then
175 echo "[FAIL] $file != expected/$file" >> .test-result
176 cat "$file.diff" >> .test-result
177 else
178 echo " [OK] $file assertion" >> .test-result
180 rm -f "$file.diff"
181 else
182 echo "[FAIL] $file not found" >> .test-result
186 show_test_results()
188 if [ -n "$trace" -a -n "$TIG_TRACE" -a -e "$TIG_TRACE" ]; then
189 sed "s/^/$indent[trace] /" < "$TIG_TRACE"
191 if [ ! -d "$HOME" ]; then
192 echo "Skipped"
193 elif [ ! -e .test-result ]; then
194 [ -e stderr ] &&
195 sed "s/^/[stderr] /" < stderr
196 [ -e stderr.orig ] &&
197 sed "s/^/[stderr] /" < stderr.orig
198 echo "No test results found"
199 elif grep FAIL -q < .test-result; then
200 failed="$(grep FAIL < .test-result | wc -l)"
201 count="$(sed -n '/\(FAIL\|OK\)/p' < .test-result | wc -l)"
203 printf "Failed %d out of %d test(s)%s\n" $failed $count
205 # Show output from stderr if no output is expected
206 [ -e expected/stderr ] ||
207 sed "s/^/[stderr] /" < stderr
209 [ -e .test-result ] &&
210 cat .test-result
211 elif [ "$verbose" ]; then
212 count="$(sed -n '/\(OK\)/p' < .test-result | wc -l)"
213 printf "Passed %d assertions\n" $count
214 fi | sed "s/^/$indent| /"
217 trap 'show_test_results' EXIT
219 test_tig()
221 export TIG_NO_DISPLAY=
222 if [ -n "$trace" ]; then
223 export TIG_TRACE="$HOME/.tig-trace"
225 touch stdin stderr
226 if [ -n "$debugger" ]; then
227 echo "*** Running tests in '$(pwd)/$work_dir'"
228 if [ -s "$work_dir/stdin" ]; then
229 echo "*** - This test requires data to be injected via stdin."
230 echo "*** The expected input file is: '../stdin'"
232 if [ "$#" -gt 0 ]; then
233 echo "*** - This test expects the following arguments: $@"
235 (cd "$work_dir" && $debugger tig "$@")
236 elif [ -s stdin ]; then
237 (cd "$work_dir" && tig "$@") < stdin > stdout 2> stderr.orig
238 else
239 (cd "$work_dir" && tig "$@") > stdout 2> stderr.orig
241 # Normalize paths in stderr output
242 if [ -e stderr.orig ]; then
243 sed "s#$output_dir#HOME#" < stderr.orig > stderr
244 rm -f stderr.orig
248 test_graph()
250 test-graph $@ > stdout 2> stderr.orig