/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-09-23 05:30:49 UTC
  • mto: This revision was merged to the branch mainline in revision 4745.
  • Revision ID: mbp@sourcefrog.net-20090923053049-jb9gljc7wgb34hfr
Separate TextUIFactory tests

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
    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 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 = pb.note('t')
 
112
            self.assertEqual(None, result)
 
113
            self.assertEqual("t\n", stdout.getvalue())
 
114
            # Since there was no update() call, there should be no clear() call
 
115
            self.failIf(re.search(r'^\r {10,}\r$',
 
116
                                  stderr.getvalue()) is not None,
 
117
                        'We cleared the stderr without anything to put there')
 
118
        finally:
 
119
            pb.finished()
 
120
 
 
121
    def test_progress_note_clears(self):
 
122
        stderr = _TTYStringIO()
 
123
        stdout = _TTYStringIO()
 
124
        # so that we get a TextProgressBar
 
125
        os.environ['TERM'] = 'xterm'
 
126
        ui_factory = TextUIFactory(
 
127
            stdin=StringIO(''),
 
128
            stdout=stdout, stderr=stderr)
 
129
        self.assertIsInstance(ui_factory._progress_view,
 
130
            TextProgressView)
 
131
        pb = ui_factory.nested_progress_bar()
 
132
        try:
 
133
            # Create a progress update that isn't throttled
 
134
            pb.update('x', 1, 1)
 
135
            result = pb.note('t')
 
136
            self.assertEqual(None, result)
 
137
            self.assertEqual("t\n", stdout.getvalue())
 
138
            # the exact contents will depend on the terminal width and we don't
 
139
            # care about that right now - but you're probably running it on at
 
140
            # least a 10-character wide terminal :)
 
141
            self.assertContainsRe(stderr.getvalue(), r'\r {10,}\r$')
 
142
        finally:
 
143
            pb.finished()
 
144
 
 
145
    def test_progress_nested(self):
 
146
        # test factory based nested and popping.
 
147
        ui = TextUIFactory(None, None, None)
 
148
        pb1 = ui.nested_progress_bar()
 
149
        pb2 = ui.nested_progress_bar()
 
150
        # You do get a warning if the outermost progress bar wasn't finished
 
151
        # first - it's not clear if this is really useful or if it should just
 
152
        # become orphaned -- mbp 20090120
 
153
        warnings, _ = self.callCatchWarnings(pb1.finished)
 
154
        if len(warnings) != 1:
 
155
            self.fail("unexpected warnings: %r" % (warnings,))
 
156
        pb2.finished()
 
157
        pb1.finished()
 
158
 
 
159
    def test_text_ui_get_boolean(self):
 
160
        stdin = StringIO("y\n" # True
 
161
                         "n\n" # False
 
162
                         "yes with garbage\nY\n" # True
 
163
                         "not an answer\nno\n" # False
 
164
                         "I'm sure!\nyes\n" # True
 
165
                         "NO\n" # False
 
166
                         "foo\n")
 
167
        stdout = StringIO()
 
168
        stderr = StringIO()
 
169
        factory = TextUIFactory(stdin, stdout, stderr)
 
170
        self.assertEqual(True, factory.get_boolean(""))
 
171
        self.assertEqual(False, factory.get_boolean(""))
 
172
        self.assertEqual(True, factory.get_boolean(""))
 
173
        self.assertEqual(False, factory.get_boolean(""))
 
174
        self.assertEqual(True, factory.get_boolean(""))
 
175
        self.assertEqual(False, factory.get_boolean(""))
 
176
        self.assertEqual("foo\n", factory.stdin.read())
 
177
        # stdin should be empty
 
178
        self.assertEqual('', factory.stdin.readline())
 
179
 
 
180
    def test_text_factory_prompt(self):
 
181
        # see <https://launchpad.net/bugs/365891>
 
182
        factory = TextUIFactory(StringIO(), StringIO(), StringIO())
 
183
        factory.prompt('foo %2e')
 
184
        self.assertEqual('', factory.stdout.getvalue())
 
185
        self.assertEqual('foo %2e', factory.stderr.getvalue())
 
186
 
 
187
    def test_text_factory_prompts_and_clears(self):
 
188
        # a get_boolean call should clear the pb before prompting
 
189
        out = _TTYStringIO()
 
190
        os.environ['TERM'] = 'xterm'
 
191
        factory = TextUIFactory(stdin=StringIO("yada\ny\n"), stdout=out, stderr=out)
 
192
        pb = factory.nested_progress_bar()
 
193
        pb.show_bar = False
 
194
        pb.show_spinner = False
 
195
        pb.show_count = False
 
196
        pb.update("foo", 0, 1)
 
197
        self.assertEqual(True,
 
198
                         self.apply_redirected(None, factory.stdout,
 
199
                                               factory.stdout,
 
200
                                               factory.get_boolean,
 
201
                                               "what do you want"))
 
202
        output = out.getvalue()
 
203
        self.assertContainsRe(factory.stdout.getvalue(),
 
204
            "foo *\r\r  *\r*")
 
205
        self.assertContainsRe(factory.stdout.getvalue(),
 
206
            r"what do you want\? \[y/n\]: what do you want\? \[y/n\]: ")
 
207
        # stdin should have been totally consumed
 
208
        self.assertEqual('', factory.stdin.readline())
 
209
 
 
210
    def test_text_tick_after_update(self):
 
211
        ui_factory = TextUIFactory(stdout=StringIO(), stderr=StringIO())
 
212
        pb = ui_factory.nested_progress_bar()
 
213
        try:
 
214
            pb.update('task', 0, 3)
 
215
            # Reset the clock, so that it actually tries to repaint itself
 
216
            ui_factory._progress_view._last_repaint = time.time() - 1.0
 
217
            pb.tick()
 
218
        finally:
 
219
            pb.finished()
 
220
 
 
221
    def test_text_ui_getusername(self):
 
222
        factory = TextUIFactory(None, None, None)
 
223
        factory.stdin = StringIO("someuser\n\n")
 
224
        factory.stdout = StringIO()
 
225
        factory.stderr = StringIO()
 
226
        factory.stdout.encoding = "utf8"
 
227
        # there is no output from the base factory
 
228
        self.assertEqual("someuser",
 
229
                         factory.get_username('Hello %(host)s', host='some'))
 
230
        self.assertEquals("Hello some: ", factory.stderr.getvalue())
 
231
        self.assertEquals('', factory.stdout.getvalue())
 
232
        self.assertEqual("", factory.get_username("Gebruiker"))
 
233
        # stdin should be empty
 
234
        self.assertEqual('', factory.stdin.readline())
 
235
 
 
236
    def test_text_ui_getusername_utf8(self):
 
237
        ui = tests.TestUIFactory(stdin=u'someuser\u1234'.encode('utf8'),
 
238
                                 stdout=tests.StringIOWrapper(),
 
239
                                 stderr=tests.StringIOWrapper())
 
240
        ui.stderr.encoding = ui.stdout.encoding = ui.stdin.encoding = "utf8"
 
241
        pb = ui.nested_progress_bar()
 
242
        try:
 
243
            # there is no output from the base factory
 
244
            username = self.apply_redirected(ui.stdin, ui.stdout, ui.stderr,
 
245
                ui.get_username, u'Hello\u1234 %(host)s', host=u'some\u1234')
 
246
            self.assertEquals(u"someuser\u1234", username.decode('utf8'))
 
247
            self.assertEquals(u"Hello\u1234 some\u1234: ",
 
248
                              ui.stderr.getvalue().decode("utf8"))
 
249
            self.assertEquals('', ui.stdout.getvalue())
 
250
        finally:
 
251
            pb.finished()
 
252
 
 
253
 
 
254
class UITests(tests.TestCase):
 
255
 
 
256
    def test_progress_construction(self):
 
257
        """TextUIFactory constructs the right progress view.
 
258
        """
 
259
        for (file_class, term, pb, expected_pb_class) in (
 
260
            # on an xterm, either use them or not as the user requests,
 
261
            # otherwise default on
 
262
            (_TTYStringIO, 'xterm', 'none', NullProgressView),
 
263
            (_TTYStringIO, 'xterm', 'text', TextProgressView),
 
264
            (_TTYStringIO, 'xterm', None, TextProgressView),
 
265
            # on a dumb terminal, again if there's explicit configuration do
 
266
            # it, otherwise default off
 
267
            (_TTYStringIO, 'dumb', 'none', NullProgressView),
 
268
            (_TTYStringIO, 'dumb', 'text', TextProgressView),
 
269
            (_TTYStringIO, 'dumb', None, NullProgressView),
 
270
            # on a non-tty terminal, it's null regardless of $TERM
 
271
            (StringIO, 'xterm', None, NullProgressView),
 
272
            (StringIO, 'dumb', None, NullProgressView),
 
273
            # however, it can still be forced on
 
274
            (StringIO, 'dumb', 'text', TextProgressView),
 
275
            ):
 
276
            os.environ['TERM'] = term
 
277
            if pb is None:
 
278
                if 'BZR_PROGRESS_BAR' in os.environ:
 
279
                    del os.environ['BZR_PROGRESS_BAR']
 
280
            else:
 
281
                os.environ['BZR_PROGRESS_BAR'] = pb
 
282
            stdin = file_class('')
 
283
            stderr = file_class()
 
284
            stdout = file_class()
 
285
            uif = make_ui_for_terminal(stdin, stdout, stderr)
 
286
            self.assertIsInstance(uif, TextUIFactory,
 
287
                "TERM=%s BZR_PROGRESS_BAR=%s uif=%r" % (term, pb, uif,))
 
288
            self.assertIsInstance(uif.make_progress_view(),
 
289
                expected_pb_class,
 
290
                "TERM=%s BZR_PROGRESS_BAR=%s uif=%r" % (term, pb, uif,))
 
291
 
 
292
    def test_text_ui_non_terminal(self):
 
293
        """Even on non-ttys, make_ui_for_terminal gives a text ui."""
 
294
        stdin = _NonTTYStringIO('')
 
295
        stderr = _NonTTYStringIO()
 
296
        stdout = _NonTTYStringIO()
 
297
        for term_type in ['dumb', None, 'xterm']:
 
298
            if term_type is None:
 
299
                del os.environ['TERM']
 
300
            else:
 
301
                os.environ['TERM'] = term_type
 
302
            uif = make_ui_for_terminal(stdin, stdout, stderr)
 
303
            self.assertIsInstance(uif, TextUIFactory,
 
304
                'TERM=%r' % (term_type,))
 
305
 
 
306
 
 
307
class CLIUITests(TestCase):
 
308
 
 
309
    def test_cli_factory_deprecated(self):
 
310
        uif = self.applyDeprecated(deprecated_in((1, 18, 0)),
 
311
            CLIUIFactory,
 
312
            StringIO(), StringIO(), StringIO())
 
313
        self.assertIsInstance(uif, UIFactory)
 
314
 
 
315
 
 
316
class SilentUITests(TestCase):
 
317
 
 
318
    def test_silent_factory_get_password(self):
 
319
        # A silent factory that can't do user interaction can't get a
 
320
        # password.  Possibly it should raise a more specific error but it
 
321
        # can't succeed.
 
322
        ui = SilentUIFactory()
 
323
        stdout = StringIO()
 
324
        self.assertRaises(
 
325
            NotImplementedError,
 
326
            self.apply_redirected,
 
327
            None, stdout, stdout, ui.get_password)
 
328
        # and it didn't write anything out either
 
329
        self.assertEqual('', stdout.getvalue())
 
330
 
 
331
    def test_silent_ui_getbool(self):
 
332
        factory = SilentUIFactory()
 
333
        stdout = StringIO()
 
334
        self.assertRaises(
 
335
            NotImplementedError,
 
336
            self.apply_redirected,
 
337
            None, stdout, stdout, factory.get_boolean, "foo")
 
338
 
 
339
 
 
340
class TestUIFactoryTests(TestCase):
 
341
 
 
342
    def test_test_ui_factory_progress(self):
 
343
        # there's no output; we just want to make sure this doesn't crash -
 
344
        # see https://bugs.edge.launchpad.net/bzr/+bug/408201
 
345
        ui = TestUIFactory()
 
346
        pb = ui.nested_progress_bar()
 
347
        pb.update('hello')
 
348
        pb.tick()
 
349
        pb.finished()
 
350
 
 
351
 
 
352
class CannedInputUIFactoryTests(TestCase):
 
353
    
 
354
    def test_canned_input_get_input(self):
 
355
        uif = CannedInputUIFactory([True, 'mbp', 'password'])
 
356
        self.assertEqual(uif.get_boolean('Extra cheese?'), True)
 
357
        self.assertEqual(uif.get_username('Enter your user name'), 'mbp')
 
358
        self.assertEqual(uif.get_password('Password for %(host)s', host='example.com'),
 
359
            'password')
 
360
 
 
361
 
 
362
class TestBoolFromString(tests.TestCase):
 
363
 
 
364
    def assertIsTrue(self, s, accepted_values=None):
 
365
        res = _mod_ui.bool_from_string(s, accepted_values=accepted_values)
 
366
        self.assertEquals(True, res)
 
367
 
 
368
    def assertIsFalse(self, s, accepted_values=None):
 
369
        res = _mod_ui.bool_from_string(s, accepted_values=accepted_values)
 
370
        self.assertEquals(False, res)
 
371
 
 
372
    def assertIsNone(self, s, accepted_values=None):
 
373
        res = _mod_ui.bool_from_string(s, accepted_values=accepted_values)
 
374
        self.assertIs(None, res)
 
375
 
 
376
    def test_know_valid_values(self):
 
377
        self.assertIsTrue('true')
 
378
        self.assertIsFalse('false')
 
379
        self.assertIsTrue('1')
 
380
        self.assertIsFalse('0')
 
381
        self.assertIsTrue('on')
 
382
        self.assertIsFalse('off')
 
383
        self.assertIsTrue('yes')
 
384
        self.assertIsFalse('no')
 
385
        self.assertIsTrue('y')
 
386
        self.assertIsFalse('n')
 
387
        # Also try some case variations
 
388
        self.assertIsTrue('True')
 
389
        self.assertIsFalse('False')
 
390
        self.assertIsTrue('On')
 
391
        self.assertIsFalse('Off')
 
392
        self.assertIsTrue('ON')
 
393
        self.assertIsFalse('OFF')
 
394
        self.assertIsTrue('oN')
 
395
        self.assertIsFalse('oFf')
 
396
 
 
397
    def test_invalid_values(self):
 
398
        self.assertIsNone(None)
 
399
        self.assertIsNone('doubt')
 
400
        self.assertIsNone('frue')
 
401
        self.assertIsNone('talse')
 
402
        self.assertIsNone('42')
 
403
 
 
404
    def test_provided_values(self):
 
405
        av = dict(y=True, n=False, yes=True, no=False)
 
406
        self.assertIsTrue('y', av)
 
407
        self.assertIsTrue('Y', av)
 
408
        self.assertIsTrue('Yes', av)
 
409
        self.assertIsFalse('n', av)
 
410
        self.assertIsFalse('N', av)
 
411
        self.assertIsFalse('No', av)
 
412
        self.assertIsNone('1', av)
 
413
        self.assertIsNone('0', av)
 
414
        self.assertIsNone('on', av)
 
415
        self.assertIsNone('off', av)