~bzr-pqm/bzr/bzr.dev

« back to all changes in this revision

Viewing changes to bzrlib/tests/test_progress.py

Merge trunk

Show diffs side-by-side

added added

removed removed

Lines of Context:
1
 
# Copyright (C) 2006 Canonical Ltd
 
1
# Copyright (C) 2006, 2007 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
19
19
 
20
20
from bzrlib import errors
21
21
from bzrlib.progress import (
22
 
        DummyProgress, ChildProgress,
 
22
        DummyProgress,
 
23
        ChildProgress,
23
24
        TTYProgressBar,
24
25
        DotsProgressBar,
25
26
        ProgressBarStack,
 
27
        InstrumentedProgress,
26
28
        )
27
29
from bzrlib.tests import TestCase
28
30
 
29
31
 
30
32
class FakeStack:
 
33
 
31
34
    def __init__(self, top):
32
35
        self.__top = top
33
36
 
34
37
    def top(self):
35
38
        return self.__top
36
39
 
37
 
class InstrumentedProgress(TTYProgressBar):
38
 
    """TTYProgress variant that tracks outcomes"""
39
 
 
40
 
    def __init__(self, *args, **kwargs):
41
 
        self.always_throttled = True
42
 
        TTYProgressBar.__init__(self, *args, **kwargs)
43
 
 
44
 
    def throttle(self, old_message):
45
 
        result = TTYProgressBar.throttle(self, old_message)
46
 
        if result is False:
47
 
            self.always_throttled = False
48
 
        
49
40
 
50
41
class _TTYStringIO(StringIO):
51
42
    """A helper class which makes a StringIO look like a terminal"""
62
53
 
63
54
 
64
55
class TestProgress(TestCase):
 
56
 
65
57
    def setUp(self):
66
58
        q = DummyProgress()
67
59
        self.top = ChildProgress(_stack=FakeStack(q))
243
235
                         '\r                   \r',
244
236
                         out.getvalue())
245
237
 
 
238
    def test_noninteractive_progress(self):
 
239
        out = _NonTTYStringIO()
 
240
        pb = self.get_nested(out, 'xterm')
 
241
        self.assertIsInstance(pb, DummyProgress)
 
242
        try:
 
243
            pb.update('foo', 1, 2)
 
244
            pb.update('bar', 2, 2)
 
245
        finally:
 
246
            pb.finished()
 
247
        self.assertEqual('', out.getvalue())
 
248
 
246
249
    def test_dots_progress(self):
247
 
        # Make sure the ProgressBarStack thinks it is
248
 
        # not writing out to a terminal, and thus uses a 
249
 
        # DotsProgressBar
 
250
        # make sure we get the right progress bar when not on a terminal
250
251
        out = _NonTTYStringIO()
251
 
        pb = self.get_nested(out, 'xterm')
 
252
        pb = self.get_nested(out, 'xterm', 'dots')
252
253
        self.assertIsInstance(pb, DotsProgressBar)
253
254
        try:
254
255
            pb.update('foo', 1, 2)
255
256
            pb.update('bar', 2, 2)
256
257
        finally:
257
258
            pb.finished()
258
 
 
259
259
        self.assertEqual('foo: .'
260
260
                         '\nbar: .'
261
261
                         '\n',
267
267
        out = cStringIO.StringIO()
268
268
        pb = self.get_nested(out, 'xterm')
269
269
        pb.finished()
270
 
        self.assertIsInstance(pb, DotsProgressBar)
 
270
        self.assertIsInstance(pb, DummyProgress)
271
271
 
272
272
    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
275
 
        # Dots
 
273
        # using a terminal that can't do cursor movement
276
274
        out = _TTYStringIO()
277
275
        pb = self.get_nested(out, 'dumb')
278
276
        pb.finished()
279
 
        self.assertIsInstance(pb, DotsProgressBar)
 
277
        self.assertIsInstance(pb, DummyProgress)
280
278
 
281
279
    def test_progress_env_tty(self):
282
280
        # The environ variable BZR_PROGRESS_BAR controls what type of
290
288
        # Even though we are not a tty, the env_var will override
291
289
        self.assertIsInstance(pb, TTYProgressBar)
292
290
 
293
 
    def test_progress_env_dots(self):
294
 
        # Even though we are in a tty, the env_var will override
295
 
        out = _TTYStringIO()
296
 
        pb = self.get_nested(out, 'xterm', 'dots')
297
 
        pb.finished()
298
 
        self.assertIsInstance(pb, DotsProgressBar)
299
 
 
300
291
    def test_progress_env_none(self):
301
292
        # Even though we are in a valid tty, no progress
302
293
        out = _TTYStringIO()