87
99
def test_text_factory_utf8_password(self):
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'))
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()
94
105
ui.stderr.encoding = ui.stdout.encoding = ui.stdin.encoding = 'utf8'
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())
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())
111
114
def test_text_ui_get_boolean(self):
112
115
stdin = tests.StringIOWrapper("y\n" # True
114
119
"yes with garbage\nY\n" # True
115
120
"not an answer\nno\n" # False
116
121
"I'm sure!\nyes\n" # True
119
124
stdout = tests.StringIOWrapper()
120
125
stderr = tests.StringIOWrapper()
121
126
factory = _mod_ui_text.TextUIFactory(stdin, stdout, stderr)
122
self.assertEqual(True, factory.get_boolean(""))
123
self.assertEqual(False, factory.get_boolean(""))
124
self.assertEqual(True, factory.get_boolean(""))
125
self.assertEqual(False, factory.get_boolean(""))
126
self.assertEqual(True, factory.get_boolean(""))
127
self.assertEqual(False, factory.get_boolean(""))
128
self.assertEqual("foo\n", factory.stdin.read())
129
# stdin should be empty
130
self.assertEqual('', factory.stdin.readline())
127
self.assertEqual(True, factory.get_boolean(u""))
128
self.assertEqual(False, factory.get_boolean(u""))
129
self.assertEqual(True, factory.get_boolean(u""))
130
self.assertEqual(False, factory.get_boolean(u""))
131
self.assertEqual(True, factory.get_boolean(u""))
132
self.assertEqual(False, factory.get_boolean(u""))
133
self.assertEqual(True, factory.get_boolean(u""))
134
self.assertEqual(False, factory.get_boolean(u""))
135
self.assertEqual("foo\n", factory.stdin.read())
136
# stdin should be empty
137
self.assertEqual('', factory.stdin.readline())
138
# return false on EOF
139
self.assertEqual(False, factory.get_boolean(u""))
141
def test_text_ui_choose_bad_parameters(self):
142
stdin = tests.StringIOWrapper()
143
stdout = tests.StringIOWrapper()
144
stderr = tests.StringIOWrapper()
145
factory = _mod_ui_text.TextUIFactory(stdin, stdout, stderr)
146
# invalid default index
147
self.assertRaises(ValueError, factory.choose, u"", u"&Yes\n&No", 3)
149
self.assertRaises(ValueError, factory.choose, u"", u"&choice\n&ChOiCe")
150
# duplicated shortcut
151
self.assertRaises(ValueError, factory.choose, u"", u"&choice1\nchoi&ce2")
153
def test_text_ui_choose_prompt(self):
154
stdin = tests.StringIOWrapper()
155
stdout = tests.StringIOWrapper()
156
stderr = tests.StringIOWrapper()
157
factory = _mod_ui_text.TextUIFactory(stdin, stdout, stderr)
158
# choices with explicit shortcuts
159
factory.choose(u"prompt", u"&yes\n&No\nmore &info")
160
self.assertEqual("prompt ([y]es, [N]o, more [i]nfo): \n", factory.stderr.getvalue())
161
# automatic shortcuts
162
factory.stderr.truncate(0)
163
factory.choose(u"prompt", u"yes\nNo\nmore info")
164
self.assertEqual("prompt ([y]es, [N]o, [m]ore info): \n", factory.stderr.getvalue())
166
def test_text_ui_choose_return_values(self):
167
choose = lambda: factory.choose(u"", u"&Yes\n&No\nMaybe\nmore &info", 3)
168
stdin = tests.StringIOWrapper("y\n" # 0
172
"b\na\nd \n" # bad shortcuts, all ignored
173
"yes with garbage\nY\n" # 0
174
"not an answer\nno\n" # 1
175
"info\nmore info\n" # 3
178
stdout = tests.StringIOWrapper()
179
stderr = tests.StringIOWrapper()
180
factory = _mod_ui_text.TextUIFactory(stdin, stdout, stderr)
181
self.assertEqual(0, choose())
182
self.assertEqual(1, choose())
183
self.assertEqual(3, choose())
184
self.assertEqual(1, choose())
185
self.assertEqual(0, choose())
186
self.assertEqual(1, choose())
187
self.assertEqual(3, choose())
188
self.assertEqual(2, choose())
189
self.assertEqual("foo\n", factory.stdin.read())
190
# stdin should be empty
191
self.assertEqual('', factory.stdin.readline())
193
self.assertEqual(None, choose())
195
def test_text_ui_choose_no_default(self):
196
stdin = tests.StringIOWrapper(" \n" # no default, invalid!
199
stdout = tests.StringIOWrapper()
200
stderr = tests.StringIOWrapper()
201
factory = _mod_ui_text.TextUIFactory(stdin, stdout, stderr)
202
self.assertEqual(0, factory.choose(u"", u"&Yes\n&No"))
203
self.assertEqual("foo\n", factory.stdin.read())
132
205
def test_text_ui_get_integer(self):
133
206
stdin = tests.StringIOWrapper(
137
210
stdout = tests.StringIOWrapper()
138
211
stderr = tests.StringIOWrapper()
139
212
factory = _mod_ui_text.TextUIFactory(stdin, stdout, stderr)
140
self.assertEqual(1, factory.get_integer(""))
141
self.assertEqual(-2, factory.get_integer(""))
142
self.assertEqual(42, factory.get_integer(""))
213
self.assertEqual(1, factory.get_integer(u""))
214
self.assertEqual(-2, factory.get_integer(u""))
215
self.assertEqual(42, factory.get_integer(u""))
144
217
def test_text_factory_prompt(self):
145
218
# see <https://launchpad.net/bugs/365891>
146
219
StringIO = tests.StringIOWrapper
147
220
factory = _mod_ui_text.TextUIFactory(StringIO(), StringIO(), StringIO())
148
factory.prompt('foo %2e')
221
factory.prompt(u'foo %2e')
149
222
self.assertEqual('', factory.stdout.getvalue())
150
223
self.assertEqual('foo %2e', factory.stderr.getvalue())
152
225
def test_text_factory_prompts_and_clears(self):
153
226
# a get_boolean call should clear the pb before prompting
154
out = test_progress._TTYStringIO()
155
228
self.overrideEnv('TERM', 'xterm')
156
229
factory = _mod_ui_text.TextUIFactory(
157
230
stdin=tests.StringIOWrapper("yada\ny\n"),
190
263
def test_text_ui_getusername(self):
191
factory = _mod_ui_text.TextUIFactory(None, None, None)
192
factory.stdin = tests.StringIOWrapper("someuser\n\n")
193
factory.stdout = tests.StringIOWrapper()
194
factory.stderr = tests.StringIOWrapper()
195
factory.stdout.encoding = "utf8"
196
# there is no output from the base factory
197
self.assertEqual("someuser",
198
factory.get_username('Hello %(host)s', host='some'))
199
self.assertEquals("Hello some: ", factory.stderr.getvalue())
200
self.assertEquals('', factory.stdout.getvalue())
201
self.assertEqual("", factory.get_username("Gebruiker"))
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.assertEquals('Hello some: ', ui.stderr.getvalue())
272
self.assertEquals('', ui.stdout.getvalue())
273
self.assertEqual('', ui.get_username(u"Gebruiker"))
202
274
# stdin should be empty
203
self.assertEqual('', factory.stdin.readline())
275
self.assertEqual('', ui.stdin.readline())
205
277
def test_text_ui_getusername_utf8(self):
206
ui = tests.TestUIFactory(stdin=u'someuser\u1234'.encode('utf8'),
207
stdout=tests.StringIOWrapper(),
208
stderr=tests.StringIOWrapper())
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()
209
282
ui.stderr.encoding = ui.stdout.encoding = ui.stdin.encoding = "utf8"
210
pb = ui.nested_progress_bar()
212
# there is no output from the base factory
213
username = self.apply_redirected(ui.stdin, ui.stdout, ui.stderr,
214
ui.get_username, u'Hello\u1234 %(host)s', host=u'some\u1234')
215
self.assertEquals(u"someuser\u1234", username.decode('utf8'))
216
self.assertEquals(u"Hello\u1234 some\u1234: ",
217
ui.stderr.getvalue().decode("utf8"))
218
self.assertEquals('', ui.stdout.getvalue())
283
username = ui.get_username(u'Hello %(host)s', host=u'some\u1234')
284
self.assertEquals(u"someuser\u1234", username)
285
self.assertEquals(u"Hello some\u1234: ",
286
ui.stderr.getvalue().decode("utf8"))
287
self.assertEquals('', ui.stdout.getvalue())
222
289
def test_quietness(self):
223
290
self.overrideEnv('BZR_PROGRESS_BAR', 'text')
224
291
ui_factory = _mod_ui_text.TextUIFactory(None,
225
test_progress._TTYStringIO(),
226
test_progress._TTYStringIO())
227
294
self.assertIsInstance(ui_factory._progress_view,
228
295
_mod_ui_text.TextProgressView)
229
296
ui_factory.be_quiet(True)
372
438
def test_canned_input_get_input(self):
373
439
uif = _mod_ui.CannedInputUIFactory([True, 'mbp', 'password', 42])
374
self.assertEqual(True, uif.get_boolean('Extra cheese?'))
375
self.assertEqual('mbp', uif.get_username('Enter your user name'))
440
self.assertEqual(True, uif.get_boolean(u'Extra cheese?'))
441
self.assertEqual('mbp', uif.get_username(u'Enter your user name'))
376
442
self.assertEqual('password',
377
uif.get_password('Password for %(host)s',
443
uif.get_password(u'Password for %(host)s',
378
444
host='example.com'))
379
self.assertEqual(42, uif.get_integer('And all that jazz ?'))
445
self.assertEqual(42, uif.get_integer(u'And all that jazz ?'))
382
448
class TestBoolFromString(tests.TestCase):