~bzr-pqm/bzr/bzr.dev

« back to all changes in this revision

Viewing changes to bzrlib/tests/test_ui.py

  • Committer: Martin Pool
  • Date: 2010-06-02 05:03:31 UTC
  • mto: This revision was merged to the branch mainline in revision 5279.
  • Revision ID: mbp@canonical.com-20100602050331-n2p1qt8hfsahspnv
Correct more sloppy use of the term 'Linux'

Show diffs side-by-side

added added

removed removed

Lines of Context:
 
1
# Copyright (C) 2005-2010 Canonical Ltd
 
2
#
 
3
# This program is free software; you can redistribute it and/or modify
 
4
# it under the terms of the GNU General Public License as published by
 
5
# the Free Software Foundation; either version 2 of the License, or
 
6
# (at your option) any later version.
 
7
#
 
8
# This program is distributed in the hope that it will be useful,
 
9
# but WITHOUT ANY WARRANTY; without even the implied warranty of
 
10
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 
11
# GNU General Public License for more details.
 
12
#
 
13
# You should have received a copy of the GNU General Public License
 
14
# along with this program; if not, write to the Free Software
 
15
# Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
 
16
 
 
17
"""Tests for the bzrlib ui
 
18
"""
 
19
 
 
20
import os
 
21
import re
 
22
import time
 
23
 
 
24
from StringIO import StringIO
 
25
 
 
26
from bzrlib import (
 
27
    errors,
 
28
    remote,
 
29
    repository,
 
30
    tests,
 
31
    ui as _mod_ui,
 
32
    )
 
33
from bzrlib.symbol_versioning import (
 
34
    deprecated_in,
 
35
    )
 
36
from bzrlib.tests import test_progress
 
37
from bzrlib.ui import text as _mod_ui_text
 
38
 
 
39
 
 
40
class TestTextUIFactory(tests.TestCase):
 
41
 
 
42
    def test_text_factory_ascii_password(self):
 
43
        ui = tests.TestUIFactory(stdin='secret\n',
 
44
                                 stdout=tests.StringIOWrapper(),
 
45
                                 stderr=tests.StringIOWrapper())
 
46
        pb = ui.nested_progress_bar()
 
47
        try:
 
48
            self.assertEqual('secret',
 
49
                             self.apply_redirected(ui.stdin, ui.stdout,
 
50
                                                   ui.stderr,
 
51
                                                   ui.get_password))
 
52
            # ': ' is appended to prompt
 
53
            self.assertEqual(': ', ui.stderr.getvalue())
 
54
            self.assertEqual('', ui.stdout.readline())
 
55
            # stdin should be empty
 
56
            self.assertEqual('', ui.stdin.readline())
 
57
        finally:
 
58
            pb.finished()
 
59
 
 
60
    def test_text_factory_utf8_password(self):
 
61
        """Test an utf8 password.
 
62
 
 
63
        We can't predict what encoding users will have for stdin, so we force
 
64
        it to utf8 to test that we transport the password correctly.
 
65
        """
 
66
        ui = tests.TestUIFactory(stdin=u'baz\u1234'.encode('utf8'),
 
67
                                 stdout=tests.StringIOWrapper(),
 
68
                                 stderr=tests.StringIOWrapper())
 
69
        ui.stderr.encoding = ui.stdout.encoding = ui.stdin.encoding = 'utf8'
 
70
        pb = ui.nested_progress_bar()
 
71
        try:
 
72
            password = self.apply_redirected(ui.stdin, ui.stdout, ui.stderr,
 
73
                                             ui.get_password,
 
74
                                             u'Hello \u1234 %(user)s',
 
75
                                             user=u'some\u1234')
 
76
            # We use StringIO objects, we need to decode them
 
77
            self.assertEqual(u'baz\u1234', password.decode('utf8'))
 
78
            self.assertEqual(u'Hello \u1234 some\u1234: ',
 
79
                             ui.stderr.getvalue().decode('utf8'))
 
80
            # stdin and stdout should be empty
 
81
            self.assertEqual('', ui.stdin.readline())
 
82
            self.assertEqual('', ui.stdout.readline())
 
83
        finally:
 
84
            pb.finished()
 
85
 
 
86
    def test_progress_note(self):
 
87
        stderr = tests.StringIOWrapper()
 
88
        stdout = tests.StringIOWrapper()
 
89
        ui_factory = _mod_ui_text.TextUIFactory(stdin=tests.StringIOWrapper(''),
 
90
                                                stderr=stderr,
 
91
                                                stdout=stdout)
 
92
        pb = ui_factory.nested_progress_bar()
 
93
        try:
 
94
            result = self.applyDeprecated(deprecated_in((2, 1, 0)),
 
95
                pb.note,
 
96
                't')
 
97
            self.assertEqual(None, result)
 
98
            self.assertEqual("t\n", stdout.getvalue())
 
99
            # Since there was no update() call, there should be no clear() call
 
100
            self.failIf(re.search(r'^\r {10,}\r$',
 
101
                                  stderr.getvalue()) is not None,
 
102
                        'We cleared the stderr without anything to put there')
 
103
        finally:
 
104
            pb.finished()
 
105
 
 
106
    def test_progress_note_clears(self):
 
107
        stderr = test_progress._TTYStringIO()
 
108
        stdout = test_progress._TTYStringIO()
 
109
        # so that we get a TextProgressBar
 
110
        os.environ['TERM'] = 'xterm'
 
111
        ui_factory = _mod_ui_text.TextUIFactory(
 
112
            stdin=tests.StringIOWrapper(''),
 
113
            stdout=stdout, stderr=stderr)
 
114
        self.assertIsInstance(ui_factory._progress_view,
 
115
                              _mod_ui_text.TextProgressView)
 
116
        pb = ui_factory.nested_progress_bar()
 
117
        try:
 
118
            # Create a progress update that isn't throttled
 
119
            pb.update('x', 1, 1)
 
120
            result = self.applyDeprecated(deprecated_in((2, 1, 0)),
 
121
                pb.note, 't')
 
122
            self.assertEqual(None, result)
 
123
            self.assertEqual("t\n", stdout.getvalue())
 
124
            # the exact contents will depend on the terminal width and we don't
 
125
            # care about that right now - but you're probably running it on at
 
126
            # least a 10-character wide terminal :)
 
127
            self.assertContainsRe(stderr.getvalue(), r'\r {10,}\r$')
 
128
        finally:
 
129
            pb.finished()
 
130
 
 
131
    def test_text_ui_get_boolean(self):
 
132
        stdin = tests.StringIOWrapper("y\n" # True
 
133
                                      "n\n" # False
 
134
                                      "yes with garbage\nY\n" # True
 
135
                                      "not an answer\nno\n" # False
 
136
                                      "I'm sure!\nyes\n" # True
 
137
                                      "NO\n" # False
 
138
                                      "foo\n")
 
139
        stdout = tests.StringIOWrapper()
 
140
        stderr = tests.StringIOWrapper()
 
141
        factory = _mod_ui_text.TextUIFactory(stdin, stdout, stderr)
 
142
        self.assertEqual(True, factory.get_boolean(""))
 
143
        self.assertEqual(False, factory.get_boolean(""))
 
144
        self.assertEqual(True, factory.get_boolean(""))
 
145
        self.assertEqual(False, factory.get_boolean(""))
 
146
        self.assertEqual(True, factory.get_boolean(""))
 
147
        self.assertEqual(False, factory.get_boolean(""))
 
148
        self.assertEqual("foo\n", factory.stdin.read())
 
149
        # stdin should be empty
 
150
        self.assertEqual('', factory.stdin.readline())
 
151
 
 
152
    def test_text_ui_get_integer(self):
 
153
        stdin = tests.StringIOWrapper(
 
154
            "1\n"
 
155
            "  -2  \n"
 
156
            "hmmm\nwhat else ?\nCome on\nok 42\n4.24\n42\n")
 
157
        stdout = tests.StringIOWrapper()
 
158
        stderr = tests.StringIOWrapper()
 
159
        factory = _mod_ui_text.TextUIFactory(stdin, stdout, stderr)
 
160
        self.assertEqual(1, factory.get_integer(""))
 
161
        self.assertEqual(-2, factory.get_integer(""))
 
162
        self.assertEqual(42, factory.get_integer(""))
 
163
 
 
164
    def test_text_factory_prompt(self):
 
165
        # see <https://launchpad.net/bugs/365891>
 
166
        StringIO = tests.StringIOWrapper
 
167
        factory = _mod_ui_text.TextUIFactory(StringIO(), StringIO(), StringIO())
 
168
        factory.prompt('foo %2e')
 
169
        self.assertEqual('', factory.stdout.getvalue())
 
170
        self.assertEqual('foo %2e', factory.stderr.getvalue())
 
171
 
 
172
    def test_text_factory_prompts_and_clears(self):
 
173
        # a get_boolean call should clear the pb before prompting
 
174
        out = test_progress._TTYStringIO()
 
175
        os.environ['TERM'] = 'xterm'
 
176
        factory = _mod_ui_text.TextUIFactory(
 
177
            stdin=tests.StringIOWrapper("yada\ny\n"),
 
178
            stdout=out, stderr=out)
 
179
        pb = factory.nested_progress_bar()
 
180
        pb.show_bar = False
 
181
        pb.show_spinner = False
 
182
        pb.show_count = False
 
183
        pb.update("foo", 0, 1)
 
184
        self.assertEqual(True,
 
185
                         self.apply_redirected(None, factory.stdout,
 
186
                                               factory.stdout,
 
187
                                               factory.get_boolean,
 
188
                                               "what do you want"))
 
189
        output = out.getvalue()
 
190
        self.assertContainsRe(factory.stdout.getvalue(),
 
191
            "foo *\r\r  *\r*")
 
192
        self.assertContainsRe(factory.stdout.getvalue(),
 
193
            r"what do you want\? \[y/n\]: what do you want\? \[y/n\]: ")
 
194
        # stdin should have been totally consumed
 
195
        self.assertEqual('', factory.stdin.readline())
 
196
 
 
197
    def test_text_tick_after_update(self):
 
198
        ui_factory = _mod_ui_text.TextUIFactory(stdout=tests.StringIOWrapper(),
 
199
                                                stderr=tests.StringIOWrapper())
 
200
        pb = ui_factory.nested_progress_bar()
 
201
        try:
 
202
            pb.update('task', 0, 3)
 
203
            # Reset the clock, so that it actually tries to repaint itself
 
204
            ui_factory._progress_view._last_repaint = time.time() - 1.0
 
205
            pb.tick()
 
206
        finally:
 
207
            pb.finished()
 
208
 
 
209
    def test_text_ui_getusername(self):
 
210
        factory = _mod_ui_text.TextUIFactory(None, None, None)
 
211
        factory.stdin = tests.StringIOWrapper("someuser\n\n")
 
212
        factory.stdout = tests.StringIOWrapper()
 
213
        factory.stderr = tests.StringIOWrapper()
 
214
        factory.stdout.encoding = "utf8"
 
215
        # there is no output from the base factory
 
216
        self.assertEqual("someuser",
 
217
                         factory.get_username('Hello %(host)s', host='some'))
 
218
        self.assertEquals("Hello some: ", factory.stderr.getvalue())
 
219
        self.assertEquals('', factory.stdout.getvalue())
 
220
        self.assertEqual("", factory.get_username("Gebruiker"))
 
221
        # stdin should be empty
 
222
        self.assertEqual('', factory.stdin.readline())
 
223
 
 
224
    def test_text_ui_getusername_utf8(self):
 
225
        ui = tests.TestUIFactory(stdin=u'someuser\u1234'.encode('utf8'),
 
226
                                 stdout=tests.StringIOWrapper(),
 
227
                                 stderr=tests.StringIOWrapper())
 
228
        ui.stderr.encoding = ui.stdout.encoding = ui.stdin.encoding = "utf8"
 
229
        pb = ui.nested_progress_bar()
 
230
        try:
 
231
            # there is no output from the base factory
 
232
            username = self.apply_redirected(ui.stdin, ui.stdout, ui.stderr,
 
233
                ui.get_username, u'Hello\u1234 %(host)s', host=u'some\u1234')
 
234
            self.assertEquals(u"someuser\u1234", username.decode('utf8'))
 
235
            self.assertEquals(u"Hello\u1234 some\u1234: ",
 
236
                              ui.stderr.getvalue().decode("utf8"))
 
237
            self.assertEquals('', ui.stdout.getvalue())
 
238
        finally:
 
239
            pb.finished()
 
240
 
 
241
    def test_quietness(self):
 
242
        os.environ['BZR_PROGRESS_BAR'] = 'text'
 
243
        ui_factory = _mod_ui_text.TextUIFactory(None,
 
244
            test_progress._TTYStringIO(),
 
245
            test_progress._TTYStringIO())
 
246
        self.assertIsInstance(ui_factory._progress_view,
 
247
            _mod_ui_text.TextProgressView)
 
248
        ui_factory.be_quiet(True)
 
249
        self.assertIsInstance(ui_factory._progress_view,
 
250
            _mod_ui_text.NullProgressView)
 
251
 
 
252
    def test_text_ui_show_user_warning(self):
 
253
        from bzrlib.repofmt.groupcompress_repo import RepositoryFormat2a
 
254
        from bzrlib.repofmt.pack_repo import RepositoryFormatKnitPack5
 
255
        err = StringIO()
 
256
        out = StringIO()
 
257
        ui = tests.TextUIFactory(stdin=None, stdout=out, stderr=err)
 
258
        remote_fmt = remote.RemoteRepositoryFormat()
 
259
        remote_fmt._network_name = RepositoryFormatKnitPack5().network_name()
 
260
        ui.show_user_warning('cross_format_fetch', from_format=RepositoryFormat2a(),
 
261
            to_format=remote_fmt)
 
262
        self.assertEquals('', out.getvalue())
 
263
        self.assertEquals("Doing on-the-fly conversion from RepositoryFormat2a() to "
 
264
            "RemoteRepositoryFormat(_network_name='Bazaar RepositoryFormatKnitPack5 "
 
265
            "(bzr 1.6)\\n').\nThis may take some time. Upgrade the repositories to "
 
266
            "the same format for better performance.\n",
 
267
            err.getvalue())
 
268
        # and now with it suppressed please
 
269
        err = StringIO()
 
270
        out = StringIO()
 
271
        ui = tests.TextUIFactory(stdin=None, stdout=out, stderr=err)
 
272
        ui.suppressed_warnings.add('cross_format_fetch')
 
273
        ui.show_user_warning('cross_format_fetch', from_format=RepositoryFormat2a(),
 
274
            to_format=remote_fmt)
 
275
        self.assertEquals('', out.getvalue())
 
276
        self.assertEquals('', err.getvalue())
 
277
 
 
278
 
 
279
class TestTextUIOutputStream(tests.TestCase):
 
280
    """Tests for output stream that synchronizes with progress bar."""
 
281
 
 
282
    def test_output_clears_terminal(self):
 
283
        stdout = tests.StringIOWrapper()
 
284
        stderr = tests.StringIOWrapper()
 
285
        clear_calls = []
 
286
 
 
287
        uif =  _mod_ui_text.TextUIFactory(None, stdout, stderr)
 
288
        uif.clear_term = lambda: clear_calls.append('clear')
 
289
 
 
290
        stream = _mod_ui_text.TextUIOutputStream(uif, uif.stdout)
 
291
        stream.write("Hello world!\n")
 
292
        stream.write("there's more...\n")
 
293
        stream.writelines(["1\n", "2\n", "3\n"])
 
294
 
 
295
        self.assertEqual(stdout.getvalue(),
 
296
            "Hello world!\n"
 
297
            "there's more...\n"
 
298
            "1\n2\n3\n")
 
299
        self.assertEqual(['clear', 'clear', 'clear'],
 
300
            clear_calls)
 
301
 
 
302
        stream.flush()
 
303
 
 
304
 
 
305
class UITests(tests.TestCase):
 
306
 
 
307
    def test_progress_construction(self):
 
308
        """TextUIFactory constructs the right progress view.
 
309
        """
 
310
        TTYStringIO = test_progress._TTYStringIO
 
311
        FileStringIO = tests.StringIOWrapper
 
312
        for (file_class, term, pb, expected_pb_class) in (
 
313
            # on an xterm, either use them or not as the user requests,
 
314
            # otherwise default on
 
315
            (TTYStringIO, 'xterm', 'none', _mod_ui_text.NullProgressView),
 
316
            (TTYStringIO, 'xterm', 'text', _mod_ui_text.TextProgressView),
 
317
            (TTYStringIO, 'xterm', None, _mod_ui_text.TextProgressView),
 
318
            # on a dumb terminal, again if there's explicit configuration do
 
319
            # it, otherwise default off
 
320
            (TTYStringIO, 'dumb', 'none', _mod_ui_text.NullProgressView),
 
321
            (TTYStringIO, 'dumb', 'text', _mod_ui_text.TextProgressView),
 
322
            (TTYStringIO, 'dumb', None, _mod_ui_text.NullProgressView),
 
323
            # on a non-tty terminal, it's null regardless of $TERM
 
324
            (FileStringIO, 'xterm', None, _mod_ui_text.NullProgressView),
 
325
            (FileStringIO, 'dumb', None, _mod_ui_text.NullProgressView),
 
326
            # however, it can still be forced on
 
327
            (FileStringIO, 'dumb', 'text', _mod_ui_text.TextProgressView),
 
328
            ):
 
329
            os.environ['TERM'] = term
 
330
            if pb is None:
 
331
                if 'BZR_PROGRESS_BAR' in os.environ:
 
332
                    del os.environ['BZR_PROGRESS_BAR']
 
333
            else:
 
334
                os.environ['BZR_PROGRESS_BAR'] = pb
 
335
            stdin = file_class('')
 
336
            stderr = file_class()
 
337
            stdout = file_class()
 
338
            uif = _mod_ui.make_ui_for_terminal(stdin, stdout, stderr)
 
339
            self.assertIsInstance(uif, _mod_ui_text.TextUIFactory,
 
340
                "TERM=%s BZR_PROGRESS_BAR=%s uif=%r" % (term, pb, uif,))
 
341
            self.assertIsInstance(uif.make_progress_view(),
 
342
                expected_pb_class,
 
343
                "TERM=%s BZR_PROGRESS_BAR=%s uif=%r" % (term, pb, uif,))
 
344
 
 
345
    def test_text_ui_non_terminal(self):
 
346
        """Even on non-ttys, make_ui_for_terminal gives a text ui."""
 
347
        stdin = test_progress._NonTTYStringIO('')
 
348
        stderr = test_progress._NonTTYStringIO()
 
349
        stdout = test_progress._NonTTYStringIO()
 
350
        for term_type in ['dumb', None, 'xterm']:
 
351
            if term_type is None:
 
352
                del os.environ['TERM']
 
353
            else:
 
354
                os.environ['TERM'] = term_type
 
355
            uif = _mod_ui.make_ui_for_terminal(stdin, stdout, stderr)
 
356
            self.assertIsInstance(uif, _mod_ui_text.TextUIFactory,
 
357
                'TERM=%r' % (term_type,))
 
358
 
 
359
 
 
360
class SilentUITests(tests.TestCase):
 
361
 
 
362
    def test_silent_factory_get_password(self):
 
363
        # A silent factory that can't do user interaction can't get a
 
364
        # password.  Possibly it should raise a more specific error but it
 
365
        # can't succeed.
 
366
        ui = _mod_ui.SilentUIFactory()
 
367
        stdout = tests.StringIOWrapper()
 
368
        self.assertRaises(
 
369
            NotImplementedError,
 
370
            self.apply_redirected,
 
371
            None, stdout, stdout, ui.get_password)
 
372
        # and it didn't write anything out either
 
373
        self.assertEqual('', stdout.getvalue())
 
374
 
 
375
    def test_silent_ui_getbool(self):
 
376
        factory = _mod_ui.SilentUIFactory()
 
377
        stdout = tests.StringIOWrapper()
 
378
        self.assertRaises(
 
379
            NotImplementedError,
 
380
            self.apply_redirected,
 
381
            None, stdout, stdout, factory.get_boolean, "foo")
 
382
 
 
383
 
 
384
class TestUIFactoryTests(tests.TestCase):
 
385
 
 
386
    def test_test_ui_factory_progress(self):
 
387
        # there's no output; we just want to make sure this doesn't crash -
 
388
        # see https://bugs.launchpad.net/bzr/+bug/408201
 
389
        ui = tests.TestUIFactory()
 
390
        pb = ui.nested_progress_bar()
 
391
        pb.update('hello')
 
392
        pb.tick()
 
393
        pb.finished()
 
394
 
 
395
 
 
396
class CannedInputUIFactoryTests(tests.TestCase):
 
397
 
 
398
    def test_canned_input_get_input(self):
 
399
        uif = _mod_ui.CannedInputUIFactory([True, 'mbp', 'password', 42])
 
400
        self.assertEqual(True, uif.get_boolean('Extra cheese?'))
 
401
        self.assertEqual('mbp', uif.get_username('Enter your user name'))
 
402
        self.assertEqual('password',
 
403
                         uif.get_password('Password for %(host)s',
 
404
                                          host='example.com'))
 
405
        self.assertEqual(42, uif.get_integer('And all that jazz ?'))
 
406
 
 
407
 
 
408
class TestBoolFromString(tests.TestCase):
 
409
 
 
410
    def assertIsTrue(self, s, accepted_values=None):
 
411
        res = _mod_ui.bool_from_string(s, accepted_values=accepted_values)
 
412
        self.assertEquals(True, res)
 
413
 
 
414
    def assertIsFalse(self, s, accepted_values=None):
 
415
        res = _mod_ui.bool_from_string(s, accepted_values=accepted_values)
 
416
        self.assertEquals(False, res)
 
417
 
 
418
    def assertIsNone(self, s, accepted_values=None):
 
419
        res = _mod_ui.bool_from_string(s, accepted_values=accepted_values)
 
420
        self.assertIs(None, res)
 
421
 
 
422
    def test_know_valid_values(self):
 
423
        self.assertIsTrue('true')
 
424
        self.assertIsFalse('false')
 
425
        self.assertIsTrue('1')
 
426
        self.assertIsFalse('0')
 
427
        self.assertIsTrue('on')
 
428
        self.assertIsFalse('off')
 
429
        self.assertIsTrue('yes')
 
430
        self.assertIsFalse('no')
 
431
        self.assertIsTrue('y')
 
432
        self.assertIsFalse('n')
 
433
        # Also try some case variations
 
434
        self.assertIsTrue('True')
 
435
        self.assertIsFalse('False')
 
436
        self.assertIsTrue('On')
 
437
        self.assertIsFalse('Off')
 
438
        self.assertIsTrue('ON')
 
439
        self.assertIsFalse('OFF')
 
440
        self.assertIsTrue('oN')
 
441
        self.assertIsFalse('oFf')
 
442
 
 
443
    def test_invalid_values(self):
 
444
        self.assertIsNone(None)
 
445
        self.assertIsNone('doubt')
 
446
        self.assertIsNone('frue')
 
447
        self.assertIsNone('talse')
 
448
        self.assertIsNone('42')
 
449
 
 
450
    def test_provided_values(self):
 
451
        av = dict(y=True, n=False, yes=True, no=False)
 
452
        self.assertIsTrue('y', av)
 
453
        self.assertIsTrue('Y', av)
 
454
        self.assertIsTrue('Yes', av)
 
455
        self.assertIsFalse('n', av)
 
456
        self.assertIsFalse('N', av)
 
457
        self.assertIsFalse('No', av)
 
458
        self.assertIsNone('1', av)
 
459
        self.assertIsNone('0', av)
 
460
        self.assertIsNone('on', av)
 
461
        self.assertIsNone('off', av)