47
34
def test_silent_factory(self):
48
35
ui = SilentUIFactory()
50
self.assertEqual(None,
51
self.apply_redirected(None, stdout, stdout,
53
self.assertEqual('', stdout.getvalue())
54
self.assertEqual(None,
55
self.apply_redirected(None, stdout, stdout,
57
u'Hello\u1234 %(user)s',
59
self.assertEqual('', stdout.getvalue())
61
def test_text_factory_ascii_password(self):
62
ui = TestUIFactory(stdin='secret\n', stdout=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.stdout.getvalue())
71
# stdin should be empty
72
self.assertEqual('', ui.stdin.readline())
76
def test_text_factory_utf8_password(self):
77
"""Test an utf8 password.
79
We can't predict what encoding users will have for stdin, so we force
80
it to utf8 to test that we transport the password correctly.
82
ui = TestUIFactory(stdin=u'baz\u1234'.encode('utf8'),
83
stdout=StringIOWrapper())
84
ui.stdin.encoding = 'utf8'
85
ui.stdout.encoding = ui.stdin.encoding
86
pb = ui.nested_progress_bar()
88
password = self.apply_redirected(ui.stdin, ui.stdout, ui.stdout,
90
u'Hello \u1234 %(user)s',
92
# We use StringIO objects, we need to decode them
93
self.assertEqual(u'baz\u1234', password.decode('utf8'))
94
self.assertEqual(u'Hello \u1234 some\u1234: ',
95
ui.stdout.getvalue().decode('utf8'))
96
# stdin should be empty
97
self.assertEqual('', ui.stdin.readline())
36
pb = ui.nested_progress_bar()
38
# TODO: Test that there is no output from SilentUIFactory
40
self.assertEquals(ui.get_password(), None)
41
self.assertEquals(ui.get_password(u'Hello There \u1234 %(user)s',
47
def test_text_factory(self):
49
pb = ui.nested_progress_bar()
51
# TODO: Test the output from TextUIFactory, perhaps by overriding sys.stdout
53
# Unfortunately we can't actually test the ui.get_password() because
54
# that would actually prompt the user for a password during the test suite
55
# This has been tested manually with both LANG=en_US.utf-8 and LANG=C
57
# self.assertEquals(ui.get_password(u"%(user)s please type 'bogus'",
101
62
def test_progress_note(self):
102
63
stderr = StringIO()
103
64
stdout = StringIO()
104
ui_factory = TextUIFactory(bar_type=TTYProgressBar)
105
pb = ui_factory.nested_progress_bar()
107
pb.to_messages_file = stdout
108
ui_factory._progress_bar_stack.bottom().to_file = stderr
109
result = pb.note('t')
110
self.assertEqual(None, result)
111
self.assertEqual("t\n", stdout.getvalue())
112
# Since there was no update() call, there should be no clear() call
113
self.failIf(re.search(r'^\r {10,}\r$',
114
stderr.getvalue()) is not None,
115
'We cleared the stderr without anything to put there')
119
def test_progress_note_clears(self):
122
# The PQM redirects the output to a file, so it
123
# defaults to creating a Dots progress bar. we
124
# need to force it to believe we are a TTY
125
ui_factory = TextUIFactory(bar_type=TTYProgressBar)
126
pb = ui_factory.nested_progress_bar()
128
pb.to_messages_file = stdout
129
ui_factory._progress_bar_stack.bottom().to_file = stderr
130
# Create a progress update that isn't throttled
65
ui_factory = TextUIFactory()
66
pb = ui_factory.nested_progress_bar()
68
pb.to_messages_file = stdout
69
ui_factory._progress_bar_stack.bottom().to_file = stderr
133
70
result = pb.note('t')
134
71
self.assertEqual(None, result)
135
72
self.assertEqual("t\n", stdout.getvalue())
136
73
# the exact contents will depend on the terminal width and we don't
137
74
# care about that right now - but you're probably running it on at
138
75
# least a 10-character wide terminal :)
139
self.assertContainsRe(stderr.getvalue(), r'\r {10,}\r$')
76
self.assertContainsRe(stderr.getvalue(), r'^\r {10,}\r$')
174
111
def test_text_factory_setting_progress_bar(self):
175
112
# we should be able to choose the progress bar type used.
176
factory = TextUIFactory(bar_type=DotsProgressBar)
113
factory = bzrlib.ui.text.TextUIFactory(
114
bar_type=bzrlib.progress.DotsProgressBar)
177
115
bar = factory.nested_progress_bar()
179
self.assertIsInstance(bar, DotsProgressBar)
117
self.assertIsInstance(bar, bzrlib.progress.DotsProgressBar)
181
119
def test_cli_stdin_is_default_stdin(self):
182
factory = CLIUIFactory()
120
factory = bzrlib.ui.CLIUIFactory()
183
121
self.assertEqual(sys.stdin, factory.stdin)
185
123
def assert_get_bool_acceptance_of_user_input(self, factory):
186
factory.stdin = StringIO("y\nyes with garbage\n"
187
"yes\nn\nnot an answer\n"
124
factory.stdin = StringIO("y\nyes with garbage\nyes\nn\nnot an answer\nno\nfoo\n")
189
125
factory.stdout = StringIO()
190
126
# there is no output from the base factory
191
127
self.assertEqual(True, factory.get_boolean(""))
193
129
self.assertEqual(False, factory.get_boolean(""))
194
130
self.assertEqual(False, factory.get_boolean(""))
195
131
self.assertEqual("foo\n", factory.stdin.read())
196
# stdin should be empty
197
self.assertEqual('', factory.stdin.readline())
199
133
def test_silent_ui_getbool(self):
200
factory = SilentUIFactory()
134
factory = bzrlib.ui.SilentUIFactory()
201
135
self.assert_get_bool_acceptance_of_user_input(factory)
203
137
def test_silent_factory_prompts_silently(self):
204
factory = SilentUIFactory()
138
factory = bzrlib.ui.SilentUIFactory()
205
139
stdout = StringIO()
206
140
factory.stdin = StringIO("y\n")
207
self.assertEqual(True,
208
self.apply_redirected(None, stdout, stdout,
209
factory.get_boolean, "foo"))
143
self.apply_redirected(
144
None, stdout, stdout, factory.get_boolean, "foo")
210
146
self.assertEqual("", stdout.getvalue())
211
# stdin should be empty
212
self.assertEqual('', factory.stdin.readline())
214
148
def test_text_ui_getbool(self):
215
factory = TextUIFactory()
149
factory = bzrlib.ui.text.TextUIFactory()
216
150
self.assert_get_bool_acceptance_of_user_input(factory)
218
152
def test_text_factory_prompts_and_clears(self):
219
153
# a get_boolean call should clear the pb before prompting
220
factory = TextUIFactory(bar_type=DotsProgressBar)
221
factory.stdout = _TTYStringIO()
154
factory = bzrlib.ui.text.TextUIFactory()
155
factory.stdout = StringIO()
222
156
factory.stdin = StringIO("yada\ny\n")
223
pb = self.apply_redirected(factory.stdin, factory.stdout,
224
factory.stdout, factory.nested_progress_bar)
226
self.apply_redirected(factory.stdin, factory.stdout,
227
factory.stdout, pb.update, "foo", 0, 1)
228
self.assertEqual(True,
229
self.apply_redirected(None, factory.stdout,
233
output = factory.stdout.getvalue()
234
self.assertEqual("foo: .\n"
235
"what do you want? [y/n]: what do you want? [y/n]: ",
236
factory.stdout.getvalue())
237
# stdin should be empty
238
self.assertEqual('', factory.stdin.readline())
157
pb = self.apply_redirected(
158
factory.stdin, factory.stdout, factory.stdout, factory.nested_progress_bar)
159
self.apply_redirected(
160
factory.stdin, factory.stdout, factory.stdout, pb.update, "foo", 0, 1)
163
self.apply_redirected(
164
None, factory.stdout, factory.stdout, factory.get_boolean, "what do you want")
169
"\rwhat do you want? [y/n]:what do you want? [y/n]:", factory.stdout.getvalue())