1#!/bin/sh
2#
3# Copyright (c) 2009 David Aguilar
4#
5
6test_description='git-difftool
7
8Testing basic diff tool invocation
9'
10
11. ./test-lib.sh
12
13if ! test_have_prereq PERL; then
14 say 'skipping difftool tests, perl not available'
15 test_done
16fi
17
18remove_config_vars()
19{
20 # Unset all config variables used by git-difftool
21 git config --unset diff.tool
22 git config --unset diff.guitool
23 git config --unset difftool.test-tool.cmd
24 git config --unset difftool.prompt
25 git config --unset merge.tool
26 git config --unset mergetool.test-tool.cmd
27 return 0
28}
29
30restore_test_defaults()
31{
32 # Restores the test defaults used by several tests
33 remove_config_vars
34 unset GIT_DIFF_TOOL
35 unset GIT_MERGE_TOOL
36 unset GIT_DIFFTOOL_PROMPT
37 unset GIT_DIFFTOOL_NO_PROMPT
38 git config diff.tool test-tool &&
39 git config difftool.test-tool.cmd 'cat $LOCAL'
40 git config difftool.bogus-tool.cmd false
41}
42
43prompt_given()
44{
45 prompt="$1"
46 test "$prompt" = "Hit return to launch 'test-tool': branch"
47}
48
49# Create a file on master and change it on branch
50test_expect_success 'setup' '
51 echo master >file &&
52 git add file &&
53 git commit -m "added file" &&
54
55 git checkout -b branch master &&
56 echo branch >file &&
57 git commit -a -m "branch changed file" &&
58 git checkout master
59'
60
61# Configure a custom difftool.<tool>.cmd and use it
62test_expect_success 'custom commands' '
63 restore_test_defaults &&
64 git config difftool.test-tool.cmd "cat \$REMOTE" &&
65
66 diff=$(git difftool --no-prompt branch) &&
67 test "$diff" = "master" &&
68
69 restore_test_defaults &&
70 diff=$(git difftool --no-prompt branch) &&
71 test "$diff" = "branch"
72'
73
74# Ensures that git-difftool ignores bogus --tool values
75test_expect_success 'difftool ignores bad --tool values' '
76 diff=$(git difftool --no-prompt --tool=bad-tool branch)
77 test "$?" = 1 &&
78 test "$diff" = ""
79'
80
81test_expect_success 'difftool honors --gui' '
82 git config merge.tool bogus-tool &&
83 git config diff.tool bogus-tool &&
84 git config diff.guitool test-tool &&
85
86 diff=$(git difftool --no-prompt --gui branch) &&
87 test "$diff" = "branch" &&
88
89 restore_test_defaults
90'
91
92# Specify the diff tool using $GIT_DIFF_TOOL
93test_expect_success 'GIT_DIFF_TOOL variable' '
94 git config --unset diff.tool
95 GIT_DIFF_TOOL=test-tool &&
96 export GIT_DIFF_TOOL &&
97
98 diff=$(git difftool --no-prompt branch) &&
99 test "$diff" = "branch" &&
100
101 restore_test_defaults
102'
103
104# Test the $GIT_*_TOOL variables and ensure
105# that $GIT_DIFF_TOOL always wins unless --tool is specified
106test_expect_success 'GIT_DIFF_TOOL overrides' '
107 git config diff.tool bogus-tool &&
108 git config merge.tool bogus-tool &&
109
110 GIT_MERGE_TOOL=test-tool &&
111 export GIT_MERGE_TOOL &&
112 diff=$(git difftool --no-prompt branch) &&
113 test "$diff" = "branch" &&
114 unset GIT_MERGE_TOOL &&
115
116 GIT_MERGE_TOOL=bogus-tool &&
117 GIT_DIFF_TOOL=test-tool &&
118 export GIT_MERGE_TOOL &&
119 export GIT_DIFF_TOOL &&
120
121 diff=$(git difftool --no-prompt branch) &&
122 test "$diff" = "branch" &&
123
124 GIT_DIFF_TOOL=bogus-tool &&
125 export GIT_DIFF_TOOL &&
126
127 diff=$(git difftool --no-prompt --tool=test-tool branch) &&
128 test "$diff" = "branch" &&
129
130 restore_test_defaults
131'
132
133# Test that we don't have to pass --no-prompt to difftool
134# when $GIT_DIFFTOOL_NO_PROMPT is true
135test_expect_success 'GIT_DIFFTOOL_NO_PROMPT variable' '
136 GIT_DIFFTOOL_NO_PROMPT=true &&
137 export GIT_DIFFTOOL_NO_PROMPT &&
138
139 diff=$(git difftool branch) &&
140 test "$diff" = "branch" &&
141
142 restore_test_defaults
143'
144
145# git-difftool supports the difftool.prompt variable.
146# Test that GIT_DIFFTOOL_PROMPT can override difftool.prompt = false
147test_expect_success 'GIT_DIFFTOOL_PROMPT variable' '
148 git config difftool.prompt false &&
149 GIT_DIFFTOOL_PROMPT=true &&
150 export GIT_DIFFTOOL_PROMPT &&
151
152 prompt=$(echo | git difftool branch | tail -1) &&
153 prompt_given "$prompt" &&
154
155 restore_test_defaults
156'
157
158# Test that we don't have to pass --no-prompt when difftool.prompt is false
159test_expect_success 'difftool.prompt config variable is false' '
160 git config difftool.prompt false &&
161
162 diff=$(git difftool branch) &&
163 test "$diff" = "branch" &&
164
165 restore_test_defaults
166'
167
168# Test that the -y flag can override difftool.prompt = true
169test_expect_success 'difftool.prompt can overridden with -y' '
170 git config difftool.prompt true &&
171
172 diff=$(git difftool -y branch) &&
173 test "$diff" = "branch" &&
174
175 restore_test_defaults
176'
177
178# Test that the --prompt flag can override difftool.prompt = false
179test_expect_success 'difftool.prompt can overridden with --prompt' '
180 git config difftool.prompt false &&
181
182 prompt=$(echo | git difftool --prompt branch | tail -1) &&
183 prompt_given "$prompt" &&
184
185 restore_test_defaults
186'
187
188# Test that the last flag passed on the command-line wins
189test_expect_success 'difftool last flag wins' '
190 diff=$(git difftool --prompt --no-prompt branch) &&
191 test "$diff" = "branch" &&
192
193 restore_test_defaults &&
194
195 prompt=$(echo | git difftool --no-prompt --prompt branch | tail -1) &&
196 prompt_given "$prompt" &&
197
198 restore_test_defaults
199'
200
201# git-difftool falls back to git-mergetool config variables
202# so test that behavior here
203test_expect_success 'difftool + mergetool config variables' '
204 remove_config_vars
205 git config merge.tool test-tool &&
206 git config mergetool.test-tool.cmd "cat \$LOCAL" &&
207
208 diff=$(git difftool --no-prompt branch) &&
209 test "$diff" = "branch" &&
210
211 # set merge.tool to something bogus, diff.tool to test-tool
212 git config merge.tool bogus-tool &&
213 git config diff.tool test-tool &&
214
215 diff=$(git difftool --no-prompt branch) &&
216 test "$diff" = "branch" &&
217
218 restore_test_defaults
219'
220
221test_expect_success 'difftool.<tool>.path' '
222 git config difftool.tkdiff.path echo &&
223 diff=$(git difftool --tool=tkdiff --no-prompt branch) &&
224 git config --unset difftool.tkdiff.path &&
225 lines=$(echo "$diff" | grep file | wc -l) &&
226 test "$lines" -eq 1
227'
228
229test_done