/brz/remove-bazaar

To get this branch, use:
bzr branch http://gegoxaren.bato24.eu/bzr/brz/remove-bazaar
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
4183.7.1 by Sabin Iacob
update FSF mailing address
15
# Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
1185.49.22 by John Arbash Meinel
Added get_password to the UIFactory, using it inside of sftp.py
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
4449.3.4 by Martin Pool
ProgressTask now talks to ProgressView; easier to test
28
from bzrlib.progress import (
29
    ProgressTask,
30
    )
3948.2.6 by Martin Pool
ProgressBarStack is deprecated
31
from bzrlib.symbol_versioning import (
32
    deprecated_in,
33
    )
2294.4.1 by Vincent Ladeuil
Add a UIFactory.get_login method, fix tests.
34
from bzrlib.tests import (
2363.4.10 by Vincent Ladeuil
Complete tests.
35
    TestCase,
2294.4.4 by Vincent Ladeuil
Provide a better implementation for testing passwords.
36
    TestUIFactory,
2294.4.1 by Vincent Ladeuil
Add a UIFactory.get_login method, fix tests.
37
    StringIOWrapper,
38
    )
4449.3.4 by Martin Pool
ProgressTask now talks to ProgressView; easier to test
39
from bzrlib.tests.test_progress import (
40
    _NonTTYStringIO,
41
    _TTYStringIO,
42
    )
2363.4.10 by Vincent Ladeuil
Complete tests.
43
from bzrlib.ui import (
44
    CLIUIFactory,
45
    SilentUIFactory,
4449.3.18 by Martin Pool
Fuse CLIUIFactory and TextUIFactory and deprecate the old name
46
    UIFactory,
4449.3.14 by Martin Pool
Clean up pb tests into tabular form
47
    make_ui_for_terminal,
2363.4.10 by Vincent Ladeuil
Complete tests.
48
    )
4110.2.15 by Martin Pool
Fix bug in showing task progress and add a test
49
from bzrlib.ui.text import (
4449.2.4 by Martin Pool
Add tests for BZR_PROGRESS_BAR
50
    NullProgressView,
4110.2.15 by Martin Pool
Fix bug in showing task progress and add a test
51
    TextProgressView,
52
    TextUIFactory,
53
    )
1185.49.22 by John Arbash Meinel
Added get_password to the UIFactory, using it inside of sftp.py
54
1681.1.2 by Robert Collins
* bzrlib.ui.text.TextUIFactory now accepts a bar_type parameter which
55
1185.49.22 by John Arbash Meinel
Added get_password to the UIFactory, using it inside of sftp.py
56
class UITests(TestCase):
57
2294.4.1 by Vincent Ladeuil
Add a UIFactory.get_login method, fix tests.
58
    def test_text_factory_ascii_password(self):
4368.3.1 by Vincent Ladeuil
Use stderr for UI prompt to address bug #376582.
59
        ui = TestUIFactory(stdin='secret\n', stdout=StringIOWrapper(),
60
                           stderr=StringIOWrapper())
2294.4.1 by Vincent Ladeuil
Add a UIFactory.get_login method, fix tests.
61
        pb = ui.nested_progress_bar()
62
        try:
63
            self.assertEqual('secret',
64
                             self.apply_redirected(ui.stdin, ui.stdout,
4368.3.1 by Vincent Ladeuil
Use stderr for UI prompt to address bug #376582.
65
                                                   ui.stderr,
2294.4.1 by Vincent Ladeuil
Add a UIFactory.get_login method, fix tests.
66
                                                   ui.get_password))
67
            # ': ' is appended to prompt
4368.3.1 by Vincent Ladeuil
Use stderr for UI prompt to address bug #376582.
68
            self.assertEqual(': ', ui.stderr.getvalue())
69
            self.assertEqual('', ui.stdout.readline())
2363.4.3 by Vincent Ladeuil
Tidy-up tests.
70
            # stdin should be empty
2363.4.6 by Vincent Ladeuil
Fix tests around stdin emptyness.
71
            self.assertEqual('', ui.stdin.readline())
2294.4.1 by Vincent Ladeuil
Add a UIFactory.get_login method, fix tests.
72
        finally:
73
            pb.finished()
74
75
    def test_text_factory_utf8_password(self):
76
        """Test an utf8 password.
77
78
        We can't predict what encoding users will have for stdin, so we force
79
        it to utf8 to test that we transport the password correctly.
80
        """
2294.4.4 by Vincent Ladeuil
Provide a better implementation for testing passwords.
81
        ui = TestUIFactory(stdin=u'baz\u1234'.encode('utf8'),
4368.3.1 by Vincent Ladeuil
Use stderr for UI prompt to address bug #376582.
82
                           stdout=StringIOWrapper(),
83
                           stderr=StringIOWrapper())
84
        ui.stderr.encoding = ui.stdout.encoding = ui.stdin.encoding = 'utf8'
2294.4.1 by Vincent Ladeuil
Add a UIFactory.get_login method, fix tests.
85
        pb = ui.nested_progress_bar()
86
        try:
4368.3.1 by Vincent Ladeuil
Use stderr for UI prompt to address bug #376582.
87
            password = self.apply_redirected(ui.stdin, ui.stdout, ui.stderr,
2294.4.1 by Vincent Ladeuil
Add a UIFactory.get_login method, fix tests.
88
                                             ui.get_password,
89
                                             u'Hello \u1234 %(user)s',
90
                                             user=u'some\u1234')
91
            # We use StringIO objects, we need to decode them
92
            self.assertEqual(u'baz\u1234', password.decode('utf8'))
93
            self.assertEqual(u'Hello \u1234 some\u1234: ',
4368.3.1 by Vincent Ladeuil
Use stderr for UI prompt to address bug #376582.
94
                             ui.stderr.getvalue().decode('utf8'))
95
            # stdin and stdout should be empty
2363.4.6 by Vincent Ladeuil
Fix tests around stdin emptyness.
96
            self.assertEqual('', ui.stdin.readline())
4368.3.1 by Vincent Ladeuil
Use stderr for UI prompt to address bug #376582.
97
            self.assertEqual('', ui.stdout.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
4449.2.4 by Martin Pool
Add tests for BZR_PROGRESS_BAR
101
    def test_progress_construction(self):
102
        """TextUIFactory constructs the right progress view.
103
        """
4449.3.14 by Martin Pool
Clean up pb tests into tabular form
104
        for (term, pb, expected_pb_class) in (
105
            # on an xterm, either use them or not as the user requests,
106
            # otherwise default on
107
            ('xterm', 'none', NullProgressView),
108
            ('xterm', 'text', TextProgressView),
109
            ('xterm', None, TextProgressView),
110
            # on a dumb terminal, again if there's explicit configuration do
111
            # it, otherwise default off
112
            ('dumb', 'none', NullProgressView),
4449.3.18 by Martin Pool
Fuse CLIUIFactory and TextUIFactory and deprecate the old name
113
            ('dumb', 'text', TextProgressView),
4449.3.14 by Martin Pool
Clean up pb tests into tabular form
114
            ('dumb', None, NullProgressView),
115
            ):
116
            os.environ['TERM'] = term
117
            if pb is None:
118
                del os.environ['BZR_PROGRESS_BAR']
119
            else:
120
                os.environ['BZR_PROGRESS_BAR'] = pb
121
            stdin = _TTYStringIO('')
122
            stderr = _TTYStringIO()
123
            stdout = _TTYStringIO()
124
            uif = make_ui_for_terminal(stdin, stdout, stderr)
125
            self.assertIsInstance(uif.make_progress_view(),
126
                expected_pb_class,
127
                "TERM=%s BZR_PROGRESS_BAR=%s uif=%r" % (term, pb, uif,))
4449.2.4 by Martin Pool
Add tests for BZR_PROGRESS_BAR
128
4449.3.20 by Martin Pool
Add extra tests for non-tty text ui
129
    def test_text_ui_non_terminal(self):
130
        """Even on non-ttys, make_ui_for_terminal gives a text ui."""
131
        stdin = _NonTTYStringIO('')
132
        stderr = _NonTTYStringIO()
133
        stdout = _NonTTYStringIO()
134
        for term_type in ['dumb', None, 'xterm']:
135
            if term_type is None:
136
                del os.environ['TERM']
137
            else:
138
                os.environ['TERM'] = term_type
139
            uif = make_ui_for_terminal(stdin, stdout, stderr)
140
            self.assertEqual(uif, TextUIFactory)
4449.3.4 by Martin Pool
ProgressTask now talks to ProgressView; easier to test
141
1534.5.6 by Robert Collins
split out converter logic into per-format objects.
142
    def test_progress_note(self):
143
        stderr = StringIO()
144
        stdout = StringIO()
3882.8.11 by Martin Pool
Choose the UIFactory class depending on the terminal capabilities
145
        ui_factory = TextUIFactory(stdin=StringIO(''),
146
            stderr=stderr,
147
            stdout=stdout)
1558.8.5 by Aaron Bentley
Pass note up the stack instead of using bzrlib.ui_factory
148
        pb = ui_factory.nested_progress_bar()
1558.8.4 by Aaron Bentley
Fixed test case for pb.note
149
        try:
150
            result = pb.note('t')
151
            self.assertEqual(None, result)
152
            self.assertEqual("t\n", stdout.getvalue())
1843.3.2 by John Arbash Meinel
Fix a ui test that depended on clearing
153
            # Since there was no update() call, there should be no clear() call
2363.4.4 by Vincent Ladeuil
More tidying-up.
154
            self.failIf(re.search(r'^\r {10,}\r$',
155
                                  stderr.getvalue()) is not None,
1843.3.2 by John Arbash Meinel
Fix a ui test that depended on clearing
156
                        'We cleared the stderr without anything to put there')
157
        finally:
158
            pb.finished()
159
160
    def test_progress_note_clears(self):
4449.3.4 by Martin Pool
ProgressTask now talks to ProgressView; easier to test
161
        stderr = _TTYStringIO()
162
        stdout = _TTYStringIO()
163
        # so that we get a TextProgressBar
164
        os.environ['TERM'] = 'xterm'
3882.8.8 by Martin Pool
Progress and UI test cleanups
165
        ui_factory = TextUIFactory(
3882.8.11 by Martin Pool
Choose the UIFactory class depending on the terminal capabilities
166
            stdin=StringIO(''),
3882.8.4 by Martin Pool
All UI factories should support note()
167
            stdout=stdout, stderr=stderr)
4449.3.4 by Martin Pool
ProgressTask now talks to ProgressView; easier to test
168
        self.assertIsInstance(ui_factory._progress_view,
169
            TextProgressView)
1843.3.2 by John Arbash Meinel
Fix a ui test that depended on clearing
170
        pb = ui_factory.nested_progress_bar()
171
        try:
172
            # Create a progress update that isn't throttled
173
            pb.update('x', 1, 1)
174
            result = pb.note('t')
175
            self.assertEqual(None, result)
176
            self.assertEqual("t\n", stdout.getvalue())
1558.8.4 by Aaron Bentley
Fixed test case for pb.note
177
            # the exact contents will depend on the terminal width and we don't
178
            # care about that right now - but you're probably running it on at
179
            # least a 10-character wide terminal :)
1843.3.2 by John Arbash Meinel
Fix a ui test that depended on clearing
180
            self.assertContainsRe(stderr.getvalue(), r'\r {10,}\r$')
1558.8.4 by Aaron Bentley
Fixed test case for pb.note
181
        finally:
1558.8.5 by Aaron Bentley
Pass note up the stack instead of using bzrlib.ui_factory
182
            pb.finished()
1594.1.1 by Robert Collins
Introduce new bzr progress bar api. ui_factory.nested_progress_bar.
183
184
    def test_progress_nested(self):
185
        # test factory based nested and popping.
3882.8.11 by Martin Pool
Choose the UIFactory class depending on the terminal capabilities
186
        ui = TextUIFactory(None, None, None)
1594.1.1 by Robert Collins
Introduce new bzr progress bar api. ui_factory.nested_progress_bar.
187
        pb1 = ui.nested_progress_bar()
188
        pb2 = ui.nested_progress_bar()
3948.2.2 by Martin Pool
Corrections to finishing progress bars
189
        # You do get a warning if the outermost progress bar wasn't finished
190
        # first - it's not clear if this is really useful or if it should just
191
        # 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
192
        warnings, _ = self.callCatchWarnings(pb1.finished)
3948.2.2 by Martin Pool
Corrections to finishing progress bars
193
        if len(warnings) != 1:
194
            self.fail("unexpected warnings: %r" % (warnings,))
1594.1.1 by Robert Collins
Introduce new bzr progress bar api. ui_factory.nested_progress_bar.
195
        pb2.finished()
196
        pb1.finished()
197
1687.1.4 by Robert Collins
Add bzrlib.ui.ui_factory.get_boolean().
198
    def assert_get_bool_acceptance_of_user_input(self, factory):
2363.4.4 by Vincent Ladeuil
More tidying-up.
199
        factory.stdin = StringIO("y\nyes with garbage\n"
200
                                 "yes\nn\nnot an answer\n"
201
                                 "no\nfoo\n")
1687.1.4 by Robert Collins
Add bzrlib.ui.ui_factory.get_boolean().
202
        factory.stdout = StringIO()
4368.3.1 by Vincent Ladeuil
Use stderr for UI prompt to address bug #376582.
203
        factory.stderr = StringIO()
1687.1.4 by Robert Collins
Add bzrlib.ui.ui_factory.get_boolean().
204
        # there is no output from the base factory
205
        self.assertEqual(True, factory.get_boolean(""))
206
        self.assertEqual(True, factory.get_boolean(""))
207
        self.assertEqual(False, factory.get_boolean(""))
208
        self.assertEqual(False, factory.get_boolean(""))
209
        self.assertEqual("foo\n", factory.stdin.read())
2363.4.4 by Vincent Ladeuil
More tidying-up.
210
        # stdin should be empty
2363.4.6 by Vincent Ladeuil
Fix tests around stdin emptyness.
211
        self.assertEqual('', factory.stdin.readline())
1687.1.4 by Robert Collins
Add bzrlib.ui.ui_factory.get_boolean().
212
213
    def test_text_ui_getbool(self):
3882.8.11 by Martin Pool
Choose the UIFactory class depending on the terminal capabilities
214
        factory = TextUIFactory(None, None, None)
1687.1.4 by Robert Collins
Add bzrlib.ui.ui_factory.get_boolean().
215
        self.assert_get_bool_acceptance_of_user_input(factory)
216
4300.3.1 by Martin Pool
Fix string expansion in TextUIFactory.prompt
217
    def test_text_factory_prompt(self):
218
        # see <https://launchpad.net/bugs/365891>
4449.3.18 by Martin Pool
Fuse CLIUIFactory and TextUIFactory and deprecate the old name
219
        factory = TextUIFactory(StringIO(), StringIO(), StringIO())
4300.3.1 by Martin Pool
Fix string expansion in TextUIFactory.prompt
220
        factory.prompt('foo %2e')
4368.3.1 by Vincent Ladeuil
Use stderr for UI prompt to address bug #376582.
221
        self.assertEqual('', factory.stdout.getvalue())
222
        self.assertEqual('foo %2e', factory.stderr.getvalue())
4300.3.1 by Martin Pool
Fix string expansion in TextUIFactory.prompt
223
1687.1.4 by Robert Collins
Add bzrlib.ui.ui_factory.get_boolean().
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()
4449.3.4 by Martin Pool
ProgressTask now talks to ProgressView; easier to test
227
        os.environ['TERM'] = 'xterm'
3882.8.11 by Martin Pool
Choose the UIFactory class depending on the terminal capabilities
228
        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
229
        pb = factory.nested_progress_bar()
230
        pb.show_bar = False
231
        pb.show_spinner = False
232
        pb.show_count = False
233
        pb.update("foo", 0, 1)
2363.4.4 by Vincent Ladeuil
More tidying-up.
234
        self.assertEqual(True,
235
                         self.apply_redirected(None, factory.stdout,
236
                                               factory.stdout,
237
                                               factory.get_boolean,
238
                                               "what do you want"))
3882.8.10 by Martin Pool
Fix up test_ui for new progress bars
239
        output = out.getvalue()
240
        self.assertContainsRe(factory.stdout.getvalue(),
241
            "foo *\r\r  *\r*")
242
        self.assertContainsRe(factory.stdout.getvalue(),
243
            r"what do you want\? \[y/n\]: what do you want\? \[y/n\]: ")
244
        # stdin should have been totally consumed
2363.4.6 by Vincent Ladeuil
Fix tests around stdin emptyness.
245
        self.assertEqual('', factory.stdin.readline())
4017.1.1 by John Arbash Meinel
Get a pb.tick() to work after calling pb.update()
246
247
    def test_text_tick_after_update(self):
248
        ui_factory = TextUIFactory(stdout=StringIO(), stderr=StringIO())
249
        pb = ui_factory.nested_progress_bar()
250
        try:
251
            pb.update('task', 0, 3)
252
            # Reset the clock, so that it actually tries to repaint itself
253
            ui_factory._progress_view._last_repaint = time.time() - 1.0
254
            pb.tick()
255
        finally:
256
            pb.finished()
4110.2.15 by Martin Pool
Fix bug in showing task progress and add a test
257
4222.2.1 by Jelmer Vernooij
Add get_username() call to the UIFactory.
258
    def test_text_ui_getusername(self):
259
        factory = TextUIFactory(None, None, None)
260
        factory.stdin = StringIO("someuser\n\n")
261
        factory.stdout = StringIO()
4368.3.1 by Vincent Ladeuil
Use stderr for UI prompt to address bug #376582.
262
        factory.stderr = StringIO()
4222.2.6 by Jelmer Vernooij
Remove use of NotATerminal.
263
        factory.stdout.encoding = "utf8"
4222.2.1 by Jelmer Vernooij
Add get_username() call to the UIFactory.
264
        # there is no output from the base factory
4368.3.1 by Vincent Ladeuil
Use stderr for UI prompt to address bug #376582.
265
        self.assertEqual("someuser",
266
                         factory.get_username('Hello %(host)s', host='some'))
267
        self.assertEquals("Hello some: ", factory.stderr.getvalue())
268
        self.assertEquals('', factory.stdout.getvalue())
4222.2.1 by Jelmer Vernooij
Add get_username() call to the UIFactory.
269
        self.assertEqual("", factory.get_username("Gebruiker"))
270
        # stdin should be empty
271
        self.assertEqual('', factory.stdin.readline())
272
4222.2.2 by Jelmer Vernooij
Review from vila: Deal with UTF8 strings in prompts, fix typo.
273
    def test_text_ui_getusername_utf8(self):
4222.2.3 by Jelmer Vernooij
Also check for unicode usernames.
274
        ui = TestUIFactory(stdin=u'someuser\u1234'.encode('utf8'),
4368.3.1 by Vincent Ladeuil
Use stderr for UI prompt to address bug #376582.
275
                           stdout=StringIOWrapper(), stderr=StringIOWrapper())
276
        ui.stderr.encoding = ui.stdout.encoding = ui.stdin.encoding = "utf8"
4222.2.3 by Jelmer Vernooij
Also check for unicode usernames.
277
        pb = ui.nested_progress_bar()
278
        try:
279
            # there is no output from the base factory
4368.3.1 by Vincent Ladeuil
Use stderr for UI prompt to address bug #376582.
280
            username = self.apply_redirected(ui.stdin, ui.stdout, ui.stderr,
4222.2.12 by Jelmer Vernooij
Redirect to fix utf8 test with LC_ALL=C.
281
                ui.get_username, u'Hello\u1234 %(host)s', host=u'some\u1234')
4222.2.8 by Jelmer Vernooij
Fix copy-n-paste error.
282
            self.assertEquals(u"someuser\u1234", username.decode('utf8'))
4368.3.1 by Vincent Ladeuil
Use stderr for UI prompt to address bug #376582.
283
            self.assertEquals(u"Hello\u1234 some\u1234: ",
284
                              ui.stderr.getvalue().decode("utf8"))
285
            self.assertEquals('', ui.stdout.getvalue())
4222.2.3 by Jelmer Vernooij
Also check for unicode usernames.
286
        finally:
287
            pb.finished()
4222.2.2 by Jelmer Vernooij
Review from vila: Deal with UTF8 strings in prompts, fix typo.
288
4110.2.15 by Martin Pool
Fix bug in showing task progress and add a test
289
4449.3.19 by Martin Pool
SilentUIFactory now always errors when asked for input
290
class CLIUITests(TestCase):
291
292
    def test_cli_factory_deprecated(self):
293
        uif = self.applyDeprecated(deprecated_in((1, 17, 0)),
294
            CLIUIFactory,
295
            StringIO(), StringIO(), StringIO())
296
        self.assertIsInstance(uif, UIFactory)
297
298
299
class SilentUITests(TestCase):
300
301
    def test_silent_factory(self):
302
        ui = SilentUIFactory()
303
        stdout = StringIO()
304
        self.assertRaises(
305
            NotImplementedError,
306
            self.apply_redirected,
307
            None, stdout, stdout, ui.get_password)
308
        # and it didn't write anything out either
309
        self.assertEqual('', stdout.getvalue())
310
311
    def test_silent_ui_getbool(self):
312
        factory = SilentUIFactory()
313
        stdout = StringIO()
314
        self.assertRaises(
315
            NotImplementedError,
316
            self.apply_redirected,
317
            None, stdout, stdout, factory.get_boolean, "foo")