/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: 2018-05-06 11:48:54 UTC
  • mto: This revision was merged to the branch mainline in revision 6960.
  • Revision ID: jelmer@jelmer.uk-20180506114854-h4qd9ojaqy8wxjsd
Move .mailmap to root.

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