/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

merge bzr.dev.

Show diffs side-by-side

added added

removed removed

Lines of Context:
1
 
# Copyright (C) 2005 Canonical Ltd
 
1
# Copyright (C) 2005, 2008, 2009 Canonical Ltd
2
2
#
3
3
# This program is free software; you can redistribute it and/or modify
4
4
# it under the terms of the GNU General Public License as published by
12
12
#
13
13
# You should have received a copy of the GNU General Public License
14
14
# along with this program; if not, write to the Free Software
15
 
# Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307  USA
 
15
# Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
16
16
 
17
17
"""Tests for the bzrlib ui
18
18
"""
21
21
from StringIO import StringIO
22
22
import re
23
23
import sys
 
24
import time
24
25
 
25
26
import bzrlib
26
27
import bzrlib.errors as errors
27
28
from bzrlib.progress import (
28
29
    DotsProgressBar,
29
30
    ProgressBarStack,
 
31
    ProgressTask,
30
32
    TTYProgressBar,
31
33
    )
 
34
from bzrlib.symbol_versioning import (
 
35
    deprecated_in,
 
36
    )
32
37
from bzrlib.tests import (
33
38
    TestCase,
34
39
    TestUIFactory,
39
44
    CLIUIFactory,
40
45
    SilentUIFactory,
41
46
    )
42
 
from bzrlib.ui.text import TextUIFactory
 
47
from bzrlib.ui.text import (
 
48
    TextProgressView,
 
49
    TextUIFactory,
 
50
    )
43
51
 
44
52
 
45
53
class UITests(TestCase):
101
109
    def test_progress_note(self):
102
110
        stderr = StringIO()
103
111
        stdout = StringIO()
104
 
        ui_factory = TextUIFactory(bar_type=TTYProgressBar)
 
112
        ui_factory = TextUIFactory(stdin=StringIO(''),
 
113
            stderr=stderr,
 
114
            stdout=stdout)
105
115
        pb = ui_factory.nested_progress_bar()
106
116
        try:
107
 
            pb.to_messages_file = stdout
108
 
            ui_factory._progress_bar_stack.bottom().to_file = stderr
109
117
            result = pb.note('t')
110
118
            self.assertEqual(None, result)
111
119
            self.assertEqual("t\n", stdout.getvalue())
122
130
        # The PQM redirects the output to a file, so it
123
131
        # defaults to creating a Dots progress bar. we
124
132
        # need to force it to believe we are a TTY
125
 
        ui_factory = TextUIFactory(bar_type=TTYProgressBar)
 
133
        ui_factory = TextUIFactory(
 
134
            stdin=StringIO(''),
 
135
            stdout=stdout, stderr=stderr)
126
136
        pb = ui_factory.nested_progress_bar()
127
137
        try:
128
 
            pb.to_messages_file = stdout
129
 
            ui_factory._progress_bar_stack.bottom().to_file = stderr
130
138
            # Create a progress update that isn't throttled
131
 
            pb.start_time -= 10
132
139
            pb.update('x', 1, 1)
133
140
            result = pb.note('t')
134
141
            self.assertEqual(None, result)
142
149
 
143
150
    def test_progress_nested(self):
144
151
        # test factory based nested and popping.
145
 
        ui = TextUIFactory()
 
152
        ui = TextUIFactory(None, None, None)
146
153
        pb1 = ui.nested_progress_bar()
147
154
        pb2 = ui.nested_progress_bar()
148
 
        self.assertRaises(errors.MissingProgressBarFinish, pb1.finished)
 
155
        # You do get a warning if the outermost progress bar wasn't finished
 
156
        # first - it's not clear if this is really useful or if it should just
 
157
        # become orphaned -- mbp 20090120
 
158
        warnings, _ = self.callCatchWarnings(pb1.finished)
 
159
        if len(warnings) != 1:
 
160
            self.fail("unexpected warnings: %r" % (warnings,))
149
161
        pb2.finished()
150
162
        pb1.finished()
151
163
 
152
164
    def test_progress_stack(self):
153
 
        # test the progress bar stack which the default text factory 
 
165
        # test the progress bar stack which the default text factory
154
166
        # uses.
155
167
        stderr = StringIO()
156
168
        stdout = StringIO()
157
169
        # make a stack, which accepts parameters like a pb.
158
 
        stack = ProgressBarStack(to_file=stderr, to_messages_file=stdout)
 
170
        stack = self.applyDeprecated(
 
171
            deprecated_in((1, 12, 0)),
 
172
            ProgressBarStack,
 
173
            to_file=stderr, to_messages_file=stdout)
159
174
        # but is not one
160
175
        self.assertFalse(getattr(stack, 'note', False))
161
176
        pb1 = stack.get_nested()
162
177
        pb2 = stack.get_nested()
163
 
        self.assertRaises(errors.MissingProgressBarFinish, pb1.finished)
 
178
        warnings, _ = self.callCatchWarnings(pb1.finished)
 
179
        self.assertEqual(len(warnings), 1)
164
180
        pb2.finished()
165
181
        pb1.finished()
166
182
        # the text ui factory never actually removes the stack once its setup.
167
183
        # we need to be able to nest again correctly from here.
168
184
        pb1 = stack.get_nested()
169
185
        pb2 = stack.get_nested()
170
 
        self.assertRaises(errors.MissingProgressBarFinish, pb1.finished)
 
186
        warnings, _ = self.callCatchWarnings(pb1.finished)
 
187
        self.assertEqual(len(warnings), 1)
171
188
        pb2.finished()
172
189
        pb1.finished()
173
190
 
174
 
    def test_text_factory_setting_progress_bar(self):
175
 
        # we should be able to choose the progress bar type used.
176
 
        factory = TextUIFactory(bar_type=DotsProgressBar)
177
 
        bar = factory.nested_progress_bar()
178
 
        bar.finished()
179
 
        self.assertIsInstance(bar, DotsProgressBar)
180
 
 
181
 
    def test_cli_stdin_is_default_stdin(self):
182
 
        factory = CLIUIFactory()
183
 
        self.assertEqual(sys.stdin, factory.stdin)
184
 
 
185
191
    def assert_get_bool_acceptance_of_user_input(self, factory):
186
192
        factory.stdin = StringIO("y\nyes with garbage\n"
187
193
                                 "yes\nn\nnot an answer\n"
212
218
        self.assertEqual('', factory.stdin.readline())
213
219
 
214
220
    def test_text_ui_getbool(self):
215
 
        factory = TextUIFactory()
 
221
        factory = TextUIFactory(None, None, None)
216
222
        self.assert_get_bool_acceptance_of_user_input(factory)
217
223
 
 
224
    def test_text_factory_prompt(self):
 
225
        # see <https://launchpad.net/bugs/365891>
 
226
        factory = TextUIFactory(None, StringIO(), StringIO())
 
227
        factory.prompt('foo %2e')
 
228
 
218
229
    def test_text_factory_prompts_and_clears(self):
219
230
        # a get_boolean call should clear the pb before prompting
220
 
        factory = TextUIFactory(bar_type=DotsProgressBar)
221
 
        factory.stdout = _TTYStringIO()
222
 
        factory.stdin = StringIO("yada\ny\n")
223
 
        pb = self.apply_redirected(factory.stdin, factory.stdout,
224
 
                                   factory.stdout, factory.nested_progress_bar)
225
 
        pb.start_time = None
226
 
        self.apply_redirected(factory.stdin, factory.stdout,
227
 
                              factory.stdout, pb.update, "foo", 0, 1)
 
231
        out = _TTYStringIO()
 
232
        factory = TextUIFactory(stdin=StringIO("yada\ny\n"), stdout=out, stderr=out)
 
233
        pb = factory.nested_progress_bar()
 
234
        pb.show_bar = False
 
235
        pb.show_spinner = False
 
236
        pb.show_count = False
 
237
        pb.update("foo", 0, 1)
228
238
        self.assertEqual(True,
229
239
                         self.apply_redirected(None, factory.stdout,
230
240
                                               factory.stdout,
231
241
                                               factory.get_boolean,
232
242
                                               "what do you want"))
233
 
        output = factory.stdout.getvalue()
234
 
        self.assertEqual("foo: .\n"
235
 
                         "what do you want? [y/n]: what do you want? [y/n]: ",
236
 
                         factory.stdout.getvalue())
 
243
        output = out.getvalue()
 
244
        self.assertContainsRe(factory.stdout.getvalue(),
 
245
            "foo *\r\r  *\r*")
 
246
        self.assertContainsRe(factory.stdout.getvalue(),
 
247
            r"what do you want\? \[y/n\]: what do you want\? \[y/n\]: ")
 
248
        # stdin should have been totally consumed
 
249
        self.assertEqual('', factory.stdin.readline())
 
250
 
 
251
    def test_text_tick_after_update(self):
 
252
        ui_factory = TextUIFactory(stdout=StringIO(), stderr=StringIO())
 
253
        pb = ui_factory.nested_progress_bar()
 
254
        try:
 
255
            pb.update('task', 0, 3)
 
256
            # Reset the clock, so that it actually tries to repaint itself
 
257
            ui_factory._progress_view._last_repaint = time.time() - 1.0
 
258
            pb.tick()
 
259
        finally:
 
260
            pb.finished()
 
261
 
 
262
    def test_silent_ui_getusername(self):
 
263
        factory = SilentUIFactory()
 
264
        factory.stdin = StringIO("someuser\n\n")
 
265
        factory.stdout = StringIO()
 
266
        self.assertEquals(None, 
 
267
            factory.get_username(u'Hello\u1234 %(host)s', host=u'some\u1234'))
 
268
        self.assertEquals("", factory.stdout.getvalue())
 
269
        self.assertEquals("someuser\n\n", factory.stdin.getvalue())
 
270
 
 
271
    def test_text_ui_getusername(self):
 
272
        factory = TextUIFactory(None, None, None)
 
273
        factory.stdin = StringIO("someuser\n\n")
 
274
        factory.stdout = StringIO()
 
275
        factory.stdout.encoding = "utf8"
 
276
        # there is no output from the base factory
 
277
        self.assertEqual("someuser", 
 
278
            factory.get_username('Hello %(host)s', host='some'))
 
279
        self.assertEquals("Hello some: ", factory.stdout.getvalue())
 
280
        self.assertEqual("", factory.get_username("Gebruiker"))
237
281
        # stdin should be empty
238
282
        self.assertEqual('', factory.stdin.readline())
239
283
 
 
284
    def test_text_ui_getusername_utf8(self):
 
285
        ui = TestUIFactory(stdin=u'someuser\u1234'.encode('utf8'),
 
286
                           stdout=StringIOWrapper())
 
287
        ui.stdin.encoding = "utf8"
 
288
        ui.stdout.encoding = ui.stdin.encoding
 
289
        pb = ui.nested_progress_bar()
 
290
        try:
 
291
            # there is no output from the base factory
 
292
            username = self.apply_redirected(ui.stdin, ui.stdout, ui.stdout,
 
293
                ui.get_username, u'Hello\u1234 %(host)s', host=u'some\u1234')
 
294
            self.assertEquals(u"someuser\u1234", username.decode('utf8'))
 
295
            self.assertEquals(u"Hello\u1234 some\u1234: ", 
 
296
                ui.stdout.getvalue().decode("utf8"))
 
297
        finally:
 
298
            pb.finished()
 
299
 
 
300
 
 
301
class TestTextProgressView(TestCase):
 
302
    """Tests for text display of progress bars.
 
303
    """
 
304
    # XXX: These might be a bit easier to write if the rendering and
 
305
    # state-maintaining parts of TextProgressView were more separate, and if
 
306
    # the progress task called back directly to its own view not to the ui
 
307
    # factory. -- mbp 20090312
 
308
    
 
309
    def _make_factory(self):
 
310
        out = StringIO()
 
311
        uif = TextUIFactory(stderr=out)
 
312
        uif._progress_view._width = 80
 
313
        return out, uif
 
314
 
 
315
    def test_render_progress_easy(self):
 
316
        """Just one task and one quarter done"""
 
317
        out, uif = self._make_factory()
 
318
        task = uif.nested_progress_bar()
 
319
        task.update('reticulating splines', 5, 20)
 
320
        self.assertEqual(
 
321
'\r[####/               ] reticulating splines 5/20                               \r'
 
322
            , out.getvalue())
 
323
 
 
324
    def test_render_progress_nested(self):
 
325
        """Tasks proportionally contribute to overall progress"""
 
326
        out, uif = self._make_factory()
 
327
        task = uif.nested_progress_bar()
 
328
        task.update('reticulating splines', 0, 2)
 
329
        task2 = uif.nested_progress_bar()
 
330
        task2.update('stage2', 1, 2)
 
331
        # so we're in the first half of the main task, and half way through
 
332
        # that
 
333
        self.assertEqual(
 
334
r'[####\               ] reticulating splines:stage2 1/2'
 
335
            , uif._progress_view._render_line())
 
336
        # if the nested task is complete, then we're all the way through the
 
337
        # first half of the overall work
 
338
        task2.update('stage2', 2, 2)
 
339
        self.assertEqual(
 
340
r'[#########|          ] reticulating splines:stage2 2/2'
 
341
            , uif._progress_view._render_line())
 
342
 
 
343
    def test_render_progress_sub_nested(self):
 
344
        """Intermediate tasks don't mess up calculation."""
 
345
        out, uif = self._make_factory()
 
346
        task_a = uif.nested_progress_bar()
 
347
        task_a.update('a', 0, 2)
 
348
        task_b = uif.nested_progress_bar()
 
349
        task_b.update('b')
 
350
        task_c = uif.nested_progress_bar()
 
351
        task_c.update('c', 1, 2)
 
352
        # the top-level task is in its first half; the middle one has no
 
353
        # progress indication, just a label; and the bottom one is half done,
 
354
        # so the overall fraction is 1/4
 
355
        self.assertEqual(
 
356
            r'[####|               ] a:b:c 1/2'
 
357
            , uif._progress_view._render_line())
 
358