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'],
282
class UITests(tests.TestCase):
284
def test_progress_construction(self):
285
"""TextUIFactory constructs the right progress view.
287
for (file_class, term, pb, expected_pb_class) in (
288
# on an xterm, either use them or not as the user requests,
289
# otherwise default on
290
(_TTYStringIO, 'xterm', 'none', NullProgressView),
291
(_TTYStringIO, 'xterm', 'text', TextProgressView),
292
(_TTYStringIO, 'xterm', None, TextProgressView),
293
# on a dumb terminal, again if there's explicit configuration do
294
# it, otherwise default off
295
(_TTYStringIO, 'dumb', 'none', NullProgressView),
296
(_TTYStringIO, 'dumb', 'text', TextProgressView),
297
(_TTYStringIO, 'dumb', None, NullProgressView),
298
# on a non-tty terminal, it's null regardless of $TERM
299
(StringIO, 'xterm', None, NullProgressView),
300
(StringIO, 'dumb', None, NullProgressView),
301
# however, it can still be forced on
302
(StringIO, 'dumb', 'text', TextProgressView),
304
os.environ['TERM'] = term
306
if 'BZR_PROGRESS_BAR' in os.environ:
307
del os.environ['BZR_PROGRESS_BAR']
309
os.environ['BZR_PROGRESS_BAR'] = pb
310
stdin = file_class('')
311
stderr = file_class()
312
stdout = file_class()
313
uif = make_ui_for_terminal(stdin, stdout, stderr)
314
self.assertIsInstance(uif, TextUIFactory,
315
"TERM=%s BZR_PROGRESS_BAR=%s uif=%r" % (term, pb, uif,))
316
self.assertIsInstance(uif.make_progress_view(),
318
"TERM=%s BZR_PROGRESS_BAR=%s uif=%r" % (term, pb, uif,))
320
def test_text_ui_non_terminal(self):
321
"""Even on non-ttys, make_ui_for_terminal gives a text ui."""
322
stdin = _NonTTYStringIO('')
323
stderr = _NonTTYStringIO()
324
stdout = _NonTTYStringIO()
325
for term_type in ['dumb', None, 'xterm']:
326
if term_type is None:
327
del os.environ['TERM']
329
os.environ['TERM'] = term_type
330
uif = make_ui_for_terminal(stdin, stdout, stderr)
331
self.assertIsInstance(uif, TextUIFactory,
332
'TERM=%r' % (term_type,))
335
class SilentUITests(TestCase):
337
def test_silent_factory_get_password(self):
338
# A silent factory that can't do user interaction can't get a
339
# password. Possibly it should raise a more specific error but it
341
ui = SilentUIFactory()
345
self.apply_redirected,
346
None, stdout, stdout, ui.get_password)
347
# and it didn't write anything out either
348
self.assertEqual('', stdout.getvalue())
350
def test_silent_ui_getbool(self):
351
factory = SilentUIFactory()
355
self.apply_redirected,
356
None, stdout, stdout, factory.get_boolean, "foo")
359
class TestUIFactoryTests(TestCase):
361
def test_test_ui_factory_progress(self):
362
# there's no output; we just want to make sure this doesn't crash -
363
# see https://bugs.edge.launchpad.net/bzr/+bug/408201
365
pb = ui.nested_progress_bar()
371
class CannedInputUIFactoryTests(TestCase):
373
def test_canned_input_get_input(self):
374
uif = CannedInputUIFactory([True, 'mbp', 'password'])
375
self.assertEqual(uif.get_boolean('Extra cheese?'), True)
376
self.assertEqual(uif.get_username('Enter your user name'), 'mbp')
377
self.assertEqual(uif.get_password('Password for %(host)s', host='example.com'),
381
class TestBoolFromString(tests.TestCase):
383
def assertIsTrue(self, s, accepted_values=None):
384
res = _mod_ui.bool_from_string(s, accepted_values=accepted_values)
385
self.assertEquals(True, res)
387
def assertIsFalse(self, s, accepted_values=None):
388
res = _mod_ui.bool_from_string(s, accepted_values=accepted_values)
389
self.assertEquals(False, res)
391
def assertIsNone(self, s, accepted_values=None):
392
res = _mod_ui.bool_from_string(s, accepted_values=accepted_values)
393
self.assertIs(None, res)
395
def test_know_valid_values(self):
396
self.assertIsTrue('true')
397
self.assertIsFalse('false')
398
self.assertIsTrue('1')
399
self.assertIsFalse('0')
400
self.assertIsTrue('on')
401
self.assertIsFalse('off')
402
self.assertIsTrue('yes')
403
self.assertIsFalse('no')
404
self.assertIsTrue('y')
405
self.assertIsFalse('n')
406
# Also try some case variations
407
self.assertIsTrue('True')
408
self.assertIsFalse('False')
409
self.assertIsTrue('On')
410
self.assertIsFalse('Off')
411
self.assertIsTrue('ON')
412
self.assertIsFalse('OFF')
413
self.assertIsTrue('oN')
414
self.assertIsFalse('oFf')
416
def test_invalid_values(self):
417
self.assertIsNone(None)
418
self.assertIsNone('doubt')
419
self.assertIsNone('frue')
420
self.assertIsNone('talse')
421
self.assertIsNone('42')
423
def test_provided_values(self):
424
av = dict(y=True, n=False, yes=True, no=False)
425
self.assertIsTrue('y', av)
426
self.assertIsTrue('Y', av)
427
self.assertIsTrue('Yes', av)
428
self.assertIsFalse('n', av)
429
self.assertIsFalse('N', av)
430
self.assertIsFalse('No', av)
431
self.assertIsNone('1', av)
432
self.assertIsNone('0', av)
433
self.assertIsNone('on', av)
434
self.assertIsNone('off', av)