24
from StringIO import StringIO
26
24
from bzrlib import (
34
29
from bzrlib.symbol_versioning import (
37
from bzrlib.tests import (
32
from bzrlib.tests import test_progress
41
33
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)
57
36
class TestTextUIFactory(tests.TestCase):
59
38
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
def test_progress_nested(self):
128
# test factory based nested and popping.
129
ui = _mod_ui_text.TextUIFactory(None, None, None)
130
pb1 = ui.nested_progress_bar()
131
pb2 = ui.nested_progress_bar()
132
# You do get a warning if the outermost progress bar wasn't finished
133
# first - it's not clear if this is really useful or if it should just
134
# become orphaned -- mbp 20090120
135
warnings, _ = self.callCatchWarnings(pb1.finished)
136
if len(warnings) != 1:
137
self.fail("unexpected warnings: %r" % (warnings,))
103
141
def test_text_ui_get_boolean(self):
104
142
stdin = tests.StringIOWrapper("y\n" # True
160
197
factory.get_boolean,
161
198
"what do you want"))
162
199
output = out.getvalue()
163
self.assertContainsRe(output,
165
self.assertContainsRe(output,
200
self.assertContainsRe(factory.stdout.getvalue(),
202
self.assertContainsRe(factory.stdout.getvalue(),
166
203
r"what do you want\? \[y/n\]: what do you want\? \[y/n\]: ")
167
204
# stdin should have been totally consumed
168
205
self.assertEqual('', factory.stdin.readline())
222
259
self.assertIsInstance(ui_factory._progress_view,
223
260
_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())
252
263
class TestTextUIOutputStream(tests.TestCase):
253
264
"""Tests for output stream that synchronizes with progress bar."""