Statistics
| Branch: | Tag: | Revision:

root / test / py / bash_completion.bash @ 560ef132

History | View | Annotate | Download (5.2 kB)

1
#!/bin/bash
2
#
3

    
4
# Copyright (C) 2012 Google Inc.
5
#
6
# This program is free software; you can redistribute it and/or modify
7
# it under the terms of the GNU General Public License as published by
8
# the Free Software Foundation; either version 2 of the License, or
9
# (at your option) any later version.
10
#
11
# This program is distributed in the hope that it will be useful, but
12
# WITHOUT ANY WARRANTY; without even the implied warranty of
13
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
14
# General Public License for more details.
15
#
16
# You should have received a copy of the GNU General Public License
17
# along with this program; if not, write to the Free Software
18
# Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA
19
# 02110-1301, USA.
20

    
21
set -e -u -o pipefail
22

    
23
# Disable any locales
24
export LC_ALL=C
25

    
26
readonly bash_completion=${TOP_BUILDDIR:-.}/doc/examples/bash_completion-debug
27
readonly default_wordbreaks=$' \t\n"'\''@><=;|&(:'
28

    
29
err() {
30
  echo "$@"
31
  echo 'Aborting'
32
  exit 1
33
}
34

    
35
contains() {
36
  local -r needle="$1"; shift
37

    
38
  for value; do
39
    if [[ "$value" = "$needle" ]]; then
40
      return 0
41
    fi
42
  done
43

    
44
  return 1
45
}
46

    
47
# Prepares a subshell for testing bash completion functions
48
setup() {
49
  local -r unused=UNUSED
50

    
51
  set +e +u -o pipefail
52

    
53
  . $bash_completion
54

    
55
  COMP_KEY=$unused
56
  COMP_LINE=$unused
57
  COMP_POINT=$unused
58
  COMP_TYPE=$unused
59
  COMP_WORDBREAKS="$default_wordbreaks"
60

    
61
  GANETI_COMPL_LOG=
62

    
63
  unset COMP_CWORD
64
  unset COMP_WORDS
65
}
66

    
67
# Check if default wordbreaks are still valid (this detects cases where Bash
68
# were to change its built-in default value)
69
# TODO: May need an update for older Bash versions; some didn't include the
70
# colon character (':') in COMP_WORDBREAKS
71
(
72
  bashdef=$(env - bash --noprofile --norc -c 'echo -n "$COMP_WORDBREAKS"')
73
  case "$bashdef" in
74
    $default_wordbreaks) ;;
75
    *)
76
      err 'Bash uses unknown value for COMP_WORDBREAKS'
77
      ;;
78
  esac
79
)
80

    
81
# Check for --help
82
for cmd in gnt-{instance,node,group,job}; do
83
  (
84
    setup
85
    COMP_CWORD=2
86
    COMP_WORDS=( $cmd list - )
87
    _${cmd/-/_}
88
    contains --help "${COMPREPLY[@]}" || \
89
      err "'$cmd list' did not list --help as an option"
90
  )
91
done
92

    
93
# Completing a yes/no option
94
(
95
  setup
96
  COMP_CWORD=3
97
  COMP_WORDS=( gnt-node modify --drained )
98
  _gnt_node
99
  if [[ "${COMPREPLY[*]}" != 'no yes' ]]; then
100
    err "Completing '${COMP_WORDS[@]}' did not give correct result"
101
  fi
102
)
103

    
104
# Completing a multiple-choice option
105
(
106
  setup
107
  COMP_CWORD=2
108
  COMP_WORDS=( gnt-debug allocator --disk-template=sh foo )
109
  _gnt_debug
110
  if [[ "${COMPREPLY[*]}" != sharedfile ]]; then
111
    err "Completing '${COMP_WORDS[*]}' did not give correct result"
112
  fi
113
)
114

    
115
# Completing a node name
116
(
117
  setup
118

    
119
  # Override built-in function
120
  _ganeti_nodes() {
121
    echo aanode1 bbnode2 aanode3
122
  }
123

    
124
  COMP_CWORD=4
125
  COMP_WORDS=( gnt-node modify --drained yes aa )
126
  _gnt_node
127
  if [[ "${COMPREPLY[*]}" != 'aanode1 aanode3' ]]; then
128
    err 'Completing node names failed'
129
  fi
130
)
131

    
132
# Completing an option when it's not at the end
133
(
134
  setup
135

    
136
  # Override built-in function
137
  _ganeti_instances() {
138
    echo inst{1..5}
139
  }
140

    
141
  # Completing word in the middle
142
  COMP_CWORD=2
143
  COMP_WORDS=( gnt-instance list --o inst3 inst inst5 )
144
  _gnt_node
145
  contains --output "${COMPREPLY[@]}" || err 'Did not complete parameter'
146
)
147

    
148
# Completing an instance name
149
(
150
  setup
151

    
152
  # Override built-in function
153
  _ganeti_instances() {
154
    echo inst{1..5}
155
  }
156

    
157
  # Completing word in the middle
158
  COMP_CWORD=5
159
  COMP_WORDS=( gnt-instance list -o foobar inst1 inst inst5 )
160
  _gnt_instance
161
  if [[ "${COMPREPLY[*]}" != "$(echo inst{1..5})" ]]; then
162
    err "Completing '${COMP_WORDS[*]}' did not give correct result"
163
  fi
164
)
165

    
166
# Need to test node expansion with different wordbreak settings
167
[[ "$default_wordbreaks" == *:* ]] || \
168
  err 'No colon in default wordbreak characters'
169
for wb in "$default_wordbreaks" "${default_wordbreaks/:/}"; do
170
  (
171
    setup
172

    
173
    # Override built-in function
174
    _ganeti_nodes() {
175
      echo node{A..C}
176
    }
177

    
178
    COMP_WORDBREAKS="$wb"
179

    
180
    # Completing nodes
181
    COMP_CWORD=3
182
    COMP_WORDS=( gnt-instance add -n )
183
    _gnt_instance
184
    if [[ "${COMPREPLY[*]}" != 'nodeA nodeA: nodeB nodeB: nodeC nodeC:' ]]; then
185
      err 'Got wrong node list'
186
    fi
187

    
188
    COMP_CWORD=3
189
    COMP_WORDS=( gnt-instance add -n nodeB )
190
    _gnt_instance
191
    if [[ "${COMPREPLY[*]}" != 'nodeB nodeB:' ]]; then
192
      err 'Got wrong node list'
193
    fi
194

    
195
    COMP_CWORD=3
196
    COMP_WORDS=( gnt-instance add -n nodeC: )
197
    _gnt_instance
198
    if [[ "$COMP_WORDBREAKS" == *:* ]]; then
199
      expected='nodeA nodeB'
200
    else
201
      expected='nodeC:nodeA nodeC:nodeB'
202
    fi
203
    if [[ "${COMPREPLY[*]}" != "$expected" ]]; then
204
      err 'Got wrong node list'
205
    fi
206
  )
207
done
208

    
209
# Need to test different settings for the extglob shell option
210
for opt in -u -s; do
211
  verify_extglob() {
212
    if [[ "$(shopt -p extglob)" != "shopt $opt extglob" ]]; then
213
      err 'The "extglob" shell option has an unexpected value'
214
    fi
215
  }
216

    
217
  (
218
    shopt $opt extglob
219

    
220
    verify_extglob
221
    setup
222
    verify_extglob
223

    
224
    # Completing nodes
225
    COMP_CWORD=4
226
    COMP_WORDS=( gnt-instance add --os-type busybox --no-n )
227
    _gnt_instance
228
    if [[ "${COMPREPLY[*]}" != '--no-name-check --no-nics' ]]; then
229
      err "Completing '${COMP_WORDS[*]}' did not give correct result"
230
    fi
231
    verify_extglob
232
  )
233
done
234

    
235
exit 0