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