~bzr-pqm/bzr/bzr.dev

1185.49.22 by John Arbash Meinel
Added get_password to the UIFactory, using it inside of sftp.py
1
# Copyright (C) 2005 by 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
1534.5.6 by Robert Collins
split out converter logic into per-format objects.
21
from StringIO import StringIO
1704.2.9 by Martin Pool
Make text_factory test not depend on 80-col terminal
22
import re
1185.49.22 by John Arbash Meinel
Added get_password to the UIFactory, using it inside of sftp.py
23
import sys
24
1681.1.2 by Robert Collins
* bzrlib.ui.text.TextUIFactory now accepts a bar_type parameter which
25
import bzrlib
1594.1.1 by Robert Collins
Introduce new bzr progress bar api. ui_factory.nested_progress_bar.
26
import bzrlib.errors as errors
27
from bzrlib.progress import TTYProgressBar, ProgressBarStack
1185.49.22 by John Arbash Meinel
Added get_password to the UIFactory, using it inside of sftp.py
28
from bzrlib.tests import TestCase
1843.3.10 by John Arbash Meinel
ui tests were failing when output was redirected to a file. (thus blocked by the pqm)
29
from bzrlib.tests.test_progress import _TTYStringIO
1185.49.22 by John Arbash Meinel
Added get_password to the UIFactory, using it inside of sftp.py
30
from bzrlib.ui import SilentUIFactory
31
from bzrlib.ui.text import TextUIFactory
32
1681.1.2 by Robert Collins
* bzrlib.ui.text.TextUIFactory now accepts a bar_type parameter which
33
1185.49.22 by John Arbash Meinel
Added get_password to the UIFactory, using it inside of sftp.py
34
class UITests(TestCase):
35
36
    def test_silent_factory(self):
37
        ui = SilentUIFactory()
1594.1.3 by Robert Collins
Fixup pb usage to use nested_progress_bar.
38
        pb = ui.nested_progress_bar()
39
        try:
40
            # TODO: Test that there is no output from SilentUIFactory
41
    
42
            self.assertEquals(ui.get_password(), None)
43
            self.assertEquals(ui.get_password(u'Hello There \u1234 %(user)s',
44
                                              user=u'some\u1234')
45
                             , None)
46
        finally:
47
            pb.finished()
1185.49.22 by John Arbash Meinel
Added get_password to the UIFactory, using it inside of sftp.py
48
49
    def test_text_factory(self):
50
        ui = TextUIFactory()
1594.1.3 by Robert Collins
Fixup pb usage to use nested_progress_bar.
51
        pb = ui.nested_progress_bar()
52
        pb.finished()
1185.49.22 by John Arbash Meinel
Added get_password to the UIFactory, using it inside of sftp.py
53
        # TODO: Test the output from TextUIFactory, perhaps by overriding sys.stdout
54
55
        # Unfortunately we can't actually test the ui.get_password() because 
56
        # that would actually prompt the user for a password during the test suite
57
        # This has been tested manually with both LANG=en_US.utf-8 and LANG=C
58
        # print
59
        # self.assertEquals(ui.get_password(u"%(user)s please type 'bogus'",
60
        #                                   user=u'some\u1234')
61
        #                  , 'bogus')
62
1534.5.6 by Robert Collins
split out converter logic into per-format objects.
63
64
    def test_progress_note(self):
65
        stderr = StringIO()
66
        stdout = StringIO()
1843.3.10 by John Arbash Meinel
ui tests were failing when output was redirected to a file. (thus blocked by the pqm)
67
        ui_factory = TextUIFactory(bar_type=TTYProgressBar)
1558.8.5 by Aaron Bentley
Pass note up the stack instead of using bzrlib.ui_factory
68
        pb = ui_factory.nested_progress_bar()
1558.8.4 by Aaron Bentley
Fixed test case for pb.note
69
        try:
70
            pb.to_messages_file = stdout
1558.8.5 by Aaron Bentley
Pass note up the stack instead of using bzrlib.ui_factory
71
            ui_factory._progress_bar_stack.bottom().to_file = stderr
1558.8.4 by Aaron Bentley
Fixed test case for pb.note
72
            result = pb.note('t')
73
            self.assertEqual(None, result)
74
            self.assertEqual("t\n", stdout.getvalue())
1843.3.2 by John Arbash Meinel
Fix a ui test that depended on clearing
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()
1843.3.10 by John Arbash Meinel
ui tests were failing when output was redirected to a file. (thus blocked by the pqm)
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)
1843.3.2 by John Arbash Meinel
Fix a ui test that depended on clearing
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)
95
            result = pb.note('t')
96
            self.assertEqual(None, result)
97
            self.assertEqual("t\n", stdout.getvalue())
1558.8.4 by Aaron Bentley
Fixed test case for pb.note
98
            # the exact contents will depend on the terminal width and we don't
99
            # care about that right now - but you're probably running it on at
100
            # least a 10-character wide terminal :)
1843.3.2 by John Arbash Meinel
Fix a ui test that depended on clearing
101
            self.assertContainsRe(stderr.getvalue(), r'\r {10,}\r$')
1558.8.4 by Aaron Bentley
Fixed test case for pb.note
102
        finally:
1558.8.5 by Aaron Bentley
Pass note up the stack instead of using bzrlib.ui_factory
103
            pb.finished()
1594.1.1 by Robert Collins
Introduce new bzr progress bar api. ui_factory.nested_progress_bar.
104
105
    def test_progress_nested(self):
106
        # test factory based nested and popping.
107
        ui = TextUIFactory()
108
        pb1 = ui.nested_progress_bar()
109
        pb2 = ui.nested_progress_bar()
110
        self.assertRaises(errors.MissingProgressBarFinish, pb1.finished)
111
        pb2.finished()
112
        pb1.finished()
113
114
    def test_progress_stack(self):
115
        # test the progress bar stack which the default text factory 
116
        # uses.
117
        stderr = StringIO()
118
        stdout = StringIO()
119
        # make a stack, which accepts parameters like a pb.
120
        stack = ProgressBarStack(to_file=stderr, to_messages_file=stdout)
121
        # but is not one
122
        self.assertFalse(getattr(stack, 'note', False))
123
        pb1 = stack.get_nested()
124
        pb2 = stack.get_nested()
125
        self.assertRaises(errors.MissingProgressBarFinish, pb1.finished)
126
        pb2.finished()
127
        pb1.finished()
128
        # the text ui factory never actually removes the stack once its setup.
129
        # we need to be able to nest again correctly from here.
130
        pb1 = stack.get_nested()
131
        pb2 = stack.get_nested()
132
        self.assertRaises(errors.MissingProgressBarFinish, pb1.finished)
133
        pb2.finished()
134
        pb1.finished()
1681.1.2 by Robert Collins
* bzrlib.ui.text.TextUIFactory now accepts a bar_type parameter which
135
136
    def test_text_factory_setting_progress_bar(self):
137
        # we should be able to choose the progress bar type used.
138
        factory = bzrlib.ui.text.TextUIFactory(
139
            bar_type=bzrlib.progress.DotsProgressBar)
140
        bar = factory.nested_progress_bar()
141
        bar.finished()
142
        self.assertIsInstance(bar, bzrlib.progress.DotsProgressBar)
1687.1.4 by Robert Collins
Add bzrlib.ui.ui_factory.get_boolean().
143
144
    def test_cli_stdin_is_default_stdin(self):
145
        factory = bzrlib.ui.CLIUIFactory()
146
        self.assertEqual(sys.stdin, factory.stdin)
147
148
    def assert_get_bool_acceptance_of_user_input(self, factory):
149
        factory.stdin = StringIO("y\nyes with garbage\nyes\nn\nnot an answer\nno\nfoo\n")
150
        factory.stdout = StringIO()
151
        # there is no output from the base factory
152
        self.assertEqual(True, factory.get_boolean(""))
153
        self.assertEqual(True, factory.get_boolean(""))
154
        self.assertEqual(False, factory.get_boolean(""))
155
        self.assertEqual(False, factory.get_boolean(""))
156
        self.assertEqual("foo\n", factory.stdin.read())
157
158
    def test_silent_ui_getbool(self):
159
        factory = bzrlib.ui.SilentUIFactory()
160
        self.assert_get_bool_acceptance_of_user_input(factory)
161
162
    def test_silent_factory_prompts_silently(self):
163
        factory = bzrlib.ui.SilentUIFactory()
164
        stdout = StringIO()
165
        factory.stdin = StringIO("y\n")
166
        self.assertEqual(
167
            True,
168
            self.apply_redirected(
169
                None, stdout, stdout, factory.get_boolean, "foo")
170
            )
171
        self.assertEqual("", stdout.getvalue())
172
        
173
    def test_text_ui_getbool(self):
174
        factory = bzrlib.ui.text.TextUIFactory()
175
        self.assert_get_bool_acceptance_of_user_input(factory)
176
177
    def test_text_factory_prompts_and_clears(self):
178
        # a get_boolean call should clear the pb before prompting
179
        factory = bzrlib.ui.text.TextUIFactory()
1843.3.10 by John Arbash Meinel
ui tests were failing when output was redirected to a file. (thus blocked by the pqm)
180
        factory.stdout = _TTYStringIO()
1687.1.4 by Robert Collins
Add bzrlib.ui.ui_factory.get_boolean().
181
        factory.stdin = StringIO("yada\ny\n")
182
        pb = self.apply_redirected(
183
            factory.stdin, factory.stdout, factory.stdout, factory.nested_progress_bar)
1793.1.1 by Aaron Bentley
Hide TTYProgressBars unless they last more than 1 second
184
        pb.start_time = None
1687.1.4 by Robert Collins
Add bzrlib.ui.ui_factory.get_boolean().
185
        self.apply_redirected(
186
            factory.stdin, factory.stdout, factory.stdout, pb.update, "foo", 0, 1)
187
        self.assertEqual(
188
            True,
189
            self.apply_redirected(
190
                None, factory.stdout, factory.stdout, factory.get_boolean, "what do you want")
191
            )
1704.2.9 by Martin Pool
Make text_factory test not depend on 80-col terminal
192
        # use a regular expression so that we don't depend on the particular
193
        # screen width - could also set and restore $COLUMN if that has
194
        # priority on all platforms, but it doesn't at present.
195
        output = factory.stdout.getvalue()
196
        if not re.match(
197
            "\r/ \\[    *\\] foo 0/1"
198
            "\r   *" 
199
            "\rwhat do you want\\? \\[y/n\\]:what do you want\\? \\[y/n\\]:", 
200
            output):
1843.3.10 by John Arbash Meinel
ui tests were failing when output was redirected to a file. (thus blocked by the pqm)
201
            self.fail("didn't match factory output %r, %s" % (factory, output))