tests: use "env" to run commands with temporary env-var settings
authorDavid Tran <unsignedzero@gmail.com>
Tue, 18 Mar 2014 18:54:05 +0000 (18:54 +0000)
committerJunio C Hamano <gitster@pobox.com>
Wed, 19 Mar 2014 19:55:57 +0000 (12:55 -0700)
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>
12 files changed:
t/t1300-repo-config.sh
t/t1510-repo-setup.sh
t/t3200-branch.sh
t/t3301-notes.sh
t/t3404-rebase-interactive.sh
t/t3413-rebase-hook.sh
t/t4014-format-patch.sh
t/t5305-include-tag.sh
t/t5602-clone-remote-exec.sh
t/t5801-remote-helpers.sh
t/t6006-rev-list-format.sh
t/t7006-pager.sh
index 967359344dab8118dfd55816ef08f4809a9f33ad..cd23d070291166d67f5b30adcba358e5958c80a5 100755 (executable)
@@ -961,24 +961,15 @@ test_expect_success SYMLINKS 'symlinked configuration' '
 '
 
 test_expect_success 'nonexistent configuration' '
-       (
-               GIT_CONFIG=doesnotexist &&
-               export GIT_CONFIG &&
-               test_must_fail git config --list &&
-               test_must_fail git config test.xyzzy
-       )
+       test_must_fail env GIT_CONFIG=doesnotexist git config --list &&
+       test_must_fail env GIT_CONFIG=doesnotexist git config test.xyzzy
 '
 
 test_expect_success SYMLINKS 'symlink to nonexistent configuration' '
        ln -s doesnotexist linktonada &&
        ln -s linktonada linktolinktonada &&
-       (
-               GIT_CONFIG=linktonada &&
-               export GIT_CONFIG &&
-               test_must_fail git config --list &&
-               GIT_CONFIG=linktolinktonada &&
-               test_must_fail git config --list
-       )
+       test_must_fail env GIT_CONFIG=linktonada git config --list &&
+       test_must_fail env GIT_CONFIG=linktolinktonada git config --list
 '
 
 test_expect_success 'check split_cmdline return' "
index cf2ee7885ac9c2172deccc27dfb994546593e842..e1b2a99f105f8700e72974428c126ea049247194 100755 (executable)
@@ -777,9 +777,7 @@ test_expect_success '#30: core.worktree and core.bare conflict (gitfile version)
        setup_repo 30 "$here/30" gitfile true &&
        (
                cd 30 &&
-               GIT_DIR=.git &&
-               export GIT_DIR &&
-               test_must_fail git symbolic-ref HEAD 2>result
+               test_must_fail env GIT_DIR=.git git symbolic-ref HEAD 2>result
        ) &&
        grep "core.bare and core.worktree" 30/result
 '
index fcdb867748f3c651fdb58c148016f3da1cf2c9be..d45e95cfec85ec712f1a85a499641b1f5f1595bc 100755 (executable)
@@ -849,11 +849,7 @@ test_expect_success 'detect typo in branch name when using --edit-description' '
        write_script editor <<-\EOF &&
                echo "New contents" >"$1"
        EOF
-       (
-               EDITOR=./editor &&
-               export EDITOR &&
-               test_must_fail git branch --edit-description no-such-branch
-       )
+       test_must_fail env EDITOR=./editor git branch --edit-description no-such-branch
 '
 
 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' '
                echo "New contents" >"$1"
        EOF
        git checkout --orphan unborn &&
-       (
-               EDITOR=./editor &&
-               export EDITOR &&
-               test_must_fail git branch --edit-description
-       )
+       test_must_fail env EDITOR=./editor git branch --edit-description
 '
 
 test_expect_success '--merged catches invalid object names' '
index 16de05aff941c73b3e5f5371bd4739959f679efa..bf52db239a390462cf9aab9f0582a321e13036a5 100755 (executable)
@@ -17,7 +17,7 @@ GIT_EDITOR=./fake_editor.sh
 export GIT_EDITOR
 
 test_expect_success 'cannot annotate non-existing HEAD' '
-       (MSG=3 && export MSG && test_must_fail git notes add)
+       test_must_fail env MSG=3 git notes add
 '
 
 test_expect_success setup '
@@ -32,22 +32,16 @@ test_expect_success setup '
 '
 
 test_expect_success 'need valid notes ref' '
-       (MSG=1 GIT_NOTES_REF=/ && export MSG GIT_NOTES_REF &&
-        test_must_fail git notes add) &&
-       (MSG=2 GIT_NOTES_REF=/ && export MSG GIT_NOTES_REF &&
-        test_must_fail git notes show)
+       test_must_fail env MSG=1 GIT_NOTES_REF=/ git notes show &&
+       test_must_fail env MSG=2 GIT_NOTES_REF=/ git notes show
 '
 
 test_expect_success 'refusing to add notes in refs/heads/' '
-       (MSG=1 GIT_NOTES_REF=refs/heads/bogus &&
-        export MSG GIT_NOTES_REF &&
-        test_must_fail git notes add)
+       test_must_fail env MSG=1 GIT_NOTES_REF=refs/heads/bogus git notes add
 '
 
 test_expect_success 'refusing to edit notes in refs/remotes/' '
-       (MSG=1 GIT_NOTES_REF=refs/remotes/bogus &&
-        export MSG GIT_NOTES_REF &&
-        test_must_fail git notes edit)
+       test_must_fail env MSG=1 GIT_NOTES_REF=refs/heads/bogus git notes edit
 '
 
 # 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"
        git add a10 &&
        test_tick &&
        git commit -m 10th &&
-       (
-               MSG="yet another note" &&
-               export MSG &&
-               test_must_fail git notes add -c deadbeef
-       ) &&
+       test_must_fail env MSG="yet another note" git notes add -c deadbeef &&
        test_must_fail git notes list HEAD
 '
 
index 50e22b1cadff4252dfb552a4db930f49544952aa..c0023a5b4ff6ef60f5d77e9e54a64adf387bce6e 100755 (executable)
@@ -102,12 +102,8 @@ test_expect_success 'rebase -i with the exec command runs from tree root' '
 
 test_expect_success 'rebase -i with the exec command checks tree cleanness' '
        git checkout master &&
-       (
        set_fake_editor &&
-       FAKE_LINES="exec_echo_foo_>file1 1" &&
-       export FAKE_LINES &&
-       test_must_fail git rebase -i HEAD^
-       ) &&
+       test_must_fail env FAKE_LINES="exec_echo_foo_>file1 1" git rebase -i HEAD^ &&
        test_cmp_rev master^ HEAD &&
        git reset --hard &&
        git rebase --continue
@@ -116,12 +112,9 @@ test_expect_success 'rebase -i with the exec command checks tree cleanness' '
 test_expect_success 'rebase -i with exec of inexistent command' '
        git checkout master &&
        test_when_finished "git rebase --abort" &&
-       (
        set_fake_editor &&
-       FAKE_LINES="exec_this-command-does-not-exist 1" &&
-       export FAKE_LINES &&
-       test_must_fail git rebase -i HEAD^ >actual 2>&1
-       ) &&
+       test_must_fail env FAKE_LINES="exec_this-command-does-not-exist 1" \
+       git rebase -i HEAD^ >actual 2>&1 &&
        ! grep "Maybe git-rebase is broken" actual
 '
 
@@ -375,11 +368,7 @@ test_expect_success 'commit message used after conflict' '
        git checkout -b conflict-fixup conflict-branch &&
        base=$(git rev-parse HEAD~4) &&
        set_fake_editor &&
-       (
-               FAKE_LINES="1 fixup 3 fixup 4" &&
-               export FAKE_LINES &&
-               test_must_fail git rebase -i $base
-       ) &&
+       test_must_fail env FAKE_LINES="1 fixup 3 fixup 4" git rebase -i $base &&
        echo three > conflict &&
        git add conflict &&
        FAKE_COMMIT_AMEND="ONCE" EXPECT_HEADER_COUNT=2 \
@@ -394,11 +383,7 @@ test_expect_success 'commit message retained after conflict' '
        git checkout -b conflict-squash conflict-branch &&
        base=$(git rev-parse HEAD~4) &&
        set_fake_editor &&
-       (
-               FAKE_LINES="1 fixup 3 squash 4" &&
-               export FAKE_LINES &&
-               test_must_fail git rebase -i $base
-       ) &&
+       test_must_fail env FAKE_LINES="1 fixup 3 squash 4" git rebase -i $base &&
        echo three > conflict &&
        git add conflict &&
        FAKE_COMMIT_AMEND="TWICE" EXPECT_HEADER_COUNT=2 \
@@ -469,11 +454,7 @@ test_expect_success 'interrupted squash works as expected' '
        git checkout -b interrupted-squash conflict-branch &&
        one=$(git rev-parse HEAD~3) &&
        set_fake_editor &&
-       (
-               FAKE_LINES="1 squash 3 2" &&
-               export FAKE_LINES &&
-               test_must_fail git rebase -i HEAD~3
-       ) &&
+       test_must_fail env FAKE_LINES="1 squash 3 2" git rebase -i HEAD~3 &&
        (echo one; echo two; echo four) > conflict &&
        git add conflict &&
        test_must_fail git rebase --continue &&
@@ -487,11 +468,7 @@ test_expect_success 'interrupted squash works as expected (case 2)' '
        git checkout -b interrupted-squash2 conflict-branch &&
        one=$(git rev-parse HEAD~3) &&
        set_fake_editor &&
-       (
-               FAKE_LINES="3 squash 1 2" &&
-               export FAKE_LINES &&
-               test_must_fail git rebase -i HEAD~3
-       ) &&
+       test_must_fail env FAKE_LINES="3 squash 1 2" git rebase -i HEAD~3 &&
        (echo one; echo four) > conflict &&
        git add conflict &&
        test_must_fail git rebase --continue &&
@@ -528,11 +505,7 @@ test_expect_success 'aborted --continue does not squash commits after "edit"' '
        FAKE_LINES="edit 1" git rebase -i HEAD^ &&
        echo "edited again" > file7 &&
        git add file7 &&
-       (
-               FAKE_COMMIT_MESSAGE=" " &&
-               export FAKE_COMMIT_MESSAGE &&
-               test_must_fail git rebase --continue
-       ) &&
+       test_must_fail env FAKE_COMMIT_MESSAGE=" " git rebase --continue &&
        test $old = $(git rev-parse HEAD) &&
        git rebase --abort
 '
@@ -547,11 +520,7 @@ test_expect_success 'auto-amend only edited commits after "edit"' '
        echo "and again" > file7 &&
        git add file7 &&
        test_tick &&
-       (
-               FAKE_COMMIT_MESSAGE="and again" &&
-               export FAKE_COMMIT_MESSAGE &&
-               test_must_fail git rebase --continue
-       ) &&
+       test_must_fail env FAKE_COMMIT_MESSAGE="and again" git rebase --continue &&
        git rebase --abort
 '
 
@@ -559,11 +528,7 @@ test_expect_success 'clean error after failed "exec"' '
        test_tick &&
        test_when_finished "git rebase --abort || :" &&
        set_fake_editor &&
-       (
-               FAKE_LINES="1 exec_false" &&
-               export FAKE_LINES &&
-               test_must_fail git rebase -i HEAD^
-       ) &&
+       test_must_fail env FAKE_LINES="1 exec_false" git rebase -i HEAD^ &&
        echo "edited again" > file7 &&
        git add file7 &&
        test_must_fail git rebase --continue 2>error &&
@@ -947,12 +912,8 @@ test_expect_success 'rebase -i --root retain root commit author and message' '
 
 test_expect_success 'rebase -i --root temporary sentinel commit' '
        git checkout B &&
-       (
-               set_fake_editor &&
-               FAKE_LINES="2" &&
-               export FAKE_LINES &&
-               test_must_fail git rebase -i --root
-       ) &&
+       set_fake_editor &&
+       test_must_fail env FAKE_LINES="2" git rebase -i --root &&
        git cat-file commit HEAD | grep "^tree 4b825dc642cb" &&
        git rebase --abort
 '
@@ -1042,11 +1003,7 @@ test_expect_success 'rebase -i error on commits with \ in message' '
        test_when_finished "git rebase --abort; git reset --hard $current_head; rm -f error" &&
        test_commit TO-REMOVE will-conflict old-content &&
        test_commit "\temp" will-conflict new-content dummy &&
-       (
-       EDITOR=true &&
-       export EDITOR &&
-       test_must_fail git rebase -i HEAD^ --onto HEAD^^ 2>error
-       ) &&
+       test_must_fail env EDITOR=true git rebase -i HEAD^ --onto HEAD^^ 2>error &&
        test_expect_code 1 grep  "      emp" error
 '
 
index 098b75507bf2ef6906f3f4fd4c532bb4c55ce7bd..b6833e9a5fead6d2b47ce8a3d517f64cba6a1514 100755 (executable)
@@ -118,11 +118,7 @@ test_expect_success 'pre-rebase hook stops rebase (1)' '
 test_expect_success 'pre-rebase hook stops rebase (2)' '
        git checkout test &&
        git reset --hard side &&
-       (
-               EDITOR=:
-               export EDITOR
-               test_must_fail git rebase -i master
-       ) &&
+       test_must_fail env EDITOR=: git rebase -i master &&
        test "z$(git symbolic-ref HEAD)" = zrefs/heads/test &&
        test 0 = $(git rev-list HEAD...side | wc -l)
 '
index 73194b2c3dbe267b759bfd036367366b4955f0c4..9c8063314688ec0c03d3fb2099379cbbf4ff2e19 100755 (executable)
@@ -764,22 +764,14 @@ test_expect_success 'format-patch --signature="" suppresses signatures' '
 
 test_expect_success TTY 'format-patch --stdout paginates' '
        rm -f pager_used &&
-       (
-               GIT_PAGER="wc >pager_used" &&
-               export GIT_PAGER &&
-               test_terminal git format-patch --stdout --all
-       ) &&
+       test_terminal env GIT_PAGER="wc >pager_used" git format-patch --stdout --all &&
        test_path_is_file pager_used
 '
 
  test_expect_success TTY 'format-patch --stdout pagination can be disabled' '
        rm -f pager_used &&
-       (
-               GIT_PAGER="wc >pager_used" &&
-               export GIT_PAGER &&
-               test_terminal git --no-pager format-patch --stdout --all &&
-               test_terminal git -c "pager.format-patch=false" format-patch --stdout --all
-       ) &&
+       test_terminal env GIT_PAGER="wc >pager_used" git --no-pager format-patch --stdout --all &&
+       test_terminal env GIT_PAGER="wc >pager_used" git -c "pager.format-patch=false" format-patch --stdout --all &&
        test_path_is_missing pager_used &&
        test_path_is_missing .git/pager_used
 '
index b061864a8743636ae7759ff4e8ff2694410617a1..21517c70cd49ab91ab267544311d8c33173e3c3e 100755 (executable)
@@ -45,9 +45,7 @@ test_expect_success 'unpack objects' '
 test_expect_success 'check unpacked result (have commit, no tag)' '
        git rev-list --objects $commit >list.expect &&
        (
-               GIT_DIR=clone.git &&
-               export GIT_DIR &&
-               test_must_fail git cat-file -e $tag &&
+               test_must_fail env GIT_DIR=clone.git git cat-file -e $tag &&
                git rev-list --objects $commit
        ) >list.actual &&
        test_cmp list.expect list.actual
index 3f353d99e8f4255b131b4d44d7ec2a2c73140f0f..cbcceab9d56b591ee851374c9030a23a4c65a462 100755 (executable)
@@ -12,21 +12,14 @@ test_expect_success setup '
 '
 
 test_expect_success 'clone calls git upload-pack unqualified with no -u option' '
-       (
-               GIT_SSH=./not_ssh &&
-               export GIT_SSH &&
-               test_must_fail git clone localhost:/path/to/repo junk
-       ) &&
+       test_must_fail env GIT_SSH=./not_ssh git clone localhost:/path/to/repo junk &&
        echo "localhost git-upload-pack '\''/path/to/repo'\''" >expected &&
        test_cmp expected not_ssh_output
 '
 
 test_expect_success 'clone calls specified git upload-pack with -u option' '
-       (
-               GIT_SSH=./not_ssh &&
-               export GIT_SSH &&
-               test_must_fail git clone -u ./something/bin/git-upload-pack localhost:/path/to/repo junk
-       ) &&
+       test_must_fail env GIT_SSH=./not_ssh \
+               git clone -u ./something/bin/git-upload-pack localhost:/path/to/repo junk &&
        echo "localhost ./something/bin/git-upload-pack '\''/path/to/repo'\''" >expected &&
        test_cmp expected not_ssh_output
 '
index 613f69a254bab23e1f09126b4a7f3dca8692deaa..ca1983827007738e5c8b0dec4a02f13631aaa75b 100755 (executable)
@@ -218,10 +218,8 @@ test_expect_success 'proper failure checks for fetching' '
 '
 
 test_expect_success 'proper failure checks for pushing' '
-       (GIT_REMOTE_TESTGIT_FAILURE=1 &&
-       export GIT_REMOTE_TESTGIT_FAILURE &&
-       cd local &&
-       test_must_fail git push --all
+       (cd local &&
+       test_must_fail env GIT_REMOTE_TESTGIT_FAILURE=1 git push --all
        )
 '
 
index 98744038eccb396c752e6a4b6ced702733c0d396..9d9d9de08e1926525c24fc01c1217a96964fa1ee 100755 (executable)
@@ -190,12 +190,9 @@ test_expect_success '%C(auto) respects --no-color' '
 '
 
 test_expect_success TTY '%C(auto) respects --color=auto (stdout is tty)' '
-       (
-               TERM=vt100 && export TERM &&
-               test_terminal \
-                       git log --format=$AUTO_COLOR -1 --color=auto >actual &&
-               has_color actual
-       )
+       test_terminal env TERM=vt100 \
+               git log --format=$AUTO_COLOR -1 --color=auto >actual &&
+       has_color actual
 '
 
 test_expect_success '%C(auto) respects --color=auto (stdout not tty)' '
index b9365b431d1e412905ddd0efad1f809625224ba9..da958a8b569e6e8ad0c4d2e20b0454a0180155cc 100755 (executable)
@@ -146,11 +146,7 @@ test_expect_success 'no color when stdout is a regular file' '
 test_expect_success TTY 'color when writing to a pager' '
        rm -f paginated.out &&
        test_config color.ui auto &&
-       (
-               TERM=vt100 &&
-               export TERM &&
-               test_terminal git log
-       ) &&
+       test_terminal env TERM=vt100 git log &&
        colorful paginated.out
 '
 
@@ -158,11 +154,7 @@ test_expect_success TTY 'colors are suppressed by color.pager' '
        rm -f paginated.out &&
        test_config color.ui auto &&
        test_config color.pager false &&
-       (
-               TERM=vt100 &&
-               export TERM &&
-               test_terminal git log
-       ) &&
+       test_terminal env TERM=vt100 git log &&
        ! colorful paginated.out
 '
 
@@ -181,11 +173,7 @@ test_expect_success 'color when writing to a file intended for a pager' '
 test_expect_success TTY 'colors are sent to pager for external commands' '
        test_config alias.externallog "!git log" &&
        test_config color.ui auto &&
-       (
-               TERM=vt100 &&
-               export TERM &&
-               test_terminal git -p externallog
-       ) &&
+       test_terminal env TERM=vt100 git -p externallog &&
        colorful paginated.out
 '