1
# Copyright (C) 2005 by Canonical Ltd
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.
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.
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
17
"""Tests for the bzrlib ui
21
from StringIO import StringIO
25
import bzrlib.errors as errors
26
from bzrlib.progress import TTYProgressBar, ProgressBarStack
27
from bzrlib.tests import TestCase
29
from bzrlib.ui import SilentUIFactory
30
from bzrlib.ui.text import TextUIFactory
32
class UITests(TestCase):
34
def test_silent_factory(self):
36
ui = SilentUIFactory()
37
pb = ui.nested_progress_bar()
39
# TODO: Test that there is no output from SilentUIFactory
41
self.assertEquals(ui.get_password(), None)
42
self.assertEquals(ui.get_password(u'Hello There \u1234 %(user)s',
48
def test_text_factory(self):
50
pb = ui.nested_progress_bar()
52
# TODO: Test the output from TextUIFactory, perhaps by overriding sys.stdout
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
58
# self.assertEquals(ui.get_password(u"%(user)s please type 'bogus'",
63
def test_progress_note(self):
66
old_factory = bzrlib.ui.ui_factory
67
bzrlib.ui.ui_factory = TextUIFactory()
70
pb = bzrlib.ui.ui_factory.nested_progress_bar()
71
pb.to_messages_file = stdout
72
bzrlib.ui.ui_factory._progress_bar_stack.bottom().to_file = stderr
74
self.assertEqual(None, result)
75
self.assertEqual("t\n", stdout.getvalue())
76
# the exact contents will depend on the terminal width and we don't
77
# care about that right now - but you're probably running it on at
78
# least a 10-character wide terminal :)
79
self.assertContainsRe(stderr.getvalue(), r'^\r {10,}\r$')
83
bzrlib.ui.ui_factory = old_factory
85
def test_progress_nested(self):
86
# test factory based nested and popping.
88
pb1 = ui.nested_progress_bar()
89
pb2 = ui.nested_progress_bar()
90
self.assertRaises(errors.MissingProgressBarFinish, pb1.finished)
94
def test_progress_stack(self):
95
# test the progress bar stack which the default text factory
99
# make a stack, which accepts parameters like a pb.
100
stack = ProgressBarStack(to_file=stderr, to_messages_file=stdout)
102
self.assertFalse(getattr(stack, 'note', False))
103
pb1 = stack.get_nested()
104
pb2 = stack.get_nested()
105
self.assertRaises(errors.MissingProgressBarFinish, pb1.finished)
108
# the text ui factory never actually removes the stack once its setup.
109
# we need to be able to nest again correctly from here.
110
pb1 = stack.get_nested()
111
pb2 = stack.get_nested()
112
self.assertRaises(errors.MissingProgressBarFinish, pb1.finished)