Skip to content

Commit 27434bf

Browse files
Denton-Lgitster
authored andcommitted
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 <[email protected]> Signed-off-by: Junio C Hamano <[email protected]>
1 parent aeb582a commit 27434bf

File tree

1 file changed

+17
-26
lines changed

1 file changed

+17
-26
lines changed

t/t2018-checkout-branch.sh

Lines changed: 17 additions & 26 deletions
Original file line numberDiff line numberDiff line change
@@ -60,66 +60,59 @@ test_expect_success 'setup' '
6060
'
6161

6262
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" &&
6366
do_checkout branch2
6467
'
6568

6669
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" &&
7073
do_checkout branch2 $HEAD1
7174
'
7275

7376
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-
7977
setup_dirty_unmergeable &&
8078
test_must_fail do_checkout branch2 $HEAD1 &&
8179
test_dirty_unmergeable
8280
'
8381

8482
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+
8587
# still dirty and on branch1
8688
do_checkout branch2 $HEAD1 "-f -b" &&
8789
test_must_fail test_dirty_unmergeable
8890
'
8991

9092
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" &&
9597
9698
setup_dirty_mergeable &&
9799
do_checkout branch2 $HEAD1 &&
98100
test_dirty_mergeable
99101
'
100102

101103
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 &&
110105
setup_dirty_mergeable &&
111106
do_checkout branch2 $HEAD1 "-f -b" &&
112107
test_must_fail test_dirty_mergeable
113108
'
114109

115110
test_expect_success 'checkout -b to an existing branch fails' '
116-
git reset --hard HEAD &&
117-
111+
test_when_finished git reset --hard HEAD &&
118112
test_must_fail do_checkout branch2 $HEAD2
119113
'
120114

121115
test_expect_success 'checkout -b to @{-1} fails with the right branch name' '
122-
git reset --hard HEAD &&
123116
git checkout branch1 &&
124117
git checkout branch2 &&
125118
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
160153
'
161154

162155
test_expect_success 'checkout -B to an existing branch preserves mergeable changes' '
156+
test_when_finished git reset --hard &&
163157
git checkout branch1 &&
164158
165159
setup_dirty_mergeable &&
@@ -168,9 +162,6 @@ test_expect_success 'checkout -B to an existing branch preserves mergeable chang
168162
'
169163

170164
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-
174165
git checkout branch1 &&
175166
176167
setup_dirty_mergeable &&

0 commit comments

Comments
 (0)