~bzr-pqm/bzr/bzr.dev

« back to all changes in this revision

Viewing changes to bzrlib/tests/test_ui.py

  • Committer: Jelmer Vernooij
  • Date: 2016-04-03 16:32:31 UTC
  • mto: This revision was merged to the branch mainline in revision 6617.
  • Revision ID: jelmer@jelmer.uk-20160403163231-h72bo0uyek2gikw0
Don't put French text in doc/en/user-reference when LANGUAGE=fr_CH.UTF_8.

Show diffs side-by-side

added added

removed removed

Lines of Context:
1
 
# Copyright (C) 2005-2011 Canonical Ltd
 
1
# Copyright (C) 2005-2012, 2016 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,
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())
51
63
        output = ui.make_output_stream()
52
 
        self.assertEquals(output.encoding, enc)
 
64
        self.assertEqual(output.encoding, enc)
53
65
 
54
66
 
55
67
class TestTextUIFactory(tests.TestCase):
63
75
    def test_text_factory_confirm(self):
64
76
        # turns into reading a regular boolean
65
77
        ui = self.make_test_ui_factory('n\n')
66
 
        self.assertEquals(ui.confirm_action(u'Should %(thing)s pass?',
 
78
        self.assertEqual(ui.confirm_action(u'Should %(thing)s pass?',
67
79
            'bzrlib.tests.test_ui.confirmation',
68
80
            {'thing': 'this'},),
69
81
            False)
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
113
116
                                      "n\n" # False
 
117
                                      " \n y \n" # True
 
118
                                      " no \n" # False
114
119
                                      "yes with garbage\nY\n" # True
115
120
                                      "not an answer\nno\n" # False
116
121
                                      "I'm sure!\nyes\n" # True
125
130
        self.assertEqual(False, factory.get_boolean(u""))
126
131
        self.assertEqual(True, factory.get_boolean(u""))
127
132
        self.assertEqual(False, factory.get_boolean(u""))
128
 
        self.assertEqual("foo\n", factory.stdin.read())
129
 
        # stdin should be empty
130
 
        self.assertEqual('', factory.stdin.readline())
 
133
        self.assertEqual(True, factory.get_boolean(u""))
 
134
        self.assertEqual(False, factory.get_boolean(u""))
 
135
        self.assertEqual("foo\n", factory.stdin.read())
 
136
        # stdin should be empty
 
137
        self.assertEqual('', factory.stdin.readline())
 
138
        # return false on EOF
 
139
        self.assertEqual(False, factory.get_boolean(u""))
 
140
 
 
141
    def test_text_ui_choose_bad_parameters(self):
 
142
        stdin = tests.StringIOWrapper()
 
143
        stdout = tests.StringIOWrapper()
 
144
        stderr = tests.StringIOWrapper()
 
145
        factory = _mod_ui_text.TextUIFactory(stdin, stdout, stderr)
 
146
        # invalid default index
 
147
        self.assertRaises(ValueError, factory.choose, u"", u"&Yes\n&No", 3)
 
148
        # duplicated choice
 
149
        self.assertRaises(ValueError, factory.choose, u"", u"&choice\n&ChOiCe")
 
150
        # duplicated shortcut
 
151
        self.assertRaises(ValueError, factory.choose, u"", u"&choice1\nchoi&ce2")
 
152
 
 
153
    def test_text_ui_choose_prompt(self):
 
154
        stdin = tests.StringIOWrapper()
 
155
        stdout = tests.StringIOWrapper()
 
156
        stderr = tests.StringIOWrapper()
 
157
        factory = _mod_ui_text.TextUIFactory(stdin, stdout, stderr)
 
158
        # choices with explicit shortcuts
 
159
        factory.choose(u"prompt", u"&yes\n&No\nmore &info")
 
160
        self.assertEqual("prompt ([y]es, [N]o, more [i]nfo): \n", factory.stderr.getvalue())
 
161
        # automatic shortcuts
 
162
        factory.stderr.truncate(0)
 
163
        factory.choose(u"prompt", u"yes\nNo\nmore info")
 
164
        self.assertEqual("prompt ([y]es, [N]o, [m]ore info): \n", factory.stderr.getvalue())
 
165
 
 
166
    def test_text_ui_choose_return_values(self):
 
167
        choose = lambda: factory.choose(u"", u"&Yes\n&No\nMaybe\nmore &info", 3)
 
168
        stdin = tests.StringIOWrapper("y\n" # 0
 
169
                                      "n\n" # 1
 
170
                                      " \n" # default: 3
 
171
                                      " no \n" # 1
 
172
                                      "b\na\nd \n" # bad shortcuts, all ignored
 
173
                                      "yes with garbage\nY\n" # 0
 
174
                                      "not an answer\nno\n" # 1
 
175
                                      "info\nmore info\n" # 3
 
176
                                      "Maybe\n" # 2
 
177
                                      "foo\n")
 
178
        stdout = tests.StringIOWrapper()
 
179
        stderr = tests.StringIOWrapper()
 
180
        factory = _mod_ui_text.TextUIFactory(stdin, stdout, stderr)
 
181
        self.assertEqual(0, choose())
 
182
        self.assertEqual(1, choose())
 
183
        self.assertEqual(3, choose())
 
184
        self.assertEqual(1, choose())
 
185
        self.assertEqual(0, choose())
 
186
        self.assertEqual(1, choose())
 
187
        self.assertEqual(3, choose())
 
188
        self.assertEqual(2, choose())
 
189
        self.assertEqual("foo\n", factory.stdin.read())
 
190
        # stdin should be empty
 
191
        self.assertEqual('', factory.stdin.readline())
 
192
        # return None on EOF
 
193
        self.assertEqual(None, choose())
 
194
 
 
195
    def test_text_ui_choose_no_default(self):
 
196
        stdin = tests.StringIOWrapper(" \n" # no default, invalid!
 
197
                                      " yes \n" # 0
 
198
                                      "foo\n")
 
199
        stdout = tests.StringIOWrapper()
 
200
        stderr = tests.StringIOWrapper()
 
201
        factory = _mod_ui_text.TextUIFactory(stdin, stdout, stderr)
 
202
        self.assertEqual(0, factory.choose(u"", u"&Yes\n&No"))
 
203
        self.assertEqual("foo\n", factory.stdin.read())
131
204
 
132
205
    def test_text_ui_get_integer(self):
133
206
        stdin = tests.StringIOWrapper(
151
224
 
152
225
    def test_text_factory_prompts_and_clears(self):
153
226
        # a get_boolean call should clear the pb before prompting
154
 
        out = test_progress._TTYStringIO()
 
227
        out = TTYStringIO()
155
228
        self.overrideEnv('TERM', 'xterm')
156
229
        factory = _mod_ui_text.TextUIFactory(
157
230
            stdin=tests.StringIOWrapper("yada\ny\n"),
170
243
        output = out.getvalue()
171
244
        self.assertContainsRe(output,
172
245
            "| foo *\r\r  *\r*")
173
 
        self.assertContainsRe(output,
174
 
            r"what do you want\? \[y/n\]: what do you want\? \[y/n\]: ")
 
246
        self.assertContainsString(output,
 
247
            r"what do you want? ([y]es, [n]o): what do you want? ([y]es, [n]o): ")
175
248
        # stdin should have been totally consumed
176
249
        self.assertEqual('', factory.stdin.readline())
177
250
 
188
261
            pb.finished()
189
262
 
190
263
    def test_text_ui_getusername(self):
191
 
        factory = _mod_ui_text.TextUIFactory(None, None, None)
192
 
        factory.stdin = tests.StringIOWrapper("someuser\n\n")
193
 
        factory.stdout = tests.StringIOWrapper()
194
 
        factory.stderr = tests.StringIOWrapper()
195
 
        factory.stdout.encoding = "utf8"
196
 
        # there is no output from the base factory
197
 
        self.assertEqual("someuser",
198
 
                         factory.get_username(u'Hello %(host)s', host='some'))
199
 
        self.assertEquals("Hello some: ", factory.stderr.getvalue())
200
 
        self.assertEquals('', factory.stdout.getvalue())
201
 
        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.assertEqual('Hello some: ', ui.stderr.getvalue())
 
272
        self.assertEqual('', ui.stdout.getvalue())
 
273
        self.assertEqual('', ui.get_username(u"Gebruiker"))
202
274
        # stdin should be empty
203
 
        self.assertEqual('', factory.stdin.readline())
 
275
        self.assertEqual('', ui.stdin.readline())
204
276
 
205
277
    def test_text_ui_getusername_utf8(self):
206
 
        ui = tests.TestUIFactory(stdin=u'someuser\u1234'.encode('utf8'),
207
 
                                 stdout=tests.StringIOWrapper(),
208
 
                                 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()
209
282
        ui.stderr.encoding = ui.stdout.encoding = ui.stdin.encoding = "utf8"
210
 
        pb = ui.nested_progress_bar()
211
 
        try:
212
 
            # there is no output from the base factory
213
 
            username = self.apply_redirected(ui.stdin, ui.stdout, ui.stderr,
214
 
                ui.get_username, u'Hello\u1234 %(host)s', host=u'some\u1234')
215
 
            self.assertEquals(u"someuser\u1234", username.decode('utf8'))
216
 
            self.assertEquals(u"Hello\u1234 some\u1234: ",
217
 
                              ui.stderr.getvalue().decode("utf8"))
218
 
            self.assertEquals('', ui.stdout.getvalue())
219
 
        finally:
220
 
            pb.finished()
 
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())
221
288
 
222
289
    def test_quietness(self):
223
290
        self.overrideEnv('BZR_PROGRESS_BAR', 'text')
224
291
        ui_factory = _mod_ui_text.TextUIFactory(None,
225
 
            test_progress._TTYStringIO(),
226
 
            test_progress._TTYStringIO())
 
292
            TTYStringIO(),
 
293
            TTYStringIO())
227
294
        self.assertIsInstance(ui_factory._progress_view,
228
295
            _mod_ui_text.TextProgressView)
229
296
        ui_factory.be_quiet(True)
240
307
        remote_fmt._network_name = RepositoryFormatKnitPack5().network_name()
241
308
        ui.show_user_warning('cross_format_fetch', from_format=RepositoryFormat2a(),
242
309
            to_format=remote_fmt)
243
 
        self.assertEquals('', out.getvalue())
244
 
        self.assertEquals("Doing on-the-fly conversion from RepositoryFormat2a() to "
 
310
        self.assertEqual('', out.getvalue())
 
311
        self.assertEqual("Doing on-the-fly conversion from RepositoryFormat2a() to "
245
312
            "RemoteRepositoryFormat(_network_name='Bazaar RepositoryFormatKnitPack5 "
246
313
            "(bzr 1.6)\\n').\nThis may take some time. Upgrade the repositories to "
247
314
            "the same format for better performance.\n",
253
320
        ui.suppressed_warnings.add('cross_format_fetch')
254
321
        ui.show_user_warning('cross_format_fetch', from_format=RepositoryFormat2a(),
255
322
            to_format=remote_fmt)
256
 
        self.assertEquals('', out.getvalue())
257
 
        self.assertEquals('', err.getvalue())
 
323
        self.assertEqual('', out.getvalue())
 
324
        self.assertEqual('', err.getvalue())
258
325
 
259
326
 
260
327
class TestTextUIOutputStream(tests.TestCase):
288
355
    def test_progress_construction(self):
289
356
        """TextUIFactory constructs the right progress view.
290
357
        """
291
 
        TTYStringIO = test_progress._TTYStringIO
292
358
        FileStringIO = tests.StringIOWrapper
293
359
        for (file_class, term, pb, expected_pb_class) in (
294
360
            # on an xterm, either use them or not as the user requests,
321
387
 
322
388
    def test_text_ui_non_terminal(self):
323
389
        """Even on non-ttys, make_ui_for_terminal gives a text ui."""
324
 
        stdin = test_progress._NonTTYStringIO('')
325
 
        stderr = test_progress._NonTTYStringIO()
326
 
        stdout = test_progress._NonTTYStringIO()
 
390
        stdin = NonTTYStringIO('')
 
391
        stderr = NonTTYStringIO()
 
392
        stdout = NonTTYStringIO()
327
393
        for term_type in ['dumb', None, 'xterm']:
328
394
            self.overrideEnv('TERM', term_type)
329
395
            uif = _mod_ui.make_ui_for_terminal(stdin, stdout, stderr)
383
449
 
384
450
    def assertIsTrue(self, s, accepted_values=None):
385
451
        res = _mod_ui.bool_from_string(s, accepted_values=accepted_values)
386
 
        self.assertEquals(True, res)
 
452
        self.assertEqual(True, res)
387
453
 
388
454
    def assertIsFalse(self, s, accepted_values=None):
389
455
        res = _mod_ui.bool_from_string(s, accepted_values=accepted_values)
390
 
        self.assertEquals(False, res)
 
456
        self.assertEqual(False, res)
391
457
 
392
458
    def assertIsNone(self, s, accepted_values=None):
393
459
        res = _mod_ui.bool_from_string(s, accepted_values=accepted_values)
440
506
    def test_confirm_action_default(self):
441
507
        base_ui = _mod_ui.NoninteractiveUIFactory()
442
508
        for answer in [True, False]:
443
 
            self.assertEquals(
 
509
            self.assertEqual(
444
510
                _mod_ui.ConfirmationUserInterfacePolicy(base_ui, answer, {})
445
511
                .confirm_action("Do something?",
446
512
                    "bzrlib.tests.do_something", {}),
455
521
                        base_ui, default_answer, dict(given_id=specific_answer))
456
522
                    result = wrapper.confirm_action("Do something?", conf_id, {})
457
523
                    if conf_id == 'given_id':
458
 
                        self.assertEquals(result, specific_answer)
 
524
                        self.assertEqual(result, specific_answer)
459
525
                    else:
460
 
                        self.assertEquals(result, default_answer)
 
526
                        self.assertEqual(result, default_answer)
461
527
 
462
528
    def test_repr(self):
463
529
        base_ui = _mod_ui.NoninteractiveUIFactory()