Skip to content

Commit 04083f2

Browse files
committed
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 <[email protected]>
1 parent f3cfc3b commit 04083f2

File tree

2 files changed

+46
-3
lines changed

2 files changed

+46
-3
lines changed

t/test-lib-functions.sh

Lines changed: 42 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -224,6 +224,32 @@ test_set_prereq () {
224224
satisfied_prereq="$satisfied_prereq$1 "
225225
}
226226
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+
}
227253

228254
test_have_prereq () {
229255
# prerequisites can be concatenated with ','
@@ -238,6 +264,22 @@ test_have_prereq () {
238264

239265
for prerequisite
240266
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+
241283
total_prereq=$(($total_prereq + 1))
242284
case "$satisfied_prereq" in
243285
*" $prerequisite "*)

t/test-lib.sh

Lines changed: 4 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -659,9 +659,10 @@ test_i18ngrep () {
659659
fi
660660
}
661661

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+
'
665666

666667
# When the tests are run as root, permission tests will report that
667668
# things are writable when they shouldn't be.

0 commit comments

Comments
 (0)