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