~bzr-pqm/bzr/bzr.dev

« back to all changes in this revision

Viewing changes to bzrlib/tests/test_ui.py

  • Committer: John Arbash Meinel
  • Date: 2006-03-08 14:31:23 UTC
  • mfrom: (1598 +trunk)
  • mto: (1685.1.1 bzr-encoding)
  • mto: This revision was merged to the branch mainline in revision 1752.
  • Revision ID: john@arbash-meinel.com-20060308143123-448308b0db4de410
[merge] bzr.dev 1573, lots of updates

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
 
 
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
class UITests(TestCase):
 
32
 
 
33
    def test_silent_factory(self):
 
34
 
 
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
        pb = TTYProgressBar(to_file=stderr, to_messages_file=stdout)
 
66
        result = pb.note('t')
 
67
        self.assertEqual(None, result)
 
68
        self.assertEqual("t\n", stdout.getvalue())
 
69
        # the exact contents will depend on the terminal width and we don't
 
70
        # care about that right now - but you're probably running it on at
 
71
        # least a 10-character wide terminal :)
 
72
        self.assertContainsRe(stderr.getvalue(), r'^\r {10,}\r$')
 
73
 
 
74
    def test_progress_nested(self):
 
75
        # test factory based nested and popping.
 
76
        ui = TextUIFactory()
 
77
        pb1 = ui.nested_progress_bar()
 
78
        pb2 = ui.nested_progress_bar()
 
79
        self.assertRaises(errors.MissingProgressBarFinish, pb1.finished)
 
80
        pb2.finished()
 
81
        pb1.finished()
 
82
 
 
83
    def test_progress_stack(self):
 
84
        # test the progress bar stack which the default text factory 
 
85
        # uses.
 
86
        stderr = StringIO()
 
87
        stdout = StringIO()
 
88
        # make a stack, which accepts parameters like a pb.
 
89
        stack = ProgressBarStack(to_file=stderr, to_messages_file=stdout)
 
90
        # but is not one
 
91
        self.assertFalse(getattr(stack, 'note', False))
 
92
        pb1 = stack.get_nested()
 
93
        pb2 = stack.get_nested()
 
94
        self.assertRaises(errors.MissingProgressBarFinish, pb1.finished)
 
95
        pb2.finished()
 
96
        pb1.finished()
 
97
        # the text ui factory never actually removes the stack once its setup.
 
98
        # we need to be able to nest again correctly from here.
 
99
        pb1 = stack.get_nested()
 
100
        pb2 = stack.get_nested()
 
101
        self.assertRaises(errors.MissingProgressBarFinish, pb1.finished)
 
102
        pb2.finished()
 
103
        pb1.finished()