t2018: teach do_checkout() to accept `!` arg
authorDenton Liu <redacted>
Tue, 7 Jan 2020 04:53:02 +0000 (23:53 -0500)
committerJunio C Hamano <redacted>
Mon, 27 Jan 2020 20:56:02 +0000 (12:56 -0800)
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 <redacted>
Signed-off-by: Junio C Hamano <redacted>
t/t2018-checkout-branch.sh

index 7ca55efc6b6152069efa89f9e94612ae6c1770e6..687ab6713cf46c5e0e7fbd5306c3cf5c5f24b4d3 100755 (executable)
@@ -4,7 +4,7 @@ test_description='checkout'
 
 . ./test-lib.sh
 
-# Arguments: <branch> <sha> [<checkout options>]
+# Arguments: [!] <branch> <sha> [<checkout options>]
 #
 # Runs "git checkout" to switch to <branch>, testing that
 #
@@ -12,7 +12,16 @@ test_description='checkout'
 #   2) HEAD is <sha>; if <sha> is not specified, the old HEAD is used.
 #
 # If <checkout options> is not specified, "git checkout" is run with -b.
+#
+# If the first argument is `!`, "git checkout" is expected to fail when
+# it is run.
 do_checkout () {
+       should_fail= &&
+       if test "x$1" = "x!"
+       then
+               should_fail=yes &&
+               shift
+       fi &&
        exp_branch=$1 &&
        exp_ref="refs/heads/$exp_branch" &&
 
@@ -27,10 +36,14 @@ do_checkout () {
                opts="$3"
        fi
 
-       git checkout $opts $exp_branch $exp_sha &&
-
-       test $exp_ref = $(git rev-parse --symbolic-full-name HEAD) &&
-       test $exp_sha = $(git rev-parse --verify HEAD)
+       if test -n "$should_fail"
+       then
+               test_must_fail git checkout $opts $exp_branch $exp_sha
+       else
+               git checkout $opts $exp_branch $exp_sha &&
+               test $exp_ref = $(git rev-parse --symbolic-full-name HEAD) &&
+               test $exp_sha = $(git rev-parse --verify HEAD)
+       fi
 }
 
 test_dirty_unmergeable () {
@@ -91,7 +104,7 @@ test_expect_success 'checkout -b to a new branch, set to an explicit ref' '
 
 test_expect_success 'checkout -b to a new branch with unmergeable changes fails' '
        setup_dirty_unmergeable &&
-       test_must_fail do_checkout branch2 $HEAD1 &&
+       do_checkout ! branch2 $HEAD1 &&
        test_dirty_unmergeable
 '
 
@@ -125,7 +138,7 @@ test_expect_success 'checkout -f -b to a new branch with mergeable changes disca
 
 test_expect_success 'checkout -b to an existing branch fails' '
        test_when_finished git reset --hard HEAD &&
-       test_must_fail do_checkout branch2 $HEAD2
+       do_checkout ! branch2 $HEAD2
 '
 
 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
        git checkout branch1 &&
 
        setup_dirty_unmergeable &&
-       test_must_fail do_checkout branch2 $HEAD1 -B &&
+       do_checkout ! branch2 $HEAD1 -B &&
        test_dirty_unmergeable
 '
 
git clone https://git.99rst.org/PROJECT