149
194
# stdin should be empty
150
195
self.assertEqual('', factory.stdin.readline())
152
def test_text_ui_get_integer(self):
153
stdin = tests.StringIOWrapper(
156
"hmmm\nwhat else ?\nCome on\nok 42\n4.24\n42\n")
157
stdout = tests.StringIOWrapper()
158
stderr = tests.StringIOWrapper()
159
factory = _mod_ui_text.TextUIFactory(stdin, stdout, stderr)
160
self.assertEqual(1, factory.get_integer(""))
161
self.assertEqual(-2, factory.get_integer(""))
162
self.assertEqual(42, factory.get_integer(""))
197
def test_silent_ui_getbool(self):
198
factory = _mod_ui.SilentUIFactory()
199
self.assert_get_bool_acceptance_of_user_input(factory)
201
def test_silent_factory_prompts_silently(self):
202
factory = _mod_ui.SilentUIFactory()
204
factory.stdin = StringIO("y\n")
205
self.assertEqual(True,
206
self.apply_redirected(None, stdout, stdout,
207
factory.get_boolean, "foo"))
208
self.assertEqual("", stdout.getvalue())
209
# stdin should be empty
210
self.assertEqual('', factory.stdin.readline())
212
def test_text_ui_getbool(self):
213
factory = TextUIFactory(None, None, None)
214
self.assert_get_bool_acceptance_of_user_input(factory)
164
216
def test_text_factory_prompt(self):
165
217
# see <https://launchpad.net/bugs/365891>
166
StringIO = tests.StringIOWrapper
167
factory = _mod_ui_text.TextUIFactory(StringIO(), StringIO(), StringIO())
218
factory = TextUIFactory(None, StringIO(), StringIO(), StringIO())
168
219
factory.prompt('foo %2e')
169
220
self.assertEqual('', factory.stdout.getvalue())
170
221
self.assertEqual('foo %2e', factory.stderr.getvalue())
172
223
def test_text_factory_prompts_and_clears(self):
173
224
# a get_boolean call should clear the pb before prompting
174
out = test_progress._TTYStringIO()
175
os.environ['TERM'] = 'xterm'
176
factory = _mod_ui_text.TextUIFactory(
177
stdin=tests.StringIOWrapper("yada\ny\n"),
178
stdout=out, stderr=out)
226
factory = TextUIFactory(stdin=StringIO("yada\ny\n"),
227
stdout=out, stderr=out)
179
228
pb = factory.nested_progress_bar()
180
229
pb.show_bar = False
181
230
pb.show_spinner = False
241
def test_quietness(self):
242
os.environ['BZR_PROGRESS_BAR'] = 'text'
243
ui_factory = _mod_ui_text.TextUIFactory(None,
244
test_progress._TTYStringIO(),
245
test_progress._TTYStringIO())
246
self.assertIsInstance(ui_factory._progress_view,
247
_mod_ui_text.TextProgressView)
248
ui_factory.be_quiet(True)
249
self.assertIsInstance(ui_factory._progress_view,
250
_mod_ui_text.NullProgressView)
252
def test_text_ui_show_user_warning(self):
253
from bzrlib.repofmt.groupcompress_repo import RepositoryFormat2a
254
from bzrlib.repofmt.pack_repo import RepositoryFormatKnitPack5
257
ui = tests.TextUIFactory(stdin=None, stdout=out, stderr=err)
258
remote_fmt = remote.RemoteRepositoryFormat()
259
remote_fmt._network_name = RepositoryFormatKnitPack5().network_name()
260
ui.show_user_warning('cross_format_fetch', from_format=RepositoryFormat2a(),
261
to_format=remote_fmt)
262
self.assertEquals('', out.getvalue())
263
self.assertEquals("Doing on-the-fly conversion from RepositoryFormat2a() to "
264
"RemoteRepositoryFormat(_network_name='Bazaar RepositoryFormatKnitPack5 "
265
"(bzr 1.6)\\n').\nThis may take some time. Upgrade the repositories to "
266
"the same format for better performance.\n",
268
# and now with it suppressed please
271
ui = tests.TextUIFactory(stdin=None, stdout=out, stderr=err)
272
ui.suppressed_warnings.add('cross_format_fetch')
273
ui.show_user_warning('cross_format_fetch', from_format=RepositoryFormat2a(),
274
to_format=remote_fmt)
275
self.assertEquals('', out.getvalue())
276
self.assertEquals('', err.getvalue())
279
class TestTextUIOutputStream(tests.TestCase):
280
"""Tests for output stream that synchronizes with progress bar."""
282
def test_output_clears_terminal(self):
283
stdout = tests.StringIOWrapper()
284
stderr = tests.StringIOWrapper()
287
uif = _mod_ui_text.TextUIFactory(None, stdout, stderr)
288
uif.clear_term = lambda: clear_calls.append('clear')
290
stream = _mod_ui_text.TextUIOutputStream(uif, uif.stdout)
291
stream.write("Hello world!\n")
292
stream.write("there's more...\n")
293
stream.writelines(["1\n", "2\n", "3\n"])
295
self.assertEqual(stdout.getvalue(),
299
self.assertEqual(['clear', 'clear', 'clear'],
305
class UITests(tests.TestCase):
307
def test_progress_construction(self):
308
"""TextUIFactory constructs the right progress view.
310
TTYStringIO = test_progress._TTYStringIO
311
FileStringIO = tests.StringIOWrapper
312
for (file_class, term, pb, expected_pb_class) in (
313
# on an xterm, either use them or not as the user requests,
314
# otherwise default on
315
(TTYStringIO, 'xterm', 'none', _mod_ui_text.NullProgressView),
316
(TTYStringIO, 'xterm', 'text', _mod_ui_text.TextProgressView),
317
(TTYStringIO, 'xterm', None, _mod_ui_text.TextProgressView),
318
# on a dumb terminal, again if there's explicit configuration do
319
# it, otherwise default off
320
(TTYStringIO, 'dumb', 'none', _mod_ui_text.NullProgressView),
321
(TTYStringIO, 'dumb', 'text', _mod_ui_text.TextProgressView),
322
(TTYStringIO, 'dumb', None, _mod_ui_text.NullProgressView),
323
# on a non-tty terminal, it's null regardless of $TERM
324
(FileStringIO, 'xterm', None, _mod_ui_text.NullProgressView),
325
(FileStringIO, 'dumb', None, _mod_ui_text.NullProgressView),
326
# however, it can still be forced on
327
(FileStringIO, 'dumb', 'text', _mod_ui_text.TextProgressView),
329
os.environ['TERM'] = term
331
if 'BZR_PROGRESS_BAR' in os.environ:
332
del os.environ['BZR_PROGRESS_BAR']
334
os.environ['BZR_PROGRESS_BAR'] = pb
335
stdin = file_class('')
336
stderr = file_class()
337
stdout = file_class()
338
uif = _mod_ui.make_ui_for_terminal(stdin, stdout, stderr)
339
self.assertIsInstance(uif, _mod_ui_text.TextUIFactory,
340
"TERM=%s BZR_PROGRESS_BAR=%s uif=%r" % (term, pb, uif,))
341
self.assertIsInstance(uif.make_progress_view(),
343
"TERM=%s BZR_PROGRESS_BAR=%s uif=%r" % (term, pb, uif,))
345
def test_text_ui_non_terminal(self):
346
"""Even on non-ttys, make_ui_for_terminal gives a text ui."""
347
stdin = test_progress._NonTTYStringIO('')
348
stderr = test_progress._NonTTYStringIO()
349
stdout = test_progress._NonTTYStringIO()
350
for term_type in ['dumb', None, 'xterm']:
351
if term_type is None:
352
del os.environ['TERM']
354
os.environ['TERM'] = term_type
355
uif = _mod_ui.make_ui_for_terminal(stdin, stdout, stderr)
356
self.assertIsInstance(uif, _mod_ui_text.TextUIFactory,
357
'TERM=%r' % (term_type,))
360
class SilentUITests(tests.TestCase):
362
def test_silent_factory_get_password(self):
363
# A silent factory that can't do user interaction can't get a
364
# password. Possibly it should raise a more specific error but it
366
ui = _mod_ui.SilentUIFactory()
367
stdout = tests.StringIOWrapper()
370
self.apply_redirected,
371
None, stdout, stdout, ui.get_password)
372
# and it didn't write anything out either
373
self.assertEqual('', stdout.getvalue())
375
def test_silent_ui_getbool(self):
376
factory = _mod_ui.SilentUIFactory()
377
stdout = tests.StringIOWrapper()
380
self.apply_redirected,
381
None, stdout, stdout, factory.get_boolean, "foo")
384
class TestUIFactoryTests(tests.TestCase):
386
def test_test_ui_factory_progress(self):
387
# there's no output; we just want to make sure this doesn't crash -
388
# see https://bugs.edge.launchpad.net/bzr/+bug/408201
389
ui = tests.TestUIFactory()
390
pb = ui.nested_progress_bar()
396
class CannedInputUIFactoryTests(tests.TestCase):
398
def test_canned_input_get_input(self):
399
uif = _mod_ui.CannedInputUIFactory([True, 'mbp', 'password', 42])
400
self.assertEqual(True, uif.get_boolean('Extra cheese?'))
401
self.assertEqual('mbp', uif.get_username('Enter your user name'))
402
self.assertEqual('password',
403
uif.get_password('Password for %(host)s',
405
self.assertEqual(42, uif.get_integer('And all that jazz ?'))
301
class TestTextProgressView(tests.TestCase):
302
"""Tests for text display of progress bars.
304
# XXX: These might be a bit easier to write if the rendering and
305
# state-maintaining parts of TextProgressView were more separate, and if
306
# the progress task called back directly to its own view not to the ui
307
# factory. -- mbp 20090312
309
def _make_factory(self):
311
uif = TextUIFactory(stderr=out)
312
uif._progress_view._width = 80
315
def test_render_progress_easy(self):
316
"""Just one task and one quarter done"""
317
out, uif = self._make_factory()
318
task = uif.nested_progress_bar()
319
task.update('reticulating splines', 5, 20)
321
'\r[####/ ] reticulating splines 5/20 \r'
324
def test_render_progress_nested(self):
325
"""Tasks proportionally contribute to overall progress"""
326
out, uif = self._make_factory()
327
task = uif.nested_progress_bar()
328
task.update('reticulating splines', 0, 2)
329
task2 = uif.nested_progress_bar()
330
task2.update('stage2', 1, 2)
331
# so we're in the first half of the main task, and half way through
334
r'[####\ ] reticulating splines:stage2 1/2'
335
, uif._progress_view._render_line())
336
# if the nested task is complete, then we're all the way through the
337
# first half of the overall work
338
task2.update('stage2', 2, 2)
340
r'[#########| ] reticulating splines:stage2 2/2'
341
, uif._progress_view._render_line())
343
def test_render_progress_sub_nested(self):
344
"""Intermediate tasks don't mess up calculation."""
345
out, uif = self._make_factory()
346
task_a = uif.nested_progress_bar()
347
task_a.update('a', 0, 2)
348
task_b = uif.nested_progress_bar()
350
task_c = uif.nested_progress_bar()
351
task_c.update('c', 1, 2)
352
# the top-level task is in its first half; the middle one has no
353
# progress indication, just a label; and the bottom one is half done,
354
# so the overall fraction is 1/4
356
r'[####| ] a:b:c 1/2'
357
, uif._progress_view._render_line())
408
360
class TestBoolFromString(tests.TestCase):