test: allow prerequisite to be evaluated lazily

The test prerequisite mechanism is a useful way to allow some tests in a test script to be skipped in environments that do not support certain features (e.g. it is pointless to attempt checking how well symbolic links are handled by Git on filesystems that do not support them). It is OK for commonly used prerequisites to be always tested during start-up of a test script by having a codeblock that tests a feature and calls test_set_prereq, but for an uncommon feature, forcing 90% of scripts to pay the same probing overhead for prerequisite they do not care about is wasteful. Introduce a mechanism to probe the prerequiste lazily. Changes are: - test_lazy_prereq () function, which takes the name of the prerequisite it probes and the script to probe for it, is added. This only registers the name of the prerequiste that can be lazily probed and the script to eval (without running). - test_have_prereq() function (which is used by test_expect_success and also can be called directly by test scripts) learns to look at the list of prerequisites that can be lazily probed, and the prerequisites that have already been probed that way. When asked for a prerequiste that can be but haven't been probed, the script registered with an earlier call to test_lazy_prereq is evaluated and the prerequisite is set. - test_run_lazy_prereq_() function is a helper to run the probe script with the same kind of sandbox as regular tests, helped by Jeff King. Update the codeblock to probe and set SYMLINKS prerequisite using the new mechanism as an example. Signed-off-by: Junio C Hamano <gitster@pobox.com>

Junio C Hamano committed Jul 26, 2012 at 15:50 UTC 04083f278d19998e0e3f26627791ab54886ac12a
2 files changed +46 -3
t/test-lib-functions.sh
+42
@@ -224,6 +224,32 @@ test_set_prereq () {
224 satisfied_prereq="$satisfied_prereq$1 "
225 }
226 satisfied_prereq=" "
227 +lazily_testable_prereq= lazily_tested_prereq=
228 +
229 +# Usage: test_lazy_prereq PREREQ 'script'
230 +test_lazy_prereq () {
231 + lazily_testable_prereq="$lazily_testable_prereq$1 "
232 + eval test_prereq_lazily_$1=\$2
233 +}
234 +
235 +test_run_lazy_prereq_ () {
236 + script='
237 +mkdir -p "$TRASH_DIRECTORY/prereq-test-dir" &&
238 +(
239 + cd "$TRASH_DIRECTORY/prereq-test-dir" &&'"$2"'
240 +)'
241 + say >&3 "checking prerequisite: $1"
242 + say >&3 "$script"
243 + test_eval_ "$script"
244 + eval_ret=$?
245 + rm -rf "$TRASH_DIRECTORY/prereq-test-dir"
246 + if test "$eval_ret" = 0; then
247 + say >&3 "prerequisite $1 ok"
248 + else
249 + say >&3 "prerequisite $1 not satisfied"
250 + fi
251 + return $eval_ret
252 +}
253
254 test_have_prereq () {
255 # prerequisites can be concatenated with ','
@@ -238,6 +264,22 @@ test_have_prereq () {
264
265 for prerequisite
266 do
267 + case " $lazily_tested_prereq " in
268 + *" $prerequisite "*)
269 + ;;
270 + *)
271 + case " $lazily_testable_prereq " in
272 + *" $prerequisite "*)
273 + eval "script=\$test_prereq_lazily_$prerequisite" &&
274 + if test_run_lazy_prereq_ "$prerequisite" "$script"
275 + then
276 + test_set_prereq $prerequisite
277 + fi
278 + lazily_tested_prereq="$lazily_tested_prereq$prerequisite "
279 + esac
280 + ;;
281 + esac
282 +
283 total_prereq=$(($total_prereq + 1))
284 case "$satisfied_prereq" in
285 *" $prerequisite "*)
t/test-lib.sh
+4 -3
@@ -659,9 +659,10 @@ test_i18ngrep () {
659 fi
660 }
661
662 -# test whether the filesystem supports symbolic links
663 -ln -s x y 2>/dev/null && test -h y 2>/dev/null && test_set_prereq SYMLINKS
664 -rm -f y
662 +test_lazy_prereq SYMLINKS '
663 + # test whether the filesystem supports symbolic links
664 + ln -s x y && test -h y
665 +'
666
667 # When the tests are run as root, permission tests will report that
668 # things are writable when they shouldn't be.