21
from StringIO import StringIO
26
import bzrlib.errors as errors
27
from bzrlib.progress import TTYProgressBar, ProgressBarStack
23
28
from bzrlib.tests import TestCase
29
from bzrlib.tests.test_progress import _TTYStringIO
24
30
from bzrlib.ui import SilentUIFactory
25
31
from bzrlib.ui.text import TextUIFactory
27
34
class UITests(TestCase):
29
36
def test_silent_factory(self):
31
37
ui = SilentUIFactory()
32
pb = ui.progress_bar()
33
# TODO: Test that there is no output from SilentUIFactory
35
self.assertEquals(ui.get_password(), None)
36
self.assertEquals(ui.get_password(u'Hello There \u1234 %(user)s',
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',
40
49
def test_text_factory(self):
41
50
ui = TextUIFactory()
42
pb = ui.progress_bar()
51
pb = ui.nested_progress_bar()
43
53
# TODO: Test the output from TextUIFactory, perhaps by overriding sys.stdout
45
55
# Unfortunately we can't actually test the ui.get_password() because
50
60
# user=u'some\u1234')
64
def test_progress_note(self):
67
ui_factory = TextUIFactory(bar_type=TTYProgressBar)
68
pb = ui_factory.nested_progress_bar()
70
pb.to_messages_file = stdout
71
ui_factory._progress_bar_stack.bottom().to_file = stderr
73
self.assertEqual(None, result)
74
self.assertEqual("t\n", stdout.getvalue())
75
# Since there was no update() call, there should be no clear() call
76
self.failIf(re.search(r'^\r {10,}\r$', stderr.getvalue()) is not None,
77
'We cleared the stderr without anything to put there')
81
def test_progress_note_clears(self):
84
# The PQM redirects the output to a file, so it
85
# defaults to creating a Dots progress bar. we
86
# need to force it to believe we are a TTY
87
ui_factory = TextUIFactory(bar_type=TTYProgressBar)
88
pb = ui_factory.nested_progress_bar()
90
pb.to_messages_file = stdout
91
ui_factory._progress_bar_stack.bottom().to_file = stderr
92
# Create a progress update that isn't throttled
96
self.assertEqual(None, result)
97
self.assertEqual("t\n", stdout.getvalue())
98
# the exact contents will depend on the terminal width and we don't
99
# care about that right now - but you're probably running it on at
100
# least a 10-character wide terminal :)
101
self.assertContainsRe(stderr.getvalue(), r'\r {10,}\r$')
105
def test_progress_nested(self):
106
# test factory based nested and popping.
108
pb1 = ui.nested_progress_bar()
109
pb2 = ui.nested_progress_bar()
110
self.assertRaises(errors.MissingProgressBarFinish, pb1.finished)
114
def test_progress_stack(self):
115
# test the progress bar stack which the default text factory
119
# make a stack, which accepts parameters like a pb.
120
stack = ProgressBarStack(to_file=stderr, to_messages_file=stdout)
122
self.assertFalse(getattr(stack, 'note', False))
123
pb1 = stack.get_nested()
124
pb2 = stack.get_nested()
125
self.assertRaises(errors.MissingProgressBarFinish, pb1.finished)
128
# the text ui factory never actually removes the stack once its setup.
129
# we need to be able to nest again correctly from here.
130
pb1 = stack.get_nested()
131
pb2 = stack.get_nested()
132
self.assertRaises(errors.MissingProgressBarFinish, pb1.finished)
136
def test_text_factory_setting_progress_bar(self):
137
# we should be able to choose the progress bar type used.
138
factory = bzrlib.ui.text.TextUIFactory(
139
bar_type=bzrlib.progress.DotsProgressBar)
140
bar = factory.nested_progress_bar()
142
self.assertIsInstance(bar, bzrlib.progress.DotsProgressBar)
144
def test_cli_stdin_is_default_stdin(self):
145
factory = bzrlib.ui.CLIUIFactory()
146
self.assertEqual(sys.stdin, factory.stdin)
148
def assert_get_bool_acceptance_of_user_input(self, factory):
149
factory.stdin = StringIO("y\nyes with garbage\nyes\nn\nnot an answer\nno\nfoo\n")
150
factory.stdout = StringIO()
151
# there is no output from the base factory
152
self.assertEqual(True, factory.get_boolean(""))
153
self.assertEqual(True, factory.get_boolean(""))
154
self.assertEqual(False, factory.get_boolean(""))
155
self.assertEqual(False, factory.get_boolean(""))
156
self.assertEqual("foo\n", factory.stdin.read())
158
def test_silent_ui_getbool(self):
159
factory = bzrlib.ui.SilentUIFactory()
160
self.assert_get_bool_acceptance_of_user_input(factory)
162
def test_silent_factory_prompts_silently(self):
163
factory = bzrlib.ui.SilentUIFactory()
165
factory.stdin = StringIO("y\n")
168
self.apply_redirected(
169
None, stdout, stdout, factory.get_boolean, "foo")
171
self.assertEqual("", stdout.getvalue())
173
def test_text_ui_getbool(self):
174
factory = bzrlib.ui.text.TextUIFactory()
175
self.assert_get_bool_acceptance_of_user_input(factory)
177
def test_text_factory_prompts_and_clears(self):
178
# a get_boolean call should clear the pb before prompting
179
factory = bzrlib.ui.text.TextUIFactory()
180
factory.stdout = _TTYStringIO()
181
factory.stdin = StringIO("yada\ny\n")
182
pb = self.apply_redirected(
183
factory.stdin, factory.stdout, factory.stdout, factory.nested_progress_bar)
185
self.apply_redirected(
186
factory.stdin, factory.stdout, factory.stdout, pb.update, "foo", 0, 1)
189
self.apply_redirected(
190
None, factory.stdout, factory.stdout, factory.get_boolean, "what do you want")
192
# use a regular expression so that we don't depend on the particular
193
# screen width - could also set and restore $COLUMN if that has
194
# priority on all platforms, but it doesn't at present.
195
output = factory.stdout.getvalue()
197
"\r/ \\[ *\\] foo 0/1"
199
"\rwhat do you want\\? \\[y/n\\]:what do you want\\? \\[y/n\\]:",
201
self.fail("didn't match factory output %r, %s" % (factory, output))