~bzr-pqm/bzr/bzr.dev

« back to all changes in this revision

Viewing changes to bzrlib/tests/test_ui.py

Merge from bzr.dev

Show diffs side-by-side

added added

removed removed

Lines of Context:
24
24
 
25
25
import bzrlib
26
26
import bzrlib.errors as errors
27
 
from bzrlib.progress import TTYProgressBar, ProgressBarStack
28
 
from bzrlib.tests import TestCase
 
27
from bzrlib.progress import DotsProgressBar, TTYProgressBar, ProgressBarStack
 
28
from bzrlib.tests import (
 
29
    TestUIFactory,
 
30
    StringIOWrapper,
 
31
    TestCase,
 
32
    )
29
33
from bzrlib.tests.test_progress import _TTYStringIO
30
34
from bzrlib.ui import SilentUIFactory
31
35
from bzrlib.ui.text import TextUIFactory
35
39
 
36
40
    def test_silent_factory(self):
37
41
        ui = SilentUIFactory()
38
 
        pb = ui.nested_progress_bar()
39
 
        try:
40
 
            # TODO: Test that there is no output from SilentUIFactory
41
 
    
42
 
            self.assertEquals(ui.get_password(), None)
43
 
            self.assertEquals(ui.get_password(u'Hello There \u1234 %(user)s',
44
 
                                              user=u'some\u1234')
45
 
                             , None)
46
 
        finally:
47
 
            pb.finished()
48
 
 
49
 
    def test_text_factory(self):
50
 
        ui = TextUIFactory()
51
 
        pb = ui.nested_progress_bar()
52
 
        pb.finished()
53
 
        # TODO: Test the output from TextUIFactory, perhaps by overriding sys.stdout
54
 
 
55
 
        # Unfortunately we can't actually test the ui.get_password() because 
56
 
        # that would actually prompt the user for a password during the test suite
57
 
        # This has been tested manually with both LANG=en_US.utf-8 and LANG=C
58
 
        # print
59
 
        # self.assertEquals(ui.get_password(u"%(user)s please type 'bogus'",
60
 
        #                                   user=u'some\u1234')
61
 
        #                  , 'bogus')
62
 
 
 
42
        stdout = StringIO()
 
43
        self.assertEqual(None,
 
44
                         self.apply_redirected(None, stdout, stdout,
 
45
                                               ui.get_password))
 
46
        self.assertEqual('', stdout.getvalue())
 
47
        self.assertEqual(None,
 
48
                         self.apply_redirected(None, stdout, stdout,
 
49
                                               ui.get_password,
 
50
                                               u'Hello\u1234 %(user)s',
 
51
                                               user=u'some\u1234'))
 
52
        self.assertEqual('', stdout.getvalue())
 
53
 
 
54
    def test_text_factory_ascii_password(self):
 
55
        ui = TestUIFactory(stdin='secret\n', stdout=StringIOWrapper())
 
56
        pb = ui.nested_progress_bar()
 
57
        try:
 
58
            self.assertEqual('secret',
 
59
                             self.apply_redirected(ui.stdin, ui.stdout,
 
60
                                                   ui.stdout,
 
61
                                                   ui.get_password))
 
62
            # ': ' is appended to prompt
 
63
            self.assertEqual(': ', ui.stdout.getvalue())
 
64
        finally:
 
65
            pb.finished()
 
66
 
 
67
    def test_text_factory_utf8_password(self):
 
68
        """Test an utf8 password.
 
69
 
 
70
        We can't predict what encoding users will have for stdin, so we force
 
71
        it to utf8 to test that we transport the password correctly.
 
72
        """
 
73
        ui = TestUIFactory(stdin=u'baz\u1234'.encode('utf8'),
 
74
                           stdout=StringIOWrapper())
 
75
        ui.stdin.encoding = 'utf8'
 
76
        ui.stdout.encoding = ui.stdin.encoding
 
77
        pb = ui.nested_progress_bar()
 
78
        try:
 
79
            password = self.apply_redirected(ui.stdin, ui.stdout, ui.stdout,
 
80
                                             ui.get_password,
 
81
                                             u'Hello \u1234 %(user)s',
 
82
                                             user=u'some\u1234')
 
83
            # We use StringIO objects, we need to decode them
 
84
            self.assertEqual(u'baz\u1234', password.decode('utf8'))
 
85
            self.assertEqual(u'Hello \u1234 some\u1234: ',
 
86
                             ui.stdout.getvalue().decode('utf8'))
 
87
        finally:
 
88
            pb.finished()
63
89
 
64
90
    def test_progress_note(self):
65
91
        stderr = StringIO()
136
162
    def test_text_factory_setting_progress_bar(self):
137
163
        # we should be able to choose the progress bar type used.
138
164
        factory = bzrlib.ui.text.TextUIFactory(
139
 
            bar_type=bzrlib.progress.DotsProgressBar)
 
165
            bar_type=DotsProgressBar)
140
166
        bar = factory.nested_progress_bar()
141
167
        bar.finished()
142
 
        self.assertIsInstance(bar, bzrlib.progress.DotsProgressBar)
 
168
        self.assertIsInstance(bar, DotsProgressBar)
143
169
 
144
170
    def test_cli_stdin_is_default_stdin(self):
145
171
        factory = bzrlib.ui.CLIUIFactory()
176
202
 
177
203
    def test_text_factory_prompts_and_clears(self):
178
204
        # a get_boolean call should clear the pb before prompting
179
 
        factory = bzrlib.ui.text.TextUIFactory()
 
205
        factory = bzrlib.ui.text.TextUIFactory(bar_type=DotsProgressBar)
180
206
        factory.stdout = _TTYStringIO()
181
207
        factory.stdin = StringIO("yada\ny\n")
182
208
        pb = self.apply_redirected(
189
215
            self.apply_redirected(
190
216
                None, factory.stdout, factory.stdout, factory.get_boolean, "what do you want")
191
217
            )
192
 
        # FIXME: This assumes the factory's going to produce a spinner-style
193
 
        # progress bar, but it won't if this is run from a dumb terminal (e.g.
194
 
        # from inside gvim.) -- mbp 20061014
195
 
        #
196
 
        # use a regular expression so that we don't depend on the particular
197
 
        # screen width - could also set and restore $COLUMN if that has
198
 
        # priority on all platforms, but it doesn't at present.
199
218
        output = factory.stdout.getvalue()
200
 
        if not re.match(
201
 
            "\r/ \\[    *\\] foo 0/1"
202
 
            "\r   *" 
203
 
            "\rwhat do you want\\? \\[y/n\\]:what do you want\\? \\[y/n\\]:", 
204
 
            output):
205
 
            self.fail("didn't match factory output %r, %r" % (factory, output))
 
219
        self.assertEqual("foo: .\n"
 
220
                         "what do you want? [y/n]: what do you want? [y/n]: ",
 
221
                         factory.stdout.getvalue())