1
# Copyright (C) 2005 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., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
17
"""Tests for the bzrlib ui
21
from StringIO import StringIO
26
import bzrlib.errors as errors
27
from bzrlib.progress import DotsProgressBar, TTYProgressBar, ProgressBarStack
28
from bzrlib.tests import (
33
from bzrlib.tests.test_progress import _TTYStringIO
34
from bzrlib.ui import SilentUIFactory
35
from bzrlib.ui.text import TextUIFactory
38
class UITests(TestCase):
40
def test_silent_factory(self):
41
ui = SilentUIFactory()
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'))
90
def test_progress_note(self):
93
ui_factory = TextUIFactory(bar_type=TTYProgressBar)
94
pb = ui_factory.nested_progress_bar()
96
pb.to_messages_file = stdout
97
ui_factory._progress_bar_stack.bottom().to_file = stderr
99
self.assertEqual(None, result)
100
self.assertEqual("t\n", stdout.getvalue())
101
# Since there was no update() call, there should be no clear() call
102
self.failIf(re.search(r'^\r {10,}\r$', stderr.getvalue()) is not None,
103
'We cleared the stderr without anything to put there')
107
def test_progress_note_clears(self):
110
# The PQM redirects the output to a file, so it
111
# defaults to creating a Dots progress bar. we
112
# need to force it to believe we are a TTY
113
ui_factory = TextUIFactory(bar_type=TTYProgressBar)
114
pb = ui_factory.nested_progress_bar()
116
pb.to_messages_file = stdout
117
ui_factory._progress_bar_stack.bottom().to_file = stderr
118
# Create a progress update that isn't throttled
121
result = pb.note('t')
122
self.assertEqual(None, result)
123
self.assertEqual("t\n", stdout.getvalue())
124
# the exact contents will depend on the terminal width and we don't
125
# care about that right now - but you're probably running it on at
126
# least a 10-character wide terminal :)
127
self.assertContainsRe(stderr.getvalue(), r'\r {10,}\r$')
131
def test_progress_nested(self):
132
# test factory based nested and popping.
134
pb1 = ui.nested_progress_bar()
135
pb2 = ui.nested_progress_bar()
136
self.assertRaises(errors.MissingProgressBarFinish, pb1.finished)
140
def test_progress_stack(self):
141
# test the progress bar stack which the default text factory
145
# make a stack, which accepts parameters like a pb.
146
stack = ProgressBarStack(to_file=stderr, to_messages_file=stdout)
148
self.assertFalse(getattr(stack, 'note', False))
149
pb1 = stack.get_nested()
150
pb2 = stack.get_nested()
151
self.assertRaises(errors.MissingProgressBarFinish, pb1.finished)
154
# the text ui factory never actually removes the stack once its setup.
155
# we need to be able to nest again correctly from here.
156
pb1 = stack.get_nested()
157
pb2 = stack.get_nested()
158
self.assertRaises(errors.MissingProgressBarFinish, pb1.finished)
162
def test_text_factory_setting_progress_bar(self):
163
# we should be able to choose the progress bar type used.
164
factory = bzrlib.ui.text.TextUIFactory(
165
bar_type=DotsProgressBar)
166
bar = factory.nested_progress_bar()
168
self.assertIsInstance(bar, DotsProgressBar)
170
def test_cli_stdin_is_default_stdin(self):
171
factory = bzrlib.ui.CLIUIFactory()
172
self.assertEqual(sys.stdin, factory.stdin)
174
def assert_get_bool_acceptance_of_user_input(self, factory):
175
factory.stdin = StringIO("y\nyes with garbage\nyes\nn\nnot an answer\nno\nfoo\n")
176
factory.stdout = StringIO()
177
# there is no output from the base factory
178
self.assertEqual(True, factory.get_boolean(""))
179
self.assertEqual(True, factory.get_boolean(""))
180
self.assertEqual(False, factory.get_boolean(""))
181
self.assertEqual(False, factory.get_boolean(""))
182
self.assertEqual("foo\n", factory.stdin.read())
184
def test_silent_ui_getbool(self):
185
factory = bzrlib.ui.SilentUIFactory()
186
self.assert_get_bool_acceptance_of_user_input(factory)
188
def test_silent_factory_prompts_silently(self):
189
factory = bzrlib.ui.SilentUIFactory()
191
factory.stdin = StringIO("y\n")
194
self.apply_redirected(
195
None, stdout, stdout, factory.get_boolean, "foo")
197
self.assertEqual("", stdout.getvalue())
199
def test_text_ui_getbool(self):
200
factory = bzrlib.ui.text.TextUIFactory()
201
self.assert_get_bool_acceptance_of_user_input(factory)
203
def test_text_factory_prompts_and_clears(self):
204
# a get_boolean call should clear the pb before prompting
205
factory = bzrlib.ui.text.TextUIFactory(bar_type=DotsProgressBar)
206
factory.stdout = _TTYStringIO()
207
factory.stdin = StringIO("yada\ny\n")
208
pb = self.apply_redirected(
209
factory.stdin, factory.stdout, factory.stdout, factory.nested_progress_bar)
211
self.apply_redirected(
212
factory.stdin, factory.stdout, factory.stdout, pb.update, "foo", 0, 1)
215
self.apply_redirected(
216
None, factory.stdout, factory.stdout, factory.get_boolean, "what do you want")
218
output = factory.stdout.getvalue()
219
self.assertEqual("foo: .\n"
220
"what do you want? [y/n]: what do you want? [y/n]: ",
221
factory.stdout.getvalue())