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.
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
35 export HOME
="$output_dir"
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
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"
78 mkdir
-p "$(dirname "$path")"
81 stdin|expected
*) cat ;;
85 printf '%s' "$@" > "$path"
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|" \
95 export TIG_SCRIPT
="$HOME/steps"
103 file "$HOME/.tigrc" "$@"
107 file "$HOME/.gitconfig" "$@"
114 [ -t 1 ] && diff_color_arg
=--color
122 while [ $# -gt 0 ]; do
125 verbose
) verbose
=yes ;;
126 no-indent
) indent
= ;;
127 debugger
=*) debugger
=$
(expr "$arg" : 'debugger=\(.*\)') ;;
132 # Test runners and assertion checking.
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
147 echo " [OK] $file assertion" >> .test-result
151 echo "[FAIL] $file not found" >> .test-result
157 if [ ! -e .test-result
]; then
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
] &&
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
185 export TIG_NO_DISPLAY
=
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
"$@")
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
205 test-graph $@
> stdout
2> stderr.orig