t / t7800-difftool.shon commit Merge branch 'maint' (57a0111)
   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 a custom difftool.<tool>.cmd overrides built-ins
  80test_expect_success PERL 'custom commands override built-ins' '
  81        restore_test_defaults &&
  82        git config difftool.defaults.cmd "cat \$REMOTE" &&
  83
  84        diff=$(git difftool --tool defaults --no-prompt branch) &&
  85        test "$diff" = "master" &&
  86
  87        git config --unset difftool.defaults.cmd
  88'
  89
  90# Ensures that git-difftool ignores bogus --tool values
  91test_expect_success PERL 'difftool ignores bad --tool values' '
  92        diff=$(git difftool --no-prompt --tool=bad-tool branch)
  93        test "$?" = 1 &&
  94        test "$diff" = ""
  95'
  96
  97test_expect_success PERL 'difftool forwards arguments to diff' '
  98        >for-diff &&
  99        git add for-diff &&
 100        echo changes>for-diff &&
 101        git add for-diff &&
 102        diff=$(git difftool --cached --no-prompt -- for-diff) &&
 103        test "$diff" = "" &&
 104        git reset -- for-diff &&
 105        rm for-diff
 106'
 107
 108test_expect_success PERL 'difftool honors --gui' '
 109        git config merge.tool bogus-tool &&
 110        git config diff.tool bogus-tool &&
 111        git config diff.guitool test-tool &&
 112
 113        diff=$(git difftool --no-prompt --gui branch) &&
 114        test "$diff" = "branch" &&
 115
 116        restore_test_defaults
 117'
 118
 119test_expect_success PERL 'difftool --gui last setting wins' '
 120        git config diff.guitool bogus-tool &&
 121        git difftool --no-prompt --gui --no-gui &&
 122
 123        git config merge.tool bogus-tool &&
 124        git config diff.tool bogus-tool &&
 125        git config diff.guitool test-tool &&
 126        diff=$(git difftool --no-prompt --no-gui --gui branch) &&
 127        test "$diff" = "branch" &&
 128
 129        restore_test_defaults
 130'
 131
 132test_expect_success PERL 'difftool --gui works without configured diff.guitool' '
 133        git config diff.tool test-tool &&
 134
 135        diff=$(git difftool --no-prompt --gui branch) &&
 136        test "$diff" = "branch" &&
 137
 138        restore_test_defaults
 139'
 140
 141# Specify the diff tool using $GIT_DIFF_TOOL
 142test_expect_success PERL 'GIT_DIFF_TOOL variable' '
 143        test_might_fail git config --unset diff.tool &&
 144        GIT_DIFF_TOOL=test-tool &&
 145        export GIT_DIFF_TOOL &&
 146
 147        diff=$(git difftool --no-prompt branch) &&
 148        test "$diff" = "branch" &&
 149
 150        restore_test_defaults
 151'
 152
 153# Test the $GIT_*_TOOL variables and ensure
 154# that $GIT_DIFF_TOOL always wins unless --tool is specified
 155test_expect_success PERL 'GIT_DIFF_TOOL overrides' '
 156        git config diff.tool bogus-tool &&
 157        git config merge.tool bogus-tool &&
 158
 159        GIT_DIFF_TOOL=test-tool &&
 160        export GIT_DIFF_TOOL &&
 161
 162        diff=$(git difftool --no-prompt branch) &&
 163        test "$diff" = "branch" &&
 164
 165        GIT_DIFF_TOOL=bogus-tool &&
 166        export GIT_DIFF_TOOL &&
 167
 168        diff=$(git difftool --no-prompt --tool=test-tool branch) &&
 169        test "$diff" = "branch" &&
 170
 171        restore_test_defaults
 172'
 173
 174# Test that we don't have to pass --no-prompt to difftool
 175# when $GIT_DIFFTOOL_NO_PROMPT is true
 176test_expect_success PERL 'GIT_DIFFTOOL_NO_PROMPT variable' '
 177        GIT_DIFFTOOL_NO_PROMPT=true &&
 178        export GIT_DIFFTOOL_NO_PROMPT &&
 179
 180        diff=$(git difftool branch) &&
 181        test "$diff" = "branch" &&
 182
 183        restore_test_defaults
 184'
 185
 186# git-difftool supports the difftool.prompt variable.
 187# Test that GIT_DIFFTOOL_PROMPT can override difftool.prompt = false
 188test_expect_success PERL 'GIT_DIFFTOOL_PROMPT variable' '
 189        git config difftool.prompt false &&
 190        GIT_DIFFTOOL_PROMPT=true &&
 191        export GIT_DIFFTOOL_PROMPT &&
 192
 193        prompt=$(echo | git difftool branch | tail -1) &&
 194        prompt_given "$prompt" &&
 195
 196        restore_test_defaults
 197'
 198
 199# Test that we don't have to pass --no-prompt when difftool.prompt is false
 200test_expect_success PERL 'difftool.prompt config variable is false' '
 201        git config difftool.prompt false &&
 202
 203        diff=$(git difftool branch) &&
 204        test "$diff" = "branch" &&
 205
 206        restore_test_defaults
 207'
 208
 209# Test that we don't have to pass --no-prompt when mergetool.prompt is false
 210test_expect_success PERL 'difftool merge.prompt = false' '
 211        test_might_fail git config --unset difftool.prompt &&
 212        git config mergetool.prompt false &&
 213
 214        diff=$(git difftool branch) &&
 215        test "$diff" = "branch" &&
 216
 217        restore_test_defaults
 218'
 219
 220# Test that the -y flag can override difftool.prompt = true
 221test_expect_success PERL 'difftool.prompt can overridden with -y' '
 222        git config difftool.prompt true &&
 223
 224        diff=$(git difftool -y branch) &&
 225        test "$diff" = "branch" &&
 226
 227        restore_test_defaults
 228'
 229
 230# Test that the --prompt flag can override difftool.prompt = false
 231test_expect_success PERL 'difftool.prompt can overridden with --prompt' '
 232        git config difftool.prompt false &&
 233
 234        prompt=$(echo | git difftool --prompt branch | tail -1) &&
 235        prompt_given "$prompt" &&
 236
 237        restore_test_defaults
 238'
 239
 240# Test that the last flag passed on the command-line wins
 241test_expect_success PERL 'difftool last flag wins' '
 242        diff=$(git difftool --prompt --no-prompt branch) &&
 243        test "$diff" = "branch" &&
 244
 245        restore_test_defaults &&
 246
 247        prompt=$(echo | git difftool --no-prompt --prompt branch | tail -1) &&
 248        prompt_given "$prompt" &&
 249
 250        restore_test_defaults
 251'
 252
 253# git-difftool falls back to git-mergetool config variables
 254# so test that behavior here
 255test_expect_success PERL 'difftool + mergetool config variables' '
 256        remove_config_vars &&
 257        git config merge.tool test-tool &&
 258        git config mergetool.test-tool.cmd "cat \$LOCAL" &&
 259
 260        diff=$(git difftool --no-prompt branch) &&
 261        test "$diff" = "branch" &&
 262
 263        # set merge.tool to something bogus, diff.tool to test-tool
 264        git config merge.tool bogus-tool &&
 265        git config diff.tool test-tool &&
 266
 267        diff=$(git difftool --no-prompt branch) &&
 268        test "$diff" = "branch" &&
 269
 270        restore_test_defaults
 271'
 272
 273test_expect_success PERL 'difftool.<tool>.path' '
 274        git config difftool.tkdiff.path echo &&
 275        diff=$(git difftool --tool=tkdiff --no-prompt branch) &&
 276        git config --unset difftool.tkdiff.path &&
 277        lines=$(echo "$diff" | grep file | wc -l) &&
 278        test "$lines" -eq 1 &&
 279
 280        restore_test_defaults
 281'
 282
 283test_expect_success PERL 'difftool --extcmd=cat' '
 284        diff=$(git difftool --no-prompt --extcmd=cat branch) &&
 285        test "$diff" = branch"$LF"master
 286'
 287
 288test_expect_success PERL 'difftool --extcmd cat' '
 289        diff=$(git difftool --no-prompt --extcmd cat branch) &&
 290        test "$diff" = branch"$LF"master
 291'
 292
 293test_expect_success PERL 'difftool -x cat' '
 294        diff=$(git difftool --no-prompt -x cat branch) &&
 295        test "$diff" = branch"$LF"master
 296'
 297
 298test_expect_success PERL 'difftool --extcmd echo arg1' '
 299        diff=$(git difftool --no-prompt --extcmd sh\ -c\ \"echo\ \$1\" branch) &&
 300        test "$diff" = file
 301'
 302
 303test_expect_success PERL 'difftool --extcmd cat arg1' '
 304        diff=$(git difftool --no-prompt --extcmd sh\ -c\ \"cat\ \$1\" branch) &&
 305        test "$diff" = master
 306'
 307
 308test_expect_success PERL 'difftool --extcmd cat arg2' '
 309        diff=$(git difftool --no-prompt --extcmd sh\ -c\ \"cat\ \$2\" branch) &&
 310        test "$diff" = branch
 311'
 312
 313# Create a second file on master and a different version on branch
 314test_expect_success PERL 'setup with 2 files different' '
 315        echo m2 >file2 &&
 316        git add file2 &&
 317        git commit -m "added file2" &&
 318
 319        git checkout branch &&
 320        echo br2 >file2 &&
 321        git add file2 &&
 322        git commit -a -m "branch changed file2" &&
 323        git checkout master
 324'
 325
 326test_expect_success PERL 'say no to the first file' '
 327        diff=$( (echo n; echo) | git difftool -x cat branch ) &&
 328
 329        echo "$diff" | stdin_contains m2 &&
 330        echo "$diff" | stdin_contains br2 &&
 331        echo "$diff" | stdin_doesnot_contain master &&
 332        echo "$diff" | stdin_doesnot_contain branch
 333'
 334
 335test_expect_success PERL 'say no to the second file' '
 336        diff=$( (echo; echo n) | git difftool -x cat branch ) &&
 337
 338        echo "$diff" | stdin_contains master &&
 339        echo "$diff" | stdin_contains branch &&
 340        echo "$diff" | stdin_doesnot_contain m2 &&
 341        echo "$diff" | stdin_doesnot_contain br2
 342'
 343
 344test_expect_success PERL 'difftool --tool-help' '
 345        tool_help=$(git difftool --tool-help) &&
 346        echo "$tool_help" | stdin_contains tool
 347'
 348
 349test_expect_success PERL 'setup change in subdirectory' '
 350        git checkout master &&
 351        mkdir sub &&
 352        echo master >sub/sub &&
 353        git add sub/sub &&
 354        git commit -m "added sub/sub" &&
 355        echo test >>file &&
 356        echo test >>sub/sub &&
 357        git add . &&
 358        git commit -m "modified both"
 359'
 360
 361test_expect_success PERL 'difftool -d' '
 362        diff=$(git difftool -d --extcmd ls branch) &&
 363        echo "$diff" | stdin_contains sub &&
 364        echo "$diff" | stdin_contains file
 365'
 366
 367test_expect_success PERL 'difftool --dir-diff' '
 368        diff=$(git difftool --dir-diff --extcmd ls branch) &&
 369        echo "$diff" | stdin_contains sub &&
 370        echo "$diff" | stdin_contains file
 371'
 372
 373test_expect_success PERL 'difftool --dir-diff ignores --prompt' '
 374        diff=$(git difftool --dir-diff --prompt --extcmd ls branch) &&
 375        echo "$diff" | stdin_contains sub &&
 376        echo "$diff" | stdin_contains file
 377'
 378
 379test_expect_success PERL 'difftool --dir-diff from subdirectory' '
 380        (
 381                cd sub &&
 382                diff=$(git difftool --dir-diff --extcmd ls branch) &&
 383                echo "$diff" | stdin_contains sub &&
 384                echo "$diff" | stdin_contains file
 385        )
 386'
 387
 388test_done