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.den...@gmail.com>
---
 t/t2018-checkout-branch.sh | 43 +++++++++++++++-----------------------
 1 file changed, 17 insertions(+), 26 deletions(-)

diff --git a/t/t2018-checkout-branch.sh b/t/t2018-checkout-branch.sh
index c5014ad9a6..f1c7023e1a 100755
--- a/t/t2018-checkout-branch.sh
+++ b/t/t2018-checkout-branch.sh
@@ -60,38 +60,40 @@ test_expect_success 'setup' '
 '
 
 test_expect_success 'checkout -b to a new branch, set to HEAD' '
+       test_when_finished "
+               git checkout branch1 &&
+               test_might_fail git branch -D branch2" &&
        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 "
+               git checkout branch1 &&
+               test_might_fail git branch -D branch2" &&
        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 "
+               git checkout branch1 &&
+               test_might_fail git branch -D branch2" &&
+
        # 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 "
+               git reset --hard &&
+               git checkout branch1 &&
+               test_might_fail git branch -D branch2" &&
 
        setup_dirty_mergeable &&
        do_checkout branch2 $HEAD1 &&
@@ -99,27 +101,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 +153,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 +162,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

Reply via email to