/brz/remove-bazaar

To get this branch, use:
bzr branch http://gegoxaren.bato24.eu/bzr/brz/remove-bazaar

« back to all changes in this revision

Viewing changes to bzrlib/tests/test_ui.py

  • Committer: Richard Wilbur
  • Date: 2016-02-04 19:07:28 UTC
  • mto: This revision was merged to the branch mainline in revision 6618.
  • Revision ID: richard.wilbur@gmail.com-20160204190728-p0zvfii6zase0fw7
Update COPYING.txt from the original http://www.gnu.org/licenses/gpl-2.0.txt  (Only differences were in whitespace.)  Thanks to Petr Stodulka for pointing out the discrepancy.

Show diffs side-by-side

added added

removed removed

Lines of Context:
1
 
# Copyright (C) 2005-2012, 2016 Canonical Ltd
 
1
# Copyright (C) 2005-2011 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
14
14
# along with this program; if not, write to the Free Software
15
15
# Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
16
16
 
17
 
"""Tests for the breezy ui."""
 
17
"""Tests for the bzrlib ui
 
18
"""
18
19
 
19
20
import time
20
21
 
 
22
from StringIO import StringIO
 
23
 
21
24
from testtools.matchers import *
22
25
 
23
 
from .. import (
 
26
from bzrlib import (
24
27
    config,
 
28
    remote,
25
29
    tests,
26
30
    ui as _mod_ui,
27
31
    )
28
 
from ..bzr import (
29
 
    remote,
30
 
    )
31
 
from . import (
 
32
from bzrlib.tests import (
32
33
    fixtures,
33
 
    ui_testing,
34
34
    )
35
 
from ..ui import text as _mod_ui_text
36
 
from .testui import (
 
35
from bzrlib.ui import text as _mod_ui_text
 
36
from bzrlib.tests.testui import (
37
37
    ProgressRecordingUIFactory,
38
38
    )
39
39
 
40
40
 
41
 
class TestUIConfiguration(tests.TestCaseInTempDir):
 
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):
42
56
 
43
57
    def test_output_encoding_configuration(self):
44
 
        enc = next(fixtures.generate_unicode_encodings())
 
58
        enc = fixtures.generate_unicode_encodings().next()
45
59
        config.GlobalStack().set('output_encoding', enc)
46
 
        IO = ui_testing.BytesIOWithEncoding
47
 
        ui = _mod_ui.make_ui_for_terminal(IO(), IO(), IO())
 
60
        ui = tests.TestUIFactory(stdin=None,
 
61
            stdout=tests.StringIOWrapper(),
 
62
            stderr=tests.StringIOWrapper())
48
63
        output = ui.make_output_stream()
49
 
        self.assertEqual(output.encoding, enc)
 
64
        self.assertEquals(output.encoding, enc)
50
65
 
51
66
 
52
67
class TestTextUIFactory(tests.TestCase):
53
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
 
54
75
    def test_text_factory_confirm(self):
55
76
        # turns into reading a regular boolean
56
 
        with ui_testing.TestUIFactory('n\n') as ui:
57
 
            self.assertEqual(
58
 
                False,
59
 
                ui.confirm_action(
60
 
                    u'Should %(thing)s pass?',
61
 
                    'breezy.tests.test_ui.confirmation',
62
 
                    {'thing': 'this'}))
 
77
        ui = self.make_test_ui_factory('n\n')
 
78
        self.assertEquals(ui.confirm_action(u'Should %(thing)s pass?',
 
79
            'bzrlib.tests.test_ui.confirmation',
 
80
            {'thing': 'this'},),
 
81
            False)
63
82
 
64
83
    def test_text_factory_ascii_password(self):
65
 
        ui = ui_testing.TestUIFactory('secret\n')
66
 
        with ui.nested_progress_bar() as pb:
 
84
        ui = self.make_test_ui_factory('secret\n')
 
85
        pb = ui.nested_progress_bar()
 
86
        try:
67
87
            self.assertEqual('secret',
68
88
                             self.apply_redirected(ui.stdin, ui.stdout,
69
89
                                                   ui.stderr,
73
93
            self.assertEqual('', ui.stdout.readline())
74
94
            # stdin should be empty
75
95
            self.assertEqual('', ui.stdin.readline())
 
96
        finally:
 
97
            pb.finished()
76
98
 
77
 
    def test_text_factory_unicode_password(self):
78
 
        """Test a unicode password."""
79
 
        ui = ui_testing.TextUIFactory(u'baz\u1234')
 
99
    def test_text_factory_utf8_password(self):
 
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'
80
106
        password = ui.get_password(u'Hello \u1234 %(user)s', user=u'some\u1234')
81
107
        self.assertEqual(u'baz\u1234', password)
82
 
        self.assertEqual(u'Hello \u1234 some\u1234: ', ui.stderr.getvalue())
 
108
        self.assertEqual(u'Hello \u1234 some\u1234: ',
 
109
                         ui.stderr.getvalue().decode('utf8'))
83
110
        # stdin and stdout should be empty
84
111
        self.assertEqual('', ui.stdin.readline())
85
112
        self.assertEqual('', ui.stdout.getvalue())
86
113
 
87
114
    def test_text_ui_get_boolean(self):
88
 
        stdin_text = (
89
 
            "y\n" # True
90
 
            "n\n" # False
91
 
            " \n y \n" # True
92
 
            " no \n" # False
93
 
            "yes with garbage\nY\n" # True
94
 
            "not an answer\nno\n" # False
95
 
            "I'm sure!\nyes\n" # True
96
 
            "NO\n" # False
97
 
            "foo\n")
98
 
        with ui_testing.TextUIFactory(stdin_text) as factory:
99
 
            self.assertEqual(True, factory.get_boolean(u""))
100
 
            self.assertEqual(False, factory.get_boolean(u""))
101
 
            self.assertEqual(True, factory.get_boolean(u""))
102
 
            self.assertEqual(False, factory.get_boolean(u""))
103
 
            self.assertEqual(True, factory.get_boolean(u""))
104
 
            self.assertEqual(False, factory.get_boolean(u""))
105
 
            self.assertEqual(True, factory.get_boolean(u""))
106
 
            self.assertEqual(False, factory.get_boolean(u""))
107
 
            self.assertEqual("foo\n", factory.stdin.read())
108
 
            # stdin should be empty
109
 
            self.assertEqual('', factory.stdin.readline())
110
 
            # return false on EOF
111
 
            self.assertEqual(False, factory.get_boolean(u""))
 
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""))
112
140
 
113
141
    def test_text_ui_choose_bad_parameters(self):
114
 
        with ui_testing.TextUIFactory(u"") as factory:
115
 
            # invalid default index
116
 
            self.assertRaises(ValueError, factory.choose, u"", u"&Yes\n&No", 3)
117
 
            # duplicated choice
118
 
            self.assertRaises(
119
 
                ValueError, factory.choose, u"", u"&choice\n&ChOiCe")
120
 
            # duplicated shortcut
121
 
            self.assertRaises(
122
 
                ValueError, factory.choose, u"", u"&choice1\nchoi&ce2")
 
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")
123
152
 
124
 
    def test_text_ui_choose_prompt_explicit(self):
 
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)
125
158
        # choices with explicit shortcuts
126
 
        with ui_testing.TextUIFactory(u"") as factory:
127
 
            factory.choose(u"prompt", u"&yes\n&No\nmore &info")
128
 
            self.assertEqual(
129
 
                "prompt ([y]es, [N]o, more [i]nfo): \n",
130
 
                factory.stderr.getvalue())
131
 
 
132
 
    def test_text_ui_choose_prompt_automatic(self):
 
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())
133
161
        # automatic shortcuts
134
 
        with ui_testing.TextUIFactory(u"") as factory:
135
 
            factory.choose(u"prompt", u"yes\nNo\nmore info")
136
 
            self.assertEqual(
137
 
                "prompt ([y]es, [N]o, [m]ore info): \n",
138
 
                factory.stderr.getvalue())
 
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())
139
165
 
140
166
    def test_text_ui_choose_return_values(self):
141
 
        def choose():
142
 
            return factory.choose(u"", u"&Yes\n&No\nMaybe\nmore &info", 3)
143
 
        stdin_text = (
144
 
            "y\n" # 0
145
 
            "n\n" # 1
146
 
            " \n" # default: 3
147
 
            " no \n" # 1
148
 
            "b\na\nd \n" # bad shortcuts, all ignored
149
 
            "yes with garbage\nY\n" # 0
150
 
            "not an answer\nno\n" # 1
151
 
            "info\nmore info\n" # 3
152
 
            "Maybe\n" # 2
153
 
            "foo\n")
154
 
        with ui_testing.TextUIFactory(stdin_text) as factory:
155
 
            self.assertEqual(0, choose())
156
 
            self.assertEqual(1, choose())
157
 
            self.assertEqual(3, choose())
158
 
            self.assertEqual(1, choose())
159
 
            self.assertEqual(0, choose())
160
 
            self.assertEqual(1, choose())
161
 
            self.assertEqual(3, choose())
162
 
            self.assertEqual(2, choose())
163
 
            self.assertEqual("foo\n", factory.stdin.read())
164
 
            # stdin should be empty
165
 
            self.assertEqual('', factory.stdin.readline())
166
 
            # return None on EOF
167
 
            self.assertEqual(None, choose())
 
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())
168
194
 
169
195
    def test_text_ui_choose_no_default(self):
170
 
        stdin_text = (
171
 
            " \n" # no default, invalid!
172
 
            " yes \n" # 0
173
 
            "foo\n")
174
 
        with ui_testing.TextUIFactory(stdin_text) as factory:
175
 
            self.assertEqual(0, factory.choose(u"", u"&Yes\n&No"))
176
 
            self.assertEqual("foo\n", factory.stdin.read())
 
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())
177
204
 
178
205
    def test_text_ui_get_integer(self):
179
 
        stdin_text = (
 
206
        stdin = tests.StringIOWrapper(
180
207
            "1\n"
181
208
            "  -2  \n"
182
209
            "hmmm\nwhat else ?\nCome on\nok 42\n4.24\n42\n")
183
 
        with ui_testing.TextUIFactory(stdin_text) as factory:
184
 
            self.assertEqual(1, factory.get_integer(u""))
185
 
            self.assertEqual(-2, factory.get_integer(u""))
186
 
            self.assertEqual(42, factory.get_integer(u""))
 
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""))
187
216
 
188
217
    def test_text_factory_prompt(self):
189
218
        # see <https://launchpad.net/bugs/365891>
190
 
        with ui_testing.TextUIFactory() as factory:
191
 
            factory.prompt(u'foo %2e')
192
 
            self.assertEqual('', factory.stdout.getvalue())
193
 
            self.assertEqual('foo %2e', factory.stderr.getvalue())
 
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())
194
224
 
195
225
    def test_text_factory_prompts_and_clears(self):
196
226
        # a get_boolean call should clear the pb before prompting
197
 
        out = ui_testing.StringIOAsTTY()
 
227
        out = TTYStringIO()
198
228
        self.overrideEnv('TERM', 'xterm')
199
 
        factory = ui_testing.TextUIFactory("yada\ny\n", stdout=out, stderr=out)
200
 
        with factory:
201
 
            pb = factory.nested_progress_bar()
202
 
            pb._avail_width = lambda: 79
203
 
            pb.show_bar = False
204
 
            pb.show_spinner = False
205
 
            pb.show_count = False
206
 
            pb.update("foo", 0, 1)
207
 
            self.assertEqual(
208
 
                True,
209
 
                self.apply_redirected(
210
 
                    None, factory.stdout, factory.stdout, factory.get_boolean,
211
 
                    u"what do you want"))
212
 
            output = out.getvalue()
213
 
            self.assertContainsRe(output,
214
 
                "| foo *\r\r  *\r*")
215
 
            self.assertContainsString(output,
216
 
                r"what do you want? ([y]es, [n]o): what do you want? ([y]es, [n]o): ")
217
 
            # stdin should have been totally consumed
218
 
            self.assertEqual('', factory.stdin.readline())
 
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)
 
238
        self.assertEqual(True,
 
239
                         self.apply_redirected(None, factory.stdout,
 
240
                                               factory.stdout,
 
241
                                               factory.get_boolean,
 
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())
219
250
 
220
251
    def test_text_tick_after_update(self):
221
 
        ui_factory = ui_testing.TextUIFactory()
222
 
        with ui_factory.nested_progress_bar() as pb:
 
252
        ui_factory = _mod_ui_text.TextUIFactory(stdout=tests.StringIOWrapper(),
 
253
                                                stderr=tests.StringIOWrapper())
 
254
        pb = ui_factory.nested_progress_bar()
 
255
        try:
223
256
            pb.update('task', 0, 3)
224
257
            # Reset the clock, so that it actually tries to repaint itself
225
258
            ui_factory._progress_view._last_repaint = time.time() - 1.0
226
259
            pb.tick()
 
260
        finally:
 
261
            pb.finished()
227
262
 
228
263
    def test_text_ui_getusername(self):
229
 
        ui = ui_testing.TextUIFactory('someuser\n\n')
 
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'
230
269
        self.assertEqual('someuser',
231
270
                         ui.get_username(u'Hello %(host)s', host='some'))
232
 
        self.assertEqual('Hello some: ', ui.stderr.getvalue())
233
 
        self.assertEqual('', ui.stdout.getvalue())
 
271
        self.assertEquals('Hello some: ', ui.stderr.getvalue())
 
272
        self.assertEquals('', ui.stdout.getvalue())
234
273
        self.assertEqual('', ui.get_username(u"Gebruiker"))
235
274
        # stdin should be empty
236
275
        self.assertEqual('', ui.stdin.readline())
237
276
 
238
 
    def test_text_ui_getusername_unicode(self):
239
 
        ui = ui_testing.TextUIFactory(u'someuser\u1234')
 
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"
240
283
        username = ui.get_username(u'Hello %(host)s', host=u'some\u1234')
241
 
        self.assertEqual(u"someuser\u1234", username)
242
 
        self.assertEqual(u"Hello some\u1234: ", ui.stderr.getvalue())
243
 
        self.assertEqual('', ui.stdout.getvalue())
 
284
        self.assertEquals(u"someuser\u1234", username)
 
285
        self.assertEquals(u"Hello some\u1234: ",
 
286
                          ui.stderr.getvalue().decode("utf8"))
 
287
        self.assertEquals('', ui.stdout.getvalue())
244
288
 
245
289
    def test_quietness(self):
246
 
        self.overrideEnv('BRZ_PROGRESS_BAR', 'text')
247
 
        ui_factory = ui_testing.TextUIFactory(
248
 
            stderr=ui_testing.StringIOAsTTY())
249
 
        with ui_factory:
250
 
            self.assertIsInstance(ui_factory._progress_view,
251
 
                _mod_ui_text.TextProgressView)
252
 
            ui_factory.be_quiet(True)
253
 
            self.assertIsInstance(ui_factory._progress_view,
254
 
                _mod_ui_text.NullProgressView)
 
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)
255
299
 
256
300
    def test_text_ui_show_user_warning(self):
257
 
        from ..bzr.groupcompress_repo import RepositoryFormat2a
258
 
        from ..bzr.knitpack_repo import RepositoryFormatKnitPack5
259
 
        ui = ui_testing.TextUIFactory()
 
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)
260
306
        remote_fmt = remote.RemoteRepositoryFormat()
261
307
        remote_fmt._network_name = RepositoryFormatKnitPack5().network_name()
262
308
        ui.show_user_warning('cross_format_fetch', from_format=RepositoryFormat2a(),
263
309
            to_format=remote_fmt)
264
 
        self.assertEqual('', ui.stdout.getvalue())
265
 
        self.assertContainsRe(
266
 
            ui.stderr.getvalue(),
267
 
            "^Doing on-the-fly conversion from RepositoryFormat2a\\(\\) to "
268
 
                "RemoteRepositoryFormat\\(_network_name="
269
 
                "b?'Bazaar RepositoryFormatKnitPack5 \\(bzr 1.6\\)\\\\n'\\)\\.\n"
270
 
            "This may take some time. Upgrade the repositories to "
271
 
                "the same format for better performance\\.\n$")
 
310
        self.assertEquals('', out.getvalue())
 
311
        self.assertEquals("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())
272
316
        # and now with it suppressed please
273
 
        ui = ui_testing.TextUIFactory()
 
317
        err = StringIO()
 
318
        out = StringIO()
 
319
        ui = tests.TextUIFactory(stdin=None, stdout=out, stderr=err)
274
320
        ui.suppressed_warnings.add('cross_format_fetch')
275
321
        ui.show_user_warning('cross_format_fetch', from_format=RepositoryFormat2a(),
276
322
            to_format=remote_fmt)
277
 
        self.assertEqual('', ui.stdout.getvalue())
278
 
        self.assertEqual('', ui.stderr.getvalue())
 
323
        self.assertEquals('', out.getvalue())
 
324
        self.assertEquals('', err.getvalue())
279
325
 
280
326
 
281
327
class TestTextUIOutputStream(tests.TestCase):
282
328
    """Tests for output stream that synchronizes with progress bar."""
283
329
 
284
330
    def test_output_clears_terminal(self):
 
331
        stdout = tests.StringIOWrapper()
 
332
        stderr = tests.StringIOWrapper()
285
333
        clear_calls = []
286
334
 
287
 
        uif =  ui_testing.TextUIFactory()
 
335
        uif =  _mod_ui_text.TextUIFactory(None, stdout, stderr)
288
336
        uif.clear_term = lambda: clear_calls.append('clear')
289
337
 
290
 
        stream = _mod_ui_text.TextUIOutputStream(uif, uif.stdout, 'utf-8', 'strict')
291
 
        stream.write(u"Hello world!\n")
292
 
        stream.write(u"there's more...\n")
293
 
        stream.writelines([u"1\n", u"2\n", u"3\n"])
 
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"])
294
342
 
295
 
        self.assertEqual(uif.stdout.getvalue(),
296
 
            u"Hello world!\n"
297
 
            u"there's more...\n"
298
 
            u"1\n2\n3\n")
 
343
        self.assertEqual(stdout.getvalue(),
 
344
            "Hello world!\n"
 
345
            "there's more...\n"
 
346
            "1\n2\n3\n")
299
347
        self.assertEqual(['clear', 'clear', 'clear'],
300
348
            clear_calls)
301
349
 
307
355
    def test_progress_construction(self):
308
356
        """TextUIFactory constructs the right progress view.
309
357
        """
310
 
        FileStringIO = ui_testing.StringIOWithEncoding
311
 
        TTYStringIO = ui_testing.StringIOAsTTY
 
358
        FileStringIO = tests.StringIOWrapper
312
359
        for (file_class, term, pb, expected_pb_class) in (
313
360
            # on an xterm, either use them or not as the user requests,
314
361
            # otherwise default on
327
374
            (FileStringIO, 'dumb', 'text', _mod_ui_text.TextProgressView),
328
375
            ):
329
376
            self.overrideEnv('TERM', term)
330
 
            self.overrideEnv('BRZ_PROGRESS_BAR', pb)
331
 
            stdin = file_class(u'')
 
377
            self.overrideEnv('BZR_PROGRESS_BAR', pb)
 
378
            stdin = file_class('')
332
379
            stderr = file_class()
333
380
            stdout = file_class()
334
381
            uif = _mod_ui.make_ui_for_terminal(stdin, stdout, stderr)
335
382
            self.assertIsInstance(uif, _mod_ui_text.TextUIFactory,
336
 
                "TERM=%s BRZ_PROGRESS_BAR=%s uif=%r" % (term, pb, uif,))
 
383
                "TERM=%s BZR_PROGRESS_BAR=%s uif=%r" % (term, pb, uif,))
337
384
            self.assertIsInstance(uif.make_progress_view(),
338
385
                expected_pb_class,
339
 
                "TERM=%s BRZ_PROGRESS_BAR=%s uif=%r" % (term, pb, uif,))
 
386
                "TERM=%s BZR_PROGRESS_BAR=%s uif=%r" % (term, pb, uif,))
340
387
 
341
388
    def test_text_ui_non_terminal(self):
342
389
        """Even on non-ttys, make_ui_for_terminal gives a text ui."""
343
 
        stdin = stderr = stdout = ui_testing.StringIOWithEncoding()
 
390
        stdin = NonTTYStringIO('')
 
391
        stderr = NonTTYStringIO()
 
392
        stdout = NonTTYStringIO()
344
393
        for term_type in ['dumb', None, 'xterm']:
345
394
            self.overrideEnv('TERM', term_type)
346
395
            uif = _mod_ui.make_ui_for_terminal(stdin, stdout, stderr)
355
404
        # password.  Possibly it should raise a more specific error but it
356
405
        # can't succeed.
357
406
        ui = _mod_ui.SilentUIFactory()
358
 
        stdout = ui_testing.StringIOWithEncoding()
 
407
        stdout = tests.StringIOWrapper()
359
408
        self.assertRaises(
360
409
            NotImplementedError,
361
410
            self.apply_redirected,
365
414
 
366
415
    def test_silent_ui_getbool(self):
367
416
        factory = _mod_ui.SilentUIFactory()
368
 
        stdout = ui_testing.StringIOWithEncoding()
 
417
        stdout = tests.StringIOWrapper()
369
418
        self.assertRaises(
370
419
            NotImplementedError,
371
420
            self.apply_redirected,
377
426
    def test_test_ui_factory_progress(self):
378
427
        # there's no output; we just want to make sure this doesn't crash -
379
428
        # see https://bugs.launchpad.net/bzr/+bug/408201
380
 
        ui = ui_testing.TestUIFactory()
381
 
        with ui.nested_progress_bar() as pb:
382
 
            pb.update('hello')
383
 
            pb.tick()
 
429
        ui = tests.TestUIFactory()
 
430
        pb = ui.nested_progress_bar()
 
431
        pb.update('hello')
 
432
        pb.tick()
 
433
        pb.finished()
384
434
 
385
435
 
386
436
class CannedInputUIFactoryTests(tests.TestCase):
399
449
 
400
450
    def assertIsTrue(self, s, accepted_values=None):
401
451
        res = _mod_ui.bool_from_string(s, accepted_values=accepted_values)
402
 
        self.assertEqual(True, res)
 
452
        self.assertEquals(True, res)
403
453
 
404
454
    def assertIsFalse(self, s, accepted_values=None):
405
455
        res = _mod_ui.bool_from_string(s, accepted_values=accepted_values)
406
 
        self.assertEqual(False, res)
 
456
        self.assertEquals(False, res)
407
457
 
408
458
    def assertIsNone(self, s, accepted_values=None):
409
459
        res = _mod_ui.bool_from_string(s, accepted_values=accepted_values)
456
506
    def test_confirm_action_default(self):
457
507
        base_ui = _mod_ui.NoninteractiveUIFactory()
458
508
        for answer in [True, False]:
459
 
            self.assertEqual(
 
509
            self.assertEquals(
460
510
                _mod_ui.ConfirmationUserInterfacePolicy(base_ui, answer, {})
461
511
                .confirm_action("Do something?",
462
 
                    "breezy.tests.do_something", {}),
 
512
                    "bzrlib.tests.do_something", {}),
463
513
                answer)
464
514
 
465
515
    def test_confirm_action_specific(self):
471
521
                        base_ui, default_answer, dict(given_id=specific_answer))
472
522
                    result = wrapper.confirm_action("Do something?", conf_id, {})
473
523
                    if conf_id == 'given_id':
474
 
                        self.assertEqual(result, specific_answer)
 
524
                        self.assertEquals(result, specific_answer)
475
525
                    else:
476
 
                        self.assertEqual(result, default_answer)
 
526
                        self.assertEquals(result, default_answer)
477
527
 
478
528
    def test_repr(self):
479
529
        base_ui = _mod_ui.NoninteractiveUIFactory()