/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: Martin Pool
  • Date: 2009-11-16 02:56:34 UTC
  • mto: This revision was merged to the branch mainline in revision 4880.
  • Revision ID: mbp@sourcefrog.net-20091116025634-lxfe18oxvti3qe6e
Add TextUIOutputStream.flush

Show diffs side-by-side

added added

removed removed

Lines of Context:
 
1
# Copyright (C) 2005, 2008, 2009 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
from StringIO import StringIO
 
22
import re
 
23
import sys
 
24
import time
 
25
 
 
26
from bzrlib import (
 
27
    errors,
 
28
    tests,
 
29
    ui as _mod_ui,
 
30
    )
 
31
from bzrlib.symbol_versioning import (
 
32
    deprecated_in,
 
33
    )
 
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
    SilentUIFactory,
 
46
    UIFactory,
 
47
    make_ui_for_terminal,
 
48
    )
 
49
from bzrlib.ui.text import (
 
50
    NullProgressView,
 
51
    TextProgressView,
 
52
    TextUIFactory,
 
53
    TextUIOutputStream,
 
54
    )
 
55
 
 
56
 
 
57
class TestTextUIFactory(tests.TestCase):
 
58
 
 
59
    def test_text_factory_ascii_password(self):
 
60
        ui = tests.TestUIFactory(stdin='secret\n',
 
61
                                 stdout=tests.StringIOWrapper(),
 
62
                                 stderr=tests.StringIOWrapper())
 
63
        pb = ui.nested_progress_bar()
 
64
        try:
 
65
            self.assertEqual('secret',
 
66
                             self.apply_redirected(ui.stdin, ui.stdout,
 
67
                                                   ui.stderr,
 
68
                                                   ui.get_password))
 
69
            # ': ' is appended to prompt
 
70
            self.assertEqual(': ', ui.stderr.getvalue())
 
71
            self.assertEqual('', ui.stdout.readline())
 
72
            # stdin should be empty
 
73
            self.assertEqual('', ui.stdin.readline())
 
74
        finally:
 
75
            pb.finished()
 
76
 
 
77
    def test_text_factory_utf8_password(self):
 
78
        """Test an utf8 password.
 
79
 
 
80
        We can't predict what encoding users will have for stdin, so we force
 
81
        it to utf8 to test that we transport the password correctly.
 
82
        """
 
83
        ui = tests.TestUIFactory(stdin=u'baz\u1234'.encode('utf8'),
 
84
                                 stdout=tests.StringIOWrapper(),
 
85
                                 stderr=tests.StringIOWrapper())
 
86
        ui.stderr.encoding = ui.stdout.encoding = ui.stdin.encoding = 'utf8'
 
87
        pb = ui.nested_progress_bar()
 
88
        try:
 
89
            password = self.apply_redirected(ui.stdin, ui.stdout, ui.stderr,
 
90
                                             ui.get_password,
 
91
                                             u'Hello \u1234 %(user)s',
 
92
                                             user=u'some\u1234')
 
93
            # We use StringIO objects, we need to decode them
 
94
            self.assertEqual(u'baz\u1234', password.decode('utf8'))
 
95
            self.assertEqual(u'Hello \u1234 some\u1234: ',
 
96
                             ui.stderr.getvalue().decode('utf8'))
 
97
            # stdin and stdout should be empty
 
98
            self.assertEqual('', ui.stdin.readline())
 
99
            self.assertEqual('', ui.stdout.readline())
 
100
        finally:
 
101
            pb.finished()
 
102
 
 
103
    def test_progress_note(self):
 
104
        stderr = StringIO()
 
105
        stdout = StringIO()
 
106
        ui_factory = TextUIFactory(stdin=StringIO(''),
 
107
            stderr=stderr,
 
108
            stdout=stdout)
 
109
        pb = ui_factory.nested_progress_bar()
 
110
        try:
 
111
            result = self.applyDeprecated(deprecated_in((2, 1, 0)),
 
112
                pb.note,
 
113
                't')
 
114
            self.assertEqual(None, result)
 
115
            self.assertEqual("t\n", stdout.getvalue())
 
116
            # Since there was no update() call, there should be no clear() call
 
117
            self.failIf(re.search(r'^\r {10,}\r$',
 
118
                                  stderr.getvalue()) is not None,
 
119
                        'We cleared the stderr without anything to put there')
 
120
        finally:
 
121
            pb.finished()
 
122
 
 
123
    def test_progress_note_clears(self):
 
124
        stderr = _TTYStringIO()
 
125
        stdout = _TTYStringIO()
 
126
        # so that we get a TextProgressBar
 
127
        os.environ['TERM'] = 'xterm'
 
128
        ui_factory = TextUIFactory(
 
129
            stdin=StringIO(''),
 
130
            stdout=stdout, stderr=stderr)
 
131
        self.assertIsInstance(ui_factory._progress_view,
 
132
            TextProgressView)
 
133
        pb = ui_factory.nested_progress_bar()
 
134
        try:
 
135
            # Create a progress update that isn't throttled
 
136
            pb.update('x', 1, 1)
 
137
            result = self.applyDeprecated(deprecated_in((2, 1, 0)),
 
138
                pb.note, 't')
 
139
            self.assertEqual(None, result)
 
140
            self.assertEqual("t\n", stdout.getvalue())
 
141
            # the exact contents will depend on the terminal width and we don't
 
142
            # care about that right now - but you're probably running it on at
 
143
            # least a 10-character wide terminal :)
 
144
            self.assertContainsRe(stderr.getvalue(), r'\r {10,}\r$')
 
145
        finally:
 
146
            pb.finished()
 
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
 
 
162
    def test_text_ui_get_boolean(self):
 
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)
 
173
        self.assertEqual(True, factory.get_boolean(""))
 
174
        self.assertEqual(False, factory.get_boolean(""))
 
175
        self.assertEqual(True, factory.get_boolean(""))
 
176
        self.assertEqual(False, factory.get_boolean(""))
 
177
        self.assertEqual(True, factory.get_boolean(""))
 
178
        self.assertEqual(False, factory.get_boolean(""))
 
179
        self.assertEqual("foo\n", factory.stdin.read())
 
180
        # stdin should be empty
 
181
        self.assertEqual('', factory.stdin.readline())
 
182
 
 
183
    def test_text_factory_prompt(self):
 
184
        # see <https://launchpad.net/bugs/365891>
 
185
        factory = TextUIFactory(StringIO(), StringIO(), StringIO())
 
186
        factory.prompt('foo %2e')
 
187
        self.assertEqual('', factory.stdout.getvalue())
 
188
        self.assertEqual('foo %2e', factory.stderr.getvalue())
 
189
 
 
190
    def test_text_factory_prompts_and_clears(self):
 
191
        # a get_boolean call should clear the pb before prompting
 
192
        out = _TTYStringIO()
 
193
        os.environ['TERM'] = 'xterm'
 
194
        factory = TextUIFactory(stdin=StringIO("yada\ny\n"), stdout=out, stderr=out)
 
195
        pb = factory.nested_progress_bar()
 
196
        pb.show_bar = False
 
197
        pb.show_spinner = False
 
198
        pb.show_count = False
 
199
        pb.update("foo", 0, 1)
 
200
        self.assertEqual(True,
 
201
                         self.apply_redirected(None, factory.stdout,
 
202
                                               factory.stdout,
 
203
                                               factory.get_boolean,
 
204
                                               "what do you want"))
 
205
        output = out.getvalue()
 
206
        self.assertContainsRe(factory.stdout.getvalue(),
 
207
            "foo *\r\r  *\r*")
 
208
        self.assertContainsRe(factory.stdout.getvalue(),
 
209
            r"what do you want\? \[y/n\]: what do you want\? \[y/n\]: ")
 
210
        # stdin should have been totally consumed
 
211
        self.assertEqual('', factory.stdin.readline())
 
212
 
 
213
    def test_text_tick_after_update(self):
 
214
        ui_factory = TextUIFactory(stdout=StringIO(), stderr=StringIO())
 
215
        pb = ui_factory.nested_progress_bar()
 
216
        try:
 
217
            pb.update('task', 0, 3)
 
218
            # Reset the clock, so that it actually tries to repaint itself
 
219
            ui_factory._progress_view._last_repaint = time.time() - 1.0
 
220
            pb.tick()
 
221
        finally:
 
222
            pb.finished()
 
223
 
 
224
    def test_text_ui_getusername(self):
 
225
        factory = TextUIFactory(None, None, None)
 
226
        factory.stdin = StringIO("someuser\n\n")
 
227
        factory.stdout = StringIO()
 
228
        factory.stderr = StringIO()
 
229
        factory.stdout.encoding = "utf8"
 
230
        # there is no output from the base factory
 
231
        self.assertEqual("someuser",
 
232
                         factory.get_username('Hello %(host)s', host='some'))
 
233
        self.assertEquals("Hello some: ", factory.stderr.getvalue())
 
234
        self.assertEquals('', factory.stdout.getvalue())
 
235
        self.assertEqual("", factory.get_username("Gebruiker"))
 
236
        # stdin should be empty
 
237
        self.assertEqual('', factory.stdin.readline())
 
238
 
 
239
    def test_text_ui_getusername_utf8(self):
 
240
        ui = tests.TestUIFactory(stdin=u'someuser\u1234'.encode('utf8'),
 
241
                                 stdout=tests.StringIOWrapper(),
 
242
                                 stderr=tests.StringIOWrapper())
 
243
        ui.stderr.encoding = ui.stdout.encoding = ui.stdin.encoding = "utf8"
 
244
        pb = ui.nested_progress_bar()
 
245
        try:
 
246
            # there is no output from the base factory
 
247
            username = self.apply_redirected(ui.stdin, ui.stdout, ui.stderr,
 
248
                ui.get_username, u'Hello\u1234 %(host)s', host=u'some\u1234')
 
249
            self.assertEquals(u"someuser\u1234", username.decode('utf8'))
 
250
            self.assertEquals(u"Hello\u1234 some\u1234: ",
 
251
                              ui.stderr.getvalue().decode("utf8"))
 
252
            self.assertEquals('', ui.stdout.getvalue())
 
253
        finally:
 
254
            pb.finished()
 
255
 
 
256
 
 
257
class TestTextUIOutputStream(TestCase):
 
258
    """Tests for output stream that synchronizes with progress bar."""
 
259
 
 
260
    def test_output_clears_terminal(self):
 
261
        stdout = StringIO()
 
262
        stderr = StringIO()
 
263
        clear_calls = []
 
264
 
 
265
        uif = TextUIFactory(None, stdout, stderr)
 
266
        uif.clear_term = lambda: clear_calls.append('clear')
 
267
 
 
268
        stream = TextUIOutputStream(uif, uif.stdout)
 
269
        stream.write("Hello world!\n")
 
270
        stream.write("there's more...\n")
 
271
        stream.writelines(["1\n", "2\n", "3\n"])
 
272
        
 
273
        self.assertEqual(stdout.getvalue(),
 
274
            "Hello world!\n"
 
275
            "there's more...\n"
 
276
            "1\n2\n3\n")
 
277
        self.assertEqual(['clear', 'clear', 'clear'],
 
278
            clear_calls)
 
279
 
 
280
        stream.flush()
 
281
 
 
282
 
 
283
class UITests(tests.TestCase):
 
284
 
 
285
    def test_progress_construction(self):
 
286
        """TextUIFactory constructs the right progress view.
 
287
        """
 
288
        for (file_class, term, pb, expected_pb_class) in (
 
289
            # on an xterm, either use them or not as the user requests,
 
290
            # otherwise default on
 
291
            (_TTYStringIO, 'xterm', 'none', NullProgressView),
 
292
            (_TTYStringIO, 'xterm', 'text', TextProgressView),
 
293
            (_TTYStringIO, 'xterm', None, TextProgressView),
 
294
            # on a dumb terminal, again if there's explicit configuration do
 
295
            # it, otherwise default off
 
296
            (_TTYStringIO, 'dumb', 'none', NullProgressView),
 
297
            (_TTYStringIO, 'dumb', 'text', TextProgressView),
 
298
            (_TTYStringIO, 'dumb', None, NullProgressView),
 
299
            # on a non-tty terminal, it's null regardless of $TERM
 
300
            (StringIO, 'xterm', None, NullProgressView),
 
301
            (StringIO, 'dumb', None, NullProgressView),
 
302
            # however, it can still be forced on
 
303
            (StringIO, 'dumb', 'text', TextProgressView),
 
304
            ):
 
305
            os.environ['TERM'] = term
 
306
            if pb is None:
 
307
                if 'BZR_PROGRESS_BAR' in os.environ:
 
308
                    del os.environ['BZR_PROGRESS_BAR']
 
309
            else:
 
310
                os.environ['BZR_PROGRESS_BAR'] = pb
 
311
            stdin = file_class('')
 
312
            stderr = file_class()
 
313
            stdout = file_class()
 
314
            uif = make_ui_for_terminal(stdin, stdout, stderr)
 
315
            self.assertIsInstance(uif, TextUIFactory,
 
316
                "TERM=%s BZR_PROGRESS_BAR=%s uif=%r" % (term, pb, uif,))
 
317
            self.assertIsInstance(uif.make_progress_view(),
 
318
                expected_pb_class,
 
319
                "TERM=%s BZR_PROGRESS_BAR=%s uif=%r" % (term, pb, uif,))
 
320
 
 
321
    def test_text_ui_non_terminal(self):
 
322
        """Even on non-ttys, make_ui_for_terminal gives a text ui."""
 
323
        stdin = _NonTTYStringIO('')
 
324
        stderr = _NonTTYStringIO()
 
325
        stdout = _NonTTYStringIO()
 
326
        for term_type in ['dumb', None, 'xterm']:
 
327
            if term_type is None:
 
328
                del os.environ['TERM']
 
329
            else:
 
330
                os.environ['TERM'] = term_type
 
331
            uif = make_ui_for_terminal(stdin, stdout, stderr)
 
332
            self.assertIsInstance(uif, TextUIFactory,
 
333
                'TERM=%r' % (term_type,))
 
334
 
 
335
 
 
336
class SilentUITests(TestCase):
 
337
 
 
338
    def test_silent_factory_get_password(self):
 
339
        # A silent factory that can't do user interaction can't get a
 
340
        # password.  Possibly it should raise a more specific error but it
 
341
        # can't succeed.
 
342
        ui = SilentUIFactory()
 
343
        stdout = StringIO()
 
344
        self.assertRaises(
 
345
            NotImplementedError,
 
346
            self.apply_redirected,
 
347
            None, stdout, stdout, ui.get_password)
 
348
        # and it didn't write anything out either
 
349
        self.assertEqual('', stdout.getvalue())
 
350
 
 
351
    def test_silent_ui_getbool(self):
 
352
        factory = SilentUIFactory()
 
353
        stdout = StringIO()
 
354
        self.assertRaises(
 
355
            NotImplementedError,
 
356
            self.apply_redirected,
 
357
            None, stdout, stdout, factory.get_boolean, "foo")
 
358
 
 
359
 
 
360
class TestUIFactoryTests(TestCase):
 
361
 
 
362
    def test_test_ui_factory_progress(self):
 
363
        # there's no output; we just want to make sure this doesn't crash -
 
364
        # see https://bugs.edge.launchpad.net/bzr/+bug/408201
 
365
        ui = TestUIFactory()
 
366
        pb = ui.nested_progress_bar()
 
367
        pb.update('hello')
 
368
        pb.tick()
 
369
        pb.finished()
 
370
 
 
371
 
 
372
class CannedInputUIFactoryTests(TestCase):
 
373
    
 
374
    def test_canned_input_get_input(self):
 
375
        uif = CannedInputUIFactory([True, 'mbp', 'password'])
 
376
        self.assertEqual(uif.get_boolean('Extra cheese?'), True)
 
377
        self.assertEqual(uif.get_username('Enter your user name'), 'mbp')
 
378
        self.assertEqual(uif.get_password('Password for %(host)s', host='example.com'),
 
379
            'password')
 
380
 
 
381
 
 
382
class TestBoolFromString(tests.TestCase):
 
383
 
 
384
    def assertIsTrue(self, s, accepted_values=None):
 
385
        res = _mod_ui.bool_from_string(s, accepted_values=accepted_values)
 
386
        self.assertEquals(True, res)
 
387
 
 
388
    def assertIsFalse(self, s, accepted_values=None):
 
389
        res = _mod_ui.bool_from_string(s, accepted_values=accepted_values)
 
390
        self.assertEquals(False, res)
 
391
 
 
392
    def assertIsNone(self, s, accepted_values=None):
 
393
        res = _mod_ui.bool_from_string(s, accepted_values=accepted_values)
 
394
        self.assertIs(None, res)
 
395
 
 
396
    def test_know_valid_values(self):
 
397
        self.assertIsTrue('true')
 
398
        self.assertIsFalse('false')
 
399
        self.assertIsTrue('1')
 
400
        self.assertIsFalse('0')
 
401
        self.assertIsTrue('on')
 
402
        self.assertIsFalse('off')
 
403
        self.assertIsTrue('yes')
 
404
        self.assertIsFalse('no')
 
405
        self.assertIsTrue('y')
 
406
        self.assertIsFalse('n')
 
407
        # Also try some case variations
 
408
        self.assertIsTrue('True')
 
409
        self.assertIsFalse('False')
 
410
        self.assertIsTrue('On')
 
411
        self.assertIsFalse('Off')
 
412
        self.assertIsTrue('ON')
 
413
        self.assertIsFalse('OFF')
 
414
        self.assertIsTrue('oN')
 
415
        self.assertIsFalse('oFf')
 
416
 
 
417
    def test_invalid_values(self):
 
418
        self.assertIsNone(None)
 
419
        self.assertIsNone('doubt')
 
420
        self.assertIsNone('frue')
 
421
        self.assertIsNone('talse')
 
422
        self.assertIsNone('42')
 
423
 
 
424
    def test_provided_values(self):
 
425
        av = dict(y=True, n=False, yes=True, no=False)
 
426
        self.assertIsTrue('y', av)
 
427
        self.assertIsTrue('Y', av)
 
428
        self.assertIsTrue('Yes', av)
 
429
        self.assertIsFalse('n', av)
 
430
        self.assertIsFalse('N', av)
 
431
        self.assertIsFalse('No', av)
 
432
        self.assertIsNone('1', av)
 
433
        self.assertIsNone('0', av)
 
434
        self.assertIsNone('on', av)
 
435
        self.assertIsNone('off', av)