~bzr-pqm/bzr/bzr.dev

« back to all changes in this revision

Viewing changes to bzrlib/tests/test_ui.py

  • Committer: John Arbash Meinel
  • Date: 2010-08-04 04:33:24 UTC
  • mto: This revision was merged to the branch mainline in revision 5390.
  • Revision ID: john@arbash-meinel.com-20100804043324-1ldc2v2w1kza7ox4
get into the nitty gritty for the _key_to_sha1 function.

It doesn't seem to help to op-out of the actual unhexlify call, so at least
that doesn't seem to be the performance overhead.
We get down to around 19.9us for _key_to_sha1 across 120 keys, which is
0.166us per key.

Show diffs side-by-side

added added

removed removed

Lines of Context:
21
21
import re
22
22
import time
23
23
 
 
24
from StringIO import StringIO
 
25
 
24
26
from bzrlib import (
 
27
    config,
25
28
    errors,
 
29
    remote,
 
30
    repository,
26
31
    tests,
27
32
    ui as _mod_ui,
28
33
    )
29
34
from bzrlib.symbol_versioning import (
30
35
    deprecated_in,
31
36
    )
32
 
from bzrlib.tests import test_progress
 
37
from bzrlib.tests import (
 
38
    fixtures,
 
39
    test_progress,
 
40
    )
33
41
from bzrlib.ui import text as _mod_ui_text
34
42
 
35
43
 
 
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
 
36
57
class TestTextUIFactory(tests.TestCase):
37
58
 
38
59
    def test_text_factory_ascii_password(self):
79
100
        finally:
80
101
            pb.finished()
81
102
 
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
103
    def test_text_ui_get_boolean(self):
128
104
        stdin = tests.StringIOWrapper("y\n" # True
129
105
                                      "n\n" # False
172
148
        factory = _mod_ui_text.TextUIFactory(
173
149
            stdin=tests.StringIOWrapper("yada\ny\n"),
174
150
            stdout=out, stderr=out)
 
151
        factory._avail_width = lambda: 79
175
152
        pb = factory.nested_progress_bar()
176
153
        pb.show_bar = False
177
154
        pb.show_spinner = False
183
160
                                               factory.get_boolean,
184
161
                                               "what do you want"))
185
162
        output = out.getvalue()
186
 
        self.assertContainsRe(factory.stdout.getvalue(),
187
 
            "foo *\r\r  *\r*")
188
 
        self.assertContainsRe(factory.stdout.getvalue(),
 
163
        self.assertContainsRe(output,
 
164
            "| foo *\r\r  *\r*")
 
165
        self.assertContainsRe(output,
189
166
            r"what do you want\? \[y/n\]: what do you want\? \[y/n\]: ")
190
167
        # stdin should have been totally consumed
191
168
        self.assertEqual('', factory.stdin.readline())
245
222
        self.assertIsInstance(ui_factory._progress_view,
246
223
            _mod_ui_text.NullProgressView)
247
224
 
 
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
 
248
251
 
249
252
class TestTextUIOutputStream(tests.TestCase):
250
253
    """Tests for output stream that synchronizes with progress bar."""
355
358
 
356
359
    def test_test_ui_factory_progress(self):
357
360
        # there's no output; we just want to make sure this doesn't crash -
358
 
        # see https://bugs.edge.launchpad.net/bzr/+bug/408201
 
361
        # see https://bugs.launchpad.net/bzr/+bug/408201
359
362
        ui = tests.TestUIFactory()
360
363
        pb = ui.nested_progress_bar()
361
364
        pb.update('hello')