Skip to content

Commit 9ddc5ac

Browse files
peffgitster
authored andcommitted
t: wrap complicated expect_code users in a block
If we are expecting a command to produce a particular exit code, we can use test_expect_code. However, some cases are more complicated, and want to accept one of a range of exit codes. For these, we end up with something like: cmd; case "$?" in ... That unfortunately breaks the &&-chain and fools --chain-lint. Since these special cases are so few, we can wrap them in a block, like this: { cmd; ret=$?; } && case "$ret" in ... This accomplishes the same thing, and retains the &&-chain (the exit status fed to the && is that of the assignment, which should always be true). It's technically longer, but it is probably a good thing for unusual code like this to stand out. Signed-off-by: Jeff King <[email protected]> Signed-off-by: Junio C Hamano <[email protected]>
1 parent c21fc9d commit 9ddc5ac

File tree

4 files changed

+13
-9
lines changed

4 files changed

+13
-9
lines changed

t/t0005-signals.sh

+2-2
Original file line numberDiff line numberDiff line change
@@ -10,8 +10,8 @@ one
1010
EOF
1111

1212
test_expect_success 'sigchain works' '
13-
test-sigchain >actual
14-
case "$?" in
13+
{ test-sigchain >actual; ret=$?; } &&
14+
case "$ret" in
1515
143) true ;; # POSIX w/ SIGTERM=15
1616
271) true ;; # ksh w/ SIGTERM=15
1717
3) true ;; # Windows

t/t4026-color.sh

+3-3
Original file line numberDiff line numberDiff line change
@@ -111,9 +111,9 @@ test_expect_success 'unknown color slots are ignored (branch)' '
111111
'
112112

113113
test_expect_success 'unknown color slots are ignored (status)' '
114-
git config color.status.nosuchslotwilleverbedefined white || exit
115-
git status
116-
case $? in 0|1) : ok ;; *) false ;; esac
114+
git config color.status.nosuchslotwilleverbedefined white &&
115+
{ git status; ret=$?; } &&
116+
case $ret in 0|1) : ok ;; *) false ;; esac
117117
'
118118

119119
test_done

t/t5004-archive-corner-cases.sh

+4-2
Original file line numberDiff line numberDiff line change
@@ -66,8 +66,10 @@ test_expect_success UNZIP 'zip archive of empty tree is empty' '
6666
# handle the empty repo at all, making our later check of its exit code
6767
# a no-op). But we cannot do anything reasonable except skip the test
6868
# on such platforms anyway, and this is the moral equivalent.
69-
"$GIT_UNZIP" "$TEST_DIRECTORY"/t5004/empty.zip
70-
expect_code=$?
69+
{
70+
"$GIT_UNZIP" "$TEST_DIRECTORY"/t5004/empty.zip
71+
expect_code=$?
72+
} &&
7173
7274
git archive --format=zip HEAD >empty.zip &&
7375
make_dir extract &&

t/t5512-ls-remote.sh

+4-2
Original file line numberDiff line numberDiff line change
@@ -103,8 +103,10 @@ test_expect_success 'confuses pattern as remote when no remote specified' '
103103
'
104104

105105
test_expect_success 'die with non-2 for wrong repository even with --exit-code' '
106-
git ls-remote --exit-code ./no-such-repository ;# not &&
107-
status=$? &&
106+
{
107+
git ls-remote --exit-code ./no-such-repository
108+
status=$?
109+
} &&
108110
test $status != 2 && test $status != 0
109111
'
110112

0 commit comments

Comments
 (0)