]> git.ipfire.org Git - thirdparty/git.git/commitdiff
t2018: be more discerning when checking for expected exit codes
authorDenton Liu <liu.denton@gmail.com>
Sun, 26 Jan 2020 20:23:05 +0000 (15:23 -0500)
committerJunio C Hamano <gitster@pobox.com>
Mon, 27 Jan 2020 20:55:42 +0000 (12:55 -0800)
Functions test_dirty_unmergeable() and test_dirty_mergeable()
expect git-diff to exit with the specific code 1. However, rather
than checking for that value explicitly, they instead negate the
exit code. Unfortunately, this negation makes it impossible to
distinguish the expected code from some other unexpected non-zero
code, for instance, from a segmentation fault. Therefore, be more
discerning by checking the exit code explicitly using
test_expect_code().

Furthermore, some callers of those functions want to negate the
result again, and do so with test_must_fail(). However,
test_must_fail() should only be used with git commands. Address
this by introducing a couple new tiny helper functions which test
the exact condition expected (without the unnecessarily confusing
double-negation).

Helped-by: Eric Sunshine <sunshine@sunshineco.com>
Signed-off-by: Denton Liu <liu.denton@gmail.com>
Signed-off-by: Junio C Hamano <gitster@pobox.com>
t/t2018-checkout-branch.sh

index 61206a90fb485e181664451abe72d9c3f0db687d..7ca55efc6b6152069efa89f9e94612ae6c1770e6 100755 (executable)
@@ -34,7 +34,11 @@ do_checkout () {
 }
 
 test_dirty_unmergeable () {
-       ! git diff --exit-code >/dev/null
+       test_expect_code 1 git diff --exit-code
+}
+
+test_dirty_unmergeable_discards_changes () {
+       git diff --exit-code
 }
 
 setup_dirty_unmergeable () {
@@ -42,7 +46,11 @@ setup_dirty_unmergeable () {
 }
 
 test_dirty_mergeable () {
-       ! git diff --cached --exit-code >/dev/null
+       test_expect_code 1 git diff --cached --exit-code
+}
+
+test_dirty_mergeable_discards_changes () {
+       git diff --cached --exit-code
 }
 
 setup_dirty_mergeable () {
@@ -94,7 +102,7 @@ test_expect_success 'checkout -f -b to a new branch with unmergeable changes dis
 
        # still dirty and on branch1
        do_checkout branch2 $HEAD1 "-f -b" &&
-       test_must_fail test_dirty_unmergeable
+       test_dirty_unmergeable_discards_changes
 '
 
 test_expect_success 'checkout -b to a new branch preserves mergeable changes' '
@@ -112,7 +120,7 @@ test_expect_success 'checkout -f -b to a new branch with mergeable changes disca
        test_when_finished git reset --hard HEAD &&
        setup_dirty_mergeable &&
        do_checkout branch2 $HEAD1 "-f -b" &&
-       test_must_fail test_dirty_mergeable
+       test_dirty_mergeable_discards_changes
 '
 
 test_expect_success 'checkout -b to an existing branch fails' '
@@ -163,7 +171,7 @@ test_expect_success 'checkout -B to an existing branch with unmergeable changes
 test_expect_success 'checkout -f -B to an existing branch with unmergeable changes discards changes' '
        # still dirty and on branch1
        do_checkout branch2 $HEAD1 "-f -B" &&
-       test_must_fail test_dirty_unmergeable
+       test_dirty_unmergeable_discards_changes
 '
 
 test_expect_success 'checkout -B to an existing branch preserves mergeable changes' '
@@ -180,7 +188,7 @@ test_expect_success 'checkout -f -B to an existing branch with mergeable changes
 
        setup_dirty_mergeable &&
        do_checkout branch2 $HEAD1 "-f -B" &&
-       test_must_fail test_dirty_mergeable
+       test_dirty_mergeable_discards_changes
 '
 
 test_expect_success 'checkout -b <describe>' '