161
141
def test_text_ui_get_boolean(self):
162
stdin = StringIO("y\n" # True
164
"yes with garbage\nY\n" # True
165
"not an answer\nno\n" # False
166
"I'm sure!\nyes\n" # True
171
factory = TextUIFactory(stdin, stdout, stderr)
142
stdin = tests.StringIOWrapper("y\n" # True
144
"yes with garbage\nY\n" # True
145
"not an answer\nno\n" # False
146
"I'm sure!\nyes\n" # True
149
stdout = tests.StringIOWrapper()
150
stderr = tests.StringIOWrapper()
151
factory = _mod_ui_text.TextUIFactory(stdin, stdout, stderr)
172
152
self.assertEqual(True, factory.get_boolean(""))
173
153
self.assertEqual(False, factory.get_boolean(""))
174
154
self.assertEqual(True, factory.get_boolean(""))
179
159
# stdin should be empty
180
160
self.assertEqual('', factory.stdin.readline())
162
def test_text_ui_get_integer(self):
163
stdin = tests.StringIOWrapper(
166
"hmmm\nwhat else ?\nCome on\nok 42\n4.24\n42\n")
167
stdout = tests.StringIOWrapper()
168
stderr = tests.StringIOWrapper()
169
factory = _mod_ui_text.TextUIFactory(stdin, stdout, stderr)
170
self.assertEqual(1, factory.get_integer(""))
171
self.assertEqual(-2, factory.get_integer(""))
172
self.assertEqual(42, factory.get_integer(""))
182
174
def test_text_factory_prompt(self):
183
175
# see <https://launchpad.net/bugs/365891>
184
factory = TextUIFactory(StringIO(), StringIO(), StringIO())
176
StringIO = tests.StringIOWrapper
177
factory = _mod_ui_text.TextUIFactory(StringIO(), StringIO(), StringIO())
185
178
factory.prompt('foo %2e')
186
179
self.assertEqual('', factory.stdout.getvalue())
187
180
self.assertEqual('foo %2e', factory.stderr.getvalue())
189
182
def test_text_factory_prompts_and_clears(self):
190
183
# a get_boolean call should clear the pb before prompting
184
out = test_progress._TTYStringIO()
192
185
os.environ['TERM'] = 'xterm'
193
factory = TextUIFactory(stdin=StringIO("yada\ny\n"), stdout=out, stderr=out)
186
factory = _mod_ui_text.TextUIFactory(
187
stdin=tests.StringIOWrapper("yada\ny\n"),
188
stdout=out, stderr=out)
194
189
pb = factory.nested_progress_bar()
195
190
pb.show_bar = False
196
191
pb.show_spinner = False
251
def test_quietness(self):
252
os.environ['BZR_PROGRESS_BAR'] = 'text'
253
ui_factory = _mod_ui_text.TextUIFactory(None,
254
test_progress._TTYStringIO(),
255
test_progress._TTYStringIO())
256
self.assertIsInstance(ui_factory._progress_view,
257
_mod_ui_text.TextProgressView)
258
ui_factory.be_quiet(True)
259
self.assertIsInstance(ui_factory._progress_view,
260
_mod_ui_text.NullProgressView)
263
class TestTextUIOutputStream(tests.TestCase):
264
"""Tests for output stream that synchronizes with progress bar."""
266
def test_output_clears_terminal(self):
267
stdout = tests.StringIOWrapper()
268
stderr = tests.StringIOWrapper()
271
uif = _mod_ui_text.TextUIFactory(None, stdout, stderr)
272
uif.clear_term = lambda: clear_calls.append('clear')
274
stream = _mod_ui_text.TextUIOutputStream(uif, uif.stdout)
275
stream.write("Hello world!\n")
276
stream.write("there's more...\n")
277
stream.writelines(["1\n", "2\n", "3\n"])
279
self.assertEqual(stdout.getvalue(),
283
self.assertEqual(['clear', 'clear', 'clear'],
256
289
class UITests(tests.TestCase):
258
291
def test_progress_construction(self):
259
292
"""TextUIFactory constructs the right progress view.
294
TTYStringIO = test_progress._TTYStringIO
295
FileStringIO = tests.StringIOWrapper
261
296
for (file_class, term, pb, expected_pb_class) in (
262
297
# on an xterm, either use them or not as the user requests,
263
298
# otherwise default on
264
(_TTYStringIO, 'xterm', 'none', NullProgressView),
265
(_TTYStringIO, 'xterm', 'text', TextProgressView),
266
(_TTYStringIO, 'xterm', None, TextProgressView),
299
(TTYStringIO, 'xterm', 'none', _mod_ui_text.NullProgressView),
300
(TTYStringIO, 'xterm', 'text', _mod_ui_text.TextProgressView),
301
(TTYStringIO, 'xterm', None, _mod_ui_text.TextProgressView),
267
302
# on a dumb terminal, again if there's explicit configuration do
268
303
# it, otherwise default off
269
(_TTYStringIO, 'dumb', 'none', NullProgressView),
270
(_TTYStringIO, 'dumb', 'text', TextProgressView),
271
(_TTYStringIO, 'dumb', None, NullProgressView),
304
(TTYStringIO, 'dumb', 'none', _mod_ui_text.NullProgressView),
305
(TTYStringIO, 'dumb', 'text', _mod_ui_text.TextProgressView),
306
(TTYStringIO, 'dumb', None, _mod_ui_text.NullProgressView),
272
307
# on a non-tty terminal, it's null regardless of $TERM
273
(StringIO, 'xterm', None, NullProgressView),
274
(StringIO, 'dumb', None, NullProgressView),
308
(FileStringIO, 'xterm', None, _mod_ui_text.NullProgressView),
309
(FileStringIO, 'dumb', None, _mod_ui_text.NullProgressView),
275
310
# however, it can still be forced on
276
(StringIO, 'dumb', 'text', TextProgressView),
311
(FileStringIO, 'dumb', 'text', _mod_ui_text.TextProgressView),
278
313
os.environ['TERM'] = term
294
329
def test_text_ui_non_terminal(self):
295
330
"""Even on non-ttys, make_ui_for_terminal gives a text ui."""
296
stdin = _NonTTYStringIO('')
297
stderr = _NonTTYStringIO()
298
stdout = _NonTTYStringIO()
331
stdin = test_progress._NonTTYStringIO('')
332
stderr = test_progress._NonTTYStringIO()
333
stdout = test_progress._NonTTYStringIO()
299
334
for term_type in ['dumb', None, 'xterm']:
300
335
if term_type is None:
301
336
del os.environ['TERM']
303
338
os.environ['TERM'] = term_type
304
uif = make_ui_for_terminal(stdin, stdout, stderr)
305
self.assertIsInstance(uif, TextUIFactory,
339
uif = _mod_ui.make_ui_for_terminal(stdin, stdout, stderr)
340
self.assertIsInstance(uif, _mod_ui_text.TextUIFactory,
306
341
'TERM=%r' % (term_type,))
309
class SilentUITests(TestCase):
344
class SilentUITests(tests.TestCase):
311
346
def test_silent_factory_get_password(self):
312
347
# A silent factory that can't do user interaction can't get a
313
348
# password. Possibly it should raise a more specific error but it
315
ui = SilentUIFactory()
350
ui = _mod_ui.SilentUIFactory()
351
stdout = tests.StringIOWrapper()
317
352
self.assertRaises(
318
353
NotImplementedError,
319
354
self.apply_redirected,
322
357
self.assertEqual('', stdout.getvalue())
324
359
def test_silent_ui_getbool(self):
325
factory = SilentUIFactory()
360
factory = _mod_ui.SilentUIFactory()
361
stdout = tests.StringIOWrapper()
327
362
self.assertRaises(
328
363
NotImplementedError,
329
364
self.apply_redirected,
330
365
None, stdout, stdout, factory.get_boolean, "foo")
333
class TestUIFactoryTests(TestCase):
368
class TestUIFactoryTests(tests.TestCase):
335
370
def test_test_ui_factory_progress(self):
336
371
# there's no output; we just want to make sure this doesn't crash -
337
372
# see https://bugs.edge.launchpad.net/bzr/+bug/408201
373
ui = tests.TestUIFactory()
339
374
pb = ui.nested_progress_bar()
340
375
pb.update('hello')
345
class CannedInputUIFactoryTests(TestCase):
380
class CannedInputUIFactoryTests(tests.TestCase):
347
382
def test_canned_input_get_input(self):
348
uif = CannedInputUIFactory([True, 'mbp', 'password'])
349
self.assertEqual(uif.get_boolean('Extra cheese?'), True)
350
self.assertEqual(uif.get_username('Enter your user name'), 'mbp')
351
self.assertEqual(uif.get_password('Password for %(host)s', host='example.com'),
383
uif = _mod_ui.CannedInputUIFactory([True, 'mbp', 'password', 42])
384
self.assertEqual(True, uif.get_boolean('Extra cheese?'))
385
self.assertEqual('mbp', uif.get_username('Enter your user name'))
386
self.assertEqual('password',
387
uif.get_password('Password for %(host)s',
389
self.assertEqual(42, uif.get_integer('And all that jazz ?'))
355
392
class TestBoolFromString(tests.TestCase):