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