1
# Copyright (C) 2005, 2008, 2009 Canonical Ltd
3
# This program is free software; you can redistribute it and/or modify
4
# it under the terms of the GNU General Public License as published by
5
# the Free Software Foundation; either version 2 of the License, or
6
# (at your option) any later version.
8
# This program is distributed in the hope that it will be useful,
9
# but WITHOUT ANY WARRANTY; without even the implied warranty of
10
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
11
# GNU General Public License for more details.
13
# You should have received a copy of the GNU General Public License
14
# along with this program; if not, write to the Free Software
15
# Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
17
"""Tests for the bzrlib ui
21
from StringIO import StringIO
31
from bzrlib.symbol_versioning import (
34
from bzrlib.tests import (
39
from bzrlib.tests.test_progress import (
43
from bzrlib.ui import (
49
from bzrlib.ui.text import (
57
class TestTextUIFactory(tests.TestCase):
59
def test_text_factory_ascii_password(self):
60
ui = tests.TestUIFactory(stdin='secret\n',
61
stdout=tests.StringIOWrapper(),
62
stderr=tests.StringIOWrapper())
63
pb = ui.nested_progress_bar()
65
self.assertEqual('secret',
66
self.apply_redirected(ui.stdin, ui.stdout,
69
# ': ' is appended to prompt
70
self.assertEqual(': ', ui.stderr.getvalue())
71
self.assertEqual('', ui.stdout.readline())
72
# stdin should be empty
73
self.assertEqual('', ui.stdin.readline())
77
def test_text_factory_utf8_password(self):
78
"""Test an utf8 password.
80
We can't predict what encoding users will have for stdin, so we force
81
it to utf8 to test that we transport the password correctly.
83
ui = tests.TestUIFactory(stdin=u'baz\u1234'.encode('utf8'),
84
stdout=tests.StringIOWrapper(),
85
stderr=tests.StringIOWrapper())
86
ui.stderr.encoding = ui.stdout.encoding = ui.stdin.encoding = 'utf8'
87
pb = ui.nested_progress_bar()
89
password = self.apply_redirected(ui.stdin, ui.stdout, ui.stderr,
91
u'Hello \u1234 %(user)s',
93
# We use StringIO objects, we need to decode them
94
self.assertEqual(u'baz\u1234', password.decode('utf8'))
95
self.assertEqual(u'Hello \u1234 some\u1234: ',
96
ui.stderr.getvalue().decode('utf8'))
97
# stdin and stdout should be empty
98
self.assertEqual('', ui.stdin.readline())
99
self.assertEqual('', ui.stdout.readline())
103
def test_progress_note(self):
106
ui_factory = TextUIFactory(stdin=StringIO(''),
109
pb = ui_factory.nested_progress_bar()
111
result = self.applyDeprecated(deprecated_in((2, 1, 0)),
114
self.assertEqual(None, result)
115
self.assertEqual("t\n", stdout.getvalue())
116
# Since there was no update() call, there should be no clear() call
117
self.failIf(re.search(r'^\r {10,}\r$',
118
stderr.getvalue()) is not None,
119
'We cleared the stderr without anything to put there')
123
def test_progress_note_clears(self):
124
stderr = _TTYStringIO()
125
stdout = _TTYStringIO()
126
# so that we get a TextProgressBar
127
os.environ['TERM'] = 'xterm'
128
ui_factory = TextUIFactory(
130
stdout=stdout, stderr=stderr)
131
self.assertIsInstance(ui_factory._progress_view,
133
pb = ui_factory.nested_progress_bar()
135
# Create a progress update that isn't throttled
137
result = self.applyDeprecated(deprecated_in((2, 1, 0)),
139
self.assertEqual(None, result)
140
self.assertEqual("t\n", stdout.getvalue())
141
# the exact contents will depend on the terminal width and we don't
142
# care about that right now - but you're probably running it on at
143
# least a 10-character wide terminal :)
144
self.assertContainsRe(stderr.getvalue(), r'\r {10,}\r$')
148
def test_progress_nested(self):
149
# test factory based nested and popping.
150
ui = TextUIFactory(None, None, None)
151
pb1 = ui.nested_progress_bar()
152
pb2 = ui.nested_progress_bar()
153
# You do get a warning if the outermost progress bar wasn't finished
154
# first - it's not clear if this is really useful or if it should just
155
# become orphaned -- mbp 20090120
156
warnings, _ = self.callCatchWarnings(pb1.finished)
157
if len(warnings) != 1:
158
self.fail("unexpected warnings: %r" % (warnings,))
162
def test_text_ui_get_boolean(self):
163
stdin = StringIO("y\n" # True
165
"yes with garbage\nY\n" # True
166
"not an answer\nno\n" # False
167
"I'm sure!\nyes\n" # True
172
factory = TextUIFactory(stdin, stdout, stderr)
173
self.assertEqual(True, factory.get_boolean(""))
174
self.assertEqual(False, factory.get_boolean(""))
175
self.assertEqual(True, factory.get_boolean(""))
176
self.assertEqual(False, factory.get_boolean(""))
177
self.assertEqual(True, factory.get_boolean(""))
178
self.assertEqual(False, factory.get_boolean(""))
179
self.assertEqual("foo\n", factory.stdin.read())
180
# stdin should be empty
181
self.assertEqual('', factory.stdin.readline())
183
def test_text_factory_prompt(self):
184
# see <https://launchpad.net/bugs/365891>
185
factory = TextUIFactory(StringIO(), StringIO(), StringIO())
186
factory.prompt('foo %2e')
187
self.assertEqual('', factory.stdout.getvalue())
188
self.assertEqual('foo %2e', factory.stderr.getvalue())
190
def test_text_factory_prompts_and_clears(self):
191
# a get_boolean call should clear the pb before prompting
193
os.environ['TERM'] = 'xterm'
194
factory = TextUIFactory(stdin=StringIO("yada\ny\n"), stdout=out, stderr=out)
195
pb = factory.nested_progress_bar()
197
pb.show_spinner = False
198
pb.show_count = False
199
pb.update("foo", 0, 1)
200
self.assertEqual(True,
201
self.apply_redirected(None, factory.stdout,
205
output = out.getvalue()
206
self.assertContainsRe(factory.stdout.getvalue(),
208
self.assertContainsRe(factory.stdout.getvalue(),
209
r"what do you want\? \[y/n\]: what do you want\? \[y/n\]: ")
210
# stdin should have been totally consumed
211
self.assertEqual('', factory.stdin.readline())
213
def test_text_tick_after_update(self):
214
ui_factory = TextUIFactory(stdout=StringIO(), stderr=StringIO())
215
pb = ui_factory.nested_progress_bar()
217
pb.update('task', 0, 3)
218
# Reset the clock, so that it actually tries to repaint itself
219
ui_factory._progress_view._last_repaint = time.time() - 1.0
224
def test_text_ui_getusername(self):
225
factory = TextUIFactory(None, None, None)
226
factory.stdin = StringIO("someuser\n\n")
227
factory.stdout = StringIO()
228
factory.stderr = StringIO()
229
factory.stdout.encoding = "utf8"
230
# there is no output from the base factory
231
self.assertEqual("someuser",
232
factory.get_username('Hello %(host)s', host='some'))
233
self.assertEquals("Hello some: ", factory.stderr.getvalue())
234
self.assertEquals('', factory.stdout.getvalue())
235
self.assertEqual("", factory.get_username("Gebruiker"))
236
# stdin should be empty
237
self.assertEqual('', factory.stdin.readline())
239
def test_text_ui_getusername_utf8(self):
240
ui = tests.TestUIFactory(stdin=u'someuser\u1234'.encode('utf8'),
241
stdout=tests.StringIOWrapper(),
242
stderr=tests.StringIOWrapper())
243
ui.stderr.encoding = ui.stdout.encoding = ui.stdin.encoding = "utf8"
244
pb = ui.nested_progress_bar()
246
# there is no output from the base factory
247
username = self.apply_redirected(ui.stdin, ui.stdout, ui.stderr,
248
ui.get_username, u'Hello\u1234 %(host)s', host=u'some\u1234')
249
self.assertEquals(u"someuser\u1234", username.decode('utf8'))
250
self.assertEquals(u"Hello\u1234 some\u1234: ",
251
ui.stderr.getvalue().decode("utf8"))
252
self.assertEquals('', ui.stdout.getvalue())
257
class TestTextUIOutputStream(TestCase):
258
"""Tests for output stream that synchronizes with progress bar."""
260
def test_output_clears_terminal(self):
265
uif = TextUIFactory(None, stdout, stderr)
266
uif.clear_term = lambda: clear_calls.append('clear')
268
stream = TextUIOutputStream(uif, uif.stdout)
269
stream.write("Hello world!\n")
270
stream.write("there's more...\n")
271
stream.writelines(["1\n", "2\n", "3\n"])
273
self.assertEqual(stdout.getvalue(),
277
self.assertEqual(['clear', 'clear', 'clear'],
283
class UITests(tests.TestCase):
285
def test_progress_construction(self):
286
"""TextUIFactory constructs the right progress view.
288
for (file_class, term, pb, expected_pb_class) in (
289
# on an xterm, either use them or not as the user requests,
290
# otherwise default on
291
(_TTYStringIO, 'xterm', 'none', NullProgressView),
292
(_TTYStringIO, 'xterm', 'text', TextProgressView),
293
(_TTYStringIO, 'xterm', None, TextProgressView),
294
# on a dumb terminal, again if there's explicit configuration do
295
# it, otherwise default off
296
(_TTYStringIO, 'dumb', 'none', NullProgressView),
297
(_TTYStringIO, 'dumb', 'text', TextProgressView),
298
(_TTYStringIO, 'dumb', None, NullProgressView),
299
# on a non-tty terminal, it's null regardless of $TERM
300
(StringIO, 'xterm', None, NullProgressView),
301
(StringIO, 'dumb', None, NullProgressView),
302
# however, it can still be forced on
303
(StringIO, 'dumb', 'text', TextProgressView),
305
os.environ['TERM'] = term
307
if 'BZR_PROGRESS_BAR' in os.environ:
308
del os.environ['BZR_PROGRESS_BAR']
310
os.environ['BZR_PROGRESS_BAR'] = pb
311
stdin = file_class('')
312
stderr = file_class()
313
stdout = file_class()
314
uif = make_ui_for_terminal(stdin, stdout, stderr)
315
self.assertIsInstance(uif, TextUIFactory,
316
"TERM=%s BZR_PROGRESS_BAR=%s uif=%r" % (term, pb, uif,))
317
self.assertIsInstance(uif.make_progress_view(),
319
"TERM=%s BZR_PROGRESS_BAR=%s uif=%r" % (term, pb, uif,))
321
def test_text_ui_non_terminal(self):
322
"""Even on non-ttys, make_ui_for_terminal gives a text ui."""
323
stdin = _NonTTYStringIO('')
324
stderr = _NonTTYStringIO()
325
stdout = _NonTTYStringIO()
326
for term_type in ['dumb', None, 'xterm']:
327
if term_type is None:
328
del os.environ['TERM']
330
os.environ['TERM'] = term_type
331
uif = make_ui_for_terminal(stdin, stdout, stderr)
332
self.assertIsInstance(uif, TextUIFactory,
333
'TERM=%r' % (term_type,))
336
class SilentUITests(TestCase):
338
def test_silent_factory_get_password(self):
339
# A silent factory that can't do user interaction can't get a
340
# password. Possibly it should raise a more specific error but it
342
ui = SilentUIFactory()
346
self.apply_redirected,
347
None, stdout, stdout, ui.get_password)
348
# and it didn't write anything out either
349
self.assertEqual('', stdout.getvalue())
351
def test_silent_ui_getbool(self):
352
factory = SilentUIFactory()
356
self.apply_redirected,
357
None, stdout, stdout, factory.get_boolean, "foo")
360
class TestUIFactoryTests(TestCase):
362
def test_test_ui_factory_progress(self):
363
# there's no output; we just want to make sure this doesn't crash -
364
# see https://bugs.edge.launchpad.net/bzr/+bug/408201
366
pb = ui.nested_progress_bar()
372
class CannedInputUIFactoryTests(TestCase):
374
def test_canned_input_get_input(self):
375
uif = CannedInputUIFactory([True, 'mbp', 'password'])
376
self.assertEqual(uif.get_boolean('Extra cheese?'), True)
377
self.assertEqual(uif.get_username('Enter your user name'), 'mbp')
378
self.assertEqual(uif.get_password('Password for %(host)s', host='example.com'),
382
class TestBoolFromString(tests.TestCase):
384
def assertIsTrue(self, s, accepted_values=None):
385
res = _mod_ui.bool_from_string(s, accepted_values=accepted_values)
386
self.assertEquals(True, res)
388
def assertIsFalse(self, s, accepted_values=None):
389
res = _mod_ui.bool_from_string(s, accepted_values=accepted_values)
390
self.assertEquals(False, res)
392
def assertIsNone(self, s, accepted_values=None):
393
res = _mod_ui.bool_from_string(s, accepted_values=accepted_values)
394
self.assertIs(None, res)
396
def test_know_valid_values(self):
397
self.assertIsTrue('true')
398
self.assertIsFalse('false')
399
self.assertIsTrue('1')
400
self.assertIsFalse('0')
401
self.assertIsTrue('on')
402
self.assertIsFalse('off')
403
self.assertIsTrue('yes')
404
self.assertIsFalse('no')
405
self.assertIsTrue('y')
406
self.assertIsFalse('n')
407
# Also try some case variations
408
self.assertIsTrue('True')
409
self.assertIsFalse('False')
410
self.assertIsTrue('On')
411
self.assertIsFalse('Off')
412
self.assertIsTrue('ON')
413
self.assertIsFalse('OFF')
414
self.assertIsTrue('oN')
415
self.assertIsFalse('oFf')
417
def test_invalid_values(self):
418
self.assertIsNone(None)
419
self.assertIsNone('doubt')
420
self.assertIsNone('frue')
421
self.assertIsNone('talse')
422
self.assertIsNone('42')
424
def test_provided_values(self):
425
av = dict(y=True, n=False, yes=True, no=False)
426
self.assertIsTrue('y', av)
427
self.assertIsTrue('Y', av)
428
self.assertIsTrue('Yes', av)
429
self.assertIsFalse('n', av)
430
self.assertIsFalse('N', av)
431
self.assertIsFalse('No', av)
432
self.assertIsNone('1', av)
433
self.assertIsNone('0', av)
434
self.assertIsNone('on', av)
435
self.assertIsNone('off', av)