bash_completion_test [plain text]
#!/bin/bash
THIS_DIR=`dirname "$0"`
SCRIPT="$1"
if [ -z "$SCRIPT" ]; then
SCRIPT="$THIS_DIR/bash_completion"
fi
if [ ! -r "$SCRIPT" ] || [ "$2" ]; then
echo "Usage: bash_completion_test [BASH_COMPLETION_PATHNAME]"
echo "Tests the specified \"bash_completion\" script,"
echo "defaulting to the one in the same directory as this test,"
echo "including checking it against the \"svn\" program found in the current PATH."
exit 1
fi
set -e shopt -s extglob
export LC_ALL=C
. "$SCRIPT"
get_completions() {
SVN_CMD="$1"
COMP_WORDS=("$@")
if [ $ COMP_CWORD=1
else
COMP_CWORD=$(($ fi
"_$SVN_CMD"
echo -n "${COMPREPLY[*]}"
}
fail() {
PREFIX="FAIL: "
for LINE in "$@"; do
echo "$PREFIX$LINE"
PREFIX=" "
done
TESTS_FAILED=1
false
}
includes() {
SVN_CMD="$1"
SVN_ARGS="$2"
EXPECTED_WORD="$3"
COMPLETIONS=`get_completions "$SVN_CMD" $SVN_ARGS`
if [[ "$EXPECTED_WORD" != @(${COMPLETIONS// /|}) ]]; then
fail "completions of \"$SVN_CMD $SVN_ARGS\" should include \"$EXPECTED_WORD\"" \
"(completions: $COMPLETIONS)"
fi
}
excludes() {
SVN_CMD="$1"
SVN_ARGS="$2"
EXPECTED_WORD="$3"
COMPLETIONS=`get_completions "$SVN_CMD" $SVN_ARGS`
if [[ "$EXPECTED_WORD" == @(${COMPLETIONS// /|}) ]]; then
fail "completions of \"$SVN_CMD $SVN_ARGS\" should exclude \"$EXPECTED_WORD\"" \
"(completions: $COMPLETIONS)"
fi
}
get_svn_subcommands() {
SVN_CMD="$1"
"$SVN_CMD" help |
sed -n -e '1,/^Available subcommands:$/d;/^$/q;p' |
tr -d ' )' | tr '(,' ' ' |
( while read SYNONYMS; do
for CMD in $SYNONYMS; do
if [ "$CMD" != "?" ]; then
for SYNONYM in $SYNONYMS; do
case $SYNONYM in
$CMD) ;;
$CMD*) CMD= ; break ;;
esac
done
if [ $CMD ]; then
echo $CMD
fi
fi
done
done
) |
sort
}
get_svn_options() {
SVN_CMD="$1"
SUBCMD="$2"
{ "$SVN_CMD" help "$SUBCMD" |
grep -v deprecated |
sed -n -e '1,/^\(Valid\|Global\) options:$/d;/^ -/!d' \
-e 's/\( ARG\)* * : .*//;p' |
tr -d '] ' | tr '[' '\n'
if [ "$SUBCMD" != "help" ] ; then
echo "-h"
echo "--help"
fi
} | sort
}
set +e TESTS_FAILED=
echo "Checking general completion"
includes svn "he" "help"
includes svn "" "help"
includes svn "" "--version"
for SVN_CMD in svn svnadmin svndumpfilter svnlook svnrdump svnsync; do
echo "Checking list of subcommands: $SVN_CMD"
HELP_SUBCMDS=`get_svn_subcommands "$SVN_CMD" | tr "\n" " "`
COMPLETION_SUBCMDS=`get_completions "$SVN_CMD" | tr " " "\n" | grep -v "^-" | sort | tr "\n" " "`
if [ "$HELP_SUBCMDS" != "$COMPLETION_SUBCMDS" ]; then
fail "non-option completions for \"$SVN_CMD\" != subcommands accepted" \
" (non-o. cmpl.: $COMPLETION_SUBCMDS)" \
" (help says: $HELP_SUBCMDS)"
fi
echo "Checking list of options for each subcommand"
for SUBCMD in $HELP_SUBCMDS; do
HELP_OPTIONS=`get_svn_options $SVN_CMD $SUBCMD | tr "\n" " "`
COMPLETION_OPTIONS=`get_completions $SVN_CMD $SUBCMD - | tr " " "\n" | sort | tr "\n" " "`
if [ "$HELP_OPTIONS" != "$COMPLETION_OPTIONS" ]; then
fail "completions for \"$SVN_CMD $SUBCMD -\" != options accepted" \
" (completions: $COMPLETION_OPTIONS)" \
" (help says: $HELP_OPTIONS)"
fi
done
done
echo "Checking rejection of synonyms"
excludes svn "diff -x -u -" "-x"
excludes svn "diff -x -u --e" "--extensions"
excludes svn "diff --extensions -u -" "--extensions"
excludes svn "diff --extensions -u -" "-x"
excludes svn "diff --extensions=-u -" "-x"
if [ $TESTS_FAILED ]; then
echo "FAILURE: at least one bash_completion test failed."
else
echo "All bash_completion tests passed."
fi