/brz/remove-bazaar

To get this branch, use:
bzr branch http://gegoxaren.bato24.eu/bzr/brz/remove-bazaar

« back to all changes in this revision

Viewing changes to bzrlib/tests/test_ui.py

  • Committer: Andrew Bennetts
  • Date: 2009-06-03 06:43:45 UTC
  • mfrom: (4400 +trunk)
  • mto: This revision was merged to the branch mainline in revision 4401.
  • Revision ID: andrew.bennetts@canonical.com-20090603064345-p44bv6u05mkmnox6
Merge, resolving NEWS conflict.

Show diffs side-by-side

added added

removed removed

Lines of Context:
 
1
# Copyright (C) 2005, 2008, 2009 Canonical Ltd
 
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
 
 
17
"""Tests for the bzrlib ui
 
18
"""
 
19
 
 
20
import os
 
21
from StringIO import StringIO
 
22
import re
 
23
import sys
 
24
import time
 
25
 
 
26
import bzrlib
 
27
import bzrlib.errors as errors
 
28
from bzrlib.progress import (
 
29
    DotsProgressBar,
 
30
    ProgressBarStack,
 
31
    ProgressTask,
 
32
    TTYProgressBar,
 
33
    )
 
34
from bzrlib.symbol_versioning import (
 
35
    deprecated_in,
 
36
    )
 
37
from bzrlib.tests import (
 
38
    TestCase,
 
39
    TestUIFactory,
 
40
    StringIOWrapper,
 
41
    )
 
42
from bzrlib.tests.test_progress import _TTYStringIO
 
43
from bzrlib.ui import (
 
44
    CLIUIFactory,
 
45
    SilentUIFactory,
 
46
    )
 
47
from bzrlib.ui.text import (
 
48
    TextProgressView,
 
49
    TextUIFactory,
 
50
    )
 
51
 
 
52
 
 
53
class UITests(TestCase):
 
54
 
 
55
    def test_silent_factory(self):
 
56
        ui = SilentUIFactory()
 
57
        stdout = StringIO()
 
58
        self.assertEqual(None,
 
59
                         self.apply_redirected(None, stdout, stdout,
 
60
                                               ui.get_password))
 
61
        self.assertEqual('', stdout.getvalue())
 
62
        self.assertEqual(None,
 
63
                         self.apply_redirected(None, stdout, stdout,
 
64
                                               ui.get_password,
 
65
                                               u'Hello\u1234 %(user)s',
 
66
                                               user=u'some\u1234'))
 
67
        self.assertEqual('', stdout.getvalue())
 
68
 
 
69
    def test_text_factory_ascii_password(self):
 
70
        ui = TestUIFactory(stdin='secret\n', stdout=StringIOWrapper(),
 
71
                           stderr=StringIOWrapper())
 
72
        pb = ui.nested_progress_bar()
 
73
        try:
 
74
            self.assertEqual('secret',
 
75
                             self.apply_redirected(ui.stdin, ui.stdout,
 
76
                                                   ui.stderr,
 
77
                                                   ui.get_password))
 
78
            # ': ' is appended to prompt
 
79
            self.assertEqual(': ', ui.stderr.getvalue())
 
80
            self.assertEqual('', ui.stdout.readline())
 
81
            # stdin should be empty
 
82
            self.assertEqual('', ui.stdin.readline())
 
83
        finally:
 
84
            pb.finished()
 
85
 
 
86
    def test_text_factory_utf8_password(self):
 
87
        """Test an utf8 password.
 
88
 
 
89
        We can't predict what encoding users will have for stdin, so we force
 
90
        it to utf8 to test that we transport the password correctly.
 
91
        """
 
92
        ui = TestUIFactory(stdin=u'baz\u1234'.encode('utf8'),
 
93
                           stdout=StringIOWrapper(),
 
94
                           stderr=StringIOWrapper())
 
95
        ui.stderr.encoding = ui.stdout.encoding = ui.stdin.encoding = 'utf8'
 
96
        pb = ui.nested_progress_bar()
 
97
        try:
 
98
            password = self.apply_redirected(ui.stdin, ui.stdout, ui.stderr,
 
99
                                             ui.get_password,
 
100
                                             u'Hello \u1234 %(user)s',
 
101
                                             user=u'some\u1234')
 
102
            # We use StringIO objects, we need to decode them
 
103
            self.assertEqual(u'baz\u1234', password.decode('utf8'))
 
104
            self.assertEqual(u'Hello \u1234 some\u1234: ',
 
105
                             ui.stderr.getvalue().decode('utf8'))
 
106
            # stdin and stdout should be empty
 
107
            self.assertEqual('', ui.stdin.readline())
 
108
            self.assertEqual('', ui.stdout.readline())
 
109
        finally:
 
110
            pb.finished()
 
111
 
 
112
    def test_progress_note(self):
 
113
        stderr = StringIO()
 
114
        stdout = StringIO()
 
115
        ui_factory = TextUIFactory(stdin=StringIO(''),
 
116
            stderr=stderr,
 
117
            stdout=stdout)
 
118
        pb = ui_factory.nested_progress_bar()
 
119
        try:
 
120
            result = pb.note('t')
 
121
            self.assertEqual(None, result)
 
122
            self.assertEqual("t\n", stdout.getvalue())
 
123
            # Since there was no update() call, there should be no clear() call
 
124
            self.failIf(re.search(r'^\r {10,}\r$',
 
125
                                  stderr.getvalue()) is not None,
 
126
                        'We cleared the stderr without anything to put there')
 
127
        finally:
 
128
            pb.finished()
 
129
 
 
130
    def test_progress_note_clears(self):
 
131
        stderr = StringIO()
 
132
        stdout = StringIO()
 
133
        # The PQM redirects the output to a file, so it
 
134
        # defaults to creating a Dots progress bar. we
 
135
        # need to force it to believe we are a TTY
 
136
        ui_factory = TextUIFactory(
 
137
            stdin=StringIO(''),
 
138
            stdout=stdout, stderr=stderr)
 
139
        pb = ui_factory.nested_progress_bar()
 
140
        try:
 
141
            # Create a progress update that isn't throttled
 
142
            pb.update('x', 1, 1)
 
143
            result = pb.note('t')
 
144
            self.assertEqual(None, result)
 
145
            self.assertEqual("t\n", stdout.getvalue())
 
146
            # the exact contents will depend on the terminal width and we don't
 
147
            # care about that right now - but you're probably running it on at
 
148
            # least a 10-character wide terminal :)
 
149
            self.assertContainsRe(stderr.getvalue(), r'\r {10,}\r$')
 
150
        finally:
 
151
            pb.finished()
 
152
 
 
153
    def test_progress_nested(self):
 
154
        # test factory based nested and popping.
 
155
        ui = TextUIFactory(None, None, None)
 
156
        pb1 = ui.nested_progress_bar()
 
157
        pb2 = ui.nested_progress_bar()
 
158
        # You do get a warning if the outermost progress bar wasn't finished
 
159
        # first - it's not clear if this is really useful or if it should just
 
160
        # become orphaned -- mbp 20090120
 
161
        warnings, _ = self.callCatchWarnings(pb1.finished)
 
162
        if len(warnings) != 1:
 
163
            self.fail("unexpected warnings: %r" % (warnings,))
 
164
        pb2.finished()
 
165
        pb1.finished()
 
166
 
 
167
    def test_progress_stack(self):
 
168
        # test the progress bar stack which the default text factory
 
169
        # uses.
 
170
        stderr = StringIO()
 
171
        stdout = StringIO()
 
172
        # make a stack, which accepts parameters like a pb.
 
173
        stack = self.applyDeprecated(
 
174
            deprecated_in((1, 12, 0)),
 
175
            ProgressBarStack,
 
176
            to_file=stderr, to_messages_file=stdout)
 
177
        # but is not one
 
178
        self.assertFalse(getattr(stack, 'note', False))
 
179
        pb1 = stack.get_nested()
 
180
        pb2 = stack.get_nested()
 
181
        warnings, _ = self.callCatchWarnings(pb1.finished)
 
182
        self.assertEqual(len(warnings), 1)
 
183
        pb2.finished()
 
184
        pb1.finished()
 
185
        # the text ui factory never actually removes the stack once its setup.
 
186
        # we need to be able to nest again correctly from here.
 
187
        pb1 = stack.get_nested()
 
188
        pb2 = stack.get_nested()
 
189
        warnings, _ = self.callCatchWarnings(pb1.finished)
 
190
        self.assertEqual(len(warnings), 1)
 
191
        pb2.finished()
 
192
        pb1.finished()
 
193
 
 
194
    def assert_get_bool_acceptance_of_user_input(self, factory):
 
195
        factory.stdin = StringIO("y\nyes with garbage\n"
 
196
                                 "yes\nn\nnot an answer\n"
 
197
                                 "no\nfoo\n")
 
198
        factory.stdout = StringIO()
 
199
        factory.stderr = StringIO()
 
200
        # there is no output from the base factory
 
201
        self.assertEqual(True, factory.get_boolean(""))
 
202
        self.assertEqual(True, factory.get_boolean(""))
 
203
        self.assertEqual(False, factory.get_boolean(""))
 
204
        self.assertEqual(False, factory.get_boolean(""))
 
205
        self.assertEqual("foo\n", factory.stdin.read())
 
206
        # stdin should be empty
 
207
        self.assertEqual('', factory.stdin.readline())
 
208
 
 
209
    def test_silent_ui_getbool(self):
 
210
        factory = SilentUIFactory()
 
211
        self.assert_get_bool_acceptance_of_user_input(factory)
 
212
 
 
213
    def test_silent_factory_prompts_silently(self):
 
214
        factory = SilentUIFactory()
 
215
        stdout = StringIO()
 
216
        factory.stdin = StringIO("y\n")
 
217
        self.assertEqual(True,
 
218
                         self.apply_redirected(None, stdout, stdout,
 
219
                                               factory.get_boolean, "foo"))
 
220
        self.assertEqual("", stdout.getvalue())
 
221
        # stdin should be empty
 
222
        self.assertEqual('', factory.stdin.readline())
 
223
 
 
224
    def test_text_ui_getbool(self):
 
225
        factory = TextUIFactory(None, None, None)
 
226
        self.assert_get_bool_acceptance_of_user_input(factory)
 
227
 
 
228
    def test_text_factory_prompt(self):
 
229
        # see <https://launchpad.net/bugs/365891>
 
230
        factory = TextUIFactory(None, StringIO(), StringIO(), StringIO())
 
231
        factory.prompt('foo %2e')
 
232
        self.assertEqual('', factory.stdout.getvalue())
 
233
        self.assertEqual('foo %2e', factory.stderr.getvalue())
 
234
 
 
235
    def test_text_factory_prompts_and_clears(self):
 
236
        # a get_boolean call should clear the pb before prompting
 
237
        out = _TTYStringIO()
 
238
        factory = TextUIFactory(stdin=StringIO("yada\ny\n"), stdout=out, stderr=out)
 
239
        pb = factory.nested_progress_bar()
 
240
        pb.show_bar = False
 
241
        pb.show_spinner = False
 
242
        pb.show_count = False
 
243
        pb.update("foo", 0, 1)
 
244
        self.assertEqual(True,
 
245
                         self.apply_redirected(None, factory.stdout,
 
246
                                               factory.stdout,
 
247
                                               factory.get_boolean,
 
248
                                               "what do you want"))
 
249
        output = out.getvalue()
 
250
        self.assertContainsRe(factory.stdout.getvalue(),
 
251
            "foo *\r\r  *\r*")
 
252
        self.assertContainsRe(factory.stdout.getvalue(),
 
253
            r"what do you want\? \[y/n\]: what do you want\? \[y/n\]: ")
 
254
        # stdin should have been totally consumed
 
255
        self.assertEqual('', factory.stdin.readline())
 
256
 
 
257
    def test_text_tick_after_update(self):
 
258
        ui_factory = TextUIFactory(stdout=StringIO(), stderr=StringIO())
 
259
        pb = ui_factory.nested_progress_bar()
 
260
        try:
 
261
            pb.update('task', 0, 3)
 
262
            # Reset the clock, so that it actually tries to repaint itself
 
263
            ui_factory._progress_view._last_repaint = time.time() - 1.0
 
264
            pb.tick()
 
265
        finally:
 
266
            pb.finished()
 
267
 
 
268
    def test_silent_ui_getusername(self):
 
269
        factory = SilentUIFactory()
 
270
        factory.stdin = StringIO("someuser\n\n")
 
271
        factory.stdout = StringIO()
 
272
        factory.stderr = StringIO()
 
273
        self.assertEquals(None,
 
274
            factory.get_username(u'Hello\u1234 %(host)s', host=u'some\u1234'))
 
275
        self.assertEquals("", factory.stdout.getvalue())
 
276
        self.assertEquals("", factory.stderr.getvalue())
 
277
        self.assertEquals("someuser\n\n", factory.stdin.getvalue())
 
278
 
 
279
    def test_text_ui_getusername(self):
 
280
        factory = TextUIFactory(None, None, None)
 
281
        factory.stdin = StringIO("someuser\n\n")
 
282
        factory.stdout = StringIO()
 
283
        factory.stderr = StringIO()
 
284
        factory.stdout.encoding = "utf8"
 
285
        # there is no output from the base factory
 
286
        self.assertEqual("someuser",
 
287
                         factory.get_username('Hello %(host)s', host='some'))
 
288
        self.assertEquals("Hello some: ", factory.stderr.getvalue())
 
289
        self.assertEquals('', factory.stdout.getvalue())
 
290
        self.assertEqual("", factory.get_username("Gebruiker"))
 
291
        # stdin should be empty
 
292
        self.assertEqual('', factory.stdin.readline())
 
293
 
 
294
    def test_text_ui_getusername_utf8(self):
 
295
        ui = TestUIFactory(stdin=u'someuser\u1234'.encode('utf8'),
 
296
                           stdout=StringIOWrapper(), stderr=StringIOWrapper())
 
297
        ui.stderr.encoding = ui.stdout.encoding = ui.stdin.encoding = "utf8"
 
298
        pb = ui.nested_progress_bar()
 
299
        try:
 
300
            # there is no output from the base factory
 
301
            username = self.apply_redirected(ui.stdin, ui.stdout, ui.stderr,
 
302
                ui.get_username, u'Hello\u1234 %(host)s', host=u'some\u1234')
 
303
            self.assertEquals(u"someuser\u1234", username.decode('utf8'))
 
304
            self.assertEquals(u"Hello\u1234 some\u1234: ",
 
305
                              ui.stderr.getvalue().decode("utf8"))
 
306
            self.assertEquals('', ui.stdout.getvalue())
 
307
        finally:
 
308
            pb.finished()
 
309
 
 
310
 
 
311
class TestTextProgressView(TestCase):
 
312
    """Tests for text display of progress bars.
 
313
    """
 
314
    # XXX: These might be a bit easier to write if the rendering and
 
315
    # state-maintaining parts of TextProgressView were more separate, and if
 
316
    # the progress task called back directly to its own view not to the ui
 
317
    # factory. -- mbp 20090312
 
318
    
 
319
    def _make_factory(self):
 
320
        out = StringIO()
 
321
        uif = TextUIFactory(stderr=out)
 
322
        uif._progress_view._width = 80
 
323
        return out, uif
 
324
 
 
325
    def test_render_progress_easy(self):
 
326
        """Just one task and one quarter done"""
 
327
        out, uif = self._make_factory()
 
328
        task = uif.nested_progress_bar()
 
329
        task.update('reticulating splines', 5, 20)
 
330
        self.assertEqual(
 
331
'\r[####/               ] reticulating splines 5/20                               \r'
 
332
            , out.getvalue())
 
333
 
 
334
    def test_render_progress_nested(self):
 
335
        """Tasks proportionally contribute to overall progress"""
 
336
        out, uif = self._make_factory()
 
337
        task = uif.nested_progress_bar()
 
338
        task.update('reticulating splines', 0, 2)
 
339
        task2 = uif.nested_progress_bar()
 
340
        task2.update('stage2', 1, 2)
 
341
        # so we're in the first half of the main task, and half way through
 
342
        # that
 
343
        self.assertEqual(
 
344
r'[####\               ] reticulating splines:stage2 1/2'
 
345
            , uif._progress_view._render_line())
 
346
        # if the nested task is complete, then we're all the way through the
 
347
        # first half of the overall work
 
348
        task2.update('stage2', 2, 2)
 
349
        self.assertEqual(
 
350
r'[#########|          ] reticulating splines:stage2 2/2'
 
351
            , uif._progress_view._render_line())
 
352
 
 
353
    def test_render_progress_sub_nested(self):
 
354
        """Intermediate tasks don't mess up calculation."""
 
355
        out, uif = self._make_factory()
 
356
        task_a = uif.nested_progress_bar()
 
357
        task_a.update('a', 0, 2)
 
358
        task_b = uif.nested_progress_bar()
 
359
        task_b.update('b')
 
360
        task_c = uif.nested_progress_bar()
 
361
        task_c.update('c', 1, 2)
 
362
        # the top-level task is in its first half; the middle one has no
 
363
        # progress indication, just a label; and the bottom one is half done,
 
364
        # so the overall fraction is 1/4
 
365
        self.assertEqual(
 
366
            r'[####|               ] a:b:c 1/2'
 
367
            , uif._progress_view._render_line())
 
368