~bzr-pqm/bzr/bzr.dev

« back to all changes in this revision

Viewing changes to bzrlib/tests/test_ui.py

  • Committer: Martin Pool
  • Date: 2010-01-29 14:09:05 UTC
  • mto: This revision was merged to the branch mainline in revision 4992.
  • Revision ID: mbp@sourcefrog.net-20100129140905-2uiarb6p8di1ywsr
Correction to url

from review: https://code.edge.launchpad.net/~mbp/bzr/doc/+merge/18250

Show diffs side-by-side

added added

removed removed

Lines of Context:
1
 
# Copyright (C) 2005-2010 Canonical Ltd
 
1
# Copyright (C) 2005, 2008, 2009, 2010 Canonical Ltd
2
2
#
3
3
# This program is free software; you can redistribute it and/or modify
4
4
# it under the terms of the GNU General Public License as published by
21
21
import re
22
22
import time
23
23
 
24
 
from StringIO import StringIO
25
 
 
26
24
from bzrlib import (
27
 
    config,
28
25
    errors,
29
 
    remote,
30
 
    repository,
31
26
    tests,
32
27
    ui as _mod_ui,
33
28
    )
34
29
from bzrlib.symbol_versioning import (
35
30
    deprecated_in,
36
31
    )
37
 
from bzrlib.tests import (
38
 
    fixtures,
39
 
    test_progress,
40
 
    )
 
32
from bzrlib.tests import test_progress
41
33
from bzrlib.ui import text as _mod_ui_text
42
34
 
43
35
 
44
 
class TestUIConfiguration(tests.TestCaseWithTransport):
45
 
 
46
 
    def test_output_encoding_configuration(self):
47
 
        enc = fixtures.generate_unicode_encodings().next()
48
 
        config.GlobalConfig().set_user_option('output_encoding',
49
 
            enc)
50
 
        ui = tests.TestUIFactory(stdin=None,
51
 
            stdout=tests.StringIOWrapper(),
52
 
            stderr=tests.StringIOWrapper())
53
 
        os = ui.make_output_stream()
54
 
        self.assertEquals(os.encoding, enc)
55
 
 
56
 
 
57
36
class TestTextUIFactory(tests.TestCase):
58
37
 
59
38
    def test_text_factory_ascii_password(self):
100
79
        finally:
101
80
            pb.finished()
102
81
 
 
82
    def test_progress_note(self):
 
83
        stderr = tests.StringIOWrapper()
 
84
        stdout = tests.StringIOWrapper()
 
85
        ui_factory = _mod_ui_text.TextUIFactory(stdin=tests.StringIOWrapper(''),
 
86
                                                stderr=stderr,
 
87
                                                stdout=stdout)
 
88
        pb = ui_factory.nested_progress_bar()
 
89
        try:
 
90
            result = self.applyDeprecated(deprecated_in((2, 1, 0)),
 
91
                pb.note,
 
92
                't')
 
93
            self.assertEqual(None, result)
 
94
            self.assertEqual("t\n", stdout.getvalue())
 
95
            # Since there was no update() call, there should be no clear() call
 
96
            self.failIf(re.search(r'^\r {10,}\r$',
 
97
                                  stderr.getvalue()) is not None,
 
98
                        'We cleared the stderr without anything to put there')
 
99
        finally:
 
100
            pb.finished()
 
101
 
 
102
    def test_progress_note_clears(self):
 
103
        stderr = test_progress._TTYStringIO()
 
104
        stdout = test_progress._TTYStringIO()
 
105
        # so that we get a TextProgressBar
 
106
        os.environ['TERM'] = 'xterm'
 
107
        ui_factory = _mod_ui_text.TextUIFactory(
 
108
            stdin=tests.StringIOWrapper(''),
 
109
            stdout=stdout, stderr=stderr)
 
110
        self.assertIsInstance(ui_factory._progress_view,
 
111
                              _mod_ui_text.TextProgressView)
 
112
        pb = ui_factory.nested_progress_bar()
 
113
        try:
 
114
            # Create a progress update that isn't throttled
 
115
            pb.update('x', 1, 1)
 
116
            result = self.applyDeprecated(deprecated_in((2, 1, 0)),
 
117
                pb.note, 't')
 
118
            self.assertEqual(None, result)
 
119
            self.assertEqual("t\n", stdout.getvalue())
 
120
            # the exact contents will depend on the terminal width and we don't
 
121
            # care about that right now - but you're probably running it on at
 
122
            # least a 10-character wide terminal :)
 
123
            self.assertContainsRe(stderr.getvalue(), r'\r {10,}\r$')
 
124
        finally:
 
125
            pb.finished()
 
126
 
 
127
    def test_progress_nested(self):
 
128
        # test factory based nested and popping.
 
129
        ui = _mod_ui_text.TextUIFactory(None, None, None)
 
130
        pb1 = ui.nested_progress_bar()
 
131
        pb2 = ui.nested_progress_bar()
 
132
        # You do get a warning if the outermost progress bar wasn't finished
 
133
        # first - it's not clear if this is really useful or if it should just
 
134
        # become orphaned -- mbp 20090120
 
135
        warnings, _ = self.callCatchWarnings(pb1.finished)
 
136
        if len(warnings) != 1:
 
137
            self.fail("unexpected warnings: %r" % (warnings,))
 
138
        pb2.finished()
 
139
        pb1.finished()
 
140
 
103
141
    def test_text_ui_get_boolean(self):
104
142
        stdin = tests.StringIOWrapper("y\n" # True
105
143
                                      "n\n" # False
148
186
        factory = _mod_ui_text.TextUIFactory(
149
187
            stdin=tests.StringIOWrapper("yada\ny\n"),
150
188
            stdout=out, stderr=out)
151
 
        factory._avail_width = lambda: 79
152
189
        pb = factory.nested_progress_bar()
153
190
        pb.show_bar = False
154
191
        pb.show_spinner = False
160
197
                                               factory.get_boolean,
161
198
                                               "what do you want"))
162
199
        output = out.getvalue()
163
 
        self.assertContainsRe(output,
164
 
            "| foo *\r\r  *\r*")
165
 
        self.assertContainsRe(output,
 
200
        self.assertContainsRe(factory.stdout.getvalue(),
 
201
            "foo *\r\r  *\r*")
 
202
        self.assertContainsRe(factory.stdout.getvalue(),
166
203
            r"what do you want\? \[y/n\]: what do you want\? \[y/n\]: ")
167
204
        # stdin should have been totally consumed
168
205
        self.assertEqual('', factory.stdin.readline())
222
259
        self.assertIsInstance(ui_factory._progress_view,
223
260
            _mod_ui_text.NullProgressView)
224
261
 
225
 
    def test_text_ui_show_user_warning(self):
226
 
        from bzrlib.repofmt.groupcompress_repo import RepositoryFormat2a
227
 
        from bzrlib.repofmt.pack_repo import RepositoryFormatKnitPack5
228
 
        err = StringIO()
229
 
        out = StringIO()
230
 
        ui = tests.TextUIFactory(stdin=None, stdout=out, stderr=err)
231
 
        remote_fmt = remote.RemoteRepositoryFormat()
232
 
        remote_fmt._network_name = RepositoryFormatKnitPack5().network_name()
233
 
        ui.show_user_warning('cross_format_fetch', from_format=RepositoryFormat2a(),
234
 
            to_format=remote_fmt)
235
 
        self.assertEquals('', out.getvalue())
236
 
        self.assertEquals("Doing on-the-fly conversion from RepositoryFormat2a() to "
237
 
            "RemoteRepositoryFormat(_network_name='Bazaar RepositoryFormatKnitPack5 "
238
 
            "(bzr 1.6)\\n').\nThis may take some time. Upgrade the repositories to "
239
 
            "the same format for better performance.\n",
240
 
            err.getvalue())
241
 
        # and now with it suppressed please
242
 
        err = StringIO()
243
 
        out = StringIO()
244
 
        ui = tests.TextUIFactory(stdin=None, stdout=out, stderr=err)
245
 
        ui.suppressed_warnings.add('cross_format_fetch')
246
 
        ui.show_user_warning('cross_format_fetch', from_format=RepositoryFormat2a(),
247
 
            to_format=remote_fmt)
248
 
        self.assertEquals('', out.getvalue())
249
 
        self.assertEquals('', err.getvalue())
250
 
 
251
262
 
252
263
class TestTextUIOutputStream(tests.TestCase):
253
264
    """Tests for output stream that synchronizes with progress bar."""
358
369
 
359
370
    def test_test_ui_factory_progress(self):
360
371
        # there's no output; we just want to make sure this doesn't crash -
361
 
        # see https://bugs.launchpad.net/bzr/+bug/408201
 
372
        # see https://bugs.edge.launchpad.net/bzr/+bug/408201
362
373
        ui = tests.TestUIFactory()
363
374
        pb = ui.nested_progress_bar()
364
375
        pb.update('hello')