~bzr-pqm/bzr/bzr.dev

3948.2.6 by Martin Pool
ProgressBarStack is deprecated
1
# Copyright (C) 2005, 2008, 2009 Canonical Ltd
1185.49.22 by John Arbash Meinel
Added get_password to the UIFactory, using it inside of sftp.py
2
#
3
# This program is free software; you can redistribute it and/or modify
4
# it under the terms of the GNU General Public License as published by
5
# the Free Software Foundation; either version 2 of the License, or
6
# (at your option) any later version.
7
#
8
# This program is distributed in the hope that it will be useful,
9
# but WITHOUT ANY WARRANTY; without even the implied warranty of
10
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
11
# GNU General Public License for more details.
12
#
13
# You should have received a copy of the GNU General Public License
14
# along with this program; if not, write to the Free Software
15
# Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307  USA
16
17
"""Tests for the bzrlib ui
18
"""
19
20
import os
1534.5.6 by Robert Collins
split out converter logic into per-format objects.
21
from StringIO import StringIO
1704.2.9 by Martin Pool
Make text_factory test not depend on 80-col terminal
22
import re
1185.49.22 by John Arbash Meinel
Added get_password to the UIFactory, using it inside of sftp.py
23
import sys
4017.1.1 by John Arbash Meinel
Get a pb.tick() to work after calling pb.update()
24
import time
1185.49.22 by John Arbash Meinel
Added get_password to the UIFactory, using it inside of sftp.py
25
1681.1.2 by Robert Collins
* bzrlib.ui.text.TextUIFactory now accepts a bar_type parameter which
26
import bzrlib
1594.1.1 by Robert Collins
Introduce new bzr progress bar api. ui_factory.nested_progress_bar.
27
import bzrlib.errors as errors
2363.4.10 by Vincent Ladeuil
Complete tests.
28
from bzrlib.progress import (
29
    DotsProgressBar,
30
    ProgressBarStack,
4110.2.15 by Martin Pool
Fix bug in showing task progress and add a test
31
    ProgressTask,
2363.4.10 by Vincent Ladeuil
Complete tests.
32
    TTYProgressBar,
33
    )
3948.2.6 by Martin Pool
ProgressBarStack is deprecated
34
from bzrlib.symbol_versioning import (
35
    deprecated_in,
36
    )
2294.4.1 by Vincent Ladeuil
Add a UIFactory.get_login method, fix tests.
37
from bzrlib.tests import (
2363.4.10 by Vincent Ladeuil
Complete tests.
38
    TestCase,
2294.4.4 by Vincent Ladeuil
Provide a better implementation for testing passwords.
39
    TestUIFactory,
2294.4.1 by Vincent Ladeuil
Add a UIFactory.get_login method, fix tests.
40
    StringIOWrapper,
41
    )
1843.3.10 by John Arbash Meinel
ui tests were failing when output was redirected to a file. (thus blocked by the pqm)
42
from bzrlib.tests.test_progress import _TTYStringIO
2363.4.10 by Vincent Ladeuil
Complete tests.
43
from bzrlib.ui import (
44
    CLIUIFactory,
45
    SilentUIFactory,
46
    )
4110.2.15 by Martin Pool
Fix bug in showing task progress and add a test
47
from bzrlib.ui.text import (
48
    TextProgressView,
49
    TextUIFactory,
50
    )
1185.49.22 by John Arbash Meinel
Added get_password to the UIFactory, using it inside of sftp.py
51
1681.1.2 by Robert Collins
* bzrlib.ui.text.TextUIFactory now accepts a bar_type parameter which
52
1185.49.22 by John Arbash Meinel
Added get_password to the UIFactory, using it inside of sftp.py
53
class UITests(TestCase):
54
55
    def test_silent_factory(self):
56
        ui = SilentUIFactory()
2294.4.1 by Vincent Ladeuil
Add a UIFactory.get_login method, fix tests.
57
        stdout = StringIO()
58
        self.assertEqual(None,
59
                         self.apply_redirected(None, stdout, stdout,
60
                                               ui.get_password))
61
        self.assertEqual('', stdout.getvalue())
62
        self.assertEqual(None,
63
                         self.apply_redirected(None, stdout, stdout,
64
                                               ui.get_password,
65
                                               u'Hello\u1234 %(user)s',
66
                                               user=u'some\u1234'))
67
        self.assertEqual('', stdout.getvalue())
68
69
    def test_text_factory_ascii_password(self):
2294.4.4 by Vincent Ladeuil
Provide a better implementation for testing passwords.
70
        ui = TestUIFactory(stdin='secret\n', stdout=StringIOWrapper())
2294.4.1 by Vincent Ladeuil
Add a UIFactory.get_login method, fix tests.
71
        pb = ui.nested_progress_bar()
72
        try:
73
            self.assertEqual('secret',
74
                             self.apply_redirected(ui.stdin, ui.stdout,
75
                                                   ui.stdout,
76
                                                   ui.get_password))
77
            # ': ' is appended to prompt
2294.4.4 by Vincent Ladeuil
Provide a better implementation for testing passwords.
78
            self.assertEqual(': ', ui.stdout.getvalue())
2363.4.3 by Vincent Ladeuil
Tidy-up tests.
79
            # stdin should be empty
2363.4.6 by Vincent Ladeuil
Fix tests around stdin emptyness.
80
            self.assertEqual('', ui.stdin.readline())
2294.4.1 by Vincent Ladeuil
Add a UIFactory.get_login method, fix tests.
81
        finally:
82
            pb.finished()
83
84
    def test_text_factory_utf8_password(self):
85
        """Test an utf8 password.
86
87
        We can't predict what encoding users will have for stdin, so we force
88
        it to utf8 to test that we transport the password correctly.
89
        """
2294.4.4 by Vincent Ladeuil
Provide a better implementation for testing passwords.
90
        ui = TestUIFactory(stdin=u'baz\u1234'.encode('utf8'),
91
                           stdout=StringIOWrapper())
92
        ui.stdin.encoding = 'utf8'
93
        ui.stdout.encoding = ui.stdin.encoding
2294.4.1 by Vincent Ladeuil
Add a UIFactory.get_login method, fix tests.
94
        pb = ui.nested_progress_bar()
95
        try:
96
            password = self.apply_redirected(ui.stdin, ui.stdout, ui.stdout,
97
                                             ui.get_password,
98
                                             u'Hello \u1234 %(user)s',
99
                                             user=u'some\u1234')
100
            # We use StringIO objects, we need to decode them
101
            self.assertEqual(u'baz\u1234', password.decode('utf8'))
102
            self.assertEqual(u'Hello \u1234 some\u1234: ',
103
                             ui.stdout.getvalue().decode('utf8'))
2363.4.3 by Vincent Ladeuil
Tidy-up tests.
104
            # stdin should be empty
2363.4.6 by Vincent Ladeuil
Fix tests around stdin emptyness.
105
            self.assertEqual('', ui.stdin.readline())
2294.4.1 by Vincent Ladeuil
Add a UIFactory.get_login method, fix tests.
106
        finally:
107
            pb.finished()
1534.5.6 by Robert Collins
split out converter logic into per-format objects.
108
109
    def test_progress_note(self):
110
        stderr = StringIO()
111
        stdout = StringIO()
3882.8.11 by Martin Pool
Choose the UIFactory class depending on the terminal capabilities
112
        ui_factory = TextUIFactory(stdin=StringIO(''),
113
            stderr=stderr,
114
            stdout=stdout)
1558.8.5 by Aaron Bentley
Pass note up the stack instead of using bzrlib.ui_factory
115
        pb = ui_factory.nested_progress_bar()
1558.8.4 by Aaron Bentley
Fixed test case for pb.note
116
        try:
117
            result = pb.note('t')
118
            self.assertEqual(None, result)
119
            self.assertEqual("t\n", stdout.getvalue())
1843.3.2 by John Arbash Meinel
Fix a ui test that depended on clearing
120
            # Since there was no update() call, there should be no clear() call
2363.4.4 by Vincent Ladeuil
More tidying-up.
121
            self.failIf(re.search(r'^\r {10,}\r$',
122
                                  stderr.getvalue()) is not None,
1843.3.2 by John Arbash Meinel
Fix a ui test that depended on clearing
123
                        'We cleared the stderr without anything to put there')
124
        finally:
125
            pb.finished()
126
127
    def test_progress_note_clears(self):
128
        stderr = StringIO()
129
        stdout = StringIO()
1843.3.10 by John Arbash Meinel
ui tests were failing when output was redirected to a file. (thus blocked by the pqm)
130
        # The PQM redirects the output to a file, so it
131
        # defaults to creating a Dots progress bar. we
132
        # need to force it to believe we are a TTY
3882.8.8 by Martin Pool
Progress and UI test cleanups
133
        ui_factory = TextUIFactory(
3882.8.11 by Martin Pool
Choose the UIFactory class depending on the terminal capabilities
134
            stdin=StringIO(''),
3882.8.4 by Martin Pool
All UI factories should support note()
135
            stdout=stdout, stderr=stderr)
1843.3.2 by John Arbash Meinel
Fix a ui test that depended on clearing
136
        pb = ui_factory.nested_progress_bar()
137
        try:
138
            # Create a progress update that isn't throttled
139
            pb.update('x', 1, 1)
140
            result = pb.note('t')
141
            self.assertEqual(None, result)
142
            self.assertEqual("t\n", stdout.getvalue())
1558.8.4 by Aaron Bentley
Fixed test case for pb.note
143
            # the exact contents will depend on the terminal width and we don't
144
            # care about that right now - but you're probably running it on at
145
            # least a 10-character wide terminal :)
1843.3.2 by John Arbash Meinel
Fix a ui test that depended on clearing
146
            self.assertContainsRe(stderr.getvalue(), r'\r {10,}\r$')
1558.8.4 by Aaron Bentley
Fixed test case for pb.note
147
        finally:
1558.8.5 by Aaron Bentley
Pass note up the stack instead of using bzrlib.ui_factory
148
            pb.finished()
1594.1.1 by Robert Collins
Introduce new bzr progress bar api. ui_factory.nested_progress_bar.
149
150
    def test_progress_nested(self):
151
        # test factory based nested and popping.
3882.8.11 by Martin Pool
Choose the UIFactory class depending on the terminal capabilities
152
        ui = TextUIFactory(None, None, None)
1594.1.1 by Robert Collins
Introduce new bzr progress bar api. ui_factory.nested_progress_bar.
153
        pb1 = ui.nested_progress_bar()
154
        pb2 = ui.nested_progress_bar()
3948.2.2 by Martin Pool
Corrections to finishing progress bars
155
        # You do get a warning if the outermost progress bar wasn't finished
156
        # first - it's not clear if this is really useful or if it should just
157
        # become orphaned -- mbp 20090120
3882.8.12 by Martin Pool
Give a warning, not an error, if a progress bar is not finished in order
158
        warnings, _ = self.callCatchWarnings(pb1.finished)
3948.2.2 by Martin Pool
Corrections to finishing progress bars
159
        if len(warnings) != 1:
160
            self.fail("unexpected warnings: %r" % (warnings,))
1594.1.1 by Robert Collins
Introduce new bzr progress bar api. ui_factory.nested_progress_bar.
161
        pb2.finished()
162
        pb1.finished()
163
164
    def test_progress_stack(self):
3943.8.1 by Marius Kruger
remove all trailing whitespace from bzr source
165
        # test the progress bar stack which the default text factory
1594.1.1 by Robert Collins
Introduce new bzr progress bar api. ui_factory.nested_progress_bar.
166
        # uses.
167
        stderr = StringIO()
168
        stdout = StringIO()
169
        # make a stack, which accepts parameters like a pb.
3948.2.6 by Martin Pool
ProgressBarStack is deprecated
170
        stack = self.applyDeprecated(
171
            deprecated_in((1, 12, 0)),
172
            ProgressBarStack,
173
            to_file=stderr, to_messages_file=stdout)
1594.1.1 by Robert Collins
Introduce new bzr progress bar api. ui_factory.nested_progress_bar.
174
        # but is not one
175
        self.assertFalse(getattr(stack, 'note', False))
176
        pb1 = stack.get_nested()
177
        pb2 = stack.get_nested()
3882.8.12 by Martin Pool
Give a warning, not an error, if a progress bar is not finished in order
178
        warnings, _ = self.callCatchWarnings(pb1.finished)
179
        self.assertEqual(len(warnings), 1)
1594.1.1 by Robert Collins
Introduce new bzr progress bar api. ui_factory.nested_progress_bar.
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()
3882.8.12 by Martin Pool
Give a warning, not an error, if a progress bar is not finished in order
186
        warnings, _ = self.callCatchWarnings(pb1.finished)
187
        self.assertEqual(len(warnings), 1)
1594.1.1 by Robert Collins
Introduce new bzr progress bar api. ui_factory.nested_progress_bar.
188
        pb2.finished()
189
        pb1.finished()
1681.1.2 by Robert Collins
* bzrlib.ui.text.TextUIFactory now accepts a bar_type parameter which
190
1687.1.4 by Robert Collins
Add bzrlib.ui.ui_factory.get_boolean().
191
    def assert_get_bool_acceptance_of_user_input(self, factory):
2363.4.4 by Vincent Ladeuil
More tidying-up.
192
        factory.stdin = StringIO("y\nyes with garbage\n"
193
                                 "yes\nn\nnot an answer\n"
194
                                 "no\nfoo\n")
1687.1.4 by Robert Collins
Add bzrlib.ui.ui_factory.get_boolean().
195
        factory.stdout = StringIO()
196
        # there is no output from the base factory
197
        self.assertEqual(True, factory.get_boolean(""))
198
        self.assertEqual(True, factory.get_boolean(""))
199
        self.assertEqual(False, factory.get_boolean(""))
200
        self.assertEqual(False, factory.get_boolean(""))
201
        self.assertEqual("foo\n", factory.stdin.read())
2363.4.4 by Vincent Ladeuil
More tidying-up.
202
        # stdin should be empty
2363.4.6 by Vincent Ladeuil
Fix tests around stdin emptyness.
203
        self.assertEqual('', factory.stdin.readline())
1687.1.4 by Robert Collins
Add bzrlib.ui.ui_factory.get_boolean().
204
205
    def test_silent_ui_getbool(self):
2363.4.10 by Vincent Ladeuil
Complete tests.
206
        factory = SilentUIFactory()
1687.1.4 by Robert Collins
Add bzrlib.ui.ui_factory.get_boolean().
207
        self.assert_get_bool_acceptance_of_user_input(factory)
208
209
    def test_silent_factory_prompts_silently(self):
2363.4.10 by Vincent Ladeuil
Complete tests.
210
        factory = SilentUIFactory()
1687.1.4 by Robert Collins
Add bzrlib.ui.ui_factory.get_boolean().
211
        stdout = StringIO()
212
        factory.stdin = StringIO("y\n")
2363.4.4 by Vincent Ladeuil
More tidying-up.
213
        self.assertEqual(True,
214
                         self.apply_redirected(None, stdout, stdout,
215
                                               factory.get_boolean, "foo"))
1687.1.4 by Robert Collins
Add bzrlib.ui.ui_factory.get_boolean().
216
        self.assertEqual("", stdout.getvalue())
2363.4.4 by Vincent Ladeuil
More tidying-up.
217
        # stdin should be empty
2363.4.6 by Vincent Ladeuil
Fix tests around stdin emptyness.
218
        self.assertEqual('', factory.stdin.readline())
2363.4.4 by Vincent Ladeuil
More tidying-up.
219
1687.1.4 by Robert Collins
Add bzrlib.ui.ui_factory.get_boolean().
220
    def test_text_ui_getbool(self):
3882.8.11 by Martin Pool
Choose the UIFactory class depending on the terminal capabilities
221
        factory = TextUIFactory(None, None, None)
1687.1.4 by Robert Collins
Add bzrlib.ui.ui_factory.get_boolean().
222
        self.assert_get_bool_acceptance_of_user_input(factory)
223
224
    def test_text_factory_prompts_and_clears(self):
225
        # a get_boolean call should clear the pb before prompting
3882.8.10 by Martin Pool
Fix up test_ui for new progress bars
226
        out = _TTYStringIO()
3882.8.11 by Martin Pool
Choose the UIFactory class depending on the terminal capabilities
227
        factory = TextUIFactory(stdin=StringIO("yada\ny\n"), stdout=out, stderr=out)
3882.8.10 by Martin Pool
Fix up test_ui for new progress bars
228
        pb = factory.nested_progress_bar()
229
        pb.show_bar = False
230
        pb.show_spinner = False
231
        pb.show_count = False
232
        pb.update("foo", 0, 1)
2363.4.4 by Vincent Ladeuil
More tidying-up.
233
        self.assertEqual(True,
234
                         self.apply_redirected(None, factory.stdout,
235
                                               factory.stdout,
236
                                               factory.get_boolean,
237
                                               "what do you want"))
3882.8.10 by Martin Pool
Fix up test_ui for new progress bars
238
        output = out.getvalue()
239
        self.assertContainsRe(factory.stdout.getvalue(),
240
            "foo *\r\r  *\r*")
241
        self.assertContainsRe(factory.stdout.getvalue(),
242
            r"what do you want\? \[y/n\]: what do you want\? \[y/n\]: ")
243
        # stdin should have been totally consumed
2363.4.6 by Vincent Ladeuil
Fix tests around stdin emptyness.
244
        self.assertEqual('', factory.stdin.readline())
4017.1.1 by John Arbash Meinel
Get a pb.tick() to work after calling pb.update()
245
246
    def test_text_tick_after_update(self):
247
        ui_factory = TextUIFactory(stdout=StringIO(), stderr=StringIO())
248
        pb = ui_factory.nested_progress_bar()
249
        try:
250
            pb.update('task', 0, 3)
251
            # Reset the clock, so that it actually tries to repaint itself
252
            ui_factory._progress_view._last_repaint = time.time() - 1.0
253
            pb.tick()
254
        finally:
255
            pb.finished()
4110.2.15 by Martin Pool
Fix bug in showing task progress and add a test
256
257
258
class TestTextProgressView(TestCase):
259
    """Tests for text display of progress bars.
260
    """
261
    # XXX: These might be a bit easier to write if the rendering and
262
    # state-maintaining parts of TextProgressView were more separate, and if
263
    # the progress task called back directly to its own view not to the ui
264
    # factory. -- mbp 20090312
4110.2.16 by Martin Pool
Refactor TextProgressView a bit and add another test
265
    
266
    def _make_factory(self):
4110.2.15 by Martin Pool
Fix bug in showing task progress and add a test
267
        out = StringIO()
268
        uif = TextUIFactory(stderr=out)
269
        uif._progress_view._width = 80
4110.2.16 by Martin Pool
Refactor TextProgressView a bit and add another test
270
        return out, uif
271
272
    def test_render_progress_easy(self):
273
        """Just one task and one quarter done"""
274
        out, uif = self._make_factory()
4110.2.15 by Martin Pool
Fix bug in showing task progress and add a test
275
        task = uif.nested_progress_bar()
276
        task.update('reticulating splines', 5, 20)
277
        self.assertEqual(
278
'\r[####/               ] reticulating splines 5/20                               \r'
279
            , out.getvalue())
4110.2.16 by Martin Pool
Refactor TextProgressView a bit and add another test
280
4110.2.17 by Martin Pool
If one ProgressTask has no count, it passes through that of its child
281
    def test_render_progress_nested(self):
282
        """Tasks proportionally contribute to overall progress"""
4110.2.16 by Martin Pool
Refactor TextProgressView a bit and add another test
283
        out, uif = self._make_factory()
284
        task = uif.nested_progress_bar()
285
        task.update('reticulating splines', 0, 2)
286
        task2 = uif.nested_progress_bar()
287
        task2.update('stage2', 1, 2)
288
        # so we're in the first half of the main task, and half way through
289
        # that
290
        self.assertEqual(
4110.2.18 by Martin Pool
Progress bars always repaint when task structure is changed
291
r'[####\               ] reticulating splines:stage2 1/2'
4110.2.16 by Martin Pool
Refactor TextProgressView a bit and add another test
292
            , uif._progress_view._render_line())
4110.2.17 by Martin Pool
If one ProgressTask has no count, it passes through that of its child
293
        # if the nested task is complete, then we're all the way through the
294
        # first half of the overall work
295
        task2.update('stage2', 2, 2)
296
        self.assertEqual(
4110.2.18 by Martin Pool
Progress bars always repaint when task structure is changed
297
r'[#########|          ] reticulating splines:stage2 2/2'
4110.2.17 by Martin Pool
If one ProgressTask has no count, it passes through that of its child
298
            , uif._progress_view._render_line())
299
300
    def test_render_progress_sub_nested(self):
301
        """Intermediate tasks don't mess up calculation."""
302
        out, uif = self._make_factory()
303
        task_a = uif.nested_progress_bar()
304
        task_a.update('a', 0, 2)
305
        task_b = uif.nested_progress_bar()
306
        task_b.update('b')
307
        task_c = uif.nested_progress_bar()
308
        task_c.update('c', 1, 2)
309
        # the top-level task is in its first half; the middle one has no
310
        # progress indication, just a label; and the bottom one is half done,
311
        # so the overall fraction is 1/4
312
        self.assertEqual(
4110.2.18 by Martin Pool
Progress bars always repaint when task structure is changed
313
            r'[####|               ] a:b:c 1/2'
4110.2.17 by Martin Pool
If one ProgressTask has no count, it passes through that of its child
314
            , uif._progress_view._render_line())
315