/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)
4449.3.23 by Martin Pool
Correct silly error in test_ui
140
            self.assertIsInstance(uif, TextUIFactory,
141
                'TERM=%r' % (term_type,))
4449.3.4 by Martin Pool
ProgressTask now talks to ProgressView; easier to test
142
1534.5.6 by Robert Collins
split out converter logic into per-format objects.
143
    def test_progress_note(self):
144
        stderr = StringIO()
145
        stdout = StringIO()
3882.8.11 by Martin Pool
Choose the UIFactory class depending on the terminal capabilities
146
        ui_factory = TextUIFactory(stdin=StringIO(''),
147
            stderr=stderr,
148
            stdout=stdout)
1558.8.5 by Aaron Bentley
Pass note up the stack instead of using bzrlib.ui_factory
149
        pb = ui_factory.nested_progress_bar()
1558.8.4 by Aaron Bentley
Fixed test case for pb.note
150
        try:
151
            result = pb.note('t')
152
            self.assertEqual(None, result)
153
            self.assertEqual("t\n", stdout.getvalue())
1843.3.2 by John Arbash Meinel
Fix a ui test that depended on clearing
154
            # Since there was no update() call, there should be no clear() call
2363.4.4 by Vincent Ladeuil
More tidying-up.
155
            self.failIf(re.search(r'^\r {10,}\r$',
156
                                  stderr.getvalue()) is not None,
1843.3.2 by John Arbash Meinel
Fix a ui test that depended on clearing
157
                        'We cleared the stderr without anything to put there')
158
        finally:
159
            pb.finished()
160
161
    def test_progress_note_clears(self):
4449.3.4 by Martin Pool
ProgressTask now talks to ProgressView; easier to test
162
        stderr = _TTYStringIO()
163
        stdout = _TTYStringIO()
164
        # so that we get a TextProgressBar
165
        os.environ['TERM'] = 'xterm'
3882.8.8 by Martin Pool
Progress and UI test cleanups
166
        ui_factory = TextUIFactory(
3882.8.11 by Martin Pool
Choose the UIFactory class depending on the terminal capabilities
167
            stdin=StringIO(''),
3882.8.4 by Martin Pool
All UI factories should support note()
168
            stdout=stdout, stderr=stderr)
4449.3.4 by Martin Pool
ProgressTask now talks to ProgressView; easier to test
169
        self.assertIsInstance(ui_factory._progress_view,
170
            TextProgressView)
1843.3.2 by John Arbash Meinel
Fix a ui test that depended on clearing
171
        pb = ui_factory.nested_progress_bar()
172
        try:
173
            # Create a progress update that isn't throttled
174
            pb.update('x', 1, 1)
175
            result = pb.note('t')
176
            self.assertEqual(None, result)
177
            self.assertEqual("t\n", stdout.getvalue())
1558.8.4 by Aaron Bentley
Fixed test case for pb.note
178
            # the exact contents will depend on the terminal width and we don't
179
            # care about that right now - but you're probably running it on at
180
            # least a 10-character wide terminal :)
1843.3.2 by John Arbash Meinel
Fix a ui test that depended on clearing
181
            self.assertContainsRe(stderr.getvalue(), r'\r {10,}\r$')
1558.8.4 by Aaron Bentley
Fixed test case for pb.note
182
        finally:
1558.8.5 by Aaron Bentley
Pass note up the stack instead of using bzrlib.ui_factory
183
            pb.finished()
1594.1.1 by Robert Collins
Introduce new bzr progress bar api. ui_factory.nested_progress_bar.
184
185
    def test_progress_nested(self):
186
        # test factory based nested and popping.
3882.8.11 by Martin Pool
Choose the UIFactory class depending on the terminal capabilities
187
        ui = TextUIFactory(None, None, None)
1594.1.1 by Robert Collins
Introduce new bzr progress bar api. ui_factory.nested_progress_bar.
188
        pb1 = ui.nested_progress_bar()
189
        pb2 = ui.nested_progress_bar()
3948.2.2 by Martin Pool
Corrections to finishing progress bars
190
        # You do get a warning if the outermost progress bar wasn't finished
191
        # first - it's not clear if this is really useful or if it should just
192
        # 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
193
        warnings, _ = self.callCatchWarnings(pb1.finished)
3948.2.2 by Martin Pool
Corrections to finishing progress bars
194
        if len(warnings) != 1:
195
            self.fail("unexpected warnings: %r" % (warnings,))
1594.1.1 by Robert Collins
Introduce new bzr progress bar api. ui_factory.nested_progress_bar.
196
        pb2.finished()
197
        pb1.finished()
198
1687.1.4 by Robert Collins
Add bzrlib.ui.ui_factory.get_boolean().
199
    def assert_get_bool_acceptance_of_user_input(self, factory):
2363.4.4 by Vincent Ladeuil
More tidying-up.
200
        factory.stdin = StringIO("y\nyes with garbage\n"
201
                                 "yes\nn\nnot an answer\n"
202
                                 "no\nfoo\n")
1687.1.4 by Robert Collins
Add bzrlib.ui.ui_factory.get_boolean().
203
        factory.stdout = StringIO()
4368.3.1 by Vincent Ladeuil
Use stderr for UI prompt to address bug #376582.
204
        factory.stderr = StringIO()
1687.1.4 by Robert Collins
Add bzrlib.ui.ui_factory.get_boolean().
205
        # there is no output from the base factory
206
        self.assertEqual(True, factory.get_boolean(""))
207
        self.assertEqual(True, factory.get_boolean(""))
208
        self.assertEqual(False, factory.get_boolean(""))
209
        self.assertEqual(False, factory.get_boolean(""))
210
        self.assertEqual("foo\n", factory.stdin.read())
2363.4.4 by Vincent Ladeuil
More tidying-up.
211
        # stdin should be empty
2363.4.6 by Vincent Ladeuil
Fix tests around stdin emptyness.
212
        self.assertEqual('', factory.stdin.readline())
1687.1.4 by Robert Collins
Add bzrlib.ui.ui_factory.get_boolean().
213
214
    def test_text_ui_getbool(self):
3882.8.11 by Martin Pool
Choose the UIFactory class depending on the terminal capabilities
215
        factory = TextUIFactory(None, None, None)
1687.1.4 by Robert Collins
Add bzrlib.ui.ui_factory.get_boolean().
216
        self.assert_get_bool_acceptance_of_user_input(factory)
217
4300.3.1 by Martin Pool
Fix string expansion in TextUIFactory.prompt
218
    def test_text_factory_prompt(self):
219
        # see <https://launchpad.net/bugs/365891>
4449.3.18 by Martin Pool
Fuse CLIUIFactory and TextUIFactory and deprecate the old name
220
        factory = TextUIFactory(StringIO(), StringIO(), StringIO())
4300.3.1 by Martin Pool
Fix string expansion in TextUIFactory.prompt
221
        factory.prompt('foo %2e')
4368.3.1 by Vincent Ladeuil
Use stderr for UI prompt to address bug #376582.
222
        self.assertEqual('', factory.stdout.getvalue())
223
        self.assertEqual('foo %2e', factory.stderr.getvalue())
4300.3.1 by Martin Pool
Fix string expansion in TextUIFactory.prompt
224
1687.1.4 by Robert Collins
Add bzrlib.ui.ui_factory.get_boolean().
225
    def test_text_factory_prompts_and_clears(self):
226
        # a get_boolean call should clear the pb before prompting
3882.8.10 by Martin Pool
Fix up test_ui for new progress bars
227
        out = _TTYStringIO()
4449.3.4 by Martin Pool
ProgressTask now talks to ProgressView; easier to test
228
        os.environ['TERM'] = 'xterm'
3882.8.11 by Martin Pool
Choose the UIFactory class depending on the terminal capabilities
229
        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
230
        pb = factory.nested_progress_bar()
231
        pb.show_bar = False
232
        pb.show_spinner = False
233
        pb.show_count = False
234
        pb.update("foo", 0, 1)
2363.4.4 by Vincent Ladeuil
More tidying-up.
235
        self.assertEqual(True,
236
                         self.apply_redirected(None, factory.stdout,
237
                                               factory.stdout,
238
                                               factory.get_boolean,
239
                                               "what do you want"))
3882.8.10 by Martin Pool
Fix up test_ui for new progress bars
240
        output = out.getvalue()
241
        self.assertContainsRe(factory.stdout.getvalue(),
242
            "foo *\r\r  *\r*")
243
        self.assertContainsRe(factory.stdout.getvalue(),
244
            r"what do you want\? \[y/n\]: what do you want\? \[y/n\]: ")
245
        # stdin should have been totally consumed
2363.4.6 by Vincent Ladeuil
Fix tests around stdin emptyness.
246
        self.assertEqual('', factory.stdin.readline())
4017.1.1 by John Arbash Meinel
Get a pb.tick() to work after calling pb.update()
247
248
    def test_text_tick_after_update(self):
249
        ui_factory = TextUIFactory(stdout=StringIO(), stderr=StringIO())
250
        pb = ui_factory.nested_progress_bar()
251
        try:
252
            pb.update('task', 0, 3)
253
            # Reset the clock, so that it actually tries to repaint itself
254
            ui_factory._progress_view._last_repaint = time.time() - 1.0
255
            pb.tick()
256
        finally:
257
            pb.finished()
4110.2.15 by Martin Pool
Fix bug in showing task progress and add a test
258
4222.2.1 by Jelmer Vernooij
Add get_username() call to the UIFactory.
259
    def test_text_ui_getusername(self):
260
        factory = TextUIFactory(None, None, None)
261
        factory.stdin = StringIO("someuser\n\n")
262
        factory.stdout = StringIO()
4368.3.1 by Vincent Ladeuil
Use stderr for UI prompt to address bug #376582.
263
        factory.stderr = StringIO()
4222.2.6 by Jelmer Vernooij
Remove use of NotATerminal.
264
        factory.stdout.encoding = "utf8"
4222.2.1 by Jelmer Vernooij
Add get_username() call to the UIFactory.
265
        # there is no output from the base factory
4368.3.1 by Vincent Ladeuil
Use stderr for UI prompt to address bug #376582.
266
        self.assertEqual("someuser",
267
                         factory.get_username('Hello %(host)s', host='some'))
268
        self.assertEquals("Hello some: ", factory.stderr.getvalue())
269
        self.assertEquals('', factory.stdout.getvalue())
4222.2.1 by Jelmer Vernooij
Add get_username() call to the UIFactory.
270
        self.assertEqual("", factory.get_username("Gebruiker"))
271
        # stdin should be empty
272
        self.assertEqual('', factory.stdin.readline())
273
4222.2.2 by Jelmer Vernooij
Review from vila: Deal with UTF8 strings in prompts, fix typo.
274
    def test_text_ui_getusername_utf8(self):
4222.2.3 by Jelmer Vernooij
Also check for unicode usernames.
275
        ui = TestUIFactory(stdin=u'someuser\u1234'.encode('utf8'),
4368.3.1 by Vincent Ladeuil
Use stderr for UI prompt to address bug #376582.
276
                           stdout=StringIOWrapper(), stderr=StringIOWrapper())
277
        ui.stderr.encoding = ui.stdout.encoding = ui.stdin.encoding = "utf8"
4222.2.3 by Jelmer Vernooij
Also check for unicode usernames.
278
        pb = ui.nested_progress_bar()
279
        try:
280
            # there is no output from the base factory
4368.3.1 by Vincent Ladeuil
Use stderr for UI prompt to address bug #376582.
281
            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.
282
                ui.get_username, u'Hello\u1234 %(host)s', host=u'some\u1234')
4222.2.8 by Jelmer Vernooij
Fix copy-n-paste error.
283
            self.assertEquals(u"someuser\u1234", username.decode('utf8'))
4368.3.1 by Vincent Ladeuil
Use stderr for UI prompt to address bug #376582.
284
            self.assertEquals(u"Hello\u1234 some\u1234: ",
285
                              ui.stderr.getvalue().decode("utf8"))
286
            self.assertEquals('', ui.stdout.getvalue())
4222.2.3 by Jelmer Vernooij
Also check for unicode usernames.
287
        finally:
288
            pb.finished()
4222.2.2 by Jelmer Vernooij
Review from vila: Deal with UTF8 strings in prompts, fix typo.
289
4110.2.15 by Martin Pool
Fix bug in showing task progress and add a test
290
4449.3.19 by Martin Pool
SilentUIFactory now always errors when asked for input
291
class CLIUITests(TestCase):
292
293
    def test_cli_factory_deprecated(self):
294
        uif = self.applyDeprecated(deprecated_in((1, 17, 0)),
295
            CLIUIFactory,
296
            StringIO(), StringIO(), StringIO())
297
        self.assertIsInstance(uif, UIFactory)
298
299
300
class SilentUITests(TestCase):
301
302
    def test_silent_factory(self):
303
        ui = SilentUIFactory()
304
        stdout = StringIO()
305
        self.assertRaises(
306
            NotImplementedError,
307
            self.apply_redirected,
308
            None, stdout, stdout, ui.get_password)
309
        # and it didn't write anything out either
310
        self.assertEqual('', stdout.getvalue())
311
312
    def test_silent_ui_getbool(self):
313
        factory = SilentUIFactory()
314
        stdout = StringIO()
315
        self.assertRaises(
316
            NotImplementedError,
317
            self.apply_redirected,
318
            None, stdout, stdout, factory.get_boolean, "foo")