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 <unsignedzero@gmail.com> Signed-off-by: Junio C Hamano <gitster@pobox.com>

David Tran committed Mar 18, 2014 at 18:54 UTC 512477b175288a429aaf4071dc9fe94f17f0e4ee
12 files changed +42 -152
t/t1300-repo-config.sh
+4 -13
@@ -961,24 +961,15 @@ test_expect_success SYMLINKS 'symlinked configuration' '
961 '
962
963 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
966 '
967
968 test_expect_success SYMLINKS 'symlink to nonexistent configuration' '
969 ln -s doesnotexist linktonada &&
970 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
973 '
974
975 test_expect_success 'check split_cmdline return' "
t/t1510-repo-setup.sh
+1 -3
@@ -777,9 +777,7 @@ test_expect_success '#30: core.worktree and core.bare conflict (gitfile version)
777 setup_repo 30 "$here/30" gitfile true &&
778 (
779 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
781 ) &&
782 grep "core.bare and core.worktree" 30/result
783 '
t/t3200-branch.sh
+2 -10
@@ -849,11 +849,7 @@ test_expect_success 'detect typo in branch name when using --edit-description' '
849 write_script editor <<-\EOF &&
850 echo "New contents" >"$1"
851 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
853 '
854
855 test_expect_success 'refuse --edit-description on unborn branch for now' '
@@ -861,11 +857,7 @@ test_expect_success 'refuse --edit-description on unborn branch for now' '
857 echo "New contents" >"$1"
858 EOF
859 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
861 '
862
863 test_expect_success '--merged catches invalid object names' '
t/t3301-notes.sh
+6 -16
@@ -17,7 +17,7 @@ GIT_EDITOR=./fake_editor.sh
17 export GIT_EDITOR
18
19 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
21 '
22
23 test_expect_success setup '
@@ -32,22 +32,16 @@ test_expect_success setup '
32 '
33
34 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
37 '
38
39 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
41 '
42
43 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
45 '
46
47 # 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"
832 git add a10 &&
833 test_tick &&
834 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 &&
836 test_must_fail git notes list HEAD
837 '
838
t/t3404-rebase-interactive.sh
+13 -56
@@ -102,12 +102,8 @@ test_expect_success 'rebase -i with the exec command runs from tree root' '
102
103 test_expect_success 'rebase -i with the exec command checks tree cleanness' '
104 git checkout master &&
105 - (
105 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^ &&
107 test_cmp_rev master^ HEAD &&
108 git reset --hard &&
109 git rebase --continue
@@ -116,12 +112,9 @@ test_expect_success 'rebase -i with the exec command checks tree cleanness' '
112 test_expect_success 'rebase -i with exec of inexistent command' '
113 git checkout master &&
114 test_when_finished "git rebase --abort" &&
119 - (
115 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 &&
118 ! grep "Maybe git-rebase is broken" actual
119 '
120
@@ -375,11 +368,7 @@ test_expect_success 'commit message used after conflict' '
368 git checkout -b conflict-fixup conflict-branch &&
369 base=$(git rev-parse HEAD~4) &&
370 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 &&
372 echo three > conflict &&
373 git add conflict &&
374 FAKE_COMMIT_AMEND="ONCE" EXPECT_HEADER_COUNT=2 \
@@ -394,11 +383,7 @@ test_expect_success 'commit message retained after conflict' '
383 git checkout -b conflict-squash conflict-branch &&
384 base=$(git rev-parse HEAD~4) &&
385 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 &&
387 echo three > conflict &&
388 git add conflict &&
389 FAKE_COMMIT_AMEND="TWICE" EXPECT_HEADER_COUNT=2 \
@@ -469,11 +454,7 @@ test_expect_success 'interrupted squash works as expected' '
454 git checkout -b interrupted-squash conflict-branch &&
455 one=$(git rev-parse HEAD~3) &&
456 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 &&
458 (echo one; echo two; echo four) > conflict &&
459 git add conflict &&
460 test_must_fail git rebase --continue &&
@@ -487,11 +468,7 @@ test_expect_success 'interrupted squash works as expected (case 2)' '
468 git checkout -b interrupted-squash2 conflict-branch &&
469 one=$(git rev-parse HEAD~3) &&
470 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 &&
472 (echo one; echo four) > conflict &&
473 git add conflict &&
474 test_must_fail git rebase --continue &&
@@ -528,11 +505,7 @@ test_expect_success 'aborted --continue does not squash commits after "edit"' '
505 FAKE_LINES="edit 1" git rebase -i HEAD^ &&
506 echo "edited again" > file7 &&
507 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 &&
509 test $old = $(git rev-parse HEAD) &&
510 git rebase --abort
511 '
@@ -547,11 +520,7 @@ test_expect_success 'auto-amend only edited commits after "edit"' '
520 echo "and again" > file7 &&
521 git add file7 &&
522 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 &&
524 git rebase --abort
525 '
526
@@ -559,11 +528,7 @@ test_expect_success 'clean error after failed "exec"' '
528 test_tick &&
529 test_when_finished "git rebase --abort || :" &&
530 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^ &&
532 echo "edited again" > file7 &&
533 git add file7 &&
534 test_must_fail git rebase --continue 2>error &&
@@ -947,12 +912,8 @@ test_expect_success 'rebase -i --root retain root commit author and message' '
912
913 test_expect_success 'rebase -i --root temporary sentinel commit' '
914 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 &&
917 git cat-file commit HEAD | grep "^tree 4b825dc642cb" &&
918 git rebase --abort
919 '
@@ -1042,11 +1003,7 @@ test_expect_success 'rebase -i error on commits with \ in message' '
1003 test_when_finished "git rebase --abort; git reset --hard $current_head; rm -f error" &&
1004 test_commit TO-REMOVE will-conflict old-content &&
1005 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 &&
1007 test_expect_code 1 grep " emp" error
1008 '
1009
t/t3413-rebase-hook.sh
+1 -5
@@ -118,11 +118,7 @@ test_expect_success 'pre-rebase hook stops rebase (1)' '
118 test_expect_success 'pre-rebase hook stops rebase (2)' '
119 git checkout test &&
120 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 &&
122 test "z$(git symbolic-ref HEAD)" = zrefs/heads/test &&
123 test 0 = $(git rev-list HEAD...side | wc -l)
124 '
t/t4014-format-patch.sh
+3 -11
@@ -764,22 +764,14 @@ test_expect_success 'format-patch --signature="" suppresses signatures' '
764
765 test_expect_success TTY 'format-patch --stdout paginates' '
766 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 &&
768 test_path_is_file pager_used
769 '
770
771 test_expect_success TTY 'format-patch --stdout pagination can be disabled' '
772 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 &&
775 test_path_is_missing pager_used &&
776 test_path_is_missing .git/pager_used
777 '
t/t5305-include-tag.sh
+1 -3
@@ -45,9 +45,7 @@ test_expect_success 'unpack objects' '
45 test_expect_success 'check unpacked result (have commit, no tag)' '
46 git rev-list --objects $commit >list.expect &&
47 (
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 &&
49 git rev-list --objects $commit
50 ) >list.actual &&
51 test_cmp list.expect list.actual
t/t5602-clone-remote-exec.sh
+3 -10
@@ -12,21 +12,14 @@ test_expect_success setup '
12 '
13
14 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 &&
16 echo "localhost git-upload-pack '\''/path/to/repo'\''" >expected &&
17 test_cmp expected not_ssh_output
18 '
19
20 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 &&
23 echo "localhost ./something/bin/git-upload-pack '\''/path/to/repo'\''" >expected &&
24 test_cmp expected not_ssh_output
25 '
t/t5801-remote-helpers.sh
+2 -4
@@ -218,10 +218,8 @@ test_expect_success 'proper failure checks for fetching' '
218 '
219
220 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
223 )
224 '
225
t/t6006-rev-list-format.sh
+3 -6
@@ -190,12 +190,9 @@ test_expect_success '%C(auto) respects --no-color' '
190 '
191
192 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
196 '
197
198 test_expect_success '%C(auto) respects --color=auto (stdout not tty)' '
t/t7006-pager.sh
+3 -15
@@ -146,11 +146,7 @@ test_expect_success 'no color when stdout is a regular file' '
146 test_expect_success TTY 'color when writing to a pager' '
147 rm -f paginated.out &&
148 test_config color.ui auto &&
149 - (
150 - TERM=vt100 &&
151 - export TERM &&
152 - test_terminal git log
153 - ) &&
149 + test_terminal env TERM=vt100 git log &&
150 colorful paginated.out
151 '
152
@@ -158,11 +154,7 @@ test_expect_success TTY 'colors are suppressed by color.pager' '
154 rm -f paginated.out &&
155 test_config color.ui auto &&
156 test_config color.pager false &&
161 - (
162 - TERM=vt100 &&
163 - export TERM &&
164 - test_terminal git log
165 - ) &&
157 + test_terminal env TERM=vt100 git log &&
158 ! colorful paginated.out
159 '
160
@@ -181,11 +173,7 @@ test_expect_success 'color when writing to a file intended for a pager' '
173 test_expect_success TTY 'colors are sent to pager for external commands' '
174 test_config alias.externallog "!git log" &&
175 test_config color.ui auto &&
184 - (
185 - TERM=vt100 &&
186 - export TERM &&
187 - test_terminal git -p externallog
188 - ) &&
176 + test_terminal env TERM=vt100 git -p externallog &&
177 colorful paginated.out
178 '
179