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
17 # Execute the script which is to be tested.
20 # From the given incomplete svn command, print a space-separated list of
21 # possible completions of the last argument (or of an empty first argument
22 # if no subcommand is given).
23 # Usage: get_svn_completions [SVN-SUBCOMMAND [SVN-OPTION...]]
24 get_svn_completions
() {
32 echo -n "${COMPREPLY[*]}"
35 # Print a failure message, record the failure, and return "false".
47 # Check that EXPECTED-WORD is among the completions of the last word in
48 # SVN-COMMAND. SVN-COMMAND is a single argument to this function, split
49 # into multiple arguments when passed to "get_svn_completions()".
50 # Usage: includes SVN-COMMAND EXPECTED-WORD
52 COMPLETIONS
=`get_svn_completions $1`
53 if [[ "$2" != @
(${COMPLETIONS// /|}) ]]; then
54 fail
"completions of \"svn $1\" should include \"$2\"" \
55 "(completions: $COMPLETIONS)"
60 COMPLETIONS
=`get_svn_completions $1`
61 if [[ "$2" == @
(${COMPLETIONS// /|}) ]]; then
62 fail
"completions of \"svn $1\" should exclude \"$2\"" \
63 "(completions: $COMPLETIONS)"
67 # Print the valid subcommands for "svn", one per line, sorted.
68 # Exclude any synonym that is just a truncation of its full name.
69 # Usage: get_svn_subcommands
70 get_svn_subcommands
() {
72 # Find the relevant lines.
73 sed -n -e '1,/^Available subcommands:$/d;/^$/q;p' |
74 # Remove brackets and commas
75 tr -d ' )' |
tr '(,' ' ' |
76 # Remove simple abbreviations
77 ( while read SYNONYMS
; do
78 for CMD
in $SYNONYMS; do
79 if [ "$CMD" != "?" ]; then
80 for SYNONYM
in $SYNONYMS; do
83 $CMD*) CMD
= ; break ;;
96 # Print the valid option switches for "svn SUBCMD", one per line, sorted.
97 # Usage: get_svn_options SUBCMD
100 # Find the relevant lines; remove "arg" and description.
101 sed -n -e '1,/^Valid options:$/d;/^ -/!d' \
102 -e 's/\( arg\)* * : .*//;p' |
103 # Remove brackets; put each word on its own line.
104 tr -d '] ' |
tr '[' '\n'
105 # The following options are always accepted but not listed in the help
114 set +e
# Do not exit on error
117 echo "Checking general completion"
120 includes
"" "--version"
122 echo "Checking list of subcommands"
123 HELP_SUBCMDS
=`get_svn_subcommands | tr "\n" " "`
124 COMPLETION_SUBCMDS
=`get_svn_completions | tr " " "\n" | grep -v "^-" | sort | tr "\n" " "`
125 if [ "$HELP_SUBCMDS" != "$COMPLETION_SUBCMDS" ]; then
126 fail
"non-option completions for \"svn \" != subcommands accepted" \
127 " (non-o. cmpl.: $COMPLETION_SUBCMDS)" \
128 " (svn accepts: $HELP_SUBCMDS)"
131 echo "Checking list of options for each subcommand"
132 for SUBCMD
in $HELP_SUBCMDS; do
133 HELP_OPTIONS
=`get_svn_options $SUBCMD | tr "\n" " "`
134 COMPLETION_OPTIONS
=`get_svn_completions $SUBCMD - | tr " " "\n" | sort | tr "\n" " "`
135 if [ "$HELP_OPTIONS" != "$COMPLETION_OPTIONS" ]; then
136 fail
"completions for \"svn $SUBCMD -\" != options accepted" \
137 " (completions: $COMPLETION_OPTIONS)" \
138 " (svn accepts: $HELP_OPTIONS)"
142 echo "Checking rejection of synonyms"
143 excludes
"diff -x -u -" "-x"
144 excludes
"diff -x -u --e" "--extensions"
145 excludes
"diff --extensions -u -" "--extensions"
146 excludes
"diff --extensions -u -" "-x"
147 excludes
"diff --extensions=-u -" "-x"
149 if [ $TESTS_FAILED ]; then
150 echo "FAILURE: at least one bash_completion test failed."
152 echo "All bash_completion tests passed."