~bzr-pqm/bzr/bzr.dev

« back to all changes in this revision

Viewing changes to bzrlib/tests/test_ui.py

  • Committer: Robert Collins
  • Date: 2007-03-08 04:06:06 UTC
  • mfrom: (2323.1.1 integration)
  • mto: This revision was merged to the branch mainline in revision 2442.
  • Revision ID: robertc@robertcollins.net-20070308040606-84gsniv56huiyjt4
Merge bzr.dev.

Show diffs side-by-side

added added

removed removed

Lines of Context:
1
 
# Copyright (C) 2005 by Canonical Ltd
 
1
# Copyright (C) 2005 Canonical Ltd
2
2
#
3
3
# This program is free software; you can redistribute it and/or modify
4
4
# it under the terms of the GNU General Public License as published by
26
26
import bzrlib.errors as errors
27
27
from bzrlib.progress import TTYProgressBar, ProgressBarStack
28
28
from bzrlib.tests import TestCase
 
29
from bzrlib.tests.test_progress import _TTYStringIO
29
30
from bzrlib.ui import SilentUIFactory
30
31
from bzrlib.ui.text import TextUIFactory
31
32
 
63
64
    def test_progress_note(self):
64
65
        stderr = StringIO()
65
66
        stdout = StringIO()
66
 
        ui_factory = TextUIFactory()
67
 
        pb = ui_factory.nested_progress_bar()
68
 
        try:
69
 
            pb.to_messages_file = stdout
70
 
            ui_factory._progress_bar_stack.bottom().to_file = stderr
 
67
        ui_factory = TextUIFactory(bar_type=TTYProgressBar)
 
68
        pb = ui_factory.nested_progress_bar()
 
69
        try:
 
70
            pb.to_messages_file = stdout
 
71
            ui_factory._progress_bar_stack.bottom().to_file = stderr
 
72
            result = pb.note('t')
 
73
            self.assertEqual(None, result)
 
74
            self.assertEqual("t\n", stdout.getvalue())
 
75
            # Since there was no update() call, there should be no clear() call
 
76
            self.failIf(re.search(r'^\r {10,}\r$', stderr.getvalue()) is not None,
 
77
                        'We cleared the stderr without anything to put there')
 
78
        finally:
 
79
            pb.finished()
 
80
 
 
81
    def test_progress_note_clears(self):
 
82
        stderr = StringIO()
 
83
        stdout = StringIO()
 
84
        # The PQM redirects the output to a file, so it
 
85
        # defaults to creating a Dots progress bar. we
 
86
        # need to force it to believe we are a TTY
 
87
        ui_factory = TextUIFactory(bar_type=TTYProgressBar)
 
88
        pb = ui_factory.nested_progress_bar()
 
89
        try:
 
90
            pb.to_messages_file = stdout
 
91
            ui_factory._progress_bar_stack.bottom().to_file = stderr
 
92
            # Create a progress update that isn't throttled
 
93
            pb.start_time -= 10
 
94
            pb.update('x', 1, 1)
71
95
            result = pb.note('t')
72
96
            self.assertEqual(None, result)
73
97
            self.assertEqual("t\n", stdout.getvalue())
74
98
            # the exact contents will depend on the terminal width and we don't
75
99
            # care about that right now - but you're probably running it on at
76
100
            # least a 10-character wide terminal :)
77
 
            self.assertContainsRe(stderr.getvalue(), r'^\r {10,}\r$')
 
101
            self.assertContainsRe(stderr.getvalue(), r'\r {10,}\r$')
78
102
        finally:
79
103
            pb.finished()
80
104
 
153
177
    def test_text_factory_prompts_and_clears(self):
154
178
        # a get_boolean call should clear the pb before prompting
155
179
        factory = bzrlib.ui.text.TextUIFactory()
156
 
        factory.stdout = StringIO()
 
180
        factory.stdout = _TTYStringIO()
157
181
        factory.stdin = StringIO("yada\ny\n")
158
182
        pb = self.apply_redirected(
159
183
            factory.stdin, factory.stdout, factory.stdout, factory.nested_progress_bar)
 
184
        pb.start_time = None
160
185
        self.apply_redirected(
161
186
            factory.stdin, factory.stdout, factory.stdout, pb.update, "foo", 0, 1)
162
187
        self.assertEqual(
164
189
            self.apply_redirected(
165
190
                None, factory.stdout, factory.stdout, factory.get_boolean, "what do you want")
166
191
            )
 
192
        # FIXME: This assumes the factory's going to produce a spinner-style
 
193
        # progress bar, but it won't if this is run from a dumb terminal (e.g.
 
194
        # from inside gvim.) -- mbp 20061014
 
195
        #
167
196
        # use a regular expression so that we don't depend on the particular
168
197
        # screen width - could also set and restore $COLUMN if that has
169
198
        # priority on all platforms, but it doesn't at present.
173
202
            "\r   *" 
174
203
            "\rwhat do you want\\? \\[y/n\\]:what do you want\\? \\[y/n\\]:", 
175
204
            output):
176
 
            self.fail("didn't match factory output %r" % factory)
 
205
            self.fail("didn't match factory output %r, %r" % (factory, output))