@matheusbsbs / matheus-bagliano / commits / 40caa5366a
Fork 0

t2018: be more discerning when checking for expected exit codes

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>

Denton Liu committed Jan 26, 2020 at 15:23 UTC 40caa5366a84fba6e64ea32bf98d9c1aba0c537b
1 file changed +14 -6
t/t2018-checkout-branch.sh
+14 -6
@@ -34,7 +34,11 @@ do_checkout () {
34 }
35
36 test_dirty_unmergeable () {
37 - ! git diff --exit-code >/dev/null
37 + test_expect_code 1 git diff --exit-code
38 +}
39 +
40 +test_dirty_unmergeable_discards_changes () {
41 + git diff --exit-code
42 }
43
44 setup_dirty_unmergeable () {
@@ -42,7 +46,11 @@ setup_dirty_unmergeable () {
46 }
47
48 test_dirty_mergeable () {
45 - ! git diff --cached --exit-code >/dev/null
49 + test_expect_code 1 git diff --cached --exit-code
50 +}
51 +
52 +test_dirty_mergeable_discards_changes () {
53 + git diff --cached --exit-code
54 }
55
56 setup_dirty_mergeable () {
@@ -94,7 +102,7 @@ test_expect_success 'checkout -f -b to a new branch with unmergeable changes dis
102
103 # still dirty and on branch1
104 do_checkout branch2 $HEAD1 "-f -b" &&
97 - test_must_fail test_dirty_unmergeable
105 + test_dirty_unmergeable_discards_changes
106 '
107
108 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
120 test_when_finished git reset --hard HEAD &&
121 setup_dirty_mergeable &&
122 do_checkout branch2 $HEAD1 "-f -b" &&
115 - test_must_fail test_dirty_mergeable
123 + test_dirty_mergeable_discards_changes
124 '
125
126 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
171 test_expect_success 'checkout -f -B to an existing branch with unmergeable changes discards changes' '
172 # still dirty and on branch1
173 do_checkout branch2 $HEAD1 "-f -B" &&
166 - test_must_fail test_dirty_unmergeable
174 + test_dirty_unmergeable_discards_changes
175 '
176
177 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
188
189 setup_dirty_mergeable &&
190 do_checkout branch2 $HEAD1 "-f -B" &&
183 - test_must_fail test_dirty_mergeable
191 + test_dirty_mergeable_discards_changes
192 '
193
194 test_expect_success 'checkout -b <describe>' '