707a0f54ae85a6095c57e2598bb5cd3fd83bceb3
   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 difftool.test-tool.cmd
  23        git config --unset difftool.prompt
  24        git config --unset merge.tool
  25        git config --unset mergetool.test-tool.cmd
  26        return 0
  27}
  28
  29restore_test_defaults()
  30{
  31        # Restores the test defaults used by several tests
  32        remove_config_vars
  33        unset GIT_DIFF_TOOL
  34        unset GIT_MERGE_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
  80# Specify the diff tool using $GIT_DIFF_TOOL
  81test_expect_success 'GIT_DIFF_TOOL variable' '
  82        git config --unset diff.tool
  83        GIT_DIFF_TOOL=test-tool &&
  84        export GIT_DIFF_TOOL &&
  85
  86        diff=$(git difftool --no-prompt branch) &&
  87        test "$diff" = "branch" &&
  88
  89        restore_test_defaults
  90'
  91
  92# Test the $GIT_*_TOOL variables and ensure
  93# that $GIT_DIFF_TOOL always wins unless --tool is specified
  94test_expect_success 'GIT_DIFF_TOOL overrides' '
  95        git config diff.tool bogus-tool &&
  96        git config merge.tool bogus-tool &&
  97
  98        GIT_MERGE_TOOL=test-tool &&
  99        export GIT_MERGE_TOOL &&
 100        diff=$(git difftool --no-prompt branch) &&
 101        test "$diff" = "branch" &&
 102        unset GIT_MERGE_TOOL &&
 103
 104        GIT_MERGE_TOOL=bogus-tool &&
 105        GIT_DIFF_TOOL=test-tool &&
 106        export GIT_MERGE_TOOL &&
 107        export GIT_DIFF_TOOL &&
 108
 109        diff=$(git difftool --no-prompt branch) &&
 110        test "$diff" = "branch" &&
 111
 112        GIT_DIFF_TOOL=bogus-tool &&
 113        export GIT_DIFF_TOOL &&
 114
 115        diff=$(git difftool --no-prompt --tool=test-tool branch) &&
 116        test "$diff" = "branch" &&
 117
 118        restore_test_defaults
 119'
 120
 121# Test that we don't have to pass --no-prompt to difftool
 122# when $GIT_DIFFTOOL_NO_PROMPT is true
 123test_expect_success 'GIT_DIFFTOOL_NO_PROMPT variable' '
 124        GIT_DIFFTOOL_NO_PROMPT=true &&
 125        export GIT_DIFFTOOL_NO_PROMPT &&
 126
 127        diff=$(git difftool branch) &&
 128        test "$diff" = "branch" &&
 129
 130        restore_test_defaults
 131'
 132
 133# git-difftool supports the difftool.prompt variable.
 134# Test that GIT_DIFFTOOL_PROMPT can override difftool.prompt = false
 135test_expect_success 'GIT_DIFFTOOL_PROMPT variable' '
 136        git config difftool.prompt false &&
 137        GIT_DIFFTOOL_PROMPT=true &&
 138        export GIT_DIFFTOOL_PROMPT &&
 139
 140        prompt=$(echo | git difftool branch | tail -1) &&
 141        prompt_given "$prompt" &&
 142
 143        restore_test_defaults
 144'
 145
 146# Test that we don't have to pass --no-prompt when difftool.prompt is false
 147test_expect_success 'difftool.prompt config variable is false' '
 148        git config difftool.prompt false &&
 149
 150        diff=$(git difftool branch) &&
 151        test "$diff" = "branch" &&
 152
 153        restore_test_defaults
 154'
 155
 156# Test that the -y flag can override difftool.prompt = true
 157test_expect_success 'difftool.prompt can overridden with -y' '
 158        git config difftool.prompt true &&
 159
 160        diff=$(git difftool -y branch) &&
 161        test "$diff" = "branch" &&
 162
 163        restore_test_defaults
 164'
 165
 166# Test that the --prompt flag can override difftool.prompt = false
 167test_expect_success 'difftool.prompt can overridden with --prompt' '
 168        git config difftool.prompt false &&
 169
 170        prompt=$(echo | git difftool --prompt branch | tail -1) &&
 171        prompt_given "$prompt" &&
 172
 173        restore_test_defaults
 174'
 175
 176# Test that the last flag passed on the command-line wins
 177test_expect_success 'difftool last flag wins' '
 178        diff=$(git difftool --prompt --no-prompt branch) &&
 179        test "$diff" = "branch" &&
 180
 181        restore_test_defaults &&
 182
 183        prompt=$(echo | git difftool --no-prompt --prompt branch | tail -1) &&
 184        prompt_given "$prompt" &&
 185
 186        restore_test_defaults
 187'
 188
 189# git-difftool falls back to git-mergetool config variables
 190# so test that behavior here
 191test_expect_success 'difftool + mergetool config variables' '
 192        remove_config_vars
 193        git config merge.tool test-tool &&
 194        git config mergetool.test-tool.cmd "cat \$LOCAL" &&
 195
 196        diff=$(git difftool --no-prompt branch) &&
 197        test "$diff" = "branch" &&
 198
 199        # set merge.tool to something bogus, diff.tool to test-tool
 200        git config merge.tool bogus-tool &&
 201        git config diff.tool test-tool &&
 202
 203        diff=$(git difftool --no-prompt branch) &&
 204        test "$diff" = "branch" &&
 205
 206        restore_test_defaults
 207'
 208
 209test_expect_success 'difftool.<tool>.path' '
 210        git config difftool.tkdiff.path echo &&
 211        diff=$(git difftool --tool=tkdiff --no-prompt branch) &&
 212        git config --unset difftool.tkdiff.path &&
 213        lines=$(echo "$diff" | grep file | wc -l) &&
 214        test "$lines" -eq 1
 215'
 216
 217test_done