67
62
self.assertEqual('', stdout.getvalue())
69
64
def test_text_factory_ascii_password(self):
70
ui = TestUIFactory(stdin='secret\n', stdout=StringIOWrapper())
65
ui = TestUIFactory(stdin='secret\n', stdout=StringIOWrapper(),
66
stderr=StringIOWrapper())
71
67
pb = ui.nested_progress_bar()
73
69
self.assertEqual('secret',
74
70
self.apply_redirected(ui.stdin, ui.stdout,
77
73
# ': ' is appended to prompt
78
self.assertEqual(': ', ui.stdout.getvalue())
74
self.assertEqual(': ', ui.stderr.getvalue())
75
self.assertEqual('', ui.stdout.readline())
79
76
# stdin should be empty
80
77
self.assertEqual('', ui.stdin.readline())
88
85
it to utf8 to test that we transport the password correctly.
90
87
ui = TestUIFactory(stdin=u'baz\u1234'.encode('utf8'),
91
stdout=StringIOWrapper())
92
ui.stdin.encoding = 'utf8'
93
ui.stdout.encoding = ui.stdin.encoding
88
stdout=StringIOWrapper(),
89
stderr=StringIOWrapper())
90
ui.stderr.encoding = ui.stdout.encoding = ui.stdin.encoding = 'utf8'
94
91
pb = ui.nested_progress_bar()
96
password = self.apply_redirected(ui.stdin, ui.stdout, ui.stdout,
93
password = self.apply_redirected(ui.stdin, ui.stdout, ui.stderr,
98
95
u'Hello \u1234 %(user)s',
99
96
user=u'some\u1234')
100
97
# We use StringIO objects, we need to decode them
101
98
self.assertEqual(u'baz\u1234', password.decode('utf8'))
102
99
self.assertEqual(u'Hello \u1234 some\u1234: ',
103
ui.stdout.getvalue().decode('utf8'))
104
# stdin should be empty
100
ui.stderr.getvalue().decode('utf8'))
101
# stdin and stdout should be empty
105
102
self.assertEqual('', ui.stdin.readline())
103
self.assertEqual('', ui.stdout.readline())
107
def test_progress_construction(self):
108
"""TextUIFactory constructs the right progress view.
110
os.environ['BZR_PROGRESS_BAR'] = 'none'
111
self.assertIsInstance(TextUIFactory()._progress_view,
114
os.environ['BZR_PROGRESS_BAR'] = 'text'
115
self.assertIsInstance(TextUIFactory()._progress_view,
118
os.environ['BZR_PROGRESS_BAR'] = 'text'
119
self.assertIsInstance(TextUIFactory()._progress_view,
122
del os.environ['BZR_PROGRESS_BAR']
123
self.assertIsInstance(TextUIFactory()._progress_view,
109
126
def test_progress_note(self):
110
127
stderr = StringIO()
111
128
stdout = StringIO()
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
181
def assert_get_bool_acceptance_of_user_input(self, factory):
192
182
factory.stdin = StringIO("y\nyes with garbage\n"
193
183
"yes\nn\nnot an answer\n"
195
185
factory.stdout = StringIO()
186
factory.stderr = StringIO()
196
187
# there is no output from the base factory
197
188
self.assertEqual(True, factory.get_boolean(""))
198
189
self.assertEqual(True, factory.get_boolean(""))
224
215
def test_text_factory_prompt(self):
225
216
# see <https://launchpad.net/bugs/365891>
226
factory = TextUIFactory(None, StringIO(), StringIO())
217
factory = TextUIFactory(None, StringIO(), StringIO(), StringIO())
227
218
factory.prompt('foo %2e')
219
self.assertEqual('', factory.stdout.getvalue())
220
self.assertEqual('foo %2e', factory.stderr.getvalue())
229
222
def test_text_factory_prompts_and_clears(self):
230
223
# a get_boolean call should clear the pb before prompting
263
256
factory = SilentUIFactory()
264
257
factory.stdin = StringIO("someuser\n\n")
265
258
factory.stdout = StringIO()
266
self.assertEquals(None,
259
factory.stderr = StringIO()
260
self.assertEquals(None,
267
261
factory.get_username(u'Hello\u1234 %(host)s', host=u'some\u1234'))
268
262
self.assertEquals("", factory.stdout.getvalue())
263
self.assertEquals("", factory.stderr.getvalue())
269
264
self.assertEquals("someuser\n\n", factory.stdin.getvalue())
271
266
def test_text_ui_getusername(self):
272
267
factory = TextUIFactory(None, None, None)
273
268
factory.stdin = StringIO("someuser\n\n")
274
269
factory.stdout = StringIO()
270
factory.stderr = StringIO()
275
271
factory.stdout.encoding = "utf8"
276
272
# 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())
273
self.assertEqual("someuser",
274
factory.get_username('Hello %(host)s', host='some'))
275
self.assertEquals("Hello some: ", factory.stderr.getvalue())
276
self.assertEquals('', factory.stdout.getvalue())
280
277
self.assertEqual("", factory.get_username("Gebruiker"))
281
278
# stdin should be empty
282
279
self.assertEqual('', factory.stdin.readline())
284
281
def test_text_ui_getusername_utf8(self):
285
282
ui = TestUIFactory(stdin=u'someuser\u1234'.encode('utf8'),
286
stdout=StringIOWrapper())
287
ui.stdin.encoding = "utf8"
288
ui.stdout.encoding = ui.stdin.encoding
283
stdout=StringIOWrapper(), stderr=StringIOWrapper())
284
ui.stderr.encoding = ui.stdout.encoding = ui.stdin.encoding = "utf8"
289
285
pb = ui.nested_progress_bar()
291
287
# there is no output from the base factory
292
username = self.apply_redirected(ui.stdin, ui.stdout, ui.stdout,
288
username = self.apply_redirected(ui.stdin, ui.stdout, ui.stderr,
293
289
ui.get_username, u'Hello\u1234 %(host)s', host=u'some\u1234')
294
290
self.assertEquals(u"someuser\u1234", username.decode('utf8'))
295
self.assertEquals(u"Hello\u1234 some\u1234: ",
296
ui.stdout.getvalue().decode("utf8"))
291
self.assertEquals(u"Hello\u1234 some\u1234: ",
292
ui.stderr.getvalue().decode("utf8"))
293
self.assertEquals('', ui.stdout.getvalue())