1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
|
# Copyright (C) 2010 by Canonical Ltd
#
# This program is free software; you can redistribute it and/or modify
# it under the terms of the GNU General Public License as published by
# the Free Software Foundation; either version 2 of the License, or
# (at your option) any later version.
#
# This program is distributed in the hope that it will be useful,
# but WITHOUT ANY WARRANTY; without even the implied warranty of
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
# GNU General Public License for more details.
#
# You should have received a copy of the GNU General Public License
# along with this program; if not, write to the Free Software
# Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
import sys
import bzrlib
from bzrlib import commands, tests
from bzrlib.tests import features
from bzrlib.plugins.bash_completion.bashcomp import *
import subprocess
class BashCompletionMixin(object):
"""Component for testing execution of a bash completion script."""
_test_needs_features = [features.bash_feature]
script = None
def complete(self, words, cword=-1):
"""Perform a bash completion.
:param words: a list of words representing the current command.
:param cword: the current word to complete, defaults to the last one.
"""
if self.script is None:
self.script = self.get_script()
proc = subprocess.Popen([features.bash_feature.path,
'--noprofile'],
stdin=subprocess.PIPE,
stdout=subprocess.PIPE,
stderr=subprocess.PIPE)
if cword < 0:
cword = len(words) + cword
input = '%s\n' % self.script
input += ('COMP_WORDS=( %s )\n' %
' '.join(["'"+w.replace("'", "'\\''")+"'" for w in words]))
input += 'COMP_CWORD=%d\n' % cword
input += '%s\n' % getattr(self, 'script_name', '_bzr')
input += 'echo ${#COMPREPLY[*]}\n'
input += "IFS=$'\\n'\n"
input += 'echo "${COMPREPLY[*]}"\n'
(out, err) = proc.communicate(input)
if '' != err:
raise AssertionError('Unexpected error message:\n%s' % err)
self.assertEqual('', err, 'No messages to standard error')
#import sys
#print >>sys.stdout, '---\n%s\n---\n%s\n---\n' % (input, out)
lines = out.split('\n')
nlines = int(lines[0])
del lines[0]
self.assertEqual('', lines[-1], 'Newline at end')
del lines[-1]
if nlines == 0 and len(lines) == 1 and lines[0] == '':
del lines[0]
self.assertEqual(nlines, len(lines), 'No newlines in generated words')
self.completion_result = set(lines)
return self.completion_result
def assertCompletionEquals(self, *words):
self.assertEqual(set(words), self.completion_result)
def assertCompletionContains(self, *words):
missing = set(words) - self.completion_result
if missing:
raise AssertionError('Completion should contain %r but it has %r'
% (missing, self.completion_result))
def assertCompletionOmits(self, *words):
surplus = set(words) & self.completion_result
if surplus:
raise AssertionError('Completion should omit %r but it has %r'
% (surplus, res, self.completion_result))
def get_script(self):
commands.install_bzr_command_hooks()
dc = DataCollector()
data = dc.collect()
cg = BashCodeGen(data)
res = cg.function()
return res
class TestBashCompletion(tests.TestCase, BashCompletionMixin):
"""Test bash completions that don't execute bzr."""
def test_simple_scipt(self):
"""Ensure that the test harness works as expected"""
self.script = """
_bzr() {
COMPREPLY=()
# add all words in reverse order, with some markup around them
for ((i = ${#COMP_WORDS[@]}; i > 0; --i)); do
COMPREPLY+=( "-${COMP_WORDS[i-1]}+" )
done
# and append the current word
COMPREPLY+=( "+${COMP_WORDS[COMP_CWORD]}-" )
}
"""
self.complete(['foo', '"bar', "'baz"], cword=1)
self.assertCompletionEquals("-'baz+", '-"bar+', '-foo+', '+"bar-')
def test_cmd_ini(self):
self.complete(['bzr', 'ini'])
self.assertCompletionContains('init', 'init-repo', 'init-repository')
self.assertCompletionOmits('commit')
def test_init_opts(self):
self.complete(['bzr', 'init', '-'])
self.assertCompletionContains('-h', '--2a', '--format=2a')
def test_global_opts(self):
self.complete(['bzr', '-', 'init'], cword=1)
self.assertCompletionContains('--no-plugins', '--builtin')
def test_commit_dashm(self):
self.complete(['bzr', 'commit', '-m'])
self.assertCompletionEquals('-m')
def test_status_negated(self):
self.complete(['bzr', 'status', '--n'])
self.assertCompletionContains('--no-versioned', '--no-verbose')
def test_init_format_any(self):
self.complete(['bzr', 'init', '--format', '=', 'directory'], cword=3)
self.assertCompletionContains('1.9', '2a')
def test_init_format_2(self):
self.complete(['bzr', 'init', '--format', '=', '2', 'directory'],
cword=4)
self.assertCompletionContains('2a')
self.assertCompletionOmits('1.9')
class TestBashCompletionInvoking(tests.TestCaseWithTransport,
BashCompletionMixin):
"""Test bash completions that might execute bzr.
Only the syntax ``$(bzr ...`` is supported so far. The bzr command
will be replaced by the bzr instance running this selftest.
"""
def setUp(self):
super(TestBashCompletionInvoking, self).setUp()
if sys.platform == 'win32':
raise tests.KnownFailure(
'see bug #709104, completion is broken on windows')
def get_script(self):
s = super(TestBashCompletionInvoking, self).get_script()
return s.replace("$(bzr ", "$('%s' " % self.get_bzr_path())
def test_revspec_tag_all(self):
self.requireFeature(features.sed_feature)
wt = self.make_branch_and_tree('.', format='dirstate-tags')
wt.branch.tags.set_tag('tag1', 'null:')
wt.branch.tags.set_tag('tag2', 'null:')
wt.branch.tags.set_tag('3tag', 'null:')
self.complete(['bzr', 'log', '-r', 'tag', ':'])
self.assertCompletionEquals('tag1', 'tag2', '3tag')
def test_revspec_tag_prefix(self):
self.requireFeature(features.sed_feature)
wt = self.make_branch_and_tree('.', format='dirstate-tags')
wt.branch.tags.set_tag('tag1', 'null:')
wt.branch.tags.set_tag('tag2', 'null:')
wt.branch.tags.set_tag('3tag', 'null:')
self.complete(['bzr', 'log', '-r', 'tag', ':', 't'])
self.assertCompletionEquals('tag1', 'tag2')
def test_revspec_tag_spaces(self):
self.requireFeature(features.sed_feature)
wt = self.make_branch_and_tree('.', format='dirstate-tags')
wt.branch.tags.set_tag('tag with spaces', 'null:')
self.complete(['bzr', 'log', '-r', 'tag', ':', 't'])
self.assertCompletionEquals(r'tag\ with\ spaces')
self.complete(['bzr', 'log', '-r', '"tag:t'])
self.assertCompletionEquals('tag:tag with spaces')
self.complete(['bzr', 'log', '-r', "'tag:t"])
self.assertCompletionEquals('tag:tag with spaces')
def test_revspec_tag_endrange(self):
self.requireFeature(features.sed_feature)
wt = self.make_branch_and_tree('.', format='dirstate-tags')
wt.branch.tags.set_tag('tag1', 'null:')
wt.branch.tags.set_tag('tag2', 'null:')
self.complete(['bzr', 'log', '-r', '3..tag', ':', 't'])
self.assertCompletionEquals('tag1', 'tag2')
self.complete(['bzr', 'log', '-r', '"3..tag:t'])
self.assertCompletionEquals('3..tag:tag1', '3..tag:tag2')
self.complete(['bzr', 'log', '-r', "'3..tag:t"])
self.assertCompletionEquals('3..tag:tag1', '3..tag:tag2')
class TestBashCodeGen(tests.TestCase):
def test_command_names(self):
data = CompletionData()
bar = CommandData('bar')
bar.aliases.append('baz')
data.commands.append(bar)
data.commands.append(CommandData('foo'))
cg = BashCodeGen(data)
self.assertEqual('bar baz foo', cg.command_names())
def test_debug_output(self):
data = CompletionData()
self.assertEqual('', BashCodeGen(data, debug=False).debug_output())
self.assertTrue(BashCodeGen(data, debug=True).debug_output())
def test_bzr_version(self):
data = CompletionData()
cg = BashCodeGen(data)
self.assertEqual('%s.' % bzrlib.version_string, cg.bzr_version())
data.plugins['foo'] = PluginData('foo', '1.0')
data.plugins['bar'] = PluginData('bar', '2.0')
cg = BashCodeGen(data)
self.assertEqual('''\
%s and the following plugins:
# bar 2.0
# foo 1.0''' % bzrlib.version_string, cg.bzr_version())
def test_global_options(self):
data = CompletionData()
data.global_options.add('--foo')
data.global_options.add('--bar')
cg = BashCodeGen(data)
self.assertEqual('--bar --foo', cg.global_options())
def test_command_cases(self):
data = CompletionData()
bar = CommandData('bar')
bar.aliases.append('baz')
bar.options.append(OptionData('--opt'))
data.commands.append(bar)
data.commands.append(CommandData('foo'))
cg = BashCodeGen(data)
self.assertEqualDiff('''\
\tbar|baz)
\t\tcmdOpts=( --opt )
\t\t;;
\tfoo)
\t\tcmdOpts=( )
\t\t;;
''', cg.command_cases())
def test_command_case(self):
cmd = CommandData('cmd')
cmd.plugin = PluginData('plugger', '1.0')
bar = OptionData('--bar')
bar.registry_keys = ['that', 'this']
bar.error_messages.append('Some error message')
cmd.options.append(bar)
cmd.options.append(OptionData('--foo'))
data = CompletionData()
data.commands.append(cmd)
cg = BashCodeGen(data)
self.assertEqualDiff('''\
\tcmd)
\t\t# plugin "plugger 1.0"
\t\t# Some error message
\t\tcmdOpts=( --bar=that --bar=this --foo )
\t\tcase $curOpt in
\t\t\t--bar) optEnums=( that this ) ;;
\t\tesac
\t\t;;
''', cg.command_case(cmd))
class TestDataCollector(tests.TestCase):
def setUp(self):
super(TestDataCollector, self).setUp()
commands.install_bzr_command_hooks()
def test_global_options(self):
dc = DataCollector()
dc.global_options()
self.assertSubset(['--no-plugins', '--builtin'],
dc.data.global_options)
def test_commands(self):
dc = DataCollector()
dc.commands()
self.assertSubset(['init', 'init-repo', 'init-repository'],
dc.data.all_command_aliases())
def test_commands_from_plugins(self):
dc = DataCollector()
dc.commands()
self.assertSubset(['bash-completion'],
dc.data.all_command_aliases())
def test_commit_dashm(self):
dc = DataCollector()
cmd = dc.command('commit')
self.assertSubset(['-m'],
[str(o) for o in cmd.options])
def test_status_negated(self):
dc = DataCollector()
cmd = dc.command('status')
self.assertSubset(['--no-versioned', '--no-verbose'],
[str(o) for o in cmd.options])
def test_init_format(self):
dc = DataCollector()
cmd = dc.command('init')
for opt in cmd.options:
if opt.name == '--format':
self.assertSubset(['2a'], opt.registry_keys)
return
raise AssertionError('Option --format not found')
class BlackboxTests(tests.TestCase):
def test_bash_completion(self):
self.run_bzr("bash-completion")
|