~bzr-pqm/bzr/bzr.dev

« back to all changes in this revision

Viewing changes to bzrlib/tests/test_ui.py

  • Committer: Ian Clatworthy
  • Date: 2010-02-19 03:02:07 UTC
  • mto: (4797.23.1 integration-2.1)
  • mto: This revision was merged to the branch mainline in revision 5055.
  • Revision ID: ian.clatworthy@canonical.com-20100219030207-zpbzx021zavx4sqt
What's New in 2.1 - a summary of changes since 2.0

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
 
26
24
from bzrlib import (
31
29
from bzrlib.symbol_versioning import (
32
30
    deprecated_in,
33
31
    )
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 UITests(tests.TestCase):
 
32
from bzrlib.tests import test_progress
 
33
from bzrlib.ui import text as _mod_ui_text
 
34
 
 
35
 
 
36
class TestTextUIFactory(tests.TestCase):
58
37
 
59
38
    def test_text_factory_ascii_password(self):
60
39
        ui = tests.TestUIFactory(stdin='secret\n',
100
79
        finally:
101
80
            pb.finished()
102
81
 
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
82
    def test_progress_note(self):
154
 
        stderr = StringIO()
155
 
        stdout = StringIO()
156
 
        ui_factory = TextUIFactory(stdin=StringIO(''),
157
 
            stderr=stderr,
158
 
            stdout=stdout)
 
83
        stderr = tests.StringIOWrapper()
 
84
        stdout = tests.StringIOWrapper()
 
85
        ui_factory = _mod_ui_text.TextUIFactory(stdin=tests.StringIOWrapper(''),
 
86
                                                stderr=stderr,
 
87
                                                stdout=stdout)
159
88
        pb = ui_factory.nested_progress_bar()
160
89
        try:
161
 
            result = pb.note('t')
 
90
            result = self.applyDeprecated(deprecated_in((2, 1, 0)),
 
91
                pb.note,
 
92
                't')
162
93
            self.assertEqual(None, result)
163
94
            self.assertEqual("t\n", stdout.getvalue())
164
95
            # Since there was no update() call, there should be no clear() call
169
100
            pb.finished()
170
101
 
171
102
    def test_progress_note_clears(self):
172
 
        stderr = _TTYStringIO()
173
 
        stdout = _TTYStringIO()
 
103
        stderr = test_progress._TTYStringIO()
 
104
        stdout = test_progress._TTYStringIO()
174
105
        # so that we get a TextProgressBar
175
106
        os.environ['TERM'] = 'xterm'
176
 
        ui_factory = TextUIFactory(
177
 
            stdin=StringIO(''),
 
107
        ui_factory = _mod_ui_text.TextUIFactory(
 
108
            stdin=tests.StringIOWrapper(''),
178
109
            stdout=stdout, stderr=stderr)
179
110
        self.assertIsInstance(ui_factory._progress_view,
180
 
            TextProgressView)
 
111
                              _mod_ui_text.TextProgressView)
181
112
        pb = ui_factory.nested_progress_bar()
182
113
        try:
183
114
            # Create a progress update that isn't throttled
184
115
            pb.update('x', 1, 1)
185
 
            result = pb.note('t')
 
116
            result = self.applyDeprecated(deprecated_in((2, 1, 0)),
 
117
                pb.note, 't')
186
118
            self.assertEqual(None, result)
187
119
            self.assertEqual("t\n", stdout.getvalue())
188
120
            # the exact contents will depend on the terminal width and we don't
194
126
 
195
127
    def test_progress_nested(self):
196
128
        # test factory based nested and popping.
197
 
        ui = TextUIFactory(None, None, None)
 
129
        ui = _mod_ui_text.TextUIFactory(None, None, None)
198
130
        pb1 = ui.nested_progress_bar()
199
131
        pb2 = ui.nested_progress_bar()
200
132
        # You do get a warning if the outermost progress bar wasn't finished
207
139
        pb1.finished()
208
140
 
209
141
    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)
 
142
        stdin = tests.StringIOWrapper("y\n" # True
 
143
                                      "n\n" # False
 
144
                                      "yes with garbage\nY\n" # True
 
145
                                      "not an answer\nno\n" # False
 
146
                                      "I'm sure!\nyes\n" # True
 
147
                                      "NO\n" # False
 
148
                                      "foo\n")
 
149
        stdout = tests.StringIOWrapper()
 
150
        stderr = tests.StringIOWrapper()
 
151
        factory = _mod_ui_text.TextUIFactory(stdin, stdout, stderr)
220
152
        self.assertEqual(True, factory.get_boolean(""))
221
153
        self.assertEqual(False, factory.get_boolean(""))
222
154
        self.assertEqual(True, factory.get_boolean(""))
227
159
        # stdin should be empty
228
160
        self.assertEqual('', factory.stdin.readline())
229
161
 
 
162
    def test_text_ui_get_integer(self):
 
163
        stdin = tests.StringIOWrapper(
 
164
            "1\n"
 
165
            "  -2  \n"
 
166
            "hmmm\nwhat else ?\nCome on\nok 42\n4.24\n42\n")
 
167
        stdout = tests.StringIOWrapper()
 
168
        stderr = tests.StringIOWrapper()
 
169
        factory = _mod_ui_text.TextUIFactory(stdin, stdout, stderr)
 
170
        self.assertEqual(1, factory.get_integer(""))
 
171
        self.assertEqual(-2, factory.get_integer(""))
 
172
        self.assertEqual(42, factory.get_integer(""))
 
173
 
230
174
    def test_text_factory_prompt(self):
231
175
        # see <https://launchpad.net/bugs/365891>
232
 
        factory = TextUIFactory(StringIO(), StringIO(), StringIO())
 
176
        StringIO = tests.StringIOWrapper
 
177
        factory = _mod_ui_text.TextUIFactory(StringIO(), StringIO(), StringIO())
233
178
        factory.prompt('foo %2e')
234
179
        self.assertEqual('', factory.stdout.getvalue())
235
180
        self.assertEqual('foo %2e', factory.stderr.getvalue())
236
181
 
237
182
    def test_text_factory_prompts_and_clears(self):
238
183
        # a get_boolean call should clear the pb before prompting
239
 
        out = _TTYStringIO()
 
184
        out = test_progress._TTYStringIO()
240
185
        os.environ['TERM'] = 'xterm'
241
 
        factory = TextUIFactory(stdin=StringIO("yada\ny\n"), stdout=out, stderr=out)
 
186
        factory = _mod_ui_text.TextUIFactory(
 
187
            stdin=tests.StringIOWrapper("yada\ny\n"),
 
188
            stdout=out, stderr=out)
242
189
        pb = factory.nested_progress_bar()
243
190
        pb.show_bar = False
244
191
        pb.show_spinner = False
258
205
        self.assertEqual('', factory.stdin.readline())
259
206
 
260
207
    def test_text_tick_after_update(self):
261
 
        ui_factory = TextUIFactory(stdout=StringIO(), stderr=StringIO())
 
208
        ui_factory = _mod_ui_text.TextUIFactory(stdout=tests.StringIOWrapper(),
 
209
                                                stderr=tests.StringIOWrapper())
262
210
        pb = ui_factory.nested_progress_bar()
263
211
        try:
264
212
            pb.update('task', 0, 3)
269
217
            pb.finished()
270
218
 
271
219
    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()
 
220
        factory = _mod_ui_text.TextUIFactory(None, None, None)
 
221
        factory.stdin = tests.StringIOWrapper("someuser\n\n")
 
222
        factory.stdout = tests.StringIOWrapper()
 
223
        factory.stderr = tests.StringIOWrapper()
276
224
        factory.stdout.encoding = "utf8"
277
225
        # there is no output from the base factory
278
226
        self.assertEqual("someuser",
300
248
        finally:
301
249
            pb.finished()
302
250
 
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):
 
251
    def test_quietness(self):
 
252
        os.environ['BZR_PROGRESS_BAR'] = 'text'
 
253
        ui_factory = _mod_ui_text.TextUIFactory(None,
 
254
            test_progress._TTYStringIO(),
 
255
            test_progress._TTYStringIO())
 
256
        self.assertIsInstance(ui_factory._progress_view,
 
257
            _mod_ui_text.TextProgressView)
 
258
        ui_factory.be_quiet(True)
 
259
        self.assertIsInstance(ui_factory._progress_view,
 
260
            _mod_ui_text.NullProgressView)
 
261
 
 
262
 
 
263
class TestTextUIOutputStream(tests.TestCase):
 
264
    """Tests for output stream that synchronizes with progress bar."""
 
265
 
 
266
    def test_output_clears_terminal(self):
 
267
        stdout = tests.StringIOWrapper()
 
268
        stderr = tests.StringIOWrapper()
 
269
        clear_calls = []
 
270
 
 
271
        uif =  _mod_ui_text.TextUIFactory(None, stdout, stderr)
 
272
        uif.clear_term = lambda: clear_calls.append('clear')
 
273
 
 
274
        stream = _mod_ui_text.TextUIOutputStream(uif, uif.stdout)
 
275
        stream.write("Hello world!\n")
 
276
        stream.write("there's more...\n")
 
277
        stream.writelines(["1\n", "2\n", "3\n"])
 
278
 
 
279
        self.assertEqual(stdout.getvalue(),
 
280
            "Hello world!\n"
 
281
            "there's more...\n"
 
282
            "1\n2\n3\n")
 
283
        self.assertEqual(['clear', 'clear', 'clear'],
 
284
            clear_calls)
 
285
 
 
286
        stream.flush()
 
287
 
 
288
 
 
289
class UITests(tests.TestCase):
 
290
 
 
291
    def test_progress_construction(self):
 
292
        """TextUIFactory constructs the right progress view.
 
293
        """
 
294
        TTYStringIO = test_progress._TTYStringIO
 
295
        FileStringIO = tests.StringIOWrapper
 
296
        for (file_class, term, pb, expected_pb_class) in (
 
297
            # on an xterm, either use them or not as the user requests,
 
298
            # otherwise default on
 
299
            (TTYStringIO, 'xterm', 'none', _mod_ui_text.NullProgressView),
 
300
            (TTYStringIO, 'xterm', 'text', _mod_ui_text.TextProgressView),
 
301
            (TTYStringIO, 'xterm', None, _mod_ui_text.TextProgressView),
 
302
            # on a dumb terminal, again if there's explicit configuration do
 
303
            # it, otherwise default off
 
304
            (TTYStringIO, 'dumb', 'none', _mod_ui_text.NullProgressView),
 
305
            (TTYStringIO, 'dumb', 'text', _mod_ui_text.TextProgressView),
 
306
            (TTYStringIO, 'dumb', None, _mod_ui_text.NullProgressView),
 
307
            # on a non-tty terminal, it's null regardless of $TERM
 
308
            (FileStringIO, 'xterm', None, _mod_ui_text.NullProgressView),
 
309
            (FileStringIO, 'dumb', None, _mod_ui_text.NullProgressView),
 
310
            # however, it can still be forced on
 
311
            (FileStringIO, 'dumb', 'text', _mod_ui_text.TextProgressView),
 
312
            ):
 
313
            os.environ['TERM'] = term
 
314
            if pb is None:
 
315
                if 'BZR_PROGRESS_BAR' in os.environ:
 
316
                    del os.environ['BZR_PROGRESS_BAR']
 
317
            else:
 
318
                os.environ['BZR_PROGRESS_BAR'] = pb
 
319
            stdin = file_class('')
 
320
            stderr = file_class()
 
321
            stdout = file_class()
 
322
            uif = _mod_ui.make_ui_for_terminal(stdin, stdout, stderr)
 
323
            self.assertIsInstance(uif, _mod_ui_text.TextUIFactory,
 
324
                "TERM=%s BZR_PROGRESS_BAR=%s uif=%r" % (term, pb, uif,))
 
325
            self.assertIsInstance(uif.make_progress_view(),
 
326
                expected_pb_class,
 
327
                "TERM=%s BZR_PROGRESS_BAR=%s uif=%r" % (term, pb, uif,))
 
328
 
 
329
    def test_text_ui_non_terminal(self):
 
330
        """Even on non-ttys, make_ui_for_terminal gives a text ui."""
 
331
        stdin = test_progress._NonTTYStringIO('')
 
332
        stderr = test_progress._NonTTYStringIO()
 
333
        stdout = test_progress._NonTTYStringIO()
 
334
        for term_type in ['dumb', None, 'xterm']:
 
335
            if term_type is None:
 
336
                del os.environ['TERM']
 
337
            else:
 
338
                os.environ['TERM'] = term_type
 
339
            uif = _mod_ui.make_ui_for_terminal(stdin, stdout, stderr)
 
340
            self.assertIsInstance(uif, _mod_ui_text.TextUIFactory,
 
341
                'TERM=%r' % (term_type,))
 
342
 
 
343
 
 
344
class SilentUITests(tests.TestCase):
314
345
 
315
346
    def test_silent_factory_get_password(self):
316
347
        # A silent factory that can't do user interaction can't get a
317
348
        # password.  Possibly it should raise a more specific error but it
318
349
        # can't succeed.
319
 
        ui = SilentUIFactory()
320
 
        stdout = StringIO()
 
350
        ui = _mod_ui.SilentUIFactory()
 
351
        stdout = tests.StringIOWrapper()
321
352
        self.assertRaises(
322
353
            NotImplementedError,
323
354
            self.apply_redirected,
326
357
        self.assertEqual('', stdout.getvalue())
327
358
 
328
359
    def test_silent_ui_getbool(self):
329
 
        factory = SilentUIFactory()
330
 
        stdout = StringIO()
 
360
        factory = _mod_ui.SilentUIFactory()
 
361
        stdout = tests.StringIOWrapper()
331
362
        self.assertRaises(
332
363
            NotImplementedError,
333
364
            self.apply_redirected,
334
365
            None, stdout, stdout, factory.get_boolean, "foo")
335
366
 
336
367
 
337
 
class CannedInputUIFactoryTests(TestCase):
338
 
    
 
368
class TestUIFactoryTests(tests.TestCase):
 
369
 
 
370
    def test_test_ui_factory_progress(self):
 
371
        # there's no output; we just want to make sure this doesn't crash -
 
372
        # see https://bugs.edge.launchpad.net/bzr/+bug/408201
 
373
        ui = tests.TestUIFactory()
 
374
        pb = ui.nested_progress_bar()
 
375
        pb.update('hello')
 
376
        pb.tick()
 
377
        pb.finished()
 
378
 
 
379
 
 
380
class CannedInputUIFactoryTests(tests.TestCase):
 
381
 
339
382
    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')
 
383
        uif = _mod_ui.CannedInputUIFactory([True, 'mbp', 'password', 42])
 
384
        self.assertEqual(True, uif.get_boolean('Extra cheese?'))
 
385
        self.assertEqual('mbp', uif.get_username('Enter your user name'))
 
386
        self.assertEqual('password',
 
387
                         uif.get_password('Password for %(host)s',
 
388
                                          host='example.com'))
 
389
        self.assertEqual(42, uif.get_integer('And all that jazz ?'))
345
390
 
346
391
 
347
392
class TestBoolFromString(tests.TestCase):