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

[PATCH 1/3] t2018: cleanup in current test

From
Denton Liu <liu.denton@gmail.com>
Date
Apr 25, 2019, 21:10 UTC
Message-ID
<c0c7171e3d523e5d4a0ac01810378447a38854da.1556226502.git.liu.denton@gmail.com>
In-Reply-To
<cover.1556226502.git.liu.denton@gmail.com>

Before, in t2018, if do_checkout failed to create `branch2`, the next test-case would run `git branch -D branch2` but then fail because it was expecting `branch2` to exist, even though it doesn't. As a result, an early failure could cause a cascading failure of tests.

Make test-case responsible for cleaning up their own branches so that future tests can start with a sane environment.

Signed-off-by: Denton Liu <liu.denton@gmail.com>
---
 t/t2018-checkout-branch.sh | 39 +++++++++++++-------------------------
 1 file changed, 13 insertions(+), 26 deletions(-)
diff --git a/t/t2018-checkout-branch.sh b/t/t2018-checkout-branch.sh
index c5014ad9a6..fdb7fd282d 100755
--- a/t/t2018-checkout-branch.sh
+++ b/t/t2018-checkout-branch.sh
@@ -60,38 +60,36 @@ test_expect_success 'setup' '
 '
 
 test_expect_success 'checkout -b to a new branch, set to HEAD' '
+	test_when_finished test_might_fail git branch -D branch2 &&
+	test_when_finished git checkout branch1 &&
 	do_checkout branch2
 '
 
 test_expect_success 'checkout -b to a new branch, set to an explicit ref' '
-	git checkout branch1 &&
-	git branch -D branch2 &&
-
+	test_when_finished test_might_fail git branch -D branch2 &&
+	test_when_finished git checkout branch1 &&
 	do_checkout branch2 $HEAD1
 '
 
 test_expect_success 'checkout -b to a new branch with unmergeable changes fails' '
-	git checkout branch1 &&
-
-	# clean up from previous test
-	git branch -D branch2 &&
-
 	setup_dirty_unmergeable &&
 	test_must_fail do_checkout branch2 $HEAD1 &&
 	test_dirty_unmergeable
 '
 
 test_expect_success 'checkout -f -b to a new branch with unmergeable changes discards changes' '
+	test_when_finished test_might_fail git branch -D branch2 &&
+	test_when_finished git checkout branch1 &&
+
 	# still dirty and on branch1
 	do_checkout branch2 $HEAD1 "-f -b" &&
 	test_must_fail test_dirty_unmergeable
 '
 
 test_expect_success 'checkout -b to a new branch preserves mergeable changes' '
-	git checkout branch1 &&
-
-	# clean up from previous test
-	git branch -D branch2 &&
+	test_when_finished test_might_fail git branch -D branch2 &&
+	test_when_finished git checkout branch1 &&
+	test_when_finished git reset --hard &&
 
 	setup_dirty_mergeable &&
 	do_checkout branch2 $HEAD1 &&
@@ -99,27 +97,18 @@ test_expect_success 'checkout -b to a new branch preserves mergeable changes' '
 '
 
 test_expect_success 'checkout -f -b to a new branch with mergeable changes discards changes' '
-	# clean up from previous test
-	git reset --hard &&
-
-	git checkout branch1 &&
-
-	# clean up from previous test
-	git branch -D branch2 &&
-
+	test_when_finished git reset --hard HEAD &&
 	setup_dirty_mergeable &&
 	do_checkout branch2 $HEAD1 "-f -b" &&
 	test_must_fail test_dirty_mergeable
 '
 
 test_expect_success 'checkout -b to an existing branch fails' '
-	git reset --hard HEAD &&
-
+	test_when_finished git reset --hard HEAD &&
 	test_must_fail do_checkout branch2 $HEAD2
 '
 
 test_expect_success 'checkout -b to @{-1} fails with the right branch name' '
-	git reset --hard HEAD &&
 	git checkout branch1 &&
 	git checkout branch2 &&
 	echo  >expect "fatal: A branch named '\''branch1'\'' already exists." &&
@@ -160,6 +149,7 @@ test_expect_success 'checkout -f -B to an existing branch with unmergeable chang
 '
 
 test_expect_success 'checkout -B to an existing branch preserves mergeable changes' '
+	test_when_finished git reset --hard &&
 	git checkout branch1 &&
 
 	setup_dirty_mergeable &&
@@ -168,9 +158,6 @@ test_expect_success 'checkout -B to an existing branch preserves mergeable chang
 '
 
 test_expect_success 'checkout -f -B to an existing branch with mergeable changes discards changes' '
-	# clean up from previous test
-	git reset --hard &&
-
 	git checkout branch1 &&
 
 	setup_dirty_mergeable &&
-- 
2.21.0.1033.g0e8cc1100c
Previous: Denton LiuNext: Eric Sunshine
Message 2 of 21 in “checkout: allow -b/-B to work on a merge base”
  1. 0/3 checkout: allow -b/-B to work on a merge baseDenton Liu, Apr 25, 2019
  2. 1/3 t2018: cleanup in current testDenton Liu, Apr 25, 2019
  3. Eric SunshineApr 25, 2019
  4. Denton LiuApr 26, 2019
  5. Junio C HamanoApr 26, 2019
  6. Eric SunshineApr 26, 2019
  7. 2/3 t2018: demonstrate checkout -b merge base bugDenton Liu, Apr 25, 2019
  8. 3/3 checkout: allow -b/-B to work on a merge baseDenton Liu, Apr 25, 2019
  9. Junio C HamanoApr 26, 2019
  10. Junio C HamanoApr 26, 2019
  11. 0/3 allow checkout and branch to create branches on a merge baseDenton Liu, Apr 26, 2019
  12. 1/3 t2018: cleanup in current testDenton Liu, Apr 26, 2019
  13. 2/3 t2018: demonstrate checkout -b merge base bugDenton Liu, Apr 26, 2019
  14. 3/3 branch: make create_branch accept a merge base revDenton Liu, Apr 26, 2019
  15. Junio C HamanoApr 26, 2019
  16. Denton LiuApr 26, 2019
  17. Denton LiuApr 26, 2019
  18. Junio C HamanoApr 27, 2019
  19. 0/2 allow checkout and branch to create branches on a merge baseDenton Liu, Apr 27, 2019
  20. 1/2 t2018: cleanup in current testDenton Liu, Apr 27, 2019
  21. 2/2 branch: make create_branch accept a merge base revDenton Liu, Apr 27, 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.