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