/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: John Arbash Meinel
  • Date: 2009-07-08 14:37:25 UTC
  • mfrom: (4516 +trunk)
  • mto: This revision was merged to the branch mainline in revision 4517.
  • Revision ID: john@arbash-meinel.com-20090708143725-sc9sjy3mz4cxwxzz
Merge bzr.dev 4516

Show diffs side-by-side

added added

removed removed

Lines of Context:
23
23
import sys
24
24
import time
25
25
 
26
 
import bzrlib
27
 
import bzrlib.errors as errors
28
 
from bzrlib.progress import (
29
 
    DotsProgressBar,
30
 
    ProgressBarStack,
31
 
    ProgressTask,
32
 
    TTYProgressBar,
 
26
from bzrlib import (
 
27
    errors,
 
28
    tests,
 
29
    ui as _mod_ui,
33
30
    )
34
31
from bzrlib.symbol_versioning import (
35
32
    deprecated_in,
36
33
    )
37
 
from bzrlib.tests import (
38
 
    TestCase,
39
 
    TestUIFactory,
40
 
    StringIOWrapper,
41
 
    )
42
34
from bzrlib.tests.test_progress import _TTYStringIO
43
 
from bzrlib.ui import (
44
 
    CLIUIFactory,
45
 
    SilentUIFactory,
46
 
    )
47
35
from bzrlib.ui.text import (
 
36
    NullProgressView,
48
37
    TextProgressView,
49
38
    TextUIFactory,
50
39
    )
51
40
 
52
41
 
53
 
class UITests(TestCase):
 
42
class UITests(tests.TestCase):
54
43
 
55
44
    def test_silent_factory(self):
56
 
        ui = SilentUIFactory()
 
45
        ui = _mod_ui.SilentUIFactory()
57
46
        stdout = StringIO()
58
47
        self.assertEqual(None,
59
48
                         self.apply_redirected(None, stdout, stdout,
67
56
        self.assertEqual('', stdout.getvalue())
68
57
 
69
58
    def test_text_factory_ascii_password(self):
70
 
        ui = TestUIFactory(stdin='secret\n', stdout=StringIOWrapper())
 
59
        ui = tests.TestUIFactory(stdin='secret\n',
 
60
                                 stdout=tests.StringIOWrapper(),
 
61
                                 stderr=tests.StringIOWrapper())
71
62
        pb = ui.nested_progress_bar()
72
63
        try:
73
64
            self.assertEqual('secret',
74
65
                             self.apply_redirected(ui.stdin, ui.stdout,
75
 
                                                   ui.stdout,
 
66
                                                   ui.stderr,
76
67
                                                   ui.get_password))
77
68
            # ': ' is appended to prompt
78
 
            self.assertEqual(': ', ui.stdout.getvalue())
 
69
            self.assertEqual(': ', ui.stderr.getvalue())
 
70
            self.assertEqual('', ui.stdout.readline())
79
71
            # stdin should be empty
80
72
            self.assertEqual('', ui.stdin.readline())
81
73
        finally:
87
79
        We can't predict what encoding users will have for stdin, so we force
88
80
        it to utf8 to test that we transport the password correctly.
89
81
        """
90
 
        ui = TestUIFactory(stdin=u'baz\u1234'.encode('utf8'),
91
 
                           stdout=StringIOWrapper())
92
 
        ui.stdin.encoding = 'utf8'
93
 
        ui.stdout.encoding = ui.stdin.encoding
 
82
        ui = tests.TestUIFactory(stdin=u'baz\u1234'.encode('utf8'),
 
83
                                 stdout=tests.StringIOWrapper(),
 
84
                                 stderr=tests.StringIOWrapper())
 
85
        ui.stderr.encoding = ui.stdout.encoding = ui.stdin.encoding = 'utf8'
94
86
        pb = ui.nested_progress_bar()
95
87
        try:
96
 
            password = self.apply_redirected(ui.stdin, ui.stdout, ui.stdout,
 
88
            password = self.apply_redirected(ui.stdin, ui.stdout, ui.stderr,
97
89
                                             ui.get_password,
98
90
                                             u'Hello \u1234 %(user)s',
99
91
                                             user=u'some\u1234')
100
92
            # We use StringIO objects, we need to decode them
101
93
            self.assertEqual(u'baz\u1234', password.decode('utf8'))
102
94
            self.assertEqual(u'Hello \u1234 some\u1234: ',
103
 
                             ui.stdout.getvalue().decode('utf8'))
104
 
            # stdin should be empty
 
95
                             ui.stderr.getvalue().decode('utf8'))
 
96
            # stdin and stdout should be empty
105
97
            self.assertEqual('', ui.stdin.readline())
 
98
            self.assertEqual('', ui.stdout.readline())
106
99
        finally:
107
100
            pb.finished()
108
101
 
 
102
    def test_progress_construction(self):
 
103
        """TextUIFactory constructs the right progress view.
 
104
        """
 
105
        os.environ['BZR_PROGRESS_BAR'] = 'none'
 
106
        self.assertIsInstance(TextUIFactory()._progress_view,
 
107
            NullProgressView)
 
108
 
 
109
        os.environ['BZR_PROGRESS_BAR'] = 'text'
 
110
        self.assertIsInstance(TextUIFactory()._progress_view,
 
111
            TextProgressView)
 
112
 
 
113
        os.environ['BZR_PROGRESS_BAR'] = 'text'
 
114
        self.assertIsInstance(TextUIFactory()._progress_view,
 
115
            TextProgressView)
 
116
 
 
117
        del os.environ['BZR_PROGRESS_BAR']
 
118
        self.assertIsInstance(TextUIFactory()._progress_view,
 
119
            TextProgressView)
 
120
 
109
121
    def test_progress_note(self):
110
122
        stderr = StringIO()
111
123
        stdout = StringIO()
161
173
        pb2.finished()
162
174
        pb1.finished()
163
175
 
164
 
    def test_progress_stack(self):
165
 
        # test the progress bar stack which the default text factory
166
 
        # uses.
167
 
        stderr = StringIO()
168
 
        stdout = StringIO()
169
 
        # make a stack, which accepts parameters like a pb.
170
 
        stack = self.applyDeprecated(
171
 
            deprecated_in((1, 12, 0)),
172
 
            ProgressBarStack,
173
 
            to_file=stderr, to_messages_file=stdout)
174
 
        # but is not one
175
 
        self.assertFalse(getattr(stack, 'note', False))
176
 
        pb1 = stack.get_nested()
177
 
        pb2 = stack.get_nested()
178
 
        warnings, _ = self.callCatchWarnings(pb1.finished)
179
 
        self.assertEqual(len(warnings), 1)
180
 
        pb2.finished()
181
 
        pb1.finished()
182
 
        # the text ui factory never actually removes the stack once its setup.
183
 
        # we need to be able to nest again correctly from here.
184
 
        pb1 = stack.get_nested()
185
 
        pb2 = stack.get_nested()
186
 
        warnings, _ = self.callCatchWarnings(pb1.finished)
187
 
        self.assertEqual(len(warnings), 1)
188
 
        pb2.finished()
189
 
        pb1.finished()
190
 
 
191
176
    def assert_get_bool_acceptance_of_user_input(self, factory):
192
177
        factory.stdin = StringIO("y\nyes with garbage\n"
193
178
                                 "yes\nn\nnot an answer\n"
194
 
                                 "no\nfoo\n")
 
179
                                 "no\n"
 
180
                                 "N\nY\n"
 
181
                                 "foo\n"
 
182
                                )
195
183
        factory.stdout = StringIO()
 
184
        factory.stderr = StringIO()
196
185
        # there is no output from the base factory
197
186
        self.assertEqual(True, factory.get_boolean(""))
198
187
        self.assertEqual(True, factory.get_boolean(""))
199
188
        self.assertEqual(False, factory.get_boolean(""))
200
189
        self.assertEqual(False, factory.get_boolean(""))
 
190
        self.assertEqual(False, factory.get_boolean(""))
 
191
        self.assertEqual(True, factory.get_boolean(""))
201
192
        self.assertEqual("foo\n", factory.stdin.read())
202
193
        # stdin should be empty
203
194
        self.assertEqual('', factory.stdin.readline())
204
195
 
205
196
    def test_silent_ui_getbool(self):
206
 
        factory = SilentUIFactory()
 
197
        factory = _mod_ui.SilentUIFactory()
207
198
        self.assert_get_bool_acceptance_of_user_input(factory)
208
199
 
209
200
    def test_silent_factory_prompts_silently(self):
210
 
        factory = SilentUIFactory()
 
201
        factory = _mod_ui.SilentUIFactory()
211
202
        stdout = StringIO()
212
203
        factory.stdin = StringIO("y\n")
213
204
        self.assertEqual(True,
223
214
 
224
215
    def test_text_factory_prompt(self):
225
216
        # see <https://launchpad.net/bugs/365891>
226
 
        factory = TextUIFactory(None, StringIO(), StringIO())
 
217
        factory = TextUIFactory(None, StringIO(), StringIO(), StringIO())
227
218
        factory.prompt('foo %2e')
 
219
        self.assertEqual('', factory.stdout.getvalue())
 
220
        self.assertEqual('foo %2e', factory.stderr.getvalue())
228
221
 
229
222
    def test_text_factory_prompts_and_clears(self):
230
223
        # a get_boolean call should clear the pb before prompting
231
224
        out = _TTYStringIO()
232
 
        factory = TextUIFactory(stdin=StringIO("yada\ny\n"), stdout=out, stderr=out)
 
225
        factory = TextUIFactory(stdin=StringIO("yada\ny\n"),
 
226
                                stdout=out, stderr=out)
233
227
        pb = factory.nested_progress_bar()
234
228
        pb.show_bar = False
235
229
        pb.show_spinner = False
260
254
            pb.finished()
261
255
 
262
256
    def test_silent_ui_getusername(self):
263
 
        factory = SilentUIFactory()
 
257
        factory = _mod_ui.SilentUIFactory()
264
258
        factory.stdin = StringIO("someuser\n\n")
265
259
        factory.stdout = StringIO()
266
 
        self.assertEquals(None, 
 
260
        factory.stderr = StringIO()
 
261
        self.assertEquals(None,
267
262
            factory.get_username(u'Hello\u1234 %(host)s', host=u'some\u1234'))
268
263
        self.assertEquals("", factory.stdout.getvalue())
 
264
        self.assertEquals("", factory.stderr.getvalue())
269
265
        self.assertEquals("someuser\n\n", factory.stdin.getvalue())
270
266
 
271
267
    def test_text_ui_getusername(self):
272
268
        factory = TextUIFactory(None, None, None)
273
269
        factory.stdin = StringIO("someuser\n\n")
274
270
        factory.stdout = StringIO()
 
271
        factory.stderr = StringIO()
275
272
        factory.stdout.encoding = "utf8"
276
273
        # there is no output from the base factory
277
 
        self.assertEqual("someuser", 
278
 
            factory.get_username('Hello %(host)s', host='some'))
279
 
        self.assertEquals("Hello some: ", factory.stdout.getvalue())
 
274
        self.assertEqual("someuser",
 
275
                         factory.get_username('Hello %(host)s', host='some'))
 
276
        self.assertEquals("Hello some: ", factory.stderr.getvalue())
 
277
        self.assertEquals('', factory.stdout.getvalue())
280
278
        self.assertEqual("", factory.get_username("Gebruiker"))
281
279
        # stdin should be empty
282
280
        self.assertEqual('', factory.stdin.readline())
283
281
 
284
282
    def test_text_ui_getusername_utf8(self):
285
 
        ui = TestUIFactory(stdin=u'someuser\u1234'.encode('utf8'),
286
 
                           stdout=StringIOWrapper())
287
 
        ui.stdin.encoding = "utf8"
288
 
        ui.stdout.encoding = ui.stdin.encoding
 
283
        ui = tests.TestUIFactory(stdin=u'someuser\u1234'.encode('utf8'),
 
284
                                 stdout=tests.StringIOWrapper(),
 
285
                                 stderr=tests.StringIOWrapper())
 
286
        ui.stderr.encoding = ui.stdout.encoding = ui.stdin.encoding = "utf8"
289
287
        pb = ui.nested_progress_bar()
290
288
        try:
291
289
            # there is no output from the base factory
292
 
            username = self.apply_redirected(ui.stdin, ui.stdout, ui.stdout,
 
290
            username = self.apply_redirected(ui.stdin, ui.stdout, ui.stderr,
293
291
                ui.get_username, u'Hello\u1234 %(host)s', host=u'some\u1234')
294
292
            self.assertEquals(u"someuser\u1234", username.decode('utf8'))
295
 
            self.assertEquals(u"Hello\u1234 some\u1234: ", 
296
 
                ui.stdout.getvalue().decode("utf8"))
 
293
            self.assertEquals(u"Hello\u1234 some\u1234: ",
 
294
                              ui.stderr.getvalue().decode("utf8"))
 
295
            self.assertEquals('', ui.stdout.getvalue())
297
296
        finally:
298
297
            pb.finished()
299
298
 
300
299
 
301
 
class TestTextProgressView(TestCase):
 
300
class TestTextProgressView(tests.TestCase):
302
301
    """Tests for text display of progress bars.
303
302
    """
304
303
    # XXX: These might be a bit easier to write if the rendering and