/brz/remove-bazaar

To get this branch, use:
bzr branch http://gegoxaren.bato24.eu/bzr/brz/remove-bazaar
4597.9.5 by Vincent Ladeuil
Merge bzr.dev into cleanup
1
# Copyright (C) 2005-2010 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
1704.2.9 by Martin Pool
Make text_factory test not depend on 80-col terminal
21
import re
4017.1.1 by John Arbash Meinel
Get a pb.tick() to work after calling pb.update()
22
import time
1185.49.22 by John Arbash Meinel
Added get_password to the UIFactory, using it inside of sftp.py
23
4797.40.2 by Martin Pool
Add missing import
24
from StringIO import StringIO
25
4488.1.1 by Vincent Ladeuil
(vila) Cleanup imports in some test files
26
from bzrlib import (
5230.1.3 by Martin Pool
Use configured output encoding for make_output_stream
27
    config,
4488.1.1 by Vincent Ladeuil
(vila) Cleanup imports in some test files
28
    errors,
4634.144.5 by Martin Pool
Cleaner presentation and tests for warn_cross_format_fetch
29
    remote,
30
    repository,
4488.1.1 by Vincent Ladeuil
(vila) Cleanup imports in some test files
31
    tests,
32
    ui as _mod_ui,
4449.3.4 by Martin Pool
ProgressTask now talks to ProgressView; easier to test
33
    )
3948.2.6 by Martin Pool
ProgressBarStack is deprecated
34
from bzrlib.symbol_versioning import (
35
    deprecated_in,
36
    )
5230.1.3 by Martin Pool
Use configured output encoding for make_output_stream
37
from bzrlib.tests import (
38
    fixtures,
39
    test_progress,
40
    )
4597.3.36 by Vincent Ladeuil
Fix imports and various cleanups in test_ui.
41
from bzrlib.ui import text as _mod_ui_text
5422.1.5 by Martin Pool
Move ProgressRecordingUIFactory to bzrlib.tests.testui
42
from bzrlib.tests.testui import (
5422.1.4 by Martin Pool
Rename CapturingUIFactory to ProgressRecordingUIFactory
43
    ProgressRecordingUIFactory,
5422.1.1 by Martin Pool
Move CapturingUIFactory out of per_workingtree tests into somewhere reusable
44
    )
1185.49.22 by John Arbash Meinel
Added get_password to the UIFactory, using it inside of sftp.py
45
1681.1.2 by Robert Collins
* bzrlib.ui.text.TextUIFactory now accepts a bar_type parameter which
46
5230.1.3 by Martin Pool
Use configured output encoding for make_output_stream
47
class TestUIConfiguration(tests.TestCaseWithTransport):
48
49
    def test_output_encoding_configuration(self):
5230.1.5 by Martin Pool
Merge updated test fixtures
50
        enc = fixtures.generate_unicode_encodings().next()
5230.1.3 by Martin Pool
Use configured output encoding for make_output_stream
51
        config.GlobalConfig().set_user_option('output_encoding',
52
            enc)
53
        ui = tests.TestUIFactory(stdin=None,
5230.1.5 by Martin Pool
Merge updated test fixtures
54
            stdout=tests.StringIOWrapper(),
55
            stderr=tests.StringIOWrapper())
5230.1.3 by Martin Pool
Use configured output encoding for make_output_stream
56
        os = ui.make_output_stream()
57
        self.assertEquals(os.encoding, enc)
58
59
4711.1.6 by Martin Pool
Separate TextUIFactory tests
60
class TestTextUIFactory(tests.TestCase):
2294.4.1 by Vincent Ladeuil
Add a UIFactory.get_login method, fix tests.
61
62
    def test_text_factory_ascii_password(self):
4488.1.1 by Vincent Ladeuil
(vila) Cleanup imports in some test files
63
        ui = tests.TestUIFactory(stdin='secret\n',
64
                                 stdout=tests.StringIOWrapper(),
65
                                 stderr=tests.StringIOWrapper())
2294.4.1 by Vincent Ladeuil
Add a UIFactory.get_login method, fix tests.
66
        pb = ui.nested_progress_bar()
67
        try:
68
            self.assertEqual('secret',
69
                             self.apply_redirected(ui.stdin, ui.stdout,
4368.3.1 by Vincent Ladeuil
Use stderr for UI prompt to address bug #376582.
70
                                                   ui.stderr,
2294.4.1 by Vincent Ladeuil
Add a UIFactory.get_login method, fix tests.
71
                                                   ui.get_password))
72
            # ': ' is appended to prompt
4368.3.1 by Vincent Ladeuil
Use stderr for UI prompt to address bug #376582.
73
            self.assertEqual(': ', ui.stderr.getvalue())
74
            self.assertEqual('', ui.stdout.readline())
2363.4.3 by Vincent Ladeuil
Tidy-up tests.
75
            # stdin should be empty
2363.4.6 by Vincent Ladeuil
Fix tests around stdin emptyness.
76
            self.assertEqual('', ui.stdin.readline())
2294.4.1 by Vincent Ladeuil
Add a UIFactory.get_login method, fix tests.
77
        finally:
78
            pb.finished()
79
80
    def test_text_factory_utf8_password(self):
81
        """Test an utf8 password.
82
83
        We can't predict what encoding users will have for stdin, so we force
84
        it to utf8 to test that we transport the password correctly.
85
        """
4488.1.1 by Vincent Ladeuil
(vila) Cleanup imports in some test files
86
        ui = tests.TestUIFactory(stdin=u'baz\u1234'.encode('utf8'),
87
                                 stdout=tests.StringIOWrapper(),
88
                                 stderr=tests.StringIOWrapper())
4368.3.1 by Vincent Ladeuil
Use stderr for UI prompt to address bug #376582.
89
        ui.stderr.encoding = ui.stdout.encoding = ui.stdin.encoding = 'utf8'
2294.4.1 by Vincent Ladeuil
Add a UIFactory.get_login method, fix tests.
90
        pb = ui.nested_progress_bar()
91
        try:
4368.3.1 by Vincent Ladeuil
Use stderr for UI prompt to address bug #376582.
92
            password = self.apply_redirected(ui.stdin, ui.stdout, ui.stderr,
2294.4.1 by Vincent Ladeuil
Add a UIFactory.get_login method, fix tests.
93
                                             ui.get_password,
94
                                             u'Hello \u1234 %(user)s',
95
                                             user=u'some\u1234')
96
            # We use StringIO objects, we need to decode them
97
            self.assertEqual(u'baz\u1234', password.decode('utf8'))
98
            self.assertEqual(u'Hello \u1234 some\u1234: ',
4368.3.1 by Vincent Ladeuil
Use stderr for UI prompt to address bug #376582.
99
                             ui.stderr.getvalue().decode('utf8'))
100
            # stdin and stdout should be empty
2363.4.6 by Vincent Ladeuil
Fix tests around stdin emptyness.
101
            self.assertEqual('', ui.stdin.readline())
4368.3.1 by Vincent Ladeuil
Use stderr for UI prompt to address bug #376582.
102
            self.assertEqual('', ui.stdout.readline())
2294.4.1 by Vincent Ladeuil
Add a UIFactory.get_login method, fix tests.
103
        finally:
104
            pb.finished()
1534.5.6 by Robert Collins
split out converter logic into per-format objects.
105
4449.3.38 by Martin Pool
Cleanup get_boolean tests
106
    def test_text_ui_get_boolean(self):
4773.1.2 by Vincent Ladeuil
Cleanup imports in test_ui
107
        stdin = tests.StringIOWrapper("y\n" # True
108
                                      "n\n" # False
109
                                      "yes with garbage\nY\n" # True
110
                                      "not an answer\nno\n" # False
111
                                      "I'm sure!\nyes\n" # True
112
                                      "NO\n" # False
113
                                      "foo\n")
4597.3.36 by Vincent Ladeuil
Fix imports and various cleanups in test_ui.
114
        stdout = tests.StringIOWrapper()
115
        stderr = tests.StringIOWrapper()
116
        factory = _mod_ui_text.TextUIFactory(stdin, stdout, stderr)
1687.1.4 by Robert Collins
Add bzrlib.ui.ui_factory.get_boolean().
117
        self.assertEqual(True, factory.get_boolean(""))
4503.2.5 by Vincent Ladeuil
ui.get_boolean can also use bool_from_string.
118
        self.assertEqual(False, factory.get_boolean(""))
119
        self.assertEqual(True, factory.get_boolean(""))
120
        self.assertEqual(False, factory.get_boolean(""))
121
        self.assertEqual(True, factory.get_boolean(""))
122
        self.assertEqual(False, factory.get_boolean(""))
1687.1.4 by Robert Collins
Add bzrlib.ui.ui_factory.get_boolean().
123
        self.assertEqual("foo\n", factory.stdin.read())
2363.4.4 by Vincent Ladeuil
More tidying-up.
124
        # stdin should be empty
2363.4.6 by Vincent Ladeuil
Fix tests around stdin emptyness.
125
        self.assertEqual('', factory.stdin.readline())
1687.1.4 by Robert Collins
Add bzrlib.ui.ui_factory.get_boolean().
126
4597.3.37 by Vincent Ladeuil
Allows ui factories to query users for an integer.
127
    def test_text_ui_get_integer(self):
128
        stdin = tests.StringIOWrapper(
129
            "1\n"
130
            "  -2  \n"
131
            "hmmm\nwhat else ?\nCome on\nok 42\n4.24\n42\n")
132
        stdout = tests.StringIOWrapper()
133
        stderr = tests.StringIOWrapper()
134
        factory = _mod_ui_text.TextUIFactory(stdin, stdout, stderr)
135
        self.assertEqual(1, factory.get_integer(""))
136
        self.assertEqual(-2, factory.get_integer(""))
137
        self.assertEqual(42, factory.get_integer(""))
138
4300.3.1 by Martin Pool
Fix string expansion in TextUIFactory.prompt
139
    def test_text_factory_prompt(self):
140
        # see <https://launchpad.net/bugs/365891>
4597.3.36 by Vincent Ladeuil
Fix imports and various cleanups in test_ui.
141
        StringIO = tests.StringIOWrapper
142
        factory = _mod_ui_text.TextUIFactory(StringIO(), StringIO(), StringIO())
4300.3.1 by Martin Pool
Fix string expansion in TextUIFactory.prompt
143
        factory.prompt('foo %2e')
4368.3.1 by Vincent Ladeuil
Use stderr for UI prompt to address bug #376582.
144
        self.assertEqual('', factory.stdout.getvalue())
145
        self.assertEqual('foo %2e', factory.stderr.getvalue())
4300.3.1 by Martin Pool
Fix string expansion in TextUIFactory.prompt
146
1687.1.4 by Robert Collins
Add bzrlib.ui.ui_factory.get_boolean().
147
    def test_text_factory_prompts_and_clears(self):
148
        # a get_boolean call should clear the pb before prompting
4597.3.36 by Vincent Ladeuil
Fix imports and various cleanups in test_ui.
149
        out = test_progress._TTYStringIO()
4449.3.4 by Martin Pool
ProgressTask now talks to ProgressView; easier to test
150
        os.environ['TERM'] = 'xterm'
4597.3.36 by Vincent Ladeuil
Fix imports and various cleanups in test_ui.
151
        factory = _mod_ui_text.TextUIFactory(
152
            stdin=tests.StringIOWrapper("yada\ny\n"),
153
            stdout=out, stderr=out)
5339.2.6 by Martin Pool
One more UI test needs updates for spinner being at the front
154
        factory._avail_width = lambda: 79
3882.8.10 by Martin Pool
Fix up test_ui for new progress bars
155
        pb = factory.nested_progress_bar()
156
        pb.show_bar = False
157
        pb.show_spinner = False
158
        pb.show_count = False
159
        pb.update("foo", 0, 1)
2363.4.4 by Vincent Ladeuil
More tidying-up.
160
        self.assertEqual(True,
161
                         self.apply_redirected(None, factory.stdout,
162
                                               factory.stdout,
163
                                               factory.get_boolean,
164
                                               "what do you want"))
3882.8.10 by Martin Pool
Fix up test_ui for new progress bars
165
        output = out.getvalue()
5339.2.6 by Martin Pool
One more UI test needs updates for spinner being at the front
166
        self.assertContainsRe(output,
167
            "| foo *\r\r  *\r*")
168
        self.assertContainsRe(output,
3882.8.10 by Martin Pool
Fix up test_ui for new progress bars
169
            r"what do you want\? \[y/n\]: what do you want\? \[y/n\]: ")
170
        # stdin should have been totally consumed
2363.4.6 by Vincent Ladeuil
Fix tests around stdin emptyness.
171
        self.assertEqual('', factory.stdin.readline())
4017.1.1 by John Arbash Meinel
Get a pb.tick() to work after calling pb.update()
172
173
    def test_text_tick_after_update(self):
4597.3.36 by Vincent Ladeuil
Fix imports and various cleanups in test_ui.
174
        ui_factory = _mod_ui_text.TextUIFactory(stdout=tests.StringIOWrapper(),
175
                                                stderr=tests.StringIOWrapper())
4017.1.1 by John Arbash Meinel
Get a pb.tick() to work after calling pb.update()
176
        pb = ui_factory.nested_progress_bar()
177
        try:
178
            pb.update('task', 0, 3)
179
            # Reset the clock, so that it actually tries to repaint itself
180
            ui_factory._progress_view._last_repaint = time.time() - 1.0
181
            pb.tick()
182
        finally:
183
            pb.finished()
4110.2.15 by Martin Pool
Fix bug in showing task progress and add a test
184
4222.2.1 by Jelmer Vernooij
Add get_username() call to the UIFactory.
185
    def test_text_ui_getusername(self):
4597.3.36 by Vincent Ladeuil
Fix imports and various cleanups in test_ui.
186
        factory = _mod_ui_text.TextUIFactory(None, None, None)
187
        factory.stdin = tests.StringIOWrapper("someuser\n\n")
188
        factory.stdout = tests.StringIOWrapper()
189
        factory.stderr = tests.StringIOWrapper()
4222.2.6 by Jelmer Vernooij
Remove use of NotATerminal.
190
        factory.stdout.encoding = "utf8"
4222.2.1 by Jelmer Vernooij
Add get_username() call to the UIFactory.
191
        # there is no output from the base factory
4368.3.1 by Vincent Ladeuil
Use stderr for UI prompt to address bug #376582.
192
        self.assertEqual("someuser",
193
                         factory.get_username('Hello %(host)s', host='some'))
194
        self.assertEquals("Hello some: ", factory.stderr.getvalue())
195
        self.assertEquals('', factory.stdout.getvalue())
4222.2.1 by Jelmer Vernooij
Add get_username() call to the UIFactory.
196
        self.assertEqual("", factory.get_username("Gebruiker"))
197
        # stdin should be empty
198
        self.assertEqual('', factory.stdin.readline())
199
4222.2.2 by Jelmer Vernooij
Review from vila: Deal with UTF8 strings in prompts, fix typo.
200
    def test_text_ui_getusername_utf8(self):
4488.1.1 by Vincent Ladeuil
(vila) Cleanup imports in some test files
201
        ui = tests.TestUIFactory(stdin=u'someuser\u1234'.encode('utf8'),
202
                                 stdout=tests.StringIOWrapper(),
203
                                 stderr=tests.StringIOWrapper())
4368.3.1 by Vincent Ladeuil
Use stderr for UI prompt to address bug #376582.
204
        ui.stderr.encoding = ui.stdout.encoding = ui.stdin.encoding = "utf8"
4222.2.3 by Jelmer Vernooij
Also check for unicode usernames.
205
        pb = ui.nested_progress_bar()
206
        try:
207
            # there is no output from the base factory
4368.3.1 by Vincent Ladeuil
Use stderr for UI prompt to address bug #376582.
208
            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.
209
                ui.get_username, u'Hello\u1234 %(host)s', host=u'some\u1234')
4222.2.8 by Jelmer Vernooij
Fix copy-n-paste error.
210
            self.assertEquals(u"someuser\u1234", username.decode('utf8'))
4368.3.1 by Vincent Ladeuil
Use stderr for UI prompt to address bug #376582.
211
            self.assertEquals(u"Hello\u1234 some\u1234: ",
212
                              ui.stderr.getvalue().decode("utf8"))
213
            self.assertEquals('', ui.stdout.getvalue())
4222.2.3 by Jelmer Vernooij
Also check for unicode usernames.
214
        finally:
215
            pb.finished()
4222.2.2 by Jelmer Vernooij
Review from vila: Deal with UTF8 strings in prompts, fix typo.
216
4961.1.3 by Martin Pool
trace quietness now controls whether the progress bar appears
217
    def test_quietness(self):
218
        os.environ['BZR_PROGRESS_BAR'] = 'text'
219
        ui_factory = _mod_ui_text.TextUIFactory(None,
220
            test_progress._TTYStringIO(),
221
            test_progress._TTYStringIO())
222
        self.assertIsInstance(ui_factory._progress_view,
223
            _mod_ui_text.TextProgressView)
224
        ui_factory.be_quiet(True)
225
        self.assertIsInstance(ui_factory._progress_view,
226
            _mod_ui_text.NullProgressView)
227
4634.144.10 by Martin Pool
Update test_ui for warning suppression
228
    def test_text_ui_show_user_warning(self):
4634.144.5 by Martin Pool
Cleaner presentation and tests for warn_cross_format_fetch
229
        from bzrlib.repofmt.groupcompress_repo import RepositoryFormat2a
230
        from bzrlib.repofmt.pack_repo import RepositoryFormatKnitPack5
231
        err = StringIO()
232
        out = StringIO()
233
        ui = tests.TextUIFactory(stdin=None, stdout=out, stderr=err)
234
        remote_fmt = remote.RemoteRepositoryFormat()
235
        remote_fmt._network_name = RepositoryFormatKnitPack5().network_name()
4634.144.10 by Martin Pool
Update test_ui for warning suppression
236
        ui.show_user_warning('cross_format_fetch', from_format=RepositoryFormat2a(),
237
            to_format=remote_fmt)
4634.144.5 by Martin Pool
Cleaner presentation and tests for warn_cross_format_fetch
238
        self.assertEquals('', out.getvalue())
239
        self.assertEquals("Doing on-the-fly conversion from RepositoryFormat2a() to "
240
            "RemoteRepositoryFormat(_network_name='Bazaar RepositoryFormatKnitPack5 "
241
            "(bzr 1.6)\\n').\nThis may take some time. Upgrade the repositories to "
242
            "the same format for better performance.\n",
243
            err.getvalue())
4634.144.10 by Martin Pool
Update test_ui for warning suppression
244
        # and now with it suppressed please
245
        err = StringIO()
246
        out = StringIO()
247
        ui = tests.TextUIFactory(stdin=None, stdout=out, stderr=err)
4634.144.11 by Martin Pool
Rename squelched to suppressed
248
        ui.suppressed_warnings.add('cross_format_fetch')
4634.144.10 by Martin Pool
Update test_ui for warning suppression
249
        ui.show_user_warning('cross_format_fetch', from_format=RepositoryFormat2a(),
250
            to_format=remote_fmt)
251
        self.assertEquals('', out.getvalue())
252
        self.assertEquals('', err.getvalue())
4634.144.5 by Martin Pool
Cleaner presentation and tests for warn_cross_format_fetch
253
4110.2.15 by Martin Pool
Fix bug in showing task progress and add a test
254
4884.1.1 by Vincent Ladeuil
Cleanup some test imports
255
class TestTextUIOutputStream(tests.TestCase):
4792.8.1 by Martin Pool
Add TextUIOutputStream coordinated with progress view
256
    """Tests for output stream that synchronizes with progress bar."""
257
258
    def test_output_clears_terminal(self):
4884.1.1 by Vincent Ladeuil
Cleanup some test imports
259
        stdout = tests.StringIOWrapper()
260
        stderr = tests.StringIOWrapper()
4792.8.1 by Martin Pool
Add TextUIOutputStream coordinated with progress view
261
        clear_calls = []
262
4884.1.1 by Vincent Ladeuil
Cleanup some test imports
263
        uif =  _mod_ui_text.TextUIFactory(None, stdout, stderr)
4792.8.1 by Martin Pool
Add TextUIOutputStream coordinated with progress view
264
        uif.clear_term = lambda: clear_calls.append('clear')
265
4884.1.1 by Vincent Ladeuil
Cleanup some test imports
266
        stream = _mod_ui_text.TextUIOutputStream(uif, uif.stdout)
4792.8.1 by Martin Pool
Add TextUIOutputStream coordinated with progress view
267
        stream.write("Hello world!\n")
268
        stream.write("there's more...\n")
4792.8.3 by Martin Pool
Add TextUIOutputStream.writelines
269
        stream.writelines(["1\n", "2\n", "3\n"])
4884.1.1 by Vincent Ladeuil
Cleanup some test imports
270
4792.8.1 by Martin Pool
Add TextUIOutputStream coordinated with progress view
271
        self.assertEqual(stdout.getvalue(),
272
            "Hello world!\n"
4792.8.3 by Martin Pool
Add TextUIOutputStream.writelines
273
            "there's more...\n"
274
            "1\n2\n3\n")
275
        self.assertEqual(['clear', 'clear', 'clear'],
4792.8.1 by Martin Pool
Add TextUIOutputStream coordinated with progress view
276
            clear_calls)
277
4792.8.8 by Martin Pool
Add TextUIOutputStream.flush
278
        stream.flush()
4792.8.1 by Martin Pool
Add TextUIOutputStream coordinated with progress view
279
280
4711.1.6 by Martin Pool
Separate TextUIFactory tests
281
class UITests(tests.TestCase):
282
283
    def test_progress_construction(self):
284
        """TextUIFactory constructs the right progress view.
285
        """
4597.3.36 by Vincent Ladeuil
Fix imports and various cleanups in test_ui.
286
        TTYStringIO = test_progress._TTYStringIO
287
        FileStringIO = tests.StringIOWrapper
4711.1.6 by Martin Pool
Separate TextUIFactory tests
288
        for (file_class, term, pb, expected_pb_class) in (
289
            # on an xterm, either use them or not as the user requests,
290
            # otherwise default on
4597.3.36 by Vincent Ladeuil
Fix imports and various cleanups in test_ui.
291
            (TTYStringIO, 'xterm', 'none', _mod_ui_text.NullProgressView),
292
            (TTYStringIO, 'xterm', 'text', _mod_ui_text.TextProgressView),
293
            (TTYStringIO, 'xterm', None, _mod_ui_text.TextProgressView),
4711.1.6 by Martin Pool
Separate TextUIFactory tests
294
            # on a dumb terminal, again if there's explicit configuration do
295
            # it, otherwise default off
4597.3.36 by Vincent Ladeuil
Fix imports and various cleanups in test_ui.
296
            (TTYStringIO, 'dumb', 'none', _mod_ui_text.NullProgressView),
297
            (TTYStringIO, 'dumb', 'text', _mod_ui_text.TextProgressView),
298
            (TTYStringIO, 'dumb', None, _mod_ui_text.NullProgressView),
4711.1.6 by Martin Pool
Separate TextUIFactory tests
299
            # on a non-tty terminal, it's null regardless of $TERM
4597.3.36 by Vincent Ladeuil
Fix imports and various cleanups in test_ui.
300
            (FileStringIO, 'xterm', None, _mod_ui_text.NullProgressView),
301
            (FileStringIO, 'dumb', None, _mod_ui_text.NullProgressView),
4711.1.6 by Martin Pool
Separate TextUIFactory tests
302
            # however, it can still be forced on
4597.3.36 by Vincent Ladeuil
Fix imports and various cleanups in test_ui.
303
            (FileStringIO, 'dumb', 'text', _mod_ui_text.TextProgressView),
4711.1.6 by Martin Pool
Separate TextUIFactory tests
304
            ):
305
            os.environ['TERM'] = term
306
            if pb is None:
307
                if 'BZR_PROGRESS_BAR' in os.environ:
308
                    del os.environ['BZR_PROGRESS_BAR']
309
            else:
310
                os.environ['BZR_PROGRESS_BAR'] = pb
311
            stdin = file_class('')
312
            stderr = file_class()
313
            stdout = file_class()
4597.3.36 by Vincent Ladeuil
Fix imports and various cleanups in test_ui.
314
            uif = _mod_ui.make_ui_for_terminal(stdin, stdout, stderr)
315
            self.assertIsInstance(uif, _mod_ui_text.TextUIFactory,
4711.1.6 by Martin Pool
Separate TextUIFactory tests
316
                "TERM=%s BZR_PROGRESS_BAR=%s uif=%r" % (term, pb, uif,))
317
            self.assertIsInstance(uif.make_progress_view(),
318
                expected_pb_class,
319
                "TERM=%s BZR_PROGRESS_BAR=%s uif=%r" % (term, pb, uif,))
320
321
    def test_text_ui_non_terminal(self):
322
        """Even on non-ttys, make_ui_for_terminal gives a text ui."""
4597.3.36 by Vincent Ladeuil
Fix imports and various cleanups in test_ui.
323
        stdin = test_progress._NonTTYStringIO('')
324
        stderr = test_progress._NonTTYStringIO()
325
        stdout = test_progress._NonTTYStringIO()
4711.1.6 by Martin Pool
Separate TextUIFactory tests
326
        for term_type in ['dumb', None, 'xterm']:
327
            if term_type is None:
328
                del os.environ['TERM']
329
            else:
330
                os.environ['TERM'] = term_type
4597.3.36 by Vincent Ladeuil
Fix imports and various cleanups in test_ui.
331
            uif = _mod_ui.make_ui_for_terminal(stdin, stdout, stderr)
332
            self.assertIsInstance(uif, _mod_ui_text.TextUIFactory,
4711.1.6 by Martin Pool
Separate TextUIFactory tests
333
                'TERM=%r' % (term_type,))
334
335
4597.3.36 by Vincent Ladeuil
Fix imports and various cleanups in test_ui.
336
class SilentUITests(tests.TestCase):
4449.3.19 by Martin Pool
SilentUIFactory now always errors when asked for input
337
4449.3.36 by Martin Pool
Update tests: SilentUIFactory no longer does get_boolean or get_password
338
    def test_silent_factory_get_password(self):
339
        # A silent factory that can't do user interaction can't get a
340
        # password.  Possibly it should raise a more specific error but it
341
        # can't succeed.
4597.3.36 by Vincent Ladeuil
Fix imports and various cleanups in test_ui.
342
        ui = _mod_ui.SilentUIFactory()
343
        stdout = tests.StringIOWrapper()
4449.3.19 by Martin Pool
SilentUIFactory now always errors when asked for input
344
        self.assertRaises(
345
            NotImplementedError,
346
            self.apply_redirected,
347
            None, stdout, stdout, ui.get_password)
348
        # and it didn't write anything out either
349
        self.assertEqual('', stdout.getvalue())
350
351
    def test_silent_ui_getbool(self):
4597.3.36 by Vincent Ladeuil
Fix imports and various cleanups in test_ui.
352
        factory = _mod_ui.SilentUIFactory()
353
        stdout = tests.StringIOWrapper()
4449.3.19 by Martin Pool
SilentUIFactory now always errors when asked for input
354
        self.assertRaises(
355
            NotImplementedError,
356
            self.apply_redirected,
357
            None, stdout, stdout, factory.get_boolean, "foo")
4449.3.42 by Martin Pool
Add basic test for CannedInputUIFactory
358
359
4597.3.36 by Vincent Ladeuil
Fix imports and various cleanups in test_ui.
360
class TestUIFactoryTests(tests.TestCase):
4580.2.2 by Martin Pool
Add test for bug 408201
361
362
    def test_test_ui_factory_progress(self):
363
        # there's no output; we just want to make sure this doesn't crash -
5243.1.2 by Martin
Point launchpad links in comments at production server rather than edge
364
        # see https://bugs.launchpad.net/bzr/+bug/408201
4597.3.36 by Vincent Ladeuil
Fix imports and various cleanups in test_ui.
365
        ui = tests.TestUIFactory()
4580.2.2 by Martin Pool
Add test for bug 408201
366
        pb = ui.nested_progress_bar()
367
        pb.update('hello')
368
        pb.tick()
369
        pb.finished()
370
371
4597.3.36 by Vincent Ladeuil
Fix imports and various cleanups in test_ui.
372
class CannedInputUIFactoryTests(tests.TestCase):
373
4449.3.42 by Martin Pool
Add basic test for CannedInputUIFactory
374
    def test_canned_input_get_input(self):
4597.3.37 by Vincent Ladeuil
Allows ui factories to query users for an integer.
375
        uif = _mod_ui.CannedInputUIFactory([True, 'mbp', 'password', 42])
376
        self.assertEqual(True, uif.get_boolean('Extra cheese?'))
377
        self.assertEqual('mbp', uif.get_username('Enter your user name'))
378
        self.assertEqual('password',
379
                         uif.get_password('Password for %(host)s',
380
                                          host='example.com'))
4886.1.1 by Vincent Ladeuil
Allows ui factories to query users for an integer.
381
        self.assertEqual(42, uif.get_integer('And all that jazz ?'))
4110.2.17 by Martin Pool
If one ProgressTask has no count, it passes through that of its child
382
4503.2.1 by Vincent Ladeuil
Get a bool from a string.
383
384
class TestBoolFromString(tests.TestCase):
385
386
    def assertIsTrue(self, s, accepted_values=None):
387
        res = _mod_ui.bool_from_string(s, accepted_values=accepted_values)
388
        self.assertEquals(True, res)
389
390
    def assertIsFalse(self, s, accepted_values=None):
391
        res = _mod_ui.bool_from_string(s, accepted_values=accepted_values)
392
        self.assertEquals(False, res)
393
394
    def assertIsNone(self, s, accepted_values=None):
395
        res = _mod_ui.bool_from_string(s, accepted_values=accepted_values)
396
        self.assertIs(None, res)
397
398
    def test_know_valid_values(self):
399
        self.assertIsTrue('true')
400
        self.assertIsFalse('false')
401
        self.assertIsTrue('1')
402
        self.assertIsFalse('0')
403
        self.assertIsTrue('on')
404
        self.assertIsFalse('off')
405
        self.assertIsTrue('yes')
406
        self.assertIsFalse('no')
407
        self.assertIsTrue('y')
408
        self.assertIsFalse('n')
409
        # Also try some case variations
410
        self.assertIsTrue('True')
411
        self.assertIsFalse('False')
412
        self.assertIsTrue('On')
413
        self.assertIsFalse('Off')
414
        self.assertIsTrue('ON')
415
        self.assertIsFalse('OFF')
416
        self.assertIsTrue('oN')
417
        self.assertIsFalse('oFf')
418
419
    def test_invalid_values(self):
420
        self.assertIsNone(None)
421
        self.assertIsNone('doubt')
422
        self.assertIsNone('frue')
423
        self.assertIsNone('talse')
424
        self.assertIsNone('42')
425
426
    def test_provided_values(self):
427
        av = dict(y=True, n=False, yes=True, no=False)
428
        self.assertIsTrue('y', av)
429
        self.assertIsTrue('Y', av)
430
        self.assertIsTrue('Yes', av)
431
        self.assertIsFalse('n', av)
432
        self.assertIsFalse('N', av)
433
        self.assertIsFalse('No', av)
434
        self.assertIsNone('1', av)
435
        self.assertIsNone('0', av)
436
        self.assertIsNone('on', av)
437
        self.assertIsNone('off', av)
5422.1.1 by Martin Pool
Move CapturingUIFactory out of per_workingtree tests into somewhere reusable
438
439
5422.1.4 by Martin Pool
Rename CapturingUIFactory to ProgressRecordingUIFactory
440
class TestProgressRecordingUI(tests.TestCase):
5422.1.1 by Martin Pool
Move CapturingUIFactory out of per_workingtree tests into somewhere reusable
441
    """Test test-oriented UIFactory that records progress updates"""
442
443
    def test_nested_ignore_depth_beyond_one(self):
444
        # we only want to capture the first level out progress, not
445
        # want sub-components might do. So we have nested bars ignored.
5422.1.4 by Martin Pool
Rename CapturingUIFactory to ProgressRecordingUIFactory
446
        factory = ProgressRecordingUIFactory()
5422.1.1 by Martin Pool
Move CapturingUIFactory out of per_workingtree tests into somewhere reusable
447
        pb1 = factory.nested_progress_bar()
448
        pb1.update('foo', 0, 1)
449
        pb2 = factory.nested_progress_bar()
450
        pb2.update('foo', 0, 1)
451
        pb2.finished()
452
        pb1.finished()
453
        self.assertEqual([("update", 0, 1, 'foo')], factory._calls)