Code

Merge branch 'da/difftool'
[git.git] / t / t7800-difftool.sh
1 #!/bin/sh
2 #
3 # Copyright (c) 2009, 2010 David Aguilar
4 #
6 test_description='git-difftool
8 Testing basic diff tool invocation
9 '
11 . ./test-lib.sh
13 if ! test_have_prereq PERL; then
14         say 'skipping difftool tests, perl not available'
15         test_done
16 fi
18 LF='
19 '
21 remove_config_vars()
22 {
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         return 0
31 }
33 restore_test_defaults()
34 {
35         # Restores the test defaults used by several tests
36         remove_config_vars
37         unset GIT_DIFF_TOOL
38         unset GIT_DIFFTOOL_PROMPT
39         unset GIT_DIFFTOOL_NO_PROMPT
40         git config diff.tool test-tool &&
41         git config difftool.test-tool.cmd 'cat $LOCAL'
42         git config difftool.bogus-tool.cmd false
43 }
45 prompt_given()
46 {
47         prompt="$1"
48         test "$prompt" = "Hit return to launch 'test-tool': branch"
49 }
51 # Create a file on master and change it on branch
52 test_expect_success 'setup' '
53         echo master >file &&
54         git add file &&
55         git commit -m "added file" &&
57         git checkout -b branch master &&
58         echo branch >file &&
59         git commit -a -m "branch changed file" &&
60         git checkout master
61 '
63 # Configure a custom difftool.<tool>.cmd and use it
64 test_expect_success 'custom commands' '
65         restore_test_defaults &&
66         git config difftool.test-tool.cmd "cat \$REMOTE" &&
68         diff=$(git difftool --no-prompt branch) &&
69         test "$diff" = "master" &&
71         restore_test_defaults &&
72         diff=$(git difftool --no-prompt branch) &&
73         test "$diff" = "branch"
74 '
76 # Ensures that git-difftool ignores bogus --tool values
77 test_expect_success 'difftool ignores bad --tool values' '
78         diff=$(git difftool --no-prompt --tool=bad-tool branch)
79         test "$?" = 1 &&
80         test "$diff" = ""
81 '
83 test_expect_success 'difftool honors --gui' '
84         git config merge.tool bogus-tool &&
85         git config diff.tool bogus-tool &&
86         git config diff.guitool test-tool &&
88         diff=$(git difftool --no-prompt --gui branch) &&
89         test "$diff" = "branch" &&
91         restore_test_defaults
92 '
94 # Specify the diff tool using $GIT_DIFF_TOOL
95 test_expect_success 'GIT_DIFF_TOOL variable' '
96         git config --unset diff.tool
97         GIT_DIFF_TOOL=test-tool &&
98         export GIT_DIFF_TOOL &&
100         diff=$(git difftool --no-prompt branch) &&
101         test "$diff" = "branch" &&
103         restore_test_defaults
106 # Test the $GIT_*_TOOL variables and ensure
107 # that $GIT_DIFF_TOOL always wins unless --tool is specified
108 test_expect_success 'GIT_DIFF_TOOL overrides' '
109         git config diff.tool bogus-tool &&
110         git config merge.tool bogus-tool &&
112         GIT_DIFF_TOOL=test-tool &&
113         export GIT_DIFF_TOOL &&
115         diff=$(git difftool --no-prompt branch) &&
116         test "$diff" = "branch" &&
118         GIT_DIFF_TOOL=bogus-tool &&
119         export GIT_DIFF_TOOL &&
121         diff=$(git difftool --no-prompt --tool=test-tool branch) &&
122         test "$diff" = "branch" &&
124         restore_test_defaults
127 # Test that we don't have to pass --no-prompt to difftool
128 # when $GIT_DIFFTOOL_NO_PROMPT is true
129 test_expect_success 'GIT_DIFFTOOL_NO_PROMPT variable' '
130         GIT_DIFFTOOL_NO_PROMPT=true &&
131         export GIT_DIFFTOOL_NO_PROMPT &&
133         diff=$(git difftool branch) &&
134         test "$diff" = "branch" &&
136         restore_test_defaults
139 # git-difftool supports the difftool.prompt variable.
140 # Test that GIT_DIFFTOOL_PROMPT can override difftool.prompt = false
141 test_expect_success 'GIT_DIFFTOOL_PROMPT variable' '
142         git config difftool.prompt false &&
143         GIT_DIFFTOOL_PROMPT=true &&
144         export GIT_DIFFTOOL_PROMPT &&
146         prompt=$(echo | git difftool branch | tail -1) &&
147         prompt_given "$prompt" &&
149         restore_test_defaults
152 # Test that we don't have to pass --no-prompt when difftool.prompt is false
153 test_expect_success 'difftool.prompt config variable is false' '
154         git config difftool.prompt false &&
156         diff=$(git difftool branch) &&
157         test "$diff" = "branch" &&
159         restore_test_defaults
162 # Test that the -y flag can override difftool.prompt = true
163 test_expect_success 'difftool.prompt can overridden with -y' '
164         git config difftool.prompt true &&
166         diff=$(git difftool -y branch) &&
167         test "$diff" = "branch" &&
169         restore_test_defaults
172 # Test that the --prompt flag can override difftool.prompt = false
173 test_expect_success 'difftool.prompt can overridden with --prompt' '
174         git config difftool.prompt false &&
176         prompt=$(echo | git difftool --prompt branch | tail -1) &&
177         prompt_given "$prompt" &&
179         restore_test_defaults
182 # Test that the last flag passed on the command-line wins
183 test_expect_success 'difftool last flag wins' '
184         diff=$(git difftool --prompt --no-prompt branch) &&
185         test "$diff" = "branch" &&
187         restore_test_defaults &&
189         prompt=$(echo | git difftool --no-prompt --prompt branch | tail -1) &&
190         prompt_given "$prompt" &&
192         restore_test_defaults
195 # git-difftool falls back to git-mergetool config variables
196 # so test that behavior here
197 test_expect_success 'difftool + mergetool config variables' '
198         remove_config_vars
199         git config merge.tool test-tool &&
200         git config mergetool.test-tool.cmd "cat \$LOCAL" &&
202         diff=$(git difftool --no-prompt branch) &&
203         test "$diff" = "branch" &&
205         # set merge.tool to something bogus, diff.tool to test-tool
206         git config merge.tool bogus-tool &&
207         git config diff.tool test-tool &&
209         diff=$(git difftool --no-prompt branch) &&
210         test "$diff" = "branch" &&
212         restore_test_defaults
215 test_expect_success 'difftool.<tool>.path' '
216         git config difftool.tkdiff.path echo &&
217         diff=$(git difftool --tool=tkdiff --no-prompt branch) &&
218         git config --unset difftool.tkdiff.path &&
219         lines=$(echo "$diff" | grep file | wc -l) &&
220         test "$lines" -eq 1 &&
222         restore_test_defaults
225 test_expect_success 'difftool --extcmd=cat' '
226         diff=$(git difftool --no-prompt --extcmd=cat branch) &&
227         test "$diff" = branch"$LF"master
230 test_expect_success 'difftool --extcmd cat' '
231         diff=$(git difftool --no-prompt --extcmd cat branch) &&
232         test "$diff" = branch"$LF"master
235 test_expect_success 'difftool -x cat' '
236         diff=$(git difftool --no-prompt -x cat branch) &&
237         test "$diff" = branch"$LF"master
240 test_expect_success 'difftool --extcmd echo arg1' '
241         diff=$(git difftool --no-prompt --extcmd sh\ -c\ \"echo\ \$1\" branch)
242         test "$diff" = file
245 test_expect_success 'difftool --extcmd cat arg1' '
246         diff=$(git difftool --no-prompt --extcmd sh\ -c\ \"cat\ \$1\" branch)
247         test "$diff" = master
250 test_expect_success 'difftool --extcmd cat arg2' '
251         diff=$(git difftool --no-prompt --extcmd sh\ -c\ \"cat\ \$2\" branch)
252         test "$diff" = branch
255 test_done