1
# Copyright (C) 2006, 2007, 2009 Canonical Ltd
1
# Copyright (C) 2006 by Canonical Ltd
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
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., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
15
# Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
18
18
from StringIO import StringIO
20
20
from bzrlib import errors
21
21
from bzrlib.progress import (
22
DummyProgress, ChildProgress,
29
27
from bzrlib.tests import TestCase
30
from bzrlib.symbol_versioning import (
37
31
def __init__(self, top):
37
class InstrumentedProgress(TTYProgressBar):
38
"""TTYProgress variant that tracks outcomes"""
40
def __init__(self, *args, **kwargs):
41
self.always_throttled = True
42
TTYProgressBar.__init__(self, *args, **kwargs)
44
def throttle(self, old_message):
45
result = TTYProgressBar.throttle(self, old_message)
47
self.always_throttled = False
44
50
class _TTYStringIO(StringIO):
45
51
"""A helper class which makes a StringIO look like a terminal"""
92
96
self.assertEqual(self.top.child_fraction, 1)
94
98
def test_implementations(self):
95
for implementation in (TTYProgressBar, DotsProgressBar,
99
for implementation in (TTYProgressBar, DotsProgressBar,
97
101
self.check_parent_handling(implementation)
110
114
self.check_stack(DummyProgress, DummyProgress)
112
116
def check_stack(self, parent_class, child_class):
113
stack = self.applyDeprecated(
114
deprecated_in((1, 12, 0)),
116
klass=parent_class, to_file=StringIO())
117
stack = ProgressBarStack(klass=parent_class, to_file=StringIO())
117
118
parent = stack.get_nested()
119
120
self.assertIs(parent.__class__, parent_class)
220
221
self.addCleanup(reset)
222
stack = self.applyDeprecated(
223
deprecated_in((1, 12, 0)),
223
stack = ProgressBarStack(to_file=outf)
226
224
pb = stack.get_nested()
227
225
pb.start_time -= 1 # Make sure it is ready to write
228
226
pb.width = 20 # And it is of reasonable size
248
def test_noninteractive_progress(self):
249
out = _NonTTYStringIO()
250
pb = self.get_nested(out, 'xterm')
251
self.assertIsInstance(pb, DummyProgress)
253
pb.update('foo', 1, 2)
254
pb.update('bar', 2, 2)
257
self.assertEqual('', out.getvalue())
259
246
def test_dots_progress(self):
260
# make sure we get the right progress bar when not on a terminal
247
# Make sure the ProgressBarStack thinks it is
248
# not writing out to a terminal, and thus uses a
261
250
out = _NonTTYStringIO()
262
pb = self.get_nested(out, 'xterm', 'dots')
251
pb = self.get_nested(out, 'xterm')
263
252
self.assertIsInstance(pb, DotsProgressBar)
265
254
pb.update('foo', 1, 2)
266
255
pb.update('bar', 2, 2)
269
259
self.assertEqual('foo: .'
277
267
out = cStringIO.StringIO()
278
268
pb = self.get_nested(out, 'xterm')
280
self.assertIsInstance(pb, DummyProgress)
270
self.assertIsInstance(pb, DotsProgressBar)
282
272
def test_dumb_progress(self):
283
# using a terminal that can't do cursor movement
273
# Make sure the ProgressBarStack thinks it is writing out to a
274
# terminal, but it is the emacs 'dumb' terminal, so it uses
284
276
out = _TTYStringIO()
285
277
pb = self.get_nested(out, 'dumb')
287
self.assertIsInstance(pb, DummyProgress)
279
self.assertIsInstance(pb, DotsProgressBar)
289
281
def test_progress_env_tty(self):
290
282
# The environ variable BZR_PROGRESS_BAR controls what type of
298
290
# Even though we are not a tty, the env_var will override
299
291
self.assertIsInstance(pb, TTYProgressBar)
293
def test_progress_env_dots(self):
294
# Even though we are in a tty, the env_var will override
296
pb = self.get_nested(out, 'xterm', 'dots')
298
self.assertIsInstance(pb, DotsProgressBar)
301
300
def test_progress_env_none(self):
302
301
# Even though we are in a valid tty, no progress
303
302
out = _TTYStringIO()