~bzr-pqm/bzr/bzr.dev

« back to all changes in this revision

Viewing changes to bzrlib/tests/test_ui.py

  • Committer: Aaron Bentley
  • Date: 2007-03-12 19:56:41 UTC
  • mto: (1551.19.24 Aaron's mergeable stuff)
  • mto: This revision was merged to the branch mainline in revision 2353.
  • Revision ID: abentley@panoramicfeedback.com-20070312195641-ezjnseqwgjtkh0iu
merge3 auto-detects line endings for conflict markers

Show diffs side-by-side

added added

removed removed

Lines of Context:
 
1
# Copyright (C) 2005 Canonical Ltd
 
2
#
 
3
# This program is free software; you can redistribute it and/or modify
 
4
# it under the terms of the GNU General Public License as published by
 
5
# the Free Software Foundation; either version 2 of the License, or
 
6
# (at your option) any later version.
 
7
#
 
8
# This program is distributed in the hope that it will be useful,
 
9
# but WITHOUT ANY WARRANTY; without even the implied warranty of
 
10
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 
11
# GNU General Public License for more details.
 
12
#
 
13
# You should have received a copy of the GNU General Public License
 
14
# along with this program; if not, write to the Free Software
 
15
# Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307  USA
 
16
 
 
17
"""Tests for the bzrlib ui
 
18
"""
 
19
 
 
20
import os
 
21
from StringIO import StringIO
 
22
import re
 
23
import sys
 
24
 
 
25
import bzrlib
 
26
import bzrlib.errors as errors
 
27
from bzrlib.progress import DotsProgressBar, TTYProgressBar, ProgressBarStack
 
28
from bzrlib.tests import (
 
29
    TestUIFactory,
 
30
    StringIOWrapper,
 
31
    TestCase,
 
32
    )
 
33
from bzrlib.tests.test_progress import _TTYStringIO
 
34
from bzrlib.ui import SilentUIFactory
 
35
from bzrlib.ui.text import TextUIFactory
 
36
 
 
37
 
 
38
class UITests(TestCase):
 
39
 
 
40
    def test_silent_factory(self):
 
41
        ui = SilentUIFactory()
 
42
        stdout = StringIO()
 
43
        self.assertEqual(None,
 
44
                         self.apply_redirected(None, stdout, stdout,
 
45
                                               ui.get_password))
 
46
        self.assertEqual('', stdout.getvalue())
 
47
        self.assertEqual(None,
 
48
                         self.apply_redirected(None, stdout, stdout,
 
49
                                               ui.get_password,
 
50
                                               u'Hello\u1234 %(user)s',
 
51
                                               user=u'some\u1234'))
 
52
        self.assertEqual('', stdout.getvalue())
 
53
 
 
54
    def test_text_factory_ascii_password(self):
 
55
        ui = TestUIFactory(stdin='secret\n', stdout=StringIOWrapper())
 
56
        pb = ui.nested_progress_bar()
 
57
        try:
 
58
            self.assertEqual('secret',
 
59
                             self.apply_redirected(ui.stdin, ui.stdout,
 
60
                                                   ui.stdout,
 
61
                                                   ui.get_password))
 
62
            # ': ' is appended to prompt
 
63
            self.assertEqual(': ', ui.stdout.getvalue())
 
64
        finally:
 
65
            pb.finished()
 
66
 
 
67
    def test_text_factory_utf8_password(self):
 
68
        """Test an utf8 password.
 
69
 
 
70
        We can't predict what encoding users will have for stdin, so we force
 
71
        it to utf8 to test that we transport the password correctly.
 
72
        """
 
73
        ui = TestUIFactory(stdin=u'baz\u1234'.encode('utf8'),
 
74
                           stdout=StringIOWrapper())
 
75
        ui.stdin.encoding = 'utf8'
 
76
        ui.stdout.encoding = ui.stdin.encoding
 
77
        pb = ui.nested_progress_bar()
 
78
        try:
 
79
            password = self.apply_redirected(ui.stdin, ui.stdout, ui.stdout,
 
80
                                             ui.get_password,
 
81
                                             u'Hello \u1234 %(user)s',
 
82
                                             user=u'some\u1234')
 
83
            # We use StringIO objects, we need to decode them
 
84
            self.assertEqual(u'baz\u1234', password.decode('utf8'))
 
85
            self.assertEqual(u'Hello \u1234 some\u1234: ',
 
86
                             ui.stdout.getvalue().decode('utf8'))
 
87
        finally:
 
88
            pb.finished()
 
89
 
 
90
    def test_progress_note(self):
 
91
        stderr = StringIO()
 
92
        stdout = StringIO()
 
93
        ui_factory = TextUIFactory(bar_type=TTYProgressBar)
 
94
        pb = ui_factory.nested_progress_bar()
 
95
        try:
 
96
            pb.to_messages_file = stdout
 
97
            ui_factory._progress_bar_stack.bottom().to_file = stderr
 
98
            result = pb.note('t')
 
99
            self.assertEqual(None, result)
 
100
            self.assertEqual("t\n", stdout.getvalue())
 
101
            # Since there was no update() call, there should be no clear() call
 
102
            self.failIf(re.search(r'^\r {10,}\r$', stderr.getvalue()) is not None,
 
103
                        'We cleared the stderr without anything to put there')
 
104
        finally:
 
105
            pb.finished()
 
106
 
 
107
    def test_progress_note_clears(self):
 
108
        stderr = StringIO()
 
109
        stdout = StringIO()
 
110
        # The PQM redirects the output to a file, so it
 
111
        # defaults to creating a Dots progress bar. we
 
112
        # need to force it to believe we are a TTY
 
113
        ui_factory = TextUIFactory(bar_type=TTYProgressBar)
 
114
        pb = ui_factory.nested_progress_bar()
 
115
        try:
 
116
            pb.to_messages_file = stdout
 
117
            ui_factory._progress_bar_stack.bottom().to_file = stderr
 
118
            # Create a progress update that isn't throttled
 
119
            pb.start_time -= 10
 
120
            pb.update('x', 1, 1)
 
121
            result = pb.note('t')
 
122
            self.assertEqual(None, result)
 
123
            self.assertEqual("t\n", stdout.getvalue())
 
124
            # the exact contents will depend on the terminal width and we don't
 
125
            # care about that right now - but you're probably running it on at
 
126
            # least a 10-character wide terminal :)
 
127
            self.assertContainsRe(stderr.getvalue(), r'\r {10,}\r$')
 
128
        finally:
 
129
            pb.finished()
 
130
 
 
131
    def test_progress_nested(self):
 
132
        # test factory based nested and popping.
 
133
        ui = TextUIFactory()
 
134
        pb1 = ui.nested_progress_bar()
 
135
        pb2 = ui.nested_progress_bar()
 
136
        self.assertRaises(errors.MissingProgressBarFinish, pb1.finished)
 
137
        pb2.finished()
 
138
        pb1.finished()
 
139
 
 
140
    def test_progress_stack(self):
 
141
        # test the progress bar stack which the default text factory 
 
142
        # uses.
 
143
        stderr = StringIO()
 
144
        stdout = StringIO()
 
145
        # make a stack, which accepts parameters like a pb.
 
146
        stack = ProgressBarStack(to_file=stderr, to_messages_file=stdout)
 
147
        # but is not one
 
148
        self.assertFalse(getattr(stack, 'note', False))
 
149
        pb1 = stack.get_nested()
 
150
        pb2 = stack.get_nested()
 
151
        self.assertRaises(errors.MissingProgressBarFinish, pb1.finished)
 
152
        pb2.finished()
 
153
        pb1.finished()
 
154
        # the text ui factory never actually removes the stack once its setup.
 
155
        # we need to be able to nest again correctly from here.
 
156
        pb1 = stack.get_nested()
 
157
        pb2 = stack.get_nested()
 
158
        self.assertRaises(errors.MissingProgressBarFinish, pb1.finished)
 
159
        pb2.finished()
 
160
        pb1.finished()
 
161
 
 
162
    def test_text_factory_setting_progress_bar(self):
 
163
        # we should be able to choose the progress bar type used.
 
164
        factory = bzrlib.ui.text.TextUIFactory(
 
165
            bar_type=DotsProgressBar)
 
166
        bar = factory.nested_progress_bar()
 
167
        bar.finished()
 
168
        self.assertIsInstance(bar, DotsProgressBar)
 
169
 
 
170
    def test_cli_stdin_is_default_stdin(self):
 
171
        factory = bzrlib.ui.CLIUIFactory()
 
172
        self.assertEqual(sys.stdin, factory.stdin)
 
173
 
 
174
    def assert_get_bool_acceptance_of_user_input(self, factory):
 
175
        factory.stdin = StringIO("y\nyes with garbage\nyes\nn\nnot an answer\nno\nfoo\n")
 
176
        factory.stdout = StringIO()
 
177
        # there is no output from the base factory
 
178
        self.assertEqual(True, factory.get_boolean(""))
 
179
        self.assertEqual(True, factory.get_boolean(""))
 
180
        self.assertEqual(False, factory.get_boolean(""))
 
181
        self.assertEqual(False, factory.get_boolean(""))
 
182
        self.assertEqual("foo\n", factory.stdin.read())
 
183
 
 
184
    def test_silent_ui_getbool(self):
 
185
        factory = bzrlib.ui.SilentUIFactory()
 
186
        self.assert_get_bool_acceptance_of_user_input(factory)
 
187
 
 
188
    def test_silent_factory_prompts_silently(self):
 
189
        factory = bzrlib.ui.SilentUIFactory()
 
190
        stdout = StringIO()
 
191
        factory.stdin = StringIO("y\n")
 
192
        self.assertEqual(
 
193
            True,
 
194
            self.apply_redirected(
 
195
                None, stdout, stdout, factory.get_boolean, "foo")
 
196
            )
 
197
        self.assertEqual("", stdout.getvalue())
 
198
        
 
199
    def test_text_ui_getbool(self):
 
200
        factory = bzrlib.ui.text.TextUIFactory()
 
201
        self.assert_get_bool_acceptance_of_user_input(factory)
 
202
 
 
203
    def test_text_factory_prompts_and_clears(self):
 
204
        # a get_boolean call should clear the pb before prompting
 
205
        factory = bzrlib.ui.text.TextUIFactory(bar_type=DotsProgressBar)
 
206
        factory.stdout = _TTYStringIO()
 
207
        factory.stdin = StringIO("yada\ny\n")
 
208
        pb = self.apply_redirected(
 
209
            factory.stdin, factory.stdout, factory.stdout, factory.nested_progress_bar)
 
210
        pb.start_time = None
 
211
        self.apply_redirected(
 
212
            factory.stdin, factory.stdout, factory.stdout, pb.update, "foo", 0, 1)
 
213
        self.assertEqual(
 
214
            True,
 
215
            self.apply_redirected(
 
216
                None, factory.stdout, factory.stdout, factory.get_boolean, "what do you want")
 
217
            )
 
218
        output = factory.stdout.getvalue()
 
219
        self.assertEqual("foo: .\n"
 
220
                         "what do you want? [y/n]: what do you want? [y/n]: ",
 
221
                         factory.stdout.getvalue())