t / t7800-difftool.shon commit Sync with maint (65da088)
   1#!/bin/sh
   2#
   3# Copyright (c) 2009, 2010 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 diff.guitool
  18        git config --unset difftool.test-tool.cmd
  19        git config --unset difftool.prompt
  20        git config --unset merge.tool
  21        git config --unset mergetool.test-tool.cmd
  22        git config --unset mergetool.prompt
  23        return 0
  24}
  25
  26restore_test_defaults()
  27{
  28        # Restores the test defaults used by several tests
  29        remove_config_vars
  30        unset GIT_DIFF_TOOL
  31        unset GIT_DIFFTOOL_PROMPT
  32        unset GIT_DIFFTOOL_NO_PROMPT
  33        git config diff.tool test-tool &&
  34        git config difftool.test-tool.cmd 'cat $LOCAL'
  35        git config difftool.bogus-tool.cmd false
  36}
  37
  38prompt_given()
  39{
  40        prompt="$1"
  41        test "$prompt" = "Launch 'test-tool' [Y/n]: branch"
  42}
  43
  44stdin_contains()
  45{
  46        grep >/dev/null "$1"
  47}
  48
  49stdin_doesnot_contain()
  50{
  51        ! stdin_contains "$1"
  52}
  53
  54# Create a file on master and change it on branch
  55test_expect_success PERL 'setup' '
  56        echo master >file &&
  57        git add file &&
  58        git commit -m "added file" &&
  59
  60        git checkout -b branch master &&
  61        echo branch >file &&
  62        git commit -a -m "branch changed file" &&
  63        git checkout master
  64'
  65
  66# Configure a custom difftool.<tool>.cmd and use it
  67test_expect_success PERL 'custom commands' '
  68        restore_test_defaults &&
  69        git config difftool.test-tool.cmd "cat \$REMOTE" &&
  70
  71        diff=$(git difftool --no-prompt branch) &&
  72        test "$diff" = "master" &&
  73
  74        restore_test_defaults &&
  75        diff=$(git difftool --no-prompt branch) &&
  76        test "$diff" = "branch"
  77'
  78
  79# Ensures that git-difftool ignores bogus --tool values
  80test_expect_success PERL 'difftool ignores bad --tool values' '
  81        diff=$(git difftool --no-prompt --tool=bad-tool branch)
  82        test "$?" = 1 &&
  83        test "$diff" = ""
  84'
  85
  86test_expect_success PERL 'difftool honors --gui' '
  87        git config merge.tool bogus-tool &&
  88        git config diff.tool bogus-tool &&
  89        git config diff.guitool test-tool &&
  90
  91        diff=$(git difftool --no-prompt --gui branch) &&
  92        test "$diff" = "branch" &&
  93
  94        restore_test_defaults
  95'
  96
  97test_expect_success PERL 'difftool --gui works without configured diff.guitool' '
  98        git config diff.tool test-tool &&
  99
 100        diff=$(git difftool --no-prompt --gui branch) &&
 101        test "$diff" = "branch" &&
 102
 103        restore_test_defaults
 104'
 105
 106# Specify the diff tool using $GIT_DIFF_TOOL
 107test_expect_success PERL 'GIT_DIFF_TOOL variable' '
 108        test_might_fail git config --unset diff.tool &&
 109        GIT_DIFF_TOOL=test-tool &&
 110        export GIT_DIFF_TOOL &&
 111
 112        diff=$(git difftool --no-prompt branch) &&
 113        test "$diff" = "branch" &&
 114
 115        restore_test_defaults
 116'
 117
 118# Test the $GIT_*_TOOL variables and ensure
 119# that $GIT_DIFF_TOOL always wins unless --tool is specified
 120test_expect_success PERL 'GIT_DIFF_TOOL overrides' '
 121        git config diff.tool bogus-tool &&
 122        git config merge.tool bogus-tool &&
 123
 124        GIT_DIFF_TOOL=test-tool &&
 125        export GIT_DIFF_TOOL &&
 126
 127        diff=$(git difftool --no-prompt branch) &&
 128        test "$diff" = "branch" &&
 129
 130        GIT_DIFF_TOOL=bogus-tool &&
 131        export GIT_DIFF_TOOL &&
 132
 133        diff=$(git difftool --no-prompt --tool=test-tool branch) &&
 134        test "$diff" = "branch" &&
 135
 136        restore_test_defaults
 137'
 138
 139# Test that we don't have to pass --no-prompt to difftool
 140# when $GIT_DIFFTOOL_NO_PROMPT is true
 141test_expect_success PERL 'GIT_DIFFTOOL_NO_PROMPT variable' '
 142        GIT_DIFFTOOL_NO_PROMPT=true &&
 143        export GIT_DIFFTOOL_NO_PROMPT &&
 144
 145        diff=$(git difftool branch) &&
 146        test "$diff" = "branch" &&
 147
 148        restore_test_defaults
 149'
 150
 151# git-difftool supports the difftool.prompt variable.
 152# Test that GIT_DIFFTOOL_PROMPT can override difftool.prompt = false
 153test_expect_success PERL 'GIT_DIFFTOOL_PROMPT variable' '
 154        git config difftool.prompt false &&
 155        GIT_DIFFTOOL_PROMPT=true &&
 156        export GIT_DIFFTOOL_PROMPT &&
 157
 158        prompt=$(echo | git difftool branch | tail -1) &&
 159        prompt_given "$prompt" &&
 160
 161        restore_test_defaults
 162'
 163
 164# Test that we don't have to pass --no-prompt when difftool.prompt is false
 165test_expect_success PERL 'difftool.prompt config variable is false' '
 166        git config difftool.prompt false &&
 167
 168        diff=$(git difftool branch) &&
 169        test "$diff" = "branch" &&
 170
 171        restore_test_defaults
 172'
 173
 174# Test that we don't have to pass --no-prompt when mergetool.prompt is false
 175test_expect_success PERL 'difftool merge.prompt = false' '
 176        test_might_fail git config --unset difftool.prompt &&
 177        git config mergetool.prompt false &&
 178
 179        diff=$(git difftool branch) &&
 180        test "$diff" = "branch" &&
 181
 182        restore_test_defaults
 183'
 184
 185# Test that the -y flag can override difftool.prompt = true
 186test_expect_success PERL 'difftool.prompt can overridden with -y' '
 187        git config difftool.prompt true &&
 188
 189        diff=$(git difftool -y branch) &&
 190        test "$diff" = "branch" &&
 191
 192        restore_test_defaults
 193'
 194
 195# Test that the --prompt flag can override difftool.prompt = false
 196test_expect_success PERL 'difftool.prompt can overridden with --prompt' '
 197        git config difftool.prompt false &&
 198
 199        prompt=$(echo | git difftool --prompt branch | tail -1) &&
 200        prompt_given "$prompt" &&
 201
 202        restore_test_defaults
 203'
 204
 205# Test that the last flag passed on the command-line wins
 206test_expect_success PERL 'difftool last flag wins' '
 207        diff=$(git difftool --prompt --no-prompt branch) &&
 208        test "$diff" = "branch" &&
 209
 210        restore_test_defaults &&
 211
 212        prompt=$(echo | git difftool --no-prompt --prompt branch | tail -1) &&
 213        prompt_given "$prompt" &&
 214
 215        restore_test_defaults
 216'
 217
 218# git-difftool falls back to git-mergetool config variables
 219# so test that behavior here
 220test_expect_success PERL 'difftool + mergetool config variables' '
 221        remove_config_vars &&
 222        git config merge.tool test-tool &&
 223        git config mergetool.test-tool.cmd "cat \$LOCAL" &&
 224
 225        diff=$(git difftool --no-prompt branch) &&
 226        test "$diff" = "branch" &&
 227
 228        # set merge.tool to something bogus, diff.tool to test-tool
 229        git config merge.tool bogus-tool &&
 230        git config diff.tool test-tool &&
 231
 232        diff=$(git difftool --no-prompt branch) &&
 233        test "$diff" = "branch" &&
 234
 235        restore_test_defaults
 236'
 237
 238test_expect_success PERL 'difftool.<tool>.path' '
 239        git config difftool.tkdiff.path echo &&
 240        diff=$(git difftool --tool=tkdiff --no-prompt branch) &&
 241        git config --unset difftool.tkdiff.path &&
 242        lines=$(echo "$diff" | grep file | wc -l) &&
 243        test "$lines" -eq 1 &&
 244
 245        restore_test_defaults
 246'
 247
 248test_expect_success PERL 'difftool --extcmd=cat' '
 249        diff=$(git difftool --no-prompt --extcmd=cat branch) &&
 250        test "$diff" = branch"$LF"master
 251'
 252
 253test_expect_success PERL 'difftool --extcmd cat' '
 254        diff=$(git difftool --no-prompt --extcmd cat branch) &&
 255        test "$diff" = branch"$LF"master
 256'
 257
 258test_expect_success PERL 'difftool -x cat' '
 259        diff=$(git difftool --no-prompt -x cat branch) &&
 260        test "$diff" = branch"$LF"master
 261'
 262
 263test_expect_success PERL 'difftool --extcmd echo arg1' '
 264        diff=$(git difftool --no-prompt --extcmd sh\ -c\ \"echo\ \$1\" branch) &&
 265        test "$diff" = file
 266'
 267
 268test_expect_success PERL 'difftool --extcmd cat arg1' '
 269        diff=$(git difftool --no-prompt --extcmd sh\ -c\ \"cat\ \$1\" branch) &&
 270        test "$diff" = master
 271'
 272
 273test_expect_success PERL 'difftool --extcmd cat arg2' '
 274        diff=$(git difftool --no-prompt --extcmd sh\ -c\ \"cat\ \$2\" branch) &&
 275        test "$diff" = branch
 276'
 277
 278# Create a second file on master and a different version on branch
 279test_expect_success PERL 'setup with 2 files different' '
 280        echo m2 >file2 &&
 281        git add file2 &&
 282        git commit -m "added file2" &&
 283
 284        git checkout branch &&
 285        echo br2 >file2 &&
 286        git add file2 &&
 287        git commit -a -m "branch changed file2" &&
 288        git checkout master
 289'
 290
 291test_expect_success PERL 'say no to the first file' '
 292        diff=$( (echo n; echo) | git difftool -x cat branch ) &&
 293
 294        echo "$diff" | stdin_contains m2 &&
 295        echo "$diff" | stdin_contains br2 &&
 296        echo "$diff" | stdin_doesnot_contain master &&
 297        echo "$diff" | stdin_doesnot_contain branch
 298'
 299
 300test_expect_success PERL 'say no to the second file' '
 301        diff=$( (echo; echo n) | git difftool -x cat branch ) &&
 302
 303        echo "$diff" | stdin_contains master &&
 304        echo "$diff" | stdin_contains branch &&
 305        echo "$diff" | stdin_doesnot_contain m2 &&
 306        echo "$diff" | stdin_doesnot_contain br2
 307'
 308
 309test_done