14
14
# along with this program; if not, write to the Free Software
15
15
# Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
18
from cStringIO import StringIO
23
from bzrlib.progress import (
20
from ..progress import (
26
from bzrlib.ui.text import (
23
from ..ui.text import (
31
35
class TestTextProgressView(tests.TestCase):
32
36
"""Tests for text display of progress bars.
46
50
def make_view(self):
51
out = ui_testing.StringIOWithEncoding()
48
52
return out, self.make_view_only(out)
50
54
def make_task(self, parent_task, view, msg, curr, total):
63
67
task = self.make_task(None, view, 'reticulating splines', 5, 20)
64
68
view.show_progress(task)
66
'\r/ reticulating splines 5/20 \r'
70
'\r/ reticulating splines 5/20 \r', out.getvalue())
70
'\r/ reticulating splines 5/20 \r'
71
+ '\r' + 79 * ' ' + '\r',
73
'\r/ reticulating splines 5/20 \r' +
74
'\r' + 79 * ' ' + '\r',
74
77
def test_render_progress_no_bar(self):
78
81
task = self.make_task(None, view, 'reticulating splines', 5, 20)
79
82
view.show_progress(task)
81
'\r/ reticulating splines 5/20 \r'
84
'\r/ reticulating splines 5/20 \r', out.getvalue())
84
86
def test_render_progress_easy(self):
85
87
"""Just one task and one quarter done"""
88
90
task = self.make_task(None, view, 'reticulating splines', 5, 20)
89
91
view.show_progress(task)
91
'\r[####/ ] reticulating splines 5/20 \r'
93
'\r[####/ ] reticulating splines 5/20 \r', out.getvalue())
94
95
def test_render_progress_nested(self):
95
96
"""Tasks proportionally contribute to overall progress"""
101
102
# so we're in the first half of the main task, and half way through
103
104
self.assertEqual(
104
'[####- ] reticulating splines:stage2 1/2 '
105
, view._render_line())
105
'[####- ] reticulating splines:stage2 1/2 ', view._render_line())
106
106
# if the nested task is complete, then we're all the way through the
107
107
# first half of the overall work
108
108
task2.update('stage2', 2, 2)
109
109
self.assertEqual(
110
'[#########\ ] reticulating splines:stage2 2/2 '
111
, view._render_line())
110
'[#########\\ ] reticulating splines:stage2 2/2 ', view._render_line())
113
112
def test_render_progress_sub_nested(self):
114
113
"""Intermediate tasks don't mess up calculation."""
124
123
# progress indication, just a label; and the bottom one is half done,
125
124
# so the overall fraction is 1/4
126
125
self.assertEqual(
127
'[####| ] a:b:c 1/2 '
128
, view._render_line())
126
'[####| ] a:b:c 1/2 ', view._render_line())
130
128
def test_render_truncated(self):
131
129
# when the bar is too long for the terminal, we prefer not to truncate
136
134
task_a.update('start_' + 'a' * 200 + '_end', 2000, 5000)
137
135
line = view._render_line()
138
136
self.assertEqual(
139
'- start_aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa.. 2000/5000',
137
'- start_aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa.. 2000/5000',
141
139
self.assertEqual(len(line), 79)
144
141
def test_render_with_activity(self):
145
142
# if the progress view has activity, it's shown before the spinner
146
143
out, view = self.make_view()
148
145
view._last_transport_msg = ' 123kB 100kB/s '
149
146
line = view._render_line()
150
147
self.assertEqual(
153
150
self.assertEqual(len(line), 79)
155
152
task_a.update('start_' + 'a' * 200 + '_end', 2000, 5000)
156
153
view._last_transport_msg = ' 123kB 100kB/s '
157
154
line = view._render_line()
158
155
self.assertEqual(
159
' 123kB 100kB/s \\ start_aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa.. 2000/5000',
156
' 123kB 100kB/s \\ start_aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa.. 2000/5000',
161
158
self.assertEqual(len(line), 79)
163
160
def test_render_progress_unicode_enc_utf8(self):
164
out = tests.StringIOWrapper()
161
out = ui_testing.StringIOWithEncoding()
165
162
out.encoding = "utf-8"
166
163
view = self.make_view_only(out, 20)
167
164
task = self.make_task(None, view, u"\xa7", 0, 1)
168
165
view.show_progress(task)
169
self.assertEqual('\r/ \xc2\xa7 0/1 \r',
166
self.assertEqual(u'\r/ \xa7 0/1 \r', out.getvalue())
172
168
def test_render_progress_unicode_enc_missing(self):
169
out = codecs.getwriter("ascii")(io.BytesIO())
174
170
self.assertRaises(AttributeError, getattr, out, "encoding")
175
171
view = self.make_view_only(out, 20)
176
172
task = self.make_task(None, view, u"\xa7", 0, 1)
177
173
view.show_progress(task)
178
self.assertEqual('\r/ ? 0/1 \r',
174
self.assertEqual(b'\r/ ? 0/1 \r', out.getvalue())
181
176
def test_render_progress_unicode_enc_none(self):
182
out = tests.StringIOWrapper()
177
out = ui_testing.StringIOWithEncoding()
183
178
out.encoding = None
184
179
view = self.make_view_only(out, 20)
185
180
task = self.make_task(None, view, u"\xa7", 0, 1)
186
181
view.show_progress(task)
187
self.assertEqual('\r/ ? 0/1 \r',
182
self.assertEqual(u'\r/ ? 0/1 \r', out.getvalue())