t / t7800-difftool.shon commit Merge branch 'jc/rerere-train' (7b871c5)
   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" = "Launch 'test-tool' [Y/n]: branch"
  42}
  43
  44stdin_contains()
  45{
  46        grep >/dev/null "$1"
  47}
  48
  49stdin_doesnot_contain()
  50{
  51        ! stdin_contains "$1"
  52}
  53
  54# Create a file on master and change it on branch
  55test_expect_success PERL 'setup' '
  56        echo master >file &&
  57        git add file &&
  58        git commit -m "added file" &&
  59
  60        git checkout -b branch master &&
  61        echo branch >file &&
  62        git commit -a -m "branch changed file" &&
  63        git checkout master
  64'
  65
  66# Configure a custom difftool.<tool>.cmd and use it
  67test_expect_success PERL 'custom commands' '
  68        restore_test_defaults &&
  69        git config difftool.test-tool.cmd "cat \$REMOTE" &&
  70
  71        diff=$(git difftool --no-prompt branch) &&
  72        test "$diff" = "master" &&
  73
  74        restore_test_defaults &&
  75        diff=$(git difftool --no-prompt branch) &&
  76        test "$diff" = "branch"
  77'
  78
  79# Ensures that git-difftool ignores bogus --tool values
  80test_expect_success PERL 'difftool ignores bad --tool values' '
  81        diff=$(git difftool --no-prompt --tool=bad-tool branch)
  82        test "$?" = 1 &&
  83        test "$diff" = ""
  84'
  85
  86test_expect_success PERL 'difftool forwards arguments to diff' '
  87        >for-diff &&
  88        git add for-diff &&
  89        echo changes>for-diff &&
  90        git add for-diff &&
  91        diff=$(git difftool --cached --no-prompt -- for-diff) &&
  92        test "$diff" = "" &&
  93        git reset -- for-diff &&
  94        rm for-diff
  95'
  96
  97test_expect_success PERL 'difftool honors --gui' '
  98        git config merge.tool bogus-tool &&
  99        git config diff.tool bogus-tool &&
 100        git config diff.guitool test-tool &&
 101
 102        diff=$(git difftool --no-prompt --gui branch) &&
 103        test "$diff" = "branch" &&
 104
 105        restore_test_defaults
 106'
 107
 108test_expect_success PERL 'difftool --gui works without configured diff.guitool' '
 109        git config diff.tool test-tool &&
 110
 111        diff=$(git difftool --no-prompt --gui branch) &&
 112        test "$diff" = "branch" &&
 113
 114        restore_test_defaults
 115'
 116
 117# Specify the diff tool using $GIT_DIFF_TOOL
 118test_expect_success PERL 'GIT_DIFF_TOOL variable' '
 119        test_might_fail git config --unset diff.tool &&
 120        GIT_DIFF_TOOL=test-tool &&
 121        export GIT_DIFF_TOOL &&
 122
 123        diff=$(git difftool --no-prompt branch) &&
 124        test "$diff" = "branch" &&
 125
 126        restore_test_defaults
 127'
 128
 129# Test the $GIT_*_TOOL variables and ensure
 130# that $GIT_DIFF_TOOL always wins unless --tool is specified
 131test_expect_success PERL 'GIT_DIFF_TOOL overrides' '
 132        git config diff.tool bogus-tool &&
 133        git config merge.tool bogus-tool &&
 134
 135        GIT_DIFF_TOOL=test-tool &&
 136        export GIT_DIFF_TOOL &&
 137
 138        diff=$(git difftool --no-prompt branch) &&
 139        test "$diff" = "branch" &&
 140
 141        GIT_DIFF_TOOL=bogus-tool &&
 142        export GIT_DIFF_TOOL &&
 143
 144        diff=$(git difftool --no-prompt --tool=test-tool branch) &&
 145        test "$diff" = "branch" &&
 146
 147        restore_test_defaults
 148'
 149
 150# Test that we don't have to pass --no-prompt to difftool
 151# when $GIT_DIFFTOOL_NO_PROMPT is true
 152test_expect_success PERL 'GIT_DIFFTOOL_NO_PROMPT variable' '
 153        GIT_DIFFTOOL_NO_PROMPT=true &&
 154        export GIT_DIFFTOOL_NO_PROMPT &&
 155
 156        diff=$(git difftool branch) &&
 157        test "$diff" = "branch" &&
 158
 159        restore_test_defaults
 160'
 161
 162# git-difftool supports the difftool.prompt variable.
 163# Test that GIT_DIFFTOOL_PROMPT can override difftool.prompt = false
 164test_expect_success PERL 'GIT_DIFFTOOL_PROMPT variable' '
 165        git config difftool.prompt false &&
 166        GIT_DIFFTOOL_PROMPT=true &&
 167        export GIT_DIFFTOOL_PROMPT &&
 168
 169        prompt=$(echo | git difftool branch | tail -1) &&
 170        prompt_given "$prompt" &&
 171
 172        restore_test_defaults
 173'
 174
 175# Test that we don't have to pass --no-prompt when difftool.prompt is false
 176test_expect_success PERL 'difftool.prompt config variable is false' '
 177        git config difftool.prompt false &&
 178
 179        diff=$(git difftool branch) &&
 180        test "$diff" = "branch" &&
 181
 182        restore_test_defaults
 183'
 184
 185# Test that we don't have to pass --no-prompt when mergetool.prompt is false
 186test_expect_success PERL 'difftool merge.prompt = false' '
 187        test_might_fail git config --unset difftool.prompt &&
 188        git config mergetool.prompt false &&
 189
 190        diff=$(git difftool branch) &&
 191        test "$diff" = "branch" &&
 192
 193        restore_test_defaults
 194'
 195
 196# Test that the -y flag can override difftool.prompt = true
 197test_expect_success PERL 'difftool.prompt can overridden with -y' '
 198        git config difftool.prompt true &&
 199
 200        diff=$(git difftool -y branch) &&
 201        test "$diff" = "branch" &&
 202
 203        restore_test_defaults
 204'
 205
 206# Test that the --prompt flag can override difftool.prompt = false
 207test_expect_success PERL 'difftool.prompt can overridden with --prompt' '
 208        git config difftool.prompt false &&
 209
 210        prompt=$(echo | git difftool --prompt branch | tail -1) &&
 211        prompt_given "$prompt" &&
 212
 213        restore_test_defaults
 214'
 215
 216# Test that the last flag passed on the command-line wins
 217test_expect_success PERL 'difftool last flag wins' '
 218        diff=$(git difftool --prompt --no-prompt branch) &&
 219        test "$diff" = "branch" &&
 220
 221        restore_test_defaults &&
 222
 223        prompt=$(echo | git difftool --no-prompt --prompt branch | tail -1) &&
 224        prompt_given "$prompt" &&
 225
 226        restore_test_defaults
 227'
 228
 229# git-difftool falls back to git-mergetool config variables
 230# so test that behavior here
 231test_expect_success PERL 'difftool + mergetool config variables' '
 232        remove_config_vars &&
 233        git config merge.tool test-tool &&
 234        git config mergetool.test-tool.cmd "cat \$LOCAL" &&
 235
 236        diff=$(git difftool --no-prompt branch) &&
 237        test "$diff" = "branch" &&
 238
 239        # set merge.tool to something bogus, diff.tool to test-tool
 240        git config merge.tool bogus-tool &&
 241        git config diff.tool test-tool &&
 242
 243        diff=$(git difftool --no-prompt branch) &&
 244        test "$diff" = "branch" &&
 245
 246        restore_test_defaults
 247'
 248
 249test_expect_success PERL 'difftool.<tool>.path' '
 250        git config difftool.tkdiff.path echo &&
 251        diff=$(git difftool --tool=tkdiff --no-prompt branch) &&
 252        git config --unset difftool.tkdiff.path &&
 253        lines=$(echo "$diff" | grep file | wc -l) &&
 254        test "$lines" -eq 1 &&
 255
 256        restore_test_defaults
 257'
 258
 259test_expect_success PERL 'difftool --extcmd=cat' '
 260        diff=$(git difftool --no-prompt --extcmd=cat branch) &&
 261        test "$diff" = branch"$LF"master
 262'
 263
 264test_expect_success PERL 'difftool --extcmd cat' '
 265        diff=$(git difftool --no-prompt --extcmd cat branch) &&
 266        test "$diff" = branch"$LF"master
 267'
 268
 269test_expect_success PERL 'difftool -x cat' '
 270        diff=$(git difftool --no-prompt -x cat branch) &&
 271        test "$diff" = branch"$LF"master
 272'
 273
 274test_expect_success PERL 'difftool --extcmd echo arg1' '
 275        diff=$(git difftool --no-prompt --extcmd sh\ -c\ \"echo\ \$1\" branch) &&
 276        test "$diff" = file
 277'
 278
 279test_expect_success PERL 'difftool --extcmd cat arg1' '
 280        diff=$(git difftool --no-prompt --extcmd sh\ -c\ \"cat\ \$1\" branch) &&
 281        test "$diff" = master
 282'
 283
 284test_expect_success PERL 'difftool --extcmd cat arg2' '
 285        diff=$(git difftool --no-prompt --extcmd sh\ -c\ \"cat\ \$2\" branch) &&
 286        test "$diff" = branch
 287'
 288
 289# Create a second file on master and a different version on branch
 290test_expect_success PERL 'setup with 2 files different' '
 291        echo m2 >file2 &&
 292        git add file2 &&
 293        git commit -m "added file2" &&
 294
 295        git checkout branch &&
 296        echo br2 >file2 &&
 297        git add file2 &&
 298        git commit -a -m "branch changed file2" &&
 299        git checkout master
 300'
 301
 302test_expect_success PERL 'say no to the first file' '
 303        diff=$( (echo n; echo) | git difftool -x cat branch ) &&
 304
 305        echo "$diff" | stdin_contains m2 &&
 306        echo "$diff" | stdin_contains br2 &&
 307        echo "$diff" | stdin_doesnot_contain master &&
 308        echo "$diff" | stdin_doesnot_contain branch
 309'
 310
 311test_expect_success PERL 'say no to the second file' '
 312        diff=$( (echo; echo n) | git difftool -x cat branch ) &&
 313
 314        echo "$diff" | stdin_contains master &&
 315        echo "$diff" | stdin_contains branch &&
 316        echo "$diff" | stdin_doesnot_contain m2 &&
 317        echo "$diff" | stdin_doesnot_contain br2
 318'
 319
 320test_done