~bzr-pqm/bzr/bzr.dev

« back to all changes in this revision

Viewing changes to bzrlib/tests/test_ui.py

  • Committer: Patch Queue Manager
  • Date: 2016-04-21 04:10:52 UTC
  • mfrom: (6616.1.1 fix-en-user-guide)
  • Revision ID: pqm@pqm.ubuntu.com-20160421041052-clcye7ns1qcl2n7w
(richard-wilbur) Ensure build of English use guide always uses English text
 even when user's locale specifies a different language. (Jelmer Vernooij)

Show diffs side-by-side

added added

removed removed

Lines of Context:
1
 
# Copyright (C) 2005 Canonical Ltd
 
1
# Copyright (C) 2005-2012, 2016 Canonical Ltd
2
2
#
3
3
# This program is free software; you can redistribute it and/or modify
4
4
# it under the terms of the GNU General Public License as published by
12
12
#
13
13
# You should have received a copy of the GNU General Public License
14
14
# along with this program; if not, write to the Free Software
15
 
# Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307  USA
 
15
# Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
16
16
 
17
17
"""Tests for the bzrlib ui
18
18
"""
19
19
 
20
 
import os
 
20
import time
 
21
 
21
22
from StringIO import StringIO
22
 
import re
23
 
import sys
24
 
 
25
 
import bzrlib
26
 
import bzrlib.errors as errors
27
 
from bzrlib.progress import (
28
 
    DotsProgressBar,
29
 
    ProgressBarStack,
30
 
    TTYProgressBar,
 
23
 
 
24
from testtools.matchers import *
 
25
 
 
26
from bzrlib import (
 
27
    config,
 
28
    remote,
 
29
    tests,
 
30
    ui as _mod_ui,
31
31
    )
32
32
from bzrlib.tests import (
33
 
    TestCase,
34
 
    TestUIFactory,
35
 
    StringIOWrapper,
36
 
    )
37
 
from bzrlib.tests.test_progress import _TTYStringIO
38
 
from bzrlib.ui import (
39
 
    CLIUIFactory,
40
 
    SilentUIFactory,
41
 
    )
42
 
from bzrlib.ui.text import TextUIFactory
43
 
 
44
 
 
45
 
class UITests(TestCase):
46
 
 
47
 
    def test_silent_factory(self):
48
 
        ui = SilentUIFactory()
49
 
        stdout = StringIO()
50
 
        self.assertEqual(None,
51
 
                         self.apply_redirected(None, stdout, stdout,
52
 
                                               ui.get_password))
53
 
        self.assertEqual('', stdout.getvalue())
54
 
        self.assertEqual(None,
55
 
                         self.apply_redirected(None, stdout, stdout,
56
 
                                               ui.get_password,
57
 
                                               u'Hello\u1234 %(user)s',
58
 
                                               user=u'some\u1234'))
59
 
        self.assertEqual('', stdout.getvalue())
 
33
    fixtures,
 
34
    )
 
35
from bzrlib.ui import text as _mod_ui_text
 
36
from bzrlib.tests.testui import (
 
37
    ProgressRecordingUIFactory,
 
38
    )
 
39
 
 
40
 
 
41
class TTYStringIO(StringIO):
 
42
    """A helper class which makes a StringIO look like a terminal"""
 
43
 
 
44
    def isatty(self):
 
45
        return True
 
46
 
 
47
 
 
48
class NonTTYStringIO(StringIO):
 
49
    """Helper that implements isatty() but returns False"""
 
50
 
 
51
    def isatty(self):
 
52
        return False
 
53
 
 
54
 
 
55
class TestUIConfiguration(tests.TestCaseWithTransport):
 
56
 
 
57
    def test_output_encoding_configuration(self):
 
58
        enc = fixtures.generate_unicode_encodings().next()
 
59
        config.GlobalStack().set('output_encoding', enc)
 
60
        ui = tests.TestUIFactory(stdin=None,
 
61
            stdout=tests.StringIOWrapper(),
 
62
            stderr=tests.StringIOWrapper())
 
63
        output = ui.make_output_stream()
 
64
        self.assertEqual(output.encoding, enc)
 
65
 
 
66
 
 
67
class TestTextUIFactory(tests.TestCase):
 
68
 
 
69
    def make_test_ui_factory(self, stdin_contents):
 
70
        ui = tests.TestUIFactory(stdin=stdin_contents,
 
71
                                 stdout=tests.StringIOWrapper(),
 
72
                                 stderr=tests.StringIOWrapper())
 
73
        return ui
 
74
 
 
75
    def test_text_factory_confirm(self):
 
76
        # turns into reading a regular boolean
 
77
        ui = self.make_test_ui_factory('n\n')
 
78
        self.assertEqual(ui.confirm_action(u'Should %(thing)s pass?',
 
79
            'bzrlib.tests.test_ui.confirmation',
 
80
            {'thing': 'this'},),
 
81
            False)
60
82
 
61
83
    def test_text_factory_ascii_password(self):
62
 
        ui = TestUIFactory(stdin='secret\n', stdout=StringIOWrapper())
 
84
        ui = self.make_test_ui_factory('secret\n')
63
85
        pb = ui.nested_progress_bar()
64
86
        try:
65
87
            self.assertEqual('secret',
66
88
                             self.apply_redirected(ui.stdin, ui.stdout,
67
 
                                                   ui.stdout,
 
89
                                                   ui.stderr,
68
90
                                                   ui.get_password))
69
91
            # ': ' is appended to prompt
70
 
            self.assertEqual(': ', ui.stdout.getvalue())
 
92
            self.assertEqual(': ', ui.stderr.getvalue())
 
93
            self.assertEqual('', ui.stdout.readline())
71
94
            # stdin should be empty
72
95
            self.assertEqual('', ui.stdin.readline())
73
96
        finally:
74
97
            pb.finished()
75
98
 
76
99
    def test_text_factory_utf8_password(self):
77
 
        """Test an utf8 password.
78
 
 
79
 
        We can't predict what encoding users will have for stdin, so we force
80
 
        it to utf8 to test that we transport the password correctly.
81
 
        """
82
 
        ui = TestUIFactory(stdin=u'baz\u1234'.encode('utf8'),
83
 
                           stdout=StringIOWrapper())
84
 
        ui.stdin.encoding = 'utf8'
85
 
        ui.stdout.encoding = ui.stdin.encoding
86
 
        pb = ui.nested_progress_bar()
87
 
        try:
88
 
            password = self.apply_redirected(ui.stdin, ui.stdout, ui.stdout,
89
 
                                             ui.get_password,
90
 
                                             u'Hello \u1234 %(user)s',
91
 
                                             user=u'some\u1234')
92
 
            # We use StringIO objects, we need to decode them
93
 
            self.assertEqual(u'baz\u1234', password.decode('utf8'))
94
 
            self.assertEqual(u'Hello \u1234 some\u1234: ',
95
 
                             ui.stdout.getvalue().decode('utf8'))
96
 
            # stdin should be empty
97
 
            self.assertEqual('', ui.stdin.readline())
98
 
        finally:
99
 
            pb.finished()
100
 
 
101
 
    def test_progress_note(self):
102
 
        stderr = StringIO()
103
 
        stdout = StringIO()
104
 
        ui_factory = TextUIFactory(bar_type=TTYProgressBar)
105
 
        pb = ui_factory.nested_progress_bar()
106
 
        try:
107
 
            pb.to_messages_file = stdout
108
 
            ui_factory._progress_bar_stack.bottom().to_file = stderr
109
 
            result = pb.note('t')
110
 
            self.assertEqual(None, result)
111
 
            self.assertEqual("t\n", stdout.getvalue())
112
 
            # Since there was no update() call, there should be no clear() call
113
 
            self.failIf(re.search(r'^\r {10,}\r$',
114
 
                                  stderr.getvalue()) is not None,
115
 
                        'We cleared the stderr without anything to put there')
116
 
        finally:
117
 
            pb.finished()
118
 
 
119
 
    def test_progress_note_clears(self):
120
 
        stderr = StringIO()
121
 
        stdout = StringIO()
122
 
        # The PQM redirects the output to a file, so it
123
 
        # defaults to creating a Dots progress bar. we
124
 
        # need to force it to believe we are a TTY
125
 
        ui_factory = TextUIFactory(bar_type=TTYProgressBar)
126
 
        pb = ui_factory.nested_progress_bar()
127
 
        try:
128
 
            pb.to_messages_file = stdout
129
 
            ui_factory._progress_bar_stack.bottom().to_file = stderr
130
 
            # Create a progress update that isn't throttled
131
 
            pb.start_time -= 10
132
 
            pb.update('x', 1, 1)
133
 
            result = pb.note('t')
134
 
            self.assertEqual(None, result)
135
 
            self.assertEqual("t\n", stdout.getvalue())
136
 
            # the exact contents will depend on the terminal width and we don't
137
 
            # care about that right now - but you're probably running it on at
138
 
            # least a 10-character wide terminal :)
139
 
            self.assertContainsRe(stderr.getvalue(), r'\r {10,}\r$')
140
 
        finally:
141
 
            pb.finished()
142
 
 
143
 
    def test_progress_nested(self):
144
 
        # test factory based nested and popping.
145
 
        ui = TextUIFactory()
146
 
        pb1 = ui.nested_progress_bar()
147
 
        pb2 = ui.nested_progress_bar()
148
 
        self.assertRaises(errors.MissingProgressBarFinish, pb1.finished)
149
 
        pb2.finished()
150
 
        pb1.finished()
151
 
 
152
 
    def test_progress_stack(self):
153
 
        # test the progress bar stack which the default text factory 
154
 
        # uses.
155
 
        stderr = StringIO()
156
 
        stdout = StringIO()
157
 
        # make a stack, which accepts parameters like a pb.
158
 
        stack = ProgressBarStack(to_file=stderr, to_messages_file=stdout)
159
 
        # but is not one
160
 
        self.assertFalse(getattr(stack, 'note', False))
161
 
        pb1 = stack.get_nested()
162
 
        pb2 = stack.get_nested()
163
 
        self.assertRaises(errors.MissingProgressBarFinish, pb1.finished)
164
 
        pb2.finished()
165
 
        pb1.finished()
166
 
        # the text ui factory never actually removes the stack once its setup.
167
 
        # we need to be able to nest again correctly from here.
168
 
        pb1 = stack.get_nested()
169
 
        pb2 = stack.get_nested()
170
 
        self.assertRaises(errors.MissingProgressBarFinish, pb1.finished)
171
 
        pb2.finished()
172
 
        pb1.finished()
173
 
 
174
 
    def test_text_factory_setting_progress_bar(self):
175
 
        # we should be able to choose the progress bar type used.
176
 
        factory = TextUIFactory(bar_type=DotsProgressBar)
177
 
        bar = factory.nested_progress_bar()
178
 
        bar.finished()
179
 
        self.assertIsInstance(bar, DotsProgressBar)
180
 
 
181
 
    def test_cli_stdin_is_default_stdin(self):
182
 
        factory = CLIUIFactory()
183
 
        self.assertEqual(sys.stdin, factory.stdin)
184
 
 
185
 
    def assert_get_bool_acceptance_of_user_input(self, factory):
186
 
        factory.stdin = StringIO("y\nyes with garbage\n"
187
 
                                 "yes\nn\nnot an answer\n"
188
 
                                 "no\nfoo\n")
189
 
        factory.stdout = StringIO()
190
 
        # there is no output from the base factory
191
 
        self.assertEqual(True, factory.get_boolean(""))
192
 
        self.assertEqual(True, factory.get_boolean(""))
193
 
        self.assertEqual(False, factory.get_boolean(""))
194
 
        self.assertEqual(False, factory.get_boolean(""))
195
 
        self.assertEqual("foo\n", factory.stdin.read())
196
 
        # stdin should be empty
197
 
        self.assertEqual('', factory.stdin.readline())
198
 
 
199
 
    def test_silent_ui_getbool(self):
200
 
        factory = SilentUIFactory()
201
 
        self.assert_get_bool_acceptance_of_user_input(factory)
202
 
 
203
 
    def test_silent_factory_prompts_silently(self):
204
 
        factory = SilentUIFactory()
205
 
        stdout = StringIO()
206
 
        factory.stdin = StringIO("y\n")
207
 
        self.assertEqual(True,
208
 
                         self.apply_redirected(None, stdout, stdout,
209
 
                                               factory.get_boolean, "foo"))
210
 
        self.assertEqual("", stdout.getvalue())
211
 
        # stdin should be empty
212
 
        self.assertEqual('', factory.stdin.readline())
213
 
 
214
 
    def test_text_ui_getbool(self):
215
 
        factory = TextUIFactory()
216
 
        self.assert_get_bool_acceptance_of_user_input(factory)
 
100
        """Test an utf8 password."""
 
101
        ui = _mod_ui_text.TextUIFactory(None, None, None)
 
102
        ui.stdin = tests.StringIOWrapper(u'baz\u1234'.encode('utf8'))
 
103
        ui.stdout = tests.StringIOWrapper()
 
104
        ui.stderr = tests.StringIOWrapper()
 
105
        ui.stderr.encoding = ui.stdout.encoding = ui.stdin.encoding = 'utf8'
 
106
        password = ui.get_password(u'Hello \u1234 %(user)s', user=u'some\u1234')
 
107
        self.assertEqual(u'baz\u1234', password)
 
108
        self.assertEqual(u'Hello \u1234 some\u1234: ',
 
109
                         ui.stderr.getvalue().decode('utf8'))
 
110
        # stdin and stdout should be empty
 
111
        self.assertEqual('', ui.stdin.readline())
 
112
        self.assertEqual('', ui.stdout.getvalue())
 
113
 
 
114
    def test_text_ui_get_boolean(self):
 
115
        stdin = tests.StringIOWrapper("y\n" # True
 
116
                                      "n\n" # False
 
117
                                      " \n y \n" # True
 
118
                                      " no \n" # False
 
119
                                      "yes with garbage\nY\n" # True
 
120
                                      "not an answer\nno\n" # False
 
121
                                      "I'm sure!\nyes\n" # True
 
122
                                      "NO\n" # False
 
123
                                      "foo\n")
 
124
        stdout = tests.StringIOWrapper()
 
125
        stderr = tests.StringIOWrapper()
 
126
        factory = _mod_ui_text.TextUIFactory(stdin, stdout, stderr)
 
127
        self.assertEqual(True, factory.get_boolean(u""))
 
128
        self.assertEqual(False, factory.get_boolean(u""))
 
129
        self.assertEqual(True, factory.get_boolean(u""))
 
130
        self.assertEqual(False, factory.get_boolean(u""))
 
131
        self.assertEqual(True, factory.get_boolean(u""))
 
132
        self.assertEqual(False, factory.get_boolean(u""))
 
133
        self.assertEqual(True, factory.get_boolean(u""))
 
134
        self.assertEqual(False, factory.get_boolean(u""))
 
135
        self.assertEqual("foo\n", factory.stdin.read())
 
136
        # stdin should be empty
 
137
        self.assertEqual('', factory.stdin.readline())
 
138
        # return false on EOF
 
139
        self.assertEqual(False, factory.get_boolean(u""))
 
140
 
 
141
    def test_text_ui_choose_bad_parameters(self):
 
142
        stdin = tests.StringIOWrapper()
 
143
        stdout = tests.StringIOWrapper()
 
144
        stderr = tests.StringIOWrapper()
 
145
        factory = _mod_ui_text.TextUIFactory(stdin, stdout, stderr)
 
146
        # invalid default index
 
147
        self.assertRaises(ValueError, factory.choose, u"", u"&Yes\n&No", 3)
 
148
        # duplicated choice
 
149
        self.assertRaises(ValueError, factory.choose, u"", u"&choice\n&ChOiCe")
 
150
        # duplicated shortcut
 
151
        self.assertRaises(ValueError, factory.choose, u"", u"&choice1\nchoi&ce2")
 
152
 
 
153
    def test_text_ui_choose_prompt(self):
 
154
        stdin = tests.StringIOWrapper()
 
155
        stdout = tests.StringIOWrapper()
 
156
        stderr = tests.StringIOWrapper()
 
157
        factory = _mod_ui_text.TextUIFactory(stdin, stdout, stderr)
 
158
        # choices with explicit shortcuts
 
159
        factory.choose(u"prompt", u"&yes\n&No\nmore &info")
 
160
        self.assertEqual("prompt ([y]es, [N]o, more [i]nfo): \n", factory.stderr.getvalue())
 
161
        # automatic shortcuts
 
162
        factory.stderr.truncate(0)
 
163
        factory.choose(u"prompt", u"yes\nNo\nmore info")
 
164
        self.assertEqual("prompt ([y]es, [N]o, [m]ore info): \n", factory.stderr.getvalue())
 
165
 
 
166
    def test_text_ui_choose_return_values(self):
 
167
        choose = lambda: factory.choose(u"", u"&Yes\n&No\nMaybe\nmore &info", 3)
 
168
        stdin = tests.StringIOWrapper("y\n" # 0
 
169
                                      "n\n" # 1
 
170
                                      " \n" # default: 3
 
171
                                      " no \n" # 1
 
172
                                      "b\na\nd \n" # bad shortcuts, all ignored
 
173
                                      "yes with garbage\nY\n" # 0
 
174
                                      "not an answer\nno\n" # 1
 
175
                                      "info\nmore info\n" # 3
 
176
                                      "Maybe\n" # 2
 
177
                                      "foo\n")
 
178
        stdout = tests.StringIOWrapper()
 
179
        stderr = tests.StringIOWrapper()
 
180
        factory = _mod_ui_text.TextUIFactory(stdin, stdout, stderr)
 
181
        self.assertEqual(0, choose())
 
182
        self.assertEqual(1, choose())
 
183
        self.assertEqual(3, choose())
 
184
        self.assertEqual(1, choose())
 
185
        self.assertEqual(0, choose())
 
186
        self.assertEqual(1, choose())
 
187
        self.assertEqual(3, choose())
 
188
        self.assertEqual(2, choose())
 
189
        self.assertEqual("foo\n", factory.stdin.read())
 
190
        # stdin should be empty
 
191
        self.assertEqual('', factory.stdin.readline())
 
192
        # return None on EOF
 
193
        self.assertEqual(None, choose())
 
194
 
 
195
    def test_text_ui_choose_no_default(self):
 
196
        stdin = tests.StringIOWrapper(" \n" # no default, invalid!
 
197
                                      " yes \n" # 0
 
198
                                      "foo\n")
 
199
        stdout = tests.StringIOWrapper()
 
200
        stderr = tests.StringIOWrapper()
 
201
        factory = _mod_ui_text.TextUIFactory(stdin, stdout, stderr)
 
202
        self.assertEqual(0, factory.choose(u"", u"&Yes\n&No"))
 
203
        self.assertEqual("foo\n", factory.stdin.read())
 
204
 
 
205
    def test_text_ui_get_integer(self):
 
206
        stdin = tests.StringIOWrapper(
 
207
            "1\n"
 
208
            "  -2  \n"
 
209
            "hmmm\nwhat else ?\nCome on\nok 42\n4.24\n42\n")
 
210
        stdout = tests.StringIOWrapper()
 
211
        stderr = tests.StringIOWrapper()
 
212
        factory = _mod_ui_text.TextUIFactory(stdin, stdout, stderr)
 
213
        self.assertEqual(1, factory.get_integer(u""))
 
214
        self.assertEqual(-2, factory.get_integer(u""))
 
215
        self.assertEqual(42, factory.get_integer(u""))
 
216
 
 
217
    def test_text_factory_prompt(self):
 
218
        # see <https://launchpad.net/bugs/365891>
 
219
        StringIO = tests.StringIOWrapper
 
220
        factory = _mod_ui_text.TextUIFactory(StringIO(), StringIO(), StringIO())
 
221
        factory.prompt(u'foo %2e')
 
222
        self.assertEqual('', factory.stdout.getvalue())
 
223
        self.assertEqual('foo %2e', factory.stderr.getvalue())
217
224
 
218
225
    def test_text_factory_prompts_and_clears(self):
219
226
        # a get_boolean call should clear the pb before prompting
220
 
        factory = TextUIFactory(bar_type=DotsProgressBar)
221
 
        factory.stdout = _TTYStringIO()
222
 
        factory.stdin = StringIO("yada\ny\n")
223
 
        pb = self.apply_redirected(factory.stdin, factory.stdout,
224
 
                                   factory.stdout, factory.nested_progress_bar)
225
 
        pb.start_time = None
226
 
        self.apply_redirected(factory.stdin, factory.stdout,
227
 
                              factory.stdout, pb.update, "foo", 0, 1)
 
227
        out = TTYStringIO()
 
228
        self.overrideEnv('TERM', 'xterm')
 
229
        factory = _mod_ui_text.TextUIFactory(
 
230
            stdin=tests.StringIOWrapper("yada\ny\n"),
 
231
            stdout=out, stderr=out)
 
232
        factory._avail_width = lambda: 79
 
233
        pb = factory.nested_progress_bar()
 
234
        pb.show_bar = False
 
235
        pb.show_spinner = False
 
236
        pb.show_count = False
 
237
        pb.update("foo", 0, 1)
228
238
        self.assertEqual(True,
229
239
                         self.apply_redirected(None, factory.stdout,
230
240
                                               factory.stdout,
231
241
                                               factory.get_boolean,
232
 
                                               "what do you want"))
233
 
        output = factory.stdout.getvalue()
234
 
        self.assertEqual("foo: .\n"
235
 
                         "what do you want? [y/n]: what do you want? [y/n]: ",
236
 
                         factory.stdout.getvalue())
 
242
                                               u"what do you want"))
 
243
        output = out.getvalue()
 
244
        self.assertContainsRe(output,
 
245
            "| foo *\r\r  *\r*")
 
246
        self.assertContainsString(output,
 
247
            r"what do you want? ([y]es, [n]o): what do you want? ([y]es, [n]o): ")
 
248
        # stdin should have been totally consumed
 
249
        self.assertEqual('', factory.stdin.readline())
 
250
 
 
251
    def test_text_tick_after_update(self):
 
252
        ui_factory = _mod_ui_text.TextUIFactory(stdout=tests.StringIOWrapper(),
 
253
                                                stderr=tests.StringIOWrapper())
 
254
        pb = ui_factory.nested_progress_bar()
 
255
        try:
 
256
            pb.update('task', 0, 3)
 
257
            # Reset the clock, so that it actually tries to repaint itself
 
258
            ui_factory._progress_view._last_repaint = time.time() - 1.0
 
259
            pb.tick()
 
260
        finally:
 
261
            pb.finished()
 
262
 
 
263
    def test_text_ui_getusername(self):
 
264
        ui = _mod_ui_text.TextUIFactory(None, None, None)
 
265
        ui.stdin = tests.StringIOWrapper('someuser\n\n')
 
266
        ui.stdout = tests.StringIOWrapper()
 
267
        ui.stderr = tests.StringIOWrapper()
 
268
        ui.stdout.encoding = 'utf8'
 
269
        self.assertEqual('someuser',
 
270
                         ui.get_username(u'Hello %(host)s', host='some'))
 
271
        self.assertEqual('Hello some: ', ui.stderr.getvalue())
 
272
        self.assertEqual('', ui.stdout.getvalue())
 
273
        self.assertEqual('', ui.get_username(u"Gebruiker"))
237
274
        # stdin should be empty
238
 
        self.assertEqual('', factory.stdin.readline())
239
 
 
 
275
        self.assertEqual('', ui.stdin.readline())
 
276
 
 
277
    def test_text_ui_getusername_utf8(self):
 
278
        ui = _mod_ui_text.TextUIFactory(None, None, None)
 
279
        ui.stdin = tests.StringIOWrapper(u'someuser\u1234'.encode('utf8'))
 
280
        ui.stdout = tests.StringIOWrapper()
 
281
        ui.stderr = tests.StringIOWrapper()
 
282
        ui.stderr.encoding = ui.stdout.encoding = ui.stdin.encoding = "utf8"
 
283
        username = ui.get_username(u'Hello %(host)s', host=u'some\u1234')
 
284
        self.assertEqual(u"someuser\u1234", username)
 
285
        self.assertEqual(u"Hello some\u1234: ",
 
286
                          ui.stderr.getvalue().decode("utf8"))
 
287
        self.assertEqual('', ui.stdout.getvalue())
 
288
 
 
289
    def test_quietness(self):
 
290
        self.overrideEnv('BZR_PROGRESS_BAR', 'text')
 
291
        ui_factory = _mod_ui_text.TextUIFactory(None,
 
292
            TTYStringIO(),
 
293
            TTYStringIO())
 
294
        self.assertIsInstance(ui_factory._progress_view,
 
295
            _mod_ui_text.TextProgressView)
 
296
        ui_factory.be_quiet(True)
 
297
        self.assertIsInstance(ui_factory._progress_view,
 
298
            _mod_ui_text.NullProgressView)
 
299
 
 
300
    def test_text_ui_show_user_warning(self):
 
301
        from bzrlib.repofmt.groupcompress_repo import RepositoryFormat2a
 
302
        from bzrlib.repofmt.knitpack_repo import RepositoryFormatKnitPack5
 
303
        err = StringIO()
 
304
        out = StringIO()
 
305
        ui = tests.TextUIFactory(stdin=None, stdout=out, stderr=err)
 
306
        remote_fmt = remote.RemoteRepositoryFormat()
 
307
        remote_fmt._network_name = RepositoryFormatKnitPack5().network_name()
 
308
        ui.show_user_warning('cross_format_fetch', from_format=RepositoryFormat2a(),
 
309
            to_format=remote_fmt)
 
310
        self.assertEqual('', out.getvalue())
 
311
        self.assertEqual("Doing on-the-fly conversion from RepositoryFormat2a() to "
 
312
            "RemoteRepositoryFormat(_network_name='Bazaar RepositoryFormatKnitPack5 "
 
313
            "(bzr 1.6)\\n').\nThis may take some time. Upgrade the repositories to "
 
314
            "the same format for better performance.\n",
 
315
            err.getvalue())
 
316
        # and now with it suppressed please
 
317
        err = StringIO()
 
318
        out = StringIO()
 
319
        ui = tests.TextUIFactory(stdin=None, stdout=out, stderr=err)
 
320
        ui.suppressed_warnings.add('cross_format_fetch')
 
321
        ui.show_user_warning('cross_format_fetch', from_format=RepositoryFormat2a(),
 
322
            to_format=remote_fmt)
 
323
        self.assertEqual('', out.getvalue())
 
324
        self.assertEqual('', err.getvalue())
 
325
 
 
326
 
 
327
class TestTextUIOutputStream(tests.TestCase):
 
328
    """Tests for output stream that synchronizes with progress bar."""
 
329
 
 
330
    def test_output_clears_terminal(self):
 
331
        stdout = tests.StringIOWrapper()
 
332
        stderr = tests.StringIOWrapper()
 
333
        clear_calls = []
 
334
 
 
335
        uif =  _mod_ui_text.TextUIFactory(None, stdout, stderr)
 
336
        uif.clear_term = lambda: clear_calls.append('clear')
 
337
 
 
338
        stream = _mod_ui_text.TextUIOutputStream(uif, uif.stdout)
 
339
        stream.write("Hello world!\n")
 
340
        stream.write("there's more...\n")
 
341
        stream.writelines(["1\n", "2\n", "3\n"])
 
342
 
 
343
        self.assertEqual(stdout.getvalue(),
 
344
            "Hello world!\n"
 
345
            "there's more...\n"
 
346
            "1\n2\n3\n")
 
347
        self.assertEqual(['clear', 'clear', 'clear'],
 
348
            clear_calls)
 
349
 
 
350
        stream.flush()
 
351
 
 
352
 
 
353
class UITests(tests.TestCase):
 
354
 
 
355
    def test_progress_construction(self):
 
356
        """TextUIFactory constructs the right progress view.
 
357
        """
 
358
        FileStringIO = tests.StringIOWrapper
 
359
        for (file_class, term, pb, expected_pb_class) in (
 
360
            # on an xterm, either use them or not as the user requests,
 
361
            # otherwise default on
 
362
            (TTYStringIO, 'xterm', 'none', _mod_ui_text.NullProgressView),
 
363
            (TTYStringIO, 'xterm', 'text', _mod_ui_text.TextProgressView),
 
364
            (TTYStringIO, 'xterm', None, _mod_ui_text.TextProgressView),
 
365
            # on a dumb terminal, again if there's explicit configuration do
 
366
            # it, otherwise default off
 
367
            (TTYStringIO, 'dumb', 'none', _mod_ui_text.NullProgressView),
 
368
            (TTYStringIO, 'dumb', 'text', _mod_ui_text.TextProgressView),
 
369
            (TTYStringIO, 'dumb', None, _mod_ui_text.NullProgressView),
 
370
            # on a non-tty terminal, it's null regardless of $TERM
 
371
            (FileStringIO, 'xterm', None, _mod_ui_text.NullProgressView),
 
372
            (FileStringIO, 'dumb', None, _mod_ui_text.NullProgressView),
 
373
            # however, it can still be forced on
 
374
            (FileStringIO, 'dumb', 'text', _mod_ui_text.TextProgressView),
 
375
            ):
 
376
            self.overrideEnv('TERM', term)
 
377
            self.overrideEnv('BZR_PROGRESS_BAR', pb)
 
378
            stdin = file_class('')
 
379
            stderr = file_class()
 
380
            stdout = file_class()
 
381
            uif = _mod_ui.make_ui_for_terminal(stdin, stdout, stderr)
 
382
            self.assertIsInstance(uif, _mod_ui_text.TextUIFactory,
 
383
                "TERM=%s BZR_PROGRESS_BAR=%s uif=%r" % (term, pb, uif,))
 
384
            self.assertIsInstance(uif.make_progress_view(),
 
385
                expected_pb_class,
 
386
                "TERM=%s BZR_PROGRESS_BAR=%s uif=%r" % (term, pb, uif,))
 
387
 
 
388
    def test_text_ui_non_terminal(self):
 
389
        """Even on non-ttys, make_ui_for_terminal gives a text ui."""
 
390
        stdin = NonTTYStringIO('')
 
391
        stderr = NonTTYStringIO()
 
392
        stdout = NonTTYStringIO()
 
393
        for term_type in ['dumb', None, 'xterm']:
 
394
            self.overrideEnv('TERM', term_type)
 
395
            uif = _mod_ui.make_ui_for_terminal(stdin, stdout, stderr)
 
396
            self.assertIsInstance(uif, _mod_ui_text.TextUIFactory,
 
397
                'TERM=%r' % (term_type,))
 
398
 
 
399
 
 
400
class SilentUITests(tests.TestCase):
 
401
 
 
402
    def test_silent_factory_get_password(self):
 
403
        # A silent factory that can't do user interaction can't get a
 
404
        # password.  Possibly it should raise a more specific error but it
 
405
        # can't succeed.
 
406
        ui = _mod_ui.SilentUIFactory()
 
407
        stdout = tests.StringIOWrapper()
 
408
        self.assertRaises(
 
409
            NotImplementedError,
 
410
            self.apply_redirected,
 
411
            None, stdout, stdout, ui.get_password)
 
412
        # and it didn't write anything out either
 
413
        self.assertEqual('', stdout.getvalue())
 
414
 
 
415
    def test_silent_ui_getbool(self):
 
416
        factory = _mod_ui.SilentUIFactory()
 
417
        stdout = tests.StringIOWrapper()
 
418
        self.assertRaises(
 
419
            NotImplementedError,
 
420
            self.apply_redirected,
 
421
            None, stdout, stdout, factory.get_boolean, u"foo")
 
422
 
 
423
 
 
424
class TestUIFactoryTests(tests.TestCase):
 
425
 
 
426
    def test_test_ui_factory_progress(self):
 
427
        # there's no output; we just want to make sure this doesn't crash -
 
428
        # see https://bugs.launchpad.net/bzr/+bug/408201
 
429
        ui = tests.TestUIFactory()
 
430
        pb = ui.nested_progress_bar()
 
431
        pb.update('hello')
 
432
        pb.tick()
 
433
        pb.finished()
 
434
 
 
435
 
 
436
class CannedInputUIFactoryTests(tests.TestCase):
 
437
 
 
438
    def test_canned_input_get_input(self):
 
439
        uif = _mod_ui.CannedInputUIFactory([True, 'mbp', 'password', 42])
 
440
        self.assertEqual(True, uif.get_boolean(u'Extra cheese?'))
 
441
        self.assertEqual('mbp', uif.get_username(u'Enter your user name'))
 
442
        self.assertEqual('password',
 
443
                         uif.get_password(u'Password for %(host)s',
 
444
                                          host='example.com'))
 
445
        self.assertEqual(42, uif.get_integer(u'And all that jazz ?'))
 
446
 
 
447
 
 
448
class TestBoolFromString(tests.TestCase):
 
449
 
 
450
    def assertIsTrue(self, s, accepted_values=None):
 
451
        res = _mod_ui.bool_from_string(s, accepted_values=accepted_values)
 
452
        self.assertEqual(True, res)
 
453
 
 
454
    def assertIsFalse(self, s, accepted_values=None):
 
455
        res = _mod_ui.bool_from_string(s, accepted_values=accepted_values)
 
456
        self.assertEqual(False, res)
 
457
 
 
458
    def assertIsNone(self, s, accepted_values=None):
 
459
        res = _mod_ui.bool_from_string(s, accepted_values=accepted_values)
 
460
        self.assertIs(None, res)
 
461
 
 
462
    def test_know_valid_values(self):
 
463
        self.assertIsTrue('true')
 
464
        self.assertIsFalse('false')
 
465
        self.assertIsTrue('1')
 
466
        self.assertIsFalse('0')
 
467
        self.assertIsTrue('on')
 
468
        self.assertIsFalse('off')
 
469
        self.assertIsTrue('yes')
 
470
        self.assertIsFalse('no')
 
471
        self.assertIsTrue('y')
 
472
        self.assertIsFalse('n')
 
473
        # Also try some case variations
 
474
        self.assertIsTrue('True')
 
475
        self.assertIsFalse('False')
 
476
        self.assertIsTrue('On')
 
477
        self.assertIsFalse('Off')
 
478
        self.assertIsTrue('ON')
 
479
        self.assertIsFalse('OFF')
 
480
        self.assertIsTrue('oN')
 
481
        self.assertIsFalse('oFf')
 
482
 
 
483
    def test_invalid_values(self):
 
484
        self.assertIsNone(None)
 
485
        self.assertIsNone('doubt')
 
486
        self.assertIsNone('frue')
 
487
        self.assertIsNone('talse')
 
488
        self.assertIsNone('42')
 
489
 
 
490
    def test_provided_values(self):
 
491
        av = dict(y=True, n=False, yes=True, no=False)
 
492
        self.assertIsTrue('y', av)
 
493
        self.assertIsTrue('Y', av)
 
494
        self.assertIsTrue('Yes', av)
 
495
        self.assertIsFalse('n', av)
 
496
        self.assertIsFalse('N', av)
 
497
        self.assertIsFalse('No', av)
 
498
        self.assertIsNone('1', av)
 
499
        self.assertIsNone('0', av)
 
500
        self.assertIsNone('on', av)
 
501
        self.assertIsNone('off', av)
 
502
 
 
503
 
 
504
class TestConfirmationUserInterfacePolicy(tests.TestCase):
 
505
 
 
506
    def test_confirm_action_default(self):
 
507
        base_ui = _mod_ui.NoninteractiveUIFactory()
 
508
        for answer in [True, False]:
 
509
            self.assertEqual(
 
510
                _mod_ui.ConfirmationUserInterfacePolicy(base_ui, answer, {})
 
511
                .confirm_action("Do something?",
 
512
                    "bzrlib.tests.do_something", {}),
 
513
                answer)
 
514
 
 
515
    def test_confirm_action_specific(self):
 
516
        base_ui = _mod_ui.NoninteractiveUIFactory()
 
517
        for default_answer in [True, False]:
 
518
            for specific_answer in [True, False]:
 
519
                for conf_id in ['given_id', 'other_id']:
 
520
                    wrapper = _mod_ui.ConfirmationUserInterfacePolicy(
 
521
                        base_ui, default_answer, dict(given_id=specific_answer))
 
522
                    result = wrapper.confirm_action("Do something?", conf_id, {})
 
523
                    if conf_id == 'given_id':
 
524
                        self.assertEqual(result, specific_answer)
 
525
                    else:
 
526
                        self.assertEqual(result, default_answer)
 
527
 
 
528
    def test_repr(self):
 
529
        base_ui = _mod_ui.NoninteractiveUIFactory()
 
530
        wrapper = _mod_ui.ConfirmationUserInterfacePolicy(
 
531
            base_ui, True, dict(a=2))
 
532
        self.assertThat(repr(wrapper),
 
533
            Equals("ConfirmationUserInterfacePolicy("
 
534
                "NoninteractiveUIFactory(), True, {'a': 2})"))
 
535
 
 
536
 
 
537
class TestProgressRecordingUI(tests.TestCase):
 
538
    """Test test-oriented UIFactory that records progress updates"""
 
539
 
 
540
    def test_nested_ignore_depth_beyond_one(self):
 
541
        # we only want to capture the first level out progress, not
 
542
        # want sub-components might do. So we have nested bars ignored.
 
543
        factory = ProgressRecordingUIFactory()
 
544
        pb1 = factory.nested_progress_bar()
 
545
        pb1.update('foo', 0, 1)
 
546
        pb2 = factory.nested_progress_bar()
 
547
        pb2.update('foo', 0, 1)
 
548
        pb2.finished()
 
549
        pb1.finished()
 
550
        self.assertEqual([("update", 0, 1, 'foo')], factory._calls)