t4200: test the out-of-date checking code
[topgit/pro.git] / t / test-lib.sh
blob46a18839b07854a6c09ea84a216da825f9347155
1 # Test lib caching support
2 # Copyright (C) 2016,2017 Kyle J. McKay.
3 # All rights reserved.
4 # License GPLv2+
7 # some ridiculous sh implementations require 'trap ... EXIT' to be executed
8 # OUTSIDE ALL FUNCTIONS to work in a sane fashion. Always trap it and eval
9 # "${TRAPEXIT_:-exit}" as a substitute.
10 trapexit_()
12 EXITCODE_=${1:-$?}
13 trap - EXIT
14 eval "${TRAPEXIT_:-exit $EXITCODE_}"
15 exit $EXITCODE_
17 trap 'trapexit_ $?' EXIT
19 # unset that ignnores error code that shouldn't be produced according to POSIX
20 unset_() {
21 { unset "$@"; } >/dev/null 2>&1 || :
24 # stores the single-quoted value of the variable name passed as
25 # the first argument into the variable name passed as the second
26 # (use test_quotevar_ 3 varname "$value" to quote a value directly)
27 test_quotevar_() {
28 eval "set -- \"\${$1}\" \"$2\""
29 case "$1" in *"'"*)
30 set -- "$(printf '%s\nZ\n' "$1" | sed "s/'/'\\\''/g")" "$2"
31 set -- "${1%??}" "$2"
32 esac
33 eval "$2=\"'$1'\""
36 if [ "$1" = "--cache" ]; then
37 # export all state to $PWD/TG-TEST-CACHE
38 # then return a suitable value for 'TESTLIB_CACHE'
40 # CACHE_VARS is a list of variable names to cache but only if they
41 # are actually set.
43 # EXPORT_VARS is a list of variables that should be exported.
45 # UNSET_VARS is a list of variables that should always be unset
46 # it will automatically have unwanted GIT_XXX vars added to it
48 CACHE_VARS="GIT_MERGE_VERBOSITY GIT_MERGE_AUTOEDIT \
49 GIT_CONFIG_NOSYSTEM GIT_ATTR_NOSYSTEM GIT_TRACE_BARE \
50 debug verbose verbose_only test_count trace LANG LC_ALL \
51 TZ _x05 _x40 _z40 EMPTY_TREE EMPTY_BLOB LF u200c color \
52 immediate TESTLIB_TEST_LONG run_list help quiet \
53 say_color_error say_color_skip say_color_warn say_color_pass \
54 say_color_info say_color_ TERM TESTLIB_SHELL_HAS_SHOPT \
55 test_failure test_count test_fixed test_broken test_success \
56 test_external_has_tap last_verbose GIT_MINIMUM_VERSION \
57 TG_TEST_INSTALLED uname_s test_prereq TG_GIT_MINIMUM_VERSION \
58 TG_INST_BINDIR TG_INST_CMDDIR TG_INST_HOOKSDIR TG_VERSION \
59 TG_INST_SHAREDIR git_version test_auh tg_version UNAME_S \
60 lazily_tested_prereq satisfied_prereq PATH TESTLIB_TEST_CMP \
61 GIT_AUTHOR_NAME GIT_AUTHOR_EMAIL GIT_PATH DIFF \
62 GIT_COMMITTER_NAME GIT_COMMITTER_EMAIL GIT_TEMPLATE_DIR \
63 EMPTY_DIRECTORY EDITOR TESTLIB_DIRECTORY TEST_DIRECTORY \
64 TEST_OUTPUT_DIRECTORY PAGER root SHELL_PATH PERL_PATH \
65 TESTLIB_NO_TOLERATE TESTLIB_TEST_CHAIN_LINT"
67 EXPORT_VARS="PATH GIT_TEMPLATE_DIR GIT_CONFIG_NOSYSTEM \
68 GIT_ATTR_NOSYSTEM GIT_MERGE_VERBOSITY GIT_MERGE_AUTOEDIT \
69 GIT_AUTHOR_EMAIL GIT_AUTHOR_NAME GIT_COMMITTER_EMAIL TZ \
70 GIT_COMMITTER_NAME EDITOR GIT_TRACE_BARE LANG LC_ALL PAGER \
71 _x05 _x40 _z40 EMPTY_TREE EMPTY_BLOB LF u200c UNAME_S \
72 TERM SHELL_PATH PERL_PATH GIT_PATH DIFF TG_TEST_INSTALLED \
73 test_prereq TESTLIB_NO_TOLERATE TESTLIB_TEST_LONG \
74 GIT_CEILING_DIRECTORIES TG_TEST_FULL_PATH TEST_HELPER_DIRECTORY"
76 UNSET_VARS="VISUAL EMAIL LANGUAGE COLUMNS XDG_CONFIG_HOME GITPERLLIB \
77 CDPATH GREP_OPTIONS UNZIP TESTLIB_EXIT_OK last_verbose"
79 # strip off --cache
80 shift
82 # set UNAME_S if needed
83 : "${UNAME_S:=$(uname -s)}"
85 # run the standard init but avoid doing any --tee processing now
86 . ./test-lib-main.sh
87 TESTLIB_TEST_TEE_STARTED=done
88 test_lib_main_init_generic "$@" || exit $?
89 unset_ TESTLIB_TEST_TEE_STARTED
91 if [ -n "$lazily_testable_prereq" ]; then
92 # run all the "lazy" prereq tests now in a new subdir
93 # (set up on the "--root" if selected) using setup code
94 # taken (and modified) from test-lib-main.sh so that the
95 # lazy prereqs get the same answer the would when not cached
96 TRASH_DIRECTORY="cachetest"
97 test -n "$root" && TRASH_DIRECTORY="$root/$TRASH_DIRECTORY"
98 case "$TRASH_DIRECTORY" in
99 /*) ;; # absolute path is good
100 *) TRASH_DIRECTORY="$TEST_OUTPUT_DIRECTORY/$TRASH_DIRECTORY" ;;
101 esac
102 ! [ -e "$TRASH_DIRECTORY" ] || rm -fr "$TRASH_DIRECTORY" ||
103 fatal "FATAL: Cannot prepare cache test area"
104 mkdir -p "$TRASH_DIRECTORY" && [ -d "$TRASH_DIRECTORY" ] ||
105 fatal "cannot mkdir -p $TRASH_DIRECTORY"
106 TRASHTMP_DIRECTORY="$TRASH_DIRECTORY"
107 savepwd="$PWD"
108 savehome="$HOME"
109 cd -P "$TRASH_DIRECTORY" || fatal "cannot cd to $TRASH_DIRECTORY"
110 git init --quiet --template="$EMPTY_DIRECTORY" >/dev/null 2>&1 ||
111 fatal "cannot run git init"
112 HOME="$TRASH_DIRECTORY"
113 GNUPGHOME="$HOME/gnupg-home-not-used"
114 export HOME GNUPGHOME
115 for lp in $lazily_testable_prereq; do
116 ! { eval "lpscript=\$test_prereq_lazily_$lp" &&
117 (t() { eval "$lpscript";}; t) >/dev/null 2>&1;} || test_set_prereq $lp
118 lazily_tested_prereq="$lazily_tested_prereq$lp "
119 done
120 HOME="$savehome"
121 cd "$savepwd" || fatal "cannot cd to $savepwd"
122 rm -rf "$TRASH_DIRECTORY"
123 unset_ savepwd savehome TRASH_DIRECTORY TRASHTMP_DIRECTORY GNUPGHOME
126 # Add most GIT_XXX vars (variation of code from test-lib-main.sh)
127 UNSET_VARS="$UNSET_VARS $("$PERL_PATH" -e '
128 my @env = keys %ENV;
129 my $ok = join("|", qw(
130 TRACE
131 DEBUG
132 USE_LOOKUP
133 TEST
134 .*_TEST
135 MINIMUM_VERSION
136 PATH
137 PROVE
138 UNZIP
139 PERF_
140 CURL_VERBOSE
141 TRACE_CURL
142 CEILING_DIRECTORIES
144 my @vars = grep(/^GIT_/ && !/^GIT_($ok)/o, @env);
145 print join(" ", @vars);
148 # return true if variable name passed as the first argument is
149 # set even if to an empty value
150 isvarset() {
151 eval "test \"z\${$1+set}\" = \"zset\""
153 test_quotevar_ PWD PWD_SQ
154 test_quotevar_ TESTLIB_DIRECTORY TD_SQ
156 echo unset $UNSET_VARS "|| : &&"
157 while read vname && [ -n "$vname" ]; do
158 ! isvarset $vname || { test_quotevar_ "$vname" qv; printf '%s=%s &&\n' "$vname" "$qv"; }
159 done <<-EOT
160 $(echo $CACHE_VARS $EXPORT_VARS | LC_ALL=C sed 'y/ /\n/' | LC_ALL=C sort -u)
161 say_color_reset
163 echo export $EXPORT_VARS "&&"
164 printf '%s\n' \
165 "cd $PWD_SQ &&" \
166 ". $TD_SQ/test-lib-functions-tg.sh &&" \
167 ". $TD_SQ/test-lib-functions.sh &&" \
168 ". $TD_SQ/test-lib-main.sh &&"
169 echo "TESTLIB_CACHE_ACTIVE=1"
170 } >TG-TEST-CACHE
171 printf ". %s/TG-TEST-CACHE || { echo 'error: missing '\'%s'/TG-TEST-CACHE'\' >&2; exit 1; }\n" "$PWD_SQ" "$PWD_SQ"
172 TESTLIB_EXIT_OK=1
173 exit 0
176 # usage: whats_the_dir [-P | -L] [--] path-to-something varname
177 # determine path-to-something's directory and store it into varname
178 # without "-P" or "-L" a relative dirname may be returned
179 whats_the_dir() {
180 # determine "$1"'s directory and store it into the var name passed as "$2"
181 if [ "z$1" = "z-P" -o "z$1" = "z-L" ]; then
182 if [ "z$2" = "z--" ]; then
183 set -- "$3" "$4" "$1"
184 else
185 set -- "$2" "$3" "$1"
187 elif [ "z$1" = "z--" ]; then
188 shift
190 case "$1" in *"/"*);;*) set -- "./$1" "$2" "$3"; esac
191 while [ -L "$1" ]; do
192 set -- "$(readlink "$1")" "$2" "$3" "$1"
193 case "$1" in "/"*);;*)
194 set -- "${4%/*}/$1" "$2" "$3"
195 esac
196 done
197 set -- "${1%/*}" "$2" "$3"
198 if [ "z$3" != "z" ] && [ -d "$1" ] &&
199 ! case "$1" in [!/]*|*"/./"*|*"/."|*"/../"*|*"/..") ! :; esac; then
200 [ "z$3" = "z-P" ] || set -- "$1" "$2"
201 if [ "z$3" = "z" -a \( "z$1" = "z." -o "z$1" = "z$PWD" \) ]; then
202 set -- "$PWD" "$2"
203 else
204 set -- "$(cd "$1" && pwd $3)" "$2"
207 eval "$2=\"$1\""
210 [ -z "$TESTLIB_CACHE" ] || eval "$TESTLIB_CACHE" || exit $?
211 if [ -n "$TESTLIB_CACHE_ACTIVE" ]; then
212 # Everything should have been restored by the eval of "$TESTLIB_CACHE"
213 # Remove the leftover variables used to trigger use of the cache
214 unset_ TESTLIB_CACHE TESTLIB_CACHE_ACTIVE
216 # Handle --tee now if needed
217 test_lib_main_init_tee "$@"
219 # We must also still perform per-test initialization though
220 test_lib_main_init_specific "$@"
221 else
222 # Normal, non-cached case where we run the init function
223 whats_the_dir -- "${TEST_DIRECTORY:-.}/test-lib.sh" testlib_dir_
224 . "$testlib_dir_/test-lib-main.sh" &&
225 unset_ _testlib_dir_ &&
226 test_lib_main_init "$@"