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