~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-04-01 04:41:18 UTC
  • mto: This revision was merged to the branch mainline in revision 5128.
  • Revision ID: mbp@sourcefrog.net-20100401044118-shyctqc02ob08ngz
ignore .testrepository

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
24
24
from StringIO import StringIO
25
25
 
26
26
from bzrlib import (
27
 
    config,
28
27
    errors,
29
28
    remote,
30
29
    repository,
34
33
from bzrlib.symbol_versioning import (
35
34
    deprecated_in,
36
35
    )
37
 
from bzrlib.tests import (
38
 
    fixtures,
39
 
    test_progress,
40
 
    )
 
36
from bzrlib.tests import test_progress
41
37
from bzrlib.ui import text as _mod_ui_text
42
38
 
43
39
 
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
40
class TestTextUIFactory(tests.TestCase):
58
41
 
59
42
    def test_text_factory_ascii_password(self):
100
83
        finally:
101
84
            pb.finished()
102
85
 
 
86
    def test_progress_note(self):
 
87
        stderr = tests.StringIOWrapper()
 
88
        stdout = tests.StringIOWrapper()
 
89
        ui_factory = _mod_ui_text.TextUIFactory(stdin=tests.StringIOWrapper(''),
 
90
                                                stderr=stderr,
 
91
                                                stdout=stdout)
 
92
        pb = ui_factory.nested_progress_bar()
 
93
        try:
 
94
            result = self.applyDeprecated(deprecated_in((2, 1, 0)),
 
95
                pb.note,
 
96
                't')
 
97
            self.assertEqual(None, result)
 
98
            self.assertEqual("t\n", stdout.getvalue())
 
99
            # Since there was no update() call, there should be no clear() call
 
100
            self.failIf(re.search(r'^\r {10,}\r$',
 
101
                                  stderr.getvalue()) is not None,
 
102
                        'We cleared the stderr without anything to put there')
 
103
        finally:
 
104
            pb.finished()
 
105
 
 
106
    def test_progress_note_clears(self):
 
107
        stderr = test_progress._TTYStringIO()
 
108
        stdout = test_progress._TTYStringIO()
 
109
        # so that we get a TextProgressBar
 
110
        os.environ['TERM'] = 'xterm'
 
111
        ui_factory = _mod_ui_text.TextUIFactory(
 
112
            stdin=tests.StringIOWrapper(''),
 
113
            stdout=stdout, stderr=stderr)
 
114
        self.assertIsInstance(ui_factory._progress_view,
 
115
                              _mod_ui_text.TextProgressView)
 
116
        pb = ui_factory.nested_progress_bar()
 
117
        try:
 
118
            # Create a progress update that isn't throttled
 
119
            pb.update('x', 1, 1)
 
120
            result = self.applyDeprecated(deprecated_in((2, 1, 0)),
 
121
                pb.note, 't')
 
122
            self.assertEqual(None, result)
 
123
            self.assertEqual("t\n", stdout.getvalue())
 
124
            # the exact contents will depend on the terminal width and we don't
 
125
            # care about that right now - but you're probably running it on at
 
126
            # least a 10-character wide terminal :)
 
127
            self.assertContainsRe(stderr.getvalue(), r'\r {10,}\r$')
 
128
        finally:
 
129
            pb.finished()
 
130
 
103
131
    def test_text_ui_get_boolean(self):
104
132
        stdin = tests.StringIOWrapper("y\n" # True
105
133
                                      "n\n" # False
148
176
        factory = _mod_ui_text.TextUIFactory(
149
177
            stdin=tests.StringIOWrapper("yada\ny\n"),
150
178
            stdout=out, stderr=out)
151
 
        factory._avail_width = lambda: 79
152
179
        pb = factory.nested_progress_bar()
153
180
        pb.show_bar = False
154
181
        pb.show_spinner = False
160
187
                                               factory.get_boolean,
161
188
                                               "what do you want"))
162
189
        output = out.getvalue()
163
 
        self.assertContainsRe(output,
164
 
            "| foo *\r\r  *\r*")
165
 
        self.assertContainsRe(output,
 
190
        self.assertContainsRe(factory.stdout.getvalue(),
 
191
            "foo *\r\r  *\r*")
 
192
        self.assertContainsRe(factory.stdout.getvalue(),
166
193
            r"what do you want\? \[y/n\]: what do you want\? \[y/n\]: ")
167
194
        # stdin should have been totally consumed
168
195
        self.assertEqual('', factory.stdin.readline())
358
385
 
359
386
    def test_test_ui_factory_progress(self):
360
387
        # there's no output; we just want to make sure this doesn't crash -
361
 
        # see https://bugs.launchpad.net/bzr/+bug/408201
 
388
        # see https://bugs.edge.launchpad.net/bzr/+bug/408201
362
389
        ui = tests.TestUIFactory()
363
390
        pb = ui.nested_progress_bar()
364
391
        pb.update('hello')