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

[PATCH v2 2/7] test-lib: parse some --options earlier

From
SZEDER Gábor <szeder.dev@gmail.com>
Date
Dec 9, 2018, 22:56 UTC
Message-ID
<20181209225628.22216-3-szeder.dev@gmail.com>
In-Reply-To
<20181209225628.22216-1-szeder.dev@gmail.com>

'test-lib.sh' looks for the presence of certain options like '--tee' and '--verbose-log', so it can execute the test script again to save its standard output and error. This happens way before the actual option parsing loop, and the condition looking for these options looks a bit odd, too. This patch series will add two more options to look out for, and, in addition, will have to extract these options' stuck arguments (i.e. '--opt=arg') as well.

Add a proper option parsing loop to check these select options early in 'test-lib.sh', making this early option checking more readable and keeping those later changes in this series simpler. Use a 'for opt in "$@"' loop to iterate over the options to preserve "$@" intact, so options like '--verbose-log' can execute the test script again with all the original options.

As an alternative, we could parse all options early, but there are options that do require an _unstuck_ argument, which is tricky to handle properly in such a for loop, and the resulting complexity is, in my opinion, worse than having this extra, partial option parsing loop.

Signed-off-by: SZEDER Gábor <szeder.dev@gmail.com>
---
 t/test-lib.sh | 53 +++++++++++++++++++++++++++++++--------------------
 1 file changed, 32 insertions(+), 21 deletions(-)
diff --git a/t/test-lib.sh b/t/test-lib.sh
index 9a3f7930a3..5577d9dc5a 100644
--- a/t/test-lib.sh
+++ b/t/test-lib.sh
@@ -71,13 +71,33 @@ then
 	exit 1
 fi
 
+# Parse some options early, taking care to leave $@ intact.
+for opt
+do
+	case "$opt" in
+	--tee)
+		tee=t ;;
+	-V|--verbose-log)
+		verbose_log=t ;;
+	--va|--val|--valg|--valgr|--valgri|--valgrin|--valgrind)
+		valgrind=memcheck ;;
+	--valgrind=*)
+		valgrind=${opt#--*=} ;;
+	--valgrind-only=*)
+		valgrind_only=${opt#--*=} ;;
+	*)
+		# Other options will be handled later.
+	esac
+done
+
 # if --tee was passed, write the output not only to the terminal, but
 # additionally to the file test-results/$BASENAME.out, too.
-case "$GIT_TEST_TEE_STARTED, $* " in
-done,*)
-	# do not redirect again
-	;;
-*' --tee '*|*' --va'*|*' -V '*|*' --verbose-log '*)
+if test "$GIT_TEST_TEE_STARTED" = "done"
+then
+	: # do not redirect again
+elif test -n "$tee" || test -n "$verbose_log" ||
+     test -n "$valgrind" || test -n "$valgrind_only"
+then
 	mkdir -p "$TEST_OUTPUT_DIRECTORY/test-results"
 	BASE="$TEST_OUTPUT_DIRECTORY/test-results/$(basename "$0" .sh)"
 
@@ -94,8 +114,7 @@ done,*)
 	 echo $? >"$BASE.exit") | tee -a "$GIT_TEST_TEE_OUTPUT_FILE"
 	test "$(cat "$BASE.exit")" = 0
 	exit
-	;;
-esac
+fi
 
 # For repeatability, reset the environment to known value.
 # TERM is sanitized below, after saving color control sequences.
@@ -296,17 +315,6 @@ do
 		with_dashes=t; shift ;;
 	--no-color)
 		color=; shift ;;
-	--va|--val|--valg|--valgr|--valgri|--valgrin|--valgrind)
-		valgrind=memcheck
-		shift ;;
-	--valgrind=*)
-		valgrind=${1#--*=}
-		shift ;;
-	--valgrind-only=*)
-		valgrind_only=${1#--*=}
-		shift ;;
-	--tee)
-		shift ;; # was handled already
 	--root=*)
 		root=${1#--*=}
 		shift ;;
@@ -336,9 +344,12 @@ do
 			echo >&2 "warning: ignoring -x; '$0' is untraceable without BASH_XTRACEFD"
 		fi
 		shift ;;
-	-V|--verbose-log)
-		verbose_log=t
-		shift ;;
+	--tee|\
+	-V|--verbose-log|\
+	--va|--val|--valg|--valgr|--valgri|--valgrin|--valgrind|\
+	--valgrind=*|\
+	--valgrind-only=*)
+		shift ;; # These options were handled already.
 	*)
 		echo "error: unknown test option '$1'" >&2; exit 1 ;;
 	esac
-- 
2.20.0.rc2.156.g5a9fd2ce9c
Previous: SZEDER GáborNext: Jeff King
Message 31 of 67 in “test-lib: add the '--stress' option to help reproduce occasional failures in flaky tests”
  1. 0/3 test-lib: add the '--stress' option to help reproduce occasional failures in flaky testsSZEDER Gábor, Dec 4, 2018
  2. 1/3 test-lib: consolidate naming of test-results pathsSZEDER Gábor, Dec 4, 2018
  3. Jeff KingDec 5, 2018
  4. 2/3 test-lib-functions: introduce the 'test_set_port' helper functionSZEDER Gábor, Dec 4, 2018
  5. Jeff KingDec 5, 2018
  6. SZEDER GáborDec 5, 2018
  7. Jeff KingDec 5, 2018
  8. 3/3 test-lib: add the '--stress' option to run a test repeatedly under loadSZEDER Gábor, Dec 4, 2018
  9. Ævar Arnfjörð BjarmasonDec 4, 2018
  10. SZEDER GáborDec 4, 2018
  11. Jeff KingDec 5, 2018
  12. Ævar Arnfjörð BjarmasonDec 4, 2018
  13. Jeff KingDec 5, 2018
  14. SZEDER GáborDec 5, 2018
  15. Ævar Arnfjörð BjarmasonDec 5, 2018
  16. SZEDER GáborDec 5, 2018
  17. Ævar Arnfjörð BjarmasonDec 5, 2018
  18. Jeff KingDec 5, 2018
  19. SZEDER GáborDec 5, 2018
  20. Jeff KingDec 5, 2018
  21. Junio C HamanoDec 6, 2018
  22. Jeff KingDec 6, 2018
  23. Junio C HamanoDec 6, 2018
  24. SZEDER GáborDec 6, 2018
  25. Jeff KingDec 7, 2018
  26. SZEDER GáborDec 5, 2018
  27. Jeff KingDec 5, 2018
  28. SZEDER GáborDec 6, 2018
  29. Jeff KingDec 7, 2018
  30. 0/7 test-lib: add the '--stress' option to help reproduce occasional failures in flaky testsSZEDER Gábor, Dec 9, 2018
  31. 2/7 test-lib: parse some --options earlierSZEDER Gábor, Dec 9, 2018
  32. Jeff KingDec 11, 2018
  33. SZEDER GáborDec 11, 2018
  34. Jeff KingDec 17, 2018
  35. SZEDER GáborDec 30, 2018
  36. Jeff KingJan 3, 2019
  37. 1/7 test-lib: translate SIGTERM and SIGHUP to an exitSZEDER Gábor, Dec 9, 2018
  38. Jeff KingDec 11, 2018
  39. 4/7 test-lib: set $TRASH_DIRECTORY earlierSZEDER Gábor, Dec 9, 2018
  40. 5/7 test-lib: extract Bash version check for '-x' tracingSZEDER Gábor, Dec 9, 2018
  41. 3/7 test-lib: consolidate naming of test-results pathsSZEDER Gábor, Dec 9, 2018
  42. 6/7 test-lib-functions: introduce the 'test_set_port' helper functionSZEDER Gábor, Dec 9, 2018
  43. 7/7 test-lib: add the '--stress' option to run a test repeatedly under loadSZEDER Gábor, Dec 9, 2018
  44. fixup! test-lib: add the '--stress' option to run a test repeatedly under loadSZEDER Gábor, Dec 10, 2018
  45. Jeff KingDec 11, 2018
  46. 0/8 test-lib: add the '--stress' option to help reproduce occasional failures in flaky testsSZEDER Gábor, Dec 30, 2018
  47. 1/8 test-lib: translate SIGTERM and SIGHUP to an exitSZEDER Gábor, Dec 30, 2018
  48. 2/8 test-lib: parse options in a for loop to keep $@ intactSZEDER Gábor, Dec 30, 2018
  49. 3/8 test-lib: parse command line options earlierSZEDER Gábor, Dec 30, 2018
  50. 6/8 test-lib: extract Bash version check for '-x' tracingSZEDER Gábor, Dec 30, 2018
  51. Carlo ArenasDec 31, 2018
  52. 7/8 test-lib-functions: introduce the 'test_set_port' helper functionSZEDER Gábor, Dec 30, 2018
  53. 5/8 test-lib: set $TRASH_DIRECTORY earlierSZEDER Gábor, Dec 30, 2018
  54. SZEDER GáborDec 30, 2018
  55. 5/8 test-lib: set $TRASH_DIRECTORY earlierSZEDER Gábor, Dec 30, 2018
  56. 4/8 test-lib: consolidate naming of test-results pathsSZEDER Gábor, Dec 30, 2018
  57. 8/8 test-lib: add the '--stress' option to run a test repeatedly under loadSZEDER Gábor, Dec 30, 2018
  58. 0/8 test-lib: add the '--stress' option to help reproduce occasional failures in flaky testsSZEDER Gábor, Jan 5, 2019
  59. 1/8 test-lib: translate SIGTERM and SIGHUP to an exitSZEDER Gábor, Jan 5, 2019
  60. 2/8 test-lib: extract Bash version check for '-x' tracingSZEDER Gábor, Jan 5, 2019
  61. 3/8 test-lib: parse options in a for loop to keep $@ intactSZEDER Gábor, Jan 5, 2019
  62. 4/8 test-lib: parse command line options earlierSZEDER Gábor, Jan 5, 2019
  63. 5/8 test-lib: consolidate naming of test-results pathsSZEDER Gábor, Jan 5, 2019
  64. 6/8 test-lib: set $TRASH_DIRECTORY earlierSZEDER Gábor, Jan 5, 2019
  65. 8/8 test-lib: add the '--stress' option to run a test repeatedly under loadSZEDER Gábor, Jan 5, 2019
  66. 7/8 test-lib-functions: introduce the 'test_set_port' helper functionSZEDER Gábor, Jan 5, 2019
  67. Jeff KingJan 7, 2019

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.