/brz/remove-bazaar

To get this branch, use:
bzr branch http://gegoxaren.bato24.eu/bzr/brz/remove-bazaar
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
# 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

from bzrlib.tests import TestCase, TestCaseWithTransport, Feature
from bzrlib import commands
from StringIO import StringIO
from ..bashcomp import bash_completion_function
import os
import subprocess


class _BashFeature(Feature):
    """Feature testing whether a bash executable is available."""

    bash_paths = ['/bin/bash', '/usr/bin/bash']

    def __init__(self):
        super(_BashFeature, self).__init__()
        self.bash_path = None

    def available(self):
        if self.bash_path is not None:
            return self.bash_path is not False
        for path in self.bash_paths:
            if os.access(path, os.X_OK):
                self.bash_path = path
                return True
        self.bash_path = False
        return False

    def feature_name(self):
        return 'bash'

BashFeature = _BashFeature()


class BashCompletionMixin(object):
    """Component for testing execution of a bash completion script."""

    _test_needs_features = [BashFeature]

    def complete(self, words, cword=-1, expect=None,
                 contains=[], omits=[]):
        """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.
        :param expect: an exact iterable of expected completions.
        :param contains: an iterable of words required in the completion result.
        :param omits: an iterable of words forbidden in the completion result.
        """
        if self.script is None:
            self.script = self.get_script()
        proc = subprocess.Popen([BashFeature.bash_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')
        res = set(lines)
        if expect is not None:
            self.assertEqual(set(expect), res)
        missing = set(contains) - res
        if missing:
            raise AssertionError('Completion should contain %r but it has %r'
                                 % (missing, res))
        surplus = set(omits) & res
        if surplus:
            raise AssertionError('Completion should omit %r but it has %r'
                                 % (surplus, res))
        return res

    def get_script(self):
        out = StringIO()
        bash_completion_function(out, function_only=True)
        return out.getvalue()


class TestBashCompletion(TestCase, BashCompletionMixin):
    """Test bash completions that don't execute bzr."""

    def __init__(self, methodName='testMethod'):
        super(TestBashCompletion, self).__init__(methodName)
        self.script = None

    def setUp(self):
        super(TestBashCompletion, self).setUp()
        commands.install_bzr_command_hooks()

    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,
                      expect=["-'baz+", '-"bar+', '-foo+', '+"bar-'])

    def test_cmd_ini(self):
        c = self.complete(['bzr', 'ini'],
                          contains=['init', 'init-repo', 'init-repository'])
        self.assertFalse('commit' in c)

    def test_init_opts(self):
        c = self.complete(['bzr', 'init', '-'],
                          contains=['-h', '--2a', '--format=2a'])

    def test_global_opts(self):
        c = self.complete(['bzr', '-', 'init'], cword=1,
                          contains=['--no-plugins', '-?'])

    def test_commit_dashm(self):
        c = self.complete(['bzr', 'commit', '-m'], expect=['-m'])

    def test_status_negated(self):
        c = self.complete(['bzr', 'status', '--n'],
                          contains=['--no-versioned', '--no-verbose'])

    def test_init_format_any(self):
        c = self.complete(['bzr', 'init', '--format', '=', 'directory'],
                          cword=3, contains=['1.9', '2a'])

    def test_init_format_2(self):
        c = self.complete(['bzr', 'init', '--format', '=', '2', 'directory'],
                          cword=4, contains=['2a'], omits=['1.9'])


class TestBashCompletionInvoking(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 __init__(self, methodName='testMethod'):
        super(TestBashCompletionInvoking, self).__init__(methodName)
        self.script = None

    def setUp(self):
        super(TestBashCompletionInvoking, self).setUp()
        commands.install_bzr_command_hooks()

    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):
        wt = self.make_branch_and_tree('.', format='2a')
        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', ':'],
                      expect=['tag1', 'tag2', '3tag'])

    def test_revspec_tag_prefix(self):
        wt = self.make_branch_and_tree('.', format='2a')
        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'],
                      expect=['tag1', 'tag2'])