35
35
from bzrlib.ui.text import TextUIFactory
38
class FakeTextUIFactory(TextUIFactory):
39
"""A TextUIFactory with a fake stdin"""
41
def __init__(self, stdin, stdout=None, stderr=None):
42
TextUIFactory.__init__(self, stdout=stdout, stderr=stderr)
44
# To ease test writing, we suppose that stdin and stdout use the same
46
self.stdout.encoding = self.stdin.encoding
49
38
class UITests(TestCase):
51
40
def test_silent_factory(self):
61
50
u'Hello\u1234 %(user)s',
62
51
user=u'some\u1234'))
63
52
self.assertEqual('', stdout.getvalue())
64
self.assertEqual(None,
65
self.apply_redirected(None, stdout, stdout,
69
self.assertEqual('', stdout.getvalue())
71
54
def test_text_factory_ascii_password(self):
72
ui = FakeTextUIFactory(FakeStdin('secret\n'), StringIOWrapper())
55
ui = TestUIFactory(stdin='secret\n', stdout=StringIOWrapper())
73
56
pb = ui.nested_progress_bar()
75
58
self.assertEqual('secret',
79
62
# ': ' is appended to prompt
80
# A '\n' is emitted as echo after the user enter password
81
self.assertEqual(': \n', ui.stdout.getvalue())
63
self.assertEqual(': ', ui.stdout.getvalue())
88
70
We can't predict what encoding users will have for stdin, so we force
89
71
it to utf8 to test that we transport the password correctly.
91
ui = FakeTextUIFactory(FakeStdin(u'baz\u1234', 'utf8'),
73
ui = TestUIFactory(stdin=u'baz\u1234'.encode('utf8'),
74
stdout=StringIOWrapper())
75
ui.stdin.encoding = 'utf8'
76
ui.stdout.encoding = ui.stdin.encoding
93
77
pb = ui.nested_progress_bar()
95
79
password = self.apply_redirected(ui.stdin, ui.stdout, ui.stdout,
98
82
user=u'some\u1234')
99
83
# We use StringIO objects, we need to decode them
100
84
self.assertEqual(u'baz\u1234', password.decode('utf8'))
101
self.assertEqual(u'Hello \u1234 some\u1234: \n',
102
ui.stdout.getvalue().decode('utf8'))
106
def test_text_factory_ascii_login(self):
107
ui = FakeTextUIFactory(FakeStdin('itsme\n'), StringIOWrapper())
108
pb = ui.nested_progress_bar()
110
self.assertEqual('itsme',
111
self.apply_redirected(ui.stdin, ui.stdout,
114
self.assertEqual(': ', ui.stdout.getvalue())
118
def test_text_factory_utf8_login(self):
119
"""Test an utf8 password.
121
We can't predict what encoding users will have for stdin, so we force
122
it to utf8 to test that we transport the login correctly.
124
ui = FakeTextUIFactory(FakeStdin(u'metoo\u1234', 'utf8'),
126
pb = ui.nested_progress_bar()
128
login = self.apply_redirected(ui.stdin, ui.stdout, ui.stdout,
130
u'Hello \u1234 %(user)s',
132
# We use StringIO objects, we need to decode them
133
self.assertEqual(u'metoo\u1234', login.decode('utf8'))
134
85
self.assertEqual(u'Hello \u1234 some\u1234: ',
135
86
ui.stdout.getvalue().decode('utf8'))