/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 bzrlib/tests/test_ui.py

merge bzr.dev r4042

Show diffs side-by-side

added added

removed removed

Lines of Context:
1
 
# Copyright (C) 2005 Canonical Ltd
 
1
# Copyright (C) 2005, 2008, 2009 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
21
21
from StringIO import StringIO
22
22
import re
23
23
import sys
 
24
import time
24
25
 
25
26
import bzrlib
26
27
import bzrlib.errors as errors
29
30
    ProgressBarStack,
30
31
    TTYProgressBar,
31
32
    )
 
33
from bzrlib.symbol_versioning import (
 
34
    deprecated_in,
 
35
    )
32
36
from bzrlib.tests import (
33
37
    TestCase,
34
38
    TestUIFactory,
101
105
    def test_progress_note(self):
102
106
        stderr = StringIO()
103
107
        stdout = StringIO()
104
 
        ui_factory = TextUIFactory(bar_type=TTYProgressBar)
 
108
        ui_factory = TextUIFactory(stdin=StringIO(''),
 
109
            stderr=stderr,
 
110
            stdout=stdout)
105
111
        pb = ui_factory.nested_progress_bar()
106
112
        try:
107
 
            pb.to_messages_file = stdout
108
 
            ui_factory._progress_bar_stack.bottom().to_file = stderr
109
113
            result = pb.note('t')
110
114
            self.assertEqual(None, result)
111
115
            self.assertEqual("t\n", stdout.getvalue())
122
126
        # The PQM redirects the output to a file, so it
123
127
        # defaults to creating a Dots progress bar. we
124
128
        # need to force it to believe we are a TTY
125
 
        ui_factory = TextUIFactory(bar_type=TTYProgressBar)
 
129
        ui_factory = TextUIFactory(
 
130
            stdin=StringIO(''),
 
131
            stdout=stdout, stderr=stderr)
126
132
        pb = ui_factory.nested_progress_bar()
127
133
        try:
128
 
            pb.to_messages_file = stdout
129
 
            ui_factory._progress_bar_stack.bottom().to_file = stderr
130
134
            # Create a progress update that isn't throttled
131
 
            pb.start_time -= 10
132
135
            pb.update('x', 1, 1)
133
136
            result = pb.note('t')
134
137
            self.assertEqual(None, result)
142
145
 
143
146
    def test_progress_nested(self):
144
147
        # test factory based nested and popping.
145
 
        ui = TextUIFactory()
 
148
        ui = TextUIFactory(None, None, None)
146
149
        pb1 = ui.nested_progress_bar()
147
150
        pb2 = ui.nested_progress_bar()
148
 
        self.assertRaises(errors.MissingProgressBarFinish, pb1.finished)
 
151
        # You do get a warning if the outermost progress bar wasn't finished
 
152
        # first - it's not clear if this is really useful or if it should just
 
153
        # become orphaned -- mbp 20090120
 
154
        warnings, _ = self.callCatchWarnings(pb1.finished)
 
155
        if len(warnings) != 1:
 
156
            self.fail("unexpected warnings: %r" % (warnings,))
149
157
        pb2.finished()
150
158
        pb1.finished()
151
159
 
152
160
    def test_progress_stack(self):
153
 
        # test the progress bar stack which the default text factory 
 
161
        # test the progress bar stack which the default text factory
154
162
        # uses.
155
163
        stderr = StringIO()
156
164
        stdout = StringIO()
157
165
        # make a stack, which accepts parameters like a pb.
158
 
        stack = ProgressBarStack(to_file=stderr, to_messages_file=stdout)
 
166
        stack = self.applyDeprecated(
 
167
            deprecated_in((1, 12, 0)),
 
168
            ProgressBarStack,
 
169
            to_file=stderr, to_messages_file=stdout)
159
170
        # but is not one
160
171
        self.assertFalse(getattr(stack, 'note', False))
161
172
        pb1 = stack.get_nested()
162
173
        pb2 = stack.get_nested()
163
 
        self.assertRaises(errors.MissingProgressBarFinish, pb1.finished)
 
174
        warnings, _ = self.callCatchWarnings(pb1.finished)
 
175
        self.assertEqual(len(warnings), 1)
164
176
        pb2.finished()
165
177
        pb1.finished()
166
178
        # the text ui factory never actually removes the stack once its setup.
167
179
        # we need to be able to nest again correctly from here.
168
180
        pb1 = stack.get_nested()
169
181
        pb2 = stack.get_nested()
170
 
        self.assertRaises(errors.MissingProgressBarFinish, pb1.finished)
 
182
        warnings, _ = self.callCatchWarnings(pb1.finished)
 
183
        self.assertEqual(len(warnings), 1)
171
184
        pb2.finished()
172
185
        pb1.finished()
173
186
 
174
 
    def test_text_factory_setting_progress_bar(self):
175
 
        # we should be able to choose the progress bar type used.
176
 
        factory = TextUIFactory(bar_type=DotsProgressBar)
177
 
        bar = factory.nested_progress_bar()
178
 
        bar.finished()
179
 
        self.assertIsInstance(bar, DotsProgressBar)
180
 
 
181
 
    def test_cli_stdin_is_default_stdin(self):
182
 
        factory = CLIUIFactory()
183
 
        self.assertEqual(sys.stdin, factory.stdin)
184
 
 
185
187
    def assert_get_bool_acceptance_of_user_input(self, factory):
186
188
        factory.stdin = StringIO("y\nyes with garbage\n"
187
189
                                 "yes\nn\nnot an answer\n"
212
214
        self.assertEqual('', factory.stdin.readline())
213
215
 
214
216
    def test_text_ui_getbool(self):
215
 
        factory = TextUIFactory()
 
217
        factory = TextUIFactory(None, None, None)
216
218
        self.assert_get_bool_acceptance_of_user_input(factory)
217
219
 
218
220
    def test_text_factory_prompts_and_clears(self):
219
221
        # a get_boolean call should clear the pb before prompting
220
 
        factory = TextUIFactory(bar_type=DotsProgressBar)
221
 
        factory.stdout = _TTYStringIO()
222
 
        factory.stdin = StringIO("yada\ny\n")
223
 
        pb = self.apply_redirected(factory.stdin, factory.stdout,
224
 
                                   factory.stdout, factory.nested_progress_bar)
225
 
        pb.start_time = None
226
 
        self.apply_redirected(factory.stdin, factory.stdout,
227
 
                              factory.stdout, pb.update, "foo", 0, 1)
 
222
        out = _TTYStringIO()
 
223
        factory = TextUIFactory(stdin=StringIO("yada\ny\n"), stdout=out, stderr=out)
 
224
        pb = factory.nested_progress_bar()
 
225
        pb.show_bar = False
 
226
        pb.show_spinner = False
 
227
        pb.show_count = False
 
228
        pb.update("foo", 0, 1)
228
229
        self.assertEqual(True,
229
230
                         self.apply_redirected(None, factory.stdout,
230
231
                                               factory.stdout,
231
232
                                               factory.get_boolean,
232
233
                                               "what do you want"))
233
 
        output = factory.stdout.getvalue()
234
 
        self.assertEqual("foo: .\n"
235
 
                         "what do you want? [y/n]: what do you want? [y/n]: ",
236
 
                         factory.stdout.getvalue())
237
 
        # stdin should be empty
 
234
        output = out.getvalue()
 
235
        self.assertContainsRe(factory.stdout.getvalue(),
 
236
            "foo *\r\r  *\r*")
 
237
        self.assertContainsRe(factory.stdout.getvalue(),
 
238
            r"what do you want\? \[y/n\]: what do you want\? \[y/n\]: ")
 
239
        # stdin should have been totally consumed
238
240
        self.assertEqual('', factory.stdin.readline())
239
241
 
 
242
    def test_text_tick_after_update(self):
 
243
        ui_factory = TextUIFactory(stdout=StringIO(), stderr=StringIO())
 
244
        pb = ui_factory.nested_progress_bar()
 
245
        try:
 
246
            pb.update('task', 0, 3)
 
247
            # Reset the clock, so that it actually tries to repaint itself
 
248
            ui_factory._progress_view._last_repaint = time.time() - 1.0
 
249
            pb.tick()
 
250
        finally:
 
251
            pb.finished()