2
# Checks that the "_svn" function defined in the specified "bash_completion"
3
# script produces appropriate lists of completions for various incomplete svn
6
if [ ! -f "$1" ] || [ "$2" ]; then
7
echo "Usage: bash_completion_test BASH_COMPLETION_PATHNAME"
8
echo "Tests the specified \"bash_completion\" script,"
9
echo "including checking it against the \"svn\" program found in the current PATH."
13
set -e # Exit on error
16
# Execute the script which is to be tested.
19
# From the given incomplete svn command, print a space-separated list of
20
# possible completions of the last argument (or of an empty first argument
21
# if no subcommand is given).
22
# Usage: get_svn_completions [SVN-SUBCOMMAND [SVN-OPTION...]]
23
get_svn_completions() {
31
echo -n "${COMPREPLY[*]}"
34
# Print a failure message, record the failure, and return "false".
46
# Check that EXPECTED-WORD is among the completions of the last word in
47
# SVN-COMMAND. SVN-COMMAND is a single argument to this function, split
48
# into multiple arguments when passed to "get_svn_completions()".
49
# Usage: includes SVN-COMMAND EXPECTED-WORD
51
COMPLETIONS=`get_svn_completions $1`
52
if [[ "$2" != @(${COMPLETIONS// /|}) ]]; then
53
fail "completions of \"svn $1\" should include \"$2\"" \
54
"(completions: $COMPLETIONS)"
59
COMPLETIONS=`get_svn_completions $1`
60
if [[ "$2" == @(${COMPLETIONS// /|}) ]]; then
61
fail "completions of \"svn $1\" should exclude \"$2\"" \
62
"(completions: $COMPLETIONS)"
66
# Print the valid subcommands for "svn", one per line, sorted.
67
# Exclude any synonym that is just a truncation of its full name.
68
# Usage: get_svn_subcommands
69
get_svn_subcommands() {
71
# Find the relevant lines.
72
sed -n -e '1,/^Available subcommands:$/d;/^$/q;p' |
73
# Remove brackets and commas
74
tr -d ' )' | tr '(,' ' ' |
75
# Remove simple abbreviations
76
( while read SYNONYMS; do
77
for CMD in $SYNONYMS; do
78
for SYNONYM in $SYNONYMS; do
79
if [ $CMD != "?" ]; then
82
$CMD*) CMD= ; break ;;
95
# Print the valid option switches for "svn SUBCMD", one per line, sorted.
96
# Usage: get_svn_options SUBCMD
99
# Find the relevant lines; remove "arg" and description.
100
sed -n -e '1,/^Valid options:$/d;/^ -/!d' \
101
-e 's/\( arg\)* * : .*//;p' |
102
# Remove brackets; put each word on its own line.
103
tr -d '] ' | tr '[' '\n'
104
# The following options are always accepted but not listed in the help
113
set +e # Do not exit on error
116
echo "Checking general completion"
121
includes "" "--version"
123
echo "Checking list of subcommands"
124
HELP_SUBCMDS=`get_svn_subcommands | tr "\n" " "`
125
COMPLETION_SUBCMDS=`get_svn_completions | tr " " "\n" | grep -v "^-" | sort | tr "\n" " "`
126
if [ "$HELP_SUBCMDS" != "$COMPLETION_SUBCMDS" ]; then
127
fail "non-option completions for \"svn \" != subcommands accepted" \
128
" (non-o. cmpl.: $COMPLETION_SUBCMDS)" \
129
" (svn accepts: $HELP_SUBCMDS)"
132
echo "Checking list of options for each subcommand"
133
for SUBCMD in $HELP_SUBCMDS; do
134
HELP_OPTIONS=`get_svn_options $SUBCMD | tr "\n" " "`
135
COMPLETION_OPTIONS=`get_svn_completions $SUBCMD - | tr " " "\n" | sort | tr "\n" " "`
136
if [ "$HELP_OPTIONS" != "$COMPLETION_OPTIONS" ]; then
137
fail "completions for \"svn $SUBCMD -\" != options accepted" \
138
" (completions: $COMPLETION_OPTIONS)" \
139
" (svn accepts: $HELP_OPTIONS)"
143
echo "Checking rejection of synonyms"
144
excludes "diff -x -u -" "-x"
145
excludes "diff -x -u --e" "--extensions"
146
excludes "diff --extensions -u -" "--extensions"
147
excludes "diff --extensions -u -" "-x"
148
excludes "diff --extensions=-u -" "-x"
150
if [ $TESTS_FAILED ]; then
151
echo "FAILURE: at least one bash_completion test failed."
153
echo "All bash_completion tests passed."