~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: 2006-11-10 15:38:16 UTC
  • mto: This revision was merged to the branch mainline in revision 2129.
  • Revision ID: john@arbash-meinel.com-20061110153816-46acf76fc86a512b
use try/finally to clean up a nested progress bar during weave fetching

Show diffs side-by-side

added added

removed removed

Lines of Context:
24
24
 
25
25
import bzrlib
26
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
 
    )
 
27
from bzrlib.progress import TTYProgressBar, ProgressBarStack
 
28
from bzrlib.tests import TestCase
37
29
from bzrlib.tests.test_progress import _TTYStringIO
38
 
from bzrlib.ui import (
39
 
    CLIUIFactory,
40
 
    SilentUIFactory,
41
 
    )
 
30
from bzrlib.ui import SilentUIFactory
42
31
from bzrlib.ui.text import TextUIFactory
43
32
 
44
33
 
46
35
 
47
36
    def test_silent_factory(self):
48
37
        ui = SilentUIFactory()
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()
 
38
        pb = ui.nested_progress_bar()
 
39
        try:
 
40
            # TODO: Test that there is no output from SilentUIFactory
 
41
    
 
42
            self.assertEquals(ui.get_password(), None)
 
43
            self.assertEquals(ui.get_password(u'Hello There \u1234 %(user)s',
 
44
                                              user=u'some\u1234')
 
45
                             , None)
 
46
        finally:
 
47
            pb.finished()
 
48
 
 
49
    def test_text_factory(self):
 
50
        ui = TextUIFactory()
 
51
        pb = ui.nested_progress_bar()
 
52
        pb.finished()
 
53
        # TODO: Test the output from TextUIFactory, perhaps by overriding sys.stdout
 
54
 
 
55
        # Unfortunately we can't actually test the ui.get_password() because 
 
56
        # that would actually prompt the user for a password during the test suite
 
57
        # This has been tested manually with both LANG=en_US.utf-8 and LANG=C
 
58
        # print
 
59
        # self.assertEquals(ui.get_password(u"%(user)s please type 'bogus'",
 
60
        #                                   user=u'some\u1234')
 
61
        #                  , 'bogus')
 
62
 
100
63
 
101
64
    def test_progress_note(self):
102
65
        stderr = StringIO()
110
73
            self.assertEqual(None, result)
111
74
            self.assertEqual("t\n", stdout.getvalue())
112
75
            # 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,
 
76
            self.failIf(re.search(r'^\r {10,}\r$', stderr.getvalue()) is not None,
115
77
                        'We cleared the stderr without anything to put there')
116
78
        finally:
117
79
            pb.finished()
173
135
 
174
136
    def test_text_factory_setting_progress_bar(self):
175
137
        # we should be able to choose the progress bar type used.
176
 
        factory = TextUIFactory(bar_type=DotsProgressBar)
 
138
        factory = bzrlib.ui.text.TextUIFactory(
 
139
            bar_type=bzrlib.progress.DotsProgressBar)
177
140
        bar = factory.nested_progress_bar()
178
141
        bar.finished()
179
 
        self.assertIsInstance(bar, DotsProgressBar)
 
142
        self.assertIsInstance(bar, bzrlib.progress.DotsProgressBar)
180
143
 
181
144
    def test_cli_stdin_is_default_stdin(self):
182
 
        factory = CLIUIFactory()
 
145
        factory = bzrlib.ui.CLIUIFactory()
183
146
        self.assertEqual(sys.stdin, factory.stdin)
184
147
 
185
148
    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")
 
149
        factory.stdin = StringIO("y\nyes with garbage\nyes\nn\nnot an answer\nno\nfoo\n")
189
150
        factory.stdout = StringIO()
190
151
        # there is no output from the base factory
191
152
        self.assertEqual(True, factory.get_boolean(""))
193
154
        self.assertEqual(False, factory.get_boolean(""))
194
155
        self.assertEqual(False, factory.get_boolean(""))
195
156
        self.assertEqual("foo\n", factory.stdin.read())
196
 
        # stdin should be empty
197
 
        self.assertEqual('', factory.stdin.readline())
198
157
 
199
158
    def test_silent_ui_getbool(self):
200
 
        factory = SilentUIFactory()
 
159
        factory = bzrlib.ui.SilentUIFactory()
201
160
        self.assert_get_bool_acceptance_of_user_input(factory)
202
161
 
203
162
    def test_silent_factory_prompts_silently(self):
204
 
        factory = SilentUIFactory()
 
163
        factory = bzrlib.ui.SilentUIFactory()
205
164
        stdout = StringIO()
206
165
        factory.stdin = StringIO("y\n")
207
 
        self.assertEqual(True,
208
 
                         self.apply_redirected(None, stdout, stdout,
209
 
                                               factory.get_boolean, "foo"))
 
166
        self.assertEqual(
 
167
            True,
 
168
            self.apply_redirected(
 
169
                None, stdout, stdout, factory.get_boolean, "foo")
 
170
            )
210
171
        self.assertEqual("", stdout.getvalue())
211
 
        # stdin should be empty
212
 
        self.assertEqual('', factory.stdin.readline())
213
 
 
 
172
        
214
173
    def test_text_ui_getbool(self):
215
 
        factory = TextUIFactory()
 
174
        factory = bzrlib.ui.text.TextUIFactory()
216
175
        self.assert_get_bool_acceptance_of_user_input(factory)
217
176
 
218
177
    def test_text_factory_prompts_and_clears(self):
219
178
        # a get_boolean call should clear the pb before prompting
220
 
        factory = TextUIFactory(bar_type=DotsProgressBar)
 
179
        factory = bzrlib.ui.text.TextUIFactory()
221
180
        factory.stdout = _TTYStringIO()
222
181
        factory.stdin = StringIO("yada\ny\n")
223
 
        pb = self.apply_redirected(factory.stdin, factory.stdout,
224
 
                                   factory.stdout, factory.nested_progress_bar)
 
182
        pb = self.apply_redirected(
 
183
            factory.stdin, factory.stdout, factory.stdout, factory.nested_progress_bar)
225
184
        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"))
 
185
        self.apply_redirected(
 
186
            factory.stdin, factory.stdout, factory.stdout, pb.update, "foo", 0, 1)
 
187
        self.assertEqual(
 
188
            True,
 
189
            self.apply_redirected(
 
190
                None, factory.stdout, factory.stdout, factory.get_boolean, "what do you want")
 
191
            )
 
192
        # FIXME: This assumes the factory's going to produce a spinner-style
 
193
        # progress bar, but it won't if this is run from a dumb terminal (e.g.
 
194
        # from inside gvim.) -- mbp 20061014
 
195
        #
 
196
        # use a regular expression so that we don't depend on the particular
 
197
        # screen width - could also set and restore $COLUMN if that has
 
198
        # priority on all platforms, but it doesn't at present.
233
199
        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())
239
 
 
 
200
        if not re.match(
 
201
            "\r/ \\[    *\\] foo 0/1"
 
202
            "\r   *" 
 
203
            "\rwhat do you want\\? \\[y/n\\]:what do you want\\? \\[y/n\\]:", 
 
204
            output):
 
205
            self.fail("didn't match factory output %r, %r" % (factory, output))