~bzr-pqm/bzr/bzr.dev

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
# Copyright (C) 2006-2011 Canonical Ltd
#
# This program is free software; you can redistribute it and/or modify
# it under the terms of the GNU General Public License as published by
# the Free Software Foundation; either version 2 of the License, or
# (at your option) any later version.
#
# This program is distributed in the hope that it will be useful,
# but WITHOUT ANY WARRANTY; without even the implied warranty of
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
# GNU General Public License for more details.
#
# You should have received a copy of the GNU General Public License
# along with this program; if not, write to the Free Software
# Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA


from StringIO import StringIO

from bzrlib.progress import (
    ProgressTask,
    )
from bzrlib.tests import TestCase
from bzrlib.ui.text import (
    TextProgressView,
    )


class _TTYStringIO(StringIO):
    """A helper class which makes a StringIO look like a terminal"""

    def isatty(self):
        return True


class _NonTTYStringIO(StringIO):
    """Helper that implements isatty() but returns False"""

    def isatty(self):
        return False


class TestTextProgressView(TestCase):
    """Tests for text display of progress bars.

    These try to exercise the progressview independently of its construction,
    which is arranged by the TextUIFactory.
    """
    # The ProgressTask now connects directly to the ProgressView, so we can
    # check them independently of the factory or of the determination of what
    # view to use.
    
    def make_view(self):
        out = StringIO()
        view = TextProgressView(out)
        view._avail_width = lambda: 79
        return out, view
    
    def make_task(self, parent_task, view, msg, curr, total):
        # would normally be done by UIFactory; is done here so that we don't
        # have to have one.
        task = ProgressTask(parent_task, progress_view=view)
        task.msg = msg
        task.current_cnt = curr
        task.total_cnt = total
        return task

    def test_clear(self):
        # <https://bugs.launchpad.net/bzr/+bug/611127> clear must actually
        # send spaces to clear the line
        out, view = self.make_view()
        task = self.make_task(None, view, 'reticulating splines', 5, 20)
        view.show_progress(task)
        self.assertEqual(
'\r/ reticulating splines 5/20                                                    \r'
            , out.getvalue())
        view.clear()
        self.assertEqual(
'\r/ reticulating splines 5/20                                                    \r'
            + '\r' + 79 * ' ' + '\r',
            out.getvalue())

    def test_render_progress_no_bar(self):
        """The default view now has a spinner but no bar."""
        out, view = self.make_view()
        # view.enable_bar = False
        task = self.make_task(None, view, 'reticulating splines', 5, 20)
        view.show_progress(task)
        self.assertEqual(
'\r/ reticulating splines 5/20                                                    \r'
            , out.getvalue())

    def test_render_progress_easy(self):
        """Just one task and one quarter done"""
        out, view = self.make_view()
        view.enable_bar = True
        task = self.make_task(None, view, 'reticulating splines', 5, 20)
        view.show_progress(task)
        self.assertEqual(
'\r[####/               ] reticulating splines 5/20                               \r'
            , out.getvalue())

    def test_render_progress_nested(self):
        """Tasks proportionally contribute to overall progress"""
        out, view = self.make_view()
        task = self.make_task(None, view, 'reticulating splines', 0, 2)
        task2 = self.make_task(task, view, 'stage2', 1, 2)
        view.show_progress(task2)
        view.enable_bar = True
        # so we're in the first half of the main task, and half way through
        # that
        self.assertEqual(
'[####-               ] reticulating splines:stage2 1/2                         '
            , view._render_line())
        # if the nested task is complete, then we're all the way through the
        # first half of the overall work
        task2.update('stage2', 2, 2)
        self.assertEqual(
'[#########\          ] reticulating splines:stage2 2/2                         '
            , view._render_line())

    def test_render_progress_sub_nested(self):
        """Intermediate tasks don't mess up calculation."""
        out, view = self.make_view()
        view.enable_bar = True
        task_a = ProgressTask(None, progress_view=view)
        task_a.update('a', 0, 2)
        task_b = ProgressTask(task_a, progress_view=view)
        task_b.update('b')
        task_c = ProgressTask(task_b, progress_view=view)
        task_c.update('c', 1, 2)
        # the top-level task is in its first half; the middle one has no
        # progress indication, just a label; and the bottom one is half done,
        # so the overall fraction is 1/4
        self.assertEqual(
'[####|               ] a:b:c 1/2                                               '
            , view._render_line())

    def test_render_truncated(self):
        # when the bar is too long for the terminal, we prefer not to truncate
        # the counters because they might be interesting, and because
        # truncating the numbers might be misleading
        out, view = self.make_view()
        task_a = ProgressTask(None, progress_view=view)
        task_a.update('start_' + 'a' * 200 + '_end', 2000, 5000)
        line = view._render_line()
        self.assertEqual(
'- start_aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa.. 2000/5000',
           line) 
        self.assertEqual(len(line), 79)


    def test_render_with_activity(self):
        # if the progress view has activity, it's shown before the spinner
        out, view = self.make_view()
        task_a = ProgressTask(None, progress_view=view)
        view._last_transport_msg = '   123kB   100kB/s '
        line = view._render_line()
        self.assertEqual(
'   123kB   100kB/s /                                                           ',
           line) 
        self.assertEqual(len(line), 79)

        task_a.update('start_' + 'a' * 200 + '_end', 2000, 5000)
        view._last_transport_msg = '   123kB   100kB/s '
        line = view._render_line()
        self.assertEqual(
'   123kB   100kB/s \\ start_aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa.. 2000/5000',
           line) 
        self.assertEqual(len(line), 79)