1
# Copyright (C) 2008 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., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
18
from cStringIO import StringIO
22
from bzrlib import errors, shelf_ui, tests
25
class ExpectShelver(shelf_ui.Shelver):
26
"""A variant of Shelver that intercepts console activity, for testing."""
28
def __init__(self, work_tree, target_tree, diff_writer=None,
29
auto=False, auto_apply=False, file_list=None, message=None,
31
shelf_ui.Shelver.__init__(self, work_tree, target_tree, diff_writer,
32
auto, auto_apply, file_list, message,
35
self.diff_writer = StringIO()
37
def expect(self, prompt, response):
38
self.expected.append((prompt, response))
40
def prompt(self, message):
42
prompt, response = self.expected.pop(0)
44
raise AssertionError('Unexpected prompt: %s' % message)
46
raise AssertionError('Wrong prompt: %s' % message)
50
LINES_AJ = 'a\nb\nc\nd\ne\nf\ng\nh\ni\nj\n'
53
LINES_ZY = 'z\nb\nc\nd\ne\nf\ng\nh\ni\ny\n'
56
LINES_AY = 'a\nb\nc\nd\ne\nf\ng\nh\ni\ny\n'
59
class TestShelver(tests.TestCaseWithTransport):
61
def create_shelvable_tree(self):
62
tree = self.make_branch_and_tree('tree')
63
self.build_tree_contents([('tree/foo', LINES_AJ)])
64
tree.add('foo', 'foo-id')
65
tree.commit('added foo')
66
self.build_tree_contents([('tree/foo', LINES_ZY)])
69
def test_unexpected_prompt_failure(self):
70
tree = self.create_shelvable_tree()
71
shelver = ExpectShelver(tree, tree.basis_tree())
72
e = self.assertRaises(AssertionError, shelver.run)
73
self.assertEqual('Unexpected prompt: Shelve? [yNfq?]', str(e))
75
def test_wrong_prompt_failure(self):
76
tree = self.create_shelvable_tree()
77
shelver = ExpectShelver(tree, tree.basis_tree())
78
shelver.expect('foo', 'y')
79
e = self.assertRaises(AssertionError, shelver.run)
80
self.assertEqual('Wrong prompt: Shelve? [yNfq?]', str(e))
82
def test_shelve_not_diff(self):
83
tree = self.create_shelvable_tree()
84
shelver = ExpectShelver(tree, tree.basis_tree())
85
shelver.expect('Shelve? [yNfq?]', 'n')
86
shelver.expect('Shelve? [yNfq?]', 'n')
87
# No final shelving prompt because no changes were selected
89
self.assertFileEqual(LINES_ZY, 'tree/foo')
91
def test_shelve_diff_no(self):
92
tree = self.create_shelvable_tree()
93
shelver = ExpectShelver(tree, tree.basis_tree())
94
shelver.expect('Shelve? [yNfq?]', 'y')
95
shelver.expect('Shelve? [yNfq?]', 'y')
96
shelver.expect('Shelve 2 change(s)? [yNfq?]', 'n')
98
self.assertFileEqual(LINES_ZY, 'tree/foo')
100
def test_shelve_diff(self):
101
tree = self.create_shelvable_tree()
102
shelver = ExpectShelver(tree, tree.basis_tree())
103
shelver.expect('Shelve? [yNfq?]', 'y')
104
shelver.expect('Shelve? [yNfq?]', 'y')
105
shelver.expect('Shelve 2 change(s)? [yNfq?]', 'y')
107
self.assertFileEqual(LINES_AJ, 'tree/foo')
109
def test_shelve_one_diff(self):
110
tree = self.create_shelvable_tree()
111
shelver = ExpectShelver(tree, tree.basis_tree())
112
shelver.expect('Shelve? [yNfq?]', 'y')
113
shelver.expect('Shelve? [yNfq?]', 'n')
114
shelver.expect('Shelve 1 change(s)? [yNfq?]', 'y')
116
self.assertFileEqual(LINES_AY, 'tree/foo')
118
def test_shelve_binary_change(self):
119
tree = self.create_shelvable_tree()
120
self.build_tree_contents([('tree/foo', '\x00')])
121
shelver = ExpectShelver(tree, tree.basis_tree())
122
shelver.expect('Shelve binary changes? [yNfq?]', 'y')
123
shelver.expect('Shelve 1 change(s)? [yNfq?]', 'y')
125
self.assertFileEqual(LINES_AJ, 'tree/foo')
127
def test_shelve_rename(self):
128
tree = self.create_shelvable_tree()
129
tree.rename_one('foo', 'bar')
130
shelver = ExpectShelver(tree, tree.basis_tree())
131
shelver.expect('Shelve renaming "foo" => "bar"? [yNfq?]', 'y')
132
shelver.expect('Shelve? [yNfq?]', 'y')
133
shelver.expect('Shelve? [yNfq?]', 'y')
134
shelver.expect('Shelve 3 change(s)? [yNfq?]', 'y')
136
self.assertFileEqual(LINES_AJ, 'tree/foo')
138
def test_shelve_deletion(self):
139
tree = self.create_shelvable_tree()
140
os.unlink('tree/foo')
141
shelver = ExpectShelver(tree, tree.basis_tree())
142
shelver.expect('Shelve removing file "foo"? [yNfq?]', 'y')
143
shelver.expect('Shelve 1 change(s)? [yNfq?]', 'y')
145
self.assertFileEqual(LINES_AJ, 'tree/foo')
147
def test_shelve_creation(self):
148
tree = self.make_branch_and_tree('tree')
149
tree.commit('add tree root')
150
self.build_tree(['tree/foo'])
152
shelver = ExpectShelver(tree, tree.basis_tree())
153
shelver.expect('Shelve adding file "foo"? [yNfq?]', 'y')
154
shelver.expect('Shelve 1 change(s)? [yNfq?]', 'y')
156
self.failIfExists('tree/foo')
158
def test_shelve_kind_change(self):
159
tree = self.create_shelvable_tree()
160
os.unlink('tree/foo')
162
shelver = ExpectShelver(tree, tree.basis_tree())
163
shelver.expect('Shelve changing "foo" from file to directory? [yNfq?]',
165
shelver.expect('Shelve 1 change(s)? [yNfq?]', 'y')
167
def test_shelve_modify_target(self):
168
tree = self.create_shelvable_tree()
169
os.symlink('bar', 'tree/baz')
170
tree.add('baz', 'baz-id')
171
tree.commit("Add symlink")
172
os.unlink('tree/baz')
173
os.symlink('vax', 'tree/baz')
174
shelver = ExpectShelver(tree, tree.basis_tree())
175
shelver.expect('Shelve changing target of "baz" from "bar" to '
176
'"vax"? [yNfq?]', 'y')
177
shelver.expect('Shelve 1 change(s)? [yNfq?]', 'y')
179
self.assertEqual('bar', os.readlink('tree/baz'))
181
def test_shelve_finish(self):
182
tree = self.create_shelvable_tree()
183
shelver = ExpectShelver(tree, tree.basis_tree())
184
shelver.expect('Shelve? [yNfq?]', 'f')
185
shelver.expect('Shelve 2 change(s)? [yNfq?]', 'y')
187
self.assertFileEqual(LINES_AJ, 'tree/foo')
189
def test_shelve_quit(self):
190
tree = self.create_shelvable_tree()
191
shelver = ExpectShelver(tree, tree.basis_tree())
192
shelver.expect('Shelve? [yNfq?]', 'q')
193
self.assertRaises(errors.UserAbort, shelver.run)
194
self.assertFileEqual(LINES_ZY, 'tree/foo')
196
def test_shelve_all(self):
197
tree = self.create_shelvable_tree()
198
ExpectShelver.from_args(sys.stdout, all=True, directory='tree').run()
199
self.assertFileEqual(LINES_AJ, 'tree/foo')
201
def test_shelve_filename(self):
202
tree = self.create_shelvable_tree()
203
self.build_tree(['tree/bar'])
205
shelver = ExpectShelver(tree, tree.basis_tree(), file_list=['bar'])
206
shelver.expect('Shelve adding file "bar"? [yNfq?]', 'y')
207
shelver.expect('Shelve 1 change(s)? [yNfq?]', 'y')
210
def test_shelve_help(self):
211
tree = self.create_shelvable_tree()
212
shelver = ExpectShelver(tree, tree.basis_tree())
213
shelver.expect('Shelve? [yNfq?]', '?')
214
shelver.expect('Shelve? [(y)es, (N)o, (f)inish, or (q)uit]', 'f')
215
shelver.expect('Shelve 2 change(s)? [yNfq?]', 'y')
218
def test_shelve_distroy(self):
219
tree = self.create_shelvable_tree()
220
shelver = shelf_ui.Shelver.from_args(sys.stdout, all=True,
221
directory='tree', destroy=True)
223
self.assertIs(None, tree.get_shelf_manager().last_shelf())
224
self.assertFileEqual(LINES_AJ, 'tree/foo')
227
class TestUnshelver(tests.TestCaseWithTransport):
229
def create_tree_with_shelf(self):
230
tree = self.make_branch_and_tree('tree')
231
self.build_tree_contents([('tree/foo', LINES_AJ)])
232
tree.add('foo', 'foo-id')
233
tree.commit('added foo')
234
self.build_tree_contents([('tree/foo', LINES_ZY)])
235
shelf_ui.Shelver(tree, tree.basis_tree(), auto_apply=True,
239
def test_unshelve(self):
240
tree = self.create_tree_with_shelf()
242
self.addCleanup(tree.unlock)
243
manager = tree.get_shelf_manager()
244
shelf_ui.Unshelver(tree, manager, 1, True, True, True).run()
245
self.assertFileEqual(LINES_ZY, 'tree/foo')
247
def test_unshelve_args(self):
248
tree = self.create_tree_with_shelf()
249
shelf_ui.Unshelver.from_args(directory='tree').run()
250
self.assertFileEqual(LINES_ZY, 'tree/foo')
251
self.assertIs(None, tree.get_shelf_manager().last_shelf())
253
def test_unshelve_args_dry_run(self):
254
tree = self.create_tree_with_shelf()
255
shelf_ui.Unshelver.from_args(directory='tree', action='dry-run').run()
256
self.assertFileEqual(LINES_AJ, 'tree/foo')
257
self.assertEqual(1, tree.get_shelf_manager().last_shelf())
259
def test_unshelve_args_delete_only(self):
260
tree = self.make_branch_and_tree('tree')
261
manager = tree.get_shelf_manager()
262
shelf_file = manager.new_shelf()[1]
264
shelf_file.write('garbage')
267
unshelver = shelf_ui.Unshelver.from_args(directory='tree',
268
action='delete-only')
270
self.assertIs(None, manager.last_shelf())
272
def test_unshelve_args_invalid_shelf_id(self):
273
tree = self.make_branch_and_tree('tree')
274
manager = tree.get_shelf_manager()
275
shelf_file = manager.new_shelf()[1]
277
shelf_file.write('garbage')
280
self.assertRaises(errors.InvalidShelfId,
281
shelf_ui.Unshelver.from_args, directory='tree',
282
action='delete-only', shelf_id='foo')