~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
# Copyright (C) 2006, 2007, 2009 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


import os
from StringIO import StringIO

from bzrlib import errors
from bzrlib.progress import (
    ProgressTask,
    )
from bzrlib.symbol_versioning import (
    deprecated_in,
    )
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._width = 80
        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_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(
r'[####-               ] 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(
r'[#########\          ] 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(
            r'[####|               ] a:b:c 1/2'
            , view._render_line())