~bzr-pqm/bzr/bzr.dev

« back to all changes in this revision

Viewing changes to bzrlib/tests/test_ui.py

  • Committer: mbp at sourcefrog
  • Date: 2005-03-09 04:09:29 UTC
  • Revision ID: mbp@sourcefrog.net-20050309040929-eee0eb3e6d1e7627
add python bytecode to default ignore pattern

Show diffs side-by-side

added added

removed removed

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