t2018: cleanup in current test

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>

Denton Liu committed Apr 27, 2019 at 05:02 UTC 27434bf08c6c7c065e6323e8aa3f285d8f345e83
1 file changed +17 -26
t/t2018-checkout-branch.sh
+17 -26
@@ -60,38 +60,40 @@ test_expect_success 'setup' '
60 '
61
62 test_expect_success 'checkout -b to a new branch, set to HEAD' '
63 + test_when_finished "
64 + git checkout branch1 &&
65 + test_might_fail git branch -D branch2" &&
66 do_checkout branch2
67 '
68
69 test_expect_success 'checkout -b to a new branch, set to an explicit ref' '
67 - git checkout branch1 &&
68 - git branch -D branch2 &&
69 -
70 + test_when_finished "
71 + git checkout branch1 &&
72 + test_might_fail git branch -D branch2" &&
73 do_checkout branch2 $HEAD1
74 '
75
76 test_expect_success 'checkout -b to a new branch with unmergeable changes fails' '
74 - git checkout branch1 &&
75 -
76 - # clean up from previous test
77 - git branch -D branch2 &&
78 -
77 setup_dirty_unmergeable &&
78 test_must_fail do_checkout branch2 $HEAD1 &&
79 test_dirty_unmergeable
80 '
81
82 test_expect_success 'checkout -f -b to a new branch with unmergeable changes discards changes' '
83 + test_when_finished "
84 + git checkout branch1 &&
85 + test_might_fail git branch -D branch2" &&
86 +
87 # still dirty and on branch1
88 do_checkout branch2 $HEAD1 "-f -b" &&
89 test_must_fail test_dirty_unmergeable
90 '
91
92 test_expect_success 'checkout -b to a new branch preserves mergeable changes' '
91 - git checkout branch1 &&
92 -
93 - # clean up from previous test
94 - git branch -D branch2 &&
93 + test_when_finished "
94 + git reset --hard &&
95 + git checkout branch1 &&
96 + test_might_fail git branch -D branch2" &&
97
98 setup_dirty_mergeable &&
99 do_checkout branch2 $HEAD1 &&
@@ -99,27 +101,18 @@ test_expect_success 'checkout -b to a new branch preserves mergeable changes' '
101 '
102
103 test_expect_success 'checkout -f -b to a new branch with mergeable changes discards changes' '
102 - # clean up from previous test
103 - git reset --hard &&
104 -
105 - git checkout branch1 &&
106 -
107 - # clean up from previous test
108 - git branch -D branch2 &&
109 -
104 + test_when_finished git reset --hard HEAD &&
105 setup_dirty_mergeable &&
106 do_checkout branch2 $HEAD1 "-f -b" &&
107 test_must_fail test_dirty_mergeable
108 '
109
110 test_expect_success 'checkout -b to an existing branch fails' '
116 - git reset --hard HEAD &&
117 -
111 + test_when_finished git reset --hard HEAD &&
112 test_must_fail do_checkout branch2 $HEAD2
113 '
114
115 test_expect_success 'checkout -b to @{-1} fails with the right branch name' '
122 - git reset --hard HEAD &&
116 git checkout branch1 &&
117 git checkout branch2 &&
118 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
153 '
154
155 test_expect_success 'checkout -B to an existing branch preserves mergeable changes' '
156 + test_when_finished git reset --hard &&
157 git checkout branch1 &&
158
159 setup_dirty_mergeable &&
@@ -168,9 +162,6 @@ test_expect_success 'checkout -B to an existing branch preserves mergeable chang
162 '
163
164 test_expect_success 'checkout -f -B to an existing branch with mergeable changes discards changes' '
171 - # clean up from previous test
172 - git reset --hard &&
173 -
165 git checkout branch1 &&
166
167 setup_dirty_mergeable &&