67
61
self.assertEqual('', stdout.getvalue())
69
63
def test_text_factory_ascii_password(self):
70
ui = TestUIFactory(stdin='secret\n', stdout=StringIOWrapper())
64
ui = TestUIFactory(stdin='secret\n', stdout=StringIOWrapper(),
65
stderr=StringIOWrapper())
71
66
pb = ui.nested_progress_bar()
73
68
self.assertEqual('secret',
74
69
self.apply_redirected(ui.stdin, ui.stdout,
77
72
# ': ' is appended to prompt
78
self.assertEqual(': ', ui.stdout.getvalue())
73
self.assertEqual(': ', ui.stderr.getvalue())
74
self.assertEqual('', ui.stdout.readline())
79
75
# stdin should be empty
80
76
self.assertEqual('', ui.stdin.readline())
88
84
it to utf8 to test that we transport the password correctly.
90
86
ui = TestUIFactory(stdin=u'baz\u1234'.encode('utf8'),
91
stdout=StringIOWrapper())
92
ui.stdin.encoding = 'utf8'
93
ui.stdout.encoding = ui.stdin.encoding
87
stdout=StringIOWrapper(),
88
stderr=StringIOWrapper())
89
ui.stderr.encoding = ui.stdout.encoding = ui.stdin.encoding = 'utf8'
94
90
pb = ui.nested_progress_bar()
96
password = self.apply_redirected(ui.stdin, ui.stdout, ui.stdout,
92
password = self.apply_redirected(ui.stdin, ui.stdout, ui.stderr,
98
94
u'Hello \u1234 %(user)s',
99
95
user=u'some\u1234')
100
96
# We use StringIO objects, we need to decode them
101
97
self.assertEqual(u'baz\u1234', password.decode('utf8'))
102
98
self.assertEqual(u'Hello \u1234 some\u1234: ',
103
ui.stdout.getvalue().decode('utf8'))
104
# stdin should be empty
99
ui.stderr.getvalue().decode('utf8'))
100
# stdin and stdout should be empty
105
101
self.assertEqual('', ui.stdin.readline())
102
self.assertEqual('', ui.stdout.readline())
164
def test_progress_stack(self):
165
# test the progress bar stack which the default text factory
169
# make a stack, which accepts parameters like a pb.
170
stack = self.applyDeprecated(
171
deprecated_in((1, 12, 0)),
173
to_file=stderr, to_messages_file=stdout)
175
self.assertFalse(getattr(stack, 'note', False))
176
pb1 = stack.get_nested()
177
pb2 = stack.get_nested()
178
warnings, _ = self.callCatchWarnings(pb1.finished)
179
self.assertEqual(len(warnings), 1)
182
# the text ui factory never actually removes the stack once its setup.
183
# we need to be able to nest again correctly from here.
184
pb1 = stack.get_nested()
185
pb2 = stack.get_nested()
186
warnings, _ = self.callCatchWarnings(pb1.finished)
187
self.assertEqual(len(warnings), 1)
191
161
def assert_get_bool_acceptance_of_user_input(self, factory):
192
162
factory.stdin = StringIO("y\nyes with garbage\n"
193
163
"yes\nn\nnot an answer\n"
195
165
factory.stdout = StringIO()
166
factory.stderr = StringIO()
196
167
# there is no output from the base factory
197
168
self.assertEqual(True, factory.get_boolean(""))
198
169
self.assertEqual(True, factory.get_boolean(""))
224
195
def test_text_factory_prompt(self):
225
196
# see <https://launchpad.net/bugs/365891>
226
factory = TextUIFactory(None, StringIO(), StringIO())
197
factory = TextUIFactory(None, StringIO(), StringIO(), StringIO())
227
198
factory.prompt('foo %2e')
199
self.assertEqual('', factory.stdout.getvalue())
200
self.assertEqual('foo %2e', factory.stderr.getvalue())
229
202
def test_text_factory_prompts_and_clears(self):
230
203
# a get_boolean call should clear the pb before prompting
263
236
factory = SilentUIFactory()
264
237
factory.stdin = StringIO("someuser\n\n")
265
238
factory.stdout = StringIO()
266
self.assertEquals(None,
239
factory.stderr = StringIO()
240
self.assertEquals(None,
267
241
factory.get_username(u'Hello\u1234 %(host)s', host=u'some\u1234'))
268
242
self.assertEquals("", factory.stdout.getvalue())
243
self.assertEquals("", factory.stderr.getvalue())
269
244
self.assertEquals("someuser\n\n", factory.stdin.getvalue())
271
246
def test_text_ui_getusername(self):
272
247
factory = TextUIFactory(None, None, None)
273
248
factory.stdin = StringIO("someuser\n\n")
274
249
factory.stdout = StringIO()
250
factory.stderr = StringIO()
275
251
factory.stdout.encoding = "utf8"
276
252
# 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())
253
self.assertEqual("someuser",
254
factory.get_username('Hello %(host)s', host='some'))
255
self.assertEquals("Hello some: ", factory.stderr.getvalue())
256
self.assertEquals('', factory.stdout.getvalue())
280
257
self.assertEqual("", factory.get_username("Gebruiker"))
281
258
# stdin should be empty
282
259
self.assertEqual('', factory.stdin.readline())
284
261
def test_text_ui_getusername_utf8(self):
285
262
ui = TestUIFactory(stdin=u'someuser\u1234'.encode('utf8'),
286
stdout=StringIOWrapper())
287
ui.stdin.encoding = "utf8"
288
ui.stdout.encoding = ui.stdin.encoding
263
stdout=StringIOWrapper(), stderr=StringIOWrapper())
264
ui.stderr.encoding = ui.stdout.encoding = ui.stdin.encoding = "utf8"
289
265
pb = ui.nested_progress_bar()
291
267
# there is no output from the base factory
292
username = self.apply_redirected(ui.stdin, ui.stdout, ui.stdout,
268
username = self.apply_redirected(ui.stdin, ui.stdout, ui.stderr,
293
269
ui.get_username, u'Hello\u1234 %(host)s', host=u'some\u1234')
294
270
self.assertEquals(u"someuser\u1234", username.decode('utf8'))
295
self.assertEquals(u"Hello\u1234 some\u1234: ",
296
ui.stdout.getvalue().decode("utf8"))
271
self.assertEquals(u"Hello\u1234 some\u1234: ",
272
ui.stderr.getvalue().decode("utf8"))
273
self.assertEquals('', ui.stdout.getvalue())