blob: 1de83ef98fdcbfd63469e66ba4886c2477b983ff [file] [log] [blame]
David Aguilarf92f2032009-04-07 16:30:53 -07001#!/bin/sh
2#
David Aguilarc8a56722010-01-15 19:10:03 -08003# Copyright (c) 2009, 2010 David Aguilar
David Aguilarf92f2032009-04-07 16:30:53 -07004#
5
6test_description='git-difftool
7
8Testing basic diff tool invocation
9'
10
11. ./test-lib.sh
12
Jeff Kinga7668292009-04-24 09:56:14 -040013if ! test_have_prereq PERL; then
14 say 'skipping difftool tests, perl not available'
15 test_done
16fi
17
David Aguilara9e11222010-01-15 14:03:42 -080018LF='
19'
20
David Aguilarf92f2032009-04-07 16:30:53 -070021remove_config_vars()
22{
23 # Unset all config variables used by git-difftool
24 git config --unset diff.tool
David Aguilar4cefa492009-12-22 21:27:14 -080025 git config --unset diff.guitool
David Aguilarf92f2032009-04-07 16:30:53 -070026 git config --unset difftool.test-tool.cmd
David Aguilara9043922009-04-07 01:21:22 -070027 git config --unset difftool.prompt
David Aguilarf92f2032009-04-07 16:30:53 -070028 git config --unset merge.tool
29 git config --unset mergetool.test-tool.cmd
David Aguilara88183f2010-01-22 22:03:36 -080030 git config --unset mergetool.prompt
David Aguilarf92f2032009-04-07 16:30:53 -070031 return 0
32}
33
34restore_test_defaults()
35{
36 # Restores the test defaults used by several tests
37 remove_config_vars
38 unset GIT_DIFF_TOOL
David Aguilara9043922009-04-07 01:21:22 -070039 unset GIT_DIFFTOOL_PROMPT
David Aguilarf92f2032009-04-07 16:30:53 -070040 unset GIT_DIFFTOOL_NO_PROMPT
41 git config diff.tool test-tool &&
42 git config difftool.test-tool.cmd 'cat $LOCAL'
David Aguilar23218bb2009-12-22 21:27:13 -080043 git config difftool.bogus-tool.cmd false
David Aguilarf92f2032009-04-07 16:30:53 -070044}
45
David Aguilara9043922009-04-07 01:21:22 -070046prompt_given()
47{
48 prompt="$1"
49 test "$prompt" = "Hit return to launch 'test-tool': branch"
50}
51
David Aguilarf92f2032009-04-07 16:30:53 -070052# Create a file on master and change it on branch
53test_expect_success 'setup' '
54 echo master >file &&
55 git add file &&
56 git commit -m "added file" &&
57
58 git checkout -b branch master &&
59 echo branch >file &&
60 git commit -a -m "branch changed file" &&
61 git checkout master
62'
63
64# Configure a custom difftool.<tool>.cmd and use it
65test_expect_success 'custom commands' '
66 restore_test_defaults &&
67 git config difftool.test-tool.cmd "cat \$REMOTE" &&
68
69 diff=$(git difftool --no-prompt branch) &&
70 test "$diff" = "master" &&
71
72 restore_test_defaults &&
73 diff=$(git difftool --no-prompt branch) &&
74 test "$diff" = "branch"
75'
76
77# Ensures that git-difftool ignores bogus --tool values
78test_expect_success 'difftool ignores bad --tool values' '
David Aguilar23218bb2009-12-22 21:27:13 -080079 diff=$(git difftool --no-prompt --tool=bad-tool branch)
David Aguilarf92f2032009-04-07 16:30:53 -070080 test "$?" = 1 &&
81 test "$diff" = ""
82'
83
David Aguilar4cefa492009-12-22 21:27:14 -080084test_expect_success 'difftool honors --gui' '
85 git config merge.tool bogus-tool &&
86 git config diff.tool bogus-tool &&
87 git config diff.guitool test-tool &&
88
89 diff=$(git difftool --no-prompt --gui branch) &&
90 test "$diff" = "branch" &&
91
92 restore_test_defaults
93'
94
David Aguilar42accae2010-03-27 14:58:09 -070095test_expect_success 'difftool --gui works without configured diff.guitool' '
96 git config diff.tool test-tool &&
97
98 diff=$(git difftool --no-prompt --gui branch) &&
99 test "$diff" = "branch" &&
100
101 restore_test_defaults
102'
103
David Aguilarf92f2032009-04-07 16:30:53 -0700104# Specify the diff tool using $GIT_DIFF_TOOL
105test_expect_success 'GIT_DIFF_TOOL variable' '
106 git config --unset diff.tool
107 GIT_DIFF_TOOL=test-tool &&
108 export GIT_DIFF_TOOL &&
109
110 diff=$(git difftool --no-prompt branch) &&
111 test "$diff" = "branch" &&
112
113 restore_test_defaults
114'
115
116# Test the $GIT_*_TOOL variables and ensure
117# that $GIT_DIFF_TOOL always wins unless --tool is specified
118test_expect_success 'GIT_DIFF_TOOL overrides' '
119 git config diff.tool bogus-tool &&
120 git config merge.tool bogus-tool &&
121
David Aguilarf92f2032009-04-07 16:30:53 -0700122 GIT_DIFF_TOOL=test-tool &&
David Aguilarf92f2032009-04-07 16:30:53 -0700123 export GIT_DIFF_TOOL &&
124
125 diff=$(git difftool --no-prompt branch) &&
126 test "$diff" = "branch" &&
127
128 GIT_DIFF_TOOL=bogus-tool &&
129 export GIT_DIFF_TOOL &&
130
131 diff=$(git difftool --no-prompt --tool=test-tool branch) &&
132 test "$diff" = "branch" &&
133
134 restore_test_defaults
135'
136
137# Test that we don't have to pass --no-prompt to difftool
138# when $GIT_DIFFTOOL_NO_PROMPT is true
139test_expect_success 'GIT_DIFFTOOL_NO_PROMPT variable' '
140 GIT_DIFFTOOL_NO_PROMPT=true &&
141 export GIT_DIFFTOOL_NO_PROMPT &&
142
143 diff=$(git difftool branch) &&
144 test "$diff" = "branch" &&
145
146 restore_test_defaults
147'
148
David Aguilara9043922009-04-07 01:21:22 -0700149# git-difftool supports the difftool.prompt variable.
150# Test that GIT_DIFFTOOL_PROMPT can override difftool.prompt = false
151test_expect_success 'GIT_DIFFTOOL_PROMPT variable' '
152 git config difftool.prompt false &&
153 GIT_DIFFTOOL_PROMPT=true &&
154 export GIT_DIFFTOOL_PROMPT &&
155
Markus Heidelberg3319df62009-10-25 01:39:19 +0200156 prompt=$(echo | git difftool branch | tail -1) &&
David Aguilara9043922009-04-07 01:21:22 -0700157 prompt_given "$prompt" &&
158
159 restore_test_defaults
160'
161
162# Test that we don't have to pass --no-prompt when difftool.prompt is false
163test_expect_success 'difftool.prompt config variable is false' '
164 git config difftool.prompt false &&
165
166 diff=$(git difftool branch) &&
167 test "$diff" = "branch" &&
168
169 restore_test_defaults
170'
171
David Aguilara88183f2010-01-22 22:03:36 -0800172# Test that we don't have to pass --no-prompt when mergetool.prompt is false
173test_expect_success 'difftool merge.prompt = false' '
174 git config --unset difftool.prompt
175 git config mergetool.prompt false &&
176
177 diff=$(git difftool branch) &&
178 test "$diff" = "branch" &&
179
180 restore_test_defaults
181'
182
David Aguilara9043922009-04-07 01:21:22 -0700183# Test that the -y flag can override difftool.prompt = true
184test_expect_success 'difftool.prompt can overridden with -y' '
185 git config difftool.prompt true &&
186
187 diff=$(git difftool -y branch) &&
188 test "$diff" = "branch" &&
189
190 restore_test_defaults
191'
192
193# Test that the --prompt flag can override difftool.prompt = false
194test_expect_success 'difftool.prompt can overridden with --prompt' '
195 git config difftool.prompt false &&
196
197 prompt=$(echo | git difftool --prompt branch | tail -1) &&
198 prompt_given "$prompt" &&
199
200 restore_test_defaults
201'
202
203# Test that the last flag passed on the command-line wins
204test_expect_success 'difftool last flag wins' '
205 diff=$(git difftool --prompt --no-prompt branch) &&
206 test "$diff" = "branch" &&
207
208 restore_test_defaults &&
209
210 prompt=$(echo | git difftool --no-prompt --prompt branch | tail -1) &&
211 prompt_given "$prompt" &&
212
213 restore_test_defaults
214'
215
David Aguilarf92f2032009-04-07 16:30:53 -0700216# git-difftool falls back to git-mergetool config variables
217# so test that behavior here
218test_expect_success 'difftool + mergetool config variables' '
219 remove_config_vars
220 git config merge.tool test-tool &&
221 git config mergetool.test-tool.cmd "cat \$LOCAL" &&
222
223 diff=$(git difftool --no-prompt branch) &&
224 test "$diff" = "branch" &&
225
226 # set merge.tool to something bogus, diff.tool to test-tool
227 git config merge.tool bogus-tool &&
228 git config diff.tool test-tool &&
229
230 diff=$(git difftool --no-prompt branch) &&
231 test "$diff" = "branch" &&
232
233 restore_test_defaults
234'
235
236test_expect_success 'difftool.<tool>.path' '
237 git config difftool.tkdiff.path echo &&
238 diff=$(git difftool --tool=tkdiff --no-prompt branch) &&
239 git config --unset difftool.tkdiff.path &&
240 lines=$(echo "$diff" | grep file | wc -l) &&
David Aguilar1c6f5b52010-01-09 20:02:42 -0800241 test "$lines" -eq 1 &&
242
243 restore_test_defaults
244'
245
David Aguilara9e11222010-01-15 14:03:42 -0800246test_expect_success 'difftool --extcmd=cat' '
David Aguilar1c6f5b52010-01-09 20:02:42 -0800247 diff=$(git difftool --no-prompt --extcmd=cat branch) &&
David Aguilara9e11222010-01-15 14:03:42 -0800248 test "$diff" = branch"$LF"master
David Aguilarf47f1e22010-01-15 14:03:43 -0800249'
David Aguilar1c6f5b52010-01-09 20:02:42 -0800250
David Aguilarf47f1e22010-01-15 14:03:43 -0800251test_expect_success 'difftool --extcmd cat' '
252 diff=$(git difftool --no-prompt --extcmd cat branch) &&
253 test "$diff" = branch"$LF"master
254'
David Aguilar1c6f5b52010-01-09 20:02:42 -0800255
David Aguilarf47f1e22010-01-15 14:03:43 -0800256test_expect_success 'difftool -x cat' '
257 diff=$(git difftool --no-prompt -x cat branch) &&
258 test "$diff" = branch"$LF"master
David Aguilar9f3d54d2010-01-15 14:03:44 -0800259'
David Aguilar1c6f5b52010-01-09 20:02:42 -0800260
David Aguilar9f3d54d2010-01-15 14:03:44 -0800261test_expect_success 'difftool --extcmd echo arg1' '
262 diff=$(git difftool --no-prompt --extcmd sh\ -c\ \"echo\ \$1\" branch)
263 test "$diff" = file
264'
David Aguilar1c6f5b52010-01-09 20:02:42 -0800265
David Aguilar9f3d54d2010-01-15 14:03:44 -0800266test_expect_success 'difftool --extcmd cat arg1' '
267 diff=$(git difftool --no-prompt --extcmd sh\ -c\ \"cat\ \$1\" branch)
268 test "$diff" = master
269'
270
271test_expect_success 'difftool --extcmd cat arg2' '
272 diff=$(git difftool --no-prompt --extcmd sh\ -c\ \"cat\ \$2\" branch)
273 test "$diff" = branch
David Aguilarf92f2032009-04-07 16:30:53 -0700274'
275
276test_done