~bzr-pqm/bzr/bzr.dev

« back to all changes in this revision

Viewing changes to bzrlib/tests/test_ui.py

  • Committer: Ian Clatworthy
  • Date: 2009-01-19 02:24:15 UTC
  • mto: This revision was merged to the branch mainline in revision 3944.
  • Revision ID: ian.clatworthy@canonical.com-20090119022415-mo0mcfeiexfktgwt
apply jam's log --short fix (Ian Clatworthy)

Show diffs side-by-side

added added

removed removed

Lines of Context:
1
 
# Copyright (C) 2005 by Canonical Ltd
 
1
# Copyright (C) 2005, 2008 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
 
25
25
import bzrlib
26
26
import bzrlib.errors as errors
27
 
from bzrlib.progress import TTYProgressBar, ProgressBarStack
28
 
from bzrlib.tests import TestCase
29
 
from bzrlib.ui import SilentUIFactory
 
27
from bzrlib.progress import (
 
28
    DotsProgressBar,
 
29
    ProgressBarStack,
 
30
    TTYProgressBar,
 
31
    )
 
32
from bzrlib.tests import (
 
33
    TestCase,
 
34
    TestUIFactory,
 
35
    StringIOWrapper,
 
36
    )
 
37
from bzrlib.tests.test_progress import _TTYStringIO
 
38
from bzrlib.ui import (
 
39
    CLIUIFactory,
 
40
    SilentUIFactory,
 
41
    )
30
42
from bzrlib.ui.text import TextUIFactory
31
43
 
32
44
 
34
46
 
35
47
    def test_silent_factory(self):
36
48
        ui = SilentUIFactory()
37
 
        pb = ui.nested_progress_bar()
38
 
        try:
39
 
            # TODO: Test that there is no output from SilentUIFactory
40
 
    
41
 
            self.assertEquals(ui.get_password(), None)
42
 
            self.assertEquals(ui.get_password(u'Hello There \u1234 %(user)s',
43
 
                                              user=u'some\u1234')
44
 
                             , None)
45
 
        finally:
46
 
            pb.finished()
47
 
 
48
 
    def test_text_factory(self):
49
 
        ui = TextUIFactory()
50
 
        pb = ui.nested_progress_bar()
51
 
        pb.finished()
52
 
        # TODO: Test the output from TextUIFactory, perhaps by overriding sys.stdout
53
 
 
54
 
        # Unfortunately we can't actually test the ui.get_password() because 
55
 
        # that would actually prompt the user for a password during the test suite
56
 
        # This has been tested manually with both LANG=en_US.utf-8 and LANG=C
57
 
        # print
58
 
        # self.assertEquals(ui.get_password(u"%(user)s please type 'bogus'",
59
 
        #                                   user=u'some\u1234')
60
 
        #                  , 'bogus')
61
 
 
 
49
        stdout = StringIO()
 
50
        self.assertEqual(None,
 
51
                         self.apply_redirected(None, stdout, stdout,
 
52
                                               ui.get_password))
 
53
        self.assertEqual('', stdout.getvalue())
 
54
        self.assertEqual(None,
 
55
                         self.apply_redirected(None, stdout, stdout,
 
56
                                               ui.get_password,
 
57
                                               u'Hello\u1234 %(user)s',
 
58
                                               user=u'some\u1234'))
 
59
        self.assertEqual('', stdout.getvalue())
 
60
 
 
61
    def test_text_factory_ascii_password(self):
 
62
        ui = TestUIFactory(stdin='secret\n', stdout=StringIOWrapper())
 
63
        pb = ui.nested_progress_bar()
 
64
        try:
 
65
            self.assertEqual('secret',
 
66
                             self.apply_redirected(ui.stdin, ui.stdout,
 
67
                                                   ui.stdout,
 
68
                                                   ui.get_password))
 
69
            # ': ' is appended to prompt
 
70
            self.assertEqual(': ', ui.stdout.getvalue())
 
71
            # stdin should be empty
 
72
            self.assertEqual('', ui.stdin.readline())
 
73
        finally:
 
74
            pb.finished()
 
75
 
 
76
    def test_text_factory_utf8_password(self):
 
77
        """Test an utf8 password.
 
78
 
 
79
        We can't predict what encoding users will have for stdin, so we force
 
80
        it to utf8 to test that we transport the password correctly.
 
81
        """
 
82
        ui = TestUIFactory(stdin=u'baz\u1234'.encode('utf8'),
 
83
                           stdout=StringIOWrapper())
 
84
        ui.stdin.encoding = 'utf8'
 
85
        ui.stdout.encoding = ui.stdin.encoding
 
86
        pb = ui.nested_progress_bar()
 
87
        try:
 
88
            password = self.apply_redirected(ui.stdin, ui.stdout, ui.stdout,
 
89
                                             ui.get_password,
 
90
                                             u'Hello \u1234 %(user)s',
 
91
                                             user=u'some\u1234')
 
92
            # We use StringIO objects, we need to decode them
 
93
            self.assertEqual(u'baz\u1234', password.decode('utf8'))
 
94
            self.assertEqual(u'Hello \u1234 some\u1234: ',
 
95
                             ui.stdout.getvalue().decode('utf8'))
 
96
            # stdin should be empty
 
97
            self.assertEqual('', ui.stdin.readline())
 
98
        finally:
 
99
            pb.finished()
62
100
 
63
101
    def test_progress_note(self):
64
102
        stderr = StringIO()
65
103
        stdout = StringIO()
66
 
        ui_factory = TextUIFactory()
67
 
        pb = ui_factory.nested_progress_bar()
68
 
        try:
69
 
            pb.to_messages_file = stdout
70
 
            ui_factory._progress_bar_stack.bottom().to_file = stderr
 
104
        ui_factory = TextUIFactory(stdin=StringIO(''),
 
105
            stderr=stderr,
 
106
            stdout=stdout)
 
107
        pb = ui_factory.nested_progress_bar()
 
108
        try:
 
109
            result = pb.note('t')
 
110
            self.assertEqual(None, result)
 
111
            self.assertEqual("t\n", stdout.getvalue())
 
112
            # Since there was no update() call, there should be no clear() call
 
113
            self.failIf(re.search(r'^\r {10,}\r$',
 
114
                                  stderr.getvalue()) is not None,
 
115
                        'We cleared the stderr without anything to put there')
 
116
        finally:
 
117
            pb.finished()
 
118
 
 
119
    def test_progress_note_clears(self):
 
120
        stderr = StringIO()
 
121
        stdout = StringIO()
 
122
        # The PQM redirects the output to a file, so it
 
123
        # defaults to creating a Dots progress bar. we
 
124
        # need to force it to believe we are a TTY
 
125
        ui_factory = TextUIFactory(
 
126
            stdin=StringIO(''),
 
127
            stdout=stdout, stderr=stderr)
 
128
        pb = ui_factory.nested_progress_bar()
 
129
        try:
 
130
            # Create a progress update that isn't throttled
 
131
            pb.update('x', 1, 1)
71
132
            result = pb.note('t')
72
133
            self.assertEqual(None, result)
73
134
            self.assertEqual("t\n", stdout.getvalue())
74
135
            # the exact contents will depend on the terminal width and we don't
75
136
            # care about that right now - but you're probably running it on at
76
137
            # least a 10-character wide terminal :)
77
 
            self.assertContainsRe(stderr.getvalue(), r'^\r {10,}\r$')
 
138
            self.assertContainsRe(stderr.getvalue(), r'\r {10,}\r$')
78
139
        finally:
79
140
            pb.finished()
80
141
 
81
142
    def test_progress_nested(self):
82
143
        # test factory based nested and popping.
83
 
        ui = TextUIFactory()
 
144
        ui = TextUIFactory(None, None, None)
84
145
        pb1 = ui.nested_progress_bar()
85
146
        pb2 = ui.nested_progress_bar()
86
 
        self.assertRaises(errors.MissingProgressBarFinish, pb1.finished)
 
147
        # We no longer warn about finishing unnested progress bars.
 
148
        warnings, _ = self.callCatchWarnings(pb1.finished)
 
149
        self.assertEqual(len(warnings), 0)
87
150
        pb2.finished()
88
151
        pb1.finished()
89
152
 
98
161
        self.assertFalse(getattr(stack, 'note', False))
99
162
        pb1 = stack.get_nested()
100
163
        pb2 = stack.get_nested()
101
 
        self.assertRaises(errors.MissingProgressBarFinish, pb1.finished)
 
164
        warnings, _ = self.callCatchWarnings(pb1.finished)
 
165
        self.assertEqual(len(warnings), 1)
102
166
        pb2.finished()
103
167
        pb1.finished()
104
168
        # the text ui factory never actually removes the stack once its setup.
105
169
        # we need to be able to nest again correctly from here.
106
170
        pb1 = stack.get_nested()
107
171
        pb2 = stack.get_nested()
108
 
        self.assertRaises(errors.MissingProgressBarFinish, pb1.finished)
 
172
        warnings, _ = self.callCatchWarnings(pb1.finished)
 
173
        self.assertEqual(len(warnings), 1)
109
174
        pb2.finished()
110
175
        pb1.finished()
111
176
 
112
 
    def test_text_factory_setting_progress_bar(self):
113
 
        # we should be able to choose the progress bar type used.
114
 
        factory = bzrlib.ui.text.TextUIFactory(
115
 
            bar_type=bzrlib.progress.DotsProgressBar)
116
 
        bar = factory.nested_progress_bar()
117
 
        bar.finished()
118
 
        self.assertIsInstance(bar, bzrlib.progress.DotsProgressBar)
119
 
 
120
 
    def test_cli_stdin_is_default_stdin(self):
121
 
        factory = bzrlib.ui.CLIUIFactory()
122
 
        self.assertEqual(sys.stdin, factory.stdin)
123
 
 
124
177
    def assert_get_bool_acceptance_of_user_input(self, factory):
125
 
        factory.stdin = StringIO("y\nyes with garbage\nyes\nn\nnot an answer\nno\nfoo\n")
 
178
        factory.stdin = StringIO("y\nyes with garbage\n"
 
179
                                 "yes\nn\nnot an answer\n"
 
180
                                 "no\nfoo\n")
126
181
        factory.stdout = StringIO()
127
182
        # there is no output from the base factory
128
183
        self.assertEqual(True, factory.get_boolean(""))
130
185
        self.assertEqual(False, factory.get_boolean(""))
131
186
        self.assertEqual(False, factory.get_boolean(""))
132
187
        self.assertEqual("foo\n", factory.stdin.read())
 
188
        # stdin should be empty
 
189
        self.assertEqual('', factory.stdin.readline())
133
190
 
134
191
    def test_silent_ui_getbool(self):
135
 
        factory = bzrlib.ui.SilentUIFactory()
 
192
        factory = SilentUIFactory()
136
193
        self.assert_get_bool_acceptance_of_user_input(factory)
137
194
 
138
195
    def test_silent_factory_prompts_silently(self):
139
 
        factory = bzrlib.ui.SilentUIFactory()
 
196
        factory = SilentUIFactory()
140
197
        stdout = StringIO()
141
198
        factory.stdin = StringIO("y\n")
142
 
        self.assertEqual(
143
 
            True,
144
 
            self.apply_redirected(
145
 
                None, stdout, stdout, factory.get_boolean, "foo")
146
 
            )
 
199
        self.assertEqual(True,
 
200
                         self.apply_redirected(None, stdout, stdout,
 
201
                                               factory.get_boolean, "foo"))
147
202
        self.assertEqual("", stdout.getvalue())
148
 
        
 
203
        # stdin should be empty
 
204
        self.assertEqual('', factory.stdin.readline())
 
205
 
149
206
    def test_text_ui_getbool(self):
150
 
        factory = bzrlib.ui.text.TextUIFactory()
 
207
        factory = TextUIFactory(None, None, None)
151
208
        self.assert_get_bool_acceptance_of_user_input(factory)
152
209
 
153
210
    def test_text_factory_prompts_and_clears(self):
154
211
        # a get_boolean call should clear the pb before prompting
155
 
        factory = bzrlib.ui.text.TextUIFactory()
156
 
        factory.stdout = StringIO()
157
 
        factory.stdin = StringIO("yada\ny\n")
158
 
        pb = self.apply_redirected(
159
 
            factory.stdin, factory.stdout, factory.stdout, factory.nested_progress_bar)
160
 
        self.apply_redirected(
161
 
            factory.stdin, factory.stdout, factory.stdout, pb.update, "foo", 0, 1)
162
 
        self.assertEqual(
163
 
            True,
164
 
            self.apply_redirected(
165
 
                None, factory.stdout, factory.stdout, factory.get_boolean, "what do you want")
166
 
            )
167
 
        # use a regular expression so that we don't depend on the particular
168
 
        # screen width - could also set and restore $COLUMN if that has
169
 
        # priority on all platforms, but it doesn't at present.
170
 
        output = factory.stdout.getvalue()
171
 
        if not re.match(
172
 
            "\r/ \\[    *\\] foo 0/1"
173
 
            "\r   *" 
174
 
            "\rwhat do you want\\? \\[y/n\\]:what do you want\\? \\[y/n\\]:", 
175
 
            output):
176
 
            self.fail("didn't match factory output %r" % factory)
 
212
        out = _TTYStringIO()
 
213
        factory = TextUIFactory(stdin=StringIO("yada\ny\n"), stdout=out, stderr=out)
 
214
        pb = factory.nested_progress_bar()
 
215
        pb.show_bar = False
 
216
        pb.show_spinner = False
 
217
        pb.show_count = False
 
218
        pb.update("foo", 0, 1)
 
219
        self.assertEqual(True,
 
220
                         self.apply_redirected(None, factory.stdout,
 
221
                                               factory.stdout,
 
222
                                               factory.get_boolean,
 
223
                                               "what do you want"))
 
224
        output = out.getvalue()
 
225
        self.assertContainsRe(factory.stdout.getvalue(),
 
226
            "foo *\r\r  *\r*")
 
227
        self.assertContainsRe(factory.stdout.getvalue(),
 
228
            r"what do you want\? \[y/n\]: what do you want\? \[y/n\]: ")
 
229
        # stdin should have been totally consumed
 
230
        self.assertEqual('', factory.stdin.readline())