/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

merge bzr.dev into cleanup

Show diffs side-by-side

added added

removed removed

Lines of Context:
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
 
    SilentUIFactory,
46
 
    UIFactory,
47
 
    make_ui_for_terminal,
48
 
    )
49
 
from bzrlib.ui.text import (
50
 
    NullProgressView,
51
 
    TextProgressView,
52
 
    TextUIFactory,
53
 
    )
 
32
from bzrlib.tests import test_progress
 
33
from bzrlib.ui import text as _mod_ui_text
54
34
 
55
35
 
56
36
class TestTextUIFactory(tests.TestCase):
100
80
            pb.finished()
101
81
 
102
82
    def test_progress_note(self):
103
 
        stderr = StringIO()
104
 
        stdout = StringIO()
105
 
        ui_factory = TextUIFactory(stdin=StringIO(''),
106
 
            stderr=stderr,
107
 
            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)
108
88
        pb = ui_factory.nested_progress_bar()
109
89
        try:
110
90
            result = self.applyDeprecated(deprecated_in((2, 1, 0)),
120
100
            pb.finished()
121
101
 
122
102
    def test_progress_note_clears(self):
123
 
        stderr = _TTYStringIO()
124
 
        stdout = _TTYStringIO()
 
103
        stderr = test_progress._TTYStringIO()
 
104
        stdout = test_progress._TTYStringIO()
125
105
        # so that we get a TextProgressBar
126
106
        os.environ['TERM'] = 'xterm'
127
 
        ui_factory = TextUIFactory(
128
 
            stdin=StringIO(''),
 
107
        ui_factory = _mod_ui_text.TextUIFactory(
 
108
            stdin=tests.StringIOWrapper(''),
129
109
            stdout=stdout, stderr=stderr)
130
110
        self.assertIsInstance(ui_factory._progress_view,
131
 
            TextProgressView)
 
111
                              _mod_ui_text.TextProgressView)
132
112
        pb = ui_factory.nested_progress_bar()
133
113
        try:
134
114
            # Create a progress update that isn't throttled
146
126
 
147
127
    def test_progress_nested(self):
148
128
        # test factory based nested and popping.
149
 
        ui = TextUIFactory(None, None, None)
 
129
        ui = _mod_ui_text.TextUIFactory(None, None, None)
150
130
        pb1 = ui.nested_progress_bar()
151
131
        pb2 = ui.nested_progress_bar()
152
132
        # You do get a warning if the outermost progress bar wasn't finished
159
139
        pb1.finished()
160
140
 
161
141
    def test_text_ui_get_boolean(self):
162
 
        stdin = StringIO("y\n" # True
163
 
                         "n\n" # False
164
 
                         "yes with garbage\nY\n" # True
165
 
                         "not an answer\nno\n" # False
166
 
                         "I'm sure!\nyes\n" # True
167
 
                         "NO\n" # False
168
 
                         "foo\n")
169
 
        stdout = StringIO()
170
 
        stderr = StringIO()
171
 
        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)
172
152
        self.assertEqual(True, factory.get_boolean(""))
173
153
        self.assertEqual(False, factory.get_boolean(""))
174
154
        self.assertEqual(True, factory.get_boolean(""))
181
161
 
182
162
    def test_text_factory_prompt(self):
183
163
        # see <https://launchpad.net/bugs/365891>
184
 
        factory = TextUIFactory(StringIO(), StringIO(), StringIO())
 
164
        StringIO = tests.StringIOWrapper
 
165
        factory = _mod_ui_text.TextUIFactory(StringIO(), StringIO(), StringIO())
185
166
        factory.prompt('foo %2e')
186
167
        self.assertEqual('', factory.stdout.getvalue())
187
168
        self.assertEqual('foo %2e', factory.stderr.getvalue())
188
169
 
189
170
    def test_text_factory_prompts_and_clears(self):
190
171
        # a get_boolean call should clear the pb before prompting
191
 
        out = _TTYStringIO()
 
172
        out = test_progress._TTYStringIO()
192
173
        os.environ['TERM'] = 'xterm'
193
 
        factory = TextUIFactory(stdin=StringIO("yada\ny\n"), stdout=out, stderr=out)
 
174
        factory = _mod_ui_text.TextUIFactory(
 
175
            stdin=tests.StringIOWrapper("yada\ny\n"),
 
176
            stdout=out, stderr=out)
194
177
        pb = factory.nested_progress_bar()
195
178
        pb.show_bar = False
196
179
        pb.show_spinner = False
210
193
        self.assertEqual('', factory.stdin.readline())
211
194
 
212
195
    def test_text_tick_after_update(self):
213
 
        ui_factory = TextUIFactory(stdout=StringIO(), stderr=StringIO())
 
196
        ui_factory = _mod_ui_text.TextUIFactory(stdout=tests.StringIOWrapper(),
 
197
                                                stderr=tests.StringIOWrapper())
214
198
        pb = ui_factory.nested_progress_bar()
215
199
        try:
216
200
            pb.update('task', 0, 3)
221
205
            pb.finished()
222
206
 
223
207
    def test_text_ui_getusername(self):
224
 
        factory = TextUIFactory(None, None, None)
225
 
        factory.stdin = StringIO("someuser\n\n")
226
 
        factory.stdout = StringIO()
227
 
        factory.stderr = StringIO()
 
208
        factory = _mod_ui_text.TextUIFactory(None, None, None)
 
209
        factory.stdin = tests.StringIOWrapper("someuser\n\n")
 
210
        factory.stdout = tests.StringIOWrapper()
 
211
        factory.stderr = tests.StringIOWrapper()
228
212
        factory.stdout.encoding = "utf8"
229
213
        # there is no output from the base factory
230
214
        self.assertEqual("someuser",
253
237
            pb.finished()
254
238
 
255
239
 
 
240
class TestTextUIOutputStream(tests.TestCase):
 
241
    """Tests for output stream that synchronizes with progress bar."""
 
242
 
 
243
    def test_output_clears_terminal(self):
 
244
        stdout = tests.StringIOWrapper()
 
245
        stderr = tests.StringIOWrapper()
 
246
        clear_calls = []
 
247
 
 
248
        uif =  _mod_ui_text.TextUIFactory(None, stdout, stderr)
 
249
        uif.clear_term = lambda: clear_calls.append('clear')
 
250
 
 
251
        stream = _mod_ui_text.TextUIOutputStream(uif, uif.stdout)
 
252
        stream.write("Hello world!\n")
 
253
        stream.write("there's more...\n")
 
254
        stream.writelines(["1\n", "2\n", "3\n"])
 
255
 
 
256
        self.assertEqual(stdout.getvalue(),
 
257
            "Hello world!\n"
 
258
            "there's more...\n"
 
259
            "1\n2\n3\n")
 
260
        self.assertEqual(['clear', 'clear', 'clear'],
 
261
            clear_calls)
 
262
 
 
263
        stream.flush()
 
264
 
 
265
 
256
266
class UITests(tests.TestCase):
257
267
 
258
268
    def test_progress_construction(self):
259
269
        """TextUIFactory constructs the right progress view.
260
270
        """
 
271
        TTYStringIO = test_progress._TTYStringIO
 
272
        FileStringIO = tests.StringIOWrapper
261
273
        for (file_class, term, pb, expected_pb_class) in (
262
274
            # on an xterm, either use them or not as the user requests,
263
275
            # otherwise default on
264
 
            (_TTYStringIO, 'xterm', 'none', NullProgressView),
265
 
            (_TTYStringIO, 'xterm', 'text', TextProgressView),
266
 
            (_TTYStringIO, 'xterm', None, TextProgressView),
 
276
            (TTYStringIO, 'xterm', 'none', _mod_ui_text.NullProgressView),
 
277
            (TTYStringIO, 'xterm', 'text', _mod_ui_text.TextProgressView),
 
278
            (TTYStringIO, 'xterm', None, _mod_ui_text.TextProgressView),
267
279
            # on a dumb terminal, again if there's explicit configuration do
268
280
            # it, otherwise default off
269
 
            (_TTYStringIO, 'dumb', 'none', NullProgressView),
270
 
            (_TTYStringIO, 'dumb', 'text', TextProgressView),
271
 
            (_TTYStringIO, 'dumb', None, NullProgressView),
 
281
            (TTYStringIO, 'dumb', 'none', _mod_ui_text.NullProgressView),
 
282
            (TTYStringIO, 'dumb', 'text', _mod_ui_text.TextProgressView),
 
283
            (TTYStringIO, 'dumb', None, _mod_ui_text.NullProgressView),
272
284
            # on a non-tty terminal, it's null regardless of $TERM
273
 
            (StringIO, 'xterm', None, NullProgressView),
274
 
            (StringIO, 'dumb', None, NullProgressView),
 
285
            (FileStringIO, 'xterm', None, _mod_ui_text.NullProgressView),
 
286
            (FileStringIO, 'dumb', None, _mod_ui_text.NullProgressView),
275
287
            # however, it can still be forced on
276
 
            (StringIO, 'dumb', 'text', TextProgressView),
 
288
            (FileStringIO, 'dumb', 'text', _mod_ui_text.TextProgressView),
277
289
            ):
278
290
            os.environ['TERM'] = term
279
291
            if pb is None:
284
296
            stdin = file_class('')
285
297
            stderr = file_class()
286
298
            stdout = file_class()
287
 
            uif = make_ui_for_terminal(stdin, stdout, stderr)
288
 
            self.assertIsInstance(uif, TextUIFactory,
 
299
            uif = _mod_ui.make_ui_for_terminal(stdin, stdout, stderr)
 
300
            self.assertIsInstance(uif, _mod_ui_text.TextUIFactory,
289
301
                "TERM=%s BZR_PROGRESS_BAR=%s uif=%r" % (term, pb, uif,))
290
302
            self.assertIsInstance(uif.make_progress_view(),
291
303
                expected_pb_class,
293
305
 
294
306
    def test_text_ui_non_terminal(self):
295
307
        """Even on non-ttys, make_ui_for_terminal gives a text ui."""
296
 
        stdin = _NonTTYStringIO('')
297
 
        stderr = _NonTTYStringIO()
298
 
        stdout = _NonTTYStringIO()
 
308
        stdin = test_progress._NonTTYStringIO('')
 
309
        stderr = test_progress._NonTTYStringIO()
 
310
        stdout = test_progress._NonTTYStringIO()
299
311
        for term_type in ['dumb', None, 'xterm']:
300
312
            if term_type is None:
301
313
                del os.environ['TERM']
302
314
            else:
303
315
                os.environ['TERM'] = term_type
304
 
            uif = make_ui_for_terminal(stdin, stdout, stderr)
305
 
            self.assertIsInstance(uif, TextUIFactory,
 
316
            uif = _mod_ui.make_ui_for_terminal(stdin, stdout, stderr)
 
317
            self.assertIsInstance(uif, _mod_ui_text.TextUIFactory,
306
318
                'TERM=%r' % (term_type,))
307
319
 
308
320
 
309
 
class SilentUITests(TestCase):
 
321
class SilentUITests(tests.TestCase):
310
322
 
311
323
    def test_silent_factory_get_password(self):
312
324
        # A silent factory that can't do user interaction can't get a
313
325
        # password.  Possibly it should raise a more specific error but it
314
326
        # can't succeed.
315
 
        ui = SilentUIFactory()
316
 
        stdout = StringIO()
 
327
        ui = _mod_ui.SilentUIFactory()
 
328
        stdout = tests.StringIOWrapper()
317
329
        self.assertRaises(
318
330
            NotImplementedError,
319
331
            self.apply_redirected,
322
334
        self.assertEqual('', stdout.getvalue())
323
335
 
324
336
    def test_silent_ui_getbool(self):
325
 
        factory = SilentUIFactory()
326
 
        stdout = StringIO()
 
337
        factory = _mod_ui.SilentUIFactory()
 
338
        stdout = tests.StringIOWrapper()
327
339
        self.assertRaises(
328
340
            NotImplementedError,
329
341
            self.apply_redirected,
330
342
            None, stdout, stdout, factory.get_boolean, "foo")
331
343
 
332
344
 
333
 
class TestUIFactoryTests(TestCase):
 
345
class TestUIFactoryTests(tests.TestCase):
334
346
 
335
347
    def test_test_ui_factory_progress(self):
336
348
        # there's no output; we just want to make sure this doesn't crash -
337
349
        # see https://bugs.edge.launchpad.net/bzr/+bug/408201
338
 
        ui = TestUIFactory()
 
350
        ui = tests.TestUIFactory()
339
351
        pb = ui.nested_progress_bar()
340
352
        pb.update('hello')
341
353
        pb.tick()
342
354
        pb.finished()
343
355
 
344
356
 
345
 
class CannedInputUIFactoryTests(TestCase):
346
 
    
 
357
class CannedInputUIFactoryTests(tests.TestCase):
 
358
 
347
359
    def test_canned_input_get_input(self):
348
 
        uif = CannedInputUIFactory([True, 'mbp', 'password'])
349
 
        self.assertEqual(uif.get_boolean('Extra cheese?'), True)
350
 
        self.assertEqual(uif.get_username('Enter your user name'), 'mbp')
351
 
        self.assertEqual(uif.get_password('Password for %(host)s', host='example.com'),
352
 
            'password')
 
360
        uif = _mod_ui.CannedInputUIFactory([True, 'mbp', 'password', 42])
 
361
        self.assertEqual(True, uif.get_boolean('Extra cheese?'))
 
362
        self.assertEqual('mbp', uif.get_username('Enter your user name'))
 
363
        self.assertEqual('password',
 
364
                         uif.get_password('Password for %(host)s',
 
365
                                          host='example.com'))
353
366
 
354
367
 
355
368
class TestBoolFromString(tests.TestCase):