t / t7800-difftool.shon commit Merge branch 'js/maint-no-cherry-pick-head-after-punted' into js/no-cherry-pick-head-after-punted (278f7e6)
   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" = "Hit return to launch 'test-tool': branch"
  42}
  43
  44# Create a file on master and change it on branch
  45test_expect_success PERL 'setup' '
  46        echo master >file &&
  47        git add file &&
  48        git commit -m "added file" &&
  49
  50        git checkout -b branch master &&
  51        echo branch >file &&
  52        git commit -a -m "branch changed file" &&
  53        git checkout master
  54'
  55
  56# Configure a custom difftool.<tool>.cmd and use it
  57test_expect_success PERL 'custom commands' '
  58        restore_test_defaults &&
  59        git config difftool.test-tool.cmd "cat \$REMOTE" &&
  60
  61        diff=$(git difftool --no-prompt branch) &&
  62        test "$diff" = "master" &&
  63
  64        restore_test_defaults &&
  65        diff=$(git difftool --no-prompt branch) &&
  66        test "$diff" = "branch"
  67'
  68
  69# Ensures that git-difftool ignores bogus --tool values
  70test_expect_success PERL 'difftool ignores bad --tool values' '
  71        diff=$(git difftool --no-prompt --tool=bad-tool branch)
  72        test "$?" = 1 &&
  73        test "$diff" = ""
  74'
  75
  76test_expect_success PERL 'difftool honors --gui' '
  77        git config merge.tool bogus-tool &&
  78        git config diff.tool bogus-tool &&
  79        git config diff.guitool test-tool &&
  80
  81        diff=$(git difftool --no-prompt --gui branch) &&
  82        test "$diff" = "branch" &&
  83
  84        restore_test_defaults
  85'
  86
  87test_expect_success PERL 'difftool --gui works without configured diff.guitool' '
  88        git config diff.tool test-tool &&
  89
  90        diff=$(git difftool --no-prompt --gui branch) &&
  91        test "$diff" = "branch" &&
  92
  93        restore_test_defaults
  94'
  95
  96# Specify the diff tool using $GIT_DIFF_TOOL
  97test_expect_success PERL 'GIT_DIFF_TOOL variable' '
  98        test_might_fail git config --unset diff.tool &&
  99        GIT_DIFF_TOOL=test-tool &&
 100        export GIT_DIFF_TOOL &&
 101
 102        diff=$(git difftool --no-prompt branch) &&
 103        test "$diff" = "branch" &&
 104
 105        restore_test_defaults
 106'
 107
 108# Test the $GIT_*_TOOL variables and ensure
 109# that $GIT_DIFF_TOOL always wins unless --tool is specified
 110test_expect_success PERL 'GIT_DIFF_TOOL overrides' '
 111        git config diff.tool bogus-tool &&
 112        git config merge.tool bogus-tool &&
 113
 114        GIT_DIFF_TOOL=test-tool &&
 115        export GIT_DIFF_TOOL &&
 116
 117        diff=$(git difftool --no-prompt branch) &&
 118        test "$diff" = "branch" &&
 119
 120        GIT_DIFF_TOOL=bogus-tool &&
 121        export GIT_DIFF_TOOL &&
 122
 123        diff=$(git difftool --no-prompt --tool=test-tool branch) &&
 124        test "$diff" = "branch" &&
 125
 126        restore_test_defaults
 127'
 128
 129# Test that we don't have to pass --no-prompt to difftool
 130# when $GIT_DIFFTOOL_NO_PROMPT is true
 131test_expect_success PERL 'GIT_DIFFTOOL_NO_PROMPT variable' '
 132        GIT_DIFFTOOL_NO_PROMPT=true &&
 133        export GIT_DIFFTOOL_NO_PROMPT &&
 134
 135        diff=$(git difftool branch) &&
 136        test "$diff" = "branch" &&
 137
 138        restore_test_defaults
 139'
 140
 141# git-difftool supports the difftool.prompt variable.
 142# Test that GIT_DIFFTOOL_PROMPT can override difftool.prompt = false
 143test_expect_success PERL 'GIT_DIFFTOOL_PROMPT variable' '
 144        git config difftool.prompt false &&
 145        GIT_DIFFTOOL_PROMPT=true &&
 146        export GIT_DIFFTOOL_PROMPT &&
 147
 148        prompt=$(echo | git difftool branch | tail -1) &&
 149        prompt_given "$prompt" &&
 150
 151        restore_test_defaults
 152'
 153
 154# Test that we don't have to pass --no-prompt when difftool.prompt is false
 155test_expect_success PERL 'difftool.prompt config variable is false' '
 156        git config difftool.prompt false &&
 157
 158        diff=$(git difftool branch) &&
 159        test "$diff" = "branch" &&
 160
 161        restore_test_defaults
 162'
 163
 164# Test that we don't have to pass --no-prompt when mergetool.prompt is false
 165test_expect_success PERL 'difftool merge.prompt = false' '
 166        test_might_fail git config --unset difftool.prompt &&
 167        git config mergetool.prompt false &&
 168
 169        diff=$(git difftool branch) &&
 170        test "$diff" = "branch" &&
 171
 172        restore_test_defaults
 173'
 174
 175# Test that the -y flag can override difftool.prompt = true
 176test_expect_success PERL 'difftool.prompt can overridden with -y' '
 177        git config difftool.prompt true &&
 178
 179        diff=$(git difftool -y branch) &&
 180        test "$diff" = "branch" &&
 181
 182        restore_test_defaults
 183'
 184
 185# Test that the --prompt flag can override difftool.prompt = false
 186test_expect_success PERL 'difftool.prompt can overridden with --prompt' '
 187        git config difftool.prompt false &&
 188
 189        prompt=$(echo | git difftool --prompt branch | tail -1) &&
 190        prompt_given "$prompt" &&
 191
 192        restore_test_defaults
 193'
 194
 195# Test that the last flag passed on the command-line wins
 196test_expect_success PERL 'difftool last flag wins' '
 197        diff=$(git difftool --prompt --no-prompt branch) &&
 198        test "$diff" = "branch" &&
 199
 200        restore_test_defaults &&
 201
 202        prompt=$(echo | git difftool --no-prompt --prompt branch | tail -1) &&
 203        prompt_given "$prompt" &&
 204
 205        restore_test_defaults
 206'
 207
 208# git-difftool falls back to git-mergetool config variables
 209# so test that behavior here
 210test_expect_success PERL 'difftool + mergetool config variables' '
 211        remove_config_vars &&
 212        git config merge.tool test-tool &&
 213        git config mergetool.test-tool.cmd "cat \$LOCAL" &&
 214
 215        diff=$(git difftool --no-prompt branch) &&
 216        test "$diff" = "branch" &&
 217
 218        # set merge.tool to something bogus, diff.tool to test-tool
 219        git config merge.tool bogus-tool &&
 220        git config diff.tool test-tool &&
 221
 222        diff=$(git difftool --no-prompt branch) &&
 223        test "$diff" = "branch" &&
 224
 225        restore_test_defaults
 226'
 227
 228test_expect_success PERL 'difftool.<tool>.path' '
 229        git config difftool.tkdiff.path echo &&
 230        diff=$(git difftool --tool=tkdiff --no-prompt branch) &&
 231        git config --unset difftool.tkdiff.path &&
 232        lines=$(echo "$diff" | grep file | wc -l) &&
 233        test "$lines" -eq 1 &&
 234
 235        restore_test_defaults
 236'
 237
 238test_expect_success PERL 'difftool --extcmd=cat' '
 239        diff=$(git difftool --no-prompt --extcmd=cat branch) &&
 240        test "$diff" = branch"$LF"master
 241'
 242
 243test_expect_success PERL 'difftool --extcmd cat' '
 244        diff=$(git difftool --no-prompt --extcmd cat branch) &&
 245        test "$diff" = branch"$LF"master
 246'
 247
 248test_expect_success PERL 'difftool -x cat' '
 249        diff=$(git difftool --no-prompt -x cat branch) &&
 250        test "$diff" = branch"$LF"master
 251'
 252
 253test_expect_success PERL 'difftool --extcmd echo arg1' '
 254        diff=$(git difftool --no-prompt --extcmd sh\ -c\ \"echo\ \$1\" branch) &&
 255        test "$diff" = file
 256'
 257
 258test_expect_success PERL 'difftool --extcmd cat arg1' '
 259        diff=$(git difftool --no-prompt --extcmd sh\ -c\ \"cat\ \$1\" branch) &&
 260        test "$diff" = master
 261'
 262
 263test_expect_success PERL 'difftool --extcmd cat arg2' '
 264        diff=$(git difftool --no-prompt --extcmd sh\ -c\ \"cat\ \$2\" branch) &&
 265        test "$diff" = branch
 266'
 267
 268test_done