The test prerequisite mechanism is a useful way to allow some tests
in a test script to be skipped in environments that do not offer
certain features (e.g. checking how symbolic links are handled on
filesystems that do not support them).  It is OK for commonly used
prerequisites to be always tested during start-up of 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.

Update the codeblock to probe and set SYMLINKS prerequisite using
the new mechanism as an example.

Signed-off-by: Junio C Hamano <>

 * I thought about various error conditions but didn't come up with
   a solid conclusion.  For example, what should happen when the
   prober directory cannot be created, or removed?  Perhaps aborting
   the whole thing may be a safe and better option.

   Also, I am not distinguishing a syntax error in the script and
   "the prerequisite is not satisfied" signal (they would both be a
   false in the "if ()" part). I do not think we care too much, but
   others may have better ideas.

 t/ | 28 ++++++++++++++++++++++++++++
 t/           |  7 ++++---
 2 files changed, 32 insertions(+), 3 deletions(-)

diff --git a/t/ b/t/
index 4dc027d..2214388 100644
--- a/t/
+++ b/t/
@@ -224,6 +224,13 @@ test_set_prereq () {
        satisfied_prereq="$satisfied_prereq$1 "
 satisfied_prereq=" "
+lazy_testable_prereq= lazy_tested_prereq=
+# Usage: test_lazy_prereq PREREQ 'script'
+test_lazy_prereq () {
+       lazy_testable_prereq="$lazy_testable_prereq$1 "
+       eval test_prereq_lazily_$1=\$2
 test_have_prereq () {
        # prerequisites can be concatenated with ','
@@ -238,6 +245,27 @@ test_have_prereq () {
        for prerequisite
+               case " $lazy_tested_prereq " in
+               *" $prerequisite "*)
+                       ;;
+               *)
+                       case " $lazy_testable_prereq " in
+                       *" $prerequisite "*)
+                               mkdir -p "$TRASH_DIRECTORY/prereq-test-dir"
+                               if (
+                                       eval 
"script=\$test_prereq_lazily_$prerequisite" &&
+                                       cd "$TRASH_DIRECTORY/prereq-test-dir" &&
+                                       eval "$script"
+                               )
+                               then
+                                       test_set_prereq $prerequisite
+                               fi
+                               rm -fr "$TRASH_DIRECTORY/prereq-test-dir"
lazy_tested_prereq="$lazy_tested_prereq$prerequisite "
+                       esac
+                       ;;
+               esac
                total_prereq=$(($total_prereq + 1))
                case "$satisfied_prereq" in
                *" $prerequisite "*)
diff --git a/t/ b/t/
index bb4f886..878d000 100644
--- a/t/
+++ b/t/
@@ -659,9 +659,10 @@ test_i18ngrep () {
-# test whether the filesystem supports symbolic links
-ln -s x y 2>/dev/null && test -h y 2>/dev/null && test_set_prereq SYMLINKS
-rm -f y
+test_lazy_prereq SYMLINKS '
+       # test whether the filesystem supports symbolic links
+       ln -s x y 2>/dev/null && test -h y 2>/dev/null
 # When the tests are run as root, permission tests will report that
 # things are writable when they shouldn't be.

To unsubscribe from this list: send the line "unsubscribe git" in
the body of a message to
More majordomo info at

Reply via email to