Skip to content

Commit 00648ba

Browse files
newrengitster
authored andcommitted
Introduce sane_unset and use it to ensure proper && chaining
Reviewed-by: Jonathan Nieder <jrnieder@gmail.com> Signed-off-by: Elijah Newren <newren@gmail.com> Signed-off-by: Junio C Hamano <gitster@pobox.com>
1 parent bc0f35c commit 00648ba

File tree

5 files changed

+37
-29
lines changed

5 files changed

+37
-29
lines changed

t/README

Lines changed: 5 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -259,14 +259,11 @@ Do:
259259
test ...
260260

261261
That way all of the commands in your tests will succeed or fail. If
262-
you must ignore the return value of something (e.g., the return
263-
after unsetting a variable that was already unset is unportable) it's
264-
best to indicate so explicitly with a semicolon:
265-
266-
unset HLAGH;
267-
git merge hla &&
268-
git push gh &&
269-
test ...
262+
you must ignore the return value of something, consider using a
263+
helper function (e.g. use sane_unset instead of unset, in order
264+
to avoid unportable return value for unsetting a variable that was
265+
already unset), or prepending the command with test_might_fail or
266+
test_must_fail.
270267

271268
- Check the test coverage for your tests. See the "Test coverage"
272269
below.

t/t0001-init.sh

Lines changed: 14 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -25,7 +25,7 @@ check_config () {
2525

2626
test_expect_success 'plain' '
2727
(
28-
unset GIT_DIR GIT_WORK_TREE
28+
sane_unset GIT_DIR GIT_WORK_TREE &&
2929
mkdir plain &&
3030
cd plain &&
3131
git init
@@ -35,7 +35,7 @@ test_expect_success 'plain' '
3535

3636
test_expect_success 'plain with GIT_WORK_TREE' '
3737
if (
38-
unset GIT_DIR
38+
sane_unset GIT_DIR &&
3939
mkdir plain-wt &&
4040
cd plain-wt &&
4141
GIT_WORK_TREE=$(pwd) git init
@@ -48,7 +48,7 @@ test_expect_success 'plain with GIT_WORK_TREE' '
4848

4949
test_expect_success 'plain bare' '
5050
(
51-
unset GIT_DIR GIT_WORK_TREE GIT_CONFIG
51+
sane_unset GIT_DIR GIT_WORK_TREE GIT_CONFIG &&
5252
mkdir plain-bare-1 &&
5353
cd plain-bare-1 &&
5454
git --bare init
@@ -58,7 +58,7 @@ test_expect_success 'plain bare' '
5858

5959
test_expect_success 'plain bare with GIT_WORK_TREE' '
6060
if (
61-
unset GIT_DIR GIT_CONFIG
61+
sane_unset GIT_DIR GIT_CONFIG &&
6262
mkdir plain-bare-2 &&
6363
cd plain-bare-2 &&
6464
GIT_WORK_TREE=$(pwd) git --bare init
@@ -72,7 +72,7 @@ test_expect_success 'plain bare with GIT_WORK_TREE' '
7272
test_expect_success 'GIT_DIR bare' '
7373
7474
(
75-
unset GIT_CONFIG
75+
sane_unset GIT_CONFIG &&
7676
mkdir git-dir-bare.git &&
7777
GIT_DIR=git-dir-bare.git git init
7878
) &&
@@ -82,7 +82,7 @@ test_expect_success 'GIT_DIR bare' '
8282
test_expect_success 'init --bare' '
8383
8484
(
85-
unset GIT_DIR GIT_WORK_TREE GIT_CONFIG
85+
sane_unset GIT_DIR GIT_WORK_TREE GIT_CONFIG &&
8686
mkdir init-bare.git &&
8787
cd init-bare.git &&
8888
git init --bare
@@ -93,7 +93,7 @@ test_expect_success 'init --bare' '
9393
test_expect_success 'GIT_DIR non-bare' '
9494
9595
(
96-
unset GIT_CONFIG
96+
sane_unset GIT_CONFIG &&
9797
mkdir non-bare &&
9898
cd non-bare &&
9999
GIT_DIR=.git git init
@@ -104,7 +104,7 @@ test_expect_success 'GIT_DIR non-bare' '
104104
test_expect_success 'GIT_DIR & GIT_WORK_TREE (1)' '
105105
106106
(
107-
unset GIT_CONFIG
107+
sane_unset GIT_CONFIG &&
108108
mkdir git-dir-wt-1.git &&
109109
GIT_WORK_TREE=$(pwd) GIT_DIR=git-dir-wt-1.git git init
110110
) &&
@@ -114,7 +114,7 @@ test_expect_success 'GIT_DIR & GIT_WORK_TREE (1)' '
114114
test_expect_success 'GIT_DIR & GIT_WORK_TREE (2)' '
115115
116116
if (
117-
unset GIT_CONFIG
117+
sane_unset GIT_CONFIG &&
118118
mkdir git-dir-wt-2.git &&
119119
GIT_WORK_TREE=$(pwd) GIT_DIR=git-dir-wt-2.git git --bare init
120120
)
@@ -127,7 +127,7 @@ test_expect_success 'GIT_DIR & GIT_WORK_TREE (2)' '
127127
test_expect_success 'reinit' '
128128
129129
(
130-
unset GIT_CONFIG GIT_WORK_TREE GIT_CONFIG
130+
sane_unset GIT_CONFIG GIT_WORK_TREE GIT_CONFIG &&
131131
132132
mkdir again &&
133133
cd again &&
@@ -175,8 +175,8 @@ test_expect_success 'init with init.templatedir set' '
175175
git config -f "$test_config" init.templatedir "${HOME}/templatedir-source" &&
176176
mkdir templatedir-set &&
177177
cd templatedir-set &&
178-
unset GIT_CONFIG_NOGLOBAL &&
179-
unset GIT_TEMPLATE_DIR &&
178+
sane_unset GIT_CONFIG_NOGLOBAL &&
179+
sane_unset GIT_TEMPLATE_DIR &&
180180
NO_SET_GIT_TEMPLATE_DIR=t &&
181181
export NO_SET_GIT_TEMPLATE_DIR &&
182182
git init
@@ -187,7 +187,7 @@ test_expect_success 'init with init.templatedir set' '
187187
test_expect_success 'init --bare/--shared overrides system/global config' '
188188
(
189189
test_config="$HOME"/.gitconfig &&
190-
unset GIT_CONFIG_NOGLOBAL &&
190+
sane_unset GIT_CONFIG_NOGLOBAL &&
191191
git config -f "$test_config" core.bare false &&
192192
git config -f "$test_config" core.sharedRepository 0640 &&
193193
mkdir init-bare-shared-override &&
@@ -202,7 +202,7 @@ test_expect_success 'init --bare/--shared overrides system/global config' '
202202
test_expect_success 'init honors global core.sharedRepository' '
203203
(
204204
test_config="$HOME"/.gitconfig &&
205-
unset GIT_CONFIG_NOGLOBAL &&
205+
sane_unset GIT_CONFIG_NOGLOBAL &&
206206
git config -f "$test_config" core.sharedRepository 0666 &&
207207
mkdir shared-honor-global &&
208208
cd shared-honor-global &&

t/t7006-pager.sh

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -41,7 +41,7 @@ else
4141
fi
4242

4343
test_expect_success 'setup' '
44-
unset GIT_PAGER GIT_PAGER_IN_USE;
44+
sane_unset GIT_PAGER GIT_PAGER_IN_USE &&
4545
test_might_fail git config --unset core.pager &&
4646
4747
PAGER="cat >paginated.out" &&
@@ -254,7 +254,7 @@ test_default_pager() {
254254
parse_args "$@"
255255

256256
$test_expectation SIMPLEPAGERTTY "$cmd - default pager is used by default" "
257-
unset PAGER GIT_PAGER;
257+
sane_unset PAGER GIT_PAGER &&
258258
test_might_fail git config --unset core.pager &&
259259
rm -f default_pager_used ||
260260
cleanup_fail &&
@@ -277,7 +277,7 @@ test_PAGER_overrides() {
277277
parse_args "$@"
278278

279279
$test_expectation TTY "$cmd - PAGER overrides default pager" "
280-
unset GIT_PAGER;
280+
sane_unset GIT_PAGER &&
281281
test_might_fail git config --unset core.pager &&
282282
rm -f PAGER_used ||
283283
cleanup_fail &&
@@ -305,7 +305,7 @@ test_core_pager() {
305305
parse_args "$@"
306306

307307
$test_expectation TTY "$cmd - repository-local core.pager setting $used_if_wanted" "
308-
unset GIT_PAGER;
308+
sane_unset GIT_PAGER &&
309309
rm -f core.pager_used ||
310310
cleanup_fail &&
311311
@@ -333,7 +333,7 @@ test_pager_subdir_helper() {
333333
parse_args "$@"
334334

335335
$test_expectation TTY "$cmd - core.pager $used_if_wanted from subdirectory" "
336-
unset GIT_PAGER;
336+
sane_unset GIT_PAGER &&
337337
rm -f core.pager_used &&
338338
rm -fr sub ||
339339
cleanup_fail &&

t/t7502-commit.sh

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -252,8 +252,8 @@ test_expect_success 'committer is automatic' '
252252
253253
echo >>negative &&
254254
(
255-
unset GIT_COMMITTER_EMAIL
256-
unset GIT_COMMITTER_NAME
255+
sane_unset GIT_COMMITTER_EMAIL &&
256+
sane_unset GIT_COMMITTER_NAME &&
257257
# must fail because there is no change
258258
test_must_fail git commit -e -m "sample"
259259
) &&

t/test-lib.sh

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -268,6 +268,17 @@ remove_cr () {
268268
tr '\015' Q | sed -e 's/Q$//'
269269
}
270270

271+
# In some bourne shell implementations, the "unset" builtin returns
272+
# nonzero status when a variable to be unset was not set in the first
273+
# place.
274+
#
275+
# Use sane_unset when that should not be considered an error.
276+
277+
sane_unset () {
278+
unset "$@"
279+
return 0
280+
}
281+
271282
test_tick () {
272283
if test -z "${test_tick+set}"
273284
then

0 commit comments

Comments
 (0)