Skip to content

Commit 512477b

Browse files
unsignedzerogitster
authored andcommitted
tests: use "env" to run commands with temporary env-var settings
Ordinarily, we would say "VAR=VAL command" to execute a tested command with environment variable(s) set only for that command. This however does not work if 'command' is a shell function (most notably 'test_must_fail'); the result of the assignment is retained and affects later commands. To avoid this, we used to assign and export environment variables and run such a test in a subshell, like so: ( VAR=VAL && export VAR && test_must_fail git command to be tested ) But with "env" utility, we should be able to say: test_must_fail env VAR=VAL git command to be tested which is much shorter and easier to read. Signed-off-by: David Tran <[email protected]> Signed-off-by: Junio C Hamano <[email protected]>
1 parent 5f95c9f commit 512477b

12 files changed

+42
-152
lines changed

Diff for: t/t1300-repo-config.sh

+4-13
Original file line numberDiff line numberDiff line change
@@ -961,24 +961,15 @@ test_expect_success SYMLINKS 'symlinked configuration' '
961961
'
962962

963963
test_expect_success 'nonexistent configuration' '
964-
(
965-
GIT_CONFIG=doesnotexist &&
966-
export GIT_CONFIG &&
967-
test_must_fail git config --list &&
968-
test_must_fail git config test.xyzzy
969-
)
964+
test_must_fail env GIT_CONFIG=doesnotexist git config --list &&
965+
test_must_fail env GIT_CONFIG=doesnotexist git config test.xyzzy
970966
'
971967

972968
test_expect_success SYMLINKS 'symlink to nonexistent configuration' '
973969
ln -s doesnotexist linktonada &&
974970
ln -s linktonada linktolinktonada &&
975-
(
976-
GIT_CONFIG=linktonada &&
977-
export GIT_CONFIG &&
978-
test_must_fail git config --list &&
979-
GIT_CONFIG=linktolinktonada &&
980-
test_must_fail git config --list
981-
)
971+
test_must_fail env GIT_CONFIG=linktonada git config --list &&
972+
test_must_fail env GIT_CONFIG=linktolinktonada git config --list
982973
'
983974

984975
test_expect_success 'check split_cmdline return' "

Diff for: t/t1510-repo-setup.sh

+1-3
Original file line numberDiff line numberDiff line change
@@ -777,9 +777,7 @@ test_expect_success '#30: core.worktree and core.bare conflict (gitfile version)
777777
setup_repo 30 "$here/30" gitfile true &&
778778
(
779779
cd 30 &&
780-
GIT_DIR=.git &&
781-
export GIT_DIR &&
782-
test_must_fail git symbolic-ref HEAD 2>result
780+
test_must_fail env GIT_DIR=.git git symbolic-ref HEAD 2>result
783781
) &&
784782
grep "core.bare and core.worktree" 30/result
785783
'

Diff for: t/t3200-branch.sh

+2-10
Original file line numberDiff line numberDiff line change
@@ -849,23 +849,15 @@ test_expect_success 'detect typo in branch name when using --edit-description' '
849849
write_script editor <<-\EOF &&
850850
echo "New contents" >"$1"
851851
EOF
852-
(
853-
EDITOR=./editor &&
854-
export EDITOR &&
855-
test_must_fail git branch --edit-description no-such-branch
856-
)
852+
test_must_fail env EDITOR=./editor git branch --edit-description no-such-branch
857853
'
858854

859855
test_expect_success 'refuse --edit-description on unborn branch for now' '
860856
write_script editor <<-\EOF &&
861857
echo "New contents" >"$1"
862858
EOF
863859
git checkout --orphan unborn &&
864-
(
865-
EDITOR=./editor &&
866-
export EDITOR &&
867-
test_must_fail git branch --edit-description
868-
)
860+
test_must_fail env EDITOR=./editor git branch --edit-description
869861
'
870862

871863
test_expect_success '--merged catches invalid object names' '

Diff for: t/t3301-notes.sh

+6-16
Original file line numberDiff line numberDiff line change
@@ -17,7 +17,7 @@ GIT_EDITOR=./fake_editor.sh
1717
export GIT_EDITOR
1818

1919
test_expect_success 'cannot annotate non-existing HEAD' '
20-
(MSG=3 && export MSG && test_must_fail git notes add)
20+
test_must_fail env MSG=3 git notes add
2121
'
2222

2323
test_expect_success setup '
@@ -32,22 +32,16 @@ test_expect_success setup '
3232
'
3333

3434
test_expect_success 'need valid notes ref' '
35-
(MSG=1 GIT_NOTES_REF=/ && export MSG GIT_NOTES_REF &&
36-
test_must_fail git notes add) &&
37-
(MSG=2 GIT_NOTES_REF=/ && export MSG GIT_NOTES_REF &&
38-
test_must_fail git notes show)
35+
test_must_fail env MSG=1 GIT_NOTES_REF=/ git notes show &&
36+
test_must_fail env MSG=2 GIT_NOTES_REF=/ git notes show
3937
'
4038

4139
test_expect_success 'refusing to add notes in refs/heads/' '
42-
(MSG=1 GIT_NOTES_REF=refs/heads/bogus &&
43-
export MSG GIT_NOTES_REF &&
44-
test_must_fail git notes add)
40+
test_must_fail env MSG=1 GIT_NOTES_REF=refs/heads/bogus git notes add
4541
'
4642

4743
test_expect_success 'refusing to edit notes in refs/remotes/' '
48-
(MSG=1 GIT_NOTES_REF=refs/remotes/bogus &&
49-
export MSG GIT_NOTES_REF &&
50-
test_must_fail git notes edit)
44+
test_must_fail env MSG=1 GIT_NOTES_REF=refs/heads/bogus git notes edit
5145
'
5246

5347
# 1 indicates caught gracefully by die, 128 means git-show barked
@@ -838,11 +832,7 @@ test_expect_success 'create note from non-existing note with "git notes add -c"
838832
git add a10 &&
839833
test_tick &&
840834
git commit -m 10th &&
841-
(
842-
MSG="yet another note" &&
843-
export MSG &&
844-
test_must_fail git notes add -c deadbeef
845-
) &&
835+
test_must_fail env MSG="yet another note" git notes add -c deadbeef &&
846836
test_must_fail git notes list HEAD
847837
'
848838

Diff for: t/t3404-rebase-interactive.sh

+13-56
Original file line numberDiff line numberDiff line change
@@ -102,12 +102,8 @@ test_expect_success 'rebase -i with the exec command runs from tree root' '
102102

103103
test_expect_success 'rebase -i with the exec command checks tree cleanness' '
104104
git checkout master &&
105-
(
106105
set_fake_editor &&
107-
FAKE_LINES="exec_echo_foo_>file1 1" &&
108-
export FAKE_LINES &&
109-
test_must_fail git rebase -i HEAD^
110-
) &&
106+
test_must_fail env FAKE_LINES="exec_echo_foo_>file1 1" git rebase -i HEAD^ &&
111107
test_cmp_rev master^ HEAD &&
112108
git reset --hard &&
113109
git rebase --continue
@@ -116,12 +112,9 @@ test_expect_success 'rebase -i with the exec command checks tree cleanness' '
116112
test_expect_success 'rebase -i with exec of inexistent command' '
117113
git checkout master &&
118114
test_when_finished "git rebase --abort" &&
119-
(
120115
set_fake_editor &&
121-
FAKE_LINES="exec_this-command-does-not-exist 1" &&
122-
export FAKE_LINES &&
123-
test_must_fail git rebase -i HEAD^ >actual 2>&1
124-
) &&
116+
test_must_fail env FAKE_LINES="exec_this-command-does-not-exist 1" \
117+
git rebase -i HEAD^ >actual 2>&1 &&
125118
! grep "Maybe git-rebase is broken" actual
126119
'
127120

@@ -375,11 +368,7 @@ test_expect_success 'commit message used after conflict' '
375368
git checkout -b conflict-fixup conflict-branch &&
376369
base=$(git rev-parse HEAD~4) &&
377370
set_fake_editor &&
378-
(
379-
FAKE_LINES="1 fixup 3 fixup 4" &&
380-
export FAKE_LINES &&
381-
test_must_fail git rebase -i $base
382-
) &&
371+
test_must_fail env FAKE_LINES="1 fixup 3 fixup 4" git rebase -i $base &&
383372
echo three > conflict &&
384373
git add conflict &&
385374
FAKE_COMMIT_AMEND="ONCE" EXPECT_HEADER_COUNT=2 \
@@ -394,11 +383,7 @@ test_expect_success 'commit message retained after conflict' '
394383
git checkout -b conflict-squash conflict-branch &&
395384
base=$(git rev-parse HEAD~4) &&
396385
set_fake_editor &&
397-
(
398-
FAKE_LINES="1 fixup 3 squash 4" &&
399-
export FAKE_LINES &&
400-
test_must_fail git rebase -i $base
401-
) &&
386+
test_must_fail env FAKE_LINES="1 fixup 3 squash 4" git rebase -i $base &&
402387
echo three > conflict &&
403388
git add conflict &&
404389
FAKE_COMMIT_AMEND="TWICE" EXPECT_HEADER_COUNT=2 \
@@ -469,11 +454,7 @@ test_expect_success 'interrupted squash works as expected' '
469454
git checkout -b interrupted-squash conflict-branch &&
470455
one=$(git rev-parse HEAD~3) &&
471456
set_fake_editor &&
472-
(
473-
FAKE_LINES="1 squash 3 2" &&
474-
export FAKE_LINES &&
475-
test_must_fail git rebase -i HEAD~3
476-
) &&
457+
test_must_fail env FAKE_LINES="1 squash 3 2" git rebase -i HEAD~3 &&
477458
(echo one; echo two; echo four) > conflict &&
478459
git add conflict &&
479460
test_must_fail git rebase --continue &&
@@ -487,11 +468,7 @@ test_expect_success 'interrupted squash works as expected (case 2)' '
487468
git checkout -b interrupted-squash2 conflict-branch &&
488469
one=$(git rev-parse HEAD~3) &&
489470
set_fake_editor &&
490-
(
491-
FAKE_LINES="3 squash 1 2" &&
492-
export FAKE_LINES &&
493-
test_must_fail git rebase -i HEAD~3
494-
) &&
471+
test_must_fail env FAKE_LINES="3 squash 1 2" git rebase -i HEAD~3 &&
495472
(echo one; echo four) > conflict &&
496473
git add conflict &&
497474
test_must_fail git rebase --continue &&
@@ -528,11 +505,7 @@ test_expect_success 'aborted --continue does not squash commits after "edit"' '
528505
FAKE_LINES="edit 1" git rebase -i HEAD^ &&
529506
echo "edited again" > file7 &&
530507
git add file7 &&
531-
(
532-
FAKE_COMMIT_MESSAGE=" " &&
533-
export FAKE_COMMIT_MESSAGE &&
534-
test_must_fail git rebase --continue
535-
) &&
508+
test_must_fail env FAKE_COMMIT_MESSAGE=" " git rebase --continue &&
536509
test $old = $(git rev-parse HEAD) &&
537510
git rebase --abort
538511
'
@@ -547,23 +520,15 @@ test_expect_success 'auto-amend only edited commits after "edit"' '
547520
echo "and again" > file7 &&
548521
git add file7 &&
549522
test_tick &&
550-
(
551-
FAKE_COMMIT_MESSAGE="and again" &&
552-
export FAKE_COMMIT_MESSAGE &&
553-
test_must_fail git rebase --continue
554-
) &&
523+
test_must_fail env FAKE_COMMIT_MESSAGE="and again" git rebase --continue &&
555524
git rebase --abort
556525
'
557526

558527
test_expect_success 'clean error after failed "exec"' '
559528
test_tick &&
560529
test_when_finished "git rebase --abort || :" &&
561530
set_fake_editor &&
562-
(
563-
FAKE_LINES="1 exec_false" &&
564-
export FAKE_LINES &&
565-
test_must_fail git rebase -i HEAD^
566-
) &&
531+
test_must_fail env FAKE_LINES="1 exec_false" git rebase -i HEAD^ &&
567532
echo "edited again" > file7 &&
568533
git add file7 &&
569534
test_must_fail git rebase --continue 2>error &&
@@ -947,12 +912,8 @@ test_expect_success 'rebase -i --root retain root commit author and message' '
947912

948913
test_expect_success 'rebase -i --root temporary sentinel commit' '
949914
git checkout B &&
950-
(
951-
set_fake_editor &&
952-
FAKE_LINES="2" &&
953-
export FAKE_LINES &&
954-
test_must_fail git rebase -i --root
955-
) &&
915+
set_fake_editor &&
916+
test_must_fail env FAKE_LINES="2" git rebase -i --root &&
956917
git cat-file commit HEAD | grep "^tree 4b825dc642cb" &&
957918
git rebase --abort
958919
'
@@ -1042,11 +1003,7 @@ test_expect_success 'rebase -i error on commits with \ in message' '
10421003
test_when_finished "git rebase --abort; git reset --hard $current_head; rm -f error" &&
10431004
test_commit TO-REMOVE will-conflict old-content &&
10441005
test_commit "\temp" will-conflict new-content dummy &&
1045-
(
1046-
EDITOR=true &&
1047-
export EDITOR &&
1048-
test_must_fail git rebase -i HEAD^ --onto HEAD^^ 2>error
1049-
) &&
1006+
test_must_fail env EDITOR=true git rebase -i HEAD^ --onto HEAD^^ 2>error &&
10501007
test_expect_code 1 grep " emp" error
10511008
'
10521009

Diff for: t/t3413-rebase-hook.sh

+1-5
Original file line numberDiff line numberDiff line change
@@ -118,11 +118,7 @@ test_expect_success 'pre-rebase hook stops rebase (1)' '
118118
test_expect_success 'pre-rebase hook stops rebase (2)' '
119119
git checkout test &&
120120
git reset --hard side &&
121-
(
122-
EDITOR=:
123-
export EDITOR
124-
test_must_fail git rebase -i master
125-
) &&
121+
test_must_fail env EDITOR=: git rebase -i master &&
126122
test "z$(git symbolic-ref HEAD)" = zrefs/heads/test &&
127123
test 0 = $(git rev-list HEAD...side | wc -l)
128124
'

Diff for: t/t4014-format-patch.sh

+3-11
Original file line numberDiff line numberDiff line change
@@ -764,22 +764,14 @@ test_expect_success 'format-patch --signature="" suppresses signatures' '
764764

765765
test_expect_success TTY 'format-patch --stdout paginates' '
766766
rm -f pager_used &&
767-
(
768-
GIT_PAGER="wc >pager_used" &&
769-
export GIT_PAGER &&
770-
test_terminal git format-patch --stdout --all
771-
) &&
767+
test_terminal env GIT_PAGER="wc >pager_used" git format-patch --stdout --all &&
772768
test_path_is_file pager_used
773769
'
774770

775771
test_expect_success TTY 'format-patch --stdout pagination can be disabled' '
776772
rm -f pager_used &&
777-
(
778-
GIT_PAGER="wc >pager_used" &&
779-
export GIT_PAGER &&
780-
test_terminal git --no-pager format-patch --stdout --all &&
781-
test_terminal git -c "pager.format-patch=false" format-patch --stdout --all
782-
) &&
773+
test_terminal env GIT_PAGER="wc >pager_used" git --no-pager format-patch --stdout --all &&
774+
test_terminal env GIT_PAGER="wc >pager_used" git -c "pager.format-patch=false" format-patch --stdout --all &&
783775
test_path_is_missing pager_used &&
784776
test_path_is_missing .git/pager_used
785777
'

Diff for: t/t5305-include-tag.sh

+1-3
Original file line numberDiff line numberDiff line change
@@ -45,9 +45,7 @@ test_expect_success 'unpack objects' '
4545
test_expect_success 'check unpacked result (have commit, no tag)' '
4646
git rev-list --objects $commit >list.expect &&
4747
(
48-
GIT_DIR=clone.git &&
49-
export GIT_DIR &&
50-
test_must_fail git cat-file -e $tag &&
48+
test_must_fail env GIT_DIR=clone.git git cat-file -e $tag &&
5149
git rev-list --objects $commit
5250
) >list.actual &&
5351
test_cmp list.expect list.actual

Diff for: t/t5602-clone-remote-exec.sh

+3-10
Original file line numberDiff line numberDiff line change
@@ -12,21 +12,14 @@ test_expect_success setup '
1212
'
1313

1414
test_expect_success 'clone calls git upload-pack unqualified with no -u option' '
15-
(
16-
GIT_SSH=./not_ssh &&
17-
export GIT_SSH &&
18-
test_must_fail git clone localhost:/path/to/repo junk
19-
) &&
15+
test_must_fail env GIT_SSH=./not_ssh git clone localhost:/path/to/repo junk &&
2016
echo "localhost git-upload-pack '\''/path/to/repo'\''" >expected &&
2117
test_cmp expected not_ssh_output
2218
'
2319

2420
test_expect_success 'clone calls specified git upload-pack with -u option' '
25-
(
26-
GIT_SSH=./not_ssh &&
27-
export GIT_SSH &&
28-
test_must_fail git clone -u ./something/bin/git-upload-pack localhost:/path/to/repo junk
29-
) &&
21+
test_must_fail env GIT_SSH=./not_ssh \
22+
git clone -u ./something/bin/git-upload-pack localhost:/path/to/repo junk &&
3023
echo "localhost ./something/bin/git-upload-pack '\''/path/to/repo'\''" >expected &&
3124
test_cmp expected not_ssh_output
3225
'

Diff for: t/t5801-remote-helpers.sh

+2-4
Original file line numberDiff line numberDiff line change
@@ -218,10 +218,8 @@ test_expect_success 'proper failure checks for fetching' '
218218
'
219219

220220
test_expect_success 'proper failure checks for pushing' '
221-
(GIT_REMOTE_TESTGIT_FAILURE=1 &&
222-
export GIT_REMOTE_TESTGIT_FAILURE &&
223-
cd local &&
224-
test_must_fail git push --all
221+
(cd local &&
222+
test_must_fail env GIT_REMOTE_TESTGIT_FAILURE=1 git push --all
225223
)
226224
'
227225

Diff for: t/t6006-rev-list-format.sh

+3-6
Original file line numberDiff line numberDiff line change
@@ -190,12 +190,9 @@ test_expect_success '%C(auto) respects --no-color' '
190190
'
191191

192192
test_expect_success TTY '%C(auto) respects --color=auto (stdout is tty)' '
193-
(
194-
TERM=vt100 && export TERM &&
195-
test_terminal \
196-
git log --format=$AUTO_COLOR -1 --color=auto >actual &&
197-
has_color actual
198-
)
193+
test_terminal env TERM=vt100 \
194+
git log --format=$AUTO_COLOR -1 --color=auto >actual &&
195+
has_color actual
199196
'
200197

201198
test_expect_success '%C(auto) respects --color=auto (stdout not tty)' '

0 commit comments

Comments
 (0)