t2018: cleanup in current test
authorDenton Liu <liu.denton@gmail.com>
Sat, 27 Apr 2019 12:02:19 +0000 (05:02 -0700)
committerJunio C Hamano <gitster@pobox.com>
Tue, 7 May 2019 04:23:10 +0000 (13:23 +0900)
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>
Signed-off-by: Junio C Hamano <gitster@pobox.com>
t/t2018-checkout-branch.sh
index c5014ad9a63f2451f84db5076f902a0132d318c1..f1c7023e1ad51ca8bc3a3cf0dc6eeaa3a8f18dff 100755 (executable)
@@ -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 &&