From 5c293a6be48d2e2ab4516aa113bb340b5cf0a12a Mon Sep 17 00:00:00 2001 From: Felipe Contreras Date: Thu, 12 Apr 2012 00:57:03 +0300 Subject: tests: add initial bash completion tests Signed-off-by: Felipe Contreras Signed-off-by: Junio C Hamano --- t/t9902-completion.sh | 120 ++++++++++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 120 insertions(+) create mode 100755 t/t9902-completion.sh (limited to 't/t9902-completion.sh') diff --git a/t/t9902-completion.sh b/t/t9902-completion.sh new file mode 100755 index 0000000000..a6eaa15c1c --- /dev/null +++ b/t/t9902-completion.sh @@ -0,0 +1,120 @@ +#!/bin/sh +# +# Copyright (c) 2012 Felipe Contreras +# + +if test -n "$BASH" && test -z "$POSIXLY_CORRECT"; then + # we are in full-on bash mode + true +elif type bash >/dev/null 2>&1; then + # execute in full-on bash mode + unset POSIXLY_CORRECT + exec bash "$0" "$@" +else + echo '1..0 #SKIP skipping bash completion tests; bash not available' + exit 0 +fi + +test_description='test bash completion' + +. ./test-lib.sh + +complete () +{ + # do nothing + return 0 +} + +. "$GIT_BUILD_DIR/contrib/completion/git-completion.bash" + +# We don't need this function to actually join words or do anything special. +# Also, it's cleaner to avoid touching bash's internal completion variables. +# So let's override it with a minimal version for testing purposes. +_get_comp_words_by_ref () +{ + while [ $# -gt 0 ]; do + case "$1" in + cur) + cur=${_words[_cword]} + ;; + prev) + prev=${_words[_cword-1]} + ;; + words) + words=("${_words[@]}") + ;; + cword) + cword=$_cword + ;; + esac + shift + done +} + +print_comp () +{ + local IFS=$'\n' + echo "${COMPREPLY[*]}" > out +} + +run_completion () +{ + local -a COMPREPLY _words + local _cword + _words=( $1 ) + (( _cword = ${#_words[@]} - 1 )) + _git && print_comp +} + +test_completion () +{ + test $# -gt 1 && echo "$2" > expected + run_completion "$@" && + test_cmp expected out +} + +test_expect_success 'basic' ' + run_completion "git \"\"" && + # built-in + grep -q "^add \$" out && + # script + grep -q "^filter-branch \$" out && + # plumbing + ! grep -q "^ls-files \$" out && + + run_completion "git f" && + ! grep -q -v "^f" out +' + +test_expect_success 'double dash "git" itself' ' + sed -e "s/Z$//" >expected <<-\EOF && + --paginate Z + --no-pager Z + --git-dir= + --bare Z + --version Z + --exec-path Z + --html-path Z + --work-tree= + --namespace= + --help Z + EOF + test_completion "git --" +' + +test_expect_success 'double dash "git checkout"' ' + sed -e "s/Z$//" >expected <<-\EOF && + --quiet Z + --ours Z + --theirs Z + --track Z + --no-track Z + --merge Z + --conflict= + --orphan Z + --patch Z + EOF + test_completion "git checkout --" +' + +test_done -- cgit v1.2.3 From 74a8c849f1257dde0c42feac95ba517bae625647 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?SZEDER=20G=C3=A1bor?= Date: Tue, 17 Apr 2012 12:27:15 +0200 Subject: tests: add tests for the __gitcomp() completion helper function MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit These tests check that trailing space, prefix, and suffix are added correctly. Signed-off-by: SZEDER Gábor Signed-off-by: Junio C Hamano --- t/t9902-completion.sh | 85 +++++++++++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 85 insertions(+) (limited to 't/t9902-completion.sh') diff --git a/t/t9902-completion.sh b/t/t9902-completion.sh index a6eaa15c1c..de1c3e91d3 100755 --- a/t/t9902-completion.sh +++ b/t/t9902-completion.sh @@ -73,6 +73,91 @@ test_completion () test_cmp expected out } +newline=$'\n' + +test_expect_success '__gitcomp - trailing space - options' ' + sed -e "s/Z$//" >expected <<-\EOF && + --reuse-message=Z + --reedit-message=Z + --reset-author Z + EOF + ( + local -a COMPREPLY && + cur="--re" && + __gitcomp "--dry-run --reuse-message= --reedit-message= + --reset-author" && + IFS="$newline" && + echo "${COMPREPLY[*]}" > out + ) && + test_cmp expected out +' + +test_expect_success '__gitcomp - trailing space - config keys' ' + sed -e "s/Z$//" >expected <<-\EOF && + branch.Z + branch.autosetupmerge Z + branch.autosetuprebase Z + browser.Z + EOF + ( + local -a COMPREPLY && + cur="br" && + __gitcomp "branch. branch.autosetupmerge + branch.autosetuprebase browser." && + IFS="$newline" && + echo "${COMPREPLY[*]}" > out + ) && + test_cmp expected out +' + +test_expect_success '__gitcomp - option parameter' ' + sed -e "s/Z$//" >expected <<-\EOF && + recursive Z + resolve Z + EOF + ( + local -a COMPREPLY && + cur="--strategy=re" && + __gitcomp "octopus ours recursive resolve subtree + " "" "re" && + IFS="$newline" && + echo "${COMPREPLY[*]}" > out + ) && + test_cmp expected out +' + +test_expect_success '__gitcomp - prefix' ' + sed -e "s/Z$//" >expected <<-\EOF && + branch.maint.merge Z + branch.maint.mergeoptions Z + EOF + ( + local -a COMPREPLY && + cur="branch.me" && + __gitcomp "remote merge mergeoptions rebase + " "branch.maint." "me" && + IFS="$newline" && + echo "${COMPREPLY[*]}" > out + ) && + test_cmp expected out +' + +test_expect_success '__gitcomp - suffix' ' + sed -e "s/Z$//" >expected <<-\EOF && + branch.master.Z + branch.maint.Z + EOF + ( + local -a COMPREPLY && + cur="branch.me" && + __gitcomp "master maint next pu + " "branch." "ma" "." && + IFS="$newline" && + echo "${COMPREPLY[*]}" > out + ) && + test_cmp expected out +' + test_expect_success 'basic' ' run_completion "git \"\"" && # built-in -- cgit v1.2.3 From 69ef3c0296e173535b73c7376469a722d54681f2 Mon Sep 17 00:00:00 2001 From: Felipe Contreras Date: Sun, 15 Apr 2012 22:44:18 +0300 Subject: completion: add missing general options And add relevant tests. Signed-off-by: Felipe Contreras Signed-off-by: Junio C Hamano --- t/t9902-completion.sh | 16 ++++++++++++++++ 1 file changed, 16 insertions(+) (limited to 't/t9902-completion.sh') diff --git a/t/t9902-completion.sh b/t/t9902-completion.sh index de1c3e91d3..0c0cd4100e 100755 --- a/t/t9902-completion.sh +++ b/t/t9902-completion.sh @@ -180,8 +180,10 @@ test_expect_success 'double dash "git" itself' ' --version Z --exec-path Z --html-path Z + --info-path Z --work-tree= --namespace= + --no-replace-objects Z --help Z EOF test_completion "git --" @@ -202,4 +204,18 @@ test_expect_success 'double dash "git checkout"' ' test_completion "git checkout --" ' +test_expect_success 'general options' ' + test_completion "git --ver" "--version " && + test_completion "git --hel" "--help " && + test_completion "git --exe" "--exec-path " && + test_completion "git --htm" "--html-path " && + test_completion "git --pag" "--paginate " && + test_completion "git --no-p" "--no-pager " && + test_completion "git --git" "--git-dir=" && + test_completion "git --wor" "--work-tree=" && + test_completion "git --nam" "--namespace=" && + test_completion "git --bar" "--bare " && + test_completion "git --inf" "--info-path " && + test_completion "git --no-r" "--no-replace-objects " +' test_done -- cgit v1.2.3 From 3ffcd08688a0e5f5de808c1077d10552d1f84e70 Mon Sep 17 00:00:00 2001 From: Jonathan Nieder Date: Sun, 15 Apr 2012 22:44:19 +0300 Subject: completion: avoid trailing space for --exec-path "--exec-path" looks to the completion script like an unambiguous successful completion, but it is wrong to emit a SP after it as if declaring that we are done with completion; the user could be trying to do git --exec-path; # print name of helper directory or git --exec-path=/path/to/alternative/helper/dir so the most helpful thing to do is to leave out the trailing space and leave it to the operator to type an equal sign or carriage return according to the situation. [fc: added tests] Cc: Andreas Schwab Reported-by: Felipe Contreras Signed-off-by: Jonathan Nieder Signed-off-by: Felipe Contreras Signed-off-by: Junio C Hamano --- t/t9902-completion.sh | 7 ++++++- 1 file changed, 6 insertions(+), 1 deletion(-) (limited to 't/t9902-completion.sh') diff --git a/t/t9902-completion.sh b/t/t9902-completion.sh index 0c0cd4100e..eb779d58a4 100755 --- a/t/t9902-completion.sh +++ b/t/t9902-completion.sh @@ -179,6 +179,7 @@ test_expect_success 'double dash "git" itself' ' --bare Z --version Z --exec-path Z + --exec-path= --html-path Z --info-path Z --work-tree= @@ -207,7 +208,11 @@ test_expect_success 'double dash "git checkout"' ' test_expect_success 'general options' ' test_completion "git --ver" "--version " && test_completion "git --hel" "--help " && - test_completion "git --exe" "--exec-path " && + sed -e "s/Z$//" >expected <<-\EOF && + --exec-path Z + --exec-path= + EOF + test_completion "git --exe" && test_completion "git --htm" "--html-path " && test_completion "git --pag" "--paginate " && test_completion "git --no-p" "--no-pager " && -- cgit v1.2.3 From 911d5da6f20e28cfe16ea539a4f1b86293a7280e Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?SZEDER=20G=C3=A1bor?= Date: Sun, 15 Apr 2012 22:44:20 +0300 Subject: completion: fix completion after 'git --option ' MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit The bash completion doesn't work when certain options to git itself are specified, e.g. 'git --no-pager ' errors out with error: invalid key: alias.--no-pager The main _git() completion function finds out the git command name by looping through all the words on the command line and searching for the first word that is not a known option for the git command. Unfortunately the list of known git options was not updated in a long time, and newer options are not skipped but mistaken for a git command. Such a misrecognized "command" is then passed to __git_aliased_command(), which in turn passes it to a 'git config' query, hence the error. Currently the following options are misrecognized for a git command: -c --no-pager --exec-path --html-path --man-path --info-path --no-replace-objects --work-tree= --namespace= To fix this we could just update the list of options to be skipped, but the same issue will likely arise, if the git command learns a new option in the future. Therefore, to make it more future proof against new options, this patch changes that loop to skip all option-looking words, i.e. words starting with a dash. We also have to handle the '-c' option specially, because it takes a configutation parameter in a separate word, which must be skipped, too. [fc: added tests] Signed-off-by: SZEDER Gábor Signed-off-by: Felipe Contreras Signed-off-by: Junio C Hamano --- t/t9902-completion.sh | 17 +++++++++++++++++ 1 file changed, 17 insertions(+) (limited to 't/t9902-completion.sh') diff --git a/t/t9902-completion.sh b/t/t9902-completion.sh index eb779d58a4..5bda6b6e18 100755 --- a/t/t9902-completion.sh +++ b/t/t9902-completion.sh @@ -223,4 +223,21 @@ test_expect_success 'general options' ' test_completion "git --inf" "--info-path " && test_completion "git --no-r" "--no-replace-objects " ' + +test_expect_success 'general options plus command' ' + test_completion "git --version check" "checkout " && + test_completion "git --paginate check" "checkout " && + test_completion "git --git-dir=foo check" "checkout " && + test_completion "git --bare check" "checkout " && + test_completion "git --help des" "describe " && + test_completion "git --exec-path=foo check" "checkout " && + test_completion "git --html-path check" "checkout " && + test_completion "git --no-pager check" "checkout " && + test_completion "git --work-tree=foo check" "checkout " && + test_completion "git --namespace=foo check" "checkout " && + test_completion "git --paginate check" "checkout " && + test_completion "git --info-path check" "checkout " && + test_completion "git --no-replace-objects check" "checkout " +' + test_done -- cgit v1.2.3