/brz/remove-bazaar

To get this branch, use:
bzr branch http://gegoxaren.bato24.eu/bzr/brz/remove-bazaar
1551.2.27 by Aaron Bentley
Got propogation under test
1
# Copyright (C) 2006 by Canonical Ltd
2
#
3
# This program is free software; you can redistribute it and/or modify
4
# it under the terms of the GNU General Public License as published by
5
# the Free Software Foundation; either version 2 of the License, or
6
# (at your option) any later version.
7
#
8
# This program is distributed in the hope that it will be useful,
9
# but WITHOUT ANY WARRANTY; without even the implied warranty of
10
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
11
# GNU General Public License for more details.
12
#
13
# You should have received a copy of the GNU General Public License
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
16
1551.2.28 by Aaron Bentley
Ensure all ProgressBar implementations can be used as parents
17
from StringIO import StringIO
18
1773.4.1 by Martin Pool
Add pyflakes makefile target; fix many warnings
19
from bzrlib.progress import (
20
        DummyProgress, ChildProgress,
21
        TTYProgressBar,
22
        DotsProgressBar,
23
        ProgressBarStack,
24
        )
1551.2.27 by Aaron Bentley
Got propogation under test
25
from bzrlib.tests import TestCase
26
1551.2.29 by Aaron Bentley
Got stack handling under test
27
class FakeStack:
28
    def __init__(self, top):
29
        self.__top = top
30
31
    def top(self):
32
        return self.__top
33
1793.1.1 by Aaron Bentley
Hide TTYProgressBars unless they last more than 1 second
34
class InstrumentedProgress(TTYProgressBar):
35
    """TTYProgress variant that tracks outcomes"""
36
37
    def __init__(self, *args, **kwargs):
38
        self.always_throttled = True
39
        TTYProgressBar.__init__(self, *args, **kwargs)
40
41
    def throttle(self, old_message):
42
        result = TTYProgressBar.throttle(self, old_message)
43
        if result is False:
44
            self.always_throttled = False
45
        
46
1551.2.27 by Aaron Bentley
Got propogation under test
47
class TestProgress(TestCase):
48
    def setUp(self):
1551.2.29 by Aaron Bentley
Got stack handling under test
49
        q = DummyProgress()
50
        self.top = ChildProgress(_stack=FakeStack(q))
1551.2.27 by Aaron Bentley
Got propogation under test
51
52
    def test_propogation(self):
53
        self.top.update('foobles', 1, 2)
54
        self.assertEqual(self.top.message, 'foobles')
55
        self.assertEqual(self.top.current, 1)
56
        self.assertEqual(self.top.total, 2)
57
        self.assertEqual(self.top.child_fraction, 0)
1551.2.29 by Aaron Bentley
Got stack handling under test
58
        child = ChildProgress(_stack=FakeStack(self.top))
1551.2.27 by Aaron Bentley
Got propogation under test
59
        child.update('baubles', 2, 4)
60
        self.assertEqual(self.top.message, 'foobles')
61
        self.assertEqual(self.top.current, 1)
62
        self.assertEqual(self.top.total, 2)
63
        self.assertEqual(self.top.child_fraction, 0.5)
1551.2.29 by Aaron Bentley
Got stack handling under test
64
        grandchild = ChildProgress(_stack=FakeStack(child))
1551.2.27 by Aaron Bentley
Got propogation under test
65
        grandchild.update('barbells', 1, 2)
66
        self.assertEqual(self.top.child_fraction, 0.625)
67
        self.assertEqual(child.child_fraction, 0.5)
68
        child.update('baubles', 3, 4)
69
        self.assertEqual(child.child_fraction, 0)
70
        self.assertEqual(self.top.child_fraction, 0.75)
71
        grandchild.update('barbells', 1, 2)
72
        self.assertEqual(self.top.child_fraction, 0.875)
73
        grandchild.update('barbells', 2, 2)
74
        self.assertEqual(self.top.child_fraction, 1)
75
        child.update('baubles', 4, 4)
76
        self.assertEqual(self.top.child_fraction, 1)
77
        #test clamping
78
        grandchild.update('barbells', 2, 2)
79
        self.assertEqual(self.top.child_fraction, 1)
1551.2.28 by Aaron Bentley
Ensure all ProgressBar implementations can be used as parents
80
81
    def test_implementations(self):
82
        for implementation in (TTYProgressBar, DotsProgressBar, 
83
                               DummyProgress):
84
            self.check_parent_handling(implementation)
85
86
    def check_parent_handling(self, parentclass):
87
        top = parentclass(to_file=StringIO())
88
        top.update('foobles', 1, 2)
1551.2.29 by Aaron Bentley
Got stack handling under test
89
        child = ChildProgress(_stack=FakeStack(top))
1551.2.28 by Aaron Bentley
Ensure all ProgressBar implementations can be used as parents
90
        child.update('baubles', 4, 4)
91
        top.update('lala', 2, 2)
92
        child.update('baubles', 4, 4)
1551.2.29 by Aaron Bentley
Got stack handling under test
93
94
    def test_stacking(self):
95
        self.check_stack(TTYProgressBar, ChildProgress)
96
        self.check_stack(DotsProgressBar, ChildProgress)
97
        self.check_stack(DummyProgress, DummyProgress)
98
99
    def check_stack(self, parent_class, child_class):
100
        stack = ProgressBarStack(klass=parent_class, to_file=StringIO())
101
        parent = stack.get_nested()
102
        try:
103
            self.assertIs(parent.__class__, parent_class)
104
            child = stack.get_nested()
105
            try:
106
                self.assertIs(child.__class__, child_class)
107
            finally:
108
                child.finished()
109
        finally:
110
            parent.finished()
1793.1.1 by Aaron Bentley
Hide TTYProgressBars unless they last more than 1 second
111
112
    def test_throttling(self):
113
        pb = InstrumentedProgress(to_file=StringIO())
114
        # instantaneous updates should be squelched
115
        pb.update('me', 1, 1)
116
        self.assertTrue(pb.always_throttled)
117
        pb = InstrumentedProgress(to_file=StringIO())
118
        # It's like an instant sleep(1)!
119
        pb.start_time -= 1
120
        # Updates after a second should not be squelched
121
        pb.update('me', 1, 1)
122
        self.assertFalse(pb.always_throttled)