/brz/remove-bazaar

To get this branch, use:
bzr branch http://gegoxaren.bato24.eu/bzr/brz/remove-bazaar

« back to all changes in this revision

Viewing changes to bzrlib/tests/test_ui.py

  • Committer: Vincent Ladeuil
  • Date: 2009-01-23 21:06:48 UTC
  • mto: (3966.1.1 bzr.integration)
  • mto: This revision was merged to the branch mainline in revision 3967.
  • Revision ID: v.ladeuil+lp@free.fr-20090123210648-yfb39g22yyo83d3y
Slight refactoring and test fixing.

* bzrlib/tests/test_merge.py:
(TestMergerEntriesLCAOnDisk.test_modified_symlink): Passing now.

* bzrlib/merge.py:
(Merge3Merger._lca_multi_way): Fix doc reference.
(Merge3Merger.merge_contents.contents_conflict): Try to delay
this_pair evaulation to avoid unnecessary sha1 (impyling file read
from disk) calculation. Also slightly refactor to avoid repeated
file_id in trees calculations.

Show diffs side-by-side

added added

removed removed

Lines of Context:
 
1
# Copyright (C) 2005, 2008 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 (
 
28
    DotsProgressBar,
 
29
    ProgressBarStack,
 
30
    TTYProgressBar,
 
31
    )
 
32
from bzrlib.tests import (
 
33
    TestCase,
 
34
    TestUIFactory,
 
35
    StringIOWrapper,
 
36
    )
 
37
from bzrlib.tests.test_progress import _TTYStringIO
 
38
from bzrlib.ui import (
 
39
    CLIUIFactory,
 
40
    SilentUIFactory,
 
41
    )
 
42
from bzrlib.ui.text import TextUIFactory
 
43
 
 
44
 
 
45
class UITests(TestCase):
 
46
 
 
47
    def test_silent_factory(self):
 
48
        ui = SilentUIFactory()
 
49
        stdout = StringIO()
 
50
        self.assertEqual(None,
 
51
                         self.apply_redirected(None, stdout, stdout,
 
52
                                               ui.get_password))
 
53
        self.assertEqual('', stdout.getvalue())
 
54
        self.assertEqual(None,
 
55
                         self.apply_redirected(None, stdout, stdout,
 
56
                                               ui.get_password,
 
57
                                               u'Hello\u1234 %(user)s',
 
58
                                               user=u'some\u1234'))
 
59
        self.assertEqual('', stdout.getvalue())
 
60
 
 
61
    def test_text_factory_ascii_password(self):
 
62
        ui = TestUIFactory(stdin='secret\n', stdout=StringIOWrapper())
 
63
        pb = ui.nested_progress_bar()
 
64
        try:
 
65
            self.assertEqual('secret',
 
66
                             self.apply_redirected(ui.stdin, ui.stdout,
 
67
                                                   ui.stdout,
 
68
                                                   ui.get_password))
 
69
            # ': ' is appended to prompt
 
70
            self.assertEqual(': ', ui.stdout.getvalue())
 
71
            # stdin should be empty
 
72
            self.assertEqual('', ui.stdin.readline())
 
73
        finally:
 
74
            pb.finished()
 
75
 
 
76
    def test_text_factory_utf8_password(self):
 
77
        """Test an utf8 password.
 
78
 
 
79
        We can't predict what encoding users will have for stdin, so we force
 
80
        it to utf8 to test that we transport the password correctly.
 
81
        """
 
82
        ui = TestUIFactory(stdin=u'baz\u1234'.encode('utf8'),
 
83
                           stdout=StringIOWrapper())
 
84
        ui.stdin.encoding = 'utf8'
 
85
        ui.stdout.encoding = ui.stdin.encoding
 
86
        pb = ui.nested_progress_bar()
 
87
        try:
 
88
            password = self.apply_redirected(ui.stdin, ui.stdout, ui.stdout,
 
89
                                             ui.get_password,
 
90
                                             u'Hello \u1234 %(user)s',
 
91
                                             user=u'some\u1234')
 
92
            # We use StringIO objects, we need to decode them
 
93
            self.assertEqual(u'baz\u1234', password.decode('utf8'))
 
94
            self.assertEqual(u'Hello \u1234 some\u1234: ',
 
95
                             ui.stdout.getvalue().decode('utf8'))
 
96
            # stdin should be empty
 
97
            self.assertEqual('', ui.stdin.readline())
 
98
        finally:
 
99
            pb.finished()
 
100
 
 
101
    def test_progress_note(self):
 
102
        stderr = StringIO()
 
103
        stdout = StringIO()
 
104
        ui_factory = TextUIFactory(stdin=StringIO(''),
 
105
            stderr=stderr,
 
106
            stdout=stdout)
 
107
        pb = ui_factory.nested_progress_bar()
 
108
        try:
 
109
            result = pb.note('t')
 
110
            self.assertEqual(None, result)
 
111
            self.assertEqual("t\n", stdout.getvalue())
 
112
            # Since there was no update() call, there should be no clear() call
 
113
            self.failIf(re.search(r'^\r {10,}\r$',
 
114
                                  stderr.getvalue()) is not None,
 
115
                        'We cleared the stderr without anything to put there')
 
116
        finally:
 
117
            pb.finished()
 
118
 
 
119
    def test_progress_note_clears(self):
 
120
        stderr = StringIO()
 
121
        stdout = StringIO()
 
122
        # The PQM redirects the output to a file, so it
 
123
        # defaults to creating a Dots progress bar. we
 
124
        # need to force it to believe we are a TTY
 
125
        ui_factory = TextUIFactory(
 
126
            stdin=StringIO(''),
 
127
            stdout=stdout, stderr=stderr)
 
128
        pb = ui_factory.nested_progress_bar()
 
129
        try:
 
130
            # Create a progress update that isn't throttled
 
131
            pb.update('x', 1, 1)
 
132
            result = pb.note('t')
 
133
            self.assertEqual(None, result)
 
134
            self.assertEqual("t\n", stdout.getvalue())
 
135
            # the exact contents will depend on the terminal width and we don't
 
136
            # care about that right now - but you're probably running it on at
 
137
            # least a 10-character wide terminal :)
 
138
            self.assertContainsRe(stderr.getvalue(), r'\r {10,}\r$')
 
139
        finally:
 
140
            pb.finished()
 
141
 
 
142
    def test_progress_nested(self):
 
143
        # test factory based nested and popping.
 
144
        ui = TextUIFactory(None, None, None)
 
145
        pb1 = ui.nested_progress_bar()
 
146
        pb2 = ui.nested_progress_bar()
 
147
        # We no longer warn about finishing unnested progress bars.
 
148
        warnings, _ = self.callCatchWarnings(pb1.finished)
 
149
        self.assertEqual(len(warnings), 0)
 
150
        pb2.finished()
 
151
        pb1.finished()
 
152
 
 
153
    def test_progress_stack(self):
 
154
        # test the progress bar stack which the default text factory 
 
155
        # uses.
 
156
        stderr = StringIO()
 
157
        stdout = StringIO()
 
158
        # make a stack, which accepts parameters like a pb.
 
159
        stack = ProgressBarStack(to_file=stderr, to_messages_file=stdout)
 
160
        # but is not one
 
161
        self.assertFalse(getattr(stack, 'note', False))
 
162
        pb1 = stack.get_nested()
 
163
        pb2 = stack.get_nested()
 
164
        warnings, _ = self.callCatchWarnings(pb1.finished)
 
165
        self.assertEqual(len(warnings), 1)
 
166
        pb2.finished()
 
167
        pb1.finished()
 
168
        # the text ui factory never actually removes the stack once its setup.
 
169
        # we need to be able to nest again correctly from here.
 
170
        pb1 = stack.get_nested()
 
171
        pb2 = stack.get_nested()
 
172
        warnings, _ = self.callCatchWarnings(pb1.finished)
 
173
        self.assertEqual(len(warnings), 1)
 
174
        pb2.finished()
 
175
        pb1.finished()
 
176
 
 
177
    def assert_get_bool_acceptance_of_user_input(self, factory):
 
178
        factory.stdin = StringIO("y\nyes with garbage\n"
 
179
                                 "yes\nn\nnot an answer\n"
 
180
                                 "no\nfoo\n")
 
181
        factory.stdout = StringIO()
 
182
        # there is no output from the base factory
 
183
        self.assertEqual(True, factory.get_boolean(""))
 
184
        self.assertEqual(True, factory.get_boolean(""))
 
185
        self.assertEqual(False, factory.get_boolean(""))
 
186
        self.assertEqual(False, factory.get_boolean(""))
 
187
        self.assertEqual("foo\n", factory.stdin.read())
 
188
        # stdin should be empty
 
189
        self.assertEqual('', factory.stdin.readline())
 
190
 
 
191
    def test_silent_ui_getbool(self):
 
192
        factory = SilentUIFactory()
 
193
        self.assert_get_bool_acceptance_of_user_input(factory)
 
194
 
 
195
    def test_silent_factory_prompts_silently(self):
 
196
        factory = SilentUIFactory()
 
197
        stdout = StringIO()
 
198
        factory.stdin = StringIO("y\n")
 
199
        self.assertEqual(True,
 
200
                         self.apply_redirected(None, stdout, stdout,
 
201
                                               factory.get_boolean, "foo"))
 
202
        self.assertEqual("", stdout.getvalue())
 
203
        # stdin should be empty
 
204
        self.assertEqual('', factory.stdin.readline())
 
205
 
 
206
    def test_text_ui_getbool(self):
 
207
        factory = TextUIFactory(None, None, None)
 
208
        self.assert_get_bool_acceptance_of_user_input(factory)
 
209
 
 
210
    def test_text_factory_prompts_and_clears(self):
 
211
        # a get_boolean call should clear the pb before prompting
 
212
        out = _TTYStringIO()
 
213
        factory = TextUIFactory(stdin=StringIO("yada\ny\n"), stdout=out, stderr=out)
 
214
        pb = factory.nested_progress_bar()
 
215
        pb.show_bar = False
 
216
        pb.show_spinner = False
 
217
        pb.show_count = False
 
218
        pb.update("foo", 0, 1)
 
219
        self.assertEqual(True,
 
220
                         self.apply_redirected(None, factory.stdout,
 
221
                                               factory.stdout,
 
222
                                               factory.get_boolean,
 
223
                                               "what do you want"))
 
224
        output = out.getvalue()
 
225
        self.assertContainsRe(factory.stdout.getvalue(),
 
226
            "foo *\r\r  *\r*")
 
227
        self.assertContainsRe(factory.stdout.getvalue(),
 
228
            r"what do you want\? \[y/n\]: what do you want\? \[y/n\]: ")
 
229
        # stdin should have been totally consumed
 
230
        self.assertEqual('', factory.stdin.readline())