~bzr-pqm/bzr/bzr.dev

« back to all changes in this revision

Viewing changes to bzrlib/tests/test_ui.py

  • Committer: Vincent Ladeuil
  • Date: 2009-06-02 13:52:27 UTC
  • mfrom: (4368.3.4 376582-auth-prompt)
  • mto: This revision was merged to the branch mainline in revision 4397.
  • Revision ID: v.ladeuil+lp@free.fr-20090602135227-1dfkhq2pvfgyah6b
Display prompt on stderr (not stdout)

Show diffs side-by-side

added added

removed removed

Lines of Context:
67
67
        self.assertEqual('', stdout.getvalue())
68
68
 
69
69
    def test_text_factory_ascii_password(self):
70
 
        ui = TestUIFactory(stdin='secret\n', stdout=StringIOWrapper())
 
70
        ui = TestUIFactory(stdin='secret\n', stdout=StringIOWrapper(),
 
71
                           stderr=StringIOWrapper())
71
72
        pb = ui.nested_progress_bar()
72
73
        try:
73
74
            self.assertEqual('secret',
74
75
                             self.apply_redirected(ui.stdin, ui.stdout,
75
 
                                                   ui.stdout,
 
76
                                                   ui.stderr,
76
77
                                                   ui.get_password))
77
78
            # ': ' is appended to prompt
78
 
            self.assertEqual(': ', ui.stdout.getvalue())
 
79
            self.assertEqual(': ', ui.stderr.getvalue())
 
80
            self.assertEqual('', ui.stdout.readline())
79
81
            # stdin should be empty
80
82
            self.assertEqual('', ui.stdin.readline())
81
83
        finally:
88
90
        it to utf8 to test that we transport the password correctly.
89
91
        """
90
92
        ui = TestUIFactory(stdin=u'baz\u1234'.encode('utf8'),
91
 
                           stdout=StringIOWrapper())
92
 
        ui.stdin.encoding = 'utf8'
93
 
        ui.stdout.encoding = ui.stdin.encoding
 
93
                           stdout=StringIOWrapper(),
 
94
                           stderr=StringIOWrapper())
 
95
        ui.stderr.encoding = ui.stdout.encoding = ui.stdin.encoding = 'utf8'
94
96
        pb = ui.nested_progress_bar()
95
97
        try:
96
 
            password = self.apply_redirected(ui.stdin, ui.stdout, ui.stdout,
 
98
            password = self.apply_redirected(ui.stdin, ui.stdout, ui.stderr,
97
99
                                             ui.get_password,
98
100
                                             u'Hello \u1234 %(user)s',
99
101
                                             user=u'some\u1234')
100
102
            # We use StringIO objects, we need to decode them
101
103
            self.assertEqual(u'baz\u1234', password.decode('utf8'))
102
104
            self.assertEqual(u'Hello \u1234 some\u1234: ',
103
 
                             ui.stdout.getvalue().decode('utf8'))
104
 
            # stdin should be empty
 
105
                             ui.stderr.getvalue().decode('utf8'))
 
106
            # stdin and stdout should be empty
105
107
            self.assertEqual('', ui.stdin.readline())
 
108
            self.assertEqual('', ui.stdout.readline())
106
109
        finally:
107
110
            pb.finished()
108
111
 
193
196
                                 "yes\nn\nnot an answer\n"
194
197
                                 "no\nfoo\n")
195
198
        factory.stdout = StringIO()
 
199
        factory.stderr = StringIO()
196
200
        # there is no output from the base factory
197
201
        self.assertEqual(True, factory.get_boolean(""))
198
202
        self.assertEqual(True, factory.get_boolean(""))
223
227
 
224
228
    def test_text_factory_prompt(self):
225
229
        # see <https://launchpad.net/bugs/365891>
226
 
        factory = TextUIFactory(None, StringIO(), StringIO())
 
230
        factory = TextUIFactory(None, StringIO(), StringIO(), StringIO())
227
231
        factory.prompt('foo %2e')
 
232
        self.assertEqual('', factory.stdout.getvalue())
 
233
        self.assertEqual('foo %2e', factory.stderr.getvalue())
228
234
 
229
235
    def test_text_factory_prompts_and_clears(self):
230
236
        # a get_boolean call should clear the pb before prompting
263
269
        factory = SilentUIFactory()
264
270
        factory.stdin = StringIO("someuser\n\n")
265
271
        factory.stdout = StringIO()
266
 
        self.assertEquals(None, 
 
272
        factory.stderr = StringIO()
 
273
        self.assertEquals(None,
267
274
            factory.get_username(u'Hello\u1234 %(host)s', host=u'some\u1234'))
268
275
        self.assertEquals("", factory.stdout.getvalue())
 
276
        self.assertEquals("", factory.stderr.getvalue())
269
277
        self.assertEquals("someuser\n\n", factory.stdin.getvalue())
270
278
 
271
279
    def test_text_ui_getusername(self):
272
280
        factory = TextUIFactory(None, None, None)
273
281
        factory.stdin = StringIO("someuser\n\n")
274
282
        factory.stdout = StringIO()
 
283
        factory.stderr = StringIO()
275
284
        factory.stdout.encoding = "utf8"
276
285
        # 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())
 
286
        self.assertEqual("someuser",
 
287
                         factory.get_username('Hello %(host)s', host='some'))
 
288
        self.assertEquals("Hello some: ", factory.stderr.getvalue())
 
289
        self.assertEquals('', factory.stdout.getvalue())
280
290
        self.assertEqual("", factory.get_username("Gebruiker"))
281
291
        # stdin should be empty
282
292
        self.assertEqual('', factory.stdin.readline())
283
293
 
284
294
    def test_text_ui_getusername_utf8(self):
285
295
        ui = TestUIFactory(stdin=u'someuser\u1234'.encode('utf8'),
286
 
                           stdout=StringIOWrapper())
287
 
        ui.stdin.encoding = "utf8"
288
 
        ui.stdout.encoding = ui.stdin.encoding
 
296
                           stdout=StringIOWrapper(), stderr=StringIOWrapper())
 
297
        ui.stderr.encoding = ui.stdout.encoding = ui.stdin.encoding = "utf8"
289
298
        pb = ui.nested_progress_bar()
290
299
        try:
291
300
            # there is no output from the base factory
292
 
            username = self.apply_redirected(ui.stdin, ui.stdout, ui.stdout,
 
301
            username = self.apply_redirected(ui.stdin, ui.stdout, ui.stderr,
293
302
                ui.get_username, u'Hello\u1234 %(host)s', host=u'some\u1234')
294
303
            self.assertEquals(u"someuser\u1234", username.decode('utf8'))
295
 
            self.assertEquals(u"Hello\u1234 some\u1234: ", 
296
 
                ui.stdout.getvalue().decode("utf8"))
 
304
            self.assertEquals(u"Hello\u1234 some\u1234: ",
 
305
                              ui.stderr.getvalue().decode("utf8"))
 
306
            self.assertEquals('', ui.stdout.getvalue())
297
307
        finally:
298
308
            pb.finished()
299
309