t2018: teach do_checkout() to accept `!` arg
We are running `test_must_fail do_checkout`. However, `test_must_fail` should only be used on git commands. Teach do_checkout() to accept `!` as a potential first argument which will cause the function to expect the "git checkout" to fail. This increases the granularity of the test as, instead of blindly checking that do_checkout() failed, we check that only the specific expected invocation of git fails. Signed-off-by: Denton Liu <liu.denton@gmail.com> Signed-off-by: Junio C Hamano <gitster@pobox.com>
Denton Liu committed
Jan 6, 2020 at 23:53 UTC
30c0367668ab919cc1bca3cff73820f77b573765
1 file changed
+21
-8
t/t2018-checkout-branch.sh
+21
-8
@@ -4,7 +4,7 @@ test_description='checkout'
4
5
. ./test-lib.sh
6
7
-# Arguments: <branch> <sha> [<checkout options>]
7
+# Arguments: [!] <branch> <sha> [<checkout options>]
8
#
9
# Runs "git checkout" to switch to <branch>, testing that
10
#
@@ -12,7 +12,16 @@ test_description='checkout'
12
# 2) HEAD is <sha>; if <sha> is not specified, the old HEAD is used.
13
#
14
# If <checkout options> is not specified, "git checkout" is run with -b.
15
+#
16
+# If the first argument is `!`, "git checkout" is expected to fail when
17
+# it is run.
18
do_checkout () {
19
+ should_fail= &&
20
+ if test "x$1" = "x!"
21
+ then
22
+ should_fail=yes &&
23
+ shift
24
+ fi &&
25
exp_branch=$1 &&
26
exp_ref="refs/heads/$exp_branch" &&
27
@@ -27,10 +36,14 @@ do_checkout () {
36
opts="$3"
37
fi
38
30
- git checkout $opts $exp_branch $exp_sha &&
31
-
32
- test $exp_ref = $(git rev-parse --symbolic-full-name HEAD) &&
33
- test $exp_sha = $(git rev-parse --verify HEAD)
39
+ if test -n "$should_fail"
40
+ then
41
+ test_must_fail git checkout $opts $exp_branch $exp_sha
42
+ else
43
+ git checkout $opts $exp_branch $exp_sha &&
44
+ test $exp_ref = $(git rev-parse --symbolic-full-name HEAD) &&
45
+ test $exp_sha = $(git rev-parse --verify HEAD)
46
+ fi
47
}
48
49
test_dirty_unmergeable () {
@@ -91,7 +104,7 @@ test_expect_success 'checkout -b to a new branch, set to an explicit ref' '
104
105
test_expect_success 'checkout -b to a new branch with unmergeable changes fails' '
106
setup_dirty_unmergeable &&
94
- test_must_fail do_checkout branch2 $HEAD1 &&
107
+ do_checkout ! branch2 $HEAD1 &&
108
test_dirty_unmergeable
109
'
110
@@ -125,7 +138,7 @@ test_expect_success 'checkout -f -b to a new branch with mergeable changes disca
138
139
test_expect_success 'checkout -b to an existing branch fails' '
140
test_when_finished git reset --hard HEAD &&
128
- test_must_fail do_checkout branch2 $HEAD2
141
+ do_checkout ! branch2 $HEAD2
142
'
143
144
test_expect_success 'checkout -b to @{-1} fails with the right branch name' '
@@ -164,7 +177,7 @@ test_expect_success 'checkout -B to an existing branch with unmergeable changes
177
git checkout branch1 &&
178
179
setup_dirty_unmergeable &&
167
- test_must_fail do_checkout branch2 $HEAD1 -B &&
180
+ do_checkout ! branch2 $HEAD1 -B &&
181
test_dirty_unmergeable
182
'
183