1
# Copyright (C) 2005 Canonical Ltd
1
# Copyright (C) 2005, 2008 Canonical Ltd
3
3
# This program is free software; you can redistribute it and/or modify
4
4
# it under the terms of the GNU General Public License as published by
101
101
def test_progress_note(self):
102
102
stderr = StringIO()
103
103
stdout = StringIO()
104
ui_factory = TextUIFactory(bar_type=TTYProgressBar)
104
ui_factory = TextUIFactory(stdin=StringIO(''),
105
107
pb = ui_factory.nested_progress_bar()
107
pb.to_messages_file = stdout
108
ui_factory._progress_bar_stack.bottom().to_file = stderr
109
109
result = pb.note('t')
110
110
self.assertEqual(None, result)
111
111
self.assertEqual("t\n", stdout.getvalue())
122
122
# The PQM redirects the output to a file, so it
123
123
# defaults to creating a Dots progress bar. we
124
124
# need to force it to believe we are a TTY
125
ui_factory = TextUIFactory(bar_type=TTYProgressBar)
125
ui_factory = TextUIFactory(
127
stdout=stdout, stderr=stderr)
126
128
pb = ui_factory.nested_progress_bar()
128
pb.to_messages_file = stdout
129
ui_factory._progress_bar_stack.bottom().to_file = stderr
130
130
# Create a progress update that isn't throttled
132
131
pb.update('x', 1, 1)
133
132
result = pb.note('t')
134
133
self.assertEqual(None, result)
143
142
def test_progress_nested(self):
144
143
# test factory based nested and popping.
144
ui = TextUIFactory(None, None, None)
146
145
pb1 = ui.nested_progress_bar()
147
146
pb2 = ui.nested_progress_bar()
148
self.assertRaises(errors.MissingProgressBarFinish, pb1.finished)
147
# We no longer warn about finishing unnested progress bars.
148
warnings, _ = self.callCatchWarnings(pb1.finished)
149
self.assertEqual(len(warnings), 0)
160
161
self.assertFalse(getattr(stack, 'note', False))
161
162
pb1 = stack.get_nested()
162
163
pb2 = stack.get_nested()
163
self.assertRaises(errors.MissingProgressBarFinish, pb1.finished)
164
warnings, _ = self.callCatchWarnings(pb1.finished)
165
self.assertEqual(len(warnings), 1)
166
168
# the text ui factory never actually removes the stack once its setup.
167
169
# we need to be able to nest again correctly from here.
168
170
pb1 = stack.get_nested()
169
171
pb2 = stack.get_nested()
170
self.assertRaises(errors.MissingProgressBarFinish, pb1.finished)
172
warnings, _ = self.callCatchWarnings(pb1.finished)
173
self.assertEqual(len(warnings), 1)
174
def test_text_factory_setting_progress_bar(self):
175
# we should be able to choose the progress bar type used.
176
factory = TextUIFactory(bar_type=DotsProgressBar)
177
bar = factory.nested_progress_bar()
179
self.assertIsInstance(bar, DotsProgressBar)
181
def test_cli_stdin_is_default_stdin(self):
182
factory = CLIUIFactory()
183
self.assertEqual(sys.stdin, factory.stdin)
185
177
def assert_get_bool_acceptance_of_user_input(self, factory):
186
178
factory.stdin = StringIO("y\nyes with garbage\n"
187
179
"yes\nn\nnot an answer\n"
212
204
self.assertEqual('', factory.stdin.readline())
214
206
def test_text_ui_getbool(self):
215
factory = TextUIFactory()
207
factory = TextUIFactory(None, None, None)
216
208
self.assert_get_bool_acceptance_of_user_input(factory)
218
210
def test_text_factory_prompts_and_clears(self):
219
211
# a get_boolean call should clear the pb before prompting
220
factory = TextUIFactory(bar_type=DotsProgressBar)
221
factory.stdout = _TTYStringIO()
222
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)
213
factory = TextUIFactory(stdin=StringIO("yada\ny\n"), stdout=out, stderr=out)
214
pb = factory.nested_progress_bar()
216
pb.show_spinner = False
217
pb.show_count = False
218
pb.update("foo", 0, 1)
228
219
self.assertEqual(True,
229
220
self.apply_redirected(None, factory.stdout,
231
222
factory.get_boolean,
232
223
"what do you want"))
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
224
output = out.getvalue()
225
self.assertContainsRe(factory.stdout.getvalue(),
227
self.assertContainsRe(factory.stdout.getvalue(),
228
r"what do you want\? \[y/n\]: what do you want\? \[y/n\]: ")
229
# stdin should have been totally consumed
238
230
self.assertEqual('', factory.stdin.readline())