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

[PATCH 2/2] test-lib-functions: use BUG() in 'test_must_fail'

From
SZEDER Gábor <szeder.dev@gmail.com>
Date
Feb 21, 2021, 19:25 UTC
Message-ID
<20210221192512.3096291-2-szeder.dev@gmail.com>
In-Reply-To
<20210221192512.3096291-1-szeder.dev@gmail.com>

In many test helper functions we verify that they were invoked with sensible parameters, and call BUG() to abort the test script when the parameters are buggy. 6a67c75948 (test-lib-functions: restrict test_must_fail usage, 2020-07-07) added such a parameter verification to 'test_must_fail', but it didn't report the error with BUG(), like we usually do.

As discussed in detail in the previous patch, BUG() didn't really work in 'test_must_fail' back then, but it resolved those issues, so let's use BUG() in this case as well.

The two tests checking that 'test_must_fail' recognizes invalid parameters need some updates:

  - BUG() calls 'exit 1' to abort the test script, but we don't want
    that to happen while testing 'test_must_fail' itself, so in those
    tests we must invoke that function in a subshell.
  - These tests check that 'test_must_fail' failed with the
    appropriate error message, but BUG() sends its error message to a
    different file descriptor, so update the redirection accordingly.
Signed-off-by: SZEDER Gábor <szeder.dev@gmail.com>
---
 t/t0000-basic.sh        | 4 ++--
 t/test-lib-functions.sh | 3 +--
 2 files changed, 3 insertions(+), 4 deletions(-)
diff --git a/t/t0000-basic.sh b/t/t0000-basic.sh
index a6e570d674..b9d5c6c404 100755
--- a/t/t0000-basic.sh
+++ b/t/t0000-basic.sh
@@ -1315,12 +1315,12 @@ test_expect_success 'test_must_fail on a failing git command with env' '
 '
 
 test_expect_success 'test_must_fail rejects a non-git command' '
-	! test_must_fail grep ^$ notafile 2>err &&
+	! ( test_must_fail grep ^$ notafile ) 7>err &&
 	grep -F "test_must_fail: only '"'"'git'"'"' is allowed" err
 '
 
 test_expect_success 'test_must_fail rejects a non-git command with env' '
-	! test_must_fail env var1=a var2=b grep ^$ notafile 2>err &&
+	! ( test_must_fail env var1=a var2=b grep ^$ notafile ) 7>err &&
 	grep -F "test_must_fail: only '"'"'git'"'"' is allowed" err
 '
 
diff --git a/t/test-lib-functions.sh b/t/test-lib-functions.sh
index a40c1c5d83..cdbc59e4f0 100644
--- a/t/test-lib-functions.sh
+++ b/t/test-lib-functions.sh
@@ -910,8 +910,7 @@ test_must_fail () {
 	esac
 	if ! test_must_fail_acceptable "$@"
 	then
-		echo >&6 "test_must_fail: only 'git' is allowed: $*"
-		return 1
+		BUG "test_must_fail: only 'git' is allowed: $*"
 	fi
 	"$@" 2>&6
 	exit_code=$?
-- 
2.30.1.940.gce394404de
Previous: SZEDER GáborNext: Jeff King
Message 2 of 13 in “tests: don't mess with fd 7 of test helper functions”
  1. 1/2 tests: don't mess with fd 7 of test helper functionsSZEDER Gábor, Feb 21, 2021
  2. 2/2 test-lib-functions: use BUG() in 'test_must_fail'SZEDER Gábor, Feb 21, 2021
  3. Jeff KingFeb 21, 2021
  4. Jeff KingFeb 22, 2021
  5. Jeff KingFeb 22, 2021
  6. Junio C HamanoFeb 22, 2021
  7. SZEDER GáborApr 14, 2026
  8. Junio C HamanoApr 14, 2026
  9. Jeff KingApr 14, 2026
  10. Junio C HamanoApr 15, 2026
  11. Jeff KingApr 14, 2026
  12. Jeff KingFeb 21, 2021
  13. Junio C HamanoFeb 22, 2021

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.