1 # Test libe caching support
2 # Copyright (C) 2016 Kyle J. McKay. All rights reserved.
5 if [ "$1" = "--cache" ]; then
6 # export all state to $PWD/TG-TEST-CACHE
7 # then return a suitable value for 'TESTLIB_CACHE'
9 # CACHE_VARS is a list of variable names to cache but only if they
12 # EXPORT_VARS is a list of variables that should be exported.
14 # UNSET_VARS is a list of variables that should always be unset
15 # it will automatically have unwanted GIT_XXX vars added to it
17 CACHE_VARS
="GIT_MERGE_VERBOSITY GIT_MERGE_AUTOEDIT \
18 GIT_CONFIG_NOSYSTEM GIT_ATTR_NOSYSTEM GIT_TRACE_BARE \
19 debug verbose verbose_only test_count trace LANG LC_ALL \
20 TZ _x05 _x40 _z40 EMPTY_TREE EMPTY_BLOB LF u200c color \
21 immediate TESTLIB_TEST_LONG run_list help quiet \
22 say_color_error say_color_skip say_color_warn say_color_pass \
23 say_color_info say_color_reset say_color_ TERM BASH_XTRACEFD \
24 test_failure test_count test_fixed test_broken test_success \
25 test_external_has_tap last_verbose GIT_MINIMUM_VERSION \
26 TG_TEST_INSTALLED uname_s test_prereq TG_GIT_MINIMUM_VERSION \
27 TG_INST_BINDIR TG_INST_CMDDIR TG_INST_HOOKSDIR TG_VERSION \
28 TG_INST_SHAREDIR git_version git_vernum tg_version \
29 lazily_tested_prereq satisfied_prereq PATH TESTLIB_TEST_CMP \
30 GIT_AUTHOR_NAME GIT_AUTHOR_EMAIL GIT_PATH DIFF \
31 GIT_COMMITTER_NAME GIT_COMMITTER_EMAIL GIT_TEMPLATE_DIR \
32 EMPTY_DIRECTORY EDITOR TESTLIB_DIRECTORY TEST_DIRECTORY \
33 TEST_OUTPUT_DIRECTORY PAGER root SHELL_PATH PERL_PATH \
34 TESTLIB_NO_TOLERATE TESTLIB_TEST_CHAIN_LINT"
36 EXPORT_VARS
="PATH GIT_TEMPLATE_DIR GIT_CONFIG_NOSYSTEM \
37 GIT_ATTR_NOSYSTEM GIT_MERGE_VERBOSITY GIT_MERGE_AUTOEDIT \
38 GIT_AUTHOR_EMAIL GIT_AUTHOR_NAME GIT_COMMITTER_EMAIL TZ \
39 GIT_COMMITTER_NAME EDITOR GIT_TRACE_BARE LANG LC_ALL PAGER \
40 _x05 _x40 _z40 EMPTY_TREE EMPTY_BLOB LF u200c \
41 TERM SHELL_PATH PERL_PATH GIT_PATH DIFF TG_TEST_INSTALLED \
42 test_prereq TESTLIB_NO_TOLERATE TESTLIB_TEST_LONG"
44 UNSET_VARS
="VISUAL EMAIL LANGUAGE COLUMNS XDG_CONFIG_HOME GITPERLLIB \
45 CDPATH GREP_OPTIONS UNZIP TESTLIB_EXIT_OK last_verbose"
50 # run the standard init but avoid doing any --tee processing now
52 TESTLIB_TEST_TEE_STARTED
=done
53 test_lib_main_init_generic
"$@" ||
exit $?
54 unset TESTLIB_TEST_TEE_STARTED
56 if [ -n "$lazily_testable_prereq" ]; then
57 # run all the "lazy" prereq tests now
58 cachetestdir
="$TESTLIB_DIRECTORY/cachetest"
59 ! [ -e "$cachetestdir" ] ||
rm -rf "$cachetestdir" ||
60 fatal
"cannot remove $cachetestdir"
61 mkdir
"$cachetestdir" || fatal
"cannot mkdir $cachetestdir"
63 cd "$cachetestdir" || fatal
"cannot cd to $cachetestdir"
64 for lp in $lazily_testable_prereq; do
65 ! { eval "lpscript=\$test_prereq_lazily_$lp" &&
66 (t
() { eval "$lpscript";}; t
) >/dev
/null
2>&1;} || test_set_prereq
$lp
67 lazily_tested_prereq
="$lazily_tested_prereq$lp "
69 cd "$savepwd" || fatal
"cannot cd to $savepwd"
70 rm -rf "$cachetestdir"
73 # Add most GIT_XXX vars (variation of code from test-lib-main.sh)
74 UNSET_VARS
="$UNSET_VARS $("$PERL_PATH" -e '
76 my $ok = join("|
", qw(
90 my @vars = grep(/^GIT_/ && !/^GIT_($ok)/o, @env);
91 print join(" ", @vars);
94 # writes the single-quoted value of the variable name passed as
95 # the first argument to stdout (will be '' if unset) followed by the
96 # second followed by a newline; use `quotevar 3 "" "$value"` to quote
99 eval "_scratch=\"\${$1}\""
100 case "$_scratch" in *"'"*)
101 _scratch
="$(printf '%s\nZ\n' "$_scratch" | sed "s
/'/'\\\''/g")"
102 _scratch="${_scratch%??}"
104 printf "'%s
'%s\n" "$_scratch" "$2"
106 # return true if variable name passed as the first argument is
107 # set even if to an empty value
109 test "$(eval 'echo "${'$1'+set}"')" = "set"
111 PWD_SQ="$(quotevar PWD)"
113 echo unset $UNSET_VARS "&&"
114 while read vname && [ -n "$vname" ]; do
115 ! isvarset $vname || { printf "%s=" $vname; quotevar $vname " &&"; }
117 $(echo $CACHE_VARS | sed 'y
/ /\n/' | sort -u)
119 echo export $EXPORT_VARS "&&"
121 echo ". ./test-lib-functions.sh &&"
122 echo ". ./test-lib-main.sh &&"
123 echo "TESTLIB_CACHE_ACTIVE=1"
125 printf ". %s/TG-TEST-CACHE || { echo 'error
: missing
'\'%s'/TG-TEST-CACHE
'\' >&2; exit 1; }\n" "$PWD_SQ" "$PWD_SQ"
130 [ -z "$TESTLIB_CACHE" ] || eval "$TESTLIB_CACHE" || exit $?
131 if [ -n "$TESTLIB_CACHE_ACTIVE" ]; then
132 # Everything should have been restored by the eval of "$TESTLIB_CACHE"
133 # Remove the leftover variables used to trigger use of the cache
134 unset TESTLIB_CACHE TESTLIB_CACHE_ACTIVE
136 # However, we still must parse and handle some options :(
137 # swiped from test-lib-main.sh
138 case "$TESTLIB_TEST_TEE_STARTED, $* " in
140 # do not redirect again
142 *' --tee '*|*' --va'*|*' --verbose-log '*)
143 mkdir -p "$TEST_OUTPUT_DIRECTORY/test-results"
144 BASE="$TEST_OUTPUT_DIRECTORY/test-results/$(basename "$0" .sh)"
146 # Make this filename available to the sub-process in case it is using
148 TESTLIB_TEST_TEE_OUTPUT_FILE=$BASE.out
149 export TESTLIB_TEST_TEE_OUTPUT_FILE
151 # Truncate before calling "tee -a" to get rid of the results
152 # from any previous runs.
153 >"$TESTLIB_TEST_TEE_OUTPUT_FILE"
155 (TESTLIB_TEST_TEE_STARTED=done ${SHELL_PATH} "$0" "$@" 2>&1;
156 echo $? >"$BASE.exit") | tee -a "$TESTLIB_TEST_TEE_OUTPUT_FILE"
157 test "$(cat "$BASE.exit")" = 0
162 # We must also still perform per-test initialization though
163 test_lib_main_init_specific "$@"
165 # Normal, non-cached case where we run the init function
167 test_lib_main_init "$@"