5c06709ea0601a26485c8503b4a91e645ec18226
   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. "$GIT_BUILD_DIR/contrib/completion/git-completion.bash"
  17
  18# We don't need this function to actually join words or do anything special.
  19# Also, it's cleaner to avoid touching bash's internal completion variables.
  20# So let's override it with a minimal version for testing purposes.
  21_get_comp_words_by_ref ()
  22{
  23        while [ $# -gt 0 ]; do
  24                case "$1" in
  25                cur)
  26                        cur=${_words[_cword]}
  27                        ;;
  28                prev)
  29                        prev=${_words[_cword-1]}
  30                        ;;
  31                words)
  32                        words=("${_words[@]}")
  33                        ;;
  34                cword)
  35                        cword=$_cword
  36                        ;;
  37                esac
  38                shift
  39        done
  40}
  41
  42print_comp ()
  43{
  44        local IFS=$'\n'
  45        echo "${COMPREPLY[*]}" > out
  46}
  47
  48run_completion ()
  49{
  50        local -a COMPREPLY _words
  51        local _cword
  52        _words=( $1 )
  53        (( _cword = ${#_words[@]} - 1 ))
  54        __git_wrap__git_main && print_comp
  55}
  56
  57# Test high-level completion
  58# Arguments are:
  59# 1: typed text so far (cur)
  60# 2: expected completion
  61test_completion ()
  62{
  63        test $# -gt 1 && echo "$2" > expected
  64        run_completion "$1" &&
  65        test_cmp expected out
  66}
  67
  68# Like test_completion, but reads expectation from stdin,
  69# which is convenient when it is multiline. We also process "_" into
  70# spaces to make test vectors more readable.
  71test_completion_long ()
  72{
  73        tr _ " " >expected &&
  74        test_completion "$1"
  75}
  76
  77newline=$'\n'
  78
  79test_expect_success '__gitcomp - trailing space - options' '
  80        sed -e "s/Z$//" >expected <<-\EOF &&
  81        --reuse-message=Z
  82        --reedit-message=Z
  83        --reset-author Z
  84        EOF
  85        (
  86                local -a COMPREPLY &&
  87                cur="--re" &&
  88                __gitcomp "--dry-run --reuse-message= --reedit-message=
  89                                --reset-author" &&
  90                IFS="$newline" &&
  91                echo "${COMPREPLY[*]}" > out
  92        ) &&
  93        test_cmp expected out
  94'
  95
  96test_expect_success '__gitcomp - trailing space - config keys' '
  97        sed -e "s/Z$//" >expected <<-\EOF &&
  98        branch.Z
  99        branch.autosetupmerge Z
 100        branch.autosetuprebase Z
 101        browser.Z
 102        EOF
 103        (
 104                local -a COMPREPLY &&
 105                cur="br" &&
 106                __gitcomp "branch. branch.autosetupmerge
 107                                branch.autosetuprebase browser." &&
 108                IFS="$newline" &&
 109                echo "${COMPREPLY[*]}" > out
 110        ) &&
 111        test_cmp expected out
 112'
 113
 114test_expect_success '__gitcomp - option parameter' '
 115        sed -e "s/Z$//" >expected <<-\EOF &&
 116        recursive Z
 117        resolve Z
 118        EOF
 119        (
 120                local -a COMPREPLY &&
 121                cur="--strategy=re" &&
 122                __gitcomp "octopus ours recursive resolve subtree
 123                        " "" "re" &&
 124                IFS="$newline" &&
 125                echo "${COMPREPLY[*]}" > out
 126        ) &&
 127        test_cmp expected out
 128'
 129
 130test_expect_success '__gitcomp - prefix' '
 131        sed -e "s/Z$//" >expected <<-\EOF &&
 132        branch.maint.merge Z
 133        branch.maint.mergeoptions Z
 134        EOF
 135        (
 136                local -a COMPREPLY &&
 137                cur="branch.me" &&
 138                __gitcomp "remote merge mergeoptions rebase
 139                        " "branch.maint." "me" &&
 140                IFS="$newline" &&
 141                echo "${COMPREPLY[*]}" > out
 142        ) &&
 143        test_cmp expected out
 144'
 145
 146test_expect_success '__gitcomp - suffix' '
 147        sed -e "s/Z$//" >expected <<-\EOF &&
 148        branch.master.Z
 149        branch.maint.Z
 150        EOF
 151        (
 152                local -a COMPREPLY &&
 153                cur="branch.me" &&
 154                __gitcomp "master maint next pu
 155                        " "branch." "ma" "." &&
 156                IFS="$newline" &&
 157                echo "${COMPREPLY[*]}" > out
 158        ) &&
 159        test_cmp expected out
 160'
 161
 162test_expect_success 'basic' '
 163        run_completion "git \"\"" &&
 164        # built-in
 165        grep -q "^add \$" out &&
 166        # script
 167        grep -q "^filter-branch \$" out &&
 168        # plumbing
 169        ! grep -q "^ls-files \$" out &&
 170
 171        run_completion "git f" &&
 172        ! grep -q -v "^f" out
 173'
 174
 175test_expect_success 'double dash "git" itself' '
 176        sed -e "s/Z$//" >expected <<-\EOF &&
 177        --paginate Z
 178        --no-pager Z
 179        --git-dir=
 180        --bare Z
 181        --version Z
 182        --exec-path Z
 183        --exec-path=
 184        --html-path Z
 185        --info-path Z
 186        --work-tree=
 187        --namespace=
 188        --no-replace-objects Z
 189        --help Z
 190        EOF
 191        test_completion "git --"
 192'
 193
 194test_expect_success 'double dash "git checkout"' '
 195        sed -e "s/Z$//" >expected <<-\EOF &&
 196        --quiet Z
 197        --ours Z
 198        --theirs Z
 199        --track Z
 200        --no-track Z
 201        --merge Z
 202        --conflict=
 203        --orphan Z
 204        --patch Z
 205        EOF
 206        test_completion "git checkout --"
 207'
 208
 209test_expect_success 'general options' '
 210        test_completion "git --ver" "--version " &&
 211        test_completion "git --hel" "--help " &&
 212        sed -e "s/Z$//" >expected <<-\EOF &&
 213        --exec-path Z
 214        --exec-path=
 215        EOF
 216        test_completion "git --exe" &&
 217        test_completion "git --htm" "--html-path " &&
 218        test_completion "git --pag" "--paginate " &&
 219        test_completion "git --no-p" "--no-pager " &&
 220        test_completion "git --git" "--git-dir=" &&
 221        test_completion "git --wor" "--work-tree=" &&
 222        test_completion "git --nam" "--namespace=" &&
 223        test_completion "git --bar" "--bare " &&
 224        test_completion "git --inf" "--info-path " &&
 225        test_completion "git --no-r" "--no-replace-objects "
 226'
 227
 228test_expect_success 'general options plus command' '
 229        test_completion "git --version check" "checkout " &&
 230        test_completion "git --paginate check" "checkout " &&
 231        test_completion "git --git-dir=foo check" "checkout " &&
 232        test_completion "git --bare check" "checkout " &&
 233        test_completion "git --help des" "describe " &&
 234        test_completion "git --exec-path=foo check" "checkout " &&
 235        test_completion "git --html-path check" "checkout " &&
 236        test_completion "git --no-pager check" "checkout " &&
 237        test_completion "git --work-tree=foo check" "checkout " &&
 238        test_completion "git --namespace=foo check" "checkout " &&
 239        test_completion "git --paginate check" "checkout " &&
 240        test_completion "git --info-path check" "checkout " &&
 241        test_completion "git --no-replace-objects check" "checkout "
 242'
 243
 244test_expect_success 'setup for ref completion' '
 245        echo content >file1 &&
 246        echo more >file2 &&
 247        git add . &&
 248        git commit -m one &&
 249        git branch mybranch &&
 250        git tag mytag
 251'
 252
 253test_expect_success 'checkout completes ref names' '
 254        test_completion_long "git checkout m" <<-\EOF
 255        master_
 256        mybranch_
 257        mytag_
 258        EOF
 259'
 260
 261test_expect_success 'show completes all refs' '
 262        test_completion_long "git show m" <<-\EOF
 263        master_
 264        mybranch_
 265        mytag_
 266        EOF
 267'
 268
 269test_expect_success '<ref>: completes paths' '
 270        test_completion_long "git show mytag:f" <<-\EOF
 271        file1_
 272        file2_
 273        EOF
 274'
 275
 276test_expect_success 'complete tree filename with spaces' '
 277        echo content >"name with spaces" &&
 278        git add . &&
 279        git commit -m spaces &&
 280        test_completion_long "git show HEAD:nam" <<-\EOF
 281        name with spaces_
 282        EOF
 283'
 284
 285test_expect_failure 'complete tree filename with metacharacters' '
 286        echo content >"name with \${meta}" &&
 287        git add . &&
 288        git commit -m meta &&
 289        test_completion_long "git show HEAD:nam" <<-\EOF
 290        name with ${meta}_
 291        name with spaces_
 292        EOF
 293'
 294
 295test_done