Win32: patch Windows environment on startup
[git/mingw/4msysgit/kblees.git] / t / t7800-difftool.sh
blob4fb4c9384a0045d3b041d627e9d814637d9268e2
1 #!/bin/sh
3 # Copyright (c) 2009, 2010 David Aguilar
6 test_description='git-difftool
8 Testing basic diff tool invocation
11 . ./test-lib.sh
13 remove_config_vars()
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
26 restore_test_defaults()
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
38 prompt_given()
40 prompt="$1"
41 test "$prompt" = "Launch 'test-tool' [Y/n]: branch"
44 stdin_contains()
46 grep >/dev/null "$1"
49 stdin_doesnot_contain()
51 ! stdin_contains "$1"
54 # Create a file on master and change it on branch
55 test_expect_success PERL 'setup' '
56 echo master >file &&
57 git add file &&
58 git commit -m "added file" &&
60 git checkout -b branch master &&
61 echo branch >file &&
62 git commit -a -m "branch changed file" &&
63 git checkout master
66 # Configure a custom difftool.<tool>.cmd and use it
67 test_expect_success PERL 'custom commands' '
68 restore_test_defaults &&
69 git config difftool.test-tool.cmd "cat \$REMOTE" &&
71 diff=$(git difftool --no-prompt branch) &&
72 test "$diff" = "master" &&
74 restore_test_defaults &&
75 diff=$(git difftool --no-prompt branch) &&
76 test "$diff" = "branch"
79 # Ensures that git-difftool ignores bogus --tool values
80 test_expect_success PERL 'difftool ignores bad --tool values' '
81 diff=$(git difftool --no-prompt --tool=bad-tool branch)
82 test "$?" = 1 &&
83 test "$diff" = ""
86 test_expect_success PERL 'difftool honors --gui' '
87 git config merge.tool bogus-tool &&
88 git config diff.tool bogus-tool &&
89 git config diff.guitool test-tool &&
91 diff=$(git difftool --no-prompt --gui branch) &&
92 test "$diff" = "branch" &&
94 restore_test_defaults
97 test_expect_success PERL 'difftool --gui works without configured diff.guitool' '
98 git config diff.tool test-tool &&
100 diff=$(git difftool --no-prompt --gui branch) &&
101 test "$diff" = "branch" &&
103 restore_test_defaults
106 # Specify the diff tool using $GIT_DIFF_TOOL
107 test_expect_success PERL 'GIT_DIFF_TOOL variable' '
108 test_might_fail git config --unset diff.tool &&
109 GIT_DIFF_TOOL=test-tool &&
110 export GIT_DIFF_TOOL &&
112 diff=$(git difftool --no-prompt branch) &&
113 test "$diff" = "branch" &&
115 restore_test_defaults
118 # Test the $GIT_*_TOOL variables and ensure
119 # that $GIT_DIFF_TOOL always wins unless --tool is specified
120 test_expect_success PERL 'GIT_DIFF_TOOL overrides' '
121 git config diff.tool bogus-tool &&
122 git config merge.tool bogus-tool &&
124 GIT_DIFF_TOOL=test-tool &&
125 export GIT_DIFF_TOOL &&
127 diff=$(git difftool --no-prompt branch) &&
128 test "$diff" = "branch" &&
130 GIT_DIFF_TOOL=bogus-tool &&
131 export GIT_DIFF_TOOL &&
133 diff=$(git difftool --no-prompt --tool=test-tool branch) &&
134 test "$diff" = "branch" &&
136 restore_test_defaults
139 # Test that we don't have to pass --no-prompt to difftool
140 # when $GIT_DIFFTOOL_NO_PROMPT is true
141 test_expect_success PERL 'GIT_DIFFTOOL_NO_PROMPT variable' '
142 GIT_DIFFTOOL_NO_PROMPT=true &&
143 export GIT_DIFFTOOL_NO_PROMPT &&
145 diff=$(git difftool branch) &&
146 test "$diff" = "branch" &&
148 restore_test_defaults
151 # git-difftool supports the difftool.prompt variable.
152 # Test that GIT_DIFFTOOL_PROMPT can override difftool.prompt = false
153 test_expect_success PERL 'GIT_DIFFTOOL_PROMPT variable' '
154 git config difftool.prompt false &&
155 GIT_DIFFTOOL_PROMPT=true &&
156 export GIT_DIFFTOOL_PROMPT &&
158 prompt=$(echo | git difftool branch | tail -1) &&
159 prompt_given "$prompt" &&
161 restore_test_defaults
164 # Test that we don't have to pass --no-prompt when difftool.prompt is false
165 test_expect_success PERL 'difftool.prompt config variable is false' '
166 git config difftool.prompt false &&
168 diff=$(git difftool branch) &&
169 test "$diff" = "branch" &&
171 restore_test_defaults
174 # Test that we don't have to pass --no-prompt when mergetool.prompt is false
175 test_expect_success PERL 'difftool merge.prompt = false' '
176 test_might_fail git config --unset difftool.prompt &&
177 git config mergetool.prompt false &&
179 diff=$(git difftool branch) &&
180 test "$diff" = "branch" &&
182 restore_test_defaults
185 # Test that the -y flag can override difftool.prompt = true
186 test_expect_success PERL 'difftool.prompt can overridden with -y' '
187 git config difftool.prompt true &&
189 diff=$(git difftool -y branch) &&
190 test "$diff" = "branch" &&
192 restore_test_defaults
195 # Test that the --prompt flag can override difftool.prompt = false
196 test_expect_success PERL 'difftool.prompt can overridden with --prompt' '
197 git config difftool.prompt false &&
199 prompt=$(echo | git difftool --prompt branch | tail -1) &&
200 prompt_given "$prompt" &&
202 restore_test_defaults
205 # Test that the last flag passed on the command-line wins
206 test_expect_success PERL 'difftool last flag wins' '
207 diff=$(git difftool --prompt --no-prompt branch) &&
208 test "$diff" = "branch" &&
210 restore_test_defaults &&
212 prompt=$(echo | git difftool --no-prompt --prompt branch | tail -1) &&
213 prompt_given "$prompt" &&
215 restore_test_defaults
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' '
221 remove_config_vars &&
222 git config merge.tool test-tool &&
223 git config mergetool.test-tool.cmd "cat \$LOCAL" &&
225 diff=$(git difftool --no-prompt branch) &&
226 test "$diff" = "branch" &&
228 # set merge.tool to something bogus, diff.tool to test-tool
229 git config merge.tool bogus-tool &&
230 git config diff.tool test-tool &&
232 diff=$(git difftool --no-prompt branch) &&
233 test "$diff" = "branch" &&
235 restore_test_defaults
238 test_expect_success PERL 'difftool.<tool>.path' '
239 git config difftool.tkdiff.path echo &&
240 diff=$(git difftool --tool=tkdiff --no-prompt branch) &&
241 git config --unset difftool.tkdiff.path &&
242 lines=$(echo "$diff" | grep file | wc -l) &&
243 test "$lines" -eq 1 &&
245 restore_test_defaults
248 test_expect_success PERL 'difftool --extcmd=cat' '
249 diff=$(git difftool --no-prompt --extcmd=cat branch) &&
250 test "$diff" = branch"$LF"master
253 test_expect_success PERL 'difftool --extcmd cat' '
254 diff=$(git difftool --no-prompt --extcmd cat branch) &&
255 test "$diff" = branch"$LF"master
258 test_expect_success PERL 'difftool -x cat' '
259 diff=$(git difftool --no-prompt -x cat branch) &&
260 test "$diff" = branch"$LF"master
263 test_expect_success PERL 'difftool --extcmd echo arg1' '
264 diff=$(git difftool --no-prompt --extcmd sh\ -c\ \"echo\ \$1\" branch) &&
265 test "$diff" = file
268 test_expect_success PERL 'difftool --extcmd cat arg1' '
269 diff=$(git difftool --no-prompt --extcmd sh\ -c\ \"cat\ \$1\" branch) &&
270 test "$diff" = master
273 test_expect_success PERL 'difftool --extcmd cat arg2' '
274 diff=$(git difftool --no-prompt --extcmd sh\ -c\ \"cat\ \$2\" branch) &&
275 test "$diff" = branch
278 # Create a second file on master and a different version on branch
279 test_expect_success PERL 'setup with 2 files different' '
280 echo m2 >file2 &&
281 git add file2 &&
282 git commit -m "added file2" &&
284 git checkout branch &&
285 echo br2 >file2 &&
286 git add file2 &&
287 git commit -a -m "branch changed file2" &&
288 git checkout master
291 test_expect_success PERL 'say no to the first file' '
292 diff=$( (echo n; echo) | git difftool -x cat branch ) &&
294 echo "$diff" | stdin_contains m2 &&
295 echo "$diff" | stdin_contains br2 &&
296 echo "$diff" | stdin_doesnot_contain master &&
297 echo "$diff" | stdin_doesnot_contain branch
300 test_expect_success PERL 'say no to the second file' '
301 diff=$( (echo; echo n) | git difftool -x cat branch ) &&
303 echo "$diff" | stdin_contains master &&
304 echo "$diff" | stdin_contains branch &&
305 echo "$diff" | stdin_doesnot_contain m2 &&
306 echo "$diff" | stdin_doesnot_contain br2
309 test_done