2052.3.2
by John Arbash Meinel
Change Copyright .. by Canonical to Copyright ... Canonical |
1 |
# Copyright (C) 2005 Canonical Ltd
|
1185.49.22
by John Arbash Meinel
Added get_password to the UIFactory, using it inside of sftp.py |
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 |
|
1534.5.6
by Robert Collins
split out converter logic into per-format objects. |
21 |
from StringIO import StringIO |
1704.2.9
by Martin Pool
Make text_factory test not depend on 80-col terminal |
22 |
import re |
1185.49.22
by John Arbash Meinel
Added get_password to the UIFactory, using it inside of sftp.py |
23 |
import sys |
24 |
||
1681.1.2
by Robert Collins
* bzrlib.ui.text.TextUIFactory now accepts a bar_type parameter which |
25 |
import bzrlib |
1594.1.1
by Robert Collins
Introduce new bzr progress bar api. ui_factory.nested_progress_bar. |
26 |
import bzrlib.errors as errors |
2294.4.1
by Vincent Ladeuil
Add a UIFactory.get_login method, fix tests. |
27 |
from bzrlib.progress import DotsProgressBar, TTYProgressBar, ProgressBarStack |
28 |
from bzrlib.tests import ( |
|
2294.4.4
by Vincent Ladeuil
Provide a better implementation for testing passwords. |
29 |
TestUIFactory, |
2294.4.1
by Vincent Ladeuil
Add a UIFactory.get_login method, fix tests. |
30 |
StringIOWrapper, |
31 |
TestCase, |
|
32 |
)
|
|
1843.3.10
by John Arbash Meinel
ui tests were failing when output was redirected to a file. (thus blocked by the pqm) |
33 |
from bzrlib.tests.test_progress import _TTYStringIO |
1185.49.22
by John Arbash Meinel
Added get_password to the UIFactory, using it inside of sftp.py |
34 |
from bzrlib.ui import SilentUIFactory |
35 |
from bzrlib.ui.text import TextUIFactory |
|
36 |
||
1681.1.2
by Robert Collins
* bzrlib.ui.text.TextUIFactory now accepts a bar_type parameter which |
37 |
|
1185.49.22
by John Arbash Meinel
Added get_password to the UIFactory, using it inside of sftp.py |
38 |
class UITests(TestCase): |
39 |
||
40 |
def test_silent_factory(self): |
|
41 |
ui = SilentUIFactory() |
|
2294.4.1
by Vincent Ladeuil
Add a UIFactory.get_login method, fix tests. |
42 |
stdout = StringIO() |
43 |
self.assertEqual(None, |
|
44 |
self.apply_redirected(None, stdout, stdout, |
|
45 |
ui.get_password)) |
|
46 |
self.assertEqual('', stdout.getvalue()) |
|
47 |
self.assertEqual(None, |
|
48 |
self.apply_redirected(None, stdout, stdout, |
|
49 |
ui.get_password, |
|
50 |
u'Hello\u1234 %(user)s', |
|
51 |
user=u'some\u1234')) |
|
52 |
self.assertEqual('', stdout.getvalue()) |
|
53 |
||
54 |
def test_text_factory_ascii_password(self): |
|
2294.4.4
by Vincent Ladeuil
Provide a better implementation for testing passwords. |
55 |
ui = TestUIFactory(stdin='secret\n', stdout=StringIOWrapper()) |
2294.4.1
by Vincent Ladeuil
Add a UIFactory.get_login method, fix tests. |
56 |
pb = ui.nested_progress_bar() |
57 |
try: |
|
58 |
self.assertEqual('secret', |
|
59 |
self.apply_redirected(ui.stdin, ui.stdout, |
|
60 |
ui.stdout, |
|
61 |
ui.get_password)) |
|
62 |
# ': ' is appended to prompt
|
|
2294.4.4
by Vincent Ladeuil
Provide a better implementation for testing passwords. |
63 |
self.assertEqual(': ', ui.stdout.getvalue()) |
2294.4.1
by Vincent Ladeuil
Add a UIFactory.get_login method, fix tests. |
64 |
finally: |
65 |
pb.finished() |
|
66 |
||
67 |
def test_text_factory_utf8_password(self): |
|
68 |
"""Test an utf8 password.
|
|
69 |
||
70 |
We can't predict what encoding users will have for stdin, so we force
|
|
71 |
it to utf8 to test that we transport the password correctly.
|
|
72 |
"""
|
|
2294.4.4
by Vincent Ladeuil
Provide a better implementation for testing passwords. |
73 |
ui = TestUIFactory(stdin=u'baz\u1234'.encode('utf8'), |
74 |
stdout=StringIOWrapper()) |
|
75 |
ui.stdin.encoding = 'utf8' |
|
76 |
ui.stdout.encoding = ui.stdin.encoding |
|
2294.4.1
by Vincent Ladeuil
Add a UIFactory.get_login method, fix tests. |
77 |
pb = ui.nested_progress_bar() |
78 |
try: |
|
79 |
password = self.apply_redirected(ui.stdin, ui.stdout, ui.stdout, |
|
80 |
ui.get_password, |
|
81 |
u'Hello \u1234 %(user)s', |
|
82 |
user=u'some\u1234') |
|
83 |
# We use StringIO objects, we need to decode them
|
|
84 |
self.assertEqual(u'baz\u1234', password.decode('utf8')) |
|
85 |
self.assertEqual(u'Hello \u1234 some\u1234: ', |
|
86 |
ui.stdout.getvalue().decode('utf8')) |
|
87 |
finally: |
|
88 |
pb.finished() |
|
1534.5.6
by Robert Collins
split out converter logic into per-format objects. |
89 |
|
90 |
def test_progress_note(self): |
|
91 |
stderr = StringIO() |
|
92 |
stdout = StringIO() |
|
1843.3.10
by John Arbash Meinel
ui tests were failing when output was redirected to a file. (thus blocked by the pqm) |
93 |
ui_factory = TextUIFactory(bar_type=TTYProgressBar) |
1558.8.5
by Aaron Bentley
Pass note up the stack instead of using bzrlib.ui_factory |
94 |
pb = ui_factory.nested_progress_bar() |
1558.8.4
by Aaron Bentley
Fixed test case for pb.note |
95 |
try: |
96 |
pb.to_messages_file = stdout |
|
1558.8.5
by Aaron Bentley
Pass note up the stack instead of using bzrlib.ui_factory |
97 |
ui_factory._progress_bar_stack.bottom().to_file = stderr |
1558.8.4
by Aaron Bentley
Fixed test case for pb.note |
98 |
result = pb.note('t') |
99 |
self.assertEqual(None, result) |
|
100 |
self.assertEqual("t\n", stdout.getvalue()) |
|
1843.3.2
by John Arbash Meinel
Fix a ui test that depended on clearing |
101 |
# Since there was no update() call, there should be no clear() call
|
102 |
self.failIf(re.search(r'^\r {10,}\r$', stderr.getvalue()) is not None, |
|
103 |
'We cleared the stderr without anything to put there') |
|
104 |
finally: |
|
105 |
pb.finished() |
|
106 |
||
107 |
def test_progress_note_clears(self): |
|
108 |
stderr = StringIO() |
|
109 |
stdout = StringIO() |
|
1843.3.10
by John Arbash Meinel
ui tests were failing when output was redirected to a file. (thus blocked by the pqm) |
110 |
# The PQM redirects the output to a file, so it
|
111 |
# defaults to creating a Dots progress bar. we
|
|
112 |
# need to force it to believe we are a TTY
|
|
113 |
ui_factory = TextUIFactory(bar_type=TTYProgressBar) |
|
1843.3.2
by John Arbash Meinel
Fix a ui test that depended on clearing |
114 |
pb = ui_factory.nested_progress_bar() |
115 |
try: |
|
116 |
pb.to_messages_file = stdout |
|
117 |
ui_factory._progress_bar_stack.bottom().to_file = stderr |
|
118 |
# Create a progress update that isn't throttled
|
|
119 |
pb.start_time -= 10 |
|
120 |
pb.update('x', 1, 1) |
|
121 |
result = pb.note('t') |
|
122 |
self.assertEqual(None, result) |
|
123 |
self.assertEqual("t\n", stdout.getvalue()) |
|
1558.8.4
by Aaron Bentley
Fixed test case for pb.note |
124 |
# the exact contents will depend on the terminal width and we don't
|
125 |
# care about that right now - but you're probably running it on at
|
|
126 |
# least a 10-character wide terminal :)
|
|
1843.3.2
by John Arbash Meinel
Fix a ui test that depended on clearing |
127 |
self.assertContainsRe(stderr.getvalue(), r'\r {10,}\r$') |
1558.8.4
by Aaron Bentley
Fixed test case for pb.note |
128 |
finally: |
1558.8.5
by Aaron Bentley
Pass note up the stack instead of using bzrlib.ui_factory |
129 |
pb.finished() |
1594.1.1
by Robert Collins
Introduce new bzr progress bar api. ui_factory.nested_progress_bar. |
130 |
|
131 |
def test_progress_nested(self): |
|
132 |
# test factory based nested and popping.
|
|
133 |
ui = TextUIFactory() |
|
134 |
pb1 = ui.nested_progress_bar() |
|
135 |
pb2 = ui.nested_progress_bar() |
|
136 |
self.assertRaises(errors.MissingProgressBarFinish, pb1.finished) |
|
137 |
pb2.finished() |
|
138 |
pb1.finished() |
|
139 |
||
140 |
def test_progress_stack(self): |
|
141 |
# test the progress bar stack which the default text factory
|
|
142 |
# uses.
|
|
143 |
stderr = StringIO() |
|
144 |
stdout = StringIO() |
|
145 |
# make a stack, which accepts parameters like a pb.
|
|
146 |
stack = ProgressBarStack(to_file=stderr, to_messages_file=stdout) |
|
147 |
# but is not one
|
|
148 |
self.assertFalse(getattr(stack, 'note', False)) |
|
149 |
pb1 = stack.get_nested() |
|
150 |
pb2 = stack.get_nested() |
|
151 |
self.assertRaises(errors.MissingProgressBarFinish, pb1.finished) |
|
152 |
pb2.finished() |
|
153 |
pb1.finished() |
|
154 |
# the text ui factory never actually removes the stack once its setup.
|
|
155 |
# we need to be able to nest again correctly from here.
|
|
156 |
pb1 = stack.get_nested() |
|
157 |
pb2 = stack.get_nested() |
|
158 |
self.assertRaises(errors.MissingProgressBarFinish, pb1.finished) |
|
159 |
pb2.finished() |
|
160 |
pb1.finished() |
|
1681.1.2
by Robert Collins
* bzrlib.ui.text.TextUIFactory now accepts a bar_type parameter which |
161 |
|
162 |
def test_text_factory_setting_progress_bar(self): |
|
163 |
# we should be able to choose the progress bar type used.
|
|
164 |
factory = bzrlib.ui.text.TextUIFactory( |
|
2294.4.1
by Vincent Ladeuil
Add a UIFactory.get_login method, fix tests. |
165 |
bar_type=DotsProgressBar) |
1681.1.2
by Robert Collins
* bzrlib.ui.text.TextUIFactory now accepts a bar_type parameter which |
166 |
bar = factory.nested_progress_bar() |
167 |
bar.finished() |
|
2294.4.1
by Vincent Ladeuil
Add a UIFactory.get_login method, fix tests. |
168 |
self.assertIsInstance(bar, DotsProgressBar) |
1687.1.4
by Robert Collins
Add bzrlib.ui.ui_factory.get_boolean(). |
169 |
|
170 |
def test_cli_stdin_is_default_stdin(self): |
|
171 |
factory = bzrlib.ui.CLIUIFactory() |
|
172 |
self.assertEqual(sys.stdin, factory.stdin) |
|
173 |
||
174 |
def assert_get_bool_acceptance_of_user_input(self, factory): |
|
175 |
factory.stdin = StringIO("y\nyes with garbage\nyes\nn\nnot an answer\nno\nfoo\n") |
|
176 |
factory.stdout = StringIO() |
|
177 |
# there is no output from the base factory
|
|
178 |
self.assertEqual(True, factory.get_boolean("")) |
|
179 |
self.assertEqual(True, factory.get_boolean("")) |
|
180 |
self.assertEqual(False, factory.get_boolean("")) |
|
181 |
self.assertEqual(False, factory.get_boolean("")) |
|
182 |
self.assertEqual("foo\n", factory.stdin.read()) |
|
183 |
||
184 |
def test_silent_ui_getbool(self): |
|
185 |
factory = bzrlib.ui.SilentUIFactory() |
|
186 |
self.assert_get_bool_acceptance_of_user_input(factory) |
|
187 |
||
188 |
def test_silent_factory_prompts_silently(self): |
|
189 |
factory = bzrlib.ui.SilentUIFactory() |
|
190 |
stdout = StringIO() |
|
191 |
factory.stdin = StringIO("y\n") |
|
192 |
self.assertEqual( |
|
193 |
True, |
|
194 |
self.apply_redirected( |
|
195 |
None, stdout, stdout, factory.get_boolean, "foo") |
|
196 |
)
|
|
197 |
self.assertEqual("", stdout.getvalue()) |
|
198 |
||
199 |
def test_text_ui_getbool(self): |
|
200 |
factory = bzrlib.ui.text.TextUIFactory() |
|
201 |
self.assert_get_bool_acceptance_of_user_input(factory) |
|
202 |
||
203 |
def test_text_factory_prompts_and_clears(self): |
|
204 |
# a get_boolean call should clear the pb before prompting
|
|
2294.4.1
by Vincent Ladeuil
Add a UIFactory.get_login method, fix tests. |
205 |
factory = bzrlib.ui.text.TextUIFactory(bar_type=DotsProgressBar) |
1843.3.10
by John Arbash Meinel
ui tests were failing when output was redirected to a file. (thus blocked by the pqm) |
206 |
factory.stdout = _TTYStringIO() |
1687.1.4
by Robert Collins
Add bzrlib.ui.ui_factory.get_boolean(). |
207 |
factory.stdin = StringIO("yada\ny\n") |
208 |
pb = self.apply_redirected( |
|
209 |
factory.stdin, factory.stdout, factory.stdout, factory.nested_progress_bar) |
|
1793.1.1
by Aaron Bentley
Hide TTYProgressBars unless they last more than 1 second |
210 |
pb.start_time = None |
1687.1.4
by Robert Collins
Add bzrlib.ui.ui_factory.get_boolean(). |
211 |
self.apply_redirected( |
212 |
factory.stdin, factory.stdout, factory.stdout, pb.update, "foo", 0, 1) |
|
213 |
self.assertEqual( |
|
214 |
True, |
|
215 |
self.apply_redirected( |
|
216 |
None, factory.stdout, factory.stdout, factory.get_boolean, "what do you want") |
|
217 |
)
|
|
1704.2.9
by Martin Pool
Make text_factory test not depend on 80-col terminal |
218 |
output = factory.stdout.getvalue() |
2294.4.1
by Vincent Ladeuil
Add a UIFactory.get_login method, fix tests. |
219 |
self.assertEqual("foo: .\n" |
220 |
"what do you want? [y/n]: what do you want? [y/n]: ", |
|
221 |
factory.stdout.getvalue()) |