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