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

[PATCH 0/8] more &&-chaining test fixups

From
Jeff King <peff@peff.net>
Date
Mar 25, 2015, 05:24 UTC
Message-ID
<20150325052456.GA19394@peff.net>
In-Reply-To
<20150325025635.GC15051@peff.net>
Here's what I found looking for loops like:
  for i in a b c; do
     something_important $i || break
  done &&
  something_else

which presumably expect the chain to stop when something_important fails for any loop element. The solutions are one of (depending on the surrounding code):

  1. Switch the break to "return 1". The tests are all &&-chained, so
     the effect of a failed command is to exit the test immediately
     anyway. And we wrap our eval'd test snippets inside an extra layer
     of function call to explicitly allow early returns like this.
  2. Switch the break to "exit 1". Calling "return" from a subshell
     inside a function is a bit weird. It doesn't exit the function at
     all, but rather just the subshell (in both bash and dash). But if
     you are not in a function, calling "return" at all is an error in
     bash (subshell or no), and OK in dash (where it acts like "exit").
     POSIX explicitly marks the "outside of a function" behavior as
     unspecified, but I couldn't find anything about the subshell
     behavior.
     So I'm loathe to depend on it, even though it does seem to do what
     we want, as I do not want to even think what some more obscure
     shells might do with it. And especially because we know that "exit
     1" portably does what we want (the only downside is that you have
     to recognize which situation you are in and use "exit" versus
     "return").
  3. Unroll the loops. In some cases the result is actually shorter, and
     (IMHO) more readable.
These sites were all found with:
  git grep -E '(^|\|\|)[ 	]*break' t/t*.sh

(that's a space and a tab in the brackets). There are some matches there that I did not touch, because they were already fine. E.g., t5302 and t7700 use loops to assign a value to a variable and break out early, and then check the value of the variable.

That's just the tip of the iceberg, though. Searching for
  git grep 'for .* in '

yields hundreds of hits. Most of which are probably fine (quite a few are outside &&-chains entirely). I focused on the ones that called break, because that indicated to me that the author was trying to address the &&-chain. Certainly anybody else is welcome to take a stab at the rest, but I'm also happy to fix them up as we touch nearby code and notice them. Most of the loops are in setup code that we do not expect to fail anyway, so examining them is a lot of work for a little gain.

There were a few legitimate problems, though. I've ordered the patches below by descending severity. These apply on top of jk/test-chain-lint.

  [1/8]: perf-lib: fix ignored exit code inside loop
  [2/8]: t0020: fix ignored exit code inside loops
  [3/8]: t3305: fix ignored exit code inside loop
  [4/8]: t7701: fix ignored exit code inside loop
    These four are actual bugs.
  [5/8]: t: fix some trivial cases of ignored exit codes in loops
    These ones are in setup code, and so would almost certainly never
    fail.
  [6/8]: t: simplify loop exit-code status variables
  [7/8]: t0020: use test_* helpers instead of hand-rolled messages
  [8/8]: t9001: drop save_confirm helper
    These last three are pure cleanup, no behavior changes. The last two
    are not even strictly related to the same topic, but I noticed them
    while in the area.
-Peff
Previous: Jeff KingNext: Jeff King
Message 25 of 68 in “detecting &&-chain breakage”
  1. 0/25 detecting &&-chain breakageJeff King, Mar 20, 2015
  2. 01/25 t/test-lib: introduce --chain-lint optionJeff King, Mar 20, 2015
  3. SZEDER GáborMar 25, 2015
  4. Jeff KingMar 25, 2015
  5. 02/25 t: fix severe &&-chain breakageJeff King, Mar 20, 2015
  6. 03/25 t: fix moderate &&-chain breakageJeff King, Mar 20, 2015
  7. 04/25 t: fix trivial &&-chain breakageJeff King, Mar 20, 2015
  8. 05/25 t: assume test_cmp produces verbose outputJeff King, Mar 20, 2015
  9. 06/25 t: use verbose instead of hand-rolled errorsJeff King, Mar 20, 2015
  10. 07/25 t: use test_must_fail instead of hand-rolled blocksJeff King, Mar 20, 2015
  11. 08/25 t: fix &&-chaining issues around setup which might failJeff King, Mar 20, 2015
  12. 09/25 t: use test_might_fail for diff and grepJeff King, Mar 20, 2015
  13. 10/25 t: use test_expect_code instead of hand-rolled comparisonJeff King, Mar 20, 2015
  14. 11/25 t: wrap complicated expect_code users in a blockJeff King, Mar 20, 2015
  15. 12/25 t: avoid using ":" for commentsJeff King, Mar 20, 2015
  16. 13/25 t3600: fix &&-chain breakage for setup commandsJeff King, Mar 20, 2015
  17. 14/25 t7201: fix &&-chain breakageJeff King, Mar 20, 2015
  18. 15/25 t9502: fix &&-chain breakageJeff King, Mar 20, 2015
  19. Johannes SixtMar 20, 2015
  20. Jeff KingMar 20, 2015
  21. 16/25 t6030: use modern test_* helpersJeff King, Mar 20, 2015
  22. 17/25 t0020: use modern test_* helpersJeff King, Mar 20, 2015
  23. SZEDER GáborMar 25, 2015
  24. Jeff KingMar 25, 2015
  25. 0/8 more &&-chaining test fixupsJeff King, Mar 25, 2015
  26. 1/8 perf-lib: fix ignored exit code inside loopJeff King, Mar 25, 2015
  27. 2/8 t0020: fix ignored exit code inside loopsJeff King, Mar 25, 2015
  28. 3/8 t3305: fix ignored exit code inside loopJeff King, Mar 25, 2015
  29. Johan HerlandMar 25, 2015
  30. 4/8 t7701: fix ignored exit code inside loopJeff King, Mar 25, 2015
  31. 5/8 t: fix some trivial cases of ignored exit codes in loopsJeff King, Mar 25, 2015
  32. 6/8 t: simplify loop exit-code status variablesJeff King, Mar 25, 2015
  33. Junio C HamanoMar 25, 2015
  34. Jeff KingMar 25, 2015
  35. 7/8 t0020: use test_* helpers instead of hand-rolled messagesJeff King, Mar 25, 2015
  36. 8/8 t9001: drop save_confirm helperJeff King, Mar 25, 2015
  37. Junio C HamanoMar 25, 2015
  38. 18/25 t1301: use modern test_* helpersJeff King, Mar 20, 2015
  39. SZEDER GáborMar 24, 2015
  40. Jeff KingMar 25, 2015
  41. 19/25 t6034: use modern test_* helpersJeff King, Mar 20, 2015
  42. SZEDER GáborMar 24, 2015
  43. 20/25 t4117: use modern test_* helpersJeff King, Mar 20, 2015
  44. 21/25 t9001: use test_when_finishedJeff King, Mar 20, 2015
  45. SZEDER GáborMar 25, 2015
  46. Jeff KingMar 25, 2015
  47. 22/25 t0050: appease --chain-lintJeff King, Mar 20, 2015
  48. 23/25 t7004: fix embedded single-quotesJeff King, Mar 20, 2015
  49. 24/25 t0005: fix broken &&-chainsJeff King, Mar 20, 2015
  50. 25/25 t4104: drop hand-rolled error reportingJeff King, Mar 20, 2015
  51. Jeff KingMar 20, 2015
  52. Michael J GruberMar 20, 2015
  53. 26/27 t/*svn*: fix moderate &&-chain breakageMichael J Gruber, Mar 20, 2015
  54. Junio C HamanoMar 20, 2015
  55. Junio C HamanoMar 20, 2015
  56. Jeff KingMar 20, 2015
  57. Jeff KingMar 20, 2015
  58. Michael J GruberMar 23, 2015
  59. 27/27 t9104: fix test for following larger parentsMichael J Gruber, Mar 20, 2015
  60. Jeff KingMar 20, 2015
  61. Junio C HamanoMar 20, 2015
  62. Junio C HamanoMar 20, 2015
  63. Jeff KingMar 20, 2015
  64. Junio C HamanoMar 20, 2015
  65. Eric SunshineMar 20, 2015
  66. Jeff KingMar 21, 2015
  67. Junio C HamanoMar 21, 2015
  68. Jeff KingMar 21, 2015

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.