~bzr-pqm/bzr/bzr.dev

« back to all changes in this revision

Viewing changes to bzrlib/tests/test_ui.py

  • Committer: Vincent Ladeuil
  • Date: 2012-03-13 09:07:18 UTC
  • mfrom: (6437.23.26 2.5)
  • mto: This revision was merged to the branch mainline in revision 6499.
  • Revision ID: v.ladeuil+lp@free.fr-20120313090718-2e5wf4d6otbh2k2r
Merge 2.5 branch including bugfix for #940164 and #944696

Show diffs side-by-side

added added

removed removed

Lines of Context:
1
 
# Copyright (C) 2005-2012, 2016 Canonical Ltd
 
1
# Copyright (C) 2005-2011 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
31
31
    )
32
32
from bzrlib.tests import (
33
33
    fixtures,
 
34
    test_progress,
34
35
    )
35
36
from bzrlib.ui import text as _mod_ui_text
36
37
from bzrlib.tests.testui import (
38
39
    )
39
40
 
40
41
 
41
 
class TTYStringIO(StringIO):
42
 
    """A helper class which makes a StringIO look like a terminal"""
43
 
 
44
 
    def isatty(self):
45
 
        return True
46
 
 
47
 
 
48
 
class NonTTYStringIO(StringIO):
49
 
    """Helper that implements isatty() but returns False"""
50
 
 
51
 
    def isatty(self):
52
 
        return False
53
 
 
54
 
 
55
42
class TestUIConfiguration(tests.TestCaseWithTransport):
56
43
 
57
44
    def test_output_encoding_configuration(self):
58
45
        enc = fixtures.generate_unicode_encodings().next()
59
 
        config.GlobalStack().set('output_encoding', enc)
 
46
        config.GlobalConfig().set_user_option('output_encoding',
 
47
            enc)
60
48
        ui = tests.TestUIFactory(stdin=None,
61
49
            stdout=tests.StringIOWrapper(),
62
50
            stderr=tests.StringIOWrapper())
63
51
        output = ui.make_output_stream()
64
 
        self.assertEqual(output.encoding, enc)
 
52
        self.assertEquals(output.encoding, enc)
65
53
 
66
54
 
67
55
class TestTextUIFactory(tests.TestCase):
75
63
    def test_text_factory_confirm(self):
76
64
        # turns into reading a regular boolean
77
65
        ui = self.make_test_ui_factory('n\n')
78
 
        self.assertEqual(ui.confirm_action(u'Should %(thing)s pass?',
 
66
        self.assertEquals(ui.confirm_action(u'Should %(thing)s pass?',
79
67
            'bzrlib.tests.test_ui.confirmation',
80
68
            {'thing': 'this'},),
81
69
            False)
97
85
            pb.finished()
98
86
 
99
87
    def test_text_factory_utf8_password(self):
100
 
        """Test an utf8 password."""
101
 
        ui = _mod_ui_text.TextUIFactory(None, None, None)
102
 
        ui.stdin = tests.StringIOWrapper(u'baz\u1234'.encode('utf8'))
103
 
        ui.stdout = tests.StringIOWrapper()
104
 
        ui.stderr = tests.StringIOWrapper()
 
88
        """Test an utf8 password.
 
89
 
 
90
        We can't predict what encoding users will have for stdin, so we force
 
91
        it to utf8 to test that we transport the password correctly.
 
92
        """
 
93
        ui = self.make_test_ui_factory(u'baz\u1234'.encode('utf8'))
105
94
        ui.stderr.encoding = ui.stdout.encoding = ui.stdin.encoding = 'utf8'
106
 
        password = ui.get_password(u'Hello \u1234 %(user)s', user=u'some\u1234')
107
 
        self.assertEqual(u'baz\u1234', password)
108
 
        self.assertEqual(u'Hello \u1234 some\u1234: ',
109
 
                         ui.stderr.getvalue().decode('utf8'))
110
 
        # stdin and stdout should be empty
111
 
        self.assertEqual('', ui.stdin.readline())
112
 
        self.assertEqual('', ui.stdout.getvalue())
 
95
        pb = ui.nested_progress_bar()
 
96
        try:
 
97
            password = self.apply_redirected(ui.stdin, ui.stdout, ui.stderr,
 
98
                                             ui.get_password,
 
99
                                             u'Hello \u1234 %(user)s',
 
100
                                             user=u'some\u1234')
 
101
            # We use StringIO objects, we need to decode them
 
102
            self.assertEqual(u'baz\u1234', password.decode('utf8'))
 
103
            self.assertEqual(u'Hello \u1234 some\u1234: ',
 
104
                             ui.stderr.getvalue().decode('utf8'))
 
105
            # stdin and stdout should be empty
 
106
            self.assertEqual('', ui.stdin.readline())
 
107
            self.assertEqual('', ui.stdout.readline())
 
108
        finally:
 
109
            pb.finished()
113
110
 
114
111
    def test_text_ui_get_boolean(self):
115
112
        stdin = tests.StringIOWrapper("y\n" # True
224
221
 
225
222
    def test_text_factory_prompts_and_clears(self):
226
223
        # a get_boolean call should clear the pb before prompting
227
 
        out = TTYStringIO()
 
224
        out = test_progress._TTYStringIO()
228
225
        self.overrideEnv('TERM', 'xterm')
229
226
        factory = _mod_ui_text.TextUIFactory(
230
227
            stdin=tests.StringIOWrapper("yada\ny\n"),
261
258
            pb.finished()
262
259
 
263
260
    def test_text_ui_getusername(self):
264
 
        ui = _mod_ui_text.TextUIFactory(None, None, None)
265
 
        ui.stdin = tests.StringIOWrapper('someuser\n\n')
266
 
        ui.stdout = tests.StringIOWrapper()
267
 
        ui.stderr = tests.StringIOWrapper()
268
 
        ui.stdout.encoding = 'utf8'
269
 
        self.assertEqual('someuser',
270
 
                         ui.get_username(u'Hello %(host)s', host='some'))
271
 
        self.assertEqual('Hello some: ', ui.stderr.getvalue())
272
 
        self.assertEqual('', ui.stdout.getvalue())
273
 
        self.assertEqual('', ui.get_username(u"Gebruiker"))
 
261
        factory = _mod_ui_text.TextUIFactory(None, None, None)
 
262
        factory.stdin = tests.StringIOWrapper("someuser\n\n")
 
263
        factory.stdout = tests.StringIOWrapper()
 
264
        factory.stderr = tests.StringIOWrapper()
 
265
        factory.stdout.encoding = "utf8"
 
266
        # there is no output from the base factory
 
267
        self.assertEqual("someuser",
 
268
                         factory.get_username(u'Hello %(host)s', host='some'))
 
269
        self.assertEquals("Hello some: ", factory.stderr.getvalue())
 
270
        self.assertEquals('', factory.stdout.getvalue())
 
271
        self.assertEqual("", factory.get_username(u"Gebruiker"))
274
272
        # stdin should be empty
275
 
        self.assertEqual('', ui.stdin.readline())
 
273
        self.assertEqual('', factory.stdin.readline())
276
274
 
277
275
    def test_text_ui_getusername_utf8(self):
278
 
        ui = _mod_ui_text.TextUIFactory(None, None, None)
279
 
        ui.stdin = tests.StringIOWrapper(u'someuser\u1234'.encode('utf8'))
280
 
        ui.stdout = tests.StringIOWrapper()
281
 
        ui.stderr = tests.StringIOWrapper()
 
276
        ui = tests.TestUIFactory(stdin=u'someuser\u1234'.encode('utf8'),
 
277
                                 stdout=tests.StringIOWrapper(),
 
278
                                 stderr=tests.StringIOWrapper())
282
279
        ui.stderr.encoding = ui.stdout.encoding = ui.stdin.encoding = "utf8"
283
 
        username = ui.get_username(u'Hello %(host)s', host=u'some\u1234')
284
 
        self.assertEqual(u"someuser\u1234", username)
285
 
        self.assertEqual(u"Hello some\u1234: ",
286
 
                          ui.stderr.getvalue().decode("utf8"))
287
 
        self.assertEqual('', ui.stdout.getvalue())
 
280
        pb = ui.nested_progress_bar()
 
281
        try:
 
282
            # there is no output from the base factory
 
283
            username = self.apply_redirected(ui.stdin, ui.stdout, ui.stderr,
 
284
                ui.get_username, u'Hello\u1234 %(host)s', host=u'some\u1234')
 
285
            self.assertEquals(u"someuser\u1234", username.decode('utf8'))
 
286
            self.assertEquals(u"Hello\u1234 some\u1234: ",
 
287
                              ui.stderr.getvalue().decode("utf8"))
 
288
            self.assertEquals('', ui.stdout.getvalue())
 
289
        finally:
 
290
            pb.finished()
288
291
 
289
292
    def test_quietness(self):
290
293
        self.overrideEnv('BZR_PROGRESS_BAR', 'text')
291
294
        ui_factory = _mod_ui_text.TextUIFactory(None,
292
 
            TTYStringIO(),
293
 
            TTYStringIO())
 
295
            test_progress._TTYStringIO(),
 
296
            test_progress._TTYStringIO())
294
297
        self.assertIsInstance(ui_factory._progress_view,
295
298
            _mod_ui_text.TextProgressView)
296
299
        ui_factory.be_quiet(True)
307
310
        remote_fmt._network_name = RepositoryFormatKnitPack5().network_name()
308
311
        ui.show_user_warning('cross_format_fetch', from_format=RepositoryFormat2a(),
309
312
            to_format=remote_fmt)
310
 
        self.assertEqual('', out.getvalue())
311
 
        self.assertEqual("Doing on-the-fly conversion from RepositoryFormat2a() to "
 
313
        self.assertEquals('', out.getvalue())
 
314
        self.assertEquals("Doing on-the-fly conversion from RepositoryFormat2a() to "
312
315
            "RemoteRepositoryFormat(_network_name='Bazaar RepositoryFormatKnitPack5 "
313
316
            "(bzr 1.6)\\n').\nThis may take some time. Upgrade the repositories to "
314
317
            "the same format for better performance.\n",
320
323
        ui.suppressed_warnings.add('cross_format_fetch')
321
324
        ui.show_user_warning('cross_format_fetch', from_format=RepositoryFormat2a(),
322
325
            to_format=remote_fmt)
323
 
        self.assertEqual('', out.getvalue())
324
 
        self.assertEqual('', err.getvalue())
 
326
        self.assertEquals('', out.getvalue())
 
327
        self.assertEquals('', err.getvalue())
325
328
 
326
329
 
327
330
class TestTextUIOutputStream(tests.TestCase):
355
358
    def test_progress_construction(self):
356
359
        """TextUIFactory constructs the right progress view.
357
360
        """
 
361
        TTYStringIO = test_progress._TTYStringIO
358
362
        FileStringIO = tests.StringIOWrapper
359
363
        for (file_class, term, pb, expected_pb_class) in (
360
364
            # on an xterm, either use them or not as the user requests,
387
391
 
388
392
    def test_text_ui_non_terminal(self):
389
393
        """Even on non-ttys, make_ui_for_terminal gives a text ui."""
390
 
        stdin = NonTTYStringIO('')
391
 
        stderr = NonTTYStringIO()
392
 
        stdout = NonTTYStringIO()
 
394
        stdin = test_progress._NonTTYStringIO('')
 
395
        stderr = test_progress._NonTTYStringIO()
 
396
        stdout = test_progress._NonTTYStringIO()
393
397
        for term_type in ['dumb', None, 'xterm']:
394
398
            self.overrideEnv('TERM', term_type)
395
399
            uif = _mod_ui.make_ui_for_terminal(stdin, stdout, stderr)
449
453
 
450
454
    def assertIsTrue(self, s, accepted_values=None):
451
455
        res = _mod_ui.bool_from_string(s, accepted_values=accepted_values)
452
 
        self.assertEqual(True, res)
 
456
        self.assertEquals(True, res)
453
457
 
454
458
    def assertIsFalse(self, s, accepted_values=None):
455
459
        res = _mod_ui.bool_from_string(s, accepted_values=accepted_values)
456
 
        self.assertEqual(False, res)
 
460
        self.assertEquals(False, res)
457
461
 
458
462
    def assertIsNone(self, s, accepted_values=None):
459
463
        res = _mod_ui.bool_from_string(s, accepted_values=accepted_values)
506
510
    def test_confirm_action_default(self):
507
511
        base_ui = _mod_ui.NoninteractiveUIFactory()
508
512
        for answer in [True, False]:
509
 
            self.assertEqual(
 
513
            self.assertEquals(
510
514
                _mod_ui.ConfirmationUserInterfacePolicy(base_ui, answer, {})
511
515
                .confirm_action("Do something?",
512
516
                    "bzrlib.tests.do_something", {}),
521
525
                        base_ui, default_answer, dict(given_id=specific_answer))
522
526
                    result = wrapper.confirm_action("Do something?", conf_id, {})
523
527
                    if conf_id == 'given_id':
524
 
                        self.assertEqual(result, specific_answer)
 
528
                        self.assertEquals(result, specific_answer)
525
529
                    else:
526
 
                        self.assertEqual(result, default_answer)
 
530
                        self.assertEquals(result, default_answer)
527
531
 
528
532
    def test_repr(self):
529
533
        base_ui = _mod_ui.NoninteractiveUIFactory()