1 # Test lib caching support
2 # Copyright (C) 2016,2017 Kyle J. McKay.
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.
14 eval "${TRAPEXIT_:-exit $EXITCODE_}"
17 trap 'trapexit_ $?' EXIT
19 # unset that ignnores error code that shouldn't be produced according to POSIX
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)
28 eval "set -- \"\${$1}\" \"$2\""
30 set -- "$(printf '%s\nZ\n' "$1" | sed "s
/'/'\\\''/g")" "$2"
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
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"
82 # set UNAME_S if needed
83 : "${UNAME_S:=$(uname -s)}"
85 # run the standard init but avoid doing any --tee processing now
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" ;;
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"
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 "
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 '
129 my
$ok = join("|", qw
(
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
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"; }
160 $(echo $CACHE_VARS $EXPORT_VARS | LC_ALL=C sed 'y
/ /\n/' | LC_ALL=C sort -u)
163 echo export $EXPORT_VARS "&&"
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"
171 printf ". %s/TG-TEST-CACHE || { echo 'error
: missing
'\'%s'/TG-TEST-CACHE
'\' >&2; exit 1; }\n" "$PWD_SQ" "$PWD_SQ"
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
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"
185 set -- "$2" "$3" "$1"
187 elif [ "z$1" = "z--" ]; then
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"
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
204 set -- "$(cd "$1" && pwd $3)" "$2"
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
"$@"
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
"$@"