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