~bzr-pqm/bzr/bzr.dev

« back to all changes in this revision

Viewing changes to bzrlib/tests/test_ui.py

  • Committer: Patch Queue Manager
  • Date: 2014-04-09 13:36:25 UTC
  • mfrom: (6592.1.2 1303879-py27-issues)
  • Revision ID: pqm@pqm.ubuntu.com-20140409133625-s24spv3kha2w2860
(vila) Fix python-2.7.6 test failures. (Vincent Ladeuil)

Show diffs side-by-side

added added

removed removed

Lines of Context:
31
31
    )
32
32
from bzrlib.tests import (
33
33
    fixtures,
34
 
    test_progress,
35
34
    )
36
35
from bzrlib.ui import text as _mod_ui_text
37
36
from bzrlib.tests.testui import (
39
38
    )
40
39
 
41
40
 
 
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
 
42
55
class TestUIConfiguration(tests.TestCaseWithTransport):
43
56
 
44
57
    def test_output_encoding_configuration(self):
45
58
        enc = fixtures.generate_unicode_encodings().next()
46
 
        config.GlobalConfig().set_user_option('output_encoding',
47
 
            enc)
 
59
        config.GlobalStack().set('output_encoding', enc)
48
60
        ui = tests.TestUIFactory(stdin=None,
49
61
            stdout=tests.StringIOWrapper(),
50
62
            stderr=tests.StringIOWrapper())
85
97
            pb.finished()
86
98
 
87
99
    def test_text_factory_utf8_password(self):
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'))
 
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()
94
105
        ui.stderr.encoding = ui.stdout.encoding = ui.stdin.encoding = 'utf8'
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()
 
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())
110
113
 
111
114
    def test_text_ui_get_boolean(self):
112
115
        stdin = tests.StringIOWrapper("y\n" # True
221
224
 
222
225
    def test_text_factory_prompts_and_clears(self):
223
226
        # a get_boolean call should clear the pb before prompting
224
 
        out = test_progress._TTYStringIO()
 
227
        out = TTYStringIO()
225
228
        self.overrideEnv('TERM', 'xterm')
226
229
        factory = _mod_ui_text.TextUIFactory(
227
230
            stdin=tests.StringIOWrapper("yada\ny\n"),
258
261
            pb.finished()
259
262
 
260
263
    def test_text_ui_getusername(self):
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"))
 
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.assertEquals('Hello some: ', ui.stderr.getvalue())
 
272
        self.assertEquals('', ui.stdout.getvalue())
 
273
        self.assertEqual('', ui.get_username(u"Gebruiker"))
272
274
        # stdin should be empty
273
 
        self.assertEqual('', factory.stdin.readline())
 
275
        self.assertEqual('', ui.stdin.readline())
274
276
 
275
277
    def test_text_ui_getusername_utf8(self):
276
 
        ui = tests.TestUIFactory(stdin=u'someuser\u1234'.encode('utf8'),
277
 
                                 stdout=tests.StringIOWrapper(),
278
 
                                 stderr=tests.StringIOWrapper())
 
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()
279
282
        ui.stderr.encoding = ui.stdout.encoding = ui.stdin.encoding = "utf8"
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()
 
283
        username = ui.get_username(u'Hello %(host)s', host=u'some\u1234')
 
284
        self.assertEquals(u"someuser\u1234", username)
 
285
        self.assertEquals(u"Hello some\u1234: ",
 
286
                          ui.stderr.getvalue().decode("utf8"))
 
287
        self.assertEquals('', ui.stdout.getvalue())
291
288
 
292
289
    def test_quietness(self):
293
290
        self.overrideEnv('BZR_PROGRESS_BAR', 'text')
294
291
        ui_factory = _mod_ui_text.TextUIFactory(None,
295
 
            test_progress._TTYStringIO(),
296
 
            test_progress._TTYStringIO())
 
292
            TTYStringIO(),
 
293
            TTYStringIO())
297
294
        self.assertIsInstance(ui_factory._progress_view,
298
295
            _mod_ui_text.TextProgressView)
299
296
        ui_factory.be_quiet(True)
358
355
    def test_progress_construction(self):
359
356
        """TextUIFactory constructs the right progress view.
360
357
        """
361
 
        TTYStringIO = test_progress._TTYStringIO
362
358
        FileStringIO = tests.StringIOWrapper
363
359
        for (file_class, term, pb, expected_pb_class) in (
364
360
            # on an xterm, either use them or not as the user requests,
391
387
 
392
388
    def test_text_ui_non_terminal(self):
393
389
        """Even on non-ttys, make_ui_for_terminal gives a text ui."""
394
 
        stdin = test_progress._NonTTYStringIO('')
395
 
        stderr = test_progress._NonTTYStringIO()
396
 
        stdout = test_progress._NonTTYStringIO()
 
390
        stdin = NonTTYStringIO('')
 
391
        stderr = NonTTYStringIO()
 
392
        stdout = NonTTYStringIO()
397
393
        for term_type in ['dumb', None, 'xterm']:
398
394
            self.overrideEnv('TERM', term_type)
399
395
            uif = _mod_ui.make_ui_for_terminal(stdin, stdout, stderr)