67
67
self.assertEqual('', stdout.getvalue())
69
69
def test_text_factory_ascii_password(self):
70
ui = TestUIFactory(stdin='secret\n', stdout=StringIOWrapper())
70
ui = TestUIFactory(stdin='secret\n', stdout=StringIOWrapper(),
71
stderr=StringIOWrapper())
71
72
pb = ui.nested_progress_bar()
73
74
self.assertEqual('secret',
74
75
self.apply_redirected(ui.stdin, ui.stdout,
77
78
# ': ' is appended to prompt
78
self.assertEqual(': ', ui.stdout.getvalue())
79
self.assertEqual(': ', ui.stderr.getvalue())
80
self.assertEqual('', ui.stdout.readline())
79
81
# stdin should be empty
80
82
self.assertEqual('', ui.stdin.readline())
88
90
it to utf8 to test that we transport the password correctly.
90
92
ui = TestUIFactory(stdin=u'baz\u1234'.encode('utf8'),
91
stdout=StringIOWrapper())
92
ui.stdin.encoding = 'utf8'
93
ui.stdout.encoding = ui.stdin.encoding
93
stdout=StringIOWrapper(),
94
stderr=StringIOWrapper())
95
ui.stderr.encoding = ui.stdout.encoding = ui.stdin.encoding = 'utf8'
94
96
pb = ui.nested_progress_bar()
96
password = self.apply_redirected(ui.stdin, ui.stdout, ui.stdout,
98
password = self.apply_redirected(ui.stdin, ui.stdout, ui.stderr,
98
100
u'Hello \u1234 %(user)s',
99
101
user=u'some\u1234')
100
102
# We use StringIO objects, we need to decode them
101
103
self.assertEqual(u'baz\u1234', password.decode('utf8'))
102
104
self.assertEqual(u'Hello \u1234 some\u1234: ',
103
ui.stdout.getvalue().decode('utf8'))
104
# stdin should be empty
105
ui.stderr.getvalue().decode('utf8'))
106
# stdin and stdout should be empty
105
107
self.assertEqual('', ui.stdin.readline())
108
self.assertEqual('', ui.stdout.readline())
224
228
def test_text_factory_prompt(self):
225
229
# see <https://launchpad.net/bugs/365891>
226
factory = TextUIFactory(None, StringIO(), StringIO())
230
factory = TextUIFactory(None, StringIO(), StringIO(), StringIO())
227
231
factory.prompt('foo %2e')
232
self.assertEqual('', factory.stdout.getvalue())
233
self.assertEqual('foo %2e', factory.stderr.getvalue())
229
235
def test_text_factory_prompts_and_clears(self):
230
236
# a get_boolean call should clear the pb before prompting
263
269
factory = SilentUIFactory()
264
270
factory.stdin = StringIO("someuser\n\n")
265
271
factory.stdout = StringIO()
266
self.assertEquals(None,
272
factory.stderr = StringIO()
273
self.assertEquals(None,
267
274
factory.get_username(u'Hello\u1234 %(host)s', host=u'some\u1234'))
268
275
self.assertEquals("", factory.stdout.getvalue())
276
self.assertEquals("", factory.stderr.getvalue())
269
277
self.assertEquals("someuser\n\n", factory.stdin.getvalue())
271
279
def test_text_ui_getusername(self):
272
280
factory = TextUIFactory(None, None, None)
273
281
factory.stdin = StringIO("someuser\n\n")
274
282
factory.stdout = StringIO()
283
factory.stderr = StringIO()
275
284
factory.stdout.encoding = "utf8"
276
285
# there is no output from the base factory
277
self.assertEqual("someuser",
278
factory.get_username('Hello %(host)s', host='some'))
279
self.assertEquals("Hello some: ", factory.stdout.getvalue())
286
self.assertEqual("someuser",
287
factory.get_username('Hello %(host)s', host='some'))
288
self.assertEquals("Hello some: ", factory.stderr.getvalue())
289
self.assertEquals('', factory.stdout.getvalue())
280
290
self.assertEqual("", factory.get_username("Gebruiker"))
281
291
# stdin should be empty
282
292
self.assertEqual('', factory.stdin.readline())
284
294
def test_text_ui_getusername_utf8(self):
285
295
ui = TestUIFactory(stdin=u'someuser\u1234'.encode('utf8'),
286
stdout=StringIOWrapper())
287
ui.stdin.encoding = "utf8"
288
ui.stdout.encoding = ui.stdin.encoding
296
stdout=StringIOWrapper(), stderr=StringIOWrapper())
297
ui.stderr.encoding = ui.stdout.encoding = ui.stdin.encoding = "utf8"
289
298
pb = ui.nested_progress_bar()
291
300
# there is no output from the base factory
292
username = self.apply_redirected(ui.stdin, ui.stdout, ui.stdout,
301
username = self.apply_redirected(ui.stdin, ui.stdout, ui.stderr,
293
302
ui.get_username, u'Hello\u1234 %(host)s', host=u'some\u1234')
294
303
self.assertEquals(u"someuser\u1234", username.decode('utf8'))
295
self.assertEquals(u"Hello\u1234 some\u1234: ",
296
ui.stdout.getvalue().decode("utf8"))
304
self.assertEquals(u"Hello\u1234 some\u1234: ",
305
ui.stderr.getvalue().decode("utf8"))
306
self.assertEquals('', ui.stdout.getvalue())