41
class TTYStringIO(StringIO):
42
"""A helper class which makes a StringIO look like a terminal"""
48
class NonTTYStringIO(StringIO):
49
"""Helper that implements isatty() but returns False"""
55
42
class TestUIConfiguration(tests.TestCaseWithTransport):
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',
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)
67
55
class TestTextUIFactory(tests.TestCase):
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.
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.
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()
97
password = self.apply_redirected(ui.stdin, ui.stdout, ui.stderr,
99
u'Hello \u1234 %(user)s',
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())
114
111
def test_text_ui_get_boolean(self):
115
112
stdin = tests.StringIOWrapper("y\n" # True
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())
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()
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
292
def test_quietness(self):
290
293
self.overrideEnv('BZR_PROGRESS_BAR', 'text')
291
294
ui_factory = _mod_ui_text.TextUIFactory(None,
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",
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)
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)
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)
458
462
def assertIsNone(self, s, accepted_values=None):
459
463
res = _mod_ui.bool_from_string(s, accepted_values=accepted_values)
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)
526
self.assertEqual(result, default_answer)
530
self.assertEquals(result, default_answer)
528
532
def test_repr(self):
529
533
base_ui = _mod_ui.NoninteractiveUIFactory()