~bzr-pqm/bzr/bzr.dev

« back to all changes in this revision

Viewing changes to bzrlib/tests/test_ui.py

  • Committer: John Arbash Meinel
  • Date: 2007-11-13 20:37:09 UTC
  • mto: This revision was merged to the branch mainline in revision 3001.
  • Revision ID: john@arbash-meinel.com-20071113203709-kysdte0emqv84pnj
Fix bug #162486, by having RemoteBranch properly initialize self._revision_id_to_revno_map.

Show diffs side-by-side

added added

removed removed

Lines of Context:
1
 
# Copyright (C) 2005 by Canonical Ltd
 
1
# Copyright (C) 2005 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
import re
21
23
import sys
22
24
 
23
 
from bzrlib.tests import TestCase
24
 
from bzrlib.ui import SilentUIFactory
 
25
import bzrlib
 
26
import bzrlib.errors as errors
 
27
from bzrlib.progress import (
 
28
    DotsProgressBar,
 
29
    ProgressBarStack,
 
30
    TTYProgressBar,
 
31
    )
 
32
from bzrlib.tests import (
 
33
    TestCase,
 
34
    TestUIFactory,
 
35
    StringIOWrapper,
 
36
    )
 
37
from bzrlib.tests.test_progress import _TTYStringIO
 
38
from bzrlib.ui import (
 
39
    CLIUIFactory,
 
40
    SilentUIFactory,
 
41
    )
25
42
from bzrlib.ui.text import TextUIFactory
26
43
 
 
44
 
27
45
class UITests(TestCase):
28
46
 
29
47
    def test_silent_factory(self):
30
 
 
31
48
        ui = SilentUIFactory()
32
 
        pb = ui.progress_bar()
33
 
        # TODO: Test that there is no output from SilentUIFactory
34
 
 
35
 
        self.assertEquals(ui.get_password(), None)
36
 
        self.assertEquals(ui.get_password(u'Hello There \u1234 %(user)s',
37
 
                                          user=u'some\u1234')
38
 
                         , None)
39
 
 
40
 
    def test_text_factory(self):
 
49
        stdout = StringIO()
 
50
        self.assertEqual(None,
 
51
                         self.apply_redirected(None, stdout, stdout,
 
52
                                               ui.get_password))
 
53
        self.assertEqual('', stdout.getvalue())
 
54
        self.assertEqual(None,
 
55
                         self.apply_redirected(None, stdout, stdout,
 
56
                                               ui.get_password,
 
57
                                               u'Hello\u1234 %(user)s',
 
58
                                               user=u'some\u1234'))
 
59
        self.assertEqual('', stdout.getvalue())
 
60
 
 
61
    def test_text_factory_ascii_password(self):
 
62
        ui = TestUIFactory(stdin='secret\n', stdout=StringIOWrapper())
 
63
        pb = ui.nested_progress_bar()
 
64
        try:
 
65
            self.assertEqual('secret',
 
66
                             self.apply_redirected(ui.stdin, ui.stdout,
 
67
                                                   ui.stdout,
 
68
                                                   ui.get_password))
 
69
            # ': ' is appended to prompt
 
70
            self.assertEqual(': ', ui.stdout.getvalue())
 
71
            # stdin should be empty
 
72
            self.assertEqual('', ui.stdin.readline())
 
73
        finally:
 
74
            pb.finished()
 
75
 
 
76
    def test_text_factory_utf8_password(self):
 
77
        """Test an utf8 password.
 
78
 
 
79
        We can't predict what encoding users will have for stdin, so we force
 
80
        it to utf8 to test that we transport the password correctly.
 
81
        """
 
82
        ui = TestUIFactory(stdin=u'baz\u1234'.encode('utf8'),
 
83
                           stdout=StringIOWrapper())
 
84
        ui.stdin.encoding = 'utf8'
 
85
        ui.stdout.encoding = ui.stdin.encoding
 
86
        pb = ui.nested_progress_bar()
 
87
        try:
 
88
            password = self.apply_redirected(ui.stdin, ui.stdout, ui.stdout,
 
89
                                             ui.get_password,
 
90
                                             u'Hello \u1234 %(user)s',
 
91
                                             user=u'some\u1234')
 
92
            # We use StringIO objects, we need to decode them
 
93
            self.assertEqual(u'baz\u1234', password.decode('utf8'))
 
94
            self.assertEqual(u'Hello \u1234 some\u1234: ',
 
95
                             ui.stdout.getvalue().decode('utf8'))
 
96
            # stdin should be empty
 
97
            self.assertEqual('', ui.stdin.readline())
 
98
        finally:
 
99
            pb.finished()
 
100
 
 
101
    def test_progress_note(self):
 
102
        stderr = StringIO()
 
103
        stdout = StringIO()
 
104
        ui_factory = TextUIFactory(bar_type=TTYProgressBar)
 
105
        pb = ui_factory.nested_progress_bar()
 
106
        try:
 
107
            pb.to_messages_file = stdout
 
108
            ui_factory._progress_bar_stack.bottom().to_file = stderr
 
109
            result = pb.note('t')
 
110
            self.assertEqual(None, result)
 
111
            self.assertEqual("t\n", stdout.getvalue())
 
112
            # Since there was no update() call, there should be no clear() call
 
113
            self.failIf(re.search(r'^\r {10,}\r$',
 
114
                                  stderr.getvalue()) is not None,
 
115
                        'We cleared the stderr without anything to put there')
 
116
        finally:
 
117
            pb.finished()
 
118
 
 
119
    def test_progress_note_clears(self):
 
120
        stderr = StringIO()
 
121
        stdout = StringIO()
 
122
        # The PQM redirects the output to a file, so it
 
123
        # defaults to creating a Dots progress bar. we
 
124
        # need to force it to believe we are a TTY
 
125
        ui_factory = TextUIFactory(bar_type=TTYProgressBar)
 
126
        pb = ui_factory.nested_progress_bar()
 
127
        try:
 
128
            pb.to_messages_file = stdout
 
129
            ui_factory._progress_bar_stack.bottom().to_file = stderr
 
130
            # Create a progress update that isn't throttled
 
131
            pb.start_time -= 10
 
132
            pb.update('x', 1, 1)
 
133
            result = pb.note('t')
 
134
            self.assertEqual(None, result)
 
135
            self.assertEqual("t\n", stdout.getvalue())
 
136
            # the exact contents will depend on the terminal width and we don't
 
137
            # care about that right now - but you're probably running it on at
 
138
            # least a 10-character wide terminal :)
 
139
            self.assertContainsRe(stderr.getvalue(), r'\r {10,}\r$')
 
140
        finally:
 
141
            pb.finished()
 
142
 
 
143
    def test_progress_nested(self):
 
144
        # test factory based nested and popping.
41
145
        ui = TextUIFactory()
42
 
        pb = ui.progress_bar()
43
 
        # TODO: Test the output from TextUIFactory, perhaps by overriding sys.stdout
44
 
 
45
 
        # Unfortunately we can't actually test the ui.get_password() because 
46
 
        # that would actually prompt the user for a password during the test suite
47
 
        # This has been tested manually with both LANG=en_US.utf-8 and LANG=C
48
 
        # print
49
 
        # self.assertEquals(ui.get_password(u"%(user)s please type 'bogus'",
50
 
        #                                   user=u'some\u1234')
51
 
        #                  , 'bogus')
 
146
        pb1 = ui.nested_progress_bar()
 
147
        pb2 = ui.nested_progress_bar()
 
148
        self.assertRaises(errors.MissingProgressBarFinish, pb1.finished)
 
149
        pb2.finished()
 
150
        pb1.finished()
 
151
 
 
152
    def test_progress_stack(self):
 
153
        # test the progress bar stack which the default text factory 
 
154
        # uses.
 
155
        stderr = StringIO()
 
156
        stdout = StringIO()
 
157
        # make a stack, which accepts parameters like a pb.
 
158
        stack = ProgressBarStack(to_file=stderr, to_messages_file=stdout)
 
159
        # but is not one
 
160
        self.assertFalse(getattr(stack, 'note', False))
 
161
        pb1 = stack.get_nested()
 
162
        pb2 = stack.get_nested()
 
163
        self.assertRaises(errors.MissingProgressBarFinish, pb1.finished)
 
164
        pb2.finished()
 
165
        pb1.finished()
 
166
        # the text ui factory never actually removes the stack once its setup.
 
167
        # we need to be able to nest again correctly from here.
 
168
        pb1 = stack.get_nested()
 
169
        pb2 = stack.get_nested()
 
170
        self.assertRaises(errors.MissingProgressBarFinish, pb1.finished)
 
171
        pb2.finished()
 
172
        pb1.finished()
 
173
 
 
174
    def test_text_factory_setting_progress_bar(self):
 
175
        # we should be able to choose the progress bar type used.
 
176
        factory = TextUIFactory(bar_type=DotsProgressBar)
 
177
        bar = factory.nested_progress_bar()
 
178
        bar.finished()
 
179
        self.assertIsInstance(bar, DotsProgressBar)
 
180
 
 
181
    def test_cli_stdin_is_default_stdin(self):
 
182
        factory = CLIUIFactory()
 
183
        self.assertEqual(sys.stdin, factory.stdin)
 
184
 
 
185
    def assert_get_bool_acceptance_of_user_input(self, factory):
 
186
        factory.stdin = StringIO("y\nyes with garbage\n"
 
187
                                 "yes\nn\nnot an answer\n"
 
188
                                 "no\nfoo\n")
 
189
        factory.stdout = StringIO()
 
190
        # there is no output from the base factory
 
191
        self.assertEqual(True, factory.get_boolean(""))
 
192
        self.assertEqual(True, factory.get_boolean(""))
 
193
        self.assertEqual(False, factory.get_boolean(""))
 
194
        self.assertEqual(False, factory.get_boolean(""))
 
195
        self.assertEqual("foo\n", factory.stdin.read())
 
196
        # stdin should be empty
 
197
        self.assertEqual('', factory.stdin.readline())
 
198
 
 
199
    def test_silent_ui_getbool(self):
 
200
        factory = SilentUIFactory()
 
201
        self.assert_get_bool_acceptance_of_user_input(factory)
 
202
 
 
203
    def test_silent_factory_prompts_silently(self):
 
204
        factory = SilentUIFactory()
 
205
        stdout = StringIO()
 
206
        factory.stdin = StringIO("y\n")
 
207
        self.assertEqual(True,
 
208
                         self.apply_redirected(None, stdout, stdout,
 
209
                                               factory.get_boolean, "foo"))
 
210
        self.assertEqual("", stdout.getvalue())
 
211
        # stdin should be empty
 
212
        self.assertEqual('', factory.stdin.readline())
 
213
 
 
214
    def test_text_ui_getbool(self):
 
215
        factory = TextUIFactory()
 
216
        self.assert_get_bool_acceptance_of_user_input(factory)
 
217
 
 
218
    def test_text_factory_prompts_and_clears(self):
 
219
        # a get_boolean call should clear the pb before prompting
 
220
        factory = TextUIFactory(bar_type=DotsProgressBar)
 
221
        factory.stdout = _TTYStringIO()
 
222
        factory.stdin = StringIO("yada\ny\n")
 
223
        pb = self.apply_redirected(factory.stdin, factory.stdout,
 
224
                                   factory.stdout, factory.nested_progress_bar)
 
225
        pb.start_time = None
 
226
        self.apply_redirected(factory.stdin, factory.stdout,
 
227
                              factory.stdout, pb.update, "foo", 0, 1)
 
228
        self.assertEqual(True,
 
229
                         self.apply_redirected(None, factory.stdout,
 
230
                                               factory.stdout,
 
231
                                               factory.get_boolean,
 
232
                                               "what do you want"))
 
233
        output = factory.stdout.getvalue()
 
234
        self.assertEqual("foo: .\n"
 
235
                         "what do you want? [y/n]: what do you want? [y/n]: ",
 
236
                         factory.stdout.getvalue())
 
237
        # stdin should be empty
 
238
        self.assertEqual('', factory.stdin.readline())
52
239