/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: Martin Pool
  • Date: 2009-07-17 10:38:41 UTC
  • mfrom: (4536 +trunk)
  • mto: This revision was merged to the branch mainline in revision 4558.
  • Revision ID: mbp@sourcefrog.net-20090717103841-z35onk04bkiw7zb6
Merge trunk

Show diffs side-by-side

added added

removed removed

Lines of Context:
23
23
import sys
24
24
import time
25
25
 
26
 
import bzrlib
27
 
import bzrlib.errors as errors
28
 
from bzrlib.progress import (
29
 
    ProgressTask,
 
26
from bzrlib import (
 
27
    errors,
 
28
    tests,
 
29
    ui as _mod_ui,
30
30
    )
31
31
from bzrlib.symbol_versioning import (
32
32
    deprecated_in,
53
53
    )
54
54
 
55
55
 
56
 
class UITests(TestCase):
 
56
class UITests(tests.TestCase):
 
57
 
 
58
    def test_silent_factory(self):
 
59
        ui = _mod_ui.SilentUIFactory()
 
60
        stdout = StringIO()
 
61
        self.assertEqual(None,
 
62
                         self.apply_redirected(None, stdout, stdout,
 
63
                                               ui.get_password))
 
64
        self.assertEqual('', stdout.getvalue())
 
65
        self.assertEqual(None,
 
66
                         self.apply_redirected(None, stdout, stdout,
 
67
                                               ui.get_password,
 
68
                                               u'Hello\u1234 %(user)s',
 
69
                                               user=u'some\u1234'))
 
70
        self.assertEqual('', stdout.getvalue())
57
71
 
58
72
    def test_text_factory_ascii_password(self):
59
 
        ui = TestUIFactory(stdin='secret\n', stdout=StringIOWrapper(),
60
 
                           stderr=StringIOWrapper())
 
73
        ui = tests.TestUIFactory(stdin='secret\n',
 
74
                                 stdout=tests.StringIOWrapper(),
 
75
                                 stderr=tests.StringIOWrapper())
61
76
        pb = ui.nested_progress_bar()
62
77
        try:
63
78
            self.assertEqual('secret',
78
93
        We can't predict what encoding users will have for stdin, so we force
79
94
        it to utf8 to test that we transport the password correctly.
80
95
        """
81
 
        ui = TestUIFactory(stdin=u'baz\u1234'.encode('utf8'),
82
 
                           stdout=StringIOWrapper(),
83
 
                           stderr=StringIOWrapper())
 
96
        ui = tests.TestUIFactory(stdin=u'baz\u1234'.encode('utf8'),
 
97
                                 stdout=tests.StringIOWrapper(),
 
98
                                 stderr=tests.StringIOWrapper())
84
99
        ui.stderr.encoding = ui.stdout.encoding = ui.stdin.encoding = 'utf8'
85
100
        pb = ui.nested_progress_bar()
86
101
        try:
199
214
    def assert_get_bool_acceptance_of_user_input(self, factory):
200
215
        factory.stdin = StringIO("y\nyes with garbage\n"
201
216
                                 "yes\nn\nnot an answer\n"
202
 
                                 "no\nfoo\n")
 
217
                                 "no\n"
 
218
                                 "N\nY\n"
 
219
                                 "foo\n"
 
220
                                )
203
221
        factory.stdout = StringIO()
204
222
        factory.stderr = StringIO()
205
223
        # there is no output from the base factory
207
225
        self.assertEqual(True, factory.get_boolean(""))
208
226
        self.assertEqual(False, factory.get_boolean(""))
209
227
        self.assertEqual(False, factory.get_boolean(""))
 
228
        self.assertEqual(False, factory.get_boolean(""))
 
229
        self.assertEqual(True, factory.get_boolean(""))
210
230
        self.assertEqual("foo\n", factory.stdin.read())
211
231
        # stdin should be empty
212
232
        self.assertEqual('', factory.stdin.readline())
213
233
 
 
234
    def test_silent_ui_getbool(self):
 
235
        factory = _mod_ui.SilentUIFactory()
 
236
        self.assert_get_bool_acceptance_of_user_input(factory)
 
237
 
 
238
    def test_silent_factory_prompts_silently(self):
 
239
        factory = _mod_ui.SilentUIFactory()
 
240
        stdout = StringIO()
 
241
        factory.stdin = StringIO("y\n")
 
242
        self.assertEqual(True,
 
243
                         self.apply_redirected(None, stdout, stdout,
 
244
                                               factory.get_boolean, "foo"))
 
245
        self.assertEqual("", stdout.getvalue())
 
246
        # stdin should be empty
 
247
        self.assertEqual('', factory.stdin.readline())
 
248
 
214
249
    def test_text_ui_getbool(self):
215
250
        factory = TextUIFactory(None, None, None)
216
251
        self.assert_get_bool_acceptance_of_user_input(factory)
272
307
        self.assertEqual('', factory.stdin.readline())
273
308
 
274
309
    def test_text_ui_getusername_utf8(self):
275
 
        ui = TestUIFactory(stdin=u'someuser\u1234'.encode('utf8'),
276
 
                           stdout=StringIOWrapper(), stderr=StringIOWrapper())
 
310
        ui = tests.TestUIFactory(stdin=u'someuser\u1234'.encode('utf8'),
 
311
                                 stdout=tests.StringIOWrapper(),
 
312
                                 stderr=tests.StringIOWrapper())
277
313
        ui.stderr.encoding = ui.stdout.encoding = ui.stdin.encoding = "utf8"
278
314
        pb = ui.nested_progress_bar()
279
315
        try:
316
352
            NotImplementedError,
317
353
            self.apply_redirected,
318
354
            None, stdout, stdout, factory.get_boolean, "foo")
 
355
 
 
356
 
 
357
class TestTextProgressView(tests.TestCase):
 
358
    """Tests for text display of progress bars.
 
359
    """
 
360
    # XXX: These might be a bit easier to write if the rendering and
 
361
    # state-maintaining parts of TextProgressView were more separate, and if
 
362
    # the progress task called back directly to its own view not to the ui
 
363
    # factory. -- mbp 20090312
 
364
    
 
365
    def _make_factory(self):
 
366
        out = StringIO()
 
367
        uif = TextUIFactory(stderr=out)
 
368
        uif._progress_view._width = 80
 
369
        return out, uif
 
370
 
 
371
    def test_render_progress_easy(self):
 
372
        """Just one task and one quarter done"""
 
373
        out, uif = self._make_factory()
 
374
        task = uif.nested_progress_bar()
 
375
        task.update('reticulating splines', 5, 20)
 
376
        self.assertEqual(
 
377
'\r[####/               ] reticulating splines 5/20                               \r'
 
378
            , out.getvalue())
 
379
 
 
380
    def test_render_progress_nested(self):
 
381
        """Tasks proportionally contribute to overall progress"""
 
382
        out, uif = self._make_factory()
 
383
        task = uif.nested_progress_bar()
 
384
        task.update('reticulating splines', 0, 2)
 
385
        task2 = uif.nested_progress_bar()
 
386
        task2.update('stage2', 1, 2)
 
387
        # so we're in the first half of the main task, and half way through
 
388
        # that
 
389
        self.assertEqual(
 
390
r'[####\               ] reticulating splines:stage2 1/2'
 
391
            , uif._progress_view._render_line())
 
392
        # if the nested task is complete, then we're all the way through the
 
393
        # first half of the overall work
 
394
        task2.update('stage2', 2, 2)
 
395
        self.assertEqual(
 
396
r'[#########|          ] reticulating splines:stage2 2/2'
 
397
            , uif._progress_view._render_line())
 
398
 
 
399
    def test_render_progress_sub_nested(self):
 
400
        """Intermediate tasks don't mess up calculation."""
 
401
        out, uif = self._make_factory()
 
402
        task_a = uif.nested_progress_bar()
 
403
        task_a.update('a', 0, 2)
 
404
        task_b = uif.nested_progress_bar()
 
405
        task_b.update('b')
 
406
        task_c = uif.nested_progress_bar()
 
407
        task_c.update('c', 1, 2)
 
408
        # the top-level task is in its first half; the middle one has no
 
409
        # progress indication, just a label; and the bottom one is half done,
 
410
        # so the overall fraction is 1/4
 
411
        self.assertEqual(
 
412
            r'[####|               ] a:b:c 1/2'
 
413
            , uif._progress_view._render_line())