t / t7800-difftool.shon commit Merge branch 'jk/maint-config-param' (fe01ef3)
   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
  13LF='
  14'
  15
  16remove_config_vars()
  17{
  18        # Unset all config variables used by git-difftool
  19        git config --unset diff.tool
  20        git config --unset diff.guitool
  21        git config --unset difftool.test-tool.cmd
  22        git config --unset difftool.prompt
  23        git config --unset merge.tool
  24        git config --unset mergetool.test-tool.cmd
  25        git config --unset mergetool.prompt
  26        return 0
  27}
  28
  29restore_test_defaults()
  30{
  31        # Restores the test defaults used by several tests
  32        remove_config_vars
  33        unset GIT_DIFF_TOOL
  34        unset GIT_DIFFTOOL_PROMPT
  35        unset GIT_DIFFTOOL_NO_PROMPT
  36        git config diff.tool test-tool &&
  37        git config difftool.test-tool.cmd 'cat $LOCAL'
  38        git config difftool.bogus-tool.cmd false
  39}
  40
  41prompt_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
  48test_expect_success PERL '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
  60test_expect_success PERL '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
  73test_expect_success PERL 'difftool ignores bad --tool values' '
  74        diff=$(git difftool --no-prompt --tool=bad-tool branch)
  75        test "$?" = 1 &&
  76        test "$diff" = ""
  77'
  78
  79test_expect_success PERL 'difftool honors --gui' '
  80        git config merge.tool bogus-tool &&
  81        git config diff.tool bogus-tool &&
  82        git config diff.guitool test-tool &&
  83
  84        diff=$(git difftool --no-prompt --gui branch) &&
  85        test "$diff" = "branch" &&
  86
  87        restore_test_defaults
  88'
  89
  90test_expect_success PERL 'difftool --gui works without configured diff.guitool' '
  91        git config diff.tool test-tool &&
  92
  93        diff=$(git difftool --no-prompt --gui branch) &&
  94        test "$diff" = "branch" &&
  95
  96        restore_test_defaults
  97'
  98
  99# Specify the diff tool using $GIT_DIFF_TOOL
 100test_expect_success PERL 'GIT_DIFF_TOOL variable' '
 101        test_might_fail git config --unset diff.tool &&
 102        GIT_DIFF_TOOL=test-tool &&
 103        export GIT_DIFF_TOOL &&
 104
 105        diff=$(git difftool --no-prompt branch) &&
 106        test "$diff" = "branch" &&
 107
 108        restore_test_defaults
 109'
 110
 111# Test the $GIT_*_TOOL variables and ensure
 112# that $GIT_DIFF_TOOL always wins unless --tool is specified
 113test_expect_success PERL 'GIT_DIFF_TOOL overrides' '
 114        git config diff.tool bogus-tool &&
 115        git config merge.tool bogus-tool &&
 116
 117        GIT_DIFF_TOOL=test-tool &&
 118        export GIT_DIFF_TOOL &&
 119
 120        diff=$(git difftool --no-prompt branch) &&
 121        test "$diff" = "branch" &&
 122
 123        GIT_DIFF_TOOL=bogus-tool &&
 124        export GIT_DIFF_TOOL &&
 125
 126        diff=$(git difftool --no-prompt --tool=test-tool branch) &&
 127        test "$diff" = "branch" &&
 128
 129        restore_test_defaults
 130'
 131
 132# Test that we don't have to pass --no-prompt to difftool
 133# when $GIT_DIFFTOOL_NO_PROMPT is true
 134test_expect_success PERL 'GIT_DIFFTOOL_NO_PROMPT variable' '
 135        GIT_DIFFTOOL_NO_PROMPT=true &&
 136        export GIT_DIFFTOOL_NO_PROMPT &&
 137
 138        diff=$(git difftool branch) &&
 139        test "$diff" = "branch" &&
 140
 141        restore_test_defaults
 142'
 143
 144# git-difftool supports the difftool.prompt variable.
 145# Test that GIT_DIFFTOOL_PROMPT can override difftool.prompt = false
 146test_expect_success PERL 'GIT_DIFFTOOL_PROMPT variable' '
 147        git config difftool.prompt false &&
 148        GIT_DIFFTOOL_PROMPT=true &&
 149        export GIT_DIFFTOOL_PROMPT &&
 150
 151        prompt=$(echo | git difftool branch | tail -1) &&
 152        prompt_given "$prompt" &&
 153
 154        restore_test_defaults
 155'
 156
 157# Test that we don't have to pass --no-prompt when difftool.prompt is false
 158test_expect_success PERL 'difftool.prompt config variable is false' '
 159        git config difftool.prompt false &&
 160
 161        diff=$(git difftool branch) &&
 162        test "$diff" = "branch" &&
 163
 164        restore_test_defaults
 165'
 166
 167# Test that we don't have to pass --no-prompt when mergetool.prompt is false
 168test_expect_success PERL 'difftool merge.prompt = false' '
 169        test_might_fail git config --unset difftool.prompt &&
 170        git config mergetool.prompt false &&
 171
 172        diff=$(git difftool branch) &&
 173        test "$diff" = "branch" &&
 174
 175        restore_test_defaults
 176'
 177
 178# Test that the -y flag can override difftool.prompt = true
 179test_expect_success PERL 'difftool.prompt can overridden with -y' '
 180        git config difftool.prompt true &&
 181
 182        diff=$(git difftool -y branch) &&
 183        test "$diff" = "branch" &&
 184
 185        restore_test_defaults
 186'
 187
 188# Test that the --prompt flag can override difftool.prompt = false
 189test_expect_success PERL 'difftool.prompt can overridden with --prompt' '
 190        git config difftool.prompt false &&
 191
 192        prompt=$(echo | git difftool --prompt branch | tail -1) &&
 193        prompt_given "$prompt" &&
 194
 195        restore_test_defaults
 196'
 197
 198# Test that the last flag passed on the command-line wins
 199test_expect_success PERL 'difftool last flag wins' '
 200        diff=$(git difftool --prompt --no-prompt branch) &&
 201        test "$diff" = "branch" &&
 202
 203        restore_test_defaults &&
 204
 205        prompt=$(echo | git difftool --no-prompt --prompt branch | tail -1) &&
 206        prompt_given "$prompt" &&
 207
 208        restore_test_defaults
 209'
 210
 211# git-difftool falls back to git-mergetool config variables
 212# so test that behavior here
 213test_expect_success PERL 'difftool + mergetool config variables' '
 214        remove_config_vars &&
 215        git config merge.tool test-tool &&
 216        git config mergetool.test-tool.cmd "cat \$LOCAL" &&
 217
 218        diff=$(git difftool --no-prompt branch) &&
 219        test "$diff" = "branch" &&
 220
 221        # set merge.tool to something bogus, diff.tool to test-tool
 222        git config merge.tool bogus-tool &&
 223        git config diff.tool test-tool &&
 224
 225        diff=$(git difftool --no-prompt branch) &&
 226        test "$diff" = "branch" &&
 227
 228        restore_test_defaults
 229'
 230
 231test_expect_success PERL 'difftool.<tool>.path' '
 232        git config difftool.tkdiff.path echo &&
 233        diff=$(git difftool --tool=tkdiff --no-prompt branch) &&
 234        git config --unset difftool.tkdiff.path &&
 235        lines=$(echo "$diff" | grep file | wc -l) &&
 236        test "$lines" -eq 1 &&
 237
 238        restore_test_defaults
 239'
 240
 241test_expect_success PERL 'difftool --extcmd=cat' '
 242        diff=$(git difftool --no-prompt --extcmd=cat branch) &&
 243        test "$diff" = branch"$LF"master
 244'
 245
 246test_expect_success PERL 'difftool --extcmd cat' '
 247        diff=$(git difftool --no-prompt --extcmd cat branch) &&
 248        test "$diff" = branch"$LF"master
 249'
 250
 251test_expect_success PERL 'difftool -x cat' '
 252        diff=$(git difftool --no-prompt -x cat branch) &&
 253        test "$diff" = branch"$LF"master
 254'
 255
 256test_expect_success PERL 'difftool --extcmd echo arg1' '
 257        diff=$(git difftool --no-prompt --extcmd sh\ -c\ \"echo\ \$1\" branch) &&
 258        test "$diff" = file
 259'
 260
 261test_expect_success PERL 'difftool --extcmd cat arg1' '
 262        diff=$(git difftool --no-prompt --extcmd sh\ -c\ \"cat\ \$1\" branch) &&
 263        test "$diff" = master
 264'
 265
 266test_expect_success PERL 'difftool --extcmd cat arg2' '
 267        diff=$(git difftool --no-prompt --extcmd sh\ -c\ \"cat\ \$2\" branch) &&
 268        test "$diff" = branch
 269'
 270
 271test_done