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