24
from StringIO import StringIO
24
26
from bzrlib import (
29
34
from bzrlib.symbol_versioning import (
32
from bzrlib.tests import test_progress
37
from bzrlib.tests import (
33
41
from bzrlib.ui import text as _mod_ui_text
44
class TestUIConfiguration(tests.TestCaseWithTransport):
46
def test_output_encoding_configuration(self):
47
enc = fixtures.generate_unicode_encodings().next()
48
config.GlobalConfig().set_user_option('output_encoding',
50
ui = tests.TestUIFactory(stdin=None,
51
stdout=tests.StringIOWrapper(),
52
stderr=tests.StringIOWrapper())
53
os = ui.make_output_stream()
54
self.assertEquals(os.encoding, enc)
36
57
class TestTextUIFactory(tests.TestCase):
38
59
def test_text_factory_ascii_password(self):
82
def test_progress_note(self):
83
stderr = tests.StringIOWrapper()
84
stdout = tests.StringIOWrapper()
85
ui_factory = _mod_ui_text.TextUIFactory(stdin=tests.StringIOWrapper(''),
88
pb = ui_factory.nested_progress_bar()
90
result = self.applyDeprecated(deprecated_in((2, 1, 0)),
93
self.assertEqual(None, result)
94
self.assertEqual("t\n", stdout.getvalue())
95
# Since there was no update() call, there should be no clear() call
96
self.failIf(re.search(r'^\r {10,}\r$',
97
stderr.getvalue()) is not None,
98
'We cleared the stderr without anything to put there')
102
def test_progress_note_clears(self):
103
stderr = test_progress._TTYStringIO()
104
stdout = test_progress._TTYStringIO()
105
# so that we get a TextProgressBar
106
os.environ['TERM'] = 'xterm'
107
ui_factory = _mod_ui_text.TextUIFactory(
108
stdin=tests.StringIOWrapper(''),
109
stdout=stdout, stderr=stderr)
110
self.assertIsInstance(ui_factory._progress_view,
111
_mod_ui_text.TextProgressView)
112
pb = ui_factory.nested_progress_bar()
114
# Create a progress update that isn't throttled
116
result = self.applyDeprecated(deprecated_in((2, 1, 0)),
118
self.assertEqual(None, result)
119
self.assertEqual("t\n", stdout.getvalue())
120
# the exact contents will depend on the terminal width and we don't
121
# care about that right now - but you're probably running it on at
122
# least a 10-character wide terminal :)
123
self.assertContainsRe(stderr.getvalue(), r'\r {10,}\r$')
127
103
def test_text_ui_get_boolean(self):
128
104
stdin = tests.StringIOWrapper("y\n" # True
183
160
factory.get_boolean,
184
161
"what do you want"))
185
162
output = out.getvalue()
186
self.assertContainsRe(factory.stdout.getvalue(),
188
self.assertContainsRe(factory.stdout.getvalue(),
163
self.assertContainsRe(output,
165
self.assertContainsRe(output,
189
166
r"what do you want\? \[y/n\]: what do you want\? \[y/n\]: ")
190
167
# stdin should have been totally consumed
191
168
self.assertEqual('', factory.stdin.readline())
245
222
self.assertIsInstance(ui_factory._progress_view,
246
223
_mod_ui_text.NullProgressView)
225
def test_text_ui_show_user_warning(self):
226
from bzrlib.repofmt.groupcompress_repo import RepositoryFormat2a
227
from bzrlib.repofmt.pack_repo import RepositoryFormatKnitPack5
230
ui = tests.TextUIFactory(stdin=None, stdout=out, stderr=err)
231
remote_fmt = remote.RemoteRepositoryFormat()
232
remote_fmt._network_name = RepositoryFormatKnitPack5().network_name()
233
ui.show_user_warning('cross_format_fetch', from_format=RepositoryFormat2a(),
234
to_format=remote_fmt)
235
self.assertEquals('', out.getvalue())
236
self.assertEquals("Doing on-the-fly conversion from RepositoryFormat2a() to "
237
"RemoteRepositoryFormat(_network_name='Bazaar RepositoryFormatKnitPack5 "
238
"(bzr 1.6)\\n').\nThis may take some time. Upgrade the repositories to "
239
"the same format for better performance.\n",
241
# and now with it suppressed please
244
ui = tests.TextUIFactory(stdin=None, stdout=out, stderr=err)
245
ui.suppressed_warnings.add('cross_format_fetch')
246
ui.show_user_warning('cross_format_fetch', from_format=RepositoryFormat2a(),
247
to_format=remote_fmt)
248
self.assertEquals('', out.getvalue())
249
self.assertEquals('', err.getvalue())
249
252
class TestTextUIOutputStream(tests.TestCase):
250
253
"""Tests for output stream that synchronizes with progress bar."""
356
359
def test_test_ui_factory_progress(self):
357
360
# there's no output; we just want to make sure this doesn't crash -
358
# see https://bugs.edge.launchpad.net/bzr/+bug/408201
361
# see https://bugs.launchpad.net/bzr/+bug/408201
359
362
ui = tests.TestUIFactory()
360
363
pb = ui.nested_progress_bar()
361
364
pb.update('hello')