/brz/remove-bazaar

To get this branch, use:
bzr branch http://gegoxaren.bato24.eu/bzr/brz/remove-bazaar

« back to all changes in this revision

Viewing changes to brzlib/tests/test_progress.py

  • Committer: Jelmer Vernooij
  • Date: 2017-05-21 12:41:27 UTC
  • mto: This revision was merged to the branch mainline in revision 6623.
  • Revision ID: jelmer@jelmer.uk-20170521124127-iv8etg0vwymyai6y
s/bzr/brz/ in apport config.

Show diffs side-by-side

added added

removed removed

Lines of Context:
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
16
16
 
17
 
import codecs
18
 
import io
19
 
 
20
 
from ..progress import (
 
17
 
 
18
from cStringIO import StringIO
 
19
 
 
20
from brzlib import (
 
21
    tests,
 
22
    )
 
23
from brzlib.progress import (
21
24
    ProgressTask,
22
25
    )
23
 
from ..ui.text import (
 
26
from brzlib.ui.text import (
24
27
    TextProgressView,
25
28
    )
26
29
 
27
 
from .. import (
28
 
    tests,
29
 
    )
30
 
from . import (
31
 
    ui_testing,
32
 
    )
33
 
 
34
30
 
35
31
class TestTextProgressView(tests.TestCase):
36
32
    """Tests for text display of progress bars.
48
44
        return view
49
45
 
50
46
    def make_view(self):
51
 
        out = ui_testing.StringIOWithEncoding()
 
47
        out = StringIO()
52
48
        return out, self.make_view_only(out)
53
49
 
54
50
    def make_task(self, parent_task, view, msg, curr, total):
67
63
        task = self.make_task(None, view, 'reticulating splines', 5, 20)
68
64
        view.show_progress(task)
69
65
        self.assertEqual(
70
 
            '\r/ reticulating splines 5/20                                                    \r', out.getvalue())
 
66
'\r/ reticulating splines 5/20                                                    \r'
 
67
            , out.getvalue())
71
68
        view.clear()
72
69
        self.assertEqual(
73
 
            '\r/ reticulating splines 5/20                                                    \r' +
74
 
            '\r' + 79 * ' ' + '\r',
 
70
'\r/ reticulating splines 5/20                                                    \r'
 
71
            + '\r' + 79 * ' ' + '\r',
75
72
            out.getvalue())
76
73
 
77
74
    def test_render_progress_no_bar(self):
81
78
        task = self.make_task(None, view, 'reticulating splines', 5, 20)
82
79
        view.show_progress(task)
83
80
        self.assertEqual(
84
 
            '\r/ reticulating splines 5/20                                                    \r', out.getvalue())
 
81
'\r/ reticulating splines 5/20                                                    \r'
 
82
            , out.getvalue())
85
83
 
86
84
    def test_render_progress_easy(self):
87
85
        """Just one task and one quarter done"""
90
88
        task = self.make_task(None, view, 'reticulating splines', 5, 20)
91
89
        view.show_progress(task)
92
90
        self.assertEqual(
93
 
            '\r[####/               ] reticulating splines 5/20                               \r', out.getvalue())
 
91
'\r[####/               ] reticulating splines 5/20                               \r'
 
92
            , out.getvalue())
94
93
 
95
94
    def test_render_progress_nested(self):
96
95
        """Tasks proportionally contribute to overall progress"""
102
101
        # so we're in the first half of the main task, and half way through
103
102
        # that
104
103
        self.assertEqual(
105
 
            '[####-               ] reticulating splines:stage2 1/2                         ', view._render_line())
 
104
'[####-               ] reticulating splines:stage2 1/2                         '
 
105
            , 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                         ', view._render_line())
 
110
'[#########\          ] reticulating splines:stage2 2/2                         '
 
111
            , view._render_line())
111
112
 
112
113
    def test_render_progress_sub_nested(self):
113
114
        """Intermediate tasks don't mess up calculation."""
123
124
        # progress indication, just a label; and the bottom one is half done,
124
125
        # so the overall fraction is 1/4
125
126
        self.assertEqual(
126
 
            '[####|               ] a:b:c 1/2                                               ', view._render_line())
 
127
'[####|               ] a:b:c 1/2                                               '
 
128
            , view._render_line())
127
129
 
128
130
    def test_render_truncated(self):
129
131
        # when the bar is too long for the terminal, we prefer not to truncate
134
136
        task_a.update('start_' + 'a' * 200 + '_end', 2000, 5000)
135
137
        line = view._render_line()
136
138
        self.assertEqual(
137
 
            '- start_aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa.. 2000/5000',
138
 
            line)
 
139
'- start_aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa.. 2000/5000',
 
140
           line) 
139
141
        self.assertEqual(len(line), 79)
140
142
 
 
143
 
141
144
    def test_render_with_activity(self):
142
145
        # if the progress view has activity, it's shown before the spinner
143
146
        out, view = self.make_view()
145
148
        view._last_transport_msg = '   123kB   100kB/s '
146
149
        line = view._render_line()
147
150
        self.assertEqual(
148
 
            '   123kB   100kB/s /                                                           ',
149
 
            line)
 
151
'   123kB   100kB/s /                                                           ',
 
152
           line) 
150
153
        self.assertEqual(len(line), 79)
151
154
 
152
155
        task_a.update('start_' + 'a' * 200 + '_end', 2000, 5000)
153
156
        view._last_transport_msg = '   123kB   100kB/s '
154
157
        line = view._render_line()
155
158
        self.assertEqual(
156
 
            '   123kB   100kB/s \\ start_aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa.. 2000/5000',
157
 
            line)
 
159
'   123kB   100kB/s \\ start_aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa.. 2000/5000',
 
160
           line) 
158
161
        self.assertEqual(len(line), 79)
159
162
 
160
163
    def test_render_progress_unicode_enc_utf8(self):
161
 
        out = ui_testing.StringIOWithEncoding()
 
164
        out = tests.StringIOWrapper()
162
165
        out.encoding = "utf-8"
163
166
        view = self.make_view_only(out, 20)
164
167
        task = self.make_task(None, view, u"\xa7", 0, 1)
165
168
        view.show_progress(task)
166
 
        self.assertEqual(u'\r/ \xa7 0/1            \r', out.getvalue())
 
169
        self.assertEqual('\r/ \xc2\xa7 0/1            \r',
 
170
            out.getvalue())
167
171
 
168
172
    def test_render_progress_unicode_enc_missing(self):
169
 
        out = codecs.getwriter("ascii")(io.BytesIO())
 
173
        out = StringIO()
170
174
        self.assertRaises(AttributeError, getattr, out, "encoding")
171
175
        view = self.make_view_only(out, 20)
172
176
        task = self.make_task(None, view, u"\xa7", 0, 1)
173
177
        view.show_progress(task)
174
 
        self.assertEqual(b'\r/ ? 0/1             \r', out.getvalue())
 
178
        self.assertEqual('\r/ ? 0/1             \r',
 
179
            out.getvalue())
175
180
 
176
181
    def test_render_progress_unicode_enc_none(self):
177
 
        out = ui_testing.StringIOWithEncoding()
 
182
        out = tests.StringIOWrapper()
178
183
        out.encoding = None
179
184
        view = self.make_view_only(out, 20)
180
185
        task = self.make_task(None, view, u"\xa7", 0, 1)
181
186
        view.show_progress(task)
182
 
        self.assertEqual(u'\r/ ? 0/1             \r', out.getvalue())
 
187
        self.assertEqual('\r/ ? 0/1             \r',
 
188
            out.getvalue())