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