t/t1304: use 'test -r' to test readability rather than looking at mode bits
[git/jnareb-git.git] / t / t7800-difftool.sh
blob19c72f55bf60c746f72e640e24043b2d83e00fe3
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 if ! test_have_prereq PERL; then
14 say 'skipping difftool tests, perl not available'
15 test_done
18 LF='
21 remove_config_vars()
23 # Unset all config variables used by git-difftool
24 git config --unset diff.tool
25 git config --unset diff.guitool
26 git config --unset difftool.test-tool.cmd
27 git config --unset difftool.prompt
28 git config --unset merge.tool
29 git config --unset mergetool.test-tool.cmd
30 git config --unset mergetool.prompt
31 return 0
34 restore_test_defaults()
36 # Restores the test defaults used by several tests
37 remove_config_vars
38 unset GIT_DIFF_TOOL
39 unset GIT_DIFFTOOL_PROMPT
40 unset GIT_DIFFTOOL_NO_PROMPT
41 git config diff.tool test-tool &&
42 git config difftool.test-tool.cmd 'cat $LOCAL'
43 git config difftool.bogus-tool.cmd false
46 prompt_given()
48 prompt="$1"
49 test "$prompt" = "Hit return to launch 'test-tool': branch"
52 # Create a file on master and change it on branch
53 test_expect_success 'setup' '
54 echo master >file &&
55 git add file &&
56 git commit -m "added file" &&
58 git checkout -b branch master &&
59 echo branch >file &&
60 git commit -a -m "branch changed file" &&
61 git checkout master
64 # Configure a custom difftool.<tool>.cmd and use it
65 test_expect_success 'custom commands' '
66 restore_test_defaults &&
67 git config difftool.test-tool.cmd "cat \$REMOTE" &&
69 diff=$(git difftool --no-prompt branch) &&
70 test "$diff" = "master" &&
72 restore_test_defaults &&
73 diff=$(git difftool --no-prompt branch) &&
74 test "$diff" = "branch"
77 # Ensures that git-difftool ignores bogus --tool values
78 test_expect_success 'difftool ignores bad --tool values' '
79 diff=$(git difftool --no-prompt --tool=bad-tool branch)
80 test "$?" = 1 &&
81 test "$diff" = ""
84 test_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 &&
89 diff=$(git difftool --no-prompt --gui branch) &&
90 test "$diff" = "branch" &&
92 restore_test_defaults
95 # Specify the diff tool using $GIT_DIFF_TOOL
96 test_expect_success 'GIT_DIFF_TOOL variable' '
97 git config --unset diff.tool
98 GIT_DIFF_TOOL=test-tool &&
99 export GIT_DIFF_TOOL &&
101 diff=$(git difftool --no-prompt branch) &&
102 test "$diff" = "branch" &&
104 restore_test_defaults
107 # Test the $GIT_*_TOOL variables and ensure
108 # that $GIT_DIFF_TOOL always wins unless --tool is specified
109 test_expect_success 'GIT_DIFF_TOOL overrides' '
110 git config diff.tool bogus-tool &&
111 git config merge.tool bogus-tool &&
113 GIT_DIFF_TOOL=test-tool &&
114 export GIT_DIFF_TOOL &&
116 diff=$(git difftool --no-prompt branch) &&
117 test "$diff" = "branch" &&
119 GIT_DIFF_TOOL=bogus-tool &&
120 export GIT_DIFF_TOOL &&
122 diff=$(git difftool --no-prompt --tool=test-tool branch) &&
123 test "$diff" = "branch" &&
125 restore_test_defaults
128 # Test that we don't have to pass --no-prompt to difftool
129 # when $GIT_DIFFTOOL_NO_PROMPT is true
130 test_expect_success 'GIT_DIFFTOOL_NO_PROMPT variable' '
131 GIT_DIFFTOOL_NO_PROMPT=true &&
132 export GIT_DIFFTOOL_NO_PROMPT &&
134 diff=$(git difftool branch) &&
135 test "$diff" = "branch" &&
137 restore_test_defaults
140 # git-difftool supports the difftool.prompt variable.
141 # Test that GIT_DIFFTOOL_PROMPT can override difftool.prompt = false
142 test_expect_success 'GIT_DIFFTOOL_PROMPT variable' '
143 git config difftool.prompt false &&
144 GIT_DIFFTOOL_PROMPT=true &&
145 export GIT_DIFFTOOL_PROMPT &&
147 prompt=$(echo | git difftool branch | tail -1) &&
148 prompt_given "$prompt" &&
150 restore_test_defaults
153 # Test that we don't have to pass --no-prompt when difftool.prompt is false
154 test_expect_success 'difftool.prompt config variable is false' '
155 git config difftool.prompt false &&
157 diff=$(git difftool branch) &&
158 test "$diff" = "branch" &&
160 restore_test_defaults
163 # Test that we don't have to pass --no-prompt when mergetool.prompt is false
164 test_expect_success 'difftool merge.prompt = false' '
165 git config --unset difftool.prompt
166 git config mergetool.prompt false &&
168 diff=$(git difftool branch) &&
169 test "$diff" = "branch" &&
171 restore_test_defaults
174 # Test that the -y flag can override difftool.prompt = true
175 test_expect_success 'difftool.prompt can overridden with -y' '
176 git config difftool.prompt true &&
178 diff=$(git difftool -y branch) &&
179 test "$diff" = "branch" &&
181 restore_test_defaults
184 # Test that the --prompt flag can override difftool.prompt = false
185 test_expect_success 'difftool.prompt can overridden with --prompt' '
186 git config difftool.prompt false &&
188 prompt=$(echo | git difftool --prompt branch | tail -1) &&
189 prompt_given "$prompt" &&
191 restore_test_defaults
194 # Test that the last flag passed on the command-line wins
195 test_expect_success 'difftool last flag wins' '
196 diff=$(git difftool --prompt --no-prompt branch) &&
197 test "$diff" = "branch" &&
199 restore_test_defaults &&
201 prompt=$(echo | git difftool --no-prompt --prompt branch | tail -1) &&
202 prompt_given "$prompt" &&
204 restore_test_defaults
207 # git-difftool falls back to git-mergetool config variables
208 # so test that behavior here
209 test_expect_success 'difftool + mergetool config variables' '
210 remove_config_vars
211 git config merge.tool test-tool &&
212 git config mergetool.test-tool.cmd "cat \$LOCAL" &&
214 diff=$(git difftool --no-prompt branch) &&
215 test "$diff" = "branch" &&
217 # set merge.tool to something bogus, diff.tool to test-tool
218 git config merge.tool bogus-tool &&
219 git config diff.tool test-tool &&
221 diff=$(git difftool --no-prompt branch) &&
222 test "$diff" = "branch" &&
224 restore_test_defaults
227 test_expect_success 'difftool.<tool>.path' '
228 git config difftool.tkdiff.path echo &&
229 diff=$(git difftool --tool=tkdiff --no-prompt branch) &&
230 git config --unset difftool.tkdiff.path &&
231 lines=$(echo "$diff" | grep file | wc -l) &&
232 test "$lines" -eq 1 &&
234 restore_test_defaults
237 test_expect_success 'difftool --extcmd=cat' '
238 diff=$(git difftool --no-prompt --extcmd=cat branch) &&
239 test "$diff" = branch"$LF"master
242 test_expect_success 'difftool --extcmd cat' '
243 diff=$(git difftool --no-prompt --extcmd cat branch) &&
244 test "$diff" = branch"$LF"master
247 test_expect_success 'difftool -x cat' '
248 diff=$(git difftool --no-prompt -x cat branch) &&
249 test "$diff" = branch"$LF"master
252 test_expect_success 'difftool --extcmd echo arg1' '
253 diff=$(git difftool --no-prompt --extcmd sh\ -c\ \"echo\ \$1\" branch)
254 test "$diff" = file
257 test_expect_success 'difftool --extcmd cat arg1' '
258 diff=$(git difftool --no-prompt --extcmd sh\ -c\ \"cat\ \$1\" branch)
259 test "$diff" = master
262 test_expect_success 'difftool --extcmd cat arg2' '
263 diff=$(git difftool --no-prompt --extcmd sh\ -c\ \"cat\ \$2\" branch)
264 test "$diff" = branch
267 test_done