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