~bzr-pqm/bzr/bzr.dev

« back to all changes in this revision

Viewing changes to bzrlib/tests/test_ui.py

  • Committer: Robert J. Tanner
  • Date: 2009-06-10 03:56:49 UTC
  • mfrom: (4423 +trunk)
  • mto: This revision was merged to the branch mainline in revision 4425.
  • Revision ID: tanner@real-time.com-20090610035649-7rfx4cls4550zc3c
Merge 1.15.1 back to trunk

Show diffs side-by-side

added added

removed removed

Lines of Context:
25
25
 
26
26
import bzrlib
27
27
import bzrlib.errors as errors
28
 
from bzrlib.progress import (
29
 
    DotsProgressBar,
30
 
    ProgressBarStack,
31
 
    ProgressTask,
32
 
    TTYProgressBar,
33
 
    )
34
28
from bzrlib.symbol_versioning import (
35
29
    deprecated_in,
36
30
    )
67
61
        self.assertEqual('', stdout.getvalue())
68
62
 
69
63
    def test_text_factory_ascii_password(self):
70
 
        ui = TestUIFactory(stdin='secret\n', stdout=StringIOWrapper())
 
64
        ui = TestUIFactory(stdin='secret\n', stdout=StringIOWrapper(),
 
65
                           stderr=StringIOWrapper())
71
66
        pb = ui.nested_progress_bar()
72
67
        try:
73
68
            self.assertEqual('secret',
74
69
                             self.apply_redirected(ui.stdin, ui.stdout,
75
 
                                                   ui.stdout,
 
70
                                                   ui.stderr,
76
71
                                                   ui.get_password))
77
72
            # ': ' is appended to prompt
78
 
            self.assertEqual(': ', ui.stdout.getvalue())
 
73
            self.assertEqual(': ', ui.stderr.getvalue())
 
74
            self.assertEqual('', ui.stdout.readline())
79
75
            # stdin should be empty
80
76
            self.assertEqual('', ui.stdin.readline())
81
77
        finally:
88
84
        it to utf8 to test that we transport the password correctly.
89
85
        """
90
86
        ui = TestUIFactory(stdin=u'baz\u1234'.encode('utf8'),
91
 
                           stdout=StringIOWrapper())
92
 
        ui.stdin.encoding = 'utf8'
93
 
        ui.stdout.encoding = ui.stdin.encoding
 
87
                           stdout=StringIOWrapper(),
 
88
                           stderr=StringIOWrapper())
 
89
        ui.stderr.encoding = ui.stdout.encoding = ui.stdin.encoding = 'utf8'
94
90
        pb = ui.nested_progress_bar()
95
91
        try:
96
 
            password = self.apply_redirected(ui.stdin, ui.stdout, ui.stdout,
 
92
            password = self.apply_redirected(ui.stdin, ui.stdout, ui.stderr,
97
93
                                             ui.get_password,
98
94
                                             u'Hello \u1234 %(user)s',
99
95
                                             user=u'some\u1234')
100
96
            # We use StringIO objects, we need to decode them
101
97
            self.assertEqual(u'baz\u1234', password.decode('utf8'))
102
98
            self.assertEqual(u'Hello \u1234 some\u1234: ',
103
 
                             ui.stdout.getvalue().decode('utf8'))
104
 
            # stdin should be empty
 
99
                             ui.stderr.getvalue().decode('utf8'))
 
100
            # stdin and stdout should be empty
105
101
            self.assertEqual('', ui.stdin.readline())
 
102
            self.assertEqual('', ui.stdout.readline())
106
103
        finally:
107
104
            pb.finished()
108
105
 
161
158
        pb2.finished()
162
159
        pb1.finished()
163
160
 
164
 
    def test_progress_stack(self):
165
 
        # test the progress bar stack which the default text factory
166
 
        # uses.
167
 
        stderr = StringIO()
168
 
        stdout = StringIO()
169
 
        # make a stack, which accepts parameters like a pb.
170
 
        stack = self.applyDeprecated(
171
 
            deprecated_in((1, 12, 0)),
172
 
            ProgressBarStack,
173
 
            to_file=stderr, to_messages_file=stdout)
174
 
        # but is not one
175
 
        self.assertFalse(getattr(stack, 'note', False))
176
 
        pb1 = stack.get_nested()
177
 
        pb2 = stack.get_nested()
178
 
        warnings, _ = self.callCatchWarnings(pb1.finished)
179
 
        self.assertEqual(len(warnings), 1)
180
 
        pb2.finished()
181
 
        pb1.finished()
182
 
        # the text ui factory never actually removes the stack once its setup.
183
 
        # we need to be able to nest again correctly from here.
184
 
        pb1 = stack.get_nested()
185
 
        pb2 = stack.get_nested()
186
 
        warnings, _ = self.callCatchWarnings(pb1.finished)
187
 
        self.assertEqual(len(warnings), 1)
188
 
        pb2.finished()
189
 
        pb1.finished()
190
 
 
191
161
    def assert_get_bool_acceptance_of_user_input(self, factory):
192
162
        factory.stdin = StringIO("y\nyes with garbage\n"
193
163
                                 "yes\nn\nnot an answer\n"
194
164
                                 "no\nfoo\n")
195
165
        factory.stdout = StringIO()
 
166
        factory.stderr = StringIO()
196
167
        # there is no output from the base factory
197
168
        self.assertEqual(True, factory.get_boolean(""))
198
169
        self.assertEqual(True, factory.get_boolean(""))
223
194
 
224
195
    def test_text_factory_prompt(self):
225
196
        # see <https://launchpad.net/bugs/365891>
226
 
        factory = TextUIFactory(None, StringIO(), StringIO())
 
197
        factory = TextUIFactory(None, StringIO(), StringIO(), StringIO())
227
198
        factory.prompt('foo %2e')
 
199
        self.assertEqual('', factory.stdout.getvalue())
 
200
        self.assertEqual('foo %2e', factory.stderr.getvalue())
228
201
 
229
202
    def test_text_factory_prompts_and_clears(self):
230
203
        # a get_boolean call should clear the pb before prompting
263
236
        factory = SilentUIFactory()
264
237
        factory.stdin = StringIO("someuser\n\n")
265
238
        factory.stdout = StringIO()
266
 
        self.assertEquals(None, 
 
239
        factory.stderr = StringIO()
 
240
        self.assertEquals(None,
267
241
            factory.get_username(u'Hello\u1234 %(host)s', host=u'some\u1234'))
268
242
        self.assertEquals("", factory.stdout.getvalue())
 
243
        self.assertEquals("", factory.stderr.getvalue())
269
244
        self.assertEquals("someuser\n\n", factory.stdin.getvalue())
270
245
 
271
246
    def test_text_ui_getusername(self):
272
247
        factory = TextUIFactory(None, None, None)
273
248
        factory.stdin = StringIO("someuser\n\n")
274
249
        factory.stdout = StringIO()
 
250
        factory.stderr = StringIO()
275
251
        factory.stdout.encoding = "utf8"
276
252
        # there is no output from the base factory
277
 
        self.assertEqual("someuser", 
278
 
            factory.get_username('Hello %(host)s', host='some'))
279
 
        self.assertEquals("Hello some: ", factory.stdout.getvalue())
 
253
        self.assertEqual("someuser",
 
254
                         factory.get_username('Hello %(host)s', host='some'))
 
255
        self.assertEquals("Hello some: ", factory.stderr.getvalue())
 
256
        self.assertEquals('', factory.stdout.getvalue())
280
257
        self.assertEqual("", factory.get_username("Gebruiker"))
281
258
        # stdin should be empty
282
259
        self.assertEqual('', factory.stdin.readline())
283
260
 
284
261
    def test_text_ui_getusername_utf8(self):
285
262
        ui = TestUIFactory(stdin=u'someuser\u1234'.encode('utf8'),
286
 
                           stdout=StringIOWrapper())
287
 
        ui.stdin.encoding = "utf8"
288
 
        ui.stdout.encoding = ui.stdin.encoding
 
263
                           stdout=StringIOWrapper(), stderr=StringIOWrapper())
 
264
        ui.stderr.encoding = ui.stdout.encoding = ui.stdin.encoding = "utf8"
289
265
        pb = ui.nested_progress_bar()
290
266
        try:
291
267
            # there is no output from the base factory
292
 
            username = self.apply_redirected(ui.stdin, ui.stdout, ui.stdout,
 
268
            username = self.apply_redirected(ui.stdin, ui.stdout, ui.stderr,
293
269
                ui.get_username, u'Hello\u1234 %(host)s', host=u'some\u1234')
294
270
            self.assertEquals(u"someuser\u1234", username.decode('utf8'))
295
 
            self.assertEquals(u"Hello\u1234 some\u1234: ", 
296
 
                ui.stdout.getvalue().decode("utf8"))
 
271
            self.assertEquals(u"Hello\u1234 some\u1234: ",
 
272
                              ui.stderr.getvalue().decode("utf8"))
 
273
            self.assertEquals('', ui.stdout.getvalue())
297
274
        finally:
298
275
            pb.finished()
299
276