~bzr-pqm/bzr/bzr.dev

« back to all changes in this revision

Viewing changes to bzrlib/tests/test_ui.py

  • Committer: Ian Clatworthy
  • Date: 2007-07-10 01:37:27 UTC
  • mto: This revision was merged to the branch mainline in revision 2598.
  • Revision ID: ian.clatworthy@internode.on.net-20070710013727-xrx8w91shojt1p4l
(Ian Clatworthy) Prepare for 0.19 development

Show diffs side-by-side

added added

removed removed

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