106
123
def test_progress_note_clears(self):
107
stderr = test_progress._TTYStringIO()
108
stdout = test_progress._TTYStringIO()
124
stderr = _TTYStringIO()
125
stdout = _TTYStringIO()
109
126
# so that we get a TextProgressBar
110
127
os.environ['TERM'] = 'xterm'
111
ui_factory = _mod_ui_text.TextUIFactory(
112
stdin=tests.StringIOWrapper(''),
128
ui_factory = TextUIFactory(
113
130
stdout=stdout, stderr=stderr)
114
131
self.assertIsInstance(ui_factory._progress_view,
115
_mod_ui_text.TextProgressView)
116
133
pb = ui_factory.nested_progress_bar()
118
135
# Create a progress update that isn't throttled
148
def test_progress_nested(self):
149
# test factory based nested and popping.
150
ui = TextUIFactory(None, None, None)
151
pb1 = ui.nested_progress_bar()
152
pb2 = ui.nested_progress_bar()
153
# You do get a warning if the outermost progress bar wasn't finished
154
# first - it's not clear if this is really useful or if it should just
155
# become orphaned -- mbp 20090120
156
warnings, _ = self.callCatchWarnings(pb1.finished)
157
if len(warnings) != 1:
158
self.fail("unexpected warnings: %r" % (warnings,))
131
162
def test_text_ui_get_boolean(self):
132
stdin = tests.StringIOWrapper("y\n" # True
134
"yes with garbage\nY\n" # True
135
"not an answer\nno\n" # False
136
"I'm sure!\nyes\n" # True
139
stdout = tests.StringIOWrapper()
140
stderr = tests.StringIOWrapper()
141
factory = _mod_ui_text.TextUIFactory(stdin, stdout, stderr)
163
stdin = StringIO("y\n" # True
165
"yes with garbage\nY\n" # True
166
"not an answer\nno\n" # False
167
"I'm sure!\nyes\n" # True
172
factory = TextUIFactory(stdin, stdout, stderr)
142
173
self.assertEqual(True, factory.get_boolean(""))
143
174
self.assertEqual(False, factory.get_boolean(""))
144
175
self.assertEqual(True, factory.get_boolean(""))
149
180
# stdin should be empty
150
181
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(""))
164
183
def test_text_factory_prompt(self):
165
184
# see <https://launchpad.net/bugs/365891>
166
StringIO = tests.StringIOWrapper
167
factory = _mod_ui_text.TextUIFactory(StringIO(), StringIO(), StringIO())
185
factory = TextUIFactory(StringIO(), StringIO(), StringIO())
168
186
factory.prompt('foo %2e')
169
187
self.assertEqual('', factory.stdout.getvalue())
170
188
self.assertEqual('foo %2e', factory.stderr.getvalue())
172
190
def test_text_factory_prompts_and_clears(self):
173
191
# a get_boolean call should clear the pb before prompting
174
out = test_progress._TTYStringIO()
175
193
os.environ['TERM'] = 'xterm'
176
factory = _mod_ui_text.TextUIFactory(
177
stdin=tests.StringIOWrapper("yada\ny\n"),
178
stdout=out, stderr=out)
194
factory = TextUIFactory(stdin=StringIO("yada\ny\n"), stdout=out, stderr=out)
179
195
pb = factory.nested_progress_bar()
180
196
pb.show_bar = False
181
197
pb.show_spinner = False
209
224
def test_text_ui_getusername(self):
210
factory = _mod_ui_text.TextUIFactory(None, None, None)
211
factory.stdin = tests.StringIOWrapper("someuser\n\n")
212
factory.stdout = tests.StringIOWrapper()
213
factory.stderr = tests.StringIOWrapper()
225
factory = TextUIFactory(None, None, None)
226
factory.stdin = StringIO("someuser\n\n")
227
factory.stdout = StringIO()
228
factory.stderr = StringIO()
214
229
factory.stdout.encoding = "utf8"
215
230
# there is no output from the base factory
216
231
self.assertEqual("someuser",
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):
257
class TestTextUIOutputStream(TestCase):
280
258
"""Tests for output stream that synchronizes with progress bar."""
282
260
def test_output_clears_terminal(self):
283
stdout = tests.StringIOWrapper()
284
stderr = tests.StringIOWrapper()
287
uif = _mod_ui_text.TextUIFactory(None, stdout, stderr)
265
uif = TextUIFactory(None, stdout, stderr)
288
266
uif.clear_term = lambda: clear_calls.append('clear')
290
stream = _mod_ui_text.TextUIOutputStream(uif, uif.stdout)
268
stream = TextUIOutputStream(uif, uif.stdout)
291
269
stream.write("Hello world!\n")
292
270
stream.write("there's more...\n")
293
271
stream.writelines(["1\n", "2\n", "3\n"])
295
273
self.assertEqual(stdout.getvalue(),
297
275
"there's more...\n"
307
285
def test_progress_construction(self):
308
286
"""TextUIFactory constructs the right progress view.
310
TTYStringIO = test_progress._TTYStringIO
311
FileStringIO = tests.StringIOWrapper
312
288
for (file_class, term, pb, expected_pb_class) in (
313
289
# on an xterm, either use them or not as the user requests,
314
290
# 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),
291
(_TTYStringIO, 'xterm', 'none', NullProgressView),
292
(_TTYStringIO, 'xterm', 'text', TextProgressView),
293
(_TTYStringIO, 'xterm', None, TextProgressView),
318
294
# on a dumb terminal, again if there's explicit configuration do
319
295
# 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),
296
(_TTYStringIO, 'dumb', 'none', NullProgressView),
297
(_TTYStringIO, 'dumb', 'text', TextProgressView),
298
(_TTYStringIO, 'dumb', None, NullProgressView),
323
299
# 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),
300
(StringIO, 'xterm', None, NullProgressView),
301
(StringIO, 'dumb', None, NullProgressView),
326
302
# however, it can still be forced on
327
(FileStringIO, 'dumb', 'text', _mod_ui_text.TextProgressView),
303
(StringIO, 'dumb', 'text', TextProgressView),
329
305
os.environ['TERM'] = term
335
311
stdin = file_class('')
336
312
stderr = file_class()
337
313
stdout = file_class()
338
uif = _mod_ui.make_ui_for_terminal(stdin, stdout, stderr)
339
self.assertIsInstance(uif, _mod_ui_text.TextUIFactory,
314
uif = make_ui_for_terminal(stdin, stdout, stderr)
315
self.assertIsInstance(uif, TextUIFactory,
340
316
"TERM=%s BZR_PROGRESS_BAR=%s uif=%r" % (term, pb, uif,))
341
317
self.assertIsInstance(uif.make_progress_view(),
342
318
expected_pb_class,
345
321
def test_text_ui_non_terminal(self):
346
322
"""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()
323
stdin = _NonTTYStringIO('')
324
stderr = _NonTTYStringIO()
325
stdout = _NonTTYStringIO()
350
326
for term_type in ['dumb', None, 'xterm']:
351
327
if term_type is None:
352
328
del os.environ['TERM']
354
330
os.environ['TERM'] = term_type
355
uif = _mod_ui.make_ui_for_terminal(stdin, stdout, stderr)
356
self.assertIsInstance(uif, _mod_ui_text.TextUIFactory,
331
uif = make_ui_for_terminal(stdin, stdout, stderr)
332
self.assertIsInstance(uif, TextUIFactory,
357
333
'TERM=%r' % (term_type,))
360
class SilentUITests(tests.TestCase):
336
class SilentUITests(TestCase):
362
338
def test_silent_factory_get_password(self):
363
339
# A silent factory that can't do user interaction can't get a
364
340
# password. Possibly it should raise a more specific error but it
366
ui = _mod_ui.SilentUIFactory()
367
stdout = tests.StringIOWrapper()
342
ui = SilentUIFactory()
368
344
self.assertRaises(
369
345
NotImplementedError,
370
346
self.apply_redirected,
373
349
self.assertEqual('', stdout.getvalue())
375
351
def test_silent_ui_getbool(self):
376
factory = _mod_ui.SilentUIFactory()
377
stdout = tests.StringIOWrapper()
352
factory = SilentUIFactory()
378
354
self.assertRaises(
379
355
NotImplementedError,
380
356
self.apply_redirected,
381
357
None, stdout, stdout, factory.get_boolean, "foo")
384
class TestUIFactoryTests(tests.TestCase):
360
class TestUIFactoryTests(TestCase):
386
362
def test_test_ui_factory_progress(self):
387
363
# there's no output; we just want to make sure this doesn't crash -
388
364
# see https://bugs.edge.launchpad.net/bzr/+bug/408201
389
ui = tests.TestUIFactory()
390
366
pb = ui.nested_progress_bar()
391
367
pb.update('hello')
396
class CannedInputUIFactoryTests(tests.TestCase):
372
class CannedInputUIFactoryTests(TestCase):
398
374
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 ?'))
375
uif = CannedInputUIFactory([True, 'mbp', 'password'])
376
self.assertEqual(uif.get_boolean('Extra cheese?'), True)
377
self.assertEqual(uif.get_username('Enter your user name'), 'mbp')
378
self.assertEqual(uif.get_password('Password for %(host)s', host='example.com'),
408
382
class TestBoolFromString(tests.TestCase):