git/list[1] front-page[2] threads[3] people[4] search[5] about
 

[RFC/PATCH 2/2] test: allow prerequisite to be evaluated lazily

From
Junio C Hamano <gitster@pobox.com>
Date
Jul 26, 2012, 23:11 UTC
Message-ID
<7v7gtq5da3.fsf@alter.siamese.dyndns.org>
In-Reply-To
<7vboj25dk7.fsf@alter.siamese.dyndns.org>

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 <gitster@pobox.com>
---
 * 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/test-lib-functions.sh | 28 ++++++++++++++++++++++++++++
 t/test-lib.sh           |  7 ++++---
 2 files changed, 32 insertions(+), 3 deletions(-)
diff --git a/t/test-lib-functions.sh b/t/test-lib-functions.sh
index 4dc027d..2214388 100644
--- a/t/test-lib-functions.sh
+++ b/t/test-lib-functions.sh
@@ -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
 	do
+		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/test-lib.sh b/t/test-lib.sh
index bb4f886..878d000 100644
--- a/t/test-lib.sh
+++ b/t/test-lib.sh
@@ -659,9 +659,10 @@ test_i18ngrep () {
 	fi
 }
 
-# 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.
-- 
1.7.12.rc0.44.gc69a8ad
Previous: Junio C HamanoNext: Junio C Hamano
Message 2 of 7 in “test: rename $satisfied to $satisfied_prereq”
  1. 1/2 test: rename $satisfied to $satisfied_prereqJunio C Hamano, Jul 26, 2012
  2. 2/2 test: allow prerequisite to be evaluated lazilyJunio C Hamano, Jul 26, 2012
  3. 3/2 test-lib: provide case insensitivity as a prerequisiteJunio C Hamano, Jul 26, 2012
  4. 4/2 test-lib: provide UTF8 behaviour as a prerequisiteJunio C Hamano, Jul 26, 2012
  5. Jeff KingJul 27, 2012
  6. Jeff KingJul 27, 2012
  7. Junio C HamanoJul 27, 2012

Read the whole thread, see it on lore, or plain text.

$ cat FOOTERMessages come from the public archive at lore.kernel.org/git, fetched every hour. The front page is chosen and written each morning by an AI editor and can be wrong; the threads themselves are the record. About and API. For agents: an MCP server at https://gitlist.dev/mcp, and any thread, story or person page as Markdown by adding .md to its URL (or sending Accept: text/markdown). Details in /llms.txt.