~bzr-pqm/bzr/bzr.dev

« back to all changes in this revision

Viewing changes to bzrlib/tests/test_ui.py

  • Committer: John Arbash Meinel
  • Date: 2008-07-11 21:41:24 UTC
  • mto: This revision was merged to the branch mainline in revision 3543.
  • Revision ID: john@arbash-meinel.com-20080711214124-qi09irlj7pd5cuzg
Shortcut the case when one revision is in the ancestry of the other.

At the cost of a heads() check, when one parent supersedes, we don't have to extract
the text for the other. Changes merge time from 3m37s => 3m21s. Using a
CachingParentsProvider would drop the time down to 3m11s.

Show diffs side-by-side

added added

removed removed

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