~bzr-pqm/bzr/bzr.dev

« back to all changes in this revision

Viewing changes to bzrlib/tests/test_ui.py

  • Committer: Canonical.com Patch Queue Manager
  • Date: 2010-09-29 22:03:03 UTC
  • mfrom: (5416.2.6 jam-integration)
  • Revision ID: pqm@pqm.ubuntu.com-20100929220303-cr95h8iwtggco721
(mbp) Add 'break-lock --force'

Show diffs side-by-side

added added

removed removed

Lines of Context:
1
 
# Copyright (C) 2005-2011 Canonical Ltd
 
1
# Copyright (C) 2005-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
17
17
"""Tests for the bzrlib ui
18
18
"""
19
19
 
 
20
import os
20
21
import time
21
22
 
22
23
from StringIO import StringIO
25
26
 
26
27
from bzrlib import (
27
28
    config,
 
29
    errors,
28
30
    remote,
 
31
    repository,
29
32
    tests,
30
33
    ui as _mod_ui,
31
34
    )
 
35
from bzrlib.symbol_versioning import (
 
36
    deprecated_in,
 
37
    )
32
38
from bzrlib.tests import (
33
39
    fixtures,
 
40
    test_progress,
34
41
    )
35
42
from bzrlib.ui import text as _mod_ui_text
36
43
from bzrlib.tests.testui import (
38
45
    )
39
46
 
40
47
 
41
 
class TTYStringIO(StringIO):
42
 
    """A helper class which makes a StringIO look like a terminal"""
43
 
 
44
 
    def isatty(self):
45
 
        return True
46
 
 
47
 
 
48
 
class NonTTYStringIO(StringIO):
49
 
    """Helper that implements isatty() but returns False"""
50
 
 
51
 
    def isatty(self):
52
 
        return False
53
 
 
54
 
 
55
48
class TestUIConfiguration(tests.TestCaseWithTransport):
56
49
 
57
50
    def test_output_encoding_configuration(self):
58
51
        enc = fixtures.generate_unicode_encodings().next()
59
 
        config.GlobalStack().set('output_encoding', enc)
 
52
        config.GlobalConfig().set_user_option('output_encoding',
 
53
            enc)
60
54
        ui = tests.TestUIFactory(stdin=None,
61
55
            stdout=tests.StringIOWrapper(),
62
56
            stderr=tests.StringIOWrapper())
75
69
    def test_text_factory_confirm(self):
76
70
        # turns into reading a regular boolean
77
71
        ui = self.make_test_ui_factory('n\n')
78
 
        self.assertEquals(ui.confirm_action(u'Should %(thing)s pass?',
 
72
        self.assertEquals(ui.confirm_action('Should %(thing)s pass?',
79
73
            'bzrlib.tests.test_ui.confirmation',
80
74
            {'thing': 'this'},),
81
75
            False)
97
91
            pb.finished()
98
92
 
99
93
    def test_text_factory_utf8_password(self):
100
 
        """Test an utf8 password."""
101
 
        ui = _mod_ui_text.TextUIFactory(None, None, None)
102
 
        ui.stdin = tests.StringIOWrapper(u'baz\u1234'.encode('utf8'))
103
 
        ui.stdout = tests.StringIOWrapper()
104
 
        ui.stderr = tests.StringIOWrapper()
 
94
        """Test an utf8 password.
 
95
 
 
96
        We can't predict what encoding users will have for stdin, so we force
 
97
        it to utf8 to test that we transport the password correctly.
 
98
        """
 
99
        ui = self.make_test_ui_factory(u'baz\u1234'.encode('utf8'))
105
100
        ui.stderr.encoding = ui.stdout.encoding = ui.stdin.encoding = 'utf8'
106
 
        password = ui.get_password(u'Hello \u1234 %(user)s', user=u'some\u1234')
107
 
        self.assertEqual(u'baz\u1234', password)
108
 
        self.assertEqual(u'Hello \u1234 some\u1234: ',
109
 
                         ui.stderr.getvalue().decode('utf8'))
110
 
        # stdin and stdout should be empty
111
 
        self.assertEqual('', ui.stdin.readline())
112
 
        self.assertEqual('', ui.stdout.getvalue())
 
101
        pb = ui.nested_progress_bar()
 
102
        try:
 
103
            password = self.apply_redirected(ui.stdin, ui.stdout, ui.stderr,
 
104
                                             ui.get_password,
 
105
                                             u'Hello \u1234 %(user)s',
 
106
                                             user=u'some\u1234')
 
107
            # We use StringIO objects, we need to decode them
 
108
            self.assertEqual(u'baz\u1234', password.decode('utf8'))
 
109
            self.assertEqual(u'Hello \u1234 some\u1234: ',
 
110
                             ui.stderr.getvalue().decode('utf8'))
 
111
            # stdin and stdout should be empty
 
112
            self.assertEqual('', ui.stdin.readline())
 
113
            self.assertEqual('', ui.stdout.readline())
 
114
        finally:
 
115
            pb.finished()
113
116
 
114
117
    def test_text_ui_get_boolean(self):
115
118
        stdin = tests.StringIOWrapper("y\n" # True
116
119
                                      "n\n" # False
117
 
                                      " \n y \n" # True
118
 
                                      " no \n" # False
119
120
                                      "yes with garbage\nY\n" # True
120
121
                                      "not an answer\nno\n" # False
121
122
                                      "I'm sure!\nyes\n" # True
124
125
        stdout = tests.StringIOWrapper()
125
126
        stderr = tests.StringIOWrapper()
126
127
        factory = _mod_ui_text.TextUIFactory(stdin, stdout, stderr)
127
 
        self.assertEqual(True, factory.get_boolean(u""))
128
 
        self.assertEqual(False, factory.get_boolean(u""))
129
 
        self.assertEqual(True, factory.get_boolean(u""))
130
 
        self.assertEqual(False, factory.get_boolean(u""))
131
 
        self.assertEqual(True, factory.get_boolean(u""))
132
 
        self.assertEqual(False, factory.get_boolean(u""))
133
 
        self.assertEqual(True, factory.get_boolean(u""))
134
 
        self.assertEqual(False, factory.get_boolean(u""))
135
 
        self.assertEqual("foo\n", factory.stdin.read())
136
 
        # stdin should be empty
137
 
        self.assertEqual('', factory.stdin.readline())
138
 
        # return false on EOF
139
 
        self.assertEqual(False, factory.get_boolean(u""))
140
 
 
141
 
    def test_text_ui_choose_bad_parameters(self):
142
 
        stdin = tests.StringIOWrapper()
143
 
        stdout = tests.StringIOWrapper()
144
 
        stderr = tests.StringIOWrapper()
145
 
        factory = _mod_ui_text.TextUIFactory(stdin, stdout, stderr)
146
 
        # invalid default index
147
 
        self.assertRaises(ValueError, factory.choose, u"", u"&Yes\n&No", 3)
148
 
        # duplicated choice
149
 
        self.assertRaises(ValueError, factory.choose, u"", u"&choice\n&ChOiCe")
150
 
        # duplicated shortcut
151
 
        self.assertRaises(ValueError, factory.choose, u"", u"&choice1\nchoi&ce2")
152
 
 
153
 
    def test_text_ui_choose_prompt(self):
154
 
        stdin = tests.StringIOWrapper()
155
 
        stdout = tests.StringIOWrapper()
156
 
        stderr = tests.StringIOWrapper()
157
 
        factory = _mod_ui_text.TextUIFactory(stdin, stdout, stderr)
158
 
        # choices with explicit shortcuts
159
 
        factory.choose(u"prompt", u"&yes\n&No\nmore &info")
160
 
        self.assertEqual("prompt ([y]es, [N]o, more [i]nfo): \n", factory.stderr.getvalue())
161
 
        # automatic shortcuts
162
 
        factory.stderr.truncate(0)
163
 
        factory.choose(u"prompt", u"yes\nNo\nmore info")
164
 
        self.assertEqual("prompt ([y]es, [N]o, [m]ore info): \n", factory.stderr.getvalue())
165
 
 
166
 
    def test_text_ui_choose_return_values(self):
167
 
        choose = lambda: factory.choose(u"", u"&Yes\n&No\nMaybe\nmore &info", 3)
168
 
        stdin = tests.StringIOWrapper("y\n" # 0
169
 
                                      "n\n" # 1
170
 
                                      " \n" # default: 3
171
 
                                      " no \n" # 1
172
 
                                      "b\na\nd \n" # bad shortcuts, all ignored
173
 
                                      "yes with garbage\nY\n" # 0
174
 
                                      "not an answer\nno\n" # 1
175
 
                                      "info\nmore info\n" # 3
176
 
                                      "Maybe\n" # 2
177
 
                                      "foo\n")
178
 
        stdout = tests.StringIOWrapper()
179
 
        stderr = tests.StringIOWrapper()
180
 
        factory = _mod_ui_text.TextUIFactory(stdin, stdout, stderr)
181
 
        self.assertEqual(0, choose())
182
 
        self.assertEqual(1, choose())
183
 
        self.assertEqual(3, choose())
184
 
        self.assertEqual(1, choose())
185
 
        self.assertEqual(0, choose())
186
 
        self.assertEqual(1, choose())
187
 
        self.assertEqual(3, choose())
188
 
        self.assertEqual(2, choose())
189
 
        self.assertEqual("foo\n", factory.stdin.read())
190
 
        # stdin should be empty
191
 
        self.assertEqual('', factory.stdin.readline())
192
 
        # return None on EOF
193
 
        self.assertEqual(None, choose())
194
 
 
195
 
    def test_text_ui_choose_no_default(self):
196
 
        stdin = tests.StringIOWrapper(" \n" # no default, invalid!
197
 
                                      " yes \n" # 0
198
 
                                      "foo\n")
199
 
        stdout = tests.StringIOWrapper()
200
 
        stderr = tests.StringIOWrapper()
201
 
        factory = _mod_ui_text.TextUIFactory(stdin, stdout, stderr)
202
 
        self.assertEqual(0, factory.choose(u"", u"&Yes\n&No"))
203
 
        self.assertEqual("foo\n", factory.stdin.read())
 
128
        self.assertEqual(True, factory.get_boolean(""))
 
129
        self.assertEqual(False, factory.get_boolean(""))
 
130
        self.assertEqual(True, factory.get_boolean(""))
 
131
        self.assertEqual(False, factory.get_boolean(""))
 
132
        self.assertEqual(True, factory.get_boolean(""))
 
133
        self.assertEqual(False, factory.get_boolean(""))
 
134
        self.assertEqual("foo\n", factory.stdin.read())
 
135
        # stdin should be empty
 
136
        self.assertEqual('', factory.stdin.readline())
204
137
 
205
138
    def test_text_ui_get_integer(self):
206
139
        stdin = tests.StringIOWrapper(
210
143
        stdout = tests.StringIOWrapper()
211
144
        stderr = tests.StringIOWrapper()
212
145
        factory = _mod_ui_text.TextUIFactory(stdin, stdout, stderr)
213
 
        self.assertEqual(1, factory.get_integer(u""))
214
 
        self.assertEqual(-2, factory.get_integer(u""))
215
 
        self.assertEqual(42, factory.get_integer(u""))
 
146
        self.assertEqual(1, factory.get_integer(""))
 
147
        self.assertEqual(-2, factory.get_integer(""))
 
148
        self.assertEqual(42, factory.get_integer(""))
216
149
 
217
150
    def test_text_factory_prompt(self):
218
151
        # see <https://launchpad.net/bugs/365891>
219
152
        StringIO = tests.StringIOWrapper
220
153
        factory = _mod_ui_text.TextUIFactory(StringIO(), StringIO(), StringIO())
221
 
        factory.prompt(u'foo %2e')
 
154
        factory.prompt('foo %2e')
222
155
        self.assertEqual('', factory.stdout.getvalue())
223
156
        self.assertEqual('foo %2e', factory.stderr.getvalue())
224
157
 
225
158
    def test_text_factory_prompts_and_clears(self):
226
159
        # a get_boolean call should clear the pb before prompting
227
 
        out = TTYStringIO()
228
 
        self.overrideEnv('TERM', 'xterm')
 
160
        out = test_progress._TTYStringIO()
 
161
        os.environ['TERM'] = 'xterm'
229
162
        factory = _mod_ui_text.TextUIFactory(
230
163
            stdin=tests.StringIOWrapper("yada\ny\n"),
231
164
            stdout=out, stderr=out)
239
172
                         self.apply_redirected(None, factory.stdout,
240
173
                                               factory.stdout,
241
174
                                               factory.get_boolean,
242
 
                                               u"what do you want"))
 
175
                                               "what do you want"))
243
176
        output = out.getvalue()
244
177
        self.assertContainsRe(output,
245
178
            "| foo *\r\r  *\r*")
246
 
        self.assertContainsString(output,
247
 
            r"what do you want? ([y]es, [n]o): what do you want? ([y]es, [n]o): ")
 
179
        self.assertContainsRe(output,
 
180
            r"what do you want\? \[y/n\]: what do you want\? \[y/n\]: ")
248
181
        # stdin should have been totally consumed
249
182
        self.assertEqual('', factory.stdin.readline())
250
183
 
261
194
            pb.finished()
262
195
 
263
196
    def test_text_ui_getusername(self):
264
 
        ui = _mod_ui_text.TextUIFactory(None, None, None)
265
 
        ui.stdin = tests.StringIOWrapper('someuser\n\n')
266
 
        ui.stdout = tests.StringIOWrapper()
267
 
        ui.stderr = tests.StringIOWrapper()
268
 
        ui.stdout.encoding = 'utf8'
269
 
        self.assertEqual('someuser',
270
 
                         ui.get_username(u'Hello %(host)s', host='some'))
271
 
        self.assertEquals('Hello some: ', ui.stderr.getvalue())
272
 
        self.assertEquals('', ui.stdout.getvalue())
273
 
        self.assertEqual('', ui.get_username(u"Gebruiker"))
 
197
        factory = _mod_ui_text.TextUIFactory(None, None, None)
 
198
        factory.stdin = tests.StringIOWrapper("someuser\n\n")
 
199
        factory.stdout = tests.StringIOWrapper()
 
200
        factory.stderr = tests.StringIOWrapper()
 
201
        factory.stdout.encoding = "utf8"
 
202
        # there is no output from the base factory
 
203
        self.assertEqual("someuser",
 
204
                         factory.get_username('Hello %(host)s', host='some'))
 
205
        self.assertEquals("Hello some: ", factory.stderr.getvalue())
 
206
        self.assertEquals('', factory.stdout.getvalue())
 
207
        self.assertEqual("", factory.get_username("Gebruiker"))
274
208
        # stdin should be empty
275
 
        self.assertEqual('', ui.stdin.readline())
 
209
        self.assertEqual('', factory.stdin.readline())
276
210
 
277
211
    def test_text_ui_getusername_utf8(self):
278
 
        ui = _mod_ui_text.TextUIFactory(None, None, None)
279
 
        ui.stdin = tests.StringIOWrapper(u'someuser\u1234'.encode('utf8'))
280
 
        ui.stdout = tests.StringIOWrapper()
281
 
        ui.stderr = tests.StringIOWrapper()
 
212
        ui = tests.TestUIFactory(stdin=u'someuser\u1234'.encode('utf8'),
 
213
                                 stdout=tests.StringIOWrapper(),
 
214
                                 stderr=tests.StringIOWrapper())
282
215
        ui.stderr.encoding = ui.stdout.encoding = ui.stdin.encoding = "utf8"
283
 
        username = ui.get_username(u'Hello %(host)s', host=u'some\u1234')
284
 
        self.assertEquals(u"someuser\u1234", username)
285
 
        self.assertEquals(u"Hello some\u1234: ",
286
 
                          ui.stderr.getvalue().decode("utf8"))
287
 
        self.assertEquals('', ui.stdout.getvalue())
 
216
        pb = ui.nested_progress_bar()
 
217
        try:
 
218
            # there is no output from the base factory
 
219
            username = self.apply_redirected(ui.stdin, ui.stdout, ui.stderr,
 
220
                ui.get_username, u'Hello\u1234 %(host)s', host=u'some\u1234')
 
221
            self.assertEquals(u"someuser\u1234", username.decode('utf8'))
 
222
            self.assertEquals(u"Hello\u1234 some\u1234: ",
 
223
                              ui.stderr.getvalue().decode("utf8"))
 
224
            self.assertEquals('', ui.stdout.getvalue())
 
225
        finally:
 
226
            pb.finished()
288
227
 
289
228
    def test_quietness(self):
290
 
        self.overrideEnv('BZR_PROGRESS_BAR', 'text')
 
229
        os.environ['BZR_PROGRESS_BAR'] = 'text'
291
230
        ui_factory = _mod_ui_text.TextUIFactory(None,
292
 
            TTYStringIO(),
293
 
            TTYStringIO())
 
231
            test_progress._TTYStringIO(),
 
232
            test_progress._TTYStringIO())
294
233
        self.assertIsInstance(ui_factory._progress_view,
295
234
            _mod_ui_text.TextProgressView)
296
235
        ui_factory.be_quiet(True)
299
238
 
300
239
    def test_text_ui_show_user_warning(self):
301
240
        from bzrlib.repofmt.groupcompress_repo import RepositoryFormat2a
302
 
        from bzrlib.repofmt.knitpack_repo import RepositoryFormatKnitPack5
 
241
        from bzrlib.repofmt.pack_repo import RepositoryFormatKnitPack5
303
242
        err = StringIO()
304
243
        out = StringIO()
305
244
        ui = tests.TextUIFactory(stdin=None, stdout=out, stderr=err)
355
294
    def test_progress_construction(self):
356
295
        """TextUIFactory constructs the right progress view.
357
296
        """
 
297
        TTYStringIO = test_progress._TTYStringIO
358
298
        FileStringIO = tests.StringIOWrapper
359
299
        for (file_class, term, pb, expected_pb_class) in (
360
300
            # on an xterm, either use them or not as the user requests,
373
313
            # however, it can still be forced on
374
314
            (FileStringIO, 'dumb', 'text', _mod_ui_text.TextProgressView),
375
315
            ):
376
 
            self.overrideEnv('TERM', term)
377
 
            self.overrideEnv('BZR_PROGRESS_BAR', pb)
 
316
            os.environ['TERM'] = term
 
317
            if pb is None:
 
318
                if 'BZR_PROGRESS_BAR' in os.environ:
 
319
                    del os.environ['BZR_PROGRESS_BAR']
 
320
            else:
 
321
                os.environ['BZR_PROGRESS_BAR'] = pb
378
322
            stdin = file_class('')
379
323
            stderr = file_class()
380
324
            stdout = file_class()
387
331
 
388
332
    def test_text_ui_non_terminal(self):
389
333
        """Even on non-ttys, make_ui_for_terminal gives a text ui."""
390
 
        stdin = NonTTYStringIO('')
391
 
        stderr = NonTTYStringIO()
392
 
        stdout = NonTTYStringIO()
 
334
        stdin = test_progress._NonTTYStringIO('')
 
335
        stderr = test_progress._NonTTYStringIO()
 
336
        stdout = test_progress._NonTTYStringIO()
393
337
        for term_type in ['dumb', None, 'xterm']:
394
 
            self.overrideEnv('TERM', term_type)
 
338
            if term_type is None:
 
339
                del os.environ['TERM']
 
340
            else:
 
341
                os.environ['TERM'] = term_type
395
342
            uif = _mod_ui.make_ui_for_terminal(stdin, stdout, stderr)
396
343
            self.assertIsInstance(uif, _mod_ui_text.TextUIFactory,
397
344
                'TERM=%r' % (term_type,))
418
365
        self.assertRaises(
419
366
            NotImplementedError,
420
367
            self.apply_redirected,
421
 
            None, stdout, stdout, factory.get_boolean, u"foo")
 
368
            None, stdout, stdout, factory.get_boolean, "foo")
422
369
 
423
370
 
424
371
class TestUIFactoryTests(tests.TestCase):
437
384
 
438
385
    def test_canned_input_get_input(self):
439
386
        uif = _mod_ui.CannedInputUIFactory([True, 'mbp', 'password', 42])
440
 
        self.assertEqual(True, uif.get_boolean(u'Extra cheese?'))
441
 
        self.assertEqual('mbp', uif.get_username(u'Enter your user name'))
 
387
        self.assertEqual(True, uif.get_boolean('Extra cheese?'))
 
388
        self.assertEqual('mbp', uif.get_username('Enter your user name'))
442
389
        self.assertEqual('password',
443
 
                         uif.get_password(u'Password for %(host)s',
 
390
                         uif.get_password('Password for %(host)s',
444
391
                                          host='example.com'))
445
 
        self.assertEqual(42, uif.get_integer(u'And all that jazz ?'))
 
392
        self.assertEqual(42, uif.get_integer('And all that jazz ?'))
446
393
 
447
394
 
448
395
class TestBoolFromString(tests.TestCase):