tests: eliminate unnecessary setup test assertions

Most of git's tests write files and define shell functions and variables that will last throughout a test script at the top of the script, before all test assertions: . ./test-lib.sh VAR='some value' export VAR >empty fn () { do something } test_expect_success 'setup' ' ... nontrivial commands go here ... ' Two scripts use a different style with this kind of trivial code enclosed by a test assertion; fix them. The usual style is easier to read since there is less indentation to keep track of and no need to worry about nested quotes; and on the other hand, because the commands in question are trivial, it should not make the test suite any worse at catching future bugs in git. While at it, make some other small tweaks: - spell function definitions with a space before () for consistency with other scripts; - use the self-contained command "git mktree </dev/null" in preference to "git write-tree" which looks at the index when writing an empty tree. Signed-off-by: Jonathan Nieder <jrnieder@gmail.com> Signed-off-by: Junio C Hamano <gitster@pobox.com>

Jonathan Nieder committed May 6, 2011 at 15:58 UTC 73151df0cf3b324fc5f8980774601794c4b88204
2 files changed +97 -99
t/t6010-merge-base.sh
+25 -25
@@ -8,38 +8,38 @@ test_description='Merge base and parent list computation.
8
9 . ./test-lib.sh
10
11 -test_expect_success 'setup' '
12 - T=$(git write-tree) &&
11 +M=1130000000
12 +Z=+0000
13
14 - M=1130000000 &&
15 - Z=+0000 &&
14 +GIT_COMMITTER_EMAIL=git@comm.iter.xz
15 +GIT_COMMITTER_NAME='C O Mmiter'
16 +GIT_AUTHOR_NAME='A U Thor'
17 +GIT_AUTHOR_EMAIL=git@au.thor.xz
18 +export GIT_COMMITTER_EMAIL GIT_COMMITTER_NAME GIT_AUTHOR_NAME GIT_AUTHOR_EMAIL
19
17 - GIT_COMMITTER_EMAIL=git@comm.iter.xz &&
18 - GIT_COMMITTER_NAME="C O Mmiter" &&
19 - GIT_AUTHOR_NAME="A U Thor" &&
20 - GIT_AUTHOR_EMAIL=git@au.thor.xz &&
21 - export GIT_COMMITTER_EMAIL GIT_COMMITTER_NAME GIT_AUTHOR_NAME GIT_AUTHOR_EMAIL &&
20 +doit () {
21 + OFFSET=$1 &&
22 + NAME=$2 &&
23 + shift 2 &&
24
23 - doit() {
24 - OFFSET=$1 &&
25 - NAME=$2 &&
26 - shift 2 &&
25 + PARENTS= &&
26 + for P
27 + do
28 + PARENTS="${PARENTS}-p $P "
29 + done &&
30
28 - PARENTS= &&
29 - for P
30 - do
31 - PARENTS="${PARENTS}-p $P "
32 - done &&
31 + GIT_COMMITTER_DATE="$(($M + $OFFSET)) $Z" &&
32 + GIT_AUTHOR_DATE=$GIT_COMMITTER_DATE &&
33 + export GIT_COMMITTER_DATE GIT_AUTHOR_DATE &&
34
34 - GIT_COMMITTER_DATE="$(($M + $OFFSET)) $Z" &&
35 - GIT_AUTHOR_DATE=$GIT_COMMITTER_DATE &&
36 - export GIT_COMMITTER_DATE GIT_AUTHOR_DATE &&
35 + commit=$(echo $NAME | git commit-tree $T $PARENTS) &&
36
38 - commit=$(echo $NAME | git commit-tree $T $PARENTS) &&
37 + echo $commit >.git/refs/tags/$NAME &&
38 + echo $commit
39 +}
40
40 - echo $commit >.git/refs/tags/$NAME &&
41 - echo $commit
42 - }
41 +test_expect_success 'setup' '
42 + T=$(git mktree </dev/null)
43 '
44
45 test_expect_success 'set up G and H' '
t/t7600-merge.sh
+72 -74
@@ -28,80 +28,78 @@ Testing basic merge operations/option parsing.
28
29 . ./test-lib.sh
30
31 -test_expect_success 'set up test data and helpers' '
32 - printf "%s\n" 1 2 3 4 5 6 7 8 9 >file &&
33 - printf "%s\n" "1 X" 2 3 4 5 6 7 8 9 >file.1 &&
34 - printf "%s\n" 1 2 3 4 "5 X" 6 7 8 9 >file.5 &&
35 - printf "%s\n" 1 2 3 4 5 6 7 8 "9 X" >file.9 &&
36 - printf "%s\n" "1 X" 2 3 4 5 6 7 8 9 >result.1 &&
37 - printf "%s\n" "1 X" 2 3 4 "5 X" 6 7 8 9 >result.1-5 &&
38 - printf "%s\n" "1 X" 2 3 4 "5 X" 6 7 8 "9 X" >result.1-5-9 &&
39 -
40 - create_merge_msgs() {
41 - echo "Merge commit '\''c2'\''" >msg.1-5 &&
42 - echo "Merge commit '\''c2'\''; commit '\''c3'\''" >msg.1-5-9 &&
43 - {
44 - echo "Squashed commit of the following:" &&
45 - echo &&
46 - git log --no-merges ^HEAD c1
47 - } >squash.1 &&
48 - {
49 - echo "Squashed commit of the following:" &&
50 - echo &&
51 - git log --no-merges ^HEAD c2
52 - } >squash.1-5 &&
53 - {
54 - echo "Squashed commit of the following:" &&
55 - echo &&
56 - git log --no-merges ^HEAD c2 c3
57 - } >squash.1-5-9 &&
58 - echo >msg.nolog &&
59 - {
60 - echo "* commit '\''c3'\'':" &&
61 - echo " commit 3" &&
62 - echo
63 - } >msg.log
64 - } &&
65 -
66 - verify_merge() {
67 - test_cmp "$2" "$1" &&
68 - git update-index --refresh &&
69 - git diff --exit-code &&
70 - if test -n "$3"
71 - then
72 - git show -s --pretty=format:%s HEAD >msg.act &&
73 - test_cmp "$3" msg.act
74 - fi
75 - } &&
76 -
77 - verify_head() {
78 - echo "$1" >head.expected &&
79 - git rev-parse HEAD >head.actual &&
80 - test_cmp head.expected head.actual
81 - } &&
82 -
83 - verify_parents() {
84 - printf "%s\n" "$@" >parents.expected &&
85 - >parents.actual &&
86 - i=1 &&
87 - while test $i -le $#
88 - do
89 - git rev-parse HEAD^$i >>parents.actual &&
90 - i=$(expr $i + 1) ||
91 - return 1
92 - done &&
93 - test_cmp parents.expected parents.actual
94 - } &&
95 -
96 - verify_mergeheads() {
97 - printf "%s\n" "$@" >mergehead.expected &&
98 - test_cmp mergehead.expected .git/MERGE_HEAD
99 - } &&
100 -
101 - verify_no_mergehead() {
102 - ! test -e .git/MERGE_HEAD
103 - }
104 -'
31 +printf '%s\n' 1 2 3 4 5 6 7 8 9 >file
32 +printf '%s\n' '1 X' 2 3 4 5 6 7 8 9 >file.1
33 +printf '%s\n' 1 2 3 4 '5 X' 6 7 8 9 >file.5
34 +printf '%s\n' 1 2 3 4 5 6 7 8 '9 X' >file.9
35 +printf '%s\n' '1 X' 2 3 4 5 6 7 8 9 >result.1
36 +printf '%s\n' '1 X' 2 3 4 '5 X' 6 7 8 9 >result.1-5
37 +printf '%s\n' '1 X' 2 3 4 '5 X' 6 7 8 '9 X' >result.1-5-9
38 +
39 +create_merge_msgs () {
40 + echo "Merge commit 'c2'" >msg.1-5 &&
41 + echo "Merge commit 'c2'; commit 'c3'" >msg.1-5-9 &&
42 + {
43 + echo "Squashed commit of the following:" &&
44 + echo &&
45 + git log --no-merges ^HEAD c1
46 + } >squash.1 &&
47 + {
48 + echo "Squashed commit of the following:" &&
49 + echo &&
50 + git log --no-merges ^HEAD c2
51 + } >squash.1-5 &&
52 + {
53 + echo "Squashed commit of the following:" &&
54 + echo &&
55 + git log --no-merges ^HEAD c2 c3
56 + } >squash.1-5-9 &&
57 + echo >msg.nolog &&
58 + {
59 + echo "* commit 'c3':" &&
60 + echo " commit 3" &&
61 + echo
62 + } >msg.log
63 +}
64 +
65 +verify_merge () {
66 + test_cmp "$2" "$1" &&
67 + git update-index --refresh &&
68 + git diff --exit-code &&
69 + if test -n "$3"
70 + then
71 + git show -s --pretty=format:%s HEAD >msg.act &&
72 + test_cmp "$3" msg.act
73 + fi
74 +}
75 +
76 +verify_head () {
77 + echo "$1" >head.expected &&
78 + git rev-parse HEAD >head.actual &&
79 + test_cmp head.expected head.actual
80 +}
81 +
82 +verify_parents () {
83 + printf '%s\n' "$@" >parents.expected &&
84 + >parents.actual &&
85 + i=1 &&
86 + while test $i -le $#
87 + do
88 + git rev-parse HEAD^$i >>parents.actual &&
89 + i=$(expr $i + 1) ||
90 + return 1
91 + done &&
92 + test_cmp parents.expected parents.actual
93 +}
94 +
95 +verify_mergeheads () {
96 + printf '%s\n' "$@" >mergehead.expected &&
97 + test_cmp mergehead.expected .git/MERGE_HEAD
98 +}
99 +
100 +verify_no_mergehead () {
101 + ! test -e .git/MERGE_HEAD
102 +}
103
104 test_expect_success 'setup' '
105 git add file &&