~bzr-pqm/bzr/bzr.dev

« back to all changes in this revision

Viewing changes to bzrlib/tests/test_progress.py

merge only needs a lock_tree_write() on the working tree, not a full lock_write()

Show diffs side-by-side

added added

removed removed

Lines of Context:
1
 
# Copyright (C) 2006, 2007, 2009 Canonical Ltd
 
1
# Copyright (C) 2006 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,
23
 
        ChildProgress,
 
22
        DummyProgress, ChildProgress,
24
23
        TTYProgressBar,
25
24
        DotsProgressBar,
26
25
        ProgressBarStack,
27
 
        InstrumentedProgress,
28
26
        )
29
27
from bzrlib.tests import TestCase
30
 
from bzrlib.symbol_versioning import (
31
 
    deprecated_in,
32
 
    )
33
28
 
34
29
 
35
30
class FakeStack:
36
 
 
37
31
    def __init__(self, top):
38
32
        self.__top = top
39
33
 
40
34
    def top(self):
41
35
        return self.__top
42
36
 
 
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
        
43
49
 
44
50
class _TTYStringIO(StringIO):
45
51
    """A helper class which makes a StringIO look like a terminal"""
56
62
 
57
63
 
58
64
class TestProgress(TestCase):
59
 
 
60
65
    def setUp(self):
61
66
        q = DummyProgress()
62
67
        self.top = ChildProgress(_stack=FakeStack(q))
109
114
        self.check_stack(DummyProgress, DummyProgress)
110
115
 
111
116
    def check_stack(self, parent_class, child_class):
112
 
        stack = self.applyDeprecated(
113
 
            deprecated_in((1, 12, 0)),
114
 
            ProgressBarStack,
115
 
            klass=parent_class, to_file=StringIO())
 
117
        stack = ProgressBarStack(klass=parent_class, to_file=StringIO())
116
118
        parent = stack.get_nested()
117
119
        try:
118
120
            self.assertIs(parent.__class__, parent_class)
218
220
 
219
221
        self.addCleanup(reset)
220
222
 
221
 
        stack = self.applyDeprecated(
222
 
            deprecated_in((1, 12, 0)),
223
 
            ProgressBarStack,
224
 
            to_file=outf)
 
223
        stack = ProgressBarStack(to_file=outf)
225
224
        pb = stack.get_nested()
226
225
        pb.start_time -= 1 # Make sure it is ready to write
227
226
        pb.width = 20 # And it is of reasonable size
244
243
                         '\r                   \r',
245
244
                         out.getvalue())
246
245
 
247
 
    def test_noninteractive_progress(self):
248
 
        out = _NonTTYStringIO()
249
 
        pb = self.get_nested(out, 'xterm')
250
 
        self.assertIsInstance(pb, DummyProgress)
251
 
        try:
252
 
            pb.update('foo', 1, 2)
253
 
            pb.update('bar', 2, 2)
254
 
        finally:
255
 
            pb.finished()
256
 
        self.assertEqual('', out.getvalue())
257
 
 
258
246
    def test_dots_progress(self):
259
 
        # 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 
 
249
        # DotsProgressBar
260
250
        out = _NonTTYStringIO()
261
 
        pb = self.get_nested(out, 'xterm', 'dots')
 
251
        pb = self.get_nested(out, 'xterm')
262
252
        self.assertIsInstance(pb, DotsProgressBar)
263
253
        try:
264
254
            pb.update('foo', 1, 2)
265
255
            pb.update('bar', 2, 2)
266
256
        finally:
267
257
            pb.finished()
 
258
 
268
259
        self.assertEqual('foo: .'
269
260
                         '\nbar: .'
270
261
                         '\n',
276
267
        out = cStringIO.StringIO()
277
268
        pb = self.get_nested(out, 'xterm')
278
269
        pb.finished()
279
 
        self.assertIsInstance(pb, DummyProgress)
 
270
        self.assertIsInstance(pb, DotsProgressBar)
280
271
 
281
272
    def test_dumb_progress(self):
282
 
        # 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
 
275
        # Dots
283
276
        out = _TTYStringIO()
284
277
        pb = self.get_nested(out, 'dumb')
285
278
        pb.finished()
286
 
        self.assertIsInstance(pb, DummyProgress)
 
279
        self.assertIsInstance(pb, DotsProgressBar)
287
280
 
288
281
    def test_progress_env_tty(self):
289
282
        # The environ variable BZR_PROGRESS_BAR controls what type of
297
290
        # Even though we are not a tty, the env_var will override
298
291
        self.assertIsInstance(pb, TTYProgressBar)
299
292
 
 
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
300
    def test_progress_env_none(self):
301
301
        # Even though we are in a valid tty, no progress
302
302
        out = _TTYStringIO()