~bzr-pqm/bzr/bzr.dev

« back to all changes in this revision

Viewing changes to bzrlib/tests/test_progress.py

  • Committer: Ian Clatworthy
  • Author(s): Robert Collins
  • Date: 2009-04-03 05:08:45 UTC
  • mfrom: (4241.4.1 bzr.bris-vf)
  • mto: This revision was merged to the branch mainline in revision 4245.
  • Revision ID: ian.clatworthy@canonical.com-20090403050845-w656xtmr19jtuovs
add sha generation support to versioned files (Robert Collins)

Show diffs side-by-side

added added

removed removed

Lines of Context:
1
 
# Copyright (C) 2006 Canonical Ltd
 
1
# Copyright (C) 2006, 2007, 2009 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
12
12
#
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
16
16
 
17
17
import os
18
18
from StringIO import StringIO
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
 
30
from bzrlib.symbol_versioning import (
 
31
    deprecated_in,
 
32
    )
28
33
 
29
34
 
30
35
class FakeStack:
 
36
 
31
37
    def __init__(self, top):
32
38
        self.__top = top
33
39
 
34
40
    def top(self):
35
41
        return self.__top
36
42
 
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
43
 
50
44
class _TTYStringIO(StringIO):
51
45
    """A helper class which makes a StringIO look like a terminal"""
62
56
 
63
57
 
64
58
class TestProgress(TestCase):
 
59
 
65
60
    def setUp(self):
 
61
        TestCase.setUp(self)
66
62
        q = DummyProgress()
67
63
        self.top = ChildProgress(_stack=FakeStack(q))
68
64
 
96
92
        self.assertEqual(self.top.child_fraction, 1)
97
93
 
98
94
    def test_implementations(self):
99
 
        for implementation in (TTYProgressBar, DotsProgressBar, 
 
95
        for implementation in (TTYProgressBar, DotsProgressBar,
100
96
                               DummyProgress):
101
97
            self.check_parent_handling(implementation)
102
98
 
114
110
        self.check_stack(DummyProgress, DummyProgress)
115
111
 
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)),
 
115
            ProgressBarStack,
 
116
            klass=parent_class, to_file=StringIO())
118
117
        parent = stack.get_nested()
119
118
        try:
120
119
            self.assertIs(parent.__class__, parent_class)
220
219
 
221
220
        self.addCleanup(reset)
222
221
 
223
 
        stack = ProgressBarStack(to_file=outf)
 
222
        stack = self.applyDeprecated(
 
223
            deprecated_in((1, 12, 0)),
 
224
            ProgressBarStack,
 
225
            to_file=outf)
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
243
245
                         '\r                   \r',
244
246
                         out.getvalue())
245
247
 
 
248
    def test_noninteractive_progress(self):
 
249
        out = _NonTTYStringIO()
 
250
        pb = self.get_nested(out, 'xterm')
 
251
        self.assertIsInstance(pb, DummyProgress)
 
252
        try:
 
253
            pb.update('foo', 1, 2)
 
254
            pb.update('bar', 2, 2)
 
255
        finally:
 
256
            pb.finished()
 
257
        self.assertEqual('', out.getvalue())
 
258
 
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 
249
 
        # DotsProgressBar
 
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)
253
264
        try:
254
265
            pb.update('foo', 1, 2)
255
266
            pb.update('bar', 2, 2)
256
267
        finally:
257
268
            pb.finished()
258
 
 
259
269
        self.assertEqual('foo: .'
260
270
                         '\nbar: .'
261
271
                         '\n',
267
277
        out = cStringIO.StringIO()
268
278
        pb = self.get_nested(out, 'xterm')
269
279
        pb.finished()
270
 
        self.assertIsInstance(pb, DotsProgressBar)
 
280
        self.assertIsInstance(pb, DummyProgress)
271
281
 
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
275
 
        # Dots
 
283
        # using a terminal that can't do cursor movement
276
284
        out = _TTYStringIO()
277
285
        pb = self.get_nested(out, 'dumb')
278
286
        pb.finished()
279
 
        self.assertIsInstance(pb, DotsProgressBar)
 
287
        self.assertIsInstance(pb, DummyProgress)
280
288
 
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)
292
300
 
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
301
    def test_progress_env_none(self):
301
302
        # Even though we are in a valid tty, no progress
302
303
        out = _TTYStringIO()