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.
18 if [ -n "${BASH_VERSION:-}" ]; then
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"
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
40 export HOME
="$output_dir"
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
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"
71 fake_editor
="$tmp_dir/bin/vim"
72 cat > "$fake_editor" <<EOF
75 lineno="\$(printf "%d" "\$1")"
78 echo "\$@" >> "$HOME/editor.log"
79 sed -n -e "\${lineno}p" "\$file" >> "$HOME/editor.log"
82 chmod +x
"$fake_editor"
83 export EDITOR
="$(basename "$fake_editor")"
84 export PATH
="$(dirname "$fake_editor"):$PATH"
101 mkdir
-p "$(dirname "$path")"
104 stdin|expected
*) cat ;;
108 printf '%s' "$@" > "$path"
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|" \
123 tig_script
'steps' "$@"
131 file "$HOME/.tigrc" "$@"
135 file "$HOME/.gitconfig" "$@"
143 [ -t 1 ] && diff_color_arg
=--color
152 while [ $# -gt 0 ]; do
155 verbose
) verbose
=yes ;;
156 no-indent
) indent
= ;;
157 debugger
=*) debugger
=$
(expr "$arg" : 'debugger=\(.*\)') ;;
163 # Test runners and assertion checking.
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
178 echo " [OK] $file assertion" >> .test-result
182 echo "[FAIL] $file not found" >> .test-result
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
193 elif [ ! -e .test-result
]; then
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
] &&
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
221 export TIG_NO_DISPLAY
=
222 if [ -n "$trace" ]; then
223 export TIG_TRACE
="$HOME/.tig-trace"
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
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
250 test-graph $@
> stdout
2> stderr.orig