Code

t7800: Test difftool passing arguments to diff
[git.git] / t / t7800-difftool.sh
1 #!/bin/sh
2 #
3 # Copyright (c) 2009, 2010 David Aguilar
4 #
6 test_description='git-difftool
8 Testing basic diff tool invocation
9 '
11 . ./test-lib.sh
13 remove_config_vars()
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
24 }
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 }
38 prompt_given()
39 {
40         prompt="$1"
41         test "$prompt" = "Launch 'test-tool' [Y/n]: branch"
42 }
44 stdin_contains()
45 {
46         grep >/dev/null "$1"
47 }
49 stdin_doesnot_contain()
50 {
51         ! stdin_contains "$1"
52 }
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
64 '
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"
77 '
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" = ""
84 '
86 test_expect_success PERL 'difftool forwards arguments to diff' '
87         >for-diff &&
88         git add for-diff &&
89         echo changes>for-diff &&
90         git add for-diff &&
91         diff=$(git difftool --cached --no-prompt -- for-diff) &&
92         test "$diff" = "" &&
93         git reset -- for-diff &&
94         rm for-diff
95 '
97 test_expect_success PERL 'difftool honors --gui' '
98         git config merge.tool bogus-tool &&
99         git config diff.tool bogus-tool &&
100         git config diff.guitool test-tool &&
102         diff=$(git difftool --no-prompt --gui branch) &&
103         test "$diff" = "branch" &&
105         restore_test_defaults
108 test_expect_success PERL 'difftool --gui works without configured diff.guitool' '
109         git config diff.tool test-tool &&
111         diff=$(git difftool --no-prompt --gui branch) &&
112         test "$diff" = "branch" &&
114         restore_test_defaults
117 # Specify the diff tool using $GIT_DIFF_TOOL
118 test_expect_success PERL 'GIT_DIFF_TOOL variable' '
119         test_might_fail git config --unset diff.tool &&
120         GIT_DIFF_TOOL=test-tool &&
121         export GIT_DIFF_TOOL &&
123         diff=$(git difftool --no-prompt branch) &&
124         test "$diff" = "branch" &&
126         restore_test_defaults
129 # Test the $GIT_*_TOOL variables and ensure
130 # that $GIT_DIFF_TOOL always wins unless --tool is specified
131 test_expect_success PERL 'GIT_DIFF_TOOL overrides' '
132         git config diff.tool bogus-tool &&
133         git config merge.tool bogus-tool &&
135         GIT_DIFF_TOOL=test-tool &&
136         export GIT_DIFF_TOOL &&
138         diff=$(git difftool --no-prompt branch) &&
139         test "$diff" = "branch" &&
141         GIT_DIFF_TOOL=bogus-tool &&
142         export GIT_DIFF_TOOL &&
144         diff=$(git difftool --no-prompt --tool=test-tool branch) &&
145         test "$diff" = "branch" &&
147         restore_test_defaults
150 # Test that we don't have to pass --no-prompt to difftool
151 # when $GIT_DIFFTOOL_NO_PROMPT is true
152 test_expect_success PERL 'GIT_DIFFTOOL_NO_PROMPT variable' '
153         GIT_DIFFTOOL_NO_PROMPT=true &&
154         export GIT_DIFFTOOL_NO_PROMPT &&
156         diff=$(git difftool branch) &&
157         test "$diff" = "branch" &&
159         restore_test_defaults
162 # git-difftool supports the difftool.prompt variable.
163 # Test that GIT_DIFFTOOL_PROMPT can override difftool.prompt = false
164 test_expect_success PERL 'GIT_DIFFTOOL_PROMPT variable' '
165         git config difftool.prompt false &&
166         GIT_DIFFTOOL_PROMPT=true &&
167         export GIT_DIFFTOOL_PROMPT &&
169         prompt=$(echo | git difftool branch | tail -1) &&
170         prompt_given "$prompt" &&
172         restore_test_defaults
175 # Test that we don't have to pass --no-prompt when difftool.prompt is false
176 test_expect_success PERL 'difftool.prompt config variable is false' '
177         git config difftool.prompt false &&
179         diff=$(git difftool branch) &&
180         test "$diff" = "branch" &&
182         restore_test_defaults
185 # Test that we don't have to pass --no-prompt when mergetool.prompt is false
186 test_expect_success PERL 'difftool merge.prompt = false' '
187         test_might_fail git config --unset difftool.prompt &&
188         git config mergetool.prompt false &&
190         diff=$(git difftool branch) &&
191         test "$diff" = "branch" &&
193         restore_test_defaults
196 # Test that the -y flag can override difftool.prompt = true
197 test_expect_success PERL 'difftool.prompt can overridden with -y' '
198         git config difftool.prompt true &&
200         diff=$(git difftool -y branch) &&
201         test "$diff" = "branch" &&
203         restore_test_defaults
206 # Test that the --prompt flag can override difftool.prompt = false
207 test_expect_success PERL 'difftool.prompt can overridden with --prompt' '
208         git config difftool.prompt false &&
210         prompt=$(echo | git difftool --prompt branch | tail -1) &&
211         prompt_given "$prompt" &&
213         restore_test_defaults
216 # Test that the last flag passed on the command-line wins
217 test_expect_success PERL 'difftool last flag wins' '
218         diff=$(git difftool --prompt --no-prompt branch) &&
219         test "$diff" = "branch" &&
221         restore_test_defaults &&
223         prompt=$(echo | git difftool --no-prompt --prompt branch | tail -1) &&
224         prompt_given "$prompt" &&
226         restore_test_defaults
229 # git-difftool falls back to git-mergetool config variables
230 # so test that behavior here
231 test_expect_success PERL 'difftool + mergetool config variables' '
232         remove_config_vars &&
233         git config merge.tool test-tool &&
234         git config mergetool.test-tool.cmd "cat \$LOCAL" &&
236         diff=$(git difftool --no-prompt branch) &&
237         test "$diff" = "branch" &&
239         # set merge.tool to something bogus, diff.tool to test-tool
240         git config merge.tool bogus-tool &&
241         git config diff.tool test-tool &&
243         diff=$(git difftool --no-prompt branch) &&
244         test "$diff" = "branch" &&
246         restore_test_defaults
249 test_expect_success PERL 'difftool.<tool>.path' '
250         git config difftool.tkdiff.path echo &&
251         diff=$(git difftool --tool=tkdiff --no-prompt branch) &&
252         git config --unset difftool.tkdiff.path &&
253         lines=$(echo "$diff" | grep file | wc -l) &&
254         test "$lines" -eq 1 &&
256         restore_test_defaults
259 test_expect_success PERL 'difftool --extcmd=cat' '
260         diff=$(git difftool --no-prompt --extcmd=cat branch) &&
261         test "$diff" = branch"$LF"master
264 test_expect_success PERL 'difftool --extcmd cat' '
265         diff=$(git difftool --no-prompt --extcmd cat branch) &&
266         test "$diff" = branch"$LF"master
269 test_expect_success PERL 'difftool -x cat' '
270         diff=$(git difftool --no-prompt -x cat branch) &&
271         test "$diff" = branch"$LF"master
274 test_expect_success PERL 'difftool --extcmd echo arg1' '
275         diff=$(git difftool --no-prompt --extcmd sh\ -c\ \"echo\ \$1\" branch) &&
276         test "$diff" = file
279 test_expect_success PERL 'difftool --extcmd cat arg1' '
280         diff=$(git difftool --no-prompt --extcmd sh\ -c\ \"cat\ \$1\" branch) &&
281         test "$diff" = master
284 test_expect_success PERL 'difftool --extcmd cat arg2' '
285         diff=$(git difftool --no-prompt --extcmd sh\ -c\ \"cat\ \$2\" branch) &&
286         test "$diff" = branch
289 # Create a second file on master and a different version on branch
290 test_expect_success PERL 'setup with 2 files different' '
291         echo m2 >file2 &&
292         git add file2 &&
293         git commit -m "added file2" &&
295         git checkout branch &&
296         echo br2 >file2 &&
297         git add file2 &&
298         git commit -a -m "branch changed file2" &&
299         git checkout master
302 test_expect_success PERL 'say no to the first file' '
303         diff=$( (echo n; echo) | git difftool -x cat branch ) &&
305         echo "$diff" | stdin_contains m2 &&
306         echo "$diff" | stdin_contains br2 &&
307         echo "$diff" | stdin_doesnot_contain master &&
308         echo "$diff" | stdin_doesnot_contain branch
311 test_expect_success PERL 'say no to the second file' '
312         diff=$( (echo; echo n) | git difftool -x cat branch ) &&
314         echo "$diff" | stdin_contains master &&
315         echo "$diff" | stdin_contains branch &&
316         echo "$diff" | stdin_doesnot_contain m2 &&
317         echo "$diff" | stdin_doesnot_contain br2
320 test_done