/brz/remove-bazaar

To get this branch, use:
bzr branch http://gegoxaren.bato24.eu/bzr/brz/remove-bazaar
6614.1.3 by Vincent Ladeuil
Fix assertEquals being deprecated by using assertEqual.
1
# Copyright (C) 2009, 2010, 2011, 2016 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
import unittest
37
6624 by Jelmer Vernooij
Merge Python3 porting work ('py3 pokes')
38
from ... import (
4711.1.2 by Martin Pool
Start adding tests.per_uifactory
39
    tests,
4906.1.2 by John Arbash Meinel
Get the basic interface tested.
40
    transport,
4711.1.2 by Martin Pool
Start adding tests.per_uifactory
41
    ui,
42
    )
43
6624 by Jelmer Vernooij
Merge Python3 porting work ('py3 pokes')
44
from ..ui_testing import (
6621.22.2 by Martin
Use BytesIO or StringIO from bzrlib.sixish
45
    StringIOWithEncoding,
46
    StringIOAsTTY,
47
    )
48
4711.1.2 by Martin Pool
Start adding tests.per_uifactory
49
4711.1.4 by Martin Pool
Change per_uifactory to use subclassing rather than scenarios
50
class UIFactoryTestMixin(object):
51
    """Common tests for UIFactories.
52
53
    These are supposed to be expressed with no assumptions about how the
54
    UIFactory implements the method, only that it does implement them (or
55
    fails cleanly), and that the concrete subclass will make arrangements to
56
    build a factory and to examine its behaviour.
57
58
    Note that this is *not* a TestCase, because it can't be directly run, but
59
    the concrete subclasses should be.
60
    """
61
4961.1.2 by Martin Pool
quietness-state is now tracked on UIFactory
62
    def test_be_quiet(self):
63
        self.factory.be_quiet(True)
6614.1.3 by Vincent Ladeuil
Fix assertEquals being deprecated by using assertEqual.
64
        self.assertEqual(True, self.factory.is_quiet())
4961.1.2 by Martin Pool
quietness-state is now tracked on UIFactory
65
        self.factory.be_quiet(False)
6614.1.3 by Vincent Ladeuil
Fix assertEquals being deprecated by using assertEqual.
66
        self.assertEqual(False, self.factory.is_quiet())
4961.1.2 by Martin Pool
quietness-state is now tracked on UIFactory
67
5416.1.1 by Martin Pool
Use a structured ui_factory.confirm_action rather than just get_boolean
68
    def test_confirm_action(self):
69
        # confirm_action should be answered by every ui factory; even
70
        # noninteractive ones should have a reasonable default
71
        self._load_responses([True])
5863.6.2 by Jelmer Vernooij
Fix more.
72
        result = self.factory.confirm_action(u'Break a lock?',
73
             'bzr.lock.break.confirm',
5416.1.1 by Martin Pool
Use a structured ui_factory.confirm_action rather than just get_boolean
74
            {})
75
        # will be true either because we read it from the input or because
76
        # that's the default
6614.1.3 by Vincent Ladeuil
Fix assertEquals being deprecated by using assertEqual.
77
        self.assertEqual(result, True)
5416.1.1 by Martin Pool
Use a structured ui_factory.confirm_action rather than just get_boolean
78
4711.1.4 by Martin Pool
Change per_uifactory to use subclassing rather than scenarios
79
    def test_note(self):
80
        self.factory.note("a note to the user")
4711.1.7 by Martin Pool
Add UIFactory.show_error, show_warning, show_message
81
        self._check_note("a note to the user")
82
83
    def test_show_error(self):
84
        msg = 'an error occurred'
85
        self.factory.show_error(msg)
86
        self._check_show_error(msg)
4711.1.4 by Martin Pool
Change per_uifactory to use subclassing rather than scenarios
87
4711.1.8 by Martin Pool
Add show_warning and show_message tests and implementations
88
    def test_show_message(self):
89
        msg = 'a message'
90
        self.factory.show_message(msg)
91
        self._check_show_message(msg)
92
93
    def test_show_warning(self):
94
        msg = 'a warning'
95
        self.factory.show_warning(msg)
96
        self._check_show_warning(msg)
97
4792.8.2 by Martin Pool
New method ui_factory.make_output_stream
98
    def test_make_output_stream(self):
4960.3.1 by Martin Pool
SilentUIFactory now accepts make_output_stream and discards what is written
99
        # All UIs must now be able to at least accept output, even if they
100
        # just discard it.
101
        output_stream = self.factory.make_output_stream()
4792.8.2 by Martin Pool
New method ui_factory.make_output_stream
102
        output_stream.write('hello!')
103
4906.1.2 by John Arbash Meinel
Get the basic interface tested.
104
    def test_transport_activity(self):
105
        # It doesn't matter what the implementation does, we just want to make
106
        # sure the interface is there
6083.1.1 by Jelmer Vernooij
Use get_transport_from_{url,path} in more places.
107
        t = transport.get_transport_from_url('memory:///')
4906.1.2 by John Arbash Meinel
Get the basic interface tested.
108
        self.factory.report_transport_activity(t, 1000, 'write')
109
        self.factory.report_transport_activity(t, 2000, 'read')
4906.1.4 by John Arbash Meinel
Play around with the ui display a bit more.
110
        self.factory.report_transport_activity(t, 4000, None)
4906.1.2 by John Arbash Meinel
Get the basic interface tested.
111
        self.factory.log_transport_activity()
112
        self._check_log_transport_activity_noarg()
113
        self.factory.log_transport_activity(display=True)
114
        self._check_log_transport_activity_display()
115
4906.1.7 by John Arbash Meinel
Switch to KiB/K for each value. Don't display if there are no bytes.
116
    def test_no_transport_activity(self):
117
        # No activity to report
6083.1.1 by Jelmer Vernooij
Use get_transport_from_{url,path} in more places.
118
        t = transport.get_transport_from_url('memory:///')
4906.1.7 by John Arbash Meinel
Switch to KiB/K for each value. Don't display if there are no bytes.
119
        self.factory.log_transport_activity(display=True)
120
        self._check_log_transport_activity_display_no_bytes()
121
4711.1.4 by Martin Pool
Change per_uifactory to use subclassing rather than scenarios
122
123
class TestTextUIFactory(tests.TestCase, UIFactoryTestMixin):
4711.1.2 by Martin Pool
Start adding tests.per_uifactory
124
125
    def setUp(self):
4711.1.4 by Martin Pool
Change per_uifactory to use subclassing rather than scenarios
126
        super(TestTextUIFactory, self).setUp()
6621.22.2 by Martin
Use BytesIO or StringIO from bzrlib.sixish
127
        self.stdin = StringIOWithEncoding()
128
        self.stdout = StringIOWithEncoding()
129
        self.stderr = StringIOWithEncoding()
4711.1.2 by Martin Pool
Start adding tests.per_uifactory
130
        self.factory = ui.text.TextUIFactory(self.stdin, self.stdout,
131
            self.stderr)
132
4711.1.7 by Martin Pool
Add UIFactory.show_error, show_warning, show_message
133
    def _check_note(self, note_text):
6614.1.3 by Vincent Ladeuil
Fix assertEquals being deprecated by using assertEqual.
134
        self.assertEqual("%s\n" % note_text,
4711.1.4 by Martin Pool
Change per_uifactory to use subclassing rather than scenarios
135
            self.stdout.getvalue())
4711.1.5 by Martin Pool
Add simple tests for CannedInputUIFactory and SilentUIFactory
136
4711.1.7 by Martin Pool
Add UIFactory.show_error, show_warning, show_message
137
    def _check_show_error(self, msg):
6614.1.3 by Vincent Ladeuil
Fix assertEquals being deprecated by using assertEqual.
138
        self.assertEqual("bzr: error: %s\n" % msg,
4711.1.7 by Martin Pool
Add UIFactory.show_error, show_warning, show_message
139
            self.stderr.getvalue())
6614.1.3 by Vincent Ladeuil
Fix assertEquals being deprecated by using assertEqual.
140
        self.assertEqual("", self.stdout.getvalue())
4711.1.7 by Martin Pool
Add UIFactory.show_error, show_warning, show_message
141
4711.1.8 by Martin Pool
Add show_warning and show_message tests and implementations
142
    def _check_show_message(self, msg):
6614.1.3 by Vincent Ladeuil
Fix assertEquals being deprecated by using assertEqual.
143
        self.assertEqual("%s\n" % msg,
4711.1.8 by Martin Pool
Add show_warning and show_message tests and implementations
144
            self.stdout.getvalue())
6614.1.3 by Vincent Ladeuil
Fix assertEquals being deprecated by using assertEqual.
145
        self.assertEqual("", self.stderr.getvalue())
4711.1.8 by Martin Pool
Add show_warning and show_message tests and implementations
146
147
    def _check_show_warning(self, msg):
6614.1.3 by Vincent Ladeuil
Fix assertEquals being deprecated by using assertEqual.
148
        self.assertEqual("bzr: warning: %s\n" % msg,
4711.1.8 by Martin Pool
Add show_warning and show_message tests and implementations
149
            self.stderr.getvalue())
6614.1.3 by Vincent Ladeuil
Fix assertEquals being deprecated by using assertEqual.
150
        self.assertEqual("", self.stdout.getvalue())
4711.1.8 by Martin Pool
Add show_warning and show_message tests and implementations
151
4906.1.2 by John Arbash Meinel
Get the basic interface tested.
152
    def _check_log_transport_activity_noarg(self):
153
        self.assertEqual('', self.stdout.getvalue())
4989.1.4 by Gordon Tyler
Updated tests for changes in output.
154
        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.
155
        self.assertNotContainsRe(self.stderr.getvalue(), r'Transferred:')
4906.1.2 by John Arbash Meinel
Get the basic interface tested.
156
157
    def _check_log_transport_activity_display(self):
158
        self.assertEqual('', self.stdout.getvalue())
159
        # Without a TTY, we shouldn't display anything
160
        self.assertEqual('', self.stderr.getvalue())
161
4906.1.7 by John Arbash Meinel
Switch to KiB/K for each value. Don't display if there are no bytes.
162
    def _check_log_transport_activity_display_no_bytes(self):
163
        self.assertEqual('', self.stdout.getvalue())
164
        # Without a TTY, we shouldn't display anything
165
        self.assertEqual('', self.stderr.getvalue())
166
5416.1.1 by Martin Pool
Use a structured ui_factory.confirm_action rather than just get_boolean
167
    def _load_responses(self, responses):
168
        self.factory.stdin.seek(0)
169
        self.factory.stdin.writelines([(r and "y\n" or "n\n") for r in responses])
170
        self.factory.stdin.seek(0)
171
4906.1.2 by John Arbash Meinel
Get the basic interface tested.
172
173
class TestTTYTextUIFactory(TestTextUIFactory):
174
175
    def setUp(self):
176
        super(TestTTYTextUIFactory, self).setUp()
177
178
        # Remove 'TERM' == 'dumb' which causes us to *not* treat output as a
179
        # real terminal, even though isatty returns True
5570.3.6 by Vincent Ladeuil
Get rid of all _captureVar() calls, no test failures, pfew.
180
        self.overrideEnv('TERM', None)
6621.22.2 by Martin
Use BytesIO or StringIO from bzrlib.sixish
181
        self.stderr = StringIOAsTTY()
182
        self.stdout = StringIOAsTTY()
4906.1.2 by John Arbash Meinel
Get the basic interface tested.
183
        self.factory = ui.text.TextUIFactory(self.stdin, self.stdout,
184
            self.stderr)
185
186
    def _check_log_transport_activity_display(self):
187
        self.assertEqual('', self.stdout.getvalue())
4989.1.6 by Vincent Ladeuil
Add comments and update HACKING.txt about which units should be used.
188
        # Displaying the result should write to the progress stream using
189
        # base-10 units (see HACKING.txt).
4906.1.5 by John Arbash Meinel
Include the KiB/s for the transfer.
190
        self.assertContainsRe(self.stderr.getvalue(),
4989.1.4 by Gordon Tyler
Updated tests for changes in output.
191
            r'Transferred: 7kB'
192
            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.
193
194
    def _check_log_transport_activity_display_no_bytes(self):
195
        self.assertEqual('', self.stdout.getvalue())
196
        # Without actual bytes transferred, we should report nothing
197
        self.assertEqual('', self.stderr.getvalue())
4906.1.2 by John Arbash Meinel
Get the basic interface tested.
198
4711.1.5 by Martin Pool
Add simple tests for CannedInputUIFactory and SilentUIFactory
199
200
class TestSilentUIFactory(tests.TestCase, UIFactoryTestMixin):
201
    # discards output, therefore tests for output expect nothing
202
203
    def setUp(self):
204
        super(TestSilentUIFactory, self).setUp()
205
        self.factory = ui.SilentUIFactory()
206
4711.1.7 by Martin Pool
Add UIFactory.show_error, show_warning, show_message
207
    def _check_note(self, note_text):
4711.1.5 by Martin Pool
Add simple tests for CannedInputUIFactory and SilentUIFactory
208
        # it's just discarded
209
        pass
210
4711.1.7 by Martin Pool
Add UIFactory.show_error, show_warning, show_message
211
    def _check_show_error(self, msg):
212
        pass
213
4711.1.8 by Martin Pool
Add show_warning and show_message tests and implementations
214
    def _check_show_message(self, msg):
215
        pass
216
217
    def _check_show_warning(self, msg):
218
        pass
219
4906.1.2 by John Arbash Meinel
Get the basic interface tested.
220
    def _check_log_transport_activity_noarg(self):
221
        pass
222
223
    def _check_log_transport_activity_display(self):
224
        pass
225
4906.1.7 by John Arbash Meinel
Switch to KiB/K for each value. Don't display if there are no bytes.
226
    def _check_log_transport_activity_display_no_bytes(self):
227
        pass
228
5416.1.1 by Martin Pool
Use a structured ui_factory.confirm_action rather than just get_boolean
229
    def _load_responses(self, responses):
230
        pass
231
4711.1.5 by Martin Pool
Add simple tests for CannedInputUIFactory and SilentUIFactory
232
233
class TestCannedInputUIFactory(tests.TestCase, UIFactoryTestMixin):
234
    # discards output, reads input from variables
235
236
    def setUp(self):
237
        super(TestCannedInputUIFactory, self).setUp()
238
        self.factory = ui.CannedInputUIFactory([])
239
4711.1.7 by Martin Pool
Add UIFactory.show_error, show_warning, show_message
240
    def _check_note(self, note_text):
241
        pass
242
243
    def _check_show_error(self, msg):
244
        pass
245
4711.1.8 by Martin Pool
Add show_warning and show_message tests and implementations
246
    def _check_show_message(self, msg):
247
        pass
248
249
    def _check_show_warning(self, msg):
250
        pass
251
4906.1.2 by John Arbash Meinel
Get the basic interface tested.
252
    def _check_log_transport_activity_noarg(self):
253
        pass
4711.1.7 by Martin Pool
Add UIFactory.show_error, show_warning, show_message
254
4906.1.2 by John Arbash Meinel
Get the basic interface tested.
255
    def _check_log_transport_activity_display(self):
256
        pass
4906.1.7 by John Arbash Meinel
Switch to KiB/K for each value. Don't display if there are no bytes.
257
258
    def _check_log_transport_activity_display_no_bytes(self):
259
        pass
5416.1.1 by Martin Pool
Use a structured ui_factory.confirm_action rather than just get_boolean
260
261
    def _load_responses(self, responses):
262
        self.factory.responses.extend(responses)