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