t7800: modernize tests

Eliminate a lot of redundant work by using test_config(). Catch more return codes by more use of temporary files and test_cmp. The original tests relied upon restore_test_defaults() from the previous test to provide the next test with a sane environment. Make the tests do their own setup so that they are not dependent on the success of the previous test. The end result is shorter tests and better test isolation. Signed-off-by: David Aguilar <davvid@gmail.com> Signed-off-by: Junio C Hamano <gitster@pobox.com>

David Aguilar committed Feb 20, 2013 at 20:03 UTC e42360c48eebc633b1536904839a1a073780ad8a
1 file changed +168 -198
t/t7800-difftool.sh
+168 -198
@@ -10,43 +10,25 @@ Testing basic diff tool invocation
10
11 . ./test-lib.sh
12
13 -remove_config_vars()
13 +difftool_test_setup ()
14 {
15 - # Unset all config variables used by git-difftool
16 - git config --unset diff.tool
17 - git config --unset diff.guitool
18 - git config --unset difftool.test-tool.cmd
19 - git config --unset difftool.prompt
20 - git config --unset merge.tool
21 - git config --unset mergetool.test-tool.cmd
22 - git config --unset mergetool.prompt
23 - return 0
15 + test_config diff.tool test-tool &&
16 + test_config difftool.test-tool.cmd 'cat "$LOCAL"' &&
17 + test_config difftool.bogus-tool.cmd false
18 }
19
26 -restore_test_defaults()
27 -{
28 - # Restores the test defaults used by several tests
29 - remove_config_vars
30 - unset GIT_DIFF_TOOL
31 - unset GIT_DIFFTOOL_PROMPT
32 - unset GIT_DIFFTOOL_NO_PROMPT
33 - git config diff.tool test-tool &&
34 - git config difftool.test-tool.cmd 'cat $LOCAL'
35 - git config difftool.bogus-tool.cmd false
36 -}
37 -
38 -prompt_given()
20 +prompt_given ()
21 {
22 prompt="$1"
23 test "$prompt" = "Launch 'test-tool' [Y/n]: branch"
24 }
25
44 -stdin_contains()
26 +stdin_contains ()
27 {
28 grep >/dev/null "$1"
29 }
30
49 -stdin_doesnot_contain()
31 +stdin_doesnot_contain ()
32 {
33 ! stdin_contains "$1"
34 }
@@ -65,249 +47,237 @@ test_expect_success PERL 'setup' '
47
48 # Configure a custom difftool.<tool>.cmd and use it
49 test_expect_success PERL 'custom commands' '
68 - restore_test_defaults &&
69 - git config difftool.test-tool.cmd "cat \$REMOTE" &&
50 + difftool_test_setup &&
51 + test_config difftool.test-tool.cmd "cat \"\$REMOTE\"" &&
52 + echo master >expect &&
53 + git difftool --no-prompt branch >actual &&
54 + test_cmp expect actual &&
55
71 - diff=$(git difftool --no-prompt branch) &&
72 - test "$diff" = "master" &&
73 -
74 - restore_test_defaults &&
75 - diff=$(git difftool --no-prompt branch) &&
76 - test "$diff" = "branch"
56 + test_config difftool.test-tool.cmd "cat \"\$LOCAL\"" &&
57 + echo branch >expect &&
58 + git difftool --no-prompt branch >actual &&
59 + test_cmp expect actual
60 '
61
79 -# Ensures that a custom difftool.<tool>.cmd overrides built-ins
80 -test_expect_success PERL 'custom commands override built-ins' '
81 - restore_test_defaults &&
82 - git config difftool.defaults.cmd "cat \$REMOTE" &&
83 -
84 - diff=$(git difftool --tool defaults --no-prompt branch) &&
85 - test "$diff" = "master" &&
86 -
87 - git config --unset difftool.defaults.cmd
62 +test_expect_success PERL 'custom tool commands override built-ins' '
63 + test_config difftool.defaults.cmd "cat \"\$REMOTE\"" &&
64 + echo master >expect &&
65 + git difftool --tool defaults --no-prompt branch >actual &&
66 + test_cmp expect actual
67 '
68
90 -# Ensures that git-difftool ignores bogus --tool values
69 test_expect_success PERL 'difftool ignores bad --tool values' '
92 - diff=$(git difftool --no-prompt --tool=bad-tool branch)
93 - test "$?" = 1 &&
94 - test "$diff" = ""
70 + : >expect &&
71 + test_expect_code 1 \
72 + git difftool --no-prompt --tool=bad-tool branch >actual &&
73 + test_cmp expect actual
74 '
75
76 test_expect_success PERL 'difftool forwards arguments to diff' '
77 + difftool_test_setup &&
78 >for-diff &&
79 git add for-diff &&
80 echo changes>for-diff &&
81 git add for-diff &&
102 - diff=$(git difftool --cached --no-prompt -- for-diff) &&
103 - test "$diff" = "" &&
82 + : >expect &&
83 + git difftool --cached --no-prompt -- for-diff >actual &&
84 + test_cmp expect actual &&
85 git reset -- for-diff &&
86 rm for-diff
87 '
88
89 test_expect_success PERL 'difftool honors --gui' '
109 - git config merge.tool bogus-tool &&
110 - git config diff.tool bogus-tool &&
111 - git config diff.guitool test-tool &&
112 -
113 - diff=$(git difftool --no-prompt --gui branch) &&
114 - test "$diff" = "branch" &&
90 + difftool_test_setup &&
91 + test_config merge.tool bogus-tool &&
92 + test_config diff.tool bogus-tool &&
93 + test_config diff.guitool test-tool &&
94
116 - restore_test_defaults
95 + echo branch >expect &&
96 + git difftool --no-prompt --gui branch >actual &&
97 + test_cmp expect actual
98 '
99
100 test_expect_success PERL 'difftool --gui last setting wins' '
120 - git config diff.guitool bogus-tool &&
121 - git difftool --no-prompt --gui --no-gui &&
101 + difftool_test_setup &&
102 + : >expect &&
103 + git difftool --no-prompt --gui --no-gui >actual &&
104 + test_cmp expect actual &&
105
123 - git config merge.tool bogus-tool &&
124 - git config diff.tool bogus-tool &&
125 - git config diff.guitool test-tool &&
126 - diff=$(git difftool --no-prompt --no-gui --gui branch) &&
127 - test "$diff" = "branch" &&
128 -
129 - restore_test_defaults
106 + test_config merge.tool bogus-tool &&
107 + test_config diff.tool bogus-tool &&
108 + test_config diff.guitool test-tool &&
109 + echo branch >expect &&
110 + git difftool --no-prompt --no-gui --gui branch >actual &&
111 + test_cmp expect actual
112 '
113
114 test_expect_success PERL 'difftool --gui works without configured diff.guitool' '
133 - git config diff.tool test-tool &&
134 -
135 - diff=$(git difftool --no-prompt --gui branch) &&
136 - test "$diff" = "branch" &&
137 -
138 - restore_test_defaults
115 + difftool_test_setup &&
116 + echo branch >expect &&
117 + git difftool --no-prompt --gui branch >actual &&
118 + test_cmp expect actual
119 '
120
121 # Specify the diff tool using $GIT_DIFF_TOOL
122 test_expect_success PERL 'GIT_DIFF_TOOL variable' '
143 - test_might_fail git config --unset diff.tool &&
144 - GIT_DIFF_TOOL=test-tool &&
145 - export GIT_DIFF_TOOL &&
146 -
147 - diff=$(git difftool --no-prompt branch) &&
148 - test "$diff" = "branch" &&
149 -
150 - restore_test_defaults
123 + difftool_test_setup &&
124 + git config --unset diff.tool &&
125 + echo branch >expect &&
126 + GIT_DIFF_TOOL=test-tool git difftool --no-prompt branch >actual &&
127 + test_cmp expect actual
128 '
129
130 # Test the $GIT_*_TOOL variables and ensure
131 # that $GIT_DIFF_TOOL always wins unless --tool is specified
132 test_expect_success PERL 'GIT_DIFF_TOOL overrides' '
156 - git config diff.tool bogus-tool &&
157 - git config merge.tool bogus-tool &&
158 -
159 - GIT_DIFF_TOOL=test-tool &&
160 - export GIT_DIFF_TOOL &&
161 -
162 - diff=$(git difftool --no-prompt branch) &&
163 - test "$diff" = "branch" &&
133 + difftool_test_setup &&
134 + test_config diff.tool bogus-tool &&
135 + test_config merge.tool bogus-tool &&
136
165 - GIT_DIFF_TOOL=bogus-tool &&
166 - export GIT_DIFF_TOOL &&
137 + echo branch >expect &&
138 + GIT_DIFF_TOOL=test-tool git difftool --no-prompt branch >actual &&
139 + test_cmp expect actual &&
140
168 - diff=$(git difftool --no-prompt --tool=test-tool branch) &&
169 - test "$diff" = "branch" &&
170 -
171 - restore_test_defaults
141 + test_config diff.tool bogus-tool &&
142 + test_config merge.tool bogus-tool &&
143 + GIT_DIFF_TOOL=bogus-tool \
144 + git difftool --no-prompt --tool=test-tool branch >actual &&
145 + test_cmp expect actual
146 '
147
148 # Test that we don't have to pass --no-prompt to difftool
149 # when $GIT_DIFFTOOL_NO_PROMPT is true
150 test_expect_success PERL 'GIT_DIFFTOOL_NO_PROMPT variable' '
177 - GIT_DIFFTOOL_NO_PROMPT=true &&
178 - export GIT_DIFFTOOL_NO_PROMPT &&
179 -
180 - diff=$(git difftool branch) &&
181 - test "$diff" = "branch" &&
182 -
183 - restore_test_defaults
151 + difftool_test_setup &&
152 + echo branch >expect &&
153 + GIT_DIFFTOOL_NO_PROMPT=true git difftool branch >actual &&
154 + test_cmp expect actual
155 '
156
157 # git-difftool supports the difftool.prompt variable.
158 # Test that GIT_DIFFTOOL_PROMPT can override difftool.prompt = false
159 test_expect_success PERL 'GIT_DIFFTOOL_PROMPT variable' '
189 - git config difftool.prompt false &&
190 - GIT_DIFFTOOL_PROMPT=true &&
191 - export GIT_DIFFTOOL_PROMPT &&
192 -
193 - prompt=$(echo | git difftool branch | tail -1) &&
194 - prompt_given "$prompt" &&
195 -
196 - restore_test_defaults
160 + difftool_test_setup &&
161 + test_config difftool.prompt false &&
162 + echo >input &&
163 + GIT_DIFFTOOL_PROMPT=true git difftool branch <input >output &&
164 + prompt=$(tail -1 <output) &&
165 + prompt_given "$prompt"
166 '
167
168 # Test that we don't have to pass --no-prompt when difftool.prompt is false
169 test_expect_success PERL 'difftool.prompt config variable is false' '
201 - git config difftool.prompt false &&
202 -
203 - diff=$(git difftool branch) &&
204 - test "$diff" = "branch" &&
205 -
206 - restore_test_defaults
170 + difftool_test_setup &&
171 + test_config difftool.prompt false &&
172 + echo branch >expect &&
173 + git difftool branch >actual &&
174 + test_cmp expect actual
175 '
176
177 # Test that we don't have to pass --no-prompt when mergetool.prompt is false
178 test_expect_success PERL 'difftool merge.prompt = false' '
179 + difftool_test_setup &&
180 test_might_fail git config --unset difftool.prompt &&
212 - git config mergetool.prompt false &&
213 -
214 - diff=$(git difftool branch) &&
215 - test "$diff" = "branch" &&
216 -
217 - restore_test_defaults
181 + test_config mergetool.prompt false &&
182 + echo branch >expect &&
183 + git difftool branch >actual &&
184 + test_cmp expect actual
185 '
186
187 # Test that the -y flag can override difftool.prompt = true
188 test_expect_success PERL 'difftool.prompt can overridden with -y' '
222 - git config difftool.prompt true &&
223 -
224 - diff=$(git difftool -y branch) &&
225 - test "$diff" = "branch" &&
226 -
227 - restore_test_defaults
189 + difftool_test_setup &&
190 + test_config difftool.prompt true &&
191 + echo branch >expect &&
192 + git difftool -y branch >actual &&
193 + test_cmp expect actual
194 '
195
196 # Test that the --prompt flag can override difftool.prompt = false
197 test_expect_success PERL 'difftool.prompt can overridden with --prompt' '
232 - git config difftool.prompt false &&
233 -
234 - prompt=$(echo | git difftool --prompt branch | tail -1) &&
235 - prompt_given "$prompt" &&
236 -
237 - restore_test_defaults
198 + difftool_test_setup &&
199 + test_config difftool.prompt false &&
200 + echo >input &&
201 + git difftool --prompt branch <input >output &&
202 + prompt=$(tail -1 <output) &&
203 + prompt_given "$prompt"
204 '
205
206 # Test that the last flag passed on the command-line wins
207 test_expect_success PERL 'difftool last flag wins' '
242 - diff=$(git difftool --prompt --no-prompt branch) &&
243 - test "$diff" = "branch" &&
244 -
245 - restore_test_defaults &&
246 -
247 - prompt=$(echo | git difftool --no-prompt --prompt branch | tail -1) &&
248 - prompt_given "$prompt" &&
249 -
250 - restore_test_defaults
208 + difftool_test_setup &&
209 + echo branch >expect &&
210 + git difftool --prompt --no-prompt branch >actual &&
211 + test_cmp expect actual &&
212 + echo >input &&
213 + git difftool --no-prompt --prompt branch <input >output &&
214 + prompt=$(tail -1 <output) &&
215 + prompt_given "$prompt"
216 '
217
218 # git-difftool falls back to git-mergetool config variables
219 # so test that behavior here
220 test_expect_success PERL 'difftool + mergetool config variables' '
256 - remove_config_vars &&
257 - git config merge.tool test-tool &&
258 - git config mergetool.test-tool.cmd "cat \$LOCAL" &&
259 -
260 - diff=$(git difftool --no-prompt branch) &&
261 - test "$diff" = "branch" &&
221 + test_config merge.tool test-tool &&
222 + test_config mergetool.test-tool.cmd "cat \$LOCAL" &&
223 + echo branch >expect &&
224 + git difftool --no-prompt branch >actual &&
225 + test_cmp expect actual &&
226
227 # set merge.tool to something bogus, diff.tool to test-tool
264 - git config merge.tool bogus-tool &&
265 - git config diff.tool test-tool &&
266 -
267 - diff=$(git difftool --no-prompt branch) &&
268 - test "$diff" = "branch" &&
269 -
270 - restore_test_defaults
228 + test_config merge.tool bogus-tool &&
229 + test_config diff.tool test-tool &&
230 + git difftool --no-prompt branch >actual &&
231 + test_cmp expect actual
232 '
233
234 test_expect_success PERL 'difftool.<tool>.path' '
274 - git config difftool.tkdiff.path echo &&
275 - diff=$(git difftool --tool=tkdiff --no-prompt branch) &&
276 - git config --unset difftool.tkdiff.path &&
277 - lines=$(echo "$diff" | grep file | wc -l) &&
278 - test "$lines" -eq 1 &&
279 -
280 - restore_test_defaults
235 + test_config difftool.tkdiff.path echo &&
236 + git difftool --tool=tkdiff --no-prompt branch >output &&
237 + lines=$(grep file output | wc -l) &&
238 + test "$lines" -eq 1
239 '
240
241 test_expect_success PERL 'difftool --extcmd=cat' '
284 - diff=$(git difftool --no-prompt --extcmd=cat branch) &&
285 - test "$diff" = branch"$LF"master
242 + echo branch >expect &&
243 + echo master >>expect &&
244 + git difftool --no-prompt --extcmd=cat branch >actual &&
245 + test_cmp expect actual
246 '
247
248 test_expect_success PERL 'difftool --extcmd cat' '
289 - diff=$(git difftool --no-prompt --extcmd cat branch) &&
290 - test "$diff" = branch"$LF"master
249 + echo branch >expect &&
250 + echo master >>expect &&
251 + git difftool --no-prompt --extcmd=cat branch >actual &&
252 + test_cmp expect actual
253 '
254
255 test_expect_success PERL 'difftool -x cat' '
294 - diff=$(git difftool --no-prompt -x cat branch) &&
295 - test "$diff" = branch"$LF"master
256 + echo branch >expect &&
257 + echo master >>expect &&
258 + git difftool --no-prompt -x cat branch >actual &&
259 + test_cmp expect actual
260 '
261
262 test_expect_success PERL 'difftool --extcmd echo arg1' '
299 - diff=$(git difftool --no-prompt --extcmd sh\ -c\ \"echo\ \$1\" branch) &&
300 - test "$diff" = file
263 + echo file >expect &&
264 + git difftool --no-prompt \
265 + --extcmd sh\ -c\ \"echo\ \$1\" branch >actual &&
266 + test_cmp expect actual
267 '
268
269 test_expect_success PERL 'difftool --extcmd cat arg1' '
304 - diff=$(git difftool --no-prompt --extcmd sh\ -c\ \"cat\ \$1\" branch) &&
305 - test "$diff" = master
270 + echo master >expect &&
271 + git difftool --no-prompt \
272 + --extcmd sh\ -c\ \"cat\ \$1\" branch >actual &&
273 + test_cmp expect actual
274 '
275
276 test_expect_success PERL 'difftool --extcmd cat arg2' '
309 - diff=$(git difftool --no-prompt --extcmd sh\ -c\ \"cat\ \$2\" branch) &&
310 - test "$diff" = branch
277 + echo branch >expect &&
278 + git difftool --no-prompt \
279 + --extcmd sh\ -c\ \"cat\ \$2\" branch >actual &&
280 + test_cmp expect actual
281 '
282
283 # Create a second file on master and a different version on branch
@@ -324,26 +294,26 @@ test_expect_success PERL 'setup with 2 files different' '
294 '
295
296 test_expect_success PERL 'say no to the first file' '
327 - diff=$( (echo n; echo) | git difftool -x cat branch ) &&
328 -
329 - echo "$diff" | stdin_contains m2 &&
330 - echo "$diff" | stdin_contains br2 &&
331 - echo "$diff" | stdin_doesnot_contain master &&
332 - echo "$diff" | stdin_doesnot_contain branch
297 + (echo n && echo) >input &&
298 + git difftool -x cat branch <input >output &&
299 + stdin_contains m2 <output &&
300 + stdin_contains br2 <output &&
301 + stdin_doesnot_contain master <output &&
302 + stdin_doesnot_contain branch <output
303 '
304
305 test_expect_success PERL 'say no to the second file' '
336 - diff=$( (echo; echo n) | git difftool -x cat branch ) &&
337 -
338 - echo "$diff" | stdin_contains master &&
339 - echo "$diff" | stdin_contains branch &&
340 - echo "$diff" | stdin_doesnot_contain m2 &&
341 - echo "$diff" | stdin_doesnot_contain br2
306 + (echo && echo n) >input &&
307 + git difftool -x cat branch <input >output &&
308 + stdin_contains master <output &&
309 + stdin_contains branch <output &&
310 + stdin_doesnot_contain m2 <output &&
311 + stdin_doesnot_contain br2 <output
312 '
313
314 test_expect_success PERL 'difftool --tool-help' '
345 - tool_help=$(git difftool --tool-help) &&
346 - echo "$tool_help" | stdin_contains tool
315 + git difftool --tool-help >output &&
316 + stdin_contains tool <output
317 '
318
319 test_expect_success PERL 'setup change in subdirectory' '
@@ -359,29 +329,29 @@ test_expect_success PERL 'setup change in subdirectory' '
329 '
330
331 test_expect_success PERL 'difftool -d' '
362 - diff=$(git difftool -d --extcmd ls branch) &&
363 - echo "$diff" | stdin_contains sub &&
364 - echo "$diff" | stdin_contains file
332 + git difftool -d --extcmd ls branch >output &&
333 + stdin_contains sub <output &&
334 + stdin_contains file <output
335 '
336
337 test_expect_success PERL 'difftool --dir-diff' '
368 - diff=$(git difftool --dir-diff --extcmd ls branch) &&
369 - echo "$diff" | stdin_contains sub &&
370 - echo "$diff" | stdin_contains file
338 + git difftool --dir-diff --extcmd ls branch >output &&
339 + stdin_contains sub <output &&
340 + stdin_contains file <output
341 '
342
343 test_expect_success PERL 'difftool --dir-diff ignores --prompt' '
374 - diff=$(git difftool --dir-diff --prompt --extcmd ls branch) &&
375 - echo "$diff" | stdin_contains sub &&
376 - echo "$diff" | stdin_contains file
344 + git difftool --dir-diff --prompt --extcmd ls branch >output &&
345 + stdin_contains sub <output &&
346 + stdin_contains file <output
347 '
348
349 test_expect_success PERL 'difftool --dir-diff from subdirectory' '
350 (
351 cd sub &&
382 - diff=$(git difftool --dir-diff --extcmd ls branch) &&
383 - echo "$diff" | stdin_contains sub &&
384 - echo "$diff" | stdin_contains file
352 + git difftool --dir-diff --extcmd ls branch >output &&
353 + stdin_contains sub <output &&
354 + stdin_contains file <output
355 )
356 '
357