/brz/remove-bazaar

To get this branch, use:
bzr branch http://gegoxaren.bato24.eu/bzr/brz/remove-bazaar

« back to all changes in this revision

Viewing changes to breezy/tests/test_ui.py

  • Committer: Jelmer Vernooij
  • Date: 2019-05-29 03:22:34 UTC
  • mfrom: (7303 work)
  • mto: This revision was merged to the branch mainline in revision 7306.
  • Revision ID: jelmer@jelmer.uk-20190529032234-mt3fuws8gq03tapi
Merge trunk.

Show diffs side-by-side

added added

removed removed

Lines of Context:
63
63
 
64
64
    def test_text_factory_ascii_password(self):
65
65
        ui = ui_testing.TestUIFactory('secret\n')
66
 
        with ui.nested_progress_bar() as pb:
 
66
        with ui.nested_progress_bar():
67
67
            self.assertEqual('secret',
68
68
                             self.apply_redirected(ui.stdin, ui.stdout,
69
69
                                                   ui.stderr,
77
77
    def test_text_factory_unicode_password(self):
78
78
        """Test a unicode password."""
79
79
        ui = ui_testing.TextUIFactory(u'baz\u1234')
80
 
        password = ui.get_password(u'Hello \u1234 %(user)s', user=u'some\u1234')
 
80
        password = ui.get_password(
 
81
            u'Hello \u1234 %(user)s', user=u'some\u1234')
81
82
        self.assertEqual(u'baz\u1234', password)
82
83
        self.assertEqual(u'Hello \u1234 some\u1234: ', ui.stderr.getvalue())
83
84
        # stdin and stdout should be empty
86
87
 
87
88
    def test_text_ui_get_boolean(self):
88
89
        stdin_text = (
89
 
            "y\n" # True
90
 
            "n\n" # False
91
 
            " \n y \n" # True
92
 
            " no \n" # False
93
 
            "yes with garbage\nY\n" # True
94
 
            "not an answer\nno\n" # False
95
 
            "I'm sure!\nyes\n" # True
96
 
            "NO\n" # False
 
90
            "y\n"  # True
 
91
            "n\n"  # False
 
92
            " \n y \n"  # True
 
93
            " no \n"  # False
 
94
            "yes with garbage\nY\n"  # True
 
95
            "not an answer\nno\n"  # False
 
96
            "I'm sure!\nyes\n"  # True
 
97
            "NO\n"  # False
97
98
            "foo\n")
98
99
        with ui_testing.TextUIFactory(stdin_text) as factory:
99
100
            self.assertEqual(True, factory.get_boolean(u""))
141
142
        def choose():
142
143
            return factory.choose(u"", u"&Yes\n&No\nMaybe\nmore &info", 3)
143
144
        stdin_text = (
144
 
            "y\n" # 0
145
 
            "n\n" # 1
146
 
            " \n" # default: 3
147
 
            " no \n" # 1
148
 
            "b\na\nd \n" # bad shortcuts, all ignored
149
 
            "yes with garbage\nY\n" # 0
150
 
            "not an answer\nno\n" # 1
151
 
            "info\nmore info\n" # 3
152
 
            "Maybe\n" # 2
 
145
            "y\n"  # 0
 
146
            "n\n"  # 1
 
147
            " \n"  # default: 3
 
148
            " no \n"  # 1
 
149
            "b\na\nd \n"  # bad shortcuts, all ignored
 
150
            "yes with garbage\nY\n"  # 0
 
151
            "not an answer\nno\n"  # 1
 
152
            "info\nmore info\n"  # 3
 
153
            "Maybe\n"  # 2
153
154
            "foo\n")
154
155
        with ui_testing.TextUIFactory(stdin_text) as factory:
155
156
            self.assertEqual(0, choose())
168
169
 
169
170
    def test_text_ui_choose_no_default(self):
170
171
        stdin_text = (
171
 
            " \n" # no default, invalid!
172
 
            " yes \n" # 0
 
172
            " \n"  # no default, invalid!
 
173
            " yes \n"  # 0
173
174
            "foo\n")
174
175
        with ui_testing.TextUIFactory(stdin_text) as factory:
175
176
            self.assertEqual(0, factory.choose(u"", u"&Yes\n&No"))
211
212
                    u"what do you want"))
212
213
            output = out.getvalue()
213
214
            self.assertContainsRe(output,
214
 
                "| foo *\r\r  *\r*")
215
 
            self.assertContainsString(output,
216
 
                r"what do you want? ([y]es, [n]o): what do you want? ([y]es, [n]o): ")
 
215
                                  "| foo *\r\r  *\r*")
 
216
            self.assertContainsString(
 
217
                output,
 
218
                r"what do you want? ([y]es, [n]o): what do you want? "
 
219
                r"([y]es, [n]o): ")
217
220
            # stdin should have been totally consumed
218
221
            self.assertEqual('', factory.stdin.readline())
219
222
 
248
251
            stderr=ui_testing.StringIOAsTTY())
249
252
        with ui_factory:
250
253
            self.assertIsInstance(ui_factory._progress_view,
251
 
                _mod_ui_text.TextProgressView)
 
254
                                  _mod_ui_text.TextProgressView)
252
255
            ui_factory.be_quiet(True)
253
256
            self.assertIsInstance(ui_factory._progress_view,
254
 
                _mod_ui_text.NullProgressView)
 
257
                                  _mod_ui_text.NullProgressView)
255
258
 
256
259
    def test_text_ui_show_user_warning(self):
257
260
        from ..bzr.groupcompress_repo import RepositoryFormat2a
259
262
        ui = ui_testing.TextUIFactory()
260
263
        remote_fmt = remote.RemoteRepositoryFormat()
261
264
        remote_fmt._network_name = RepositoryFormatKnitPack5().network_name()
262
 
        ui.show_user_warning('cross_format_fetch', from_format=RepositoryFormat2a(),
 
265
        ui.show_user_warning(
 
266
            'cross_format_fetch', from_format=RepositoryFormat2a(),
263
267
            to_format=remote_fmt)
264
268
        self.assertEqual('', ui.stdout.getvalue())
265
269
        self.assertContainsRe(
266
270
            ui.stderr.getvalue(),
267
271
            "^Doing on-the-fly conversion from RepositoryFormat2a\\(\\) to "
268
 
                "RemoteRepositoryFormat\\(_network_name="
269
 
                "b?'Bazaar RepositoryFormatKnitPack5 \\(bzr 1.6\\)\\\\n'\\)\\.\n"
 
272
            "RemoteRepositoryFormat\\(_network_name="
 
273
            "b?'Bazaar RepositoryFormatKnitPack5 \\(bzr 1.6\\)\\\\n'\\)\\.\n"
270
274
            "This may take some time. Upgrade the repositories to "
271
 
                "the same format for better performance\\.\n$")
 
275
            "the same format for better performance\\.\n$")
272
276
        # and now with it suppressed please
273
277
        ui = ui_testing.TextUIFactory()
274
278
        ui.suppressed_warnings.add('cross_format_fetch')
275
 
        ui.show_user_warning('cross_format_fetch', from_format=RepositoryFormat2a(),
 
279
        ui.show_user_warning(
 
280
            'cross_format_fetch', from_format=RepositoryFormat2a(),
276
281
            to_format=remote_fmt)
277
282
        self.assertEqual('', ui.stdout.getvalue())
278
283
        self.assertEqual('', ui.stderr.getvalue())
284
289
    def test_output_clears_terminal(self):
285
290
        clear_calls = []
286
291
 
287
 
        uif =  ui_testing.TextUIFactory()
 
292
        uif = ui_testing.TextUIFactory()
288
293
        uif.clear_term = lambda: clear_calls.append('clear')
289
294
 
290
 
        stream = _mod_ui_text.TextUIOutputStream(uif, uif.stdout, 'utf-8', 'strict')
 
295
        stream = _mod_ui_text.TextUIOutputStream(
 
296
            uif, uif.stdout, 'utf-8', 'strict')
291
297
        stream.write(u"Hello world!\n")
292
298
        stream.write(u"there's more...\n")
293
299
        stream.writelines([u"1\n", u"2\n", u"3\n"])
294
300
 
295
301
        self.assertEqual(uif.stdout.getvalue(),
296
 
            u"Hello world!\n"
297
 
            u"there's more...\n"
298
 
            u"1\n2\n3\n")
 
302
                         u"Hello world!\n"
 
303
                         u"there's more...\n"
 
304
                         u"1\n2\n3\n")
299
305
        self.assertEqual(['clear', 'clear', 'clear'],
300
 
            clear_calls)
 
306
                         clear_calls)
301
307
 
302
308
        stream.flush()
303
309
 
310
316
        FileStringIO = ui_testing.StringIOWithEncoding
311
317
        TTYStringIO = ui_testing.StringIOAsTTY
312
318
        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),
328
 
            ):
 
319
                # on an xterm, either use them or not as the user requests,
 
320
                # otherwise default on
 
321
                (TTYStringIO, 'xterm', 'none', _mod_ui_text.NullProgressView),
 
322
                (TTYStringIO, 'xterm', 'text', _mod_ui_text.TextProgressView),
 
323
                (TTYStringIO, 'xterm', None, _mod_ui_text.TextProgressView),
 
324
                # on a dumb terminal, again if there's explicit configuration
 
325
                # do it, otherwise default off
 
326
                (TTYStringIO, 'dumb', 'none', _mod_ui_text.NullProgressView),
 
327
                (TTYStringIO, 'dumb', 'text', _mod_ui_text.TextProgressView),
 
328
                (TTYStringIO, 'dumb', None, _mod_ui_text.NullProgressView),
 
329
                # on a non-tty terminal, it's null regardless of $TERM
 
330
                (FileStringIO, 'xterm', None, _mod_ui_text.NullProgressView),
 
331
                (FileStringIO, 'dumb', None, _mod_ui_text.NullProgressView),
 
332
                # however, it can still be forced on
 
333
                (FileStringIO, 'dumb', 'text', _mod_ui_text.TextProgressView),
 
334
                ):
329
335
            self.overrideEnv('TERM', term)
330
336
            self.overrideEnv('BRZ_PROGRESS_BAR', pb)
331
337
            stdin = file_class(u'')
332
338
            stderr = file_class()
333
339
            stdout = file_class()
334
340
            uif = _mod_ui.make_ui_for_terminal(stdin, stdout, stderr)
335
 
            self.assertIsInstance(uif, _mod_ui_text.TextUIFactory,
 
341
            self.assertIsInstance(
 
342
                uif, _mod_ui_text.TextUIFactory,
336
343
                "TERM=%s BRZ_PROGRESS_BAR=%s uif=%r" % (term, pb, uif,))
337
 
            self.assertIsInstance(uif.make_progress_view(),
 
344
            self.assertIsInstance(
 
345
                uif.make_progress_view(),
338
346
                expected_pb_class,
339
347
                "TERM=%s BRZ_PROGRESS_BAR=%s uif=%r" % (term, pb, uif,))
340
348
 
345
353
            self.overrideEnv('TERM', term_type)
346
354
            uif = _mod_ui.make_ui_for_terminal(stdin, stdout, stderr)
347
355
            self.assertIsInstance(uif, _mod_ui_text.TextUIFactory,
348
 
                'TERM=%r' % (term_type,))
 
356
                                  'TERM=%r' % (term_type,))
349
357
 
350
358
 
351
359
class SilentUITests(tests.TestCase):
459
467
            self.assertEqual(
460
468
                _mod_ui.ConfirmationUserInterfacePolicy(base_ui, answer, {})
461
469
                .confirm_action("Do something?",
462
 
                    "breezy.tests.do_something", {}),
 
470
                                "breezy.tests.do_something", {}),
463
471
                answer)
464
472
 
465
473
    def test_confirm_action_specific(self):
468
476
            for specific_answer in [True, False]:
469
477
                for conf_id in ['given_id', 'other_id']:
470
478
                    wrapper = _mod_ui.ConfirmationUserInterfacePolicy(
471
 
                        base_ui, default_answer, dict(given_id=specific_answer))
472
 
                    result = wrapper.confirm_action("Do something?", conf_id, {})
 
479
                        base_ui, default_answer,
 
480
                        dict(given_id=specific_answer))
 
481
                    result = wrapper.confirm_action(
 
482
                        "Do something?", conf_id, {})
473
483
                    if conf_id == 'given_id':
474
484
                        self.assertEqual(result, specific_answer)
475
485
                    else:
480
490
        wrapper = _mod_ui.ConfirmationUserInterfacePolicy(
481
491
            base_ui, True, dict(a=2))
482
492
        self.assertThat(repr(wrapper),
483
 
            Equals("ConfirmationUserInterfacePolicy("
484
 
                "NoninteractiveUIFactory(), True, {'a': 2})"))
 
493
                        Equals("ConfirmationUserInterfacePolicy("
 
494
                               "NoninteractiveUIFactory(), True, {'a': 2})"))
485
495
 
486
496
 
487
497
class TestProgressRecordingUI(tests.TestCase):