/brz/remove-bazaar

To get this branch, use:
bzr branch http://gegoxaren.bato24.eu/bzr/brz/remove-bazaar

« back to all changes in this revision

Viewing changes to bzrlib/tests/test_ui.py

  • Committer: Canonical.com Patch Queue Manager
  • Date: 2009-10-15 21:28:14 UTC
  • mfrom: (4634.75.2 jam-integration)
  • Revision ID: pqm@pqm.ubuntu.com-20091015212814-i4j0hjrcob5cdw6j
(mbp) Fix bug #446033 by skipping non-existing entries in readdir.

Show diffs side-by-side

added added

removed removed

Lines of Context:
1
 
# Copyright (C) 2005-2010 Canonical Ltd
 
1
# Copyright (C) 2005, 2008, 2009 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
21
22
import re
 
23
import sys
22
24
import time
23
25
 
24
 
from StringIO import StringIO
25
 
 
26
26
from bzrlib import (
27
27
    errors,
28
 
    remote,
29
 
    repository,
30
28
    tests,
31
29
    ui as _mod_ui,
32
30
    )
33
31
from bzrlib.symbol_versioning import (
34
32
    deprecated_in,
35
33
    )
36
 
from bzrlib.tests import test_progress
37
 
from bzrlib.ui import text as _mod_ui_text
 
34
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
    )
38
55
 
39
56
 
40
57
class TestTextUIFactory(tests.TestCase):
84
101
            pb.finished()
85
102
 
86
103
    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)
 
104
        stderr = StringIO()
 
105
        stdout = StringIO()
 
106
        ui_factory = TextUIFactory(stdin=StringIO(''),
 
107
            stderr=stderr,
 
108
            stdout=stdout)
92
109
        pb = ui_factory.nested_progress_bar()
93
110
        try:
94
111
            result = self.applyDeprecated(deprecated_in((2, 1, 0)),
104
121
            pb.finished()
105
122
 
106
123
    def test_progress_note_clears(self):
107
 
        stderr = test_progress._TTYStringIO()
108
 
        stdout = test_progress._TTYStringIO()
 
124
        stderr = _TTYStringIO()
 
125
        stdout = _TTYStringIO()
109
126
        # so that we get a TextProgressBar
110
127
        os.environ['TERM'] = 'xterm'
111
 
        ui_factory = _mod_ui_text.TextUIFactory(
112
 
            stdin=tests.StringIOWrapper(''),
 
128
        ui_factory = TextUIFactory(
 
129
            stdin=StringIO(''),
113
130
            stdout=stdout, stderr=stderr)
114
131
        self.assertIsInstance(ui_factory._progress_view,
115
 
                              _mod_ui_text.TextProgressView)
 
132
            TextProgressView)
116
133
        pb = ui_factory.nested_progress_bar()
117
134
        try:
118
135
            # Create a progress update that isn't throttled
128
145
        finally:
129
146
            pb.finished()
130
147
 
 
148
    def test_progress_nested(self):
 
149
        # test factory based nested and popping.
 
150
        ui = TextUIFactory(None, None, None)
 
151
        pb1 = ui.nested_progress_bar()
 
152
        pb2 = ui.nested_progress_bar()
 
153
        # You do get a warning if the outermost progress bar wasn't finished
 
154
        # first - it's not clear if this is really useful or if it should just
 
155
        # become orphaned -- mbp 20090120
 
156
        warnings, _ = self.callCatchWarnings(pb1.finished)
 
157
        if len(warnings) != 1:
 
158
            self.fail("unexpected warnings: %r" % (warnings,))
 
159
        pb2.finished()
 
160
        pb1.finished()
 
161
 
131
162
    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)
 
163
        stdin = StringIO("y\n" # True
 
164
                         "n\n" # False
 
165
                         "yes with garbage\nY\n" # True
 
166
                         "not an answer\nno\n" # False
 
167
                         "I'm sure!\nyes\n" # True
 
168
                         "NO\n" # False
 
169
                         "foo\n")
 
170
        stdout = StringIO()
 
171
        stderr = StringIO()
 
172
        factory = TextUIFactory(stdin, stdout, stderr)
142
173
        self.assertEqual(True, factory.get_boolean(""))
143
174
        self.assertEqual(False, factory.get_boolean(""))
144
175
        self.assertEqual(True, factory.get_boolean(""))
149
180
        # stdin should be empty
150
181
        self.assertEqual('', factory.stdin.readline())
151
182
 
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
183
    def test_text_factory_prompt(self):
165
184
        # see <https://launchpad.net/bugs/365891>
166
 
        StringIO = tests.StringIOWrapper
167
 
        factory = _mod_ui_text.TextUIFactory(StringIO(), StringIO(), StringIO())
 
185
        factory = TextUIFactory(StringIO(), StringIO(), StringIO())
168
186
        factory.prompt('foo %2e')
169
187
        self.assertEqual('', factory.stdout.getvalue())
170
188
        self.assertEqual('foo %2e', factory.stderr.getvalue())
171
189
 
172
190
    def test_text_factory_prompts_and_clears(self):
173
191
        # a get_boolean call should clear the pb before prompting
174
 
        out = test_progress._TTYStringIO()
 
192
        out = _TTYStringIO()
175
193
        os.environ['TERM'] = 'xterm'
176
 
        factory = _mod_ui_text.TextUIFactory(
177
 
            stdin=tests.StringIOWrapper("yada\ny\n"),
178
 
            stdout=out, stderr=out)
 
194
        factory = TextUIFactory(stdin=StringIO("yada\ny\n"), stdout=out, stderr=out)
179
195
        pb = factory.nested_progress_bar()
180
196
        pb.show_bar = False
181
197
        pb.show_spinner = False
195
211
        self.assertEqual('', factory.stdin.readline())
196
212
 
197
213
    def test_text_tick_after_update(self):
198
 
        ui_factory = _mod_ui_text.TextUIFactory(stdout=tests.StringIOWrapper(),
199
 
                                                stderr=tests.StringIOWrapper())
 
214
        ui_factory = TextUIFactory(stdout=StringIO(), stderr=StringIO())
200
215
        pb = ui_factory.nested_progress_bar()
201
216
        try:
202
217
            pb.update('task', 0, 3)
207
222
            pb.finished()
208
223
 
209
224
    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()
 
225
        factory = TextUIFactory(None, None, None)
 
226
        factory.stdin = StringIO("someuser\n\n")
 
227
        factory.stdout = StringIO()
 
228
        factory.stderr = StringIO()
214
229
        factory.stdout.encoding = "utf8"
215
230
        # there is no output from the base factory
216
231
        self.assertEqual("someuser",
238
253
        finally:
239
254
            pb.finished()
240
255
 
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
256
 
305
257
class UITests(tests.TestCase):
306
258
 
307
259
    def test_progress_construction(self):
308
260
        """TextUIFactory constructs the right progress view.
309
261
        """
310
 
        TTYStringIO = test_progress._TTYStringIO
311
 
        FileStringIO = tests.StringIOWrapper
312
262
        for (file_class, term, pb, expected_pb_class) in (
313
263
            # on an xterm, either use them or not as the user requests,
314
264
            # 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),
 
265
            (_TTYStringIO, 'xterm', 'none', NullProgressView),
 
266
            (_TTYStringIO, 'xterm', 'text', TextProgressView),
 
267
            (_TTYStringIO, 'xterm', None, TextProgressView),
318
268
            # on a dumb terminal, again if there's explicit configuration do
319
269
            # 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),
 
270
            (_TTYStringIO, 'dumb', 'none', NullProgressView),
 
271
            (_TTYStringIO, 'dumb', 'text', TextProgressView),
 
272
            (_TTYStringIO, 'dumb', None, NullProgressView),
323
273
            # 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),
 
274
            (StringIO, 'xterm', None, NullProgressView),
 
275
            (StringIO, 'dumb', None, NullProgressView),
326
276
            # however, it can still be forced on
327
 
            (FileStringIO, 'dumb', 'text', _mod_ui_text.TextProgressView),
 
277
            (StringIO, 'dumb', 'text', TextProgressView),
328
278
            ):
329
279
            os.environ['TERM'] = term
330
280
            if pb is None:
335
285
            stdin = file_class('')
336
286
            stderr = file_class()
337
287
            stdout = file_class()
338
 
            uif = _mod_ui.make_ui_for_terminal(stdin, stdout, stderr)
339
 
            self.assertIsInstance(uif, _mod_ui_text.TextUIFactory,
 
288
            uif = make_ui_for_terminal(stdin, stdout, stderr)
 
289
            self.assertIsInstance(uif, TextUIFactory,
340
290
                "TERM=%s BZR_PROGRESS_BAR=%s uif=%r" % (term, pb, uif,))
341
291
            self.assertIsInstance(uif.make_progress_view(),
342
292
                expected_pb_class,
344
294
 
345
295
    def test_text_ui_non_terminal(self):
346
296
        """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()
 
297
        stdin = _NonTTYStringIO('')
 
298
        stderr = _NonTTYStringIO()
 
299
        stdout = _NonTTYStringIO()
350
300
        for term_type in ['dumb', None, 'xterm']:
351
301
            if term_type is None:
352
302
                del os.environ['TERM']
353
303
            else:
354
304
                os.environ['TERM'] = term_type
355
 
            uif = _mod_ui.make_ui_for_terminal(stdin, stdout, stderr)
356
 
            self.assertIsInstance(uif, _mod_ui_text.TextUIFactory,
 
305
            uif = make_ui_for_terminal(stdin, stdout, stderr)
 
306
            self.assertIsInstance(uif, TextUIFactory,
357
307
                'TERM=%r' % (term_type,))
358
308
 
359
309
 
360
 
class SilentUITests(tests.TestCase):
 
310
class CLIUITests(TestCase):
 
311
 
 
312
    def test_cli_factory_deprecated(self):
 
313
        uif = self.applyDeprecated(deprecated_in((1, 18, 0)),
 
314
            CLIUIFactory,
 
315
            StringIO(), StringIO(), StringIO())
 
316
        self.assertIsInstance(uif, UIFactory)
 
317
 
 
318
 
 
319
class SilentUITests(TestCase):
361
320
 
362
321
    def test_silent_factory_get_password(self):
363
322
        # A silent factory that can't do user interaction can't get a
364
323
        # password.  Possibly it should raise a more specific error but it
365
324
        # can't succeed.
366
 
        ui = _mod_ui.SilentUIFactory()
367
 
        stdout = tests.StringIOWrapper()
 
325
        ui = SilentUIFactory()
 
326
        stdout = StringIO()
368
327
        self.assertRaises(
369
328
            NotImplementedError,
370
329
            self.apply_redirected,
373
332
        self.assertEqual('', stdout.getvalue())
374
333
 
375
334
    def test_silent_ui_getbool(self):
376
 
        factory = _mod_ui.SilentUIFactory()
377
 
        stdout = tests.StringIOWrapper()
 
335
        factory = SilentUIFactory()
 
336
        stdout = StringIO()
378
337
        self.assertRaises(
379
338
            NotImplementedError,
380
339
            self.apply_redirected,
381
340
            None, stdout, stdout, factory.get_boolean, "foo")
382
341
 
383
342
 
384
 
class TestUIFactoryTests(tests.TestCase):
 
343
class TestUIFactoryTests(TestCase):
385
344
 
386
345
    def test_test_ui_factory_progress(self):
387
346
        # there's no output; we just want to make sure this doesn't crash -
388
347
        # see https://bugs.edge.launchpad.net/bzr/+bug/408201
389
 
        ui = tests.TestUIFactory()
 
348
        ui = TestUIFactory()
390
349
        pb = ui.nested_progress_bar()
391
350
        pb.update('hello')
392
351
        pb.tick()
393
352
        pb.finished()
394
353
 
395
354
 
396
 
class CannedInputUIFactoryTests(tests.TestCase):
397
 
 
 
355
class CannedInputUIFactoryTests(TestCase):
 
356
    
398
357
    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 ?'))
 
358
        uif = CannedInputUIFactory([True, 'mbp', 'password'])
 
359
        self.assertEqual(uif.get_boolean('Extra cheese?'), True)
 
360
        self.assertEqual(uif.get_username('Enter your user name'), 'mbp')
 
361
        self.assertEqual(uif.get_password('Password for %(host)s', host='example.com'),
 
362
            'password')
406
363
 
407
364
 
408
365
class TestBoolFromString(tests.TestCase):