~bzr-pqm/bzr/bzr.dev

« back to all changes in this revision

Viewing changes to bzrlib/tests/test_ui.py

  • Committer: Vincent Ladeuil
  • Date: 2007-02-21 14:46:06 UTC
  • mto: (2326.1.1 jam-integration)
  • mto: This revision was merged to the branch mainline in revision 2327.
  • Revision ID: v.ladeuil+lp@free.fr-20070221144606-qoy01pue1p187j3b
Provide a better implementation for testing passwords.

* bzrlib/ui/__init__.py:
(UIFactory.get_login): Deleted.
(CLIUIFactory.get_non_echoed_password): New method allowing
overriding.

* bzrlib/tests/__init__.py:
(TestUIFactory.get_non_echoed_password): Allows password testing
 without worrying about echo echo.

* bzrlib/tests/__init__.py:
(TestUIFactory): Moved from bzrlib/tests/blackbox/__init__.py
(FakeStdin): Deleted.
(TestCase.run_bzr_captured): Set and reuse ui.ui_factory.stdin.

* bzrlib/ui/text.py:
(TextUIFactory.get_login): Deleted.
(TextUIFactory.get_password): Moved to CLIUIFactory.

* bzrlib/tests/test_ui.py:
(UITests): Delete get_login tests.
(FakeTextUIFactory): Deleted. Better implementation in
TestUIFactory.

* bzrlib/tests/blackbox/__init__.py:
(TestUIFactory): Moved to bzrlib/tests/__init__.py.

Show diffs side-by-side

added added

removed removed

Lines of Context:
26
26
import bzrlib.errors as errors
27
27
from bzrlib.progress import DotsProgressBar, TTYProgressBar, ProgressBarStack
28
28
from bzrlib.tests import (
29
 
    FakeStdin,
 
29
    TestUIFactory,
30
30
    StringIOWrapper,
31
31
    TestCase,
32
32
    )
35
35
from bzrlib.ui.text import TextUIFactory
36
36
 
37
37
 
38
 
class FakeTextUIFactory(TextUIFactory):
39
 
    """A TextUIFactory with a fake stdin"""
40
 
 
41
 
    def __init__(self, stdin, stdout=None, stderr=None):
42
 
        TextUIFactory.__init__(self, stdout=stdout, stderr=stderr)
43
 
        self.stdin = stdin
44
 
        # To ease test writing, we suppose that stdin and stdout use the same
45
 
        # encoding
46
 
        self.stdout.encoding = self.stdin.encoding
47
 
 
48
 
 
49
38
class UITests(TestCase):
50
39
 
51
40
    def test_silent_factory(self):
61
50
                                               u'Hello\u1234 %(user)s',
62
51
                                               user=u'some\u1234'))
63
52
        self.assertEqual('', stdout.getvalue())
64
 
        self.assertEqual(None,
65
 
                         self.apply_redirected(None, stdout, stdout,
66
 
                                               ui.get_login,
67
 
                                               u'Who are %(user)s',
68
 
                                               user=u'you'))
69
 
        self.assertEqual('', stdout.getvalue())
70
53
 
71
54
    def test_text_factory_ascii_password(self):
72
 
        ui = FakeTextUIFactory(FakeStdin('secret\n'), StringIOWrapper())
 
55
        ui = TestUIFactory(stdin='secret\n', stdout=StringIOWrapper())
73
56
        pb = ui.nested_progress_bar()
74
57
        try:
75
58
            self.assertEqual('secret',
77
60
                                                   ui.stdout,
78
61
                                                   ui.get_password))
79
62
            # ': ' is appended to prompt
80
 
            # A '\n' is emitted as echo after the user enter password
81
 
            self.assertEqual(': \n', ui.stdout.getvalue())
 
63
            self.assertEqual(': ', ui.stdout.getvalue())
82
64
        finally:
83
65
            pb.finished()
84
66
 
88
70
        We can't predict what encoding users will have for stdin, so we force
89
71
        it to utf8 to test that we transport the password correctly.
90
72
        """
91
 
        ui = FakeTextUIFactory(FakeStdin(u'baz\u1234', 'utf8'),
92
 
                               StringIOWrapper())
 
73
        ui = TestUIFactory(stdin=u'baz\u1234'.encode('utf8'),
 
74
                           stdout=StringIOWrapper())
 
75
        ui.stdin.encoding = 'utf8'
 
76
        ui.stdout.encoding = ui.stdin.encoding
93
77
        pb = ui.nested_progress_bar()
94
78
        try:
95
79
            password = self.apply_redirected(ui.stdin, ui.stdout, ui.stdout,
98
82
                                             user=u'some\u1234')
99
83
            # We use StringIO objects, we need to decode them
100
84
            self.assertEqual(u'baz\u1234', password.decode('utf8'))
101
 
            self.assertEqual(u'Hello \u1234 some\u1234: \n',
102
 
                             ui.stdout.getvalue().decode('utf8'))
103
 
        finally:
104
 
            pb.finished()
105
 
 
106
 
    def test_text_factory_ascii_login(self):
107
 
        ui = FakeTextUIFactory(FakeStdin('itsme\n'), StringIOWrapper())
108
 
        pb = ui.nested_progress_bar()
109
 
        try:
110
 
            self.assertEqual('itsme',
111
 
                             self.apply_redirected(ui.stdin, ui.stdout,
112
 
                                                   ui.stdout,
113
 
                                                   ui.get_login))
114
 
            self.assertEqual(': ', ui.stdout.getvalue())
115
 
        finally:
116
 
            pb.finished()
117
 
 
118
 
    def test_text_factory_utf8_login(self):
119
 
        """Test an utf8 password.
120
 
 
121
 
        We can't predict what encoding users will have for stdin, so we force
122
 
        it to utf8 to test that we transport the login correctly.
123
 
        """
124
 
        ui = FakeTextUIFactory(FakeStdin(u'metoo\u1234', 'utf8'),
125
 
                               StringIOWrapper())
126
 
        pb = ui.nested_progress_bar()
127
 
        try:
128
 
            login = self.apply_redirected(ui.stdin, ui.stdout, ui.stdout,
129
 
                                          ui.get_login,
130
 
                                          u'Hello \u1234 %(user)s',
131
 
                                          user=u'some\u1234')
132
 
            # We use StringIO objects, we need to decode them
133
 
            self.assertEqual(u'metoo\u1234', login.decode('utf8'))
134
85
            self.assertEqual(u'Hello \u1234 some\u1234: ',
135
86
                             ui.stdout.getvalue().decode('utf8'))
136
87
        finally: