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

[PATCH 14/19] tests: apply modern idiom for exiting loop upon failure

From
Eric Sunshine <sunshine@sunshineco.com>
Date
Dec 9, 2021, 05:11 UTC
Message-ID
<20211209051115.52629-15-sunshine@sunshineco.com>
In-Reply-To
<20211209051115.52629-1-sunshine@sunshineco.com>

Rather than maintaining a flag indicating a failure within a loop and aborting the test when the loop ends if the flag is set, modern practice is to signal the failure immediately by exiting the loop early via `return 1` (or `exit 1` if inside a subshell). Simplify these loops by following the modern idiom.

Signed-off-by: Eric Sunshine <sunshine@sunshineco.com>
---
 t/t1050-large.sh                | 26 ++++++++------------------
 t/t5505-remote.sh               |  6 ++----
 t/t9400-git-cvsserver-server.sh |  5 ++---
 3 files changed, 12 insertions(+), 25 deletions(-)
diff --git a/t/t1050-large.sh b/t/t1050-large.sh
index 99ff2866b7..0e4267c723 100755
--- a/t/t1050-large.sh
+++ b/t/t1050-large.sh
@@ -51,27 +51,21 @@ EOF
 test_expect_success 'add a large file or two' '
 	git add large1 huge large2 &&
 	# make sure we got a single packfile and no loose objects
-	bad= count=0 idx= &&
+	count=0 idx= &&
 	for p in .git/objects/pack/pack-*.pack
 	do
 		count=$(( $count + 1 )) &&
-		if test_path_is_file "$p" &&
-		   idx=${p%.pack}.idx && test_path_is_file "$idx"
-		then
-			continue
-		fi
-		bad=t
+		test_path_is_file "$p" &&
+		idx=${p%.pack}.idx &&
+		test_path_is_file "$idx" || return 1
 	done &&
-	test -z "$bad" &&
 	test $count = 1 &&
 	cnt=$(git show-index <"$idx" | wc -l) &&
 	test $cnt = 2 &&
 	for l in .git/objects/$OIDPATH_REGEX
 	do
-		test_path_is_file "$l" || continue
-		bad=t
+		test_path_is_missing "$l" || return 1
 	done &&
-	test -z "$bad" &&
 
 	# attempt to add another copy of the same
 	git add large3 &&
@@ -79,14 +73,10 @@ test_expect_success 'add a large file or two' '
 	for p in .git/objects/pack/pack-*.pack
 	do
 		count=$(( $count + 1 )) &&
-		if test_path_is_file "$p" &&
-		   idx=${p%.pack}.idx && test_path_is_file "$idx"
-		then
-			continue
-		fi
-		bad=t
+		test_path_is_file "$p" &&
+		idx=${p%.pack}.idx &&
+		test_path_is_file "$idx" || return 1
 	done &&
-	test -z "$bad" &&
 	test $count = 1
 '
 
diff --git a/t/t5505-remote.sh b/t/t5505-remote.sh
index e6e3c8f552..5ef8db481c 100755
--- a/t/t5505-remote.sh
+++ b/t/t5505-remote.sh
@@ -1332,7 +1332,6 @@ test_expect_success 'unqualified <dst> refspec DWIM and advice' '
 	(
 		cd test &&
 		git tag -a -m "Some tag" some-tag main &&
-		exit_with=true &&
 		for type in commit tag tree blob
 		do
 			if test "$type" = "blob"
@@ -1348,9 +1347,8 @@ test_expect_success 'unqualified <dst> refspec DWIM and advice' '
 				push origin $oid:dst 2>err &&
 			test_i18ngrep "error: The destination you" err &&
 			test_i18ngrep ! "hint: Did you mean" err ||
-			exit_with=false
-		done &&
-		$exit_with
+			exit 1
+		done
 	)
 '
 
diff --git a/t/t9400-git-cvsserver-server.sh b/t/t9400-git-cvsserver-server.sh
index 17f988edd2..a6a73effde 100755
--- a/t/t9400-git-cvsserver-server.sh
+++ b/t/t9400-git-cvsserver-server.sh
@@ -350,10 +350,9 @@ test_expect_success 'cvs update (subdirectories)' \
 	test_cmp "$dir/$filename" "../$dir/$filename"; then
         :
       else
-        echo >failure
+        exit 1
       fi
-    done) &&
-   test ! -f failure'
+    done)'
 
 cd "$WORKDIR"
 test_expect_success 'cvs update (delete file)' \
-- 
2.34.1.307.g9b7440fafd
Previous: Eric SunshineNext: Jeff King
Message 32 of 47 in “tests: fix broken &&-chains & abort loops on error”
  1. 00/19 tests: fix broken &&-chains & abort loops on errorEric Sunshine, Dec 9, 2021
  2. 01/19 t/lib-pager: use sane_unset() to avoid breaking &&-chainEric Sunshine, Dec 9, 2021
  3. 02/19 t1010: fix unnoticed failure on WindowsEric Sunshine, Dec 9, 2021
  4. Elijah NewrenDec 9, 2021
  5. Eric SunshineDec 9, 2021
  6. 03/19 t1020: avoid aborting entire test script when one test failsEric Sunshine, Dec 9, 2021
  7. 06/19 t6300: make `%(raw:size) --shell` test more robustEric Sunshine, Dec 9, 2021
  8. Jeff KingDec 10, 2021
  9. 04/19 t4202: clarify intent by creating expected content less cleverlyEric Sunshine, Dec 9, 2021
  10. Jeff KingDec 10, 2021
  11. 05/19 t5516: drop unnecessary subshell and command invocationEric Sunshine, Dec 9, 2021
  12. Jeff KingDec 10, 2021
  13. 07/19 t9107: use shell parameter expansion to avoid breaking &&-chainEric Sunshine, Dec 9, 2021
  14. 08/19 tests: simplify construction of large blocks of textEric Sunshine, Dec 9, 2021
  15. 11/19 tests: fix broken &&-chains in `$(...)` command substitutionsEric Sunshine, Dec 9, 2021
  16. Elijah NewrenDec 9, 2021
  17. Eric SunshineDec 9, 2021
  18. Elijah NewrenDec 9, 2021
  19. Jeff KingDec 10, 2021
  20. 10/19 tests: fix broken &&-chains in compound statementsEric Sunshine, Dec 9, 2021
  21. 09/19 tests: use test_write_lines() to generate line-oriented outputEric Sunshine, Dec 9, 2021
  22. Jeff KingDec 10, 2021
  23. Eric SunshineDec 11, 2021
  24. 12/19 tests: fix broken &&-chains in `{...}` groupsEric Sunshine, Dec 9, 2021
  25. Jeff KingDec 10, 2021
  26. Eric SunshineDec 11, 2021
  27. Fabian StelzerDec 10, 2021
  28. Eric SunshineDec 11, 2021
  29. 13/19 tests: apply modern idiom for signaling test failureEric Sunshine, Dec 9, 2021
  30. Jeff KingDec 10, 2021
  31. Eric SunshineDec 11, 2021
  32. 14/19 tests: apply modern idiom for exiting loop upon failureEric Sunshine, Dec 9, 2021
  33. Jeff KingDec 10, 2021
  34. 16/19 t0000-t3999: detect and signal failure within loopEric Sunshine, Dec 9, 2021
  35. 15/19 tests: simplify by dropping unnecessary `for` loopsEric Sunshine, Dec 9, 2021
  36. Elijah NewrenDec 9, 2021
  37. 17/19 t4000-t4999: detect and signal failure within loopEric Sunshine, Dec 9, 2021
  38. Fabian StelzerDec 10, 2021
  39. Eric SunshineDec 11, 2021
  40. 19/19 t6000-t9999: detect and signal failure within loopEric Sunshine, Dec 9, 2021
  41. 18/19 t5000-t5999: detect and signal failure within loopEric Sunshine, Dec 9, 2021
  42. Elijah NewrenDec 9, 2021
  43. Eric SunshineDec 9, 2021
  44. Jeff KingDec 10, 2021
  45. Fabian StelzerDec 10, 2021
  46. Eric SunshineDec 11, 2021
  47. 2/19 t1010: fix unnoticed failure on WindowsEric Sunshine, Dec 11, 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.