modernize t1500
v2 cover modernize t1500
This is a re-roll of [1] which modernizes t1500 by updating tests to set up their own needed state rather than relying upon manipulation of global state.
Changes since v1[1]:
In v1 patch 1/6, which makes test_rev_parse() for-loop driven, the loop control has been moved to the top of the loop for improved robustness.
v1 patch 2/6, which tweaked the value of GIT_CONFIG in preparation for removal of global cd's has been squashed into patch 3/6 which actually removes the cd's since the below diff makes perfect sense in the context of the latter patch, thus doesn't require its own preparatory patch.
-cd work || exit 1
-GIT_CONFIG="$(pwd)"/../.git/config
+GIT_CONFIG="$(pwd)/work/../.git/config"v1 patch 3/6, which teaches test_rev_parse() the -C option, has been revised to ensure that the argument to -C is always quoted upon use to avoid problems with whitespace in directory names (though current tests don't have any such directories).
v1 patches 4/6 and 5/6, which teach test_rev_parse() the -b and -g options, no longer assigns shell code to a variable for later execution/evaluation; instead they just execute the code directly.
v1 patch 5/6 ensures that the argument to -g is properly quoted when assigned to GIT_DIR to avoid problems with whitespace in directory names.
v1 patch 6/6, which changes "mv .git repo.git" to "cp -R .git repo.git", has been squashed with Junio's 7/6[2], which moves the "cp -R" earlier in the script, and now sits at the front of the series. Other setup-related actions have likewise been moved into a common "setup" test[3].
Most commit messages have seen minor tweaks.
A v1 to v2 interdiff is included below.
[1]: http://thread.gmane.org/gmane.comp.version-control.git/294088
[2]: http://thread.gmane.org/gmane.comp.version-control.git/294088/focus=294168
[3]: http://thread.gmane.org/gmane.comp.version-control.git/294088/focus=294170
Eric Sunshine (5):
t1500: be considerate to future potential tests
t1500: test_rev_parse: facilitate future test enhancements
t1500: avoid changing working directory outside of tests
t1500: avoid setting configuration options outside of tests
t1500: avoid setting environment variables outside of tests
t/t1500-rev-parse.sh | 123 ++++++++++++++++++++++++++------------------------- 1 file changed, 63 insertions(+), 60 deletions(-)
--- >8 ---
t/t1500-rev-parse.sh +27 -20
@@ -7,26 +7,21 @@ test_description='test git rev-parse' test_rev_parse () { dir= bare= - env= + gitdir= while : do case "$1" in - -C) dir="-C $2"; shift; shift ;; - -b) bare="$2"; shift; shift ;; - -g) env="GIT_DIR=$2; export GIT_DIR"; shift; shift ;; + -C) dir="$2"; shift; shift ;; + -b) case "$2" in + [tfu]*) bare="$2"; shift; shift ;; + *) error "test_rev_parse: bogus core.bare value '$2'" ;; + esac ;; + -g) gitdir="$2"; shift; shift ;; -*) error "test_rev_parse: unrecognized option '$1'" ;; *) break ;; esac done - case "$bare" in - '') ;; - t*) bare="test_config $dir core.bare true" ;; - f*) bare="test_config $dir core.bare false" ;; - u*) bare="test_unconfig $dir core.bare" ;; - *) error "test_rev_parse: unrecognized core.bare value '$bare'" - esac - name=$1 shift
@@ -36,35 +31,48 @@ test_rev_parse () { show-prefix \ git-dir do + test $# -eq 0 && break expect="$1" test_expect_success "$name: $o" ' - test_when_finished "sane_unset GIT_DIR" && - eval $env && - $bare && + if test -n "$gitdir" + then + test_when_finished "unset GIT_DIR" && + GIT_DIR="$gitdir" && + export GIT_DIR + fi && + + case "$bare" in + t*) test_config ${dir:+-C "$dir"} core.bare true ;; + f*) test_config ${dir:+-C "$dir"} core.bare false ;; + u*) test_unconfig ${dir:+-C "$dir"} core.bare ;; + esac && + echo "$expect" >expect && - git $dir rev-parse --$o >actual && + git ${dir:+-C "$dir"} rev-parse --$o >actual && test_cmp expect actual ' shift - test $# -eq 0 && break done } ROOT=$(pwd) +test_expect_success 'setup' ' + mkdir -p sub/dir work && + cp -R .git repo.git +' + test_rev_parse toplevel false false true '' .git test_rev_parse -C .git .git/ false true false '' . test_rev_parse -C .git/objects .git/objects/ false true false '' "$ROOT/.git" -test_expect_success 'setup untracked sub/dir' 'mkdir -p sub/dir' test_rev_parse -C sub/dir subdirectory false false true sub/dir/ "$ROOT/.git" test_rev_parse -b t 'core.bare = true' true false false test_rev_parse -b u 'core.bare undefined' false false true -test_expect_success 'setup non-local database ../.git' 'mkdir work' test_rev_parse -C work -g ../.git -b f 'GIT_DIR=../.git, core.bare = false' false false true ''
@@ -72,7 +80,6 @@ test_rev_parse -C work -g ../.git -b t 'GIT_DIR=../.git, core.bare = true' true test_rev_parse -C work -g ../.git -b u 'GIT_DIR=../.git, core.bare undefined' false false true '' -test_expect_success 'setup non-local database ../repo.git' 'cp -R .git repo.git' test_rev_parse -C work -g ../repo.git -b f 'GIT_DIR=../repo.git, core.bare = false' false false true ''
--- >8 ---
Signature · 1 line
2.8.2.703.g78b384c
PATCH v2 1/5 t1500: be considerate to future potential tests
The final batch of git-rev-parse tests work against a non-local object database named repo.git. This is done by renaming .git to repo.git and pointing GIT_DIR at it, but the name is never restored to .git at the end of the script, which can be problematic for tests added in the future. Be more friendly by instead making repo.git a copy of .git.
Furthermore, make it clear that tests in repo.git will be independent from the results of earlier tests done in .git by initializing repo.git earlier in the test sequence.
Likewise, bundle remaining preparation (such as directory creation) into a common setup test consistent with modern practice.
--- t/t1500-rev-parse.sh | 8 +++++--- 1 file changed, 5 insertions(+), 3 deletions(-)
t/t1500-rev-parse.sh +5 -3
@@ -37,6 +37,11 @@ test_rev_parse() { ROOT=$(pwd) +test_expect_success 'setup' ' + mkdir -p sub/dir work && + cp -R .git repo.git +' + test_rev_parse toplevel false false true '' .git cd .git || exit 1
@@ -45,7 +50,6 @@ cd objects || exit 1 test_rev_parse .git/objects/ false true false '' "$ROOT/.git" cd ../.. || exit 1 -mkdir -p sub/dir || exit 1 cd sub/dir || exit 1 test_rev_parse subdirectory false false true sub/dir/ "$ROOT/.git" cd ../.. || exit 1
@@ -56,7 +60,6 @@ test_rev_parse 'core.bare = true' true false false git config --unset core.bare test_rev_parse 'core.bare undefined' false false true -mkdir work || exit 1 cd work || exit 1 GIT_DIR=../.git GIT_CONFIG="$(pwd)"/../.git/config
@@ -71,7 +74,6 @@ test_rev_parse 'GIT_DIR=../.git, core.bare = true' true false false '' git config --unset core.bare test_rev_parse 'GIT_DIR=../.git, core.bare undefined' false false true '' -mv ../.git ../repo.git || exit 1 GIT_DIR=../repo.git GIT_CONFIG="$(pwd)"/../repo.git/config
Signature · 1 line
2.8.2.703.g78b384c
PATCH v2 2/5 t1500: test_rev_parse: facilitate future test enhancements
In reply to Eric SunshineTests run by test_rev_parse() are nearly identical; each invokes git-rev-parse with a single option and compares the result against an expected value. Such duplication makes it onerous to extend the tests since any change needs to be repeated in each test. Avoid the duplication by parameterizing the test and driving it via a for-loop.
--- t/t1500-rev-parse.sh | 44 +++++++++++++++++--------------------------- 1 file changed, 17 insertions(+), 27 deletions(-)
t/t1500-rev-parse.sh +17 -27
@@ -3,38 +3,28 @@ test_description='test git rev-parse' . ./test-lib.sh -test_rev_parse() { +# usage: label is-bare is-inside-git is-inside-work prefix git-dir +test_rev_parse () { name=$1 shift - test_expect_success "$name: is-bare-repository" \ - "test '$1' = \"\$(git rev-parse --is-bare-repository)\"" - shift - [ $# -eq 0 ] && return - - test_expect_success "$name: is-inside-git-dir" \ - "test '$1' = \"\$(git rev-parse --is-inside-git-dir)\"" - shift - [ $# -eq 0 ] && return - - test_expect_success "$name: is-inside-work-tree" \ - "test '$1' = \"\$(git rev-parse --is-inside-work-tree)\"" - shift - [ $# -eq 0 ] && return - - test_expect_success "$name: prefix" \ - "test '$1' = \"\$(git rev-parse --show-prefix)\"" - shift - [ $# -eq 0 ] && return - - test_expect_success "$name: git-dir" \ - "test '$1' = \"\$(git rev-parse --git-dir)\"" - shift - [ $# -eq 0 ] && return + for o in is-bare-repository \ + is-inside-git-dir \ + is-inside-work-tree \ + show-prefix \ + git-dir + do + test $# -eq 0 && break + expect="$1" + test_expect_success "$name: $o" ' + echo "$expect" >expect && + git rev-parse --$o >actual && + test_cmp expect actual + ' + shift + done } -# label is-bare is-inside-git is-inside-work prefix git-dir - ROOT=$(pwd) test_expect_success 'setup' '
Signature · 1 line
2.8.2.703.g78b384c
Re: [PATCH v2 2/5] t1500: test_rev_parse: facilitate future test enhancements
Quoting Eric Sunshine <[email protected]>:
Quoted 17 lines
Tests run by
test_rev_parse()are nearly identical; each invokes git-rev-parse with a single option and compares the result against an expected value. Such duplication makes it onerous to extend the tests since any change needs to be repeated in each test. Avoid the duplication by parameterizing the test and driving it via a for-loop.Signed-off-by Eric Sunshine <[email protected]>--- t/t1500-rev-parse.sh | 44 +++++++++++++++++--------------------------- 1 file changed, 17 insertions(+), 27 deletions(-)t/t1500-rev-parse.sh +1
-0+ for o in is-bare-repository \
t/t1500-rev-parse.sh line 21
Quoted 10 lines
t/t1500-rev-parse.sh +10
-0+ is-inside-git-dir \ + is-inside-work-tree \ + show-prefix \ + git-dir + do + test $# -eq 0 && break + expect="$1" + test_expect_success "$name: $o" ' + echo "$expect" >expect && + git rev-parse --$o >actual &&
I think that "--$o" looks really weird, but that's subjective, of course.
However, the idea popped up in an other thread[1] that we might want something like 'git rev-parse --absolute-path --git-dir', which wouldn't really work with '--$o'.
Even if we don't go that route, perhaps it would be better to list the options to be tested including their doubledash prefix.
[1] - http://thread.gmane.org/gmane.comp.version-control.git/287449/focus=292585
Re: [PATCH v2 2/5] t1500: test_rev_parse: facilitate future test enhancements
Quoted 13 lines · SZEDER Gábor
Quoting Eric Sunshine <[email protected]>:
t/t1500-rev-parse.sh line 21
Quoted 11 lines
t/t1500-rev-parse.sh +11
-0+ for o in is-bare-repository \ + is-inside-git-dir \ + is-inside-work-tree \ + show-prefix \ + git-dir + do + test $# -eq 0 && break + expect="$1" + test_expect_success "$name: $o" ' + echo "$expect" >expect && + git rev-parse --$o >actual &&
Quoted 8 lines
I think that "--$o" looks really weird, but that's subjective, of course.
However, the idea popped up in an other thread[1] that we might want something like 'git rev-parse --absolute-path --git-dir', which wouldn't really work with '--$o'.
Even if we don't go that route, perhaps it would be better to list the options to be tested including their doubledash prefix.
As this series is only about modernizing t1500, I'd prefer to keep the conversion faithful to the original which titles each test "$name:
is-bare-repository", "$name: is-inside-git-dir", etc., and the current approach does so without additional complexity.
I have no objection to upgrading the for-loop items to include the leading dashes or updating the logic to support --absolute-path, but such changes are outside the scope of this series and can easily be built atop it. Also, due to severe time constraints, I'd rather not re-roll only for a superficial and subjective change such as adding leading dashes to for-loop items.
Thanks.
Re: [PATCH v2 2/5] t1500: test_rev_parse: facilitate future test enhancements
Quoted 28 lines · Eric Sunshine
Quoted 13 lines · SZEDER Gábor
Quoting Eric Sunshine <[email protected]>:
t/t1500-rev-parse.sh line 21
Quoted 11 lines
t/t1500-rev-parse.sh +11
-0+ for o in is-bare-repository \ + is-inside-git-dir \ + is-inside-work-tree \ + show-prefix \ + git-dir + do + test $# -eq 0 && break + expect="$1" + test_expect_success "$name: $o" ' + echo "$expect" >expect && + git rev-parse --$o >actual &&Quoted 8 lines
I think that "--$o" looks really weird, but that's subjective, of course.
However, the idea popped up in an other thread[1] that we might want something like 'git rev-parse --absolute-path --git-dir', which wouldn't really work with '--$o'.
Even if we don't go that route, perhaps it would be better to list the options to be tested including their doubledash prefix.
As this series is only about modernizing t1500, I'd prefer to keep the conversion faithful to the original which titles each test "$name:
is-bare-repository", "$name: is-inside-git-dir", etc., and the current approach does so without additional complexity.
Quoted 6 lines
I have no objection to upgrading the for-loop items to include the leading dashes or updating the logic to support --absolute-path, but such changes are outside the scope of this series and can easily be built atop it. Also, due to severe time constraints, I'd rather not re-roll only for a superficial and subjective change such as adding leading dashes to for-loop items.
On reflection, I agree with your points and will include the change in the re-roll. Thanks.
PATCH v2 3/5 t1500: avoid changing working directory outside of tests
In reply to Eric SunshineIdeally, each test should be responsible for setting up state it needs rather than relying upon transient global state. Toward this end, teach test_rev_parse() to accept a "-C <dir>" option to allow callers to instruct it explicitly in which directory its tests should be run. Take advantage of this new option to avoid changing the working directory outside of tests.
--- t/t1500-rev-parse.sh | 42 +++++++++++++++++++++++------------------- 1 file changed, 23 insertions(+), 19 deletions(-)
t/t1500-rev-parse.sh +23 -19
@@ -3,8 +3,18 @@ test_description='test git rev-parse' . ./test-lib.sh -# usage: label is-bare is-inside-git is-inside-work prefix git-dir +# usage: [options] label is-bare is-inside-git is-inside-work prefix git-dir test_rev_parse () { + dir= + while : + do + case "$1" in + -C) dir="$2"; shift; shift ;; + -*) error "test_rev_parse: unrecognized option '$1'" ;; + *) break ;; + esac + done + name=$1 shift
@@ -18,7 +28,7 @@ test_rev_parse () { expect="$1" test_expect_success "$name: $o" ' echo "$expect" >expect && - git rev-parse --$o >actual && + git ${dir:+-C "$dir"} rev-parse --$o >actual && test_cmp expect actual ' shift
@@ -34,15 +44,10 @@ test_expect_success 'setup' ' test_rev_parse toplevel false false true '' .git -cd .git || exit 1 -test_rev_parse .git/ false true false '' . -cd objects || exit 1 -test_rev_parse .git/objects/ false true false '' "$ROOT/.git" -cd ../.. || exit 1 +test_rev_parse -C .git .git/ false true false '' . +test_rev_parse -C .git/objects .git/objects/ false true false '' "$ROOT/.git" -cd sub/dir || exit 1 -test_rev_parse subdirectory false false true sub/dir/ "$ROOT/.git" -cd ../.. || exit 1 +test_rev_parse -C sub/dir subdirectory false false true sub/dir/ "$ROOT/.git" git config core.bare true test_rev_parse 'core.bare = true' true false false
@@ -50,30 +55,29 @@ test_rev_parse 'core.bare = true' true false false git config --unset core.bare test_rev_parse 'core.bare undefined' false false true -cd work || exit 1 GIT_DIR=../.git -GIT_CONFIG="$(pwd)"/../.git/config +GIT_CONFIG="$(pwd)/work/../.git/config" export GIT_DIR GIT_CONFIG git config core.bare false -test_rev_parse 'GIT_DIR=../.git, core.bare = false' false false true '' +test_rev_parse -C work 'GIT_DIR=../.git, core.bare = false' false false true '' git config core.bare true -test_rev_parse 'GIT_DIR=../.git, core.bare = true' true false false '' +test_rev_parse -C work 'GIT_DIR=../.git, core.bare = true' true false false '' git config --unset core.bare -test_rev_parse 'GIT_DIR=../.git, core.bare undefined' false false true '' +test_rev_parse -C work 'GIT_DIR=../.git, core.bare undefined' false false true '' GIT_DIR=../repo.git -GIT_CONFIG="$(pwd)"/../repo.git/config +GIT_CONFIG="$(pwd)/work/../repo.git/config" git config core.bare false -test_rev_parse 'GIT_DIR=../repo.git, core.bare = false' false false true '' +test_rev_parse -C work 'GIT_DIR=../repo.git, core.bare = false' false false true '' git config core.bare true -test_rev_parse 'GIT_DIR=../repo.git, core.bare = true' true false false '' +test_rev_parse -C work 'GIT_DIR=../repo.git, core.bare = true' true false false '' git config --unset core.bare -test_rev_parse 'GIT_DIR=../repo.git, core.bare undefined' false false true '' +test_rev_parse -C work 'GIT_DIR=../repo.git, core.bare undefined' false false true '' test_done
Signature · 1 line
2.8.2.703.g78b384c
Re: [PATCH v2 3/5] t1500: avoid changing working directory outside of tests
t/t1500-rev-parse.sh test_rev_parse () { · line 31
Quoted 1 line · Eric Sunshine
t/t1500-rev-parse.sh +1
-0+ git ${dir:+-C "$dir"} rev-parse --$o >actual &&
This is kosher POSIX, but I vaguely recall some shells had trouble with the SP between -C and "$dir" in the past. Let's see if anybody screams; hopefully I am misremembering or buggy shells died out.
Other than that, the entire series makes the script easier to grok. Thanks.
Re: [PATCH v2 3/5] t1500: avoid changing working directory outside of tests
Quoted 6 lines · Junio C Hamano
Quoted 1 line · Eric Sunshine
+ git ${dir:+-C "$dir"} rev-parse --$o >actual &&
This is kosher POSIX, but I vaguely recall some shells had trouble with the SP between -C and "$dir" in the past. Let's see if anybody screams; hopefully I am misremembering or buggy shells died out.
I also am bothered by a vague recollection of some issue (possibly involving the internal space and lack of quotes around the entire ${...}), but couldn't remember nor find a reference to the specific details. Perhaps someone reading the patch has a better memory than I.
Re: [PATCH v2 3/5] t1500: avoid changing working directory outside of tests
Quoted 4 lines · Eric Sunshine
Quoted 3 lines · Junio C Hamano
Quoted 1 line · Eric Sunshine
+ git ${dir:+-C "$dir"} rev-parse --$o >actual &&
Quoted 8 lines
Quoted 3 lines
This is kosher POSIX, but I vaguely recall some shells had trouble with the SP between -C and "$dir" in the past. Let's see if anybody screams; hopefully I am misremembering or buggy shells died out.
I also am bothered by a vague recollection of some issue (possibly involving the internal space and lack of quotes around the entire ${...}), but couldn't remember nor find a reference to the specific details. Perhaps someone reading the patch has a better memory than I.
Probably:
http://thread.gmane.org/gmane.comp.version-control.git/265094
-Peff
Re: [PATCH v2 3/5] t1500: avoid changing working directory outside of tests
Quoted 9 lines · Jeff King
Quoted 8 lines · Eric Sunshine
Quoted 6 lines · Junio C Hamano
Quoted 1 line · Eric Sunshine
+ git ${dir:+-C "$dir"} rev-parse --$o >actual &&
This is kosher POSIX, but I vaguely recall some shells had trouble with the SP between -C and "$dir" in the past. Let's see if anybody screams; hopefully I am misremembering or buggy shells died out.
Quoted 6 lines
Quoted 4 lines
I also am bothered by a vague recollection of some issue (possibly involving the internal space and lack of quotes around the entire ${...}), but couldn't remember nor find a reference to the specific details. Perhaps someone reading the patch has a better memory than I.
http://thread.gmane.org/gmane.comp.version-control.git/265094
Thanks for the link. I just tested with FreeBSD 8, and the shell indeed exhibits that broken behavior. The workaround in Kyle's patch isn't the prettiest (and is a bit verbose), but it gets the job done.
I can send v3 using that workaround unless Junio wants to patch it locally(?).
Re: [PATCH v2 3/5] t1500: avoid changing working directory outside of tests
In reply to Jeff KingQuoted 10 lines · Jeff King
Quoted 8 lines · Eric Sunshine
Quoted 6 lines · Junio C Hamano
Quoted 1 line · Eric Sunshine
+ git ${dir:+-C "$dir"} rev-parse --$o >actual &&
This is kosher POSIX, but I vaguely recall some shells had trouble with the SP between -C and "$dir" in the past. Let's see if anybody screams; hopefully I am misremembering or buggy shells died out.
Quoted 8 lines
Quoted 4 lines
I also am bothered by a vague recollection of some issue (possibly involving the internal space and lack of quotes around the entire ${...}), but couldn't remember nor find a reference to the specific details. Perhaps someone reading the patch has a better memory than I.
Probably:
http://thread.gmane.org/gmane.comp.version-control.git/265094
Yikes, you're right. Does anybody know if FreeBSD shell is still buggy?
Re: [PATCH v2 3/5] t1500: avoid changing working directory outside of tests
Quoting Junio C Hamano <[email protected]>:
Quoted 12 lines
Quoted 10 lines · Jeff King
Quoted 8 lines · Eric Sunshine
Quoted 6 lines · Junio C Hamano
Quoted 1 line · Eric Sunshine
+ git ${dir:+-C "$dir"} rev-parse --$o >actual &&
This is kosher POSIX, but I vaguely recall some shells had trouble with the SP between -C and "$dir" in the past. Let's see if anybody screams; hopefully I am misremembering or buggy shells died out.
Quoted 8 lines
Quoted 8 lines
Quoted 4 lines
I also am bothered by a vague recollection of some issue (possibly involving the internal space and lack of quotes around the entire ${...}), but couldn't remember nor find a reference to the specific details. Perhaps someone reading the patch has a better memory than I.
Probably:
http://thread.gmane.org/gmane.comp.version-control.git/265094
And ea10b60c910e (Work around ash "alternate value" expansion bug, 2009-04-18) as well.
http://thread.gmane.org/gmane.comp.version-control.git/116816
Quoted 2 lines
Yikes, you're right. Does anybody know if FreeBSD shell is still buggy?
git-submodule.sh contains a few offending constructs:
git submodule--helper update-clone ${GIT_QUIET:+--quiet} \
${wt_prefix:+--prefix "$wt_prefix"} \
${prefix:+--recursive-prefix "$prefix"} \
${update:+--update "$update"} \
${reference:+--reference "$reference"} \
${depth:+--depth "$depth"} \
"$@" || echo "#unmatched"
} | {They were added recently in 48308681b072 (git submodule update: have a dedicated helper for cloning, 2016-02-29), which is not in any release yet, perhaps that's why noone complained yet.
Re: [PATCH v2 3/5] t1500: avoid changing working directory outside of tests
Quoted 17 lines · SZEDER Gábor
Quoting Junio C Hamano <[email protected]>:
Quoted 16 lines
Quoted 15 lines · Jeff King
Quoted 13 lines · Eric Sunshine
Quoted 6 lines · Junio C Hamano
Quoted 1 line · Eric Sunshine
+ git ${dir:+-C "$dir"} rev-parse --$o >actual &&
This is kosher POSIX, but I vaguely recall some shells had trouble with the SP between -C and "$dir" in the past. Let's see if anybody screams; hopefully I am misremembering or buggy shells died out.
I also am bothered by a vague recollection of some issue (possibly involving the internal space and lack of quotes around the entire ${...}), but couldn't remember nor find a reference to the specific details. Perhaps someone reading the patch has a better memory than I.
Quoted 7 lines
Quoted 2 lines
Quoted 2 lines
Probably:
http://thread.gmane.org/gmane.comp.version-control.git/265094And
ea10b60c910e(Work around ash "alternate value" expansion bug, 2009-04-18) as well.http://thread.gmane.org/gmane.comp.version-control.git/116816
Thanks for the additional link. I have v3 ready to roll and will send it out within the next day if no more actionable review comments arrive.
PATCH v2 4/5 t1500: avoid setting configuration options outside of tests
In reply to Eric SunshineIdeally, each test should be responsible for setting up state it needs rather than relying upon transient global state. Toward this end, teach test_rev_parse() to accept a "-b <value>" option to allow callers to set "core.bare" explicitly or undefine it. Take advantage of this new option to avoid setting "core.bare" outside of tests.
Under the hood, "-b <value>" invokes "test_config -C <dir>" (or "test_unconfig -C <dir>"), thus git-config knows explicitly where to find its configuration file. Consequently, the global GIT_CONFIG environment variable required by the manual git-config invocations outside of tests is no longer needed, and is thus dropped.
--- t/t1500-rev-parse.sh | 39 ++++++++++++++++++++------------------- 1 file changed, 20 insertions(+), 19 deletions(-)
t/t1500-rev-parse.sh +20 -19
@@ -6,10 +6,15 @@ test_description='test git rev-parse' # usage: [options] label is-bare is-inside-git is-inside-work prefix git-dir test_rev_parse () { dir= + bare= while : do case "$1" in -C) dir="$2"; shift; shift ;; + -b) case "$2" in + [tfu]*) bare="$2"; shift; shift ;; + *) error "test_rev_parse: bogus core.bare value '$2'" ;; + esac ;; -*) error "test_rev_parse: unrecognized option '$1'" ;; *) break ;; esac
@@ -27,6 +32,12 @@ test_rev_parse () { test $# -eq 0 && break expect="$1" test_expect_success "$name: $o" ' + case "$bare" in + t*) test_config ${dir:+-C "$dir"} core.bare true ;; + f*) test_config ${dir:+-C "$dir"} core.bare false ;; + u*) test_unconfig ${dir:+-C "$dir"} core.bare ;; + esac && + echo "$expect" >expect && git ${dir:+-C "$dir"} rev-parse --$o >actual && test_cmp expect actual
@@ -49,35 +60,25 @@ test_rev_parse -C .git/objects .git/objects/ false true false '' "$ROOT/.git" test_rev_parse -C sub/dir subdirectory false false true sub/dir/ "$ROOT/.git" -git config core.bare true -test_rev_parse 'core.bare = true' true false false +test_rev_parse -b t 'core.bare = true' true false false -git config --unset core.bare -test_rev_parse 'core.bare undefined' false false true +test_rev_parse -b u 'core.bare undefined' false false true GIT_DIR=../.git -GIT_CONFIG="$(pwd)/work/../.git/config" -export GIT_DIR GIT_CONFIG +export GIT_DIR -git config core.bare false -test_rev_parse -C work 'GIT_DIR=../.git, core.bare = false' false false true '' +test_rev_parse -C work -b f 'GIT_DIR=../.git, core.bare = false' false false true '' -git config core.bare true -test_rev_parse -C work 'GIT_DIR=../.git, core.bare = true' true false false '' +test_rev_parse -C work -b t 'GIT_DIR=../.git, core.bare = true' true false false '' -git config --unset core.bare -test_rev_parse -C work 'GIT_DIR=../.git, core.bare undefined' false false true '' +test_rev_parse -C work -b u 'GIT_DIR=../.git, core.bare undefined' false false true '' GIT_DIR=../repo.git -GIT_CONFIG="$(pwd)/work/../repo.git/config" -git config core.bare false -test_rev_parse -C work 'GIT_DIR=../repo.git, core.bare = false' false false true '' +test_rev_parse -C work -b f 'GIT_DIR=../repo.git, core.bare = false' false false true '' -git config core.bare true -test_rev_parse -C work 'GIT_DIR=../repo.git, core.bare = true' true false false '' +test_rev_parse -C work -b t 'GIT_DIR=../repo.git, core.bare = true' true false false '' -git config --unset core.bare -test_rev_parse -C work 'GIT_DIR=../repo.git, core.bare undefined' false false true '' +test_rev_parse -C work -b u 'GIT_DIR=../repo.git, core.bare undefined' false false true '' test_done
Signature · 1 line
2.8.2.703.g78b384c
PATCH v2 5/5 t1500: avoid setting environment variables outside of tests
In reply to Eric SunshineIdeally, each test should be responsible for setting up state it needs rather than relying upon transient global state. Toward this end, teach test_rev_parse() to accept a "-g <dir>" option to allow callers to specify the value of the GIT_DIR environment variable explicitly. Take advantage of this new option to avoid polluting the global scope with GIT_DIR assignments.
Implementation note: Typically, tests avoid polluting the global state by wrapping transient environment variable assignments within a subshell, however, this technique doesn't work here since test_config() and test_unconfig() need to know GIT_DIR, as well, but neither function can be used within a subshell. Consequently, GIT_DIR is instead cleared manually via test_when_finished().
--- t/t1500-rev-parse.sh | 24 +++++++++++++++--------- 1 file changed, 15 insertions(+), 9 deletions(-)
t/t1500-rev-parse.sh +15 -9
@@ -7,6 +7,7 @@ test_description='test git rev-parse' test_rev_parse () { dir= bare= + gitdir= while : do case "$1" in
@@ -15,6 +16,7 @@ test_rev_parse () { [tfu]*) bare="$2"; shift; shift ;; *) error "test_rev_parse: bogus core.bare value '$2'" ;; esac ;; + -g) gitdir="$2"; shift; shift ;; -*) error "test_rev_parse: unrecognized option '$1'" ;; *) break ;; esac
@@ -32,6 +34,13 @@ test_rev_parse () { test $# -eq 0 && break expect="$1" test_expect_success "$name: $o" ' + if test -n "$gitdir" + then + test_when_finished "unset GIT_DIR" && + GIT_DIR="$gitdir" && + export GIT_DIR + fi && + case "$bare" in t*) test_config ${dir:+-C "$dir"} core.bare true ;; f*) test_config ${dir:+-C "$dir"} core.bare false ;;
@@ -64,21 +73,18 @@ test_rev_parse -b t 'core.bare = true' true false false test_rev_parse -b u 'core.bare undefined' false false true -GIT_DIR=../.git -export GIT_DIR -test_rev_parse -C work -b f 'GIT_DIR=../.git, core.bare = false' false false true '' +test_rev_parse -C work -g ../.git -b f 'GIT_DIR=../.git, core.bare = false' false false true '' -test_rev_parse -C work -b t 'GIT_DIR=../.git, core.bare = true' true false false '' +test_rev_parse -C work -g ../.git -b t 'GIT_DIR=../.git, core.bare = true' true false false '' -test_rev_parse -C work -b u 'GIT_DIR=../.git, core.bare undefined' false false true '' +test_rev_parse -C work -g ../.git -b u 'GIT_DIR=../.git, core.bare undefined' false false true '' -GIT_DIR=../repo.git -test_rev_parse -C work -b f 'GIT_DIR=../repo.git, core.bare = false' false false true '' +test_rev_parse -C work -g ../repo.git -b f 'GIT_DIR=../repo.git, core.bare = false' false false true '' -test_rev_parse -C work -b t 'GIT_DIR=../repo.git, core.bare = true' true false false '' +test_rev_parse -C work -g ../repo.git -b t 'GIT_DIR=../repo.git, core.bare = true' true false false '' -test_rev_parse -C work -b u 'GIT_DIR=../repo.git, core.bare undefined' false false true '' +test_rev_parse -C work -g ../repo.git -b u 'GIT_DIR=../repo.git, core.bare undefined' false false true '' test_done
Signature · 1 line
2.8.2.703.g78b384c