init: plug tiny one-time memory leak
[git/mjg.git] / t / t7800-difftool.sh
blob58dc6f6452e69df2f91799b0ae23c620783311da
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 LF='
16 remove_config_vars()
18 # Unset all config variables used by git-difftool
19 git config --unset diff.tool
20 git config --unset diff.guitool
21 git config --unset difftool.test-tool.cmd
22 git config --unset difftool.prompt
23 git config --unset merge.tool
24 git config --unset mergetool.test-tool.cmd
25 git config --unset mergetool.prompt
26 return 0
29 restore_test_defaults()
31 # Restores the test defaults used by several tests
32 remove_config_vars
33 unset GIT_DIFF_TOOL
34 unset GIT_DIFFTOOL_PROMPT
35 unset GIT_DIFFTOOL_NO_PROMPT
36 git config diff.tool test-tool &&
37 git config difftool.test-tool.cmd 'cat $LOCAL'
38 git config difftool.bogus-tool.cmd false
41 prompt_given()
43 prompt="$1"
44 test "$prompt" = "Hit return to launch 'test-tool': branch"
47 # Create a file on master and change it on branch
48 test_expect_success PERL 'setup' '
49 echo master >file &&
50 git add file &&
51 git commit -m "added file" &&
53 git checkout -b branch master &&
54 echo branch >file &&
55 git commit -a -m "branch changed file" &&
56 git checkout master
59 # Configure a custom difftool.<tool>.cmd and use it
60 test_expect_success PERL 'custom commands' '
61 restore_test_defaults &&
62 git config difftool.test-tool.cmd "cat \$REMOTE" &&
64 diff=$(git difftool --no-prompt branch) &&
65 test "$diff" = "master" &&
67 restore_test_defaults &&
68 diff=$(git difftool --no-prompt branch) &&
69 test "$diff" = "branch"
72 # Ensures that git-difftool ignores bogus --tool values
73 test_expect_success PERL 'difftool ignores bad --tool values' '
74 diff=$(git difftool --no-prompt --tool=bad-tool branch)
75 test "$?" = 1 &&
76 test "$diff" = ""
79 test_expect_success PERL 'difftool honors --gui' '
80 git config merge.tool bogus-tool &&
81 git config diff.tool bogus-tool &&
82 git config diff.guitool test-tool &&
84 diff=$(git difftool --no-prompt --gui branch) &&
85 test "$diff" = "branch" &&
87 restore_test_defaults
90 test_expect_success PERL 'difftool --gui works without configured diff.guitool' '
91 git config diff.tool test-tool &&
93 diff=$(git difftool --no-prompt --gui branch) &&
94 test "$diff" = "branch" &&
96 restore_test_defaults
99 # Specify the diff tool using $GIT_DIFF_TOOL
100 test_expect_success PERL 'GIT_DIFF_TOOL variable' '
101 git config --unset diff.tool
102 GIT_DIFF_TOOL=test-tool &&
103 export GIT_DIFF_TOOL &&
105 diff=$(git difftool --no-prompt branch) &&
106 test "$diff" = "branch" &&
108 restore_test_defaults
111 # Test the $GIT_*_TOOL variables and ensure
112 # that $GIT_DIFF_TOOL always wins unless --tool is specified
113 test_expect_success PERL 'GIT_DIFF_TOOL overrides' '
114 git config diff.tool bogus-tool &&
115 git config merge.tool bogus-tool &&
117 GIT_DIFF_TOOL=test-tool &&
118 export GIT_DIFF_TOOL &&
120 diff=$(git difftool --no-prompt branch) &&
121 test "$diff" = "branch" &&
123 GIT_DIFF_TOOL=bogus-tool &&
124 export GIT_DIFF_TOOL &&
126 diff=$(git difftool --no-prompt --tool=test-tool branch) &&
127 test "$diff" = "branch" &&
129 restore_test_defaults
132 # Test that we don't have to pass --no-prompt to difftool
133 # when $GIT_DIFFTOOL_NO_PROMPT is true
134 test_expect_success PERL 'GIT_DIFFTOOL_NO_PROMPT variable' '
135 GIT_DIFFTOOL_NO_PROMPT=true &&
136 export GIT_DIFFTOOL_NO_PROMPT &&
138 diff=$(git difftool branch) &&
139 test "$diff" = "branch" &&
141 restore_test_defaults
144 # git-difftool supports the difftool.prompt variable.
145 # Test that GIT_DIFFTOOL_PROMPT can override difftool.prompt = false
146 test_expect_success PERL 'GIT_DIFFTOOL_PROMPT variable' '
147 git config difftool.prompt false &&
148 GIT_DIFFTOOL_PROMPT=true &&
149 export GIT_DIFFTOOL_PROMPT &&
151 prompt=$(echo | git difftool branch | tail -1) &&
152 prompt_given "$prompt" &&
154 restore_test_defaults
157 # Test that we don't have to pass --no-prompt when difftool.prompt is false
158 test_expect_success PERL 'difftool.prompt config variable is false' '
159 git config difftool.prompt false &&
161 diff=$(git difftool branch) &&
162 test "$diff" = "branch" &&
164 restore_test_defaults
167 # Test that we don't have to pass --no-prompt when mergetool.prompt is false
168 test_expect_success PERL 'difftool merge.prompt = false' '
169 git config --unset difftool.prompt
170 git config mergetool.prompt false &&
172 diff=$(git difftool branch) &&
173 test "$diff" = "branch" &&
175 restore_test_defaults
178 # Test that the -y flag can override difftool.prompt = true
179 test_expect_success PERL 'difftool.prompt can overridden with -y' '
180 git config difftool.prompt true &&
182 diff=$(git difftool -y branch) &&
183 test "$diff" = "branch" &&
185 restore_test_defaults
188 # Test that the --prompt flag can override difftool.prompt = false
189 test_expect_success PERL 'difftool.prompt can overridden with --prompt' '
190 git config difftool.prompt false &&
192 prompt=$(echo | git difftool --prompt branch | tail -1) &&
193 prompt_given "$prompt" &&
195 restore_test_defaults
198 # Test that the last flag passed on the command-line wins
199 test_expect_success PERL 'difftool last flag wins' '
200 diff=$(git difftool --prompt --no-prompt branch) &&
201 test "$diff" = "branch" &&
203 restore_test_defaults &&
205 prompt=$(echo | git difftool --no-prompt --prompt branch | tail -1) &&
206 prompt_given "$prompt" &&
208 restore_test_defaults
211 # git-difftool falls back to git-mergetool config variables
212 # so test that behavior here
213 test_expect_success PERL 'difftool + mergetool config variables' '
214 remove_config_vars
215 git config merge.tool test-tool &&
216 git config mergetool.test-tool.cmd "cat \$LOCAL" &&
218 diff=$(git difftool --no-prompt branch) &&
219 test "$diff" = "branch" &&
221 # set merge.tool to something bogus, diff.tool to test-tool
222 git config merge.tool bogus-tool &&
223 git config diff.tool test-tool &&
225 diff=$(git difftool --no-prompt branch) &&
226 test "$diff" = "branch" &&
228 restore_test_defaults
231 test_expect_success PERL 'difftool.<tool>.path' '
232 git config difftool.tkdiff.path echo &&
233 diff=$(git difftool --tool=tkdiff --no-prompt branch) &&
234 git config --unset difftool.tkdiff.path &&
235 lines=$(echo "$diff" | grep file | wc -l) &&
236 test "$lines" -eq 1 &&
238 restore_test_defaults
241 test_expect_success PERL 'difftool --extcmd=cat' '
242 diff=$(git difftool --no-prompt --extcmd=cat branch) &&
243 test "$diff" = branch"$LF"master
246 test_expect_success PERL 'difftool --extcmd cat' '
247 diff=$(git difftool --no-prompt --extcmd cat branch) &&
248 test "$diff" = branch"$LF"master
251 test_expect_success PERL 'difftool -x cat' '
252 diff=$(git difftool --no-prompt -x cat branch) &&
253 test "$diff" = branch"$LF"master
256 test_expect_success PERL 'difftool --extcmd echo arg1' '
257 diff=$(git difftool --no-prompt --extcmd sh\ -c\ \"echo\ \$1\" branch)
258 test "$diff" = file
261 test_expect_success PERL 'difftool --extcmd cat arg1' '
262 diff=$(git difftool --no-prompt --extcmd sh\ -c\ \"cat\ \$1\" branch)
263 test "$diff" = master
266 test_expect_success PERL 'difftool --extcmd cat arg2' '
267 diff=$(git difftool --no-prompt --extcmd sh\ -c\ \"cat\ \$2\" branch)
268 test "$diff" = branch
271 test_done