~bzr-pqm/bzr/bzr.dev

« back to all changes in this revision

Viewing changes to tests/test_bashcomp.py

Added test cases for DataCollector and BashCodeGen.

Thes tests can be executed even without bash.  Instead of testing the whole
shell script, they test individual aspects of the data collection or code
generation process.  Mostly one test per method.  Already found and fixed a
bug in the method generating the version information comment.

Show diffs side-by-side

added added

removed removed

Lines of Context:
17
17
from bzrlib.tests import TestCase, TestCaseWithTransport, Feature
18
18
from bzrlib import commands
19
19
from StringIO import StringIO
20
 
from ..bashcomp import bash_completion_function
 
20
from ..bashcomp import *
 
21
import bzrlib
21
22
import os
22
23
import subprocess
23
24
 
149
150
 
150
151
    def test_global_opts(self):
151
152
        c = self.complete(['bzr', '-', 'init'], cword=1,
152
 
                          contains=['--no-plugins', '-?'])
 
153
                          contains=['--no-plugins', '--builtin'])
153
154
 
154
155
    def test_commit_dashm(self):
155
156
        c = self.complete(['bzr', 'commit', '-m'], expect=['-m'])
201
202
        wt.branch.tags.set_tag('3tag', 'null:')
202
203
        self.complete(['bzr', 'log', '-r', 'tag', ':', 't'],
203
204
                      expect=['tag1', 'tag2'])
 
205
 
 
206
 
 
207
class TestBashCodeGen(TestCase):
 
208
 
 
209
    def test_command_names(self):
 
210
        data = CompletionData()
 
211
        bar = CommandData('bar')
 
212
        bar.aliases.append('baz')
 
213
        data.commands.append(bar)
 
214
        data.commands.append(CommandData('foo'))
 
215
        cg = BashCodeGen(data)
 
216
        self.assertEqual('bar baz foo', cg.command_names())
 
217
 
 
218
    def test_debug_output(self):
 
219
        data = CompletionData()
 
220
        self.assertEqual('', BashCodeGen(data, debug=False).debug_output())
 
221
        self.assertTrue(BashCodeGen(data, debug=True).debug_output())
 
222
 
 
223
    def test_bzr_version(self):
 
224
        data = CompletionData()
 
225
        cg = BashCodeGen(data)
 
226
        self.assertEqual('%s.' % bzrlib.version_string, cg.bzr_version())
 
227
        data.plugins['foo'] = PluginData('foo', '1.0')
 
228
        data.plugins['bar'] = PluginData('bar', '2.0')
 
229
        cg = BashCodeGen(data)
 
230
        self.assertEqual('''\
 
231
%s and the following plugins:
 
232
# bar 2.0
 
233
# foo 1.0''' % bzrlib.version_string, cg.bzr_version())
 
234
 
 
235
    def test_global_options(self):
 
236
        data = CompletionData()
 
237
        data.global_options.add('--foo')
 
238
        data.global_options.add('--bar')
 
239
        cg = BashCodeGen(data)
 
240
        self.assertEqual('--bar --foo', cg.global_options())
 
241
 
 
242
    def test_command_cases(self):
 
243
        data = CompletionData()
 
244
        bar = CommandData('bar')
 
245
        bar.aliases.append('baz')
 
246
        bar.options.append(OptionData('--opt'))
 
247
        data.commands.append(bar)
 
248
        data.commands.append(CommandData('foo'))
 
249
        cg = BashCodeGen(data)
 
250
        self.assertEqualDiff('''\
 
251
\tbar|baz)
 
252
\t\tcmdOpts='--opt'
 
253
\t\t;;
 
254
\tfoo)
 
255
\t\tcmdOpts=''
 
256
\t\t;;
 
257
''', cg.command_cases())
 
258
 
 
259
    def test_command_case(self):
 
260
        cmd = CommandData('cmd')
 
261
        cmd.plugin = PluginData('plugger', '1.0')
 
262
        bar = OptionData('--bar')
 
263
        bar.registry_keys = ['that', 'this']
 
264
        bar.error_messages.append('Some error message')
 
265
        cmd.options.append(bar)
 
266
        cmd.options.append(OptionData('--foo'))
 
267
        data = CompletionData()
 
268
        data.commands.append(cmd)
 
269
        cg = BashCodeGen(data)
 
270
        self.assertEqualDiff('''\
 
271
\tcmd)
 
272
\t\t# plugin "plugger 1.0"
 
273
\t\t# Some error message
 
274
\t\tcmdOpts='--bar=that --bar=this --foo'
 
275
\t\tcase $curOpt in
 
276
\t\t\t--bar) optEnums='that this' ;;
 
277
\t\tesac
 
278
\t\t;;
 
279
''', cg.command_case(cmd))
 
280
 
 
281
 
 
282
class TestDataCollector(TestCase):
 
283
 
 
284
    def setUp(self):
 
285
        super(TestDataCollector, self).setUp()
 
286
        commands.install_bzr_command_hooks()
 
287
 
 
288
    def test_global_options(self):
 
289
        dc = DataCollector()
 
290
        dc.global_options()
 
291
        self.assertSubset(['--no-plugins', '--builtin'],
 
292
                           dc.data.global_options)
 
293
 
 
294
    def test_commands(self):
 
295
        dc = DataCollector()
 
296
        dc.commands()
 
297
        self.assertSubset(['init', 'init-repo', 'init-repository'],
 
298
                           dc.data.all_command_aliases())
 
299
 
 
300
    def test_commit_dashm(self):
 
301
        dc = DataCollector()
 
302
        cmd = dc.command('commit')
 
303
        self.assertSubset(['-m'],
 
304
                           [str(o) for o in cmd.options])
 
305
 
 
306
    def test_status_negated(self):
 
307
        dc = DataCollector()
 
308
        cmd = dc.command('status')
 
309
        self.assertSubset(['--no-versioned', '--no-verbose'],
 
310
                           [str(o) for o in cmd.options])
 
311
 
 
312
    def test_init_format(self):
 
313
        dc = DataCollector()
 
314
        cmd = dc.command('init')
 
315
        for opt in cmd.options:
 
316
            if opt.name == '--format':
 
317
                self.assertSubset(['2a'], opt.registry_keys)
 
318
                return
 
319
        raise AssertionError('Option --format not found')