README-TESTLIB-TG: fix typo
[topgit/pro.git] / t / test-lib.sh
blob5dfb20ccb4596c88e40e468f5b57ae6eba1d717f
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 \
66 TESTLIB_TEST_TAP_ONLY"
68 EXPORT_VARS="PATH GIT_TEMPLATE_DIR GIT_CONFIG_NOSYSTEM \
69 GIT_ATTR_NOSYSTEM GIT_MERGE_VERBOSITY GIT_MERGE_AUTOEDIT \
70 GIT_AUTHOR_EMAIL GIT_AUTHOR_NAME GIT_COMMITTER_EMAIL TZ \
71 GIT_COMMITTER_NAME EDITOR GIT_TRACE_BARE LANG LC_ALL PAGER \
72 _x05 _x40 _z40 EMPTY_TREE EMPTY_BLOB LF u200c UNAME_S \
73 TERM SHELL_PATH PERL_PATH GIT_PATH DIFF TG_TEST_INSTALLED \
74 test_prereq TESTLIB_NO_TOLERATE TESTLIB_TEST_LONG \
75 GIT_CEILING_DIRECTORIES TG_TEST_FULL_PATH TEST_HELPER_DIRECTORY"
77 UNSET_VARS="VISUAL EMAIL LANGUAGE COLUMNS XDG_CONFIG_HOME GITPERLLIB \
78 CDPATH GREP_OPTIONS UNZIP TESTLIB_EXIT_OK last_verbose"
80 # strip off --cache
81 shift
83 # set UNAME_S if needed
84 : "${UNAME_S:=$(uname -s)}"
86 # run the standard init but avoid doing any --tee processing now
87 . ./test-lib-main.sh
88 TESTLIB_TEST_TEE_STARTED=done
89 test_lib_main_init_generic "$@" || exit $?
90 unset_ TESTLIB_TEST_TEE_STARTED
92 if [ -n "$lazily_testable_prereq" ]; then
93 # run all the "lazy" prereq tests now in a new subdir
94 # (set up on the "--root" if selected) using setup code
95 # taken (and modified) from test-lib-main.sh so that the
96 # lazy prereqs get the same answer the would when not cached
97 TRASH_DIRECTORY="cachetest"
98 test -n "$root" && TRASH_DIRECTORY="$root/$TRASH_DIRECTORY"
99 case "$TRASH_DIRECTORY" in
100 /*) ;; # absolute path is good
101 *) TRASH_DIRECTORY="$TEST_OUTPUT_DIRECTORY/$TRASH_DIRECTORY" ;;
102 esac
103 ! [ -e "$TRASH_DIRECTORY" ] || rm -fr "$TRASH_DIRECTORY" ||
104 fatal "FATAL: Cannot prepare cache test area"
105 mkdir -p "$TRASH_DIRECTORY" && [ -d "$TRASH_DIRECTORY" ] ||
106 fatal "cannot mkdir -p $TRASH_DIRECTORY"
107 TRASHTMP_DIRECTORY="$TRASH_DIRECTORY"
108 savepwd="$PWD"
109 savehome="$HOME"
110 cd -P "$TRASH_DIRECTORY" || fatal "cannot cd to $TRASH_DIRECTORY"
111 git init --quiet --template="$EMPTY_DIRECTORY" >/dev/null 2>&1 ||
112 fatal "cannot run git init"
113 HOME="$TRASH_DIRECTORY"
114 GNUPGHOME="$HOME/gnupg-home-not-used"
115 export HOME GNUPGHOME
116 for lp in $lazily_testable_prereq; do
117 ! { eval "lpscript=\$test_prereq_lazily_$lp" &&
118 (t() { eval "$lpscript";}; t) >/dev/null 2>&1;} || test_set_prereq $lp
119 lazily_tested_prereq="$lazily_tested_prereq$lp "
120 done
121 HOME="$savehome"
122 cd "$savepwd" || fatal "cannot cd to $savepwd"
123 rm -rf "$TRASH_DIRECTORY"
124 unset_ savepwd savehome TRASH_DIRECTORY TRASHTMP_DIRECTORY GNUPGHOME
127 # Add most GIT_XXX vars (variation of code from test-lib-main.sh)
128 UNSET_VARS="$UNSET_VARS $("$PERL_PATH" -e '
129 my @env = keys %ENV;
130 my $ok = join("|", qw(
131 TRACE
132 DEBUG
133 USE_LOOKUP
134 TEST
135 .*_TEST
136 MINIMUM_VERSION
137 PATH
138 PROVE
139 UNZIP
140 PERF_
141 CURL_VERBOSE
142 TRACE_CURL
143 CEILING_DIRECTORIES
145 my @vars = grep(/^GIT_/ && !/^GIT_($ok)/o, @env);
146 print join(" ", @vars);
149 # return true if variable name passed as the first argument is
150 # set even if to an empty value
151 isvarset() {
152 eval "test \"z\${$1+set}\" = \"zset\""
154 test_quotevar_ PWD PWD_SQ
155 test_quotevar_ TESTLIB_DIRECTORY TD_SQ
157 echo unset $UNSET_VARS "|| : &&"
158 while read vname && [ -n "$vname" ]; do
159 ! isvarset $vname || { test_quotevar_ "$vname" qv; printf '%s=%s &&\n' "$vname" "$qv"; }
160 done <<-EOT
161 $(echo $CACHE_VARS $EXPORT_VARS | LC_ALL=C sed 'y/ /\n/' | LC_ALL=C sort -u)
162 say_color_reset
164 echo export $EXPORT_VARS "&&"
165 printf '%s\n' \
166 "cd $PWD_SQ &&" \
167 ". $TD_SQ/test-lib-functions-tg.sh &&" \
168 ". $TD_SQ/test-lib-functions.sh &&" \
169 ". $TD_SQ/test-lib-main.sh &&"
170 echo "TESTLIB_CACHE_ACTIVE=1"
171 } >TG-TEST-CACHE
172 printf ". %s/TG-TEST-CACHE || { echo 'error: missing '\'%s'/TG-TEST-CACHE'\' >&2; exit 1; }\n" "$PWD_SQ" "$PWD_SQ"
173 TESTLIB_EXIT_OK=1
174 exit 0
177 # usage: whats_the_dir [-P | -L] [--] path-to-something varname
178 # determine path-to-something's directory and store it into varname
179 # without "-P" or "-L" a relative dirname may be returned
180 whats_the_dir() {
181 # determine "$1"'s directory and store it into the var name passed as "$2"
182 if [ "z$1" = "z-P" -o "z$1" = "z-L" ]; then
183 if [ "z$2" = "z--" ]; then
184 set -- "$3" "$4" "$1"
185 else
186 set -- "$2" "$3" "$1"
188 elif [ "z$1" = "z--" ]; then
189 shift
191 case "$1" in *"/"*);;*) set -- "./$1" "$2" "$3"; esac
192 while [ -L "$1" ]; do
193 set -- "$(readlink "$1")" "$2" "$3" "$1"
194 case "$1" in "/"*);;*)
195 set -- "${4%/*}/$1" "$2" "$3"
196 esac
197 done
198 set -- "${1%/*}" "$2" "$3"
199 if [ "z$3" != "z" ] && [ -d "$1" ] &&
200 ! case "$1" in [!/]*|*"/./"*|*"/."|*"/../"*|*"/..") ! :; esac; then
201 [ "z$3" = "z-P" ] || set -- "$1" "$2"
202 if [ "z$3" = "z" -a \( "z$1" = "z." -o "z$1" = "z$PWD" \) ]; then
203 set -- "$PWD" "$2"
204 else
205 set -- "$(cd "$1" && pwd $3)" "$2"
208 eval "$2=\"$1\""
211 [ -z "$TESTLIB_CACHE" ] || eval "$TESTLIB_CACHE" || exit $?
212 if [ -n "$TESTLIB_CACHE_ACTIVE" ]; then
213 # Everything should have been restored by the eval of "$TESTLIB_CACHE"
214 # Remove the leftover variables used to trigger use of the cache
215 unset_ TESTLIB_CACHE TESTLIB_CACHE_ACTIVE
217 # Handle --tee now if needed
218 test_lib_main_init_tee "$@"
220 # We must also still perform per-test initialization though
221 test_lib_main_init_specific "$@"
222 else
223 # Normal, non-cached case where we run the init function
224 whats_the_dir -- "${TEST_DIRECTORY:-.}/test-lib.sh" testlib_dir_
225 . "$testlib_dir_/test-lib-main.sh" &&
226 unset_ _testlib_dir_ &&
227 test_lib_main_init "$@"