173cd740ee7f75582ecbaea5c2874d6cd8b489ae
   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        if test $# -gt 1
  64        then
  65                printf '%s\n' "$2" >expected
  66        else
  67                sed -e 's/Z$//' >expected
  68        fi &&
  69        run_completion "$1" &&
  70        test_cmp expected out
  71}
  72
  73newline=$'\n'
  74
  75test_expect_success '__gitcomp - trailing space - options' '
  76        sed -e "s/Z$//" >expected <<-\EOF &&
  77        --reuse-message=Z
  78        --reedit-message=Z
  79        --reset-author Z
  80        EOF
  81        (
  82                local -a COMPREPLY &&
  83                cur="--re" &&
  84                __gitcomp "--dry-run --reuse-message= --reedit-message=
  85                                --reset-author" &&
  86                IFS="$newline" &&
  87                echo "${COMPREPLY[*]}" > out
  88        ) &&
  89        test_cmp expected out
  90'
  91
  92test_expect_success '__gitcomp - trailing space - config keys' '
  93        sed -e "s/Z$//" >expected <<-\EOF &&
  94        branch.Z
  95        branch.autosetupmerge Z
  96        branch.autosetuprebase Z
  97        browser.Z
  98        EOF
  99        (
 100                local -a COMPREPLY &&
 101                cur="br" &&
 102                __gitcomp "branch. branch.autosetupmerge
 103                                branch.autosetuprebase browser." &&
 104                IFS="$newline" &&
 105                echo "${COMPREPLY[*]}" > out
 106        ) &&
 107        test_cmp expected out
 108'
 109
 110test_expect_success '__gitcomp - option parameter' '
 111        sed -e "s/Z$//" >expected <<-\EOF &&
 112        recursive Z
 113        resolve Z
 114        EOF
 115        (
 116                local -a COMPREPLY &&
 117                cur="--strategy=re" &&
 118                __gitcomp "octopus ours recursive resolve subtree
 119                        " "" "re" &&
 120                IFS="$newline" &&
 121                echo "${COMPREPLY[*]}" > out
 122        ) &&
 123        test_cmp expected out
 124'
 125
 126test_expect_success '__gitcomp - prefix' '
 127        sed -e "s/Z$//" >expected <<-\EOF &&
 128        branch.maint.merge Z
 129        branch.maint.mergeoptions Z
 130        EOF
 131        (
 132                local -a COMPREPLY &&
 133                cur="branch.me" &&
 134                __gitcomp "remote merge mergeoptions rebase
 135                        " "branch.maint." "me" &&
 136                IFS="$newline" &&
 137                echo "${COMPREPLY[*]}" > out
 138        ) &&
 139        test_cmp expected out
 140'
 141
 142test_expect_success '__gitcomp - suffix' '
 143        sed -e "s/Z$//" >expected <<-\EOF &&
 144        branch.master.Z
 145        branch.maint.Z
 146        EOF
 147        (
 148                local -a COMPREPLY &&
 149                cur="branch.me" &&
 150                __gitcomp "master maint next pu
 151                        " "branch." "ma" "." &&
 152                IFS="$newline" &&
 153                echo "${COMPREPLY[*]}" > out
 154        ) &&
 155        test_cmp expected out
 156'
 157
 158test_expect_success 'basic' '
 159        run_completion "git \"\"" &&
 160        # built-in
 161        grep -q "^add \$" out &&
 162        # script
 163        grep -q "^filter-branch \$" out &&
 164        # plumbing
 165        ! grep -q "^ls-files \$" out &&
 166
 167        run_completion "git f" &&
 168        ! grep -q -v "^f" out
 169'
 170
 171test_expect_success 'double dash "git" itself' '
 172        test_completion "git --" <<-\EOF
 173        --paginate Z
 174        --no-pager Z
 175        --git-dir=
 176        --bare Z
 177        --version Z
 178        --exec-path Z
 179        --exec-path=
 180        --html-path Z
 181        --info-path Z
 182        --work-tree=
 183        --namespace=
 184        --no-replace-objects Z
 185        --help Z
 186        EOF
 187'
 188
 189test_expect_success 'double dash "git checkout"' '
 190        test_completion "git checkout --" <<-\EOF
 191        --quiet Z
 192        --ours Z
 193        --theirs Z
 194        --track Z
 195        --no-track Z
 196        --merge Z
 197        --conflict=
 198        --orphan Z
 199        --patch Z
 200        EOF
 201'
 202
 203test_expect_success 'general options' '
 204        test_completion "git --ver" "--version " &&
 205        test_completion "git --hel" "--help " &&
 206        test_completion "git --exe" <<-\EOF &&
 207        --exec-path Z
 208        --exec-path=
 209        EOF
 210        test_completion "git --htm" "--html-path " &&
 211        test_completion "git --pag" "--paginate " &&
 212        test_completion "git --no-p" "--no-pager " &&
 213        test_completion "git --git" "--git-dir=" &&
 214        test_completion "git --wor" "--work-tree=" &&
 215        test_completion "git --nam" "--namespace=" &&
 216        test_completion "git --bar" "--bare " &&
 217        test_completion "git --inf" "--info-path " &&
 218        test_completion "git --no-r" "--no-replace-objects "
 219'
 220
 221test_expect_success 'general options plus command' '
 222        test_completion "git --version check" "checkout " &&
 223        test_completion "git --paginate check" "checkout " &&
 224        test_completion "git --git-dir=foo check" "checkout " &&
 225        test_completion "git --bare check" "checkout " &&
 226        test_completion "git --help des" "describe " &&
 227        test_completion "git --exec-path=foo check" "checkout " &&
 228        test_completion "git --html-path check" "checkout " &&
 229        test_completion "git --no-pager check" "checkout " &&
 230        test_completion "git --work-tree=foo check" "checkout " &&
 231        test_completion "git --namespace=foo check" "checkout " &&
 232        test_completion "git --paginate check" "checkout " &&
 233        test_completion "git --info-path check" "checkout " &&
 234        test_completion "git --no-replace-objects check" "checkout "
 235'
 236
 237test_expect_success 'setup for ref completion' '
 238        echo content >file1 &&
 239        echo more >file2 &&
 240        git add . &&
 241        git commit -m one &&
 242        git branch mybranch &&
 243        git tag mytag
 244'
 245
 246test_expect_success 'checkout completes ref names' '
 247        test_completion "git checkout m" <<-\EOF
 248        master Z
 249        mybranch Z
 250        mytag Z
 251        EOF
 252'
 253
 254test_expect_success 'show completes all refs' '
 255        test_completion "git show m" <<-\EOF
 256        master Z
 257        mybranch Z
 258        mytag Z
 259        EOF
 260'
 261
 262test_expect_success '<ref>: completes paths' '
 263        test_completion "git show mytag:f" <<-\EOF
 264        file1 Z
 265        file2 Z
 266        EOF
 267'
 268
 269test_expect_success 'complete tree filename with spaces' '
 270        echo content >"name with spaces" &&
 271        git add . &&
 272        git commit -m spaces &&
 273        test_completion "git show HEAD:nam" <<-\EOF
 274        name with spaces Z
 275        EOF
 276'
 277
 278test_expect_failure 'complete tree filename with metacharacters' '
 279        echo content >"name with \${meta}" &&
 280        git add . &&
 281        git commit -m meta &&
 282        test_completion "git show HEAD:nam" <<-\EOF
 283        name with ${meta} Z
 284        name with spaces Z
 285        EOF
 286'
 287
 288test_done