102
122
def test_progress_note_clears(self):
103
stderr = test_progress._TTYStringIO()
104
stdout = test_progress._TTYStringIO()
123
stderr = _TTYStringIO()
124
stdout = _TTYStringIO()
105
125
# so that we get a TextProgressBar
106
126
os.environ['TERM'] = 'xterm'
107
ui_factory = _mod_ui_text.TextUIFactory(
108
stdin=tests.StringIOWrapper(''),
127
ui_factory = TextUIFactory(
109
129
stdout=stdout, stderr=stderr)
110
130
self.assertIsInstance(ui_factory._progress_view,
111
_mod_ui_text.TextProgressView)
112
132
pb = ui_factory.nested_progress_bar()
114
134
# Create a progress update that isn't throttled
141
161
def test_text_ui_get_boolean(self):
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)
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)
152
172
self.assertEqual(True, factory.get_boolean(""))
153
173
self.assertEqual(False, factory.get_boolean(""))
154
174
self.assertEqual(True, factory.get_boolean(""))
159
179
# stdin should be empty
160
180
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(""))
174
182
def test_text_factory_prompt(self):
175
183
# see <https://launchpad.net/bugs/365891>
176
StringIO = tests.StringIOWrapper
177
factory = _mod_ui_text.TextUIFactory(StringIO(), StringIO(), StringIO())
184
factory = TextUIFactory(StringIO(), StringIO(), StringIO())
178
185
factory.prompt('foo %2e')
179
186
self.assertEqual('', factory.stdout.getvalue())
180
187
self.assertEqual('foo %2e', factory.stderr.getvalue())
182
189
def test_text_factory_prompts_and_clears(self):
183
190
# a get_boolean call should clear the pb before prompting
184
out = test_progress._TTYStringIO()
185
192
os.environ['TERM'] = 'xterm'
186
factory = _mod_ui_text.TextUIFactory(
187
stdin=tests.StringIOWrapper("yada\ny\n"),
188
stdout=out, stderr=out)
193
factory = TextUIFactory(stdin=StringIO("yada\ny\n"), stdout=out, stderr=out)
189
194
pb = factory.nested_progress_bar()
190
195
pb.show_bar = False
191
196
pb.show_spinner = False
219
223
def test_text_ui_getusername(self):
220
factory = _mod_ui_text.TextUIFactory(None, None, None)
221
factory.stdin = tests.StringIOWrapper("someuser\n\n")
222
factory.stdout = tests.StringIOWrapper()
223
factory.stderr = tests.StringIOWrapper()
224
factory = TextUIFactory(None, None, None)
225
factory.stdin = StringIO("someuser\n\n")
226
factory.stdout = StringIO()
227
factory.stderr = StringIO()
224
228
factory.stdout.encoding = "utf8"
225
229
# there is no output from the base factory
226
230
self.assertEqual("someuser",
252
class TestTextUIOutputStream(tests.TestCase):
253
"""Tests for output stream that synchronizes with progress bar."""
255
def test_output_clears_terminal(self):
256
stdout = tests.StringIOWrapper()
257
stderr = tests.StringIOWrapper()
260
uif = _mod_ui_text.TextUIFactory(None, stdout, stderr)
261
uif.clear_term = lambda: clear_calls.append('clear')
263
stream = _mod_ui_text.TextUIOutputStream(uif, uif.stdout)
264
stream.write("Hello world!\n")
265
stream.write("there's more...\n")
266
stream.writelines(["1\n", "2\n", "3\n"])
268
self.assertEqual(stdout.getvalue(),
272
self.assertEqual(['clear', 'clear', 'clear'],
278
256
class UITests(tests.TestCase):
280
258
def test_progress_construction(self):
281
259
"""TextUIFactory constructs the right progress view.
283
TTYStringIO = test_progress._TTYStringIO
284
FileStringIO = tests.StringIOWrapper
285
261
for (file_class, term, pb, expected_pb_class) in (
286
262
# on an xterm, either use them or not as the user requests,
287
263
# otherwise default on
288
(TTYStringIO, 'xterm', 'none', _mod_ui_text.NullProgressView),
289
(TTYStringIO, 'xterm', 'text', _mod_ui_text.TextProgressView),
290
(TTYStringIO, 'xterm', None, _mod_ui_text.TextProgressView),
264
(_TTYStringIO, 'xterm', 'none', NullProgressView),
265
(_TTYStringIO, 'xterm', 'text', TextProgressView),
266
(_TTYStringIO, 'xterm', None, TextProgressView),
291
267
# on a dumb terminal, again if there's explicit configuration do
292
268
# it, otherwise default off
293
(TTYStringIO, 'dumb', 'none', _mod_ui_text.NullProgressView),
294
(TTYStringIO, 'dumb', 'text', _mod_ui_text.TextProgressView),
295
(TTYStringIO, 'dumb', None, _mod_ui_text.NullProgressView),
269
(_TTYStringIO, 'dumb', 'none', NullProgressView),
270
(_TTYStringIO, 'dumb', 'text', TextProgressView),
271
(_TTYStringIO, 'dumb', None, NullProgressView),
296
272
# on a non-tty terminal, it's null regardless of $TERM
297
(FileStringIO, 'xterm', None, _mod_ui_text.NullProgressView),
298
(FileStringIO, 'dumb', None, _mod_ui_text.NullProgressView),
273
(StringIO, 'xterm', None, NullProgressView),
274
(StringIO, 'dumb', None, NullProgressView),
299
275
# however, it can still be forced on
300
(FileStringIO, 'dumb', 'text', _mod_ui_text.TextProgressView),
276
(StringIO, 'dumb', 'text', TextProgressView),
302
278
os.environ['TERM'] = term
318
294
def test_text_ui_non_terminal(self):
319
295
"""Even on non-ttys, make_ui_for_terminal gives a text ui."""
320
stdin = test_progress._NonTTYStringIO('')
321
stderr = test_progress._NonTTYStringIO()
322
stdout = test_progress._NonTTYStringIO()
296
stdin = _NonTTYStringIO('')
297
stderr = _NonTTYStringIO()
298
stdout = _NonTTYStringIO()
323
299
for term_type in ['dumb', None, 'xterm']:
324
300
if term_type is None:
325
301
del os.environ['TERM']
327
303
os.environ['TERM'] = term_type
328
uif = _mod_ui.make_ui_for_terminal(stdin, stdout, stderr)
329
self.assertIsInstance(uif, _mod_ui_text.TextUIFactory,
304
uif = make_ui_for_terminal(stdin, stdout, stderr)
305
self.assertIsInstance(uif, TextUIFactory,
330
306
'TERM=%r' % (term_type,))
333
class SilentUITests(tests.TestCase):
309
class SilentUITests(TestCase):
335
311
def test_silent_factory_get_password(self):
336
312
# A silent factory that can't do user interaction can't get a
337
313
# password. Possibly it should raise a more specific error but it
339
ui = _mod_ui.SilentUIFactory()
340
stdout = tests.StringIOWrapper()
315
ui = SilentUIFactory()
341
317
self.assertRaises(
342
318
NotImplementedError,
343
319
self.apply_redirected,
346
322
self.assertEqual('', stdout.getvalue())
348
324
def test_silent_ui_getbool(self):
349
factory = _mod_ui.SilentUIFactory()
350
stdout = tests.StringIOWrapper()
325
factory = SilentUIFactory()
351
327
self.assertRaises(
352
328
NotImplementedError,
353
329
self.apply_redirected,
354
330
None, stdout, stdout, factory.get_boolean, "foo")
357
class TestUIFactoryTests(tests.TestCase):
333
class TestUIFactoryTests(TestCase):
359
335
def test_test_ui_factory_progress(self):
360
336
# there's no output; we just want to make sure this doesn't crash -
361
337
# see https://bugs.edge.launchpad.net/bzr/+bug/408201
362
ui = tests.TestUIFactory()
363
339
pb = ui.nested_progress_bar()
364
340
pb.update('hello')
369
class CannedInputUIFactoryTests(tests.TestCase):
345
class CannedInputUIFactoryTests(TestCase):
371
347
def test_canned_input_get_input(self):
372
uif = _mod_ui.CannedInputUIFactory([True, 'mbp', 'password', 42])
373
self.assertEqual(True, uif.get_boolean('Extra cheese?'))
374
self.assertEqual('mbp', uif.get_username('Enter your user name'))
375
self.assertEqual('password',
376
uif.get_password('Password for %(host)s',
378
self.assertEqual(42, uif.get_integer('And all that jazz ?'))
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'),
381
355
class TestBoolFromString(tests.TestCase):