/brz/remove-bazaar

To get this branch, use:
bzr branch http://gegoxaren.bato24.eu/bzr/brz/remove-bazaar
5557.1.7 by John Arbash Meinel
Merge in the bzr.dev 5582
1
# Copyright (C) 2009, 2010, 2011 Canonical Ltd
4711.1.2 by Martin Pool
Start adding tests.per_uifactory
2
#
3
# This program is free software; you can redistribute it and/or modify
4
# it under the terms of the GNU General Public License as published by
5
# the Free Software Foundation; either version 2 of the License, or
6
# (at your option) any later version.
7
#
8
# This program is distributed in the hope that it will be useful,
9
# but WITHOUT ANY WARRANTY; without even the implied warranty of
10
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
11
# GNU General Public License for more details.
12
#
13
# You should have received a copy of the GNU General Public License
14
# along with this program; if not, write to the Free Software
15
# Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
16
4711.1.4 by Martin Pool
Change per_uifactory to use subclassing rather than scenarios
17
"""Tests run per UIFactory."""
18
19
# Testing UIFactories is a bit interesting because we require they all support a
20
# common interface, but the way they implement it can vary very widely.  Between
21
# text, batch-mode, graphical and other potential UIFactories, the requirements
22
# to set up a factory, to make it respond to requests, and to simulate user
23
# input can vary a lot.
24
#
25
# We want tests that therefore allow for the evaluation of the result to vary
26
# per implementation, but we want to check that the supported facilities are
27
# the same across all UIFactorys, unless they're specifically skipped.
28
#
29
# Our normal approach is to use test scenarios but that seems to just end up
30
# creating test-like objects inside the scenario.  Therefore we fall back to
31
# the older method of putting the common tests in a mixin.
32
#
33
# Plugins that add new UIFactorys can create their own subclasses.
4711.1.2 by Martin Pool
Start adding tests.per_uifactory
34
35
36
from cStringIO import StringIO
37
import unittest
38
39
40
from bzrlib import (
41
    tests,
4906.1.2 by John Arbash Meinel
Get the basic interface tested.
42
    transport,
4711.1.2 by Martin Pool
Start adding tests.per_uifactory
43
    ui,
44
    )
45
46
4711.1.4 by Martin Pool
Change per_uifactory to use subclassing rather than scenarios
47
class UIFactoryTestMixin(object):
48
    """Common tests for UIFactories.
49
50
    These are supposed to be expressed with no assumptions about how the
51
    UIFactory implements the method, only that it does implement them (or
52
    fails cleanly), and that the concrete subclass will make arrangements to
53
    build a factory and to examine its behaviour.
54
55
    Note that this is *not* a TestCase, because it can't be directly run, but
56
    the concrete subclasses should be.
57
    """
58
4961.1.2 by Martin Pool
quietness-state is now tracked on UIFactory
59
    def test_be_quiet(self):
60
        self.factory.be_quiet(True)
61
        self.assertEquals(True, self.factory.is_quiet())
62
        self.factory.be_quiet(False)
63
        self.assertEquals(False, self.factory.is_quiet())
64
5416.1.1 by Martin Pool
Use a structured ui_factory.confirm_action rather than just get_boolean
65
    def test_confirm_action(self):
66
        # confirm_action should be answered by every ui factory; even
67
        # noninteractive ones should have a reasonable default
68
        self._load_responses([True])
69
        result = self.factory.confirm_action(
70
            'Break a lock?',
71
            'bzr.lock.break.confirm',
72
            {})
73
        # will be true either because we read it from the input or because
74
        # that's the default
75
        self.assertEquals(result, True)
76
4711.1.4 by Martin Pool
Change per_uifactory to use subclassing rather than scenarios
77
    def test_note(self):
78
        self.factory.note("a note to the user")
4711.1.7 by Martin Pool
Add UIFactory.show_error, show_warning, show_message
79
        self._check_note("a note to the user")
80
81
    def test_show_error(self):
82
        msg = 'an error occurred'
83
        self.factory.show_error(msg)
84
        self._check_show_error(msg)
4711.1.4 by Martin Pool
Change per_uifactory to use subclassing rather than scenarios
85
4711.1.8 by Martin Pool
Add show_warning and show_message tests and implementations
86
    def test_show_message(self):
87
        msg = 'a message'
88
        self.factory.show_message(msg)
89
        self._check_show_message(msg)
90
91
    def test_show_warning(self):
92
        msg = 'a warning'
93
        self.factory.show_warning(msg)
94
        self._check_show_warning(msg)
95
4792.8.2 by Martin Pool
New method ui_factory.make_output_stream
96
    def test_make_output_stream(self):
4960.3.1 by Martin Pool
SilentUIFactory now accepts make_output_stream and discards what is written
97
        # All UIs must now be able to at least accept output, even if they
98
        # just discard it.
99
        output_stream = self.factory.make_output_stream()
4792.8.2 by Martin Pool
New method ui_factory.make_output_stream
100
        output_stream.write('hello!')
101
4906.1.2 by John Arbash Meinel
Get the basic interface tested.
102
    def test_transport_activity(self):
103
        # It doesn't matter what the implementation does, we just want to make
104
        # sure the interface is there
105
        t = transport.get_transport('memory:///')
106
        self.factory.report_transport_activity(t, 1000, 'write')
107
        self.factory.report_transport_activity(t, 2000, 'read')
4906.1.4 by John Arbash Meinel
Play around with the ui display a bit more.
108
        self.factory.report_transport_activity(t, 4000, None)
4906.1.2 by John Arbash Meinel
Get the basic interface tested.
109
        self.factory.log_transport_activity()
110
        self._check_log_transport_activity_noarg()
111
        self.factory.log_transport_activity(display=True)
112
        self._check_log_transport_activity_display()
113
4906.1.7 by John Arbash Meinel
Switch to KiB/K for each value. Don't display if there are no bytes.
114
    def test_no_transport_activity(self):
115
        # No activity to report
116
        t = transport.get_transport('memory:///')
117
        self.factory.log_transport_activity(display=True)
118
        self._check_log_transport_activity_display_no_bytes()
119
4711.1.4 by Martin Pool
Change per_uifactory to use subclassing rather than scenarios
120
121
class TestTextUIFactory(tests.TestCase, UIFactoryTestMixin):
4711.1.2 by Martin Pool
Start adding tests.per_uifactory
122
123
    def setUp(self):
4711.1.4 by Martin Pool
Change per_uifactory to use subclassing rather than scenarios
124
        super(TestTextUIFactory, self).setUp()
4711.1.2 by Martin Pool
Start adding tests.per_uifactory
125
        self.stdin = StringIO()
126
        self.stdout = StringIO()
127
        self.stderr = StringIO()
128
        self.factory = ui.text.TextUIFactory(self.stdin, self.stdout,
129
            self.stderr)
130
4711.1.7 by Martin Pool
Add UIFactory.show_error, show_warning, show_message
131
    def _check_note(self, note_text):
4711.1.4 by Martin Pool
Change per_uifactory to use subclassing rather than scenarios
132
        self.assertEquals("%s\n" % note_text,
133
            self.stdout.getvalue())
4711.1.5 by Martin Pool
Add simple tests for CannedInputUIFactory and SilentUIFactory
134
4711.1.7 by Martin Pool
Add UIFactory.show_error, show_warning, show_message
135
    def _check_show_error(self, msg):
136
        self.assertEquals("bzr: error: %s\n" % msg,
137
            self.stderr.getvalue())
138
        self.assertEquals("", self.stdout.getvalue())
139
4711.1.8 by Martin Pool
Add show_warning and show_message tests and implementations
140
    def _check_show_message(self, msg):
141
        self.assertEquals("%s\n" % msg,
142
            self.stdout.getvalue())
143
        self.assertEquals("", self.stderr.getvalue())
144
145
    def _check_show_warning(self, msg):
146
        self.assertEquals("bzr: warning: %s\n" % msg,
147
            self.stderr.getvalue())
148
        self.assertEquals("", self.stdout.getvalue())
149
4906.1.2 by John Arbash Meinel
Get the basic interface tested.
150
    def _check_log_transport_activity_noarg(self):
151
        self.assertEqual('', self.stdout.getvalue())
4989.1.4 by Gordon Tyler
Updated tests for changes in output.
152
        self.assertContainsRe(self.stderr.getvalue(), r'\d+kB\s+\dkB/s |')
4906.1.10 by John Arbash Meinel
With the recent changes, transport activity doesn't get debounced.
153
        self.assertNotContainsRe(self.stderr.getvalue(), r'Transferred:')
4906.1.2 by John Arbash Meinel
Get the basic interface tested.
154
155
    def _check_log_transport_activity_display(self):
156
        self.assertEqual('', self.stdout.getvalue())
157
        # Without a TTY, we shouldn't display anything
158
        self.assertEqual('', self.stderr.getvalue())
159
4906.1.7 by John Arbash Meinel
Switch to KiB/K for each value. Don't display if there are no bytes.
160
    def _check_log_transport_activity_display_no_bytes(self):
161
        self.assertEqual('', self.stdout.getvalue())
162
        # Without a TTY, we shouldn't display anything
163
        self.assertEqual('', self.stderr.getvalue())
164
5416.1.1 by Martin Pool
Use a structured ui_factory.confirm_action rather than just get_boolean
165
    def _load_responses(self, responses):
166
        self.factory.stdin.seek(0)
167
        self.factory.stdin.writelines([(r and "y\n" or "n\n") for r in responses])
168
        self.factory.stdin.seek(0)
169
4906.1.2 by John Arbash Meinel
Get the basic interface tested.
170
171
class TestTTYTextUIFactory(TestTextUIFactory):
172
173
    def setUp(self):
174
        super(TestTTYTextUIFactory, self).setUp()
175
176
        class TTYStringIO(object):
177
            """Thunk over to StringIO() for everything but 'isatty'"""
178
179
            def __init__(self):
180
                self.__dict__['_sio'] = StringIO()
181
182
            def isatty(self):
183
                return True
184
185
            def __getattr__(self, name):
186
                return getattr(self._sio, name)
187
188
            def __setattr__(self, name, value):
189
                return setattr(self._sio, name, value)
4989.1.6 by Vincent Ladeuil
Add comments and update HACKING.txt about which units should be used.
190
4906.1.2 by John Arbash Meinel
Get the basic interface tested.
191
        # Remove 'TERM' == 'dumb' which causes us to *not* treat output as a
192
        # real terminal, even though isatty returns True
5570.3.6 by Vincent Ladeuil
Get rid of all _captureVar() calls, no test failures, pfew.
193
        self.overrideEnv('TERM', None)
4906.1.2 by John Arbash Meinel
Get the basic interface tested.
194
        self.stderr = TTYStringIO()
195
        self.stdout = TTYStringIO()
196
        self.factory = ui.text.TextUIFactory(self.stdin, self.stdout,
197
            self.stderr)
198
199
    def _check_log_transport_activity_display(self):
200
        self.assertEqual('', self.stdout.getvalue())
4989.1.6 by Vincent Ladeuil
Add comments and update HACKING.txt about which units should be used.
201
        # Displaying the result should write to the progress stream using
202
        # base-10 units (see HACKING.txt).
4906.1.5 by John Arbash Meinel
Include the KiB/s for the transfer.
203
        self.assertContainsRe(self.stderr.getvalue(),
4989.1.4 by Gordon Tyler
Updated tests for changes in output.
204
            r'Transferred: 7kB'
205
            r' \(\d+\.\dkB/s r:2kB w:1kB u:4kB\)')
4906.1.7 by John Arbash Meinel
Switch to KiB/K for each value. Don't display if there are no bytes.
206
207
    def _check_log_transport_activity_display_no_bytes(self):
208
        self.assertEqual('', self.stdout.getvalue())
209
        # Without actual bytes transferred, we should report nothing
210
        self.assertEqual('', self.stderr.getvalue())
4906.1.2 by John Arbash Meinel
Get the basic interface tested.
211
4711.1.5 by Martin Pool
Add simple tests for CannedInputUIFactory and SilentUIFactory
212
213
class TestSilentUIFactory(tests.TestCase, UIFactoryTestMixin):
214
    # discards output, therefore tests for output expect nothing
215
216
    def setUp(self):
217
        super(TestSilentUIFactory, self).setUp()
218
        self.factory = ui.SilentUIFactory()
219
4711.1.7 by Martin Pool
Add UIFactory.show_error, show_warning, show_message
220
    def _check_note(self, note_text):
4711.1.5 by Martin Pool
Add simple tests for CannedInputUIFactory and SilentUIFactory
221
        # it's just discarded
222
        pass
223
4711.1.7 by Martin Pool
Add UIFactory.show_error, show_warning, show_message
224
    def _check_show_error(self, msg):
225
        pass
226
4711.1.8 by Martin Pool
Add show_warning and show_message tests and implementations
227
    def _check_show_message(self, msg):
228
        pass
229
230
    def _check_show_warning(self, msg):
231
        pass
232
4906.1.2 by John Arbash Meinel
Get the basic interface tested.
233
    def _check_log_transport_activity_noarg(self):
234
        pass
235
236
    def _check_log_transport_activity_display(self):
237
        pass
238
4906.1.7 by John Arbash Meinel
Switch to KiB/K for each value. Don't display if there are no bytes.
239
    def _check_log_transport_activity_display_no_bytes(self):
240
        pass
241
5416.1.1 by Martin Pool
Use a structured ui_factory.confirm_action rather than just get_boolean
242
    def _load_responses(self, responses):
243
        pass
244
4711.1.5 by Martin Pool
Add simple tests for CannedInputUIFactory and SilentUIFactory
245
246
class TestCannedInputUIFactory(tests.TestCase, UIFactoryTestMixin):
247
    # discards output, reads input from variables
248
249
    def setUp(self):
250
        super(TestCannedInputUIFactory, self).setUp()
251
        self.factory = ui.CannedInputUIFactory([])
252
4711.1.7 by Martin Pool
Add UIFactory.show_error, show_warning, show_message
253
    def _check_note(self, note_text):
254
        pass
255
256
    def _check_show_error(self, msg):
257
        pass
258
4711.1.8 by Martin Pool
Add show_warning and show_message tests and implementations
259
    def _check_show_message(self, msg):
260
        pass
261
262
    def _check_show_warning(self, msg):
263
        pass
264
4906.1.2 by John Arbash Meinel
Get the basic interface tested.
265
    def _check_log_transport_activity_noarg(self):
266
        pass
4711.1.7 by Martin Pool
Add UIFactory.show_error, show_warning, show_message
267
4906.1.2 by John Arbash Meinel
Get the basic interface tested.
268
    def _check_log_transport_activity_display(self):
269
        pass
4906.1.7 by John Arbash Meinel
Switch to KiB/K for each value. Don't display if there are no bytes.
270
271
    def _check_log_transport_activity_display_no_bytes(self):
272
        pass
5416.1.1 by Martin Pool
Use a structured ui_factory.confirm_action rather than just get_boolean
273
274
    def _load_responses(self, responses):
275
        self.factory.responses.extend(responses)