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

Re: [PATCH 07/12] t: prepare `test_when_finished ()`/`test_atexit()` for `set -e`

From
Patrick Steinhardt <ps@pks.im>
Date
Apr 14, 2026, 07:24 UTC
Message-ID
<ad3rkmf2227AWrVD@pks.im>
In-Reply-To
<xmqqpl42ixk6.fsf@gitster.g>
On Mon, Apr 13, 2026 at 10:23:37AM -0700, Junio C Hamano wrote:
Show 73 quoted lines
> Patrick Steinhardt <ps@pks.im> writes:
> 
> > Both `test_when_finished ()` and `test_atexit ()` build up a chain of
> > cleanup commands by prepending each new command to the existing cleanup
> > string. To preserve the exit code of the test body across cleanup
> > execution, we append the following logic:
> >
> >     } && (exit "$eval_ret"); eval_ret=$?; ...
> >
> > The intent of this is to run the cleanup block and then unconditionally
> > restore `eval_ret`. The original behaviour of this is is:
> >
> >    +------------------+---------+------------------------------------+
> >    |test body         │ cleanup │ old behaviour                      │
> >    +------------------+---------+------------------------------------+
> >    │pass (eval_ret=0) | pass    │ && taken -> (exit 0) -> eval_ret=0 |
> >    +------------------+---------+------------------------------------+
> >    │pass (eval_ret=0) | fail    │ && not taken -> eval_ret=$?        |
> >    +------------------+---------+------------------------------------+
> >    │fail (eval_ret=1) | pass    │ && taken -> (exit 1) -> eval_ret=1 |
> >    +------------------+---------+------------------------------------+
> >    │fail (eval_ret=1) | fail    | && not taken -> eval_ret=$?        |
> >    +------------------+---------+------------------------------------+
> >
> > This logic will start to fail once we enable `set -e`. When `$eval_ret`
> > is non-zero, the subshell we create will fail, and with `set -e` we'll
> > thus bail out without evaluating the logic after the semicolon.
> >
> > Fix this issue by instead using `|| eval_ret=\$?; ...`. Besides being
> > a bit simpler, it also retains the original behaviour:
> >
> >    +------------------+---------+------------------------------------+
> >    |test body         │ cleanup │ old behaviour                      │
> >    +------------------+---------+------------------------------------+
> >    │pass (eval_ret=0) | pass    │ || not taken -> eval_ret unchanged |
> >    +------------------+---------+------------------------------------+
> >    │pass (eval_ret=0) | fail    │ || taken -> eval_ret=$?            |
> >    +------------------+---------+------------------------------------+
> >    │fail (eval_ret=1) | pass    │ || not taken -> eval_ret unchanged |
> >    +------------------+---------+------------------------------------+
> >    │fail (eval_ret=1) | fail    | || taken -> eval_ret=$?            |
> >    +------------------+---------+------------------------------------+
> >
> > Signed-off-by: Patrick Steinhardt <ps@pks.im>
> > ---
> >  t/test-lib-functions.sh | 4 ++--
> >  1 file changed, 2 insertions(+), 2 deletions(-)
> >
> > diff --git a/t/test-lib-functions.sh b/t/test-lib-functions.sh
> > index f8bc77619b..0eac676109 100644
> > --- a/t/test-lib-functions.sh
> > +++ b/t/test-lib-functions.sh
> > @@ -1516,7 +1516,7 @@ test_when_finished () {
> >  	test "${BASH_SUBSHELL-0}" = 0 ||
> >  	BUG "test_when_finished does nothing in a subshell"
> >  	test_cleanup="{ $*
> > -		} && (exit \"\$eval_ret\"); eval_ret=\$?; $test_cleanup"
> > +		} || eval_ret=\$?; $test_cleanup"
> 
> Hmph, it seems this "&& (exit $eval_ret)" pattern has been with us
> forever since it was introduced at 3bf78867 (test-lib: Let tests
> specify commands to be run at end of test, 2010-05-02), and survived
> a slight modification to work around issues on FreeBSD done in
> b6b0afdc (test-lib: some shells do not let $? propagate into an
> eval, 2010-05-06).  When a major part of test-lib.sh was split into
> test-lib-functions.s at 12a29b1a (Move the user-facing test library
> to test-lib-functions.sh, 2012-02-17), this part was copied intact.
> 
> Does this interact well with the glitch b6b0afdc (test-lib: some
> shells do not let $? propagate into an eval, 2010-05-06) tried to
> work around, by the way?
> 
> Thanks.

Ah, that's a very good question. I'll spin up a FreeBSD VM to give this a test, thanks!

Patrick
Previous: Junio C HamanoNext: Patrick Steinhardt
Message 39 of 95 in “t: detect errors outside of test cases”
  1. 00/12 t: detect errors outside of test casesPatrick Steinhardt, Apr 13, 2026
  2. 01/12 t: prepare `test_match_signal ()` calls for `set -e`Patrick Steinhardt, Apr 13, 2026
  3. Junio C HamanoApr 13, 2026
  4. Patrick SteinhardtApr 14, 2026
  5. Junio C HamanoApr 14, 2026
  6. 02/12 t: prepare `test_must_fail ()` for `set -e`Patrick Steinhardt, Apr 13, 2026
  7. Junio C HamanoApr 13, 2026
  8. Patrick SteinhardtApr 14, 2026
  9. Jeff KingApr 14, 2026
  10. Junio C HamanoApr 14, 2026
  11. Patrick SteinhardtApr 15, 2026
  12. Jeff KingApr 16, 2026
  13. 03/12 t: prepare `stop_git_daemon ()` for `set -e`Patrick Steinhardt, Apr 13, 2026
  14. Junio C HamanoApr 13, 2026
  15. 04/12 t: prepare `git config --unset` calls for `set -e`Patrick Steinhardt, Apr 13, 2026
  16. Junio C HamanoApr 13, 2026
  17. 05/12 t: prepare conditional test execution for `set -e`Patrick Steinhardt, Apr 13, 2026
  18. Junio C HamanoApr 13, 2026
  19. 06/12 t: prepare execution of potentially failing commands for `set -e`Patrick Steinhardt, Apr 13, 2026
  20. Junio C HamanoApr 13, 2026
  21. Patrick SteinhardtApr 14, 2026
  22. Junio C HamanoApr 14, 2026
  23. Junio C HamanoApr 13, 2026
  24. Junio C HamanoApr 14, 2026
  25. Patrick SteinhardtApr 14, 2026
  26. Patrick SteinhardtApr 14, 2026
  27. Junio C HamanoApr 14, 2026
  28. Jeff KingApr 14, 2026
  29. Jeff KingApr 14, 2026
  30. Jeff KingApr 14, 2026
  31. Patrick SteinhardtApr 15, 2026
  32. Jeff KingApr 16, 2026
  33. Patrick SteinhardtApr 16, 2026
  34. Junio C HamanoApr 16, 2026
  35. Jeff KingApr 18, 2026
  36. Junio C HamanoApr 15, 2026
  37. 07/12 t: prepare `test_when_finished ()`/`test_atexit()` for `set -e`Patrick Steinhardt, Apr 13, 2026
  38. Junio C HamanoApr 13, 2026
  39. Patrick SteinhardtApr 14, 2026
  40. 08/12 t0008: silence error in subshell when using `grep -v`Patrick Steinhardt, Apr 13, 2026
  41. Junio C HamanoApr 13, 2026
  42. Patrick SteinhardtApr 14, 2026
  43. 09/12 t1301: don't fail in case setfacl(1) doesn't exist or failsPatrick Steinhardt, Apr 13, 2026
  44. 10/12 t6002: fix use of `expr` with `set -e`Patrick Steinhardt, Apr 13, 2026
  45. 11/12 t9902: fix use of `read` with `set -e`Patrick Steinhardt, Apr 13, 2026
  46. 12/12 t: detect errors outside of test casesPatrick Steinhardt, Apr 13, 2026
  47. Junio C HamanoApr 13, 2026
  48. Junio C HamanoApr 13, 2026
  49. Junio C HamanoApr 13, 2026
  50. 00/12 t: detect errors outside of test casesPatrick Steinhardt, Apr 16, 2026
  51. 01/12 t: prepare `test_match_signal ()` calls for `set -e`Patrick Steinhardt, Apr 16, 2026
  52. 02/12 t: prepare `test_must_fail ()` for `set -e`Patrick Steinhardt, Apr 16, 2026
  53. 03/12 t: prepare `stop_git_daemon ()` for `set -e`Patrick Steinhardt, Apr 16, 2026
  54. 04/12 t: prepare `git config --unset` calls for `set -e`Patrick Steinhardt, Apr 16, 2026
  55. 05/12 t: prepare conditional test execution for `set -e`Patrick Steinhardt, Apr 16, 2026
  56. 06/12 t: prepare execution of potentially failing commands for `set -e`Patrick Steinhardt, Apr 16, 2026
  57. 07/12 t: prepare `test_when_finished ()`/`test_atexit()` for `set -e`Patrick Steinhardt, Apr 16, 2026
  58. 08/12 t0008: silence error in subshell when using `grep -v`Patrick Steinhardt, Apr 16, 2026
  59. 09/12 t1301: don't fail in case setfacl(1) doesn't exist or failsPatrick Steinhardt, Apr 16, 2026
  60. 10/12 t6002: fix use of `expr` with `set -e`Patrick Steinhardt, Apr 16, 2026
  61. 11/12 t9902: fix use of `read` with `set -e`Patrick Steinhardt, Apr 16, 2026
  62. SZEDER GáborApr 16, 2026
  63. Junio C HamanoApr 16, 2026
  64. Patrick SteinhardtApr 17, 2026
  65. 12/12 t: detect errors outside of test casesPatrick Steinhardt, Apr 16, 2026
  66. Junio C HamanoApr 16, 2026
  67. 00/12 t: detect errors outside of test casesPatrick Steinhardt, Apr 20, 2026
  68. 01/12 t: prepare `test_match_signal ()` calls for `set -e`Patrick Steinhardt, Apr 20, 2026
  69. 02/12 t: prepare `test_must_fail ()` for `set -e`Patrick Steinhardt, Apr 20, 2026
  70. 03/12 t: prepare `stop_git_daemon ()` for `set -e`Patrick Steinhardt, Apr 20, 2026
  71. 04/12 t: prepare `git config --unset` calls for `set -e`Patrick Steinhardt, Apr 20, 2026
  72. 05/12 t: prepare conditional test execution for `set -e`Patrick Steinhardt, Apr 20, 2026
  73. 06/12 t: prepare execution of potentially failing commands for `set -e`Patrick Steinhardt, Apr 20, 2026
  74. 07/12 t: prepare `test_when_finished ()`/`test_atexit()` for `set -e`Patrick Steinhardt, Apr 20, 2026
  75. 08/12 t0008: silence error in subshell when using `grep -v`Patrick Steinhardt, Apr 20, 2026
  76. 09/12 t1301: don't fail in case setfacl(1) doesn't exist or failsPatrick Steinhardt, Apr 20, 2026
  77. 10/12 t6002: fix use of `expr` with `set -e`Patrick Steinhardt, Apr 20, 2026
  78. 11/12 t9902: fix use of `read` with `set -e`Patrick Steinhardt, Apr 20, 2026
  79. 12/12 t: detect errors outside of test casesPatrick Steinhardt, Apr 20, 2026
  80. Junio C HamanoApr 20, 2026
  81. Jeff KingApr 21, 2026
  82. Patrick SteinhardtApr 21, 2026
  83. 00/12 t: detect errors outside of test casesPatrick Steinhardt, Apr 21, 2026
  84. 01/12 t: prepare `test_match_signal ()` calls for `set -e`Patrick Steinhardt, Apr 21, 2026
  85. 02/12 t: prepare `test_must_fail ()` for `set -e`Patrick Steinhardt, Apr 21, 2026
  86. 03/12 t: prepare `stop_git_daemon ()` for `set -e`Patrick Steinhardt, Apr 21, 2026
  87. 04/12 t: prepare `git config --unset` calls for `set -e`Patrick Steinhardt, Apr 21, 2026
  88. 05/12 t: prepare conditional test execution for `set -e`Patrick Steinhardt, Apr 21, 2026
  89. 06/12 t: prepare execution of potentially failing commands for `set -e`Patrick Steinhardt, Apr 21, 2026
  90. 07/12 t: prepare `test_when_finished ()`/`test_atexit()` for `set -e`Patrick Steinhardt, Apr 21, 2026
  91. 08/12 t0008: silence error in subshell when using `grep -v`Patrick Steinhardt, Apr 21, 2026
  92. 09/12 t1301: don't fail in case setfacl(1) doesn't exist or failsPatrick Steinhardt, Apr 21, 2026
  93. 10/12 t6002: fix use of `expr` with `set -e`Patrick Steinhardt, Apr 21, 2026
  94. 11/12 t9902: fix use of `read` with `set -e`Patrick Steinhardt, Apr 21, 2026
  95. 12/12 t: detect errors outside of test casesPatrick Steinhardt, Apr 21, 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.