07f2478c9bd97f6c40d048c901b1e3be2288adb3
   1#!/bin/sh
   2#
   3# Copyright (c) 2012 Felipe Contreras
   4#
   5
   6test_description='test bash completion'
   7
   8. ./lib-bash.sh
   9
  10complete ()
  11{
  12        # do nothing
  13        return 0
  14}
  15
  16# Be careful when updating this list:
  17#
  18# (1) The build tree may have build artifact from different branch, or
  19#     the user's $PATH may have a random executable that may begin
  20#     with "git-check" that are not part of the subcommands this build
  21#     will ship, e.g.  "check-ignore".  The tests for completion for
  22#     subcommand names tests how "check" is expanded; we limit the
  23#     possible candidates to "checkout" and "check-attr" to make sure
  24#     "check-attr", which is known by the filter function as a
  25#     subcommand to be thrown out, while excluding other random files
  26#     that happen to begin with "check" to avoid letting them get in
  27#     the way.
  28#
  29# (2) A test makes sure that common subcommands are included in the
  30#     completion for "git <TAB>", and a plumbing is excluded.  "add",
  31#     "filter-branch" and "ls-files" are listed for this.
  32
  33GIT_TESTING_COMMAND_COMPLETION='add checkout check-attr filter-branch ls-files'
  34
  35. "$GIT_BUILD_DIR/contrib/completion/git-completion.bash"
  36
  37# We don't need this function to actually join words or do anything special.
  38# Also, it's cleaner to avoid touching bash's internal completion variables.
  39# So let's override it with a minimal version for testing purposes.
  40_get_comp_words_by_ref ()
  41{
  42        while [ $# -gt 0 ]; do
  43                case "$1" in
  44                cur)
  45                        cur=${_words[_cword]}
  46                        ;;
  47                prev)
  48                        prev=${_words[_cword-1]}
  49                        ;;
  50                words)
  51                        words=("${_words[@]}")
  52                        ;;
  53                cword)
  54                        cword=$_cword
  55                        ;;
  56                esac
  57                shift
  58        done
  59}
  60
  61print_comp ()
  62{
  63        local IFS=$'\n'
  64        echo "${COMPREPLY[*]}" > out
  65}
  66
  67run_completion ()
  68{
  69        local -a COMPREPLY _words
  70        local _cword
  71        _words=( $1 )
  72        test "${1: -1}" = ' ' && _words[${#_words[@]}+1]=''
  73        (( _cword = ${#_words[@]} - 1 ))
  74        __git_wrap__git_main && print_comp
  75}
  76
  77# Test high-level completion
  78# Arguments are:
  79# 1: typed text so far (cur)
  80# 2: expected completion
  81test_completion ()
  82{
  83        if test $# -gt 1
  84        then
  85                printf '%s\n' "$2" >expected
  86        else
  87                sed -e 's/Z$//' >expected
  88        fi &&
  89        run_completion "$1" &&
  90        test_cmp expected out
  91}
  92
  93# Test __gitcomp.
  94# The first argument is the typed text so far (cur); the rest are
  95# passed to __gitcomp.  Expected output comes is read from the
  96# standard input, like test_completion().
  97test_gitcomp ()
  98{
  99        local -a COMPREPLY &&
 100        sed -e 's/Z$//' >expected &&
 101        cur="$1" &&
 102        shift &&
 103        __gitcomp "$@" &&
 104        print_comp &&
 105        test_cmp expected out
 106}
 107
 108# Test __gitcomp_nl
 109# Arguments are:
 110# 1: current word (cur)
 111# -: the rest are passed to __gitcomp_nl
 112test_gitcomp_nl ()
 113{
 114        local -a COMPREPLY &&
 115        sed -e 's/Z$//' >expected &&
 116        cur="$1" &&
 117        shift &&
 118        __gitcomp_nl "$@" &&
 119        print_comp &&
 120        test_cmp expected out
 121}
 122
 123invalid_variable_name='${foo.bar}'
 124
 125actual="$TRASH_DIRECTORY/actual"
 126
 127test_expect_success 'setup for __gitdir tests' '
 128        mkdir -p subdir/subsubdir &&
 129        git init otherrepo
 130'
 131
 132test_expect_success '__gitdir - from command line (through $__git_dir)' '
 133        echo "$TRASH_DIRECTORY/otherrepo/.git" >expected &&
 134        (
 135                __git_dir="$TRASH_DIRECTORY/otherrepo/.git" &&
 136                __gitdir >"$actual"
 137        ) &&
 138        test_cmp expected "$actual"
 139'
 140
 141test_expect_success '__gitdir - repo as argument' '
 142        echo "otherrepo/.git" >expected &&
 143        __gitdir "otherrepo" >"$actual" &&
 144        test_cmp expected "$actual"
 145'
 146
 147test_expect_success '__gitdir - remote as argument' '
 148        echo "remote" >expected &&
 149        __gitdir "remote" >"$actual" &&
 150        test_cmp expected "$actual"
 151'
 152
 153test_expect_success '__gitdir - .git directory in cwd' '
 154        echo ".git" >expected &&
 155        __gitdir >"$actual" &&
 156        test_cmp expected "$actual"
 157'
 158
 159test_expect_success '__gitdir - .git directory in parent' '
 160        echo "$(pwd -P)/.git" >expected &&
 161        (
 162                cd subdir/subsubdir &&
 163                __gitdir >"$actual"
 164        ) &&
 165        test_cmp expected "$actual"
 166'
 167
 168test_expect_success '__gitdir - cwd is a .git directory' '
 169        echo "." >expected &&
 170        (
 171                cd .git &&
 172                __gitdir >"$actual"
 173        ) &&
 174        test_cmp expected "$actual"
 175'
 176
 177test_expect_success '__gitdir - parent is a .git directory' '
 178        echo "$(pwd -P)/.git" >expected &&
 179        (
 180                cd .git/refs/heads &&
 181                __gitdir >"$actual"
 182        ) &&
 183        test_cmp expected "$actual"
 184'
 185
 186test_expect_success '__gitdir - $GIT_DIR set while .git directory in cwd' '
 187        echo "$TRASH_DIRECTORY/otherrepo/.git" >expected &&
 188        (
 189                GIT_DIR="$TRASH_DIRECTORY/otherrepo/.git" &&
 190                export GIT_DIR &&
 191                __gitdir >"$actual"
 192        ) &&
 193        test_cmp expected "$actual"
 194'
 195
 196test_expect_success '__gitdir - $GIT_DIR set while .git directory in parent' '
 197        echo "$TRASH_DIRECTORY/otherrepo/.git" >expected &&
 198        (
 199                GIT_DIR="$TRASH_DIRECTORY/otherrepo/.git" &&
 200                export GIT_DIR &&
 201                cd subdir &&
 202                __gitdir >"$actual"
 203        ) &&
 204        test_cmp expected "$actual"
 205'
 206
 207test_expect_success '__gitdir - non-existing $GIT_DIR' '
 208        (
 209                GIT_DIR="$TRASH_DIRECTORY/non-existing" &&
 210                export GIT_DIR &&
 211                test_must_fail __gitdir
 212        )
 213'
 214
 215function pwd_P_W () {
 216        if test_have_prereq MINGW
 217        then
 218                pwd -W
 219        else
 220                pwd -P
 221        fi
 222}
 223
 224test_expect_success '__gitdir - gitfile in cwd' '
 225        echo "$(pwd_P_W)/otherrepo/.git" >expected &&
 226        echo "gitdir: $(pwd_P_W)/otherrepo/.git" >subdir/.git &&
 227        test_when_finished "rm -f subdir/.git" &&
 228        (
 229                cd subdir &&
 230                __gitdir >"$actual"
 231        ) &&
 232        test_cmp expected "$actual"
 233'
 234
 235test_expect_success '__gitdir - gitfile in parent' '
 236        echo "$(pwd_P_W)/otherrepo/.git" >expected &&
 237        echo "gitdir: $(pwd_P_W)/otherrepo/.git" >subdir/.git &&
 238        test_when_finished "rm -f subdir/.git" &&
 239        (
 240                cd subdir/subsubdir &&
 241                __gitdir >"$actual"
 242        ) &&
 243        test_cmp expected "$actual"
 244'
 245
 246test_expect_success SYMLINKS '__gitdir - resulting path avoids symlinks' '
 247        echo "$(pwd -P)/otherrepo/.git" >expected &&
 248        mkdir otherrepo/dir &&
 249        test_when_finished "rm -rf otherrepo/dir" &&
 250        ln -s otherrepo/dir link &&
 251        test_when_finished "rm -f link" &&
 252        (
 253                cd link &&
 254                __gitdir >"$actual"
 255        ) &&
 256        test_cmp expected "$actual"
 257'
 258
 259test_expect_success '__gitdir - not a git repository' '
 260        (
 261                cd subdir/subsubdir &&
 262                GIT_CEILING_DIRECTORIES="$TRASH_DIRECTORY" &&
 263                export GIT_CEILING_DIRECTORIES &&
 264                test_must_fail __gitdir
 265        )
 266'
 267
 268test_expect_success '__gitcomp - trailing space - options' '
 269        test_gitcomp "--re" "--dry-run --reuse-message= --reedit-message=
 270                --reset-author" <<-EOF
 271        --reuse-message=Z
 272        --reedit-message=Z
 273        --reset-author Z
 274        EOF
 275'
 276
 277test_expect_success '__gitcomp - trailing space - config keys' '
 278        test_gitcomp "br" "branch. branch.autosetupmerge
 279                branch.autosetuprebase browser." <<-\EOF
 280        branch.Z
 281        branch.autosetupmerge Z
 282        branch.autosetuprebase Z
 283        browser.Z
 284        EOF
 285'
 286
 287test_expect_success '__gitcomp - option parameter' '
 288        test_gitcomp "--strategy=re" "octopus ours recursive resolve subtree" \
 289                "" "re" <<-\EOF
 290        recursive Z
 291        resolve Z
 292        EOF
 293'
 294
 295test_expect_success '__gitcomp - prefix' '
 296        test_gitcomp "branch.me" "remote merge mergeoptions rebase" \
 297                "branch.maint." "me" <<-\EOF
 298        branch.maint.merge Z
 299        branch.maint.mergeoptions Z
 300        EOF
 301'
 302
 303test_expect_success '__gitcomp - suffix' '
 304        test_gitcomp "branch.me" "master maint next pu" "branch." \
 305                "ma" "." <<-\EOF
 306        branch.master.Z
 307        branch.maint.Z
 308        EOF
 309'
 310
 311test_expect_success '__gitcomp - doesnt fail because of invalid variable name' '
 312        __gitcomp "$invalid_variable_name"
 313'
 314
 315read -r -d "" refs <<-\EOF
 316maint
 317master
 318next
 319pu
 320EOF
 321
 322test_expect_success '__gitcomp_nl - trailing space' '
 323        test_gitcomp_nl "m" "$refs" <<-EOF
 324        maint Z
 325        master Z
 326        EOF
 327'
 328
 329test_expect_success '__gitcomp_nl - prefix' '
 330        test_gitcomp_nl "--fixup=m" "$refs" "--fixup=" "m" <<-EOF
 331        --fixup=maint Z
 332        --fixup=master Z
 333        EOF
 334'
 335
 336test_expect_success '__gitcomp_nl - suffix' '
 337        test_gitcomp_nl "branch.ma" "$refs" "branch." "ma" "." <<-\EOF
 338        branch.maint.Z
 339        branch.master.Z
 340        EOF
 341'
 342
 343test_expect_success '__gitcomp_nl - no suffix' '
 344        test_gitcomp_nl "ma" "$refs" "" "ma" "" <<-\EOF
 345        maintZ
 346        masterZ
 347        EOF
 348'
 349
 350test_expect_success '__gitcomp_nl - doesnt fail because of invalid variable name' '
 351        __gitcomp_nl "$invalid_variable_name"
 352'
 353
 354test_expect_success '__git_remotes - list remotes from $GIT_DIR/remotes and from config file' '
 355        cat >expect <<-EOF &&
 356        remote_from_file_1
 357        remote_from_file_2
 358        remote_in_config_1
 359        remote_in_config_2
 360        EOF
 361        test_when_finished "rm -rf .git/remotes" &&
 362        mkdir -p .git/remotes &&
 363        >.git/remotes/remote_from_file_1 &&
 364        >.git/remotes/remote_from_file_2 &&
 365        test_when_finished "git remote remove remote_in_config_1" &&
 366        git remote add remote_in_config_1 git://remote_1 &&
 367        test_when_finished "git remote remove remote_in_config_2" &&
 368        git remote add remote_in_config_2 git://remote_2 &&
 369        __git_remotes >actual &&
 370        test_cmp expect actual
 371'
 372
 373test_expect_success '__git_pretty_aliases' '
 374        cat >expect <<-EOF &&
 375        author
 376        hash
 377        EOF
 378        test_config pretty.author "%an %ae" &&
 379        test_config pretty.hash %H &&
 380        __git_pretty_aliases >actual &&
 381        test_cmp expect actual
 382'
 383
 384test_expect_success '__git_aliases' '
 385        cat >expect <<-EOF &&
 386        ci
 387        co
 388        EOF
 389        test_config alias.ci commit &&
 390        test_config alias.co checkout &&
 391        __git_aliases >actual &&
 392        test_cmp expect actual
 393'
 394
 395test_expect_success 'basic' '
 396        run_completion "git " &&
 397        # built-in
 398        grep -q "^add \$" out &&
 399        # script
 400        grep -q "^filter-branch \$" out &&
 401        # plumbing
 402        ! grep -q "^ls-files \$" out &&
 403
 404        run_completion "git f" &&
 405        ! grep -q -v "^f" out
 406'
 407
 408test_expect_success 'double dash "git" itself' '
 409        test_completion "git --" <<-\EOF
 410        --paginate Z
 411        --no-pager Z
 412        --git-dir=
 413        --bare Z
 414        --version Z
 415        --exec-path Z
 416        --exec-path=
 417        --html-path Z
 418        --man-path Z
 419        --info-path Z
 420        --work-tree=
 421        --namespace=
 422        --no-replace-objects Z
 423        --help Z
 424        EOF
 425'
 426
 427test_expect_success 'double dash "git checkout"' '
 428        test_completion "git checkout --" <<-\EOF
 429        --quiet Z
 430        --ours Z
 431        --theirs Z
 432        --track Z
 433        --no-track Z
 434        --merge Z
 435        --conflict=
 436        --orphan Z
 437        --patch Z
 438        EOF
 439'
 440
 441test_expect_success 'general options' '
 442        test_completion "git --ver" "--version " &&
 443        test_completion "git --hel" "--help " &&
 444        test_completion "git --exe" <<-\EOF &&
 445        --exec-path Z
 446        --exec-path=
 447        EOF
 448        test_completion "git --htm" "--html-path " &&
 449        test_completion "git --pag" "--paginate " &&
 450        test_completion "git --no-p" "--no-pager " &&
 451        test_completion "git --git" "--git-dir=" &&
 452        test_completion "git --wor" "--work-tree=" &&
 453        test_completion "git --nam" "--namespace=" &&
 454        test_completion "git --bar" "--bare " &&
 455        test_completion "git --inf" "--info-path " &&
 456        test_completion "git --no-r" "--no-replace-objects "
 457'
 458
 459test_expect_success 'general options plus command' '
 460        test_completion "git --version check" "checkout " &&
 461        test_completion "git --paginate check" "checkout " &&
 462        test_completion "git --git-dir=foo check" "checkout " &&
 463        test_completion "git --bare check" "checkout " &&
 464        test_completion "git --exec-path=foo check" "checkout " &&
 465        test_completion "git --html-path check" "checkout " &&
 466        test_completion "git --no-pager check" "checkout " &&
 467        test_completion "git --work-tree=foo check" "checkout " &&
 468        test_completion "git --namespace=foo check" "checkout " &&
 469        test_completion "git --paginate check" "checkout " &&
 470        test_completion "git --info-path check" "checkout " &&
 471        test_completion "git --no-replace-objects check" "checkout "
 472'
 473
 474test_expect_success 'git --help completion' '
 475        test_completion "git --help ad" "add " &&
 476        test_completion "git --help core" "core-tutorial "
 477'
 478
 479test_expect_success 'setup for ref completion' '
 480        echo content >file1 &&
 481        echo more >file2 &&
 482        git add . &&
 483        git commit -m one &&
 484        git branch mybranch &&
 485        git tag mytag
 486'
 487
 488test_expect_success 'checkout completes ref names' '
 489        test_completion "git checkout m" <<-\EOF
 490        master Z
 491        mybranch Z
 492        mytag Z
 493        EOF
 494'
 495
 496test_expect_success 'show completes all refs' '
 497        test_completion "git show m" <<-\EOF
 498        master Z
 499        mybranch Z
 500        mytag Z
 501        EOF
 502'
 503
 504test_expect_success '<ref>: completes paths' '
 505        test_completion "git show mytag:f" <<-\EOF
 506        file1 Z
 507        file2 Z
 508        EOF
 509'
 510
 511test_expect_success 'complete tree filename with spaces' '
 512        echo content >"name with spaces" &&
 513        git add . &&
 514        git commit -m spaces &&
 515        test_completion "git show HEAD:nam" <<-\EOF
 516        name with spaces Z
 517        EOF
 518'
 519
 520test_expect_success 'complete tree filename with metacharacters' '
 521        echo content >"name with \${meta}" &&
 522        git add . &&
 523        git commit -m meta &&
 524        test_completion "git show HEAD:nam" <<-\EOF
 525        name with ${meta} Z
 526        name with spaces Z
 527        EOF
 528'
 529
 530test_expect_success 'send-email' '
 531        test_completion "git send-email --cov" "--cover-letter " &&
 532        test_completion "git send-email ma" "master "
 533'
 534
 535test_expect_success 'complete files' '
 536        git init tmp && cd tmp &&
 537        test_when_finished "cd .. && rm -rf tmp" &&
 538
 539        echo "expected" > .gitignore &&
 540        echo "out" >> .gitignore &&
 541
 542        git add .gitignore &&
 543        test_completion "git commit " ".gitignore" &&
 544
 545        git commit -m ignore &&
 546
 547        touch new &&
 548        test_completion "git add " "new" &&
 549
 550        git add new &&
 551        git commit -a -m new &&
 552        test_completion "git add " "" &&
 553
 554        git mv new modified &&
 555        echo modify > modified &&
 556        test_completion "git add " "modified" &&
 557
 558        touch untracked &&
 559
 560        : TODO .gitignore should not be here &&
 561        test_completion "git rm " <<-\EOF &&
 562        .gitignore
 563        modified
 564        EOF
 565
 566        test_completion "git clean " "untracked" &&
 567
 568        : TODO .gitignore should not be here &&
 569        test_completion "git mv " <<-\EOF &&
 570        .gitignore
 571        modified
 572        EOF
 573
 574        mkdir dir &&
 575        touch dir/file-in-dir &&
 576        git add dir/file-in-dir &&
 577        git commit -m dir &&
 578
 579        mkdir untracked-dir &&
 580
 581        : TODO .gitignore should not be here &&
 582        test_completion "git mv modified " <<-\EOF &&
 583        .gitignore
 584        dir
 585        modified
 586        untracked
 587        untracked-dir
 588        EOF
 589
 590        test_completion "git commit " "modified" &&
 591
 592        : TODO .gitignore should not be here &&
 593        test_completion "git ls-files " <<-\EOF &&
 594        .gitignore
 595        dir
 596        modified
 597        EOF
 598
 599        touch momified &&
 600        test_completion "git add mom" "momified"
 601'
 602
 603test_expect_success "completion uses <cmd> completion for alias: !sh -c 'git <cmd> ...'" '
 604        test_config alias.co "!sh -c '"'"'git checkout ...'"'"'" &&
 605        test_completion "git co m" <<-\EOF
 606        master Z
 607        mybranch Z
 608        mytag Z
 609        EOF
 610'
 611
 612test_expect_success 'completion uses <cmd> completion for alias: !f () { VAR=val git <cmd> ... }' '
 613        test_config alias.co "!f () { VAR=val git checkout ... ; } f" &&
 614        test_completion "git co m" <<-\EOF
 615        master Z
 616        mybranch Z
 617        mytag Z
 618        EOF
 619'
 620
 621test_expect_success 'completion used <cmd> completion for alias: !f() { : git <cmd> ; ... }' '
 622        test_config alias.co "!f() { : git checkout ; if ... } f" &&
 623        test_completion "git co m" <<-\EOF
 624        master Z
 625        mybranch Z
 626        mytag Z
 627        EOF
 628'
 629
 630test_expect_failure 'complete with tilde expansion' '
 631        git init tmp && cd tmp &&
 632        test_when_finished "cd .. && rm -rf tmp" &&
 633
 634        touch ~/tmp/file &&
 635
 636        test_completion "git add ~/tmp/" "~/tmp/file"
 637'
 638
 639test_done