/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
4488.1.1 by Vincent Ladeuil
(vila) Cleanup imports in some test files
26
from bzrlib import (
27
    errors,
28
    tests,
29
    ui as _mod_ui,
4449.3.4 by Martin Pool
ProgressTask now talks to ProgressView; easier to test
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 (
4449.3.42 by Martin Pool
Add basic test for CannedInputUIFactory
44
    CannedInputUIFactory,
2363.4.10 by Vincent Ladeuil
Complete tests.
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,
4792.8.1 by Martin Pool
Add TextUIOutputStream coordinated with progress view
53
    TextUIOutputStream,
4110.2.15 by Martin Pool
Fix bug in showing task progress and add a test
54
    )
1185.49.22 by John Arbash Meinel
Added get_password to the UIFactory, using it inside of sftp.py
55
1681.1.2 by Robert Collins
* bzrlib.ui.text.TextUIFactory now accepts a bar_type parameter which
56
4711.1.6 by Martin Pool
Separate TextUIFactory tests
57
class TestTextUIFactory(tests.TestCase):
2294.4.1 by Vincent Ladeuil
Add a UIFactory.get_login method, fix tests.
58
59
    def test_text_factory_ascii_password(self):
4488.1.1 by Vincent Ladeuil
(vila) Cleanup imports in some test files
60
        ui = tests.TestUIFactory(stdin='secret\n',
61
                                 stdout=tests.StringIOWrapper(),
62
                                 stderr=tests.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,
4368.3.1 by Vincent Ladeuil
Use stderr for UI prompt to address bug #376582.
67
                                                   ui.stderr,
2294.4.1 by Vincent Ladeuil
Add a UIFactory.get_login method, fix tests.
68
                                                   ui.get_password))
69
            # ': ' is appended to prompt
4368.3.1 by Vincent Ladeuil
Use stderr for UI prompt to address bug #376582.
70
            self.assertEqual(': ', ui.stderr.getvalue())
71
            self.assertEqual('', ui.stdout.readline())
2363.4.3 by Vincent Ladeuil
Tidy-up tests.
72
            # stdin should be empty
2363.4.6 by Vincent Ladeuil
Fix tests around stdin emptyness.
73
            self.assertEqual('', ui.stdin.readline())
2294.4.1 by Vincent Ladeuil
Add a UIFactory.get_login method, fix tests.
74
        finally:
75
            pb.finished()
76
77
    def test_text_factory_utf8_password(self):
78
        """Test an utf8 password.
79
80
        We can't predict what encoding users will have for stdin, so we force
81
        it to utf8 to test that we transport the password correctly.
82
        """
4488.1.1 by Vincent Ladeuil
(vila) Cleanup imports in some test files
83
        ui = tests.TestUIFactory(stdin=u'baz\u1234'.encode('utf8'),
84
                                 stdout=tests.StringIOWrapper(),
85
                                 stderr=tests.StringIOWrapper())
4368.3.1 by Vincent Ladeuil
Use stderr for UI prompt to address bug #376582.
86
        ui.stderr.encoding = ui.stdout.encoding = ui.stdin.encoding = 'utf8'
2294.4.1 by Vincent Ladeuil
Add a UIFactory.get_login method, fix tests.
87
        pb = ui.nested_progress_bar()
88
        try:
4368.3.1 by Vincent Ladeuil
Use stderr for UI prompt to address bug #376582.
89
            password = self.apply_redirected(ui.stdin, ui.stdout, ui.stderr,
2294.4.1 by Vincent Ladeuil
Add a UIFactory.get_login method, fix tests.
90
                                             ui.get_password,
91
                                             u'Hello \u1234 %(user)s',
92
                                             user=u'some\u1234')
93
            # We use StringIO objects, we need to decode them
94
            self.assertEqual(u'baz\u1234', password.decode('utf8'))
95
            self.assertEqual(u'Hello \u1234 some\u1234: ',
4368.3.1 by Vincent Ladeuil
Use stderr for UI prompt to address bug #376582.
96
                             ui.stderr.getvalue().decode('utf8'))
97
            # stdin and stdout should be empty
2363.4.6 by Vincent Ladeuil
Fix tests around stdin emptyness.
98
            self.assertEqual('', ui.stdin.readline())
4368.3.1 by Vincent Ladeuil
Use stderr for UI prompt to address bug #376582.
99
            self.assertEqual('', ui.stdout.readline())
2294.4.1 by Vincent Ladeuil
Add a UIFactory.get_login method, fix tests.
100
        finally:
101
            pb.finished()
1534.5.6 by Robert Collins
split out converter logic into per-format objects.
102
103
    def test_progress_note(self):
104
        stderr = StringIO()
105
        stdout = StringIO()
3882.8.11 by Martin Pool
Choose the UIFactory class depending on the terminal capabilities
106
        ui_factory = TextUIFactory(stdin=StringIO(''),
107
            stderr=stderr,
108
            stdout=stdout)
1558.8.5 by Aaron Bentley
Pass note up the stack instead of using bzrlib.ui_factory
109
        pb = ui_factory.nested_progress_bar()
1558.8.4 by Aaron Bentley
Fixed test case for pb.note
110
        try:
4712.1.3 by Martin Pool
Update deprecation version
111
            result = self.applyDeprecated(deprecated_in((2, 1, 0)),
4471.2.2 by Martin Pool
Deprecate ProgressTask.note
112
                pb.note,
113
                't')
1558.8.4 by Aaron Bentley
Fixed test case for pb.note
114
            self.assertEqual(None, result)
115
            self.assertEqual("t\n", stdout.getvalue())
1843.3.2 by John Arbash Meinel
Fix a ui test that depended on clearing
116
            # Since there was no update() call, there should be no clear() call
2363.4.4 by Vincent Ladeuil
More tidying-up.
117
            self.failIf(re.search(r'^\r {10,}\r$',
118
                                  stderr.getvalue()) is not None,
1843.3.2 by John Arbash Meinel
Fix a ui test that depended on clearing
119
                        'We cleared the stderr without anything to put there')
120
        finally:
121
            pb.finished()
122
123
    def test_progress_note_clears(self):
4449.3.4 by Martin Pool
ProgressTask now talks to ProgressView; easier to test
124
        stderr = _TTYStringIO()
125
        stdout = _TTYStringIO()
126
        # so that we get a TextProgressBar
127
        os.environ['TERM'] = 'xterm'
3882.8.8 by Martin Pool
Progress and UI test cleanups
128
        ui_factory = TextUIFactory(
3882.8.11 by Martin Pool
Choose the UIFactory class depending on the terminal capabilities
129
            stdin=StringIO(''),
3882.8.4 by Martin Pool
All UI factories should support note()
130
            stdout=stdout, stderr=stderr)
4449.3.4 by Martin Pool
ProgressTask now talks to ProgressView; easier to test
131
        self.assertIsInstance(ui_factory._progress_view,
132
            TextProgressView)
1843.3.2 by John Arbash Meinel
Fix a ui test that depended on clearing
133
        pb = ui_factory.nested_progress_bar()
134
        try:
135
            # Create a progress update that isn't throttled
136
            pb.update('x', 1, 1)
4712.1.4 by Martin Pool
One more deprecation
137
            result = self.applyDeprecated(deprecated_in((2, 1, 0)),
4471.2.2 by Martin Pool
Deprecate ProgressTask.note
138
                pb.note, 't')
1843.3.2 by John Arbash Meinel
Fix a ui test that depended on clearing
139
            self.assertEqual(None, result)
140
            self.assertEqual("t\n", stdout.getvalue())
1558.8.4 by Aaron Bentley
Fixed test case for pb.note
141
            # the exact contents will depend on the terminal width and we don't
142
            # care about that right now - but you're probably running it on at
143
            # least a 10-character wide terminal :)
1843.3.2 by John Arbash Meinel
Fix a ui test that depended on clearing
144
            self.assertContainsRe(stderr.getvalue(), r'\r {10,}\r$')
1558.8.4 by Aaron Bentley
Fixed test case for pb.note
145
        finally:
1558.8.5 by Aaron Bentley
Pass note up the stack instead of using bzrlib.ui_factory
146
            pb.finished()
1594.1.1 by Robert Collins
Introduce new bzr progress bar api. ui_factory.nested_progress_bar.
147
148
    def test_progress_nested(self):
149
        # test factory based nested and popping.
3882.8.11 by Martin Pool
Choose the UIFactory class depending on the terminal capabilities
150
        ui = TextUIFactory(None, None, None)
1594.1.1 by Robert Collins
Introduce new bzr progress bar api. ui_factory.nested_progress_bar.
151
        pb1 = ui.nested_progress_bar()
152
        pb2 = ui.nested_progress_bar()
3948.2.2 by Martin Pool
Corrections to finishing progress bars
153
        # You do get a warning if the outermost progress bar wasn't finished
154
        # first - it's not clear if this is really useful or if it should just
155
        # 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
156
        warnings, _ = self.callCatchWarnings(pb1.finished)
3948.2.2 by Martin Pool
Corrections to finishing progress bars
157
        if len(warnings) != 1:
158
            self.fail("unexpected warnings: %r" % (warnings,))
1594.1.1 by Robert Collins
Introduce new bzr progress bar api. ui_factory.nested_progress_bar.
159
        pb2.finished()
160
        pb1.finished()
161
4449.3.38 by Martin Pool
Cleanup get_boolean tests
162
    def test_text_ui_get_boolean(self):
4449.3.44 by Martin Pool
merge trunk
163
        stdin = StringIO("y\n" # True
164
                         "n\n" # False
165
                         "yes with garbage\nY\n" # True
166
                         "not an answer\nno\n" # False
167
                         "I'm sure!\nyes\n" # True
168
                         "NO\n" # False
169
                         "foo\n")
4449.3.38 by Martin Pool
Cleanup get_boolean tests
170
        stdout = StringIO()
171
        stderr = StringIO()
172
        factory = TextUIFactory(stdin, stdout, stderr)
1687.1.4 by Robert Collins
Add bzrlib.ui.ui_factory.get_boolean().
173
        self.assertEqual(True, factory.get_boolean(""))
4503.2.5 by Vincent Ladeuil
ui.get_boolean can also use bool_from_string.
174
        self.assertEqual(False, factory.get_boolean(""))
175
        self.assertEqual(True, factory.get_boolean(""))
176
        self.assertEqual(False, factory.get_boolean(""))
177
        self.assertEqual(True, factory.get_boolean(""))
178
        self.assertEqual(False, factory.get_boolean(""))
1687.1.4 by Robert Collins
Add bzrlib.ui.ui_factory.get_boolean().
179
        self.assertEqual("foo\n", factory.stdin.read())
2363.4.4 by Vincent Ladeuil
More tidying-up.
180
        # stdin should be empty
2363.4.6 by Vincent Ladeuil
Fix tests around stdin emptyness.
181
        self.assertEqual('', factory.stdin.readline())
1687.1.4 by Robert Collins
Add bzrlib.ui.ui_factory.get_boolean().
182
4300.3.1 by Martin Pool
Fix string expansion in TextUIFactory.prompt
183
    def test_text_factory_prompt(self):
184
        # see <https://launchpad.net/bugs/365891>
4449.3.18 by Martin Pool
Fuse CLIUIFactory and TextUIFactory and deprecate the old name
185
        factory = TextUIFactory(StringIO(), StringIO(), StringIO())
4300.3.1 by Martin Pool
Fix string expansion in TextUIFactory.prompt
186
        factory.prompt('foo %2e')
4368.3.1 by Vincent Ladeuil
Use stderr for UI prompt to address bug #376582.
187
        self.assertEqual('', factory.stdout.getvalue())
188
        self.assertEqual('foo %2e', factory.stderr.getvalue())
4300.3.1 by Martin Pool
Fix string expansion in TextUIFactory.prompt
189
1687.1.4 by Robert Collins
Add bzrlib.ui.ui_factory.get_boolean().
190
    def test_text_factory_prompts_and_clears(self):
191
        # a get_boolean call should clear the pb before prompting
3882.8.10 by Martin Pool
Fix up test_ui for new progress bars
192
        out = _TTYStringIO()
4449.3.4 by Martin Pool
ProgressTask now talks to ProgressView; easier to test
193
        os.environ['TERM'] = 'xterm'
3882.8.11 by Martin Pool
Choose the UIFactory class depending on the terminal capabilities
194
        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
195
        pb = factory.nested_progress_bar()
196
        pb.show_bar = False
197
        pb.show_spinner = False
198
        pb.show_count = False
199
        pb.update("foo", 0, 1)
2363.4.4 by Vincent Ladeuil
More tidying-up.
200
        self.assertEqual(True,
201
                         self.apply_redirected(None, factory.stdout,
202
                                               factory.stdout,
203
                                               factory.get_boolean,
204
                                               "what do you want"))
3882.8.10 by Martin Pool
Fix up test_ui for new progress bars
205
        output = out.getvalue()
206
        self.assertContainsRe(factory.stdout.getvalue(),
207
            "foo *\r\r  *\r*")
208
        self.assertContainsRe(factory.stdout.getvalue(),
209
            r"what do you want\? \[y/n\]: what do you want\? \[y/n\]: ")
210
        # stdin should have been totally consumed
2363.4.6 by Vincent Ladeuil
Fix tests around stdin emptyness.
211
        self.assertEqual('', factory.stdin.readline())
4017.1.1 by John Arbash Meinel
Get a pb.tick() to work after calling pb.update()
212
213
    def test_text_tick_after_update(self):
214
        ui_factory = TextUIFactory(stdout=StringIO(), stderr=StringIO())
215
        pb = ui_factory.nested_progress_bar()
216
        try:
217
            pb.update('task', 0, 3)
218
            # Reset the clock, so that it actually tries to repaint itself
219
            ui_factory._progress_view._last_repaint = time.time() - 1.0
220
            pb.tick()
221
        finally:
222
            pb.finished()
4110.2.15 by Martin Pool
Fix bug in showing task progress and add a test
223
4222.2.1 by Jelmer Vernooij
Add get_username() call to the UIFactory.
224
    def test_text_ui_getusername(self):
225
        factory = TextUIFactory(None, None, None)
226
        factory.stdin = StringIO("someuser\n\n")
227
        factory.stdout = StringIO()
4368.3.1 by Vincent Ladeuil
Use stderr for UI prompt to address bug #376582.
228
        factory.stderr = StringIO()
4222.2.6 by Jelmer Vernooij
Remove use of NotATerminal.
229
        factory.stdout.encoding = "utf8"
4222.2.1 by Jelmer Vernooij
Add get_username() call to the UIFactory.
230
        # there is no output from the base factory
4368.3.1 by Vincent Ladeuil
Use stderr for UI prompt to address bug #376582.
231
        self.assertEqual("someuser",
232
                         factory.get_username('Hello %(host)s', host='some'))
233
        self.assertEquals("Hello some: ", factory.stderr.getvalue())
234
        self.assertEquals('', factory.stdout.getvalue())
4222.2.1 by Jelmer Vernooij
Add get_username() call to the UIFactory.
235
        self.assertEqual("", factory.get_username("Gebruiker"))
236
        # stdin should be empty
237
        self.assertEqual('', factory.stdin.readline())
238
4222.2.2 by Jelmer Vernooij
Review from vila: Deal with UTF8 strings in prompts, fix typo.
239
    def test_text_ui_getusername_utf8(self):
4488.1.1 by Vincent Ladeuil
(vila) Cleanup imports in some test files
240
        ui = tests.TestUIFactory(stdin=u'someuser\u1234'.encode('utf8'),
241
                                 stdout=tests.StringIOWrapper(),
242
                                 stderr=tests.StringIOWrapper())
4368.3.1 by Vincent Ladeuil
Use stderr for UI prompt to address bug #376582.
243
        ui.stderr.encoding = ui.stdout.encoding = ui.stdin.encoding = "utf8"
4222.2.3 by Jelmer Vernooij
Also check for unicode usernames.
244
        pb = ui.nested_progress_bar()
245
        try:
246
            # there is no output from the base factory
4368.3.1 by Vincent Ladeuil
Use stderr for UI prompt to address bug #376582.
247
            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.
248
                ui.get_username, u'Hello\u1234 %(host)s', host=u'some\u1234')
4222.2.8 by Jelmer Vernooij
Fix copy-n-paste error.
249
            self.assertEquals(u"someuser\u1234", username.decode('utf8'))
4368.3.1 by Vincent Ladeuil
Use stderr for UI prompt to address bug #376582.
250
            self.assertEquals(u"Hello\u1234 some\u1234: ",
251
                              ui.stderr.getvalue().decode("utf8"))
252
            self.assertEquals('', ui.stdout.getvalue())
4222.2.3 by Jelmer Vernooij
Also check for unicode usernames.
253
        finally:
254
            pb.finished()
4222.2.2 by Jelmer Vernooij
Review from vila: Deal with UTF8 strings in prompts, fix typo.
255
4110.2.15 by Martin Pool
Fix bug in showing task progress and add a test
256
4792.8.1 by Martin Pool
Add TextUIOutputStream coordinated with progress view
257
class TestTextUIOutputStream(TestCase):
258
    """Tests for output stream that synchronizes with progress bar."""
259
260
    def test_output_clears_terminal(self):
261
        stdout = StringIO()
262
        stderr = StringIO()
263
        clear_calls = []
264
265
        uif = TextUIFactory(None, stdout, stderr)
266
        uif.clear_term = lambda: clear_calls.append('clear')
267
268
        stream = TextUIOutputStream(uif, uif.stdout)
269
        stream.write("Hello world!\n")
270
        stream.write("there's more...\n")
4792.8.3 by Martin Pool
Add TextUIOutputStream.writelines
271
        stream.writelines(["1\n", "2\n", "3\n"])
4792.8.1 by Martin Pool
Add TextUIOutputStream coordinated with progress view
272
        
273
        self.assertEqual(stdout.getvalue(),
274
            "Hello world!\n"
4792.8.3 by Martin Pool
Add TextUIOutputStream.writelines
275
            "there's more...\n"
276
            "1\n2\n3\n")
277
        self.assertEqual(['clear', 'clear', 'clear'],
4792.8.1 by Martin Pool
Add TextUIOutputStream coordinated with progress view
278
            clear_calls)
279
4792.8.8 by Martin Pool
Add TextUIOutputStream.flush
280
        stream.flush()
4792.8.1 by Martin Pool
Add TextUIOutputStream coordinated with progress view
281
282
4711.1.6 by Martin Pool
Separate TextUIFactory tests
283
class UITests(tests.TestCase):
284
285
    def test_progress_construction(self):
286
        """TextUIFactory constructs the right progress view.
287
        """
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
291
            (_TTYStringIO, 'xterm', 'none', NullProgressView),
292
            (_TTYStringIO, 'xterm', 'text', TextProgressView),
293
            (_TTYStringIO, 'xterm', None, TextProgressView),
294
            # on a dumb terminal, again if there's explicit configuration do
295
            # it, otherwise default off
296
            (_TTYStringIO, 'dumb', 'none', NullProgressView),
297
            (_TTYStringIO, 'dumb', 'text', TextProgressView),
298
            (_TTYStringIO, 'dumb', None, NullProgressView),
299
            # on a non-tty terminal, it's null regardless of $TERM
300
            (StringIO, 'xterm', None, NullProgressView),
301
            (StringIO, 'dumb', None, NullProgressView),
302
            # however, it can still be forced on
303
            (StringIO, 'dumb', 'text', TextProgressView),
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()
314
            uif = make_ui_for_terminal(stdin, stdout, stderr)
315
            self.assertIsInstance(uif, TextUIFactory,
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."""
323
        stdin = _NonTTYStringIO('')
324
        stderr = _NonTTYStringIO()
325
        stdout = _NonTTYStringIO()
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
331
            uif = make_ui_for_terminal(stdin, stdout, stderr)
332
            self.assertIsInstance(uif, TextUIFactory,
333
                'TERM=%r' % (term_type,))
334
335
4449.3.19 by Martin Pool
SilentUIFactory now always errors when asked for input
336
class SilentUITests(TestCase):
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.
4449.3.19 by Martin Pool
SilentUIFactory now always errors when asked for input
342
        ui = SilentUIFactory()
343
        stdout = StringIO()
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):
352
        factory = SilentUIFactory()
353
        stdout = StringIO()
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
4580.2.2 by Martin Pool
Add test for bug 408201
360
class TestUIFactoryTests(TestCase):
361
362
    def test_test_ui_factory_progress(self):
363
        # there's no output; we just want to make sure this doesn't crash -
364
        # see https://bugs.edge.launchpad.net/bzr/+bug/408201
365
        ui = TestUIFactory()
366
        pb = ui.nested_progress_bar()
367
        pb.update('hello')
368
        pb.tick()
369
        pb.finished()
370
371
4449.3.42 by Martin Pool
Add basic test for CannedInputUIFactory
372
class CannedInputUIFactoryTests(TestCase):
373
    
374
    def test_canned_input_get_input(self):
375
        uif = CannedInputUIFactory([True, 'mbp', 'password'])
376
        self.assertEqual(uif.get_boolean('Extra cheese?'), True)
377
        self.assertEqual(uif.get_username('Enter your user name'), 'mbp')
378
        self.assertEqual(uif.get_password('Password for %(host)s', host='example.com'),
379
            'password')
4110.2.17 by Martin Pool
If one ProgressTask has no count, it passes through that of its child
380
4503.2.1 by Vincent Ladeuil
Get a bool from a string.
381
382
class TestBoolFromString(tests.TestCase):
383
384
    def assertIsTrue(self, s, accepted_values=None):
385
        res = _mod_ui.bool_from_string(s, accepted_values=accepted_values)
386
        self.assertEquals(True, res)
387
388
    def assertIsFalse(self, s, accepted_values=None):
389
        res = _mod_ui.bool_from_string(s, accepted_values=accepted_values)
390
        self.assertEquals(False, res)
391
392
    def assertIsNone(self, s, accepted_values=None):
393
        res = _mod_ui.bool_from_string(s, accepted_values=accepted_values)
394
        self.assertIs(None, res)
395
396
    def test_know_valid_values(self):
397
        self.assertIsTrue('true')
398
        self.assertIsFalse('false')
399
        self.assertIsTrue('1')
400
        self.assertIsFalse('0')
401
        self.assertIsTrue('on')
402
        self.assertIsFalse('off')
403
        self.assertIsTrue('yes')
404
        self.assertIsFalse('no')
405
        self.assertIsTrue('y')
406
        self.assertIsFalse('n')
407
        # Also try some case variations
408
        self.assertIsTrue('True')
409
        self.assertIsFalse('False')
410
        self.assertIsTrue('On')
411
        self.assertIsFalse('Off')
412
        self.assertIsTrue('ON')
413
        self.assertIsFalse('OFF')
414
        self.assertIsTrue('oN')
415
        self.assertIsFalse('oFf')
416
417
    def test_invalid_values(self):
418
        self.assertIsNone(None)
419
        self.assertIsNone('doubt')
420
        self.assertIsNone('frue')
421
        self.assertIsNone('talse')
422
        self.assertIsNone('42')
423
424
    def test_provided_values(self):
425
        av = dict(y=True, n=False, yes=True, no=False)
426
        self.assertIsTrue('y', av)
427
        self.assertIsTrue('Y', av)
428
        self.assertIsTrue('Yes', av)
429
        self.assertIsFalse('n', av)
430
        self.assertIsFalse('N', av)
431
        self.assertIsFalse('No', av)
432
        self.assertIsNone('1', av)
433
        self.assertIsNone('0', av)
434
        self.assertIsNone('on', av)
435
        self.assertIsNone('off', av)