Reorganize the output to "svnserve --help".
[svn.git] / tools / client-side / bash_completion_test
blob23ca16344f8cf63c006159e8c9b1684366ee2160
1 #!/bin/bash
2 # Checks that the "_svn" function defined in the specified "bash_completion"
3 # script produces appropriate lists of completions for various incomplete svn
4 # command lines.
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."
10 exit 1
13 set -e # Exit on error
14 shopt -s extglob
15 export LC_ALL=C
17 # Execute the script which is to be tested.
18 . "$1"
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() {
25 COMP_WORDS=(svn "$@")
26 if [ $# == 0 ]; then
27 COMP_CWORD=1
28 else
29 COMP_CWORD=$#
31 _svn
32 echo -n "${COMPREPLY[*]}"
35 # Print a failure message, record the failure, and return "false".
36 # Usage: fail MESSAGE
37 fail() {
38 PREFIX="FAIL: "
39 for LINE in "$@"; do
40 echo "$PREFIX$LINE"
41 PREFIX=" "
42 done
43 TESTS_FAILED=1
44 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
51 includes() {
52 COMPLETIONS=`get_svn_completions $1`
53 if [[ "$2" != @(${COMPLETIONS// /|}) ]]; then
54 fail "completions of \"svn $1\" should include \"$2\"" \
55 "(completions: $COMPLETIONS)"
59 excludes() {
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() {
71 svn help |
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
81 case $SYNONYM in
82 $CMD) ;;
83 $CMD*) CMD= ; break ;;
84 esac
85 done
86 if [ $CMD ]; then
87 echo $CMD
90 done
91 done
92 ) |
93 sort
96 # Print the valid option switches for "svn SUBCMD", one per line, sorted.
97 # Usage: get_svn_options SUBCMD
98 get_svn_options() {
99 { svn help "$1" |
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
106 echo "-h"
107 echo "--help"
108 } | sort
113 # The tests.
114 set +e # Do not exit on error
115 TESTS_FAILED=
117 echo "Checking general completion"
118 includes "he" "help"
119 includes "" "help"
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)"
140 done
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."
151 else
152 echo "All bash_completion tests passed."