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