~bzr-pqm/bzr/bzr.dev

« back to all changes in this revision

Viewing changes to tests/test_bashcomp.py

Turn completion assertions into separate methods.

Many common assertions used to be expressed as arguments to the complete
method.  This makes the checks more explicit, and the code easier to read.

Show diffs side-by-side

added added

removed removed

Lines of Context:
14
14
# along with this program; if not, write to the Free Software
15
15
# Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
16
16
 
17
 
import sys
18
 
 
 
17
from bzrlib.tests import TestCase, TestCaseWithTransport, Feature
 
18
from bzrlib import commands
 
19
from StringIO import StringIO
 
20
from ..bashcomp import *
19
21
import bzrlib
20
 
from bzrlib import commands, tests
21
 
from bzrlib.tests import features
22
 
from bzrlib.plugins.bash_completion.bashcomp import *
23
 
 
24
22
import os
25
23
import subprocess
26
24
 
27
25
 
 
26
class _BashFeature(Feature):
 
27
    """Feature testing whether a bash executable is available."""
 
28
 
 
29
    bash_paths = ['/bin/bash', '/usr/bin/bash']
 
30
 
 
31
    def __init__(self):
 
32
        super(_BashFeature, self).__init__()
 
33
        self.bash_path = None
 
34
 
 
35
    def available(self):
 
36
        if self.bash_path is not None:
 
37
            return self.bash_path is not False
 
38
        for path in self.bash_paths:
 
39
            if os.access(path, os.X_OK):
 
40
                self.bash_path = path
 
41
                return True
 
42
        self.bash_path = False
 
43
        return False
 
44
 
 
45
    def feature_name(self):
 
46
        return 'bash'
 
47
 
 
48
BashFeature = _BashFeature()
 
49
 
 
50
 
28
51
class BashCompletionMixin(object):
29
52
    """Component for testing execution of a bash completion script."""
30
53
 
31
 
    _test_needs_features = [features.bash_feature]
32
 
    script = None
 
54
    _test_needs_features = [BashFeature]
33
55
 
34
56
    def complete(self, words, cword=-1):
35
57
        """Perform a bash completion.
39
61
        """
40
62
        if self.script is None:
41
63
            self.script = self.get_script()
42
 
        proc = subprocess.Popen([features.bash_feature.path,
43
 
                                 '--noprofile'],
 
64
        proc = subprocess.Popen([BashFeature.bash_path, '--noprofile'],
44
65
                                stdin=subprocess.PIPE,
45
66
                                stdout=subprocess.PIPE,
46
67
                                stderr=subprocess.PIPE)
87
108
                                 % (surplus, res, self.completion_result))
88
109
 
89
110
    def get_script(self):
90
 
        commands.install_bzr_command_hooks()
91
 
        dc = DataCollector()
92
 
        data = dc.collect()
93
 
        cg = BashCodeGen(data)
94
 
        res = cg.function()
95
 
        return res
96
 
 
97
 
 
98
 
class TestBashCompletion(tests.TestCase, BashCompletionMixin):
 
111
        out = StringIO()
 
112
        bash_completion_function(out, function_only=True)
 
113
        return out.getvalue()
 
114
 
 
115
 
 
116
class TestBashCompletion(TestCase, BashCompletionMixin):
99
117
    """Test bash completions that don't execute bzr."""
100
118
 
 
119
    def __init__(self, methodName='testMethod'):
 
120
        super(TestBashCompletion, self).__init__(methodName)
 
121
        self.script = None
 
122
 
 
123
    def setUp(self):
 
124
        super(TestBashCompletion, self).setUp()
 
125
        commands.install_bzr_command_hooks()
 
126
 
101
127
    def test_simple_scipt(self):
102
128
        """Ensure that the test harness works as expected"""
103
129
        self.script = """
146
172
        self.assertCompletionOmits('1.9')
147
173
 
148
174
 
149
 
class TestBashCompletionInvoking(tests.TestCaseWithTransport,
150
 
                                 BashCompletionMixin):
 
175
class TestBashCompletionInvoking(TestCaseWithTransport, BashCompletionMixin):
151
176
    """Test bash completions that might execute bzr.
152
177
 
153
178
    Only the syntax ``$(bzr ...`` is supported so far. The bzr command
154
179
    will be replaced by the bzr instance running this selftest.
155
180
    """
156
181
 
 
182
    def __init__(self, methodName='testMethod'):
 
183
        super(TestBashCompletionInvoking, self).__init__(methodName)
 
184
        self.script = None
 
185
 
157
186
    def setUp(self):
158
187
        super(TestBashCompletionInvoking, self).setUp()
159
 
        if sys.platform == 'win32':
160
 
            raise tests.KnownFailure(
161
 
                'see bug #709104, completion is broken on windows')
 
188
        commands.install_bzr_command_hooks()
162
189
 
163
190
    def get_script(self):
164
191
        s = super(TestBashCompletionInvoking, self).get_script()
165
192
        return s.replace("$(bzr ", "$('%s' " % self.get_bzr_path())
166
193
 
167
194
    def test_revspec_tag_all(self):
168
 
        self.requireFeature(features.sed_feature)
169
195
        wt = self.make_branch_and_tree('.', format='dirstate-tags')
170
196
        wt.branch.tags.set_tag('tag1', 'null:')
171
197
        wt.branch.tags.set_tag('tag2', 'null:')
174
200
        self.assertCompletionEquals('tag1', 'tag2', '3tag')
175
201
 
176
202
    def test_revspec_tag_prefix(self):
177
 
        self.requireFeature(features.sed_feature)
178
203
        wt = self.make_branch_and_tree('.', format='dirstate-tags')
179
204
        wt.branch.tags.set_tag('tag1', 'null:')
180
205
        wt.branch.tags.set_tag('tag2', 'null:')
182
207
        self.complete(['bzr', 'log', '-r', 'tag', ':', 't'])
183
208
        self.assertCompletionEquals('tag1', 'tag2')
184
209
 
185
 
    def test_revspec_tag_spaces(self):
186
 
        self.requireFeature(features.sed_feature)
187
 
        wt = self.make_branch_and_tree('.', format='dirstate-tags')
188
 
        wt.branch.tags.set_tag('tag with spaces', 'null:')
189
 
        self.complete(['bzr', 'log', '-r', 'tag', ':', 't'])
190
 
        self.assertCompletionEquals(r'tag\ with\ spaces')
191
 
        self.complete(['bzr', 'log', '-r', '"tag:t'])
192
 
        self.assertCompletionEquals('tag:tag with spaces')
193
 
        self.complete(['bzr', 'log', '-r', "'tag:t"])
194
 
        self.assertCompletionEquals('tag:tag with spaces')
195
 
 
196
 
    def test_revspec_tag_endrange(self):
197
 
        self.requireFeature(features.sed_feature)
198
 
        wt = self.make_branch_and_tree('.', format='dirstate-tags')
199
 
        wt.branch.tags.set_tag('tag1', 'null:')
200
 
        wt.branch.tags.set_tag('tag2', 'null:')
201
 
        self.complete(['bzr', 'log', '-r', '3..tag', ':', 't'])
202
 
        self.assertCompletionEquals('tag1', 'tag2')
203
 
        self.complete(['bzr', 'log', '-r', '"3..tag:t'])
204
 
        self.assertCompletionEquals('3..tag:tag1', '3..tag:tag2')
205
 
        self.complete(['bzr', 'log', '-r', "'3..tag:t"])
206
 
        self.assertCompletionEquals('3..tag:tag1', '3..tag:tag2')
207
 
 
208
 
 
209
 
class TestBashCodeGen(tests.TestCase):
 
210
 
 
211
class TestBashCodeGen(TestCase):
210
212
 
211
213
    def test_command_names(self):
212
214
        data = CompletionData()
251
253
        cg = BashCodeGen(data)
252
254
        self.assertEqualDiff('''\
253
255
\tbar|baz)
254
 
\t\tcmdOpts=( --opt )
 
256
\t\tcmdOpts='--opt'
255
257
\t\t;;
256
258
\tfoo)
257
 
\t\tcmdOpts=(  )
 
259
\t\tcmdOpts=''
258
260
\t\t;;
259
261
''', cg.command_cases())
260
262
 
273
275
\tcmd)
274
276
\t\t# plugin "plugger 1.0"
275
277
\t\t# Some error message
276
 
\t\tcmdOpts=( --bar=that --bar=this --foo )
 
278
\t\tcmdOpts='--bar=that --bar=this --foo'
277
279
\t\tcase $curOpt in
278
 
\t\t\t--bar) optEnums=( that this ) ;;
 
280
\t\t\t--bar) optEnums='that this' ;;
279
281
\t\tesac
280
282
\t\t;;
281
283
''', cg.command_case(cmd))
282
284
 
283
285
 
284
 
class TestDataCollector(tests.TestCase):
 
286
class TestDataCollector(TestCase):
285
287
 
286
288
    def setUp(self):
287
289
        super(TestDataCollector, self).setUp()
299
301
        self.assertSubset(['init', 'init-repo', 'init-repository'],
300
302
                           dc.data.all_command_aliases())
301
303
 
302
 
    def test_commands_from_plugins(self):
303
 
        dc = DataCollector()
304
 
        dc.commands()
305
 
        self.assertSubset(['bash-completion'],
306
 
                           dc.data.all_command_aliases())
307
 
 
308
304
    def test_commit_dashm(self):
309
305
        dc = DataCollector()
310
306
        cmd = dc.command('commit')