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

[PATCH v2 02/12] t: prepare `test_must_fail ()` for `set -e`

From
Patrick Steinhardt <ps@pks.im>
Date
Apr 15, 2026, 13:06 UTC
Message-ID
<20260415-b4-pks-tests-with-set-e-v2-2-4e4904a96f15@pks.im>
In-Reply-To
<20260415-b4-pks-tests-with-set-e-v2-0-4e4904a96f15@pks.im>

The helper function `test_must_fail ()` executes a specific Git command that may or may not fail in a specific way. This is done by executing the command in question and then comparing its exit code against a set of conditions.

This works, but once we run our test suite with `set -e` we may bail out of `test_must_fail ()` early in case the command actually fails, even though we expect it to fail. Prepare for this change by handling the failed case with `||`.

Signed-off-by: Patrick Steinhardt <ps@pks.im>
---
 t/test-lib-functions.sh | 5 +++--
 1 file changed, 3 insertions(+), 2 deletions(-)
diff --git a/t/test-lib-functions.sh b/t/test-lib-functions.sh
index f3af10fb7e..5fd5494ef1 100644
--- a/t/test-lib-functions.sh
+++ b/t/test-lib-functions.sh
@@ -1195,8 +1195,9 @@ test_must_fail () {
 		echo >&7 "test_must_fail: only 'git' is allowed: $*"
 		return 1
 	fi
-	"$@" 2>&7
-	exit_code=$?
+
+	exit_code=0; "$@" 2>&7 || exit_code=$?
+
 	if test $exit_code -eq 0 && ! list_contains "$_test_ok" success
 	then
 		echo >&4 "test_must_fail: command succeeded: $*"
-- 
2.54.0.rc2.529.gd9106f7525.dirty
Previous: Patrick SteinhardtNext: Patrick Steinhardt
Message 3 of 15 in “t: detect errors outside of test cases”
  1. 00/12 t: detect errors outside of test casesPatrick Steinhardt, Apr 15, 2026
  2. 01/12 t: prepare `test_match_signal ()` calls for `set -e`Patrick Steinhardt, Apr 15, 2026
  3. 02/12 t: prepare `test_must_fail ()` for `set -e`Patrick Steinhardt, Apr 15, 2026
  4. 03/12 t: prepare `stop_git_daemon ()` for `set -e`Patrick Steinhardt, Apr 15, 2026
  5. 04/12 t: prepare `git config --unset` calls for `set -e`Patrick Steinhardt, Apr 15, 2026
  6. 05/12 t: prepare conditional test execution for `set -e`Patrick Steinhardt, Apr 15, 2026
  7. 06/12 t: prepare execution of potentially failing commands for `set -e`Patrick Steinhardt, Apr 15, 2026
  8. 07/12 t: prepare `test_when_finished ()`/`test_atexit()` for `set -e`Patrick Steinhardt, Apr 15, 2026
  9. 08/12 t0008: silence error in subshell when using `grep -v`Patrick Steinhardt, Apr 15, 2026
  10. 09/12 t1301: don't fail in case setfacl(1) doesn't exist or failsPatrick Steinhardt, Apr 15, 2026
  11. 10/12 t6002: fix use of `expr` with `set -e`Patrick Steinhardt, Apr 15, 2026
  12. 11/12 t9902: fix use of `read` with `set -e`Patrick Steinhardt, Apr 15, 2026
  13. 12/12 t: detect errors outside of test casesPatrick Steinhardt, Apr 15, 2026
  14. Jeff KingApr 16, 2026
  15. Patrick SteinhardtApr 16, 2026

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.