Skip to content

Commit f0c570e

Browse files
committed
Merge branch 'ps/t7800-variable-interpolation-fix'
Fix the way recently added tests interpolate variables defined outside them, and document the best practice to help future developers. * ps/t7800-variable-interpolation-fix: t/README: document how to loop around test cases t7800: use single quotes for test bodies t7800: improve test descriptions with empty arguments
2 parents 6938b35 + 7c4449e commit f0c570e

File tree

2 files changed

+40
-20
lines changed

2 files changed

+40
-20
lines changed

t/README

Lines changed: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -724,6 +724,26 @@ The "do's:"
724724
Note that we still &&-chain the loop to propagate failures from
725725
earlier commands.
726726

727+
- Repeat tests with slightly different arguments in a loop.
728+
729+
In some cases it may make sense to re-run the same set of tests with
730+
different options or commands to ensure that the command behaves
731+
despite the different parameters. This can be achieved by looping
732+
around a specific parameter:
733+
734+
for arg in '' "--foo"
735+
do
736+
test_expect_success "test command ${arg:-without arguments}" '
737+
command $arg
738+
'
739+
done
740+
741+
Note that while the test title uses double quotes ("), the test body
742+
should continue to use single quotes (') to avoid breakage in case the
743+
values contain e.g. quoting characters. The loop variable will be
744+
accessible regardless of the single quotes as the test body is passed
745+
to `eval`.
746+
727747

728748
And here are the "don'ts:"
729749

t/t7800-difftool.sh

Lines changed: 20 additions & 20 deletions
Original file line numberDiff line numberDiff line change
@@ -93,42 +93,42 @@ test_expect_success 'difftool forwards arguments to diff' '
9393

9494
for opt in '' '--dir-diff'
9595
do
96-
test_expect_success "difftool ${opt} ignores exit code" "
96+
test_expect_success "difftool ${opt:-without options} ignores exit code" '
9797
test_config difftool.error.cmd false &&
9898
git difftool ${opt} -y -t error branch
99-
"
99+
'
100100

101-
test_expect_success "difftool ${opt} forwards exit code with --trust-exit-code" "
101+
test_expect_success "difftool ${opt:-without options} forwards exit code with --trust-exit-code" '
102102
test_config difftool.error.cmd false &&
103103
test_must_fail git difftool ${opt} -y --trust-exit-code -t error branch
104-
"
104+
'
105105

106-
test_expect_success "difftool ${opt} forwards exit code with --trust-exit-code for built-ins" "
106+
test_expect_success "difftool ${opt:-without options} forwards exit code with --trust-exit-code for built-ins" '
107107
test_config difftool.vimdiff.path false &&
108108
test_must_fail git difftool ${opt} -y --trust-exit-code -t vimdiff branch
109-
"
109+
'
110110

111-
test_expect_success "difftool ${opt} honors difftool.trustExitCode = true" "
111+
test_expect_success "difftool ${opt:-without options} honors difftool.trustExitCode = true" '
112112
test_config difftool.error.cmd false &&
113113
test_config difftool.trustExitCode true &&
114114
test_must_fail git difftool ${opt} -y -t error branch
115-
"
115+
'
116116

117-
test_expect_success "difftool ${opt} honors difftool.trustExitCode = false" "
117+
test_expect_success "difftool ${opt:-without options} honors difftool.trustExitCode = false" '
118118
test_config difftool.error.cmd false &&
119119
test_config difftool.trustExitCode false &&
120120
git difftool ${opt} -y -t error branch
121-
"
121+
'
122122

123-
test_expect_success "difftool ${opt} ignores exit code with --no-trust-exit-code" "
123+
test_expect_success "difftool ${opt:-without options} ignores exit code with --no-trust-exit-code" '
124124
test_config difftool.error.cmd false &&
125125
test_config difftool.trustExitCode true &&
126126
git difftool ${opt} -y --no-trust-exit-code -t error branch
127-
"
127+
'
128128

129-
test_expect_success "difftool ${opt} stops on error with --trust-exit-code" "
130-
test_when_finished 'rm -f for-diff .git/fail-right-file' &&
131-
test_when_finished 'git reset -- for-diff' &&
129+
test_expect_success "difftool ${opt:-without options} stops on error with --trust-exit-code" '
130+
test_when_finished "rm -f for-diff .git/fail-right-file" &&
131+
test_when_finished "git reset -- for-diff" &&
132132
write_script .git/fail-right-file <<-\EOF &&
133133
echo failed
134134
exit 1
@@ -138,19 +138,19 @@ do
138138
test_must_fail git difftool ${opt} -y --trust-exit-code \
139139
--extcmd .git/fail-right-file branch >actual &&
140140
test_line_count = 1 actual
141-
"
141+
'
142142

143-
test_expect_success "difftool ${opt} honors exit status if command not found" "
143+
test_expect_success "difftool ${opt:-without options} honors exit status if command not found" '
144144
test_config difftool.nonexistent.cmd i-dont-exist &&
145145
test_config difftool.trustExitCode false &&
146-
if test "${opt}" = '--dir-diff'
146+
if test "${opt}" = --dir-diff
147147
then
148148
expected_code=127
149149
else
150150
expected_code=128
151151
fi &&
152-
test_expect_code \${expected_code} git difftool ${opt} -y -t nonexistent branch
153-
"
152+
test_expect_code ${expected_code} git difftool ${opt} -y -t nonexistent branch
153+
'
154154
done
155155

156156
test_expect_success 'difftool honors --gui' '

0 commit comments

Comments
 (0)