~bzr-pqm/bzr/bzr.dev

« back to all changes in this revision

Viewing changes to bzrlib/tests/test_ui.py

  • Committer: Vincent Ladeuil
  • Date: 2010-07-07 15:03:14 UTC
  • mto: (5355.1.1 integration)
  • mto: This revision was merged to the branch mainline in revision 5356.
  • Revision ID: v.ladeuil+lp@free.fr-20100707150314-7i5po3dwg8umiv8x
Fix remaining sphinx_conf references.

Show diffs side-by-side

added added

removed removed

Lines of Context:
1
 
# Copyright (C) 2005, 2008, 2009 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
18
18
"""
19
19
 
20
20
import os
21
 
from StringIO import StringIO
22
21
import re
23
 
import sys
24
22
import time
25
23
 
 
24
from StringIO import StringIO
 
25
 
26
26
from bzrlib import (
 
27
    config,
27
28
    errors,
 
29
    remote,
 
30
    repository,
28
31
    tests,
29
32
    ui as _mod_ui,
30
33
    )
32
35
    deprecated_in,
33
36
    )
34
37
from bzrlib.tests import (
35
 
    TestCase,
36
 
    TestUIFactory,
37
 
    StringIOWrapper,
38
 
    )
39
 
from bzrlib.tests.test_progress import (
40
 
    _NonTTYStringIO,
41
 
    _TTYStringIO,
42
 
    )
43
 
from bzrlib.ui import (
44
 
    CannedInputUIFactory,
45
 
    CLIUIFactory,
46
 
    SilentUIFactory,
47
 
    UIFactory,
48
 
    make_ui_for_terminal,
49
 
    )
50
 
from bzrlib.ui.text import (
51
 
    NullProgressView,
52
 
    TextProgressView,
53
 
    TextUIFactory,
54
 
    )
55
 
 
56
 
 
57
 
class UITests(tests.TestCase):
 
38
    fixtures,
 
39
    test_progress,
 
40
    )
 
41
from bzrlib.ui import text as _mod_ui_text
 
42
 
 
43
 
 
44
class TestUIConfiguration(tests.TestCaseWithTransport):
 
45
 
 
46
    def test_output_encoding_configuration(self):
 
47
        enc = fixtures.generate_unicode_encodings().next()
 
48
        config.GlobalConfig().set_user_option('output_encoding',
 
49
            enc)
 
50
        ui = tests.TestUIFactory(stdin=None,
 
51
            stdout=tests.StringIOWrapper(),
 
52
            stderr=tests.StringIOWrapper())
 
53
        os = ui.make_output_stream()
 
54
        self.assertEquals(os.encoding, enc)
 
55
 
 
56
 
 
57
class TestTextUIFactory(tests.TestCase):
58
58
 
59
59
    def test_text_factory_ascii_password(self):
60
60
        ui = tests.TestUIFactory(stdin='secret\n',
100
100
        finally:
101
101
            pb.finished()
102
102
 
103
 
    def test_progress_construction(self):
104
 
        """TextUIFactory constructs the right progress view.
105
 
        """
106
 
        for (file_class, term, pb, expected_pb_class) in (
107
 
            # on an xterm, either use them or not as the user requests,
108
 
            # otherwise default on
109
 
            (_TTYStringIO, 'xterm', 'none', NullProgressView),
110
 
            (_TTYStringIO, 'xterm', 'text', TextProgressView),
111
 
            (_TTYStringIO, 'xterm', None, TextProgressView),
112
 
            # on a dumb terminal, again if there's explicit configuration do
113
 
            # it, otherwise default off
114
 
            (_TTYStringIO, 'dumb', 'none', NullProgressView),
115
 
            (_TTYStringIO, 'dumb', 'text', TextProgressView),
116
 
            (_TTYStringIO, 'dumb', None, NullProgressView),
117
 
            # on a non-tty terminal, it's null regardless of $TERM
118
 
            (StringIO, 'xterm', None, NullProgressView),
119
 
            (StringIO, 'dumb', None, NullProgressView),
120
 
            # however, it can still be forced on
121
 
            (StringIO, 'dumb', 'text', TextProgressView),
122
 
            ):
123
 
            os.environ['TERM'] = term
124
 
            if pb is None:
125
 
                if 'BZR_PROGRESS_BAR' in os.environ:
126
 
                    del os.environ['BZR_PROGRESS_BAR']
127
 
            else:
128
 
                os.environ['BZR_PROGRESS_BAR'] = pb
129
 
            stdin = file_class('')
130
 
            stderr = file_class()
131
 
            stdout = file_class()
132
 
            uif = make_ui_for_terminal(stdin, stdout, stderr)
133
 
            self.assertIsInstance(uif, TextUIFactory,
134
 
                "TERM=%s BZR_PROGRESS_BAR=%s uif=%r" % (term, pb, uif,))
135
 
            self.assertIsInstance(uif.make_progress_view(),
136
 
                expected_pb_class,
137
 
                "TERM=%s BZR_PROGRESS_BAR=%s uif=%r" % (term, pb, uif,))
138
 
 
139
 
    def test_text_ui_non_terminal(self):
140
 
        """Even on non-ttys, make_ui_for_terminal gives a text ui."""
141
 
        stdin = _NonTTYStringIO('')
142
 
        stderr = _NonTTYStringIO()
143
 
        stdout = _NonTTYStringIO()
144
 
        for term_type in ['dumb', None, 'xterm']:
145
 
            if term_type is None:
146
 
                del os.environ['TERM']
147
 
            else:
148
 
                os.environ['TERM'] = term_type
149
 
            uif = make_ui_for_terminal(stdin, stdout, stderr)
150
 
            self.assertIsInstance(uif, TextUIFactory,
151
 
                'TERM=%r' % (term_type,))
152
 
 
153
103
    def test_progress_note(self):
154
 
        stderr = StringIO()
155
 
        stdout = StringIO()
156
 
        ui_factory = TextUIFactory(stdin=StringIO(''),
157
 
            stderr=stderr,
158
 
            stdout=stdout)
 
104
        stderr = tests.StringIOWrapper()
 
105
        stdout = tests.StringIOWrapper()
 
106
        ui_factory = _mod_ui_text.TextUIFactory(stdin=tests.StringIOWrapper(''),
 
107
                                                stderr=stderr,
 
108
                                                stdout=stdout)
159
109
        pb = ui_factory.nested_progress_bar()
160
110
        try:
161
 
            result = pb.note('t')
 
111
            result = self.applyDeprecated(deprecated_in((2, 1, 0)),
 
112
                pb.note,
 
113
                't')
162
114
            self.assertEqual(None, result)
163
115
            self.assertEqual("t\n", stdout.getvalue())
164
116
            # Since there was no update() call, there should be no clear() call
169
121
            pb.finished()
170
122
 
171
123
    def test_progress_note_clears(self):
172
 
        stderr = _TTYStringIO()
173
 
        stdout = _TTYStringIO()
 
124
        stderr = test_progress._TTYStringIO()
 
125
        stdout = test_progress._TTYStringIO()
174
126
        # so that we get a TextProgressBar
175
127
        os.environ['TERM'] = 'xterm'
176
 
        ui_factory = TextUIFactory(
177
 
            stdin=StringIO(''),
 
128
        ui_factory = _mod_ui_text.TextUIFactory(
 
129
            stdin=tests.StringIOWrapper(''),
178
130
            stdout=stdout, stderr=stderr)
179
131
        self.assertIsInstance(ui_factory._progress_view,
180
 
            TextProgressView)
 
132
                              _mod_ui_text.TextProgressView)
181
133
        pb = ui_factory.nested_progress_bar()
182
134
        try:
183
135
            # Create a progress update that isn't throttled
184
136
            pb.update('x', 1, 1)
185
 
            result = pb.note('t')
 
137
            result = self.applyDeprecated(deprecated_in((2, 1, 0)),
 
138
                pb.note, 't')
186
139
            self.assertEqual(None, result)
187
140
            self.assertEqual("t\n", stdout.getvalue())
188
141
            # the exact contents will depend on the terminal width and we don't
192
145
        finally:
193
146
            pb.finished()
194
147
 
195
 
    def test_progress_nested(self):
196
 
        # test factory based nested and popping.
197
 
        ui = TextUIFactory(None, None, None)
198
 
        pb1 = ui.nested_progress_bar()
199
 
        pb2 = ui.nested_progress_bar()
200
 
        # You do get a warning if the outermost progress bar wasn't finished
201
 
        # first - it's not clear if this is really useful or if it should just
202
 
        # become orphaned -- mbp 20090120
203
 
        warnings, _ = self.callCatchWarnings(pb1.finished)
204
 
        if len(warnings) != 1:
205
 
            self.fail("unexpected warnings: %r" % (warnings,))
206
 
        pb2.finished()
207
 
        pb1.finished()
208
 
 
209
148
    def test_text_ui_get_boolean(self):
210
 
        stdin = StringIO("y\n" # True
211
 
                         "n\n" # False
212
 
                         "yes with garbage\nY\n" # True
213
 
                         "not an answer\nno\n" # False
214
 
                         "I'm sure!\nyes\n" # True
215
 
                         "NO\n" # False
216
 
                         "foo\n")
217
 
        stdout = StringIO()
218
 
        stderr = StringIO()
219
 
        factory = TextUIFactory(stdin, stdout, stderr)
 
149
        stdin = tests.StringIOWrapper("y\n" # True
 
150
                                      "n\n" # False
 
151
                                      "yes with garbage\nY\n" # True
 
152
                                      "not an answer\nno\n" # False
 
153
                                      "I'm sure!\nyes\n" # True
 
154
                                      "NO\n" # False
 
155
                                      "foo\n")
 
156
        stdout = tests.StringIOWrapper()
 
157
        stderr = tests.StringIOWrapper()
 
158
        factory = _mod_ui_text.TextUIFactory(stdin, stdout, stderr)
220
159
        self.assertEqual(True, factory.get_boolean(""))
221
160
        self.assertEqual(False, factory.get_boolean(""))
222
161
        self.assertEqual(True, factory.get_boolean(""))
227
166
        # stdin should be empty
228
167
        self.assertEqual('', factory.stdin.readline())
229
168
 
 
169
    def test_text_ui_get_integer(self):
 
170
        stdin = tests.StringIOWrapper(
 
171
            "1\n"
 
172
            "  -2  \n"
 
173
            "hmmm\nwhat else ?\nCome on\nok 42\n4.24\n42\n")
 
174
        stdout = tests.StringIOWrapper()
 
175
        stderr = tests.StringIOWrapper()
 
176
        factory = _mod_ui_text.TextUIFactory(stdin, stdout, stderr)
 
177
        self.assertEqual(1, factory.get_integer(""))
 
178
        self.assertEqual(-2, factory.get_integer(""))
 
179
        self.assertEqual(42, factory.get_integer(""))
 
180
 
230
181
    def test_text_factory_prompt(self):
231
182
        # see <https://launchpad.net/bugs/365891>
232
 
        factory = TextUIFactory(StringIO(), StringIO(), StringIO())
 
183
        StringIO = tests.StringIOWrapper
 
184
        factory = _mod_ui_text.TextUIFactory(StringIO(), StringIO(), StringIO())
233
185
        factory.prompt('foo %2e')
234
186
        self.assertEqual('', factory.stdout.getvalue())
235
187
        self.assertEqual('foo %2e', factory.stderr.getvalue())
236
188
 
237
189
    def test_text_factory_prompts_and_clears(self):
238
190
        # a get_boolean call should clear the pb before prompting
239
 
        out = _TTYStringIO()
 
191
        out = test_progress._TTYStringIO()
240
192
        os.environ['TERM'] = 'xterm'
241
 
        factory = TextUIFactory(stdin=StringIO("yada\ny\n"), stdout=out, stderr=out)
 
193
        factory = _mod_ui_text.TextUIFactory(
 
194
            stdin=tests.StringIOWrapper("yada\ny\n"),
 
195
            stdout=out, stderr=out)
242
196
        pb = factory.nested_progress_bar()
243
197
        pb.show_bar = False
244
198
        pb.show_spinner = False
258
212
        self.assertEqual('', factory.stdin.readline())
259
213
 
260
214
    def test_text_tick_after_update(self):
261
 
        ui_factory = TextUIFactory(stdout=StringIO(), stderr=StringIO())
 
215
        ui_factory = _mod_ui_text.TextUIFactory(stdout=tests.StringIOWrapper(),
 
216
                                                stderr=tests.StringIOWrapper())
262
217
        pb = ui_factory.nested_progress_bar()
263
218
        try:
264
219
            pb.update('task', 0, 3)
269
224
            pb.finished()
270
225
 
271
226
    def test_text_ui_getusername(self):
272
 
        factory = TextUIFactory(None, None, None)
273
 
        factory.stdin = StringIO("someuser\n\n")
274
 
        factory.stdout = StringIO()
275
 
        factory.stderr = StringIO()
 
227
        factory = _mod_ui_text.TextUIFactory(None, None, None)
 
228
        factory.stdin = tests.StringIOWrapper("someuser\n\n")
 
229
        factory.stdout = tests.StringIOWrapper()
 
230
        factory.stderr = tests.StringIOWrapper()
276
231
        factory.stdout.encoding = "utf8"
277
232
        # there is no output from the base factory
278
233
        self.assertEqual("someuser",
300
255
        finally:
301
256
            pb.finished()
302
257
 
303
 
 
304
 
class CLIUITests(TestCase):
305
 
 
306
 
    def test_cli_factory_deprecated(self):
307
 
        uif = self.applyDeprecated(deprecated_in((1, 18, 0)),
308
 
            CLIUIFactory,
309
 
            StringIO(), StringIO(), StringIO())
310
 
        self.assertIsInstance(uif, UIFactory)
311
 
 
312
 
 
313
 
class SilentUITests(TestCase):
 
258
    def test_quietness(self):
 
259
        os.environ['BZR_PROGRESS_BAR'] = 'text'
 
260
        ui_factory = _mod_ui_text.TextUIFactory(None,
 
261
            test_progress._TTYStringIO(),
 
262
            test_progress._TTYStringIO())
 
263
        self.assertIsInstance(ui_factory._progress_view,
 
264
            _mod_ui_text.TextProgressView)
 
265
        ui_factory.be_quiet(True)
 
266
        self.assertIsInstance(ui_factory._progress_view,
 
267
            _mod_ui_text.NullProgressView)
 
268
 
 
269
    def test_text_ui_show_user_warning(self):
 
270
        from bzrlib.repofmt.groupcompress_repo import RepositoryFormat2a
 
271
        from bzrlib.repofmt.pack_repo import RepositoryFormatKnitPack5
 
272
        err = StringIO()
 
273
        out = StringIO()
 
274
        ui = tests.TextUIFactory(stdin=None, stdout=out, stderr=err)
 
275
        remote_fmt = remote.RemoteRepositoryFormat()
 
276
        remote_fmt._network_name = RepositoryFormatKnitPack5().network_name()
 
277
        ui.show_user_warning('cross_format_fetch', from_format=RepositoryFormat2a(),
 
278
            to_format=remote_fmt)
 
279
        self.assertEquals('', out.getvalue())
 
280
        self.assertEquals("Doing on-the-fly conversion from RepositoryFormat2a() to "
 
281
            "RemoteRepositoryFormat(_network_name='Bazaar RepositoryFormatKnitPack5 "
 
282
            "(bzr 1.6)\\n').\nThis may take some time. Upgrade the repositories to "
 
283
            "the same format for better performance.\n",
 
284
            err.getvalue())
 
285
        # and now with it suppressed please
 
286
        err = StringIO()
 
287
        out = StringIO()
 
288
        ui = tests.TextUIFactory(stdin=None, stdout=out, stderr=err)
 
289
        ui.suppressed_warnings.add('cross_format_fetch')
 
290
        ui.show_user_warning('cross_format_fetch', from_format=RepositoryFormat2a(),
 
291
            to_format=remote_fmt)
 
292
        self.assertEquals('', out.getvalue())
 
293
        self.assertEquals('', err.getvalue())
 
294
 
 
295
 
 
296
class TestTextUIOutputStream(tests.TestCase):
 
297
    """Tests for output stream that synchronizes with progress bar."""
 
298
 
 
299
    def test_output_clears_terminal(self):
 
300
        stdout = tests.StringIOWrapper()
 
301
        stderr = tests.StringIOWrapper()
 
302
        clear_calls = []
 
303
 
 
304
        uif =  _mod_ui_text.TextUIFactory(None, stdout, stderr)
 
305
        uif.clear_term = lambda: clear_calls.append('clear')
 
306
 
 
307
        stream = _mod_ui_text.TextUIOutputStream(uif, uif.stdout)
 
308
        stream.write("Hello world!\n")
 
309
        stream.write("there's more...\n")
 
310
        stream.writelines(["1\n", "2\n", "3\n"])
 
311
 
 
312
        self.assertEqual(stdout.getvalue(),
 
313
            "Hello world!\n"
 
314
            "there's more...\n"
 
315
            "1\n2\n3\n")
 
316
        self.assertEqual(['clear', 'clear', 'clear'],
 
317
            clear_calls)
 
318
 
 
319
        stream.flush()
 
320
 
 
321
 
 
322
class UITests(tests.TestCase):
 
323
 
 
324
    def test_progress_construction(self):
 
325
        """TextUIFactory constructs the right progress view.
 
326
        """
 
327
        TTYStringIO = test_progress._TTYStringIO
 
328
        FileStringIO = tests.StringIOWrapper
 
329
        for (file_class, term, pb, expected_pb_class) in (
 
330
            # on an xterm, either use them or not as the user requests,
 
331
            # otherwise default on
 
332
            (TTYStringIO, 'xterm', 'none', _mod_ui_text.NullProgressView),
 
333
            (TTYStringIO, 'xterm', 'text', _mod_ui_text.TextProgressView),
 
334
            (TTYStringIO, 'xterm', None, _mod_ui_text.TextProgressView),
 
335
            # on a dumb terminal, again if there's explicit configuration do
 
336
            # it, otherwise default off
 
337
            (TTYStringIO, 'dumb', 'none', _mod_ui_text.NullProgressView),
 
338
            (TTYStringIO, 'dumb', 'text', _mod_ui_text.TextProgressView),
 
339
            (TTYStringIO, 'dumb', None, _mod_ui_text.NullProgressView),
 
340
            # on a non-tty terminal, it's null regardless of $TERM
 
341
            (FileStringIO, 'xterm', None, _mod_ui_text.NullProgressView),
 
342
            (FileStringIO, 'dumb', None, _mod_ui_text.NullProgressView),
 
343
            # however, it can still be forced on
 
344
            (FileStringIO, 'dumb', 'text', _mod_ui_text.TextProgressView),
 
345
            ):
 
346
            os.environ['TERM'] = term
 
347
            if pb is None:
 
348
                if 'BZR_PROGRESS_BAR' in os.environ:
 
349
                    del os.environ['BZR_PROGRESS_BAR']
 
350
            else:
 
351
                os.environ['BZR_PROGRESS_BAR'] = pb
 
352
            stdin = file_class('')
 
353
            stderr = file_class()
 
354
            stdout = file_class()
 
355
            uif = _mod_ui.make_ui_for_terminal(stdin, stdout, stderr)
 
356
            self.assertIsInstance(uif, _mod_ui_text.TextUIFactory,
 
357
                "TERM=%s BZR_PROGRESS_BAR=%s uif=%r" % (term, pb, uif,))
 
358
            self.assertIsInstance(uif.make_progress_view(),
 
359
                expected_pb_class,
 
360
                "TERM=%s BZR_PROGRESS_BAR=%s uif=%r" % (term, pb, uif,))
 
361
 
 
362
    def test_text_ui_non_terminal(self):
 
363
        """Even on non-ttys, make_ui_for_terminal gives a text ui."""
 
364
        stdin = test_progress._NonTTYStringIO('')
 
365
        stderr = test_progress._NonTTYStringIO()
 
366
        stdout = test_progress._NonTTYStringIO()
 
367
        for term_type in ['dumb', None, 'xterm']:
 
368
            if term_type is None:
 
369
                del os.environ['TERM']
 
370
            else:
 
371
                os.environ['TERM'] = term_type
 
372
            uif = _mod_ui.make_ui_for_terminal(stdin, stdout, stderr)
 
373
            self.assertIsInstance(uif, _mod_ui_text.TextUIFactory,
 
374
                'TERM=%r' % (term_type,))
 
375
 
 
376
 
 
377
class SilentUITests(tests.TestCase):
314
378
 
315
379
    def test_silent_factory_get_password(self):
316
380
        # A silent factory that can't do user interaction can't get a
317
381
        # password.  Possibly it should raise a more specific error but it
318
382
        # can't succeed.
319
 
        ui = SilentUIFactory()
320
 
        stdout = StringIO()
 
383
        ui = _mod_ui.SilentUIFactory()
 
384
        stdout = tests.StringIOWrapper()
321
385
        self.assertRaises(
322
386
            NotImplementedError,
323
387
            self.apply_redirected,
326
390
        self.assertEqual('', stdout.getvalue())
327
391
 
328
392
    def test_silent_ui_getbool(self):
329
 
        factory = SilentUIFactory()
330
 
        stdout = StringIO()
 
393
        factory = _mod_ui.SilentUIFactory()
 
394
        stdout = tests.StringIOWrapper()
331
395
        self.assertRaises(
332
396
            NotImplementedError,
333
397
            self.apply_redirected,
334
398
            None, stdout, stdout, factory.get_boolean, "foo")
335
399
 
336
400
 
337
 
class CannedInputUIFactoryTests(TestCase):
338
 
    
 
401
class TestUIFactoryTests(tests.TestCase):
 
402
 
 
403
    def test_test_ui_factory_progress(self):
 
404
        # there's no output; we just want to make sure this doesn't crash -
 
405
        # see https://bugs.launchpad.net/bzr/+bug/408201
 
406
        ui = tests.TestUIFactory()
 
407
        pb = ui.nested_progress_bar()
 
408
        pb.update('hello')
 
409
        pb.tick()
 
410
        pb.finished()
 
411
 
 
412
 
 
413
class CannedInputUIFactoryTests(tests.TestCase):
 
414
 
339
415
    def test_canned_input_get_input(self):
340
 
        uif = CannedInputUIFactory([True, 'mbp', 'password'])
341
 
        self.assertEqual(uif.get_boolean('Extra cheese?'), True)
342
 
        self.assertEqual(uif.get_username('Enter your user name'), 'mbp')
343
 
        self.assertEqual(uif.get_password('Password for %(host)s', host='example.com'),
344
 
            'password')
 
416
        uif = _mod_ui.CannedInputUIFactory([True, 'mbp', 'password', 42])
 
417
        self.assertEqual(True, uif.get_boolean('Extra cheese?'))
 
418
        self.assertEqual('mbp', uif.get_username('Enter your user name'))
 
419
        self.assertEqual('password',
 
420
                         uif.get_password('Password for %(host)s',
 
421
                                          host='example.com'))
 
422
        self.assertEqual(42, uif.get_integer('And all that jazz ?'))
345
423
 
346
424
 
347
425
class TestBoolFromString(tests.TestCase):