26
26
import bzrlib.errors as errors
27
from bzrlib.progress import TTYProgressBar, ProgressBarStack
28
from bzrlib.tests import TestCase
27
from bzrlib.progress import DotsProgressBar, TTYProgressBar, ProgressBarStack
28
from bzrlib.tests import (
29
33
from bzrlib.tests.test_progress import _TTYStringIO
30
34
from bzrlib.ui import SilentUIFactory
31
35
from bzrlib.ui.text import TextUIFactory
36
40
def test_silent_factory(self):
37
41
ui = SilentUIFactory()
38
pb = ui.nested_progress_bar()
40
# TODO: Test that there is no output from SilentUIFactory
42
self.assertEquals(ui.get_password(), None)
43
self.assertEquals(ui.get_password(u'Hello There \u1234 %(user)s',
49
def test_text_factory(self):
51
pb = ui.nested_progress_bar()
53
# TODO: Test the output from TextUIFactory, perhaps by overriding sys.stdout
55
# Unfortunately we can't actually test the ui.get_password() because
56
# that would actually prompt the user for a password during the test suite
57
# This has been tested manually with both LANG=en_US.utf-8 and LANG=C
59
# self.assertEquals(ui.get_password(u"%(user)s please type 'bogus'",
43
self.assertEqual(None,
44
self.apply_redirected(None, stdout, stdout,
46
self.assertEqual('', stdout.getvalue())
47
self.assertEqual(None,
48
self.apply_redirected(None, stdout, stdout,
50
u'Hello\u1234 %(user)s',
52
self.assertEqual('', stdout.getvalue())
54
def test_text_factory_ascii_password(self):
55
ui = TestUIFactory(stdin='secret\n', stdout=StringIOWrapper())
56
pb = ui.nested_progress_bar()
58
self.assertEqual('secret',
59
self.apply_redirected(ui.stdin, ui.stdout,
62
# ': ' is appended to prompt
63
self.assertEqual(': ', ui.stdout.getvalue())
67
def test_text_factory_utf8_password(self):
68
"""Test an utf8 password.
70
We can't predict what encoding users will have for stdin, so we force
71
it to utf8 to test that we transport the password correctly.
73
ui = TestUIFactory(stdin=u'baz\u1234'.encode('utf8'),
74
stdout=StringIOWrapper())
75
ui.stdin.encoding = 'utf8'
76
ui.stdout.encoding = ui.stdin.encoding
77
pb = ui.nested_progress_bar()
79
password = self.apply_redirected(ui.stdin, ui.stdout, ui.stdout,
81
u'Hello \u1234 %(user)s',
83
# We use StringIO objects, we need to decode them
84
self.assertEqual(u'baz\u1234', password.decode('utf8'))
85
self.assertEqual(u'Hello \u1234 some\u1234: ',
86
ui.stdout.getvalue().decode('utf8'))
64
90
def test_progress_note(self):
65
91
stderr = StringIO()
136
162
def test_text_factory_setting_progress_bar(self):
137
163
# we should be able to choose the progress bar type used.
138
164
factory = bzrlib.ui.text.TextUIFactory(
139
bar_type=bzrlib.progress.DotsProgressBar)
165
bar_type=DotsProgressBar)
140
166
bar = factory.nested_progress_bar()
142
self.assertIsInstance(bar, bzrlib.progress.DotsProgressBar)
168
self.assertIsInstance(bar, DotsProgressBar)
144
170
def test_cli_stdin_is_default_stdin(self):
145
171
factory = bzrlib.ui.CLIUIFactory()
177
203
def test_text_factory_prompts_and_clears(self):
178
204
# a get_boolean call should clear the pb before prompting
179
factory = bzrlib.ui.text.TextUIFactory()
205
factory = bzrlib.ui.text.TextUIFactory(bar_type=DotsProgressBar)
180
206
factory.stdout = _TTYStringIO()
181
207
factory.stdin = StringIO("yada\ny\n")
182
208
pb = self.apply_redirected(
189
215
self.apply_redirected(
190
216
None, factory.stdout, factory.stdout, factory.get_boolean, "what do you want")
192
# FIXME: This assumes the factory's going to produce a spinner-style
193
# progress bar, but it won't if this is run from a dumb terminal (e.g.
194
# from inside gvim.) -- mbp 20061014
196
# use a regular expression so that we don't depend on the particular
197
# screen width - could also set and restore $COLUMN if that has
198
# priority on all platforms, but it doesn't at present.
199
218
output = factory.stdout.getvalue()
201
"\r/ \\[ *\\] foo 0/1"
203
"\rwhat do you want\\? \\[y/n\\]:what do you want\\? \\[y/n\\]:",
205
self.fail("didn't match factory output %r, %r" % (factory, output))
219
self.assertEqual("foo: .\n"
220
"what do you want? [y/n]: what do you want? [y/n]: ",
221
factory.stdout.getvalue())