/brz/remove-bazaar

To get this branch, use:
bzr branch http://gegoxaren.bato24.eu/bzr/brz/remove-bazaar
3882.8.5 by Martin Pool
Progress tasks can indicate what kind of display is useful
1
# Copyright (C) 2005, 2008 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
24
1681.1.2 by Robert Collins
* bzrlib.ui.text.TextUIFactory now accepts a bar_type parameter which
25
import bzrlib
1594.1.1 by Robert Collins
Introduce new bzr progress bar api. ui_factory.nested_progress_bar.
26
import bzrlib.errors as errors
2363.4.10 by Vincent Ladeuil
Complete tests.
27
from bzrlib.progress import (
28
    DotsProgressBar,
29
    ProgressBarStack,
30
    TTYProgressBar,
31
    )
2294.4.1 by Vincent Ladeuil
Add a UIFactory.get_login method, fix tests.
32
from bzrlib.tests import (
2363.4.10 by Vincent Ladeuil
Complete tests.
33
    TestCase,
2294.4.4 by Vincent Ladeuil
Provide a better implementation for testing passwords.
34
    TestUIFactory,
2294.4.1 by Vincent Ladeuil
Add a UIFactory.get_login method, fix tests.
35
    StringIOWrapper,
36
    )
1843.3.10 by John Arbash Meinel
ui tests were failing when output was redirected to a file. (thus blocked by the pqm)
37
from bzrlib.tests.test_progress import _TTYStringIO
2363.4.10 by Vincent Ladeuil
Complete tests.
38
from bzrlib.ui import (
39
    CLIUIFactory,
40
    SilentUIFactory,
41
    )
1185.49.22 by John Arbash Meinel
Added get_password to the UIFactory, using it inside of sftp.py
42
from bzrlib.ui.text import TextUIFactory
43
1681.1.2 by Robert Collins
* bzrlib.ui.text.TextUIFactory now accepts a bar_type parameter which
44
1185.49.22 by John Arbash Meinel
Added get_password to the UIFactory, using it inside of sftp.py
45
class UITests(TestCase):
46
47
    def test_silent_factory(self):
48
        ui = SilentUIFactory()
2294.4.1 by Vincent Ladeuil
Add a UIFactory.get_login method, fix tests.
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):
2294.4.4 by Vincent Ladeuil
Provide a better implementation for testing passwords.
62
        ui = TestUIFactory(stdin='secret\n', stdout=StringIOWrapper())
2294.4.1 by Vincent Ladeuil
Add a UIFactory.get_login method, fix tests.
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
2294.4.4 by Vincent Ladeuil
Provide a better implementation for testing passwords.
70
            self.assertEqual(': ', ui.stdout.getvalue())
2363.4.3 by Vincent Ladeuil
Tidy-up tests.
71
            # stdin should be empty
2363.4.6 by Vincent Ladeuil
Fix tests around stdin emptyness.
72
            self.assertEqual('', ui.stdin.readline())
2294.4.1 by Vincent Ladeuil
Add a UIFactory.get_login method, fix tests.
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
        """
2294.4.4 by Vincent Ladeuil
Provide a better implementation for testing passwords.
82
        ui = TestUIFactory(stdin=u'baz\u1234'.encode('utf8'),
83
                           stdout=StringIOWrapper())
84
        ui.stdin.encoding = 'utf8'
85
        ui.stdout.encoding = ui.stdin.encoding
2294.4.1 by Vincent Ladeuil
Add a UIFactory.get_login method, fix tests.
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'))
2363.4.3 by Vincent Ladeuil
Tidy-up tests.
96
            # stdin should be empty
2363.4.6 by Vincent Ladeuil
Fix tests around stdin emptyness.
97
            self.assertEqual('', ui.stdin.readline())
2294.4.1 by Vincent Ladeuil
Add a UIFactory.get_login method, fix tests.
98
        finally:
99
            pb.finished()
1534.5.6 by Robert Collins
split out converter logic into per-format objects.
100
101
    def test_progress_note(self):
102
        stderr = StringIO()
103
        stdout = StringIO()
3882.8.11 by Martin Pool
Choose the UIFactory class depending on the terminal capabilities
104
        ui_factory = TextUIFactory(stdin=StringIO(''),
105
            stderr=stderr,
106
            stdout=stdout)
1558.8.5 by Aaron Bentley
Pass note up the stack instead of using bzrlib.ui_factory
107
        pb = ui_factory.nested_progress_bar()
1558.8.4 by Aaron Bentley
Fixed test case for pb.note
108
        try:
109
            result = pb.note('t')
110
            self.assertEqual(None, result)
111
            self.assertEqual("t\n", stdout.getvalue())
1843.3.2 by John Arbash Meinel
Fix a ui test that depended on clearing
112
            # Since there was no update() call, there should be no clear() call
2363.4.4 by Vincent Ladeuil
More tidying-up.
113
            self.failIf(re.search(r'^\r {10,}\r$',
114
                                  stderr.getvalue()) is not None,
1843.3.2 by John Arbash Meinel
Fix a ui test that depended on clearing
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()
1843.3.10 by John Arbash Meinel
ui tests were failing when output was redirected to a file. (thus blocked by the pqm)
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
3882.8.8 by Martin Pool
Progress and UI test cleanups
125
        ui_factory = TextUIFactory(
3882.8.11 by Martin Pool
Choose the UIFactory class depending on the terminal capabilities
126
            stdin=StringIO(''),
3882.8.4 by Martin Pool
All UI factories should support note()
127
            stdout=stdout, stderr=stderr)
1843.3.2 by John Arbash Meinel
Fix a ui test that depended on clearing
128
        pb = ui_factory.nested_progress_bar()
129
        try:
130
            # Create a progress update that isn't throttled
131
            pb.update('x', 1, 1)
132
            result = pb.note('t')
133
            self.assertEqual(None, result)
134
            self.assertEqual("t\n", stdout.getvalue())
1558.8.4 by Aaron Bentley
Fixed test case for pb.note
135
            # the exact contents will depend on the terminal width and we don't
136
            # care about that right now - but you're probably running it on at
137
            # least a 10-character wide terminal :)
1843.3.2 by John Arbash Meinel
Fix a ui test that depended on clearing
138
            self.assertContainsRe(stderr.getvalue(), r'\r {10,}\r$')
1558.8.4 by Aaron Bentley
Fixed test case for pb.note
139
        finally:
1558.8.5 by Aaron Bentley
Pass note up the stack instead of using bzrlib.ui_factory
140
            pb.finished()
1594.1.1 by Robert Collins
Introduce new bzr progress bar api. ui_factory.nested_progress_bar.
141
142
    def test_progress_nested(self):
143
        # test factory based nested and popping.
3882.8.11 by Martin Pool
Choose the UIFactory class depending on the terminal capabilities
144
        ui = TextUIFactory(None, None, None)
1594.1.1 by Robert Collins
Introduce new bzr progress bar api. ui_factory.nested_progress_bar.
145
        pb1 = ui.nested_progress_bar()
146
        pb2 = ui.nested_progress_bar()
3948.2.2 by Martin Pool
Corrections to finishing progress bars
147
        # You do get a warning if the outermost progress bar wasn't finished
148
        # first - it's not clear if this is really useful or if it should just
149
        # 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
150
        warnings, _ = self.callCatchWarnings(pb1.finished)
3948.2.2 by Martin Pool
Corrections to finishing progress bars
151
        if len(warnings) != 1:
152
            self.fail("unexpected warnings: %r" % (warnings,))
1594.1.1 by Robert Collins
Introduce new bzr progress bar api. ui_factory.nested_progress_bar.
153
        pb2.finished()
154
        pb1.finished()
155
156
    def test_progress_stack(self):
157
        # test the progress bar stack which the default text factory 
158
        # uses.
159
        stderr = StringIO()
160
        stdout = StringIO()
161
        # make a stack, which accepts parameters like a pb.
162
        stack = ProgressBarStack(to_file=stderr, to_messages_file=stdout)
163
        # but is not one
164
        self.assertFalse(getattr(stack, 'note', False))
165
        pb1 = stack.get_nested()
166
        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
167
        warnings, _ = self.callCatchWarnings(pb1.finished)
168
        self.assertEqual(len(warnings), 1)
1594.1.1 by Robert Collins
Introduce new bzr progress bar api. ui_factory.nested_progress_bar.
169
        pb2.finished()
170
        pb1.finished()
171
        # the text ui factory never actually removes the stack once its setup.
172
        # we need to be able to nest again correctly from here.
173
        pb1 = stack.get_nested()
174
        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
175
        warnings, _ = self.callCatchWarnings(pb1.finished)
176
        self.assertEqual(len(warnings), 1)
1594.1.1 by Robert Collins
Introduce new bzr progress bar api. ui_factory.nested_progress_bar.
177
        pb2.finished()
178
        pb1.finished()
1681.1.2 by Robert Collins
* bzrlib.ui.text.TextUIFactory now accepts a bar_type parameter which
179
1687.1.4 by Robert Collins
Add bzrlib.ui.ui_factory.get_boolean().
180
    def assert_get_bool_acceptance_of_user_input(self, factory):
2363.4.4 by Vincent Ladeuil
More tidying-up.
181
        factory.stdin = StringIO("y\nyes with garbage\n"
182
                                 "yes\nn\nnot an answer\n"
183
                                 "no\nfoo\n")
1687.1.4 by Robert Collins
Add bzrlib.ui.ui_factory.get_boolean().
184
        factory.stdout = StringIO()
185
        # there is no output from the base factory
186
        self.assertEqual(True, factory.get_boolean(""))
187
        self.assertEqual(True, factory.get_boolean(""))
188
        self.assertEqual(False, factory.get_boolean(""))
189
        self.assertEqual(False, factory.get_boolean(""))
190
        self.assertEqual("foo\n", factory.stdin.read())
2363.4.4 by Vincent Ladeuil
More tidying-up.
191
        # stdin should be empty
2363.4.6 by Vincent Ladeuil
Fix tests around stdin emptyness.
192
        self.assertEqual('', factory.stdin.readline())
1687.1.4 by Robert Collins
Add bzrlib.ui.ui_factory.get_boolean().
193
194
    def test_silent_ui_getbool(self):
2363.4.10 by Vincent Ladeuil
Complete tests.
195
        factory = SilentUIFactory()
1687.1.4 by Robert Collins
Add bzrlib.ui.ui_factory.get_boolean().
196
        self.assert_get_bool_acceptance_of_user_input(factory)
197
198
    def test_silent_factory_prompts_silently(self):
2363.4.10 by Vincent Ladeuil
Complete tests.
199
        factory = SilentUIFactory()
1687.1.4 by Robert Collins
Add bzrlib.ui.ui_factory.get_boolean().
200
        stdout = StringIO()
201
        factory.stdin = StringIO("y\n")
2363.4.4 by Vincent Ladeuil
More tidying-up.
202
        self.assertEqual(True,
203
                         self.apply_redirected(None, stdout, stdout,
204
                                               factory.get_boolean, "foo"))
1687.1.4 by Robert Collins
Add bzrlib.ui.ui_factory.get_boolean().
205
        self.assertEqual("", stdout.getvalue())
2363.4.4 by Vincent Ladeuil
More tidying-up.
206
        # stdin should be empty
2363.4.6 by Vincent Ladeuil
Fix tests around stdin emptyness.
207
        self.assertEqual('', factory.stdin.readline())
2363.4.4 by Vincent Ladeuil
More tidying-up.
208
1687.1.4 by Robert Collins
Add bzrlib.ui.ui_factory.get_boolean().
209
    def test_text_ui_getbool(self):
3882.8.11 by Martin Pool
Choose the UIFactory class depending on the terminal capabilities
210
        factory = TextUIFactory(None, None, None)
1687.1.4 by Robert Collins
Add bzrlib.ui.ui_factory.get_boolean().
211
        self.assert_get_bool_acceptance_of_user_input(factory)
212
213
    def test_text_factory_prompts_and_clears(self):
214
        # a get_boolean call should clear the pb before prompting
3882.8.10 by Martin Pool
Fix up test_ui for new progress bars
215
        out = _TTYStringIO()
3882.8.11 by Martin Pool
Choose the UIFactory class depending on the terminal capabilities
216
        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
217
        pb = factory.nested_progress_bar()
218
        pb.show_bar = False
219
        pb.show_spinner = False
220
        pb.show_count = False
221
        pb.update("foo", 0, 1)
2363.4.4 by Vincent Ladeuil
More tidying-up.
222
        self.assertEqual(True,
223
                         self.apply_redirected(None, factory.stdout,
224
                                               factory.stdout,
225
                                               factory.get_boolean,
226
                                               "what do you want"))
3882.8.10 by Martin Pool
Fix up test_ui for new progress bars
227
        output = out.getvalue()
228
        self.assertContainsRe(factory.stdout.getvalue(),
229
            "foo *\r\r  *\r*")
230
        self.assertContainsRe(factory.stdout.getvalue(),
231
            r"what do you want\? \[y/n\]: what do you want\? \[y/n\]: ")
232
        # stdin should have been totally consumed
2363.4.6 by Vincent Ladeuil
Fix tests around stdin emptyness.
233
        self.assertEqual('', factory.stdin.readline())