/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-06-17 17:57:15 UTC
  • mfrom: (4454 +trunk)
  • mto: This revision was merged to the branch mainline in revision 4460.
  • Revision ID: john@arbash-meinel.com-20090617175715-p9ebpwx5rhc0qin1
Merge bzr.dev 4454 in preparation for NEWS entry.

Show diffs side-by-side

added added

removed removed

Lines of Context:
25
25
 
26
26
import bzrlib
27
27
import bzrlib.errors as errors
28
 
from bzrlib.progress import (
29
 
    DotsProgressBar,
30
 
    ProgressBarStack,
31
 
    ProgressTask,
32
 
    TTYProgressBar,
33
 
    )
34
28
from bzrlib.symbol_versioning import (
35
29
    deprecated_in,
36
30
    )
45
39
    SilentUIFactory,
46
40
    )
47
41
from bzrlib.ui.text import (
 
42
    NullProgressView,
48
43
    TextProgressView,
49
44
    TextUIFactory,
50
45
    )
67
62
        self.assertEqual('', stdout.getvalue())
68
63
 
69
64
    def test_text_factory_ascii_password(self):
70
 
        ui = TestUIFactory(stdin='secret\n', stdout=StringIOWrapper())
 
65
        ui = TestUIFactory(stdin='secret\n', stdout=StringIOWrapper(),
 
66
                           stderr=StringIOWrapper())
71
67
        pb = ui.nested_progress_bar()
72
68
        try:
73
69
            self.assertEqual('secret',
74
70
                             self.apply_redirected(ui.stdin, ui.stdout,
75
 
                                                   ui.stdout,
 
71
                                                   ui.stderr,
76
72
                                                   ui.get_password))
77
73
            # ': ' is appended to prompt
78
 
            self.assertEqual(': ', ui.stdout.getvalue())
 
74
            self.assertEqual(': ', ui.stderr.getvalue())
 
75
            self.assertEqual('', ui.stdout.readline())
79
76
            # stdin should be empty
80
77
            self.assertEqual('', ui.stdin.readline())
81
78
        finally:
88
85
        it to utf8 to test that we transport the password correctly.
89
86
        """
90
87
        ui = TestUIFactory(stdin=u'baz\u1234'.encode('utf8'),
91
 
                           stdout=StringIOWrapper())
92
 
        ui.stdin.encoding = 'utf8'
93
 
        ui.stdout.encoding = ui.stdin.encoding
 
88
                           stdout=StringIOWrapper(),
 
89
                           stderr=StringIOWrapper())
 
90
        ui.stderr.encoding = ui.stdout.encoding = ui.stdin.encoding = 'utf8'
94
91
        pb = ui.nested_progress_bar()
95
92
        try:
96
 
            password = self.apply_redirected(ui.stdin, ui.stdout, ui.stdout,
 
93
            password = self.apply_redirected(ui.stdin, ui.stdout, ui.stderr,
97
94
                                             ui.get_password,
98
95
                                             u'Hello \u1234 %(user)s',
99
96
                                             user=u'some\u1234')
100
97
            # We use StringIO objects, we need to decode them
101
98
            self.assertEqual(u'baz\u1234', password.decode('utf8'))
102
99
            self.assertEqual(u'Hello \u1234 some\u1234: ',
103
 
                             ui.stdout.getvalue().decode('utf8'))
104
 
            # stdin should be empty
 
100
                             ui.stderr.getvalue().decode('utf8'))
 
101
            # stdin and stdout should be empty
105
102
            self.assertEqual('', ui.stdin.readline())
 
103
            self.assertEqual('', ui.stdout.readline())
106
104
        finally:
107
105
            pb.finished()
108
106
 
 
107
    def test_progress_construction(self):
 
108
        """TextUIFactory constructs the right progress view.
 
109
        """
 
110
        os.environ['BZR_PROGRESS_BAR'] = 'none'
 
111
        self.assertIsInstance(TextUIFactory()._progress_view,
 
112
            NullProgressView)
 
113
 
 
114
        os.environ['BZR_PROGRESS_BAR'] = 'text'
 
115
        self.assertIsInstance(TextUIFactory()._progress_view,
 
116
            TextProgressView)
 
117
 
 
118
        os.environ['BZR_PROGRESS_BAR'] = 'text'
 
119
        self.assertIsInstance(TextUIFactory()._progress_view,
 
120
            TextProgressView)
 
121
 
 
122
        del os.environ['BZR_PROGRESS_BAR']
 
123
        self.assertIsInstance(TextUIFactory()._progress_view,
 
124
            TextProgressView)
 
125
 
109
126
    def test_progress_note(self):
110
127
        stderr = StringIO()
111
128
        stdout = StringIO()
161
178
        pb2.finished()
162
179
        pb1.finished()
163
180
 
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
181
    def assert_get_bool_acceptance_of_user_input(self, factory):
192
182
        factory.stdin = StringIO("y\nyes with garbage\n"
193
183
                                 "yes\nn\nnot an answer\n"
194
184
                                 "no\nfoo\n")
195
185
        factory.stdout = StringIO()
 
186
        factory.stderr = StringIO()
196
187
        # there is no output from the base factory
197
188
        self.assertEqual(True, factory.get_boolean(""))
198
189
        self.assertEqual(True, factory.get_boolean(""))
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
263
256
        factory = SilentUIFactory()
264
257
        factory.stdin = StringIO("someuser\n\n")
265
258
        factory.stdout = StringIO()
266
 
        self.assertEquals(None, 
 
259
        factory.stderr = StringIO()
 
260
        self.assertEquals(None,
267
261
            factory.get_username(u'Hello\u1234 %(host)s', host=u'some\u1234'))
268
262
        self.assertEquals("", factory.stdout.getvalue())
 
263
        self.assertEquals("", factory.stderr.getvalue())
269
264
        self.assertEquals("someuser\n\n", factory.stdin.getvalue())
270
265
 
271
266
    def test_text_ui_getusername(self):
272
267
        factory = TextUIFactory(None, None, None)
273
268
        factory.stdin = StringIO("someuser\n\n")
274
269
        factory.stdout = StringIO()
 
270
        factory.stderr = StringIO()
275
271
        factory.stdout.encoding = "utf8"
276
272
        # 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())
 
273
        self.assertEqual("someuser",
 
274
                         factory.get_username('Hello %(host)s', host='some'))
 
275
        self.assertEquals("Hello some: ", factory.stderr.getvalue())
 
276
        self.assertEquals('', factory.stdout.getvalue())
280
277
        self.assertEqual("", factory.get_username("Gebruiker"))
281
278
        # stdin should be empty
282
279
        self.assertEqual('', factory.stdin.readline())
283
280
 
284
281
    def test_text_ui_getusername_utf8(self):
285
282
        ui = TestUIFactory(stdin=u'someuser\u1234'.encode('utf8'),
286
 
                           stdout=StringIOWrapper())
287
 
        ui.stdin.encoding = "utf8"
288
 
        ui.stdout.encoding = ui.stdin.encoding
 
283
                           stdout=StringIOWrapper(), stderr=StringIOWrapper())
 
284
        ui.stderr.encoding = ui.stdout.encoding = ui.stdin.encoding = "utf8"
289
285
        pb = ui.nested_progress_bar()
290
286
        try:
291
287
            # there is no output from the base factory
292
 
            username = self.apply_redirected(ui.stdin, ui.stdout, ui.stdout,
 
288
            username = self.apply_redirected(ui.stdin, ui.stdout, ui.stderr,
293
289
                ui.get_username, u'Hello\u1234 %(host)s', host=u'some\u1234')
294
290
            self.assertEquals(u"someuser\u1234", username.decode('utf8'))
295
 
            self.assertEquals(u"Hello\u1234 some\u1234: ", 
296
 
                ui.stdout.getvalue().decode("utf8"))
 
291
            self.assertEquals(u"Hello\u1234 some\u1234: ",
 
292
                              ui.stderr.getvalue().decode("utf8"))
 
293
            self.assertEquals('', ui.stdout.getvalue())
297
294
        finally:
298
295
            pb.finished()
299
296