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
30
class ExpectShelver(shelf_ui.Shelver):
31
"""A variant of Shelver that intercepts console activity, for testing."""
33
def __init__(self, work_tree, target_tree, diff_writer=None,
34
auto=False, auto_apply=False, file_list=None, message=None,
35
destroy=False, reporter=None):
36
shelf_ui.Shelver.__init__(self, work_tree, target_tree, diff_writer,
37
auto, auto_apply, file_list, message,
38
destroy, reporter=reporter)
40
self.diff_writer = StringIO()
42
def expect(self, prompt, response):
43
self.expected.append((prompt, response))
45
def prompt(self, message):
47
prompt, response = self.expected.pop(0)
49
raise AssertionError('Unexpected prompt: %s' % message)
51
raise AssertionError('Wrong prompt: %s' % message)
55
LINES_AJ = 'a\nb\nc\nd\ne\nf\ng\nh\ni\nj\n'
58
LINES_ZY = 'z\nb\nc\nd\ne\nf\ng\nh\ni\ny\n'
61
LINES_AY = 'a\nb\nc\nd\ne\nf\ng\nh\ni\ny\n'
64
class TestShelver(tests.TestCaseWithTransport):
66
def create_shelvable_tree(self):
67
tree = self.make_branch_and_tree('tree')
68
self.build_tree_contents([('tree/foo', LINES_AJ)])
69
tree.add('foo', 'foo-id')
70
tree.commit('added foo')
71
self.build_tree_contents([('tree/foo', LINES_ZY)])
74
def test_unexpected_prompt_failure(self):
75
tree = self.create_shelvable_tree()
76
tree.lock_tree_write()
77
self.addCleanup(tree.unlock)
78
shelver = ExpectShelver(tree, tree.basis_tree())
79
e = self.assertRaises(AssertionError, shelver.run)
80
self.assertEqual('Unexpected prompt: Shelve? [yNfq?]', str(e))
82
def test_wrong_prompt_failure(self):
83
tree = self.create_shelvable_tree()
84
tree.lock_tree_write()
85
self.addCleanup(tree.unlock)
86
shelver = ExpectShelver(tree, tree.basis_tree())
87
shelver.expect('foo', 'y')
88
e = self.assertRaises(AssertionError, shelver.run)
89
self.assertEqual('Wrong prompt: Shelve? [yNfq?]', str(e))
91
def test_shelve_not_diff(self):
92
tree = self.create_shelvable_tree()
93
tree.lock_tree_write()
94
self.addCleanup(tree.unlock)
95
shelver = ExpectShelver(tree, tree.basis_tree())
96
shelver.expect('Shelve? [yNfq?]', 'n')
97
shelver.expect('Shelve? [yNfq?]', 'n')
98
# No final shelving prompt because no changes were selected
100
self.assertFileEqual(LINES_ZY, 'tree/foo')
102
def test_shelve_diff_no(self):
103
tree = self.create_shelvable_tree()
104
tree.lock_tree_write()
105
self.addCleanup(tree.unlock)
106
shelver = ExpectShelver(tree, tree.basis_tree())
107
shelver.expect('Shelve? [yNfq?]', 'y')
108
shelver.expect('Shelve? [yNfq?]', 'y')
109
shelver.expect('Shelve 2 change(s)? [yNfq?]', 'n')
111
self.assertFileEqual(LINES_ZY, 'tree/foo')
113
def test_shelve_diff(self):
114
tree = self.create_shelvable_tree()
115
tree.lock_tree_write()
116
self.addCleanup(tree.unlock)
117
shelver = ExpectShelver(tree, tree.basis_tree())
118
shelver.expect('Shelve? [yNfq?]', 'y')
119
shelver.expect('Shelve? [yNfq?]', 'y')
120
shelver.expect('Shelve 2 change(s)? [yNfq?]', 'y')
122
self.assertFileEqual(LINES_AJ, 'tree/foo')
124
def test_shelve_one_diff(self):
125
tree = self.create_shelvable_tree()
126
tree.lock_tree_write()
127
self.addCleanup(tree.unlock)
128
shelver = ExpectShelver(tree, tree.basis_tree())
129
shelver.expect('Shelve? [yNfq?]', 'y')
130
shelver.expect('Shelve? [yNfq?]', 'n')
131
shelver.expect('Shelve 1 change(s)? [yNfq?]', 'y')
133
self.assertFileEqual(LINES_AY, 'tree/foo')
135
def test_shelve_binary_change(self):
136
tree = self.create_shelvable_tree()
137
self.build_tree_contents([('tree/foo', '\x00')])
138
tree.lock_tree_write()
139
self.addCleanup(tree.unlock)
140
shelver = ExpectShelver(tree, tree.basis_tree())
141
shelver.expect('Shelve binary changes? [yNfq?]', 'y')
142
shelver.expect('Shelve 1 change(s)? [yNfq?]', 'y')
144
self.assertFileEqual(LINES_AJ, 'tree/foo')
146
def test_shelve_rename(self):
147
tree = self.create_shelvable_tree()
148
tree.rename_one('foo', 'bar')
149
tree.lock_tree_write()
150
self.addCleanup(tree.unlock)
151
shelver = ExpectShelver(tree, tree.basis_tree())
152
shelver.expect('Shelve renaming "foo" => "bar"? [yNfq?]', 'y')
153
shelver.expect('Shelve? [yNfq?]', 'y')
154
shelver.expect('Shelve? [yNfq?]', 'y')
155
shelver.expect('Shelve 3 change(s)? [yNfq?]', 'y')
157
self.assertFileEqual(LINES_AJ, 'tree/foo')
159
def test_shelve_deletion(self):
160
tree = self.create_shelvable_tree()
161
os.unlink('tree/foo')
162
tree.lock_tree_write()
163
self.addCleanup(tree.unlock)
164
shelver = ExpectShelver(tree, tree.basis_tree())
165
shelver.expect('Shelve removing file "foo"? [yNfq?]', 'y')
166
shelver.expect('Shelve 1 change(s)? [yNfq?]', 'y')
168
self.assertFileEqual(LINES_AJ, 'tree/foo')
170
def test_shelve_creation(self):
171
tree = self.make_branch_and_tree('tree')
172
tree.commit('add tree root')
173
self.build_tree(['tree/foo'])
175
tree.lock_tree_write()
176
self.addCleanup(tree.unlock)
177
shelver = ExpectShelver(tree, tree.basis_tree())
178
shelver.expect('Shelve adding file "foo"? [yNfq?]', 'y')
179
shelver.expect('Shelve 1 change(s)? [yNfq?]', 'y')
181
self.failIfExists('tree/foo')
183
def test_shelve_kind_change(self):
184
tree = self.create_shelvable_tree()
185
os.unlink('tree/foo')
187
tree.lock_tree_write()
188
self.addCleanup(tree.unlock)
189
shelver = ExpectShelver(tree, tree.basis_tree())
190
shelver.expect('Shelve changing "foo" from file to directory? [yNfq?]',
192
shelver.expect('Shelve 1 change(s)? [yNfq?]', 'y')
194
def test_shelve_modify_target(self):
195
self.requireFeature(tests.SymlinkFeature)
196
tree = self.create_shelvable_tree()
197
os.symlink('bar', 'tree/baz')
198
tree.add('baz', 'baz-id')
199
tree.commit("Add symlink")
200
os.unlink('tree/baz')
201
os.symlink('vax', 'tree/baz')
202
tree.lock_tree_write()
203
self.addCleanup(tree.unlock)
204
shelver = ExpectShelver(tree, tree.basis_tree())
205
shelver.expect('Shelve changing target of "baz" from "bar" to '
206
'"vax"? [yNfq?]', 'y')
207
shelver.expect('Shelve 1 change(s)? [yNfq?]', 'y')
209
self.assertEqual('bar', os.readlink('tree/baz'))
211
def test_shelve_finish(self):
212
tree = self.create_shelvable_tree()
213
tree.lock_tree_write()
214
self.addCleanup(tree.unlock)
215
shelver = ExpectShelver(tree, tree.basis_tree())
216
shelver.expect('Shelve? [yNfq?]', 'f')
217
shelver.expect('Shelve 2 change(s)? [yNfq?]', 'y')
219
self.assertFileEqual(LINES_AJ, 'tree/foo')
221
def test_shelve_quit(self):
222
tree = self.create_shelvable_tree()
223
tree.lock_tree_write()
224
self.addCleanup(tree.unlock)
225
shelver = ExpectShelver(tree, tree.basis_tree())
226
shelver.expect('Shelve? [yNfq?]', 'q')
227
self.assertRaises(errors.UserAbort, shelver.run)
228
self.assertFileEqual(LINES_ZY, 'tree/foo')
230
def test_shelve_all(self):
231
tree = self.create_shelvable_tree()
232
shelver = ExpectShelver.from_args(sys.stdout, all=True,
237
shelver.work_tree.unlock()
238
self.assertFileEqual(LINES_AJ, 'tree/foo')
240
def test_shelve_filename(self):
241
tree = self.create_shelvable_tree()
242
self.build_tree(['tree/bar'])
244
tree.lock_tree_write()
245
self.addCleanup(tree.unlock)
246
shelver = ExpectShelver(tree, tree.basis_tree(), file_list=['bar'])
247
shelver.expect('Shelve adding file "bar"? [yNfq?]', 'y')
248
shelver.expect('Shelve 1 change(s)? [yNfq?]', 'y')
251
def test_shelve_help(self):
252
tree = self.create_shelvable_tree()
253
tree.lock_tree_write()
254
self.addCleanup(tree.unlock)
255
shelver = ExpectShelver(tree, tree.basis_tree())
256
shelver.expect('Shelve? [yNfq?]', '?')
257
shelver.expect('Shelve? [(y)es, (N)o, (f)inish, or (q)uit]', 'f')
258
shelver.expect('Shelve 2 change(s)? [yNfq?]', 'y')
261
def test_shelve_distroy(self):
262
tree = self.create_shelvable_tree()
263
shelver = shelf_ui.Shelver.from_args(sys.stdout, all=True,
264
directory='tree', destroy=True)
268
shelver.work_tree.unlock()
269
self.assertIs(None, tree.get_shelf_manager().last_shelf())
270
self.assertFileEqual(LINES_AJ, 'tree/foo')
273
def shelve_all(tree, target_revision_id):
276
target = tree.branch.repository.revision_tree(target_revision_id)
277
shelver = shelf_ui.Shelver(tree, target, auto=True,
283
def test_shelve_old_root_deleted(self):
284
tree1 = self.make_branch_and_tree('tree1')
285
tree1.commit('add root')
286
tree2 = self.make_branch_and_tree('tree2')
287
rev2 = tree2.commit('add root')
288
tree1.merge_from_branch(tree2.branch,
289
from_revision=revision.NULL_REVISION)
290
tree1.commit('Replaced root entry')
291
# This is essentially assertNotRaises(InconsistentDelta)
292
self.expectFailure('Cannot shelve replacing a root entry',
293
self.assertRaises, AssertionError,
294
self.assertRaises, errors.InconsistentDelta,
295
self.shelve_all, tree1, rev2)
297
def test_shelve_split(self):
298
outer_tree = self.make_branch_and_tree('outer')
299
outer_tree.commit('Add root')
300
inner_tree = self.make_branch_and_tree('outer/inner')
301
rev2 = inner_tree.commit('Add root')
302
outer_tree.subsume(inner_tree)
303
# This is essentially assertNotRaises(ValueError).
304
# The ValueError is 'None is not a valid file id'.
305
self.expectFailure('Cannot shelve a join back to the inner tree.',
306
self.assertRaises, AssertionError,
307
self.assertRaises, ValueError, self.shelve_all,
311
class TestApplyReporter(TestShelver):
313
def test_shelve_not_diff(self):
314
tree = self.create_shelvable_tree()
315
tree.lock_tree_write()
316
self.addCleanup(tree.unlock)
317
shelver = ExpectShelver(tree, tree.basis_tree(),
318
reporter=shelf_ui.ApplyReporter())
319
shelver.expect('Apply change? [yNfq?]', 'n')
320
shelver.expect('Apply change? [yNfq?]', 'n')
321
# No final shelving prompt because no changes were selected
323
self.assertFileEqual(LINES_ZY, 'tree/foo')
325
def test_shelve_diff_no(self):
326
tree = self.create_shelvable_tree()
327
tree.lock_tree_write()
328
self.addCleanup(tree.unlock)
329
shelver = ExpectShelver(tree, tree.basis_tree(),
330
reporter=shelf_ui.ApplyReporter())
331
shelver.expect('Apply change? [yNfq?]', 'y')
332
shelver.expect('Apply change? [yNfq?]', 'y')
333
shelver.expect('Apply 2 change(s)? [yNfq?]', 'n')
335
self.assertFileEqual(LINES_ZY, 'tree/foo')
337
def test_shelve_diff(self):
338
tree = self.create_shelvable_tree()
339
tree.lock_tree_write()
340
self.addCleanup(tree.unlock)
341
shelver = ExpectShelver(tree, tree.basis_tree(),
342
reporter=shelf_ui.ApplyReporter())
343
shelver.expect('Apply change? [yNfq?]', 'y')
344
shelver.expect('Apply change? [yNfq?]', 'y')
345
shelver.expect('Apply 2 change(s)? [yNfq?]', 'y')
347
self.assertFileEqual(LINES_AJ, 'tree/foo')
349
def test_shelve_binary_change(self):
350
tree = self.create_shelvable_tree()
351
self.build_tree_contents([('tree/foo', '\x00')])
352
tree.lock_tree_write()
353
self.addCleanup(tree.unlock)
354
shelver = ExpectShelver(tree, tree.basis_tree(),
355
reporter=shelf_ui.ApplyReporter())
356
shelver.expect('Apply binary changes? [yNfq?]', 'y')
357
shelver.expect('Apply 1 change(s)? [yNfq?]', 'y')
359
self.assertFileEqual(LINES_AJ, 'tree/foo')
361
def test_shelve_rename(self):
362
tree = self.create_shelvable_tree()
363
tree.rename_one('foo', 'bar')
364
tree.lock_tree_write()
365
self.addCleanup(tree.unlock)
366
shelver = ExpectShelver(tree, tree.basis_tree(),
367
reporter=shelf_ui.ApplyReporter())
368
shelver.expect('Rename "bar" => "foo"? [yNfq?]', 'y')
369
shelver.expect('Apply change? [yNfq?]', 'y')
370
shelver.expect('Apply change? [yNfq?]', 'y')
371
shelver.expect('Apply 3 change(s)? [yNfq?]', 'y')
373
self.assertFileEqual(LINES_AJ, 'tree/foo')
375
def test_shelve_deletion(self):
376
tree = self.create_shelvable_tree()
377
os.unlink('tree/foo')
378
tree.lock_tree_write()
379
self.addCleanup(tree.unlock)
380
shelver = ExpectShelver(tree, tree.basis_tree(),
381
reporter=shelf_ui.ApplyReporter())
382
shelver.expect('Add file "foo"? [yNfq?]', 'y')
383
shelver.expect('Apply 1 change(s)? [yNfq?]', 'y')
385
self.assertFileEqual(LINES_AJ, 'tree/foo')
387
def test_shelve_creation(self):
388
tree = self.make_branch_and_tree('tree')
389
tree.commit('add tree root')
390
self.build_tree(['tree/foo'])
392
tree.lock_tree_write()
393
self.addCleanup(tree.unlock)
394
shelver = ExpectShelver(tree, tree.basis_tree(),
395
reporter=shelf_ui.ApplyReporter())
396
shelver.expect('Delete file "foo"? [yNfq?]', 'y')
397
shelver.expect('Apply 1 change(s)? [yNfq?]', 'y')
399
self.failIfExists('tree/foo')
401
def test_shelve_kind_change(self):
402
tree = self.create_shelvable_tree()
403
os.unlink('tree/foo')
405
tree.lock_tree_write()
406
self.addCleanup(tree.unlock)
407
shelver = ExpectShelver(tree, tree.basis_tree(),
408
reporter=shelf_ui.ApplyReporter())
409
shelver.expect('Change "foo" from directory to a file? [yNfq?]', 'y')
410
shelver.expect('Apply 1 change(s)? [yNfq?]', 'y')
412
def test_shelve_modify_target(self):
413
self.requireFeature(tests.SymlinkFeature)
414
tree = self.create_shelvable_tree()
415
os.symlink('bar', 'tree/baz')
416
tree.add('baz', 'baz-id')
417
tree.commit("Add symlink")
418
os.unlink('tree/baz')
419
os.symlink('vax', 'tree/baz')
420
tree.lock_tree_write()
421
self.addCleanup(tree.unlock)
422
shelver = ExpectShelver(tree, tree.basis_tree(),
423
reporter=shelf_ui.ApplyReporter())
424
shelver.expect('Change target of "baz" from "vax" to "bar"? [yNfq?]',
426
shelver.expect('Apply 1 change(s)? [yNfq?]', 'y')
428
self.assertEqual('bar', os.readlink('tree/baz'))
431
class TestUnshelver(tests.TestCaseWithTransport):
433
def create_tree_with_shelf(self):
434
tree = self.make_branch_and_tree('tree')
437
self.build_tree_contents([('tree/foo', LINES_AJ)])
438
tree.add('foo', 'foo-id')
439
tree.commit('added foo')
440
self.build_tree_contents([('tree/foo', LINES_ZY)])
441
shelf_ui.Shelver(tree, tree.basis_tree(), auto_apply=True,
447
def test_unshelve(self):
448
tree = self.create_tree_with_shelf()
450
self.addCleanup(tree.unlock)
451
manager = tree.get_shelf_manager()
452
shelf_ui.Unshelver(tree, manager, 1, True, True, True).run()
453
self.assertFileEqual(LINES_ZY, 'tree/foo')
455
def test_unshelve_args(self):
456
tree = self.create_tree_with_shelf()
457
unshelver = shelf_ui.Unshelver.from_args(directory='tree')
461
unshelver.tree.unlock()
462
self.assertFileEqual(LINES_ZY, 'tree/foo')
463
self.assertIs(None, tree.get_shelf_manager().last_shelf())
465
def test_unshelve_args_dry_run(self):
466
tree = self.create_tree_with_shelf()
467
unshelver = shelf_ui.Unshelver.from_args(directory='tree',
472
unshelver.tree.unlock()
473
self.assertFileEqual(LINES_AJ, 'tree/foo')
474
self.assertEqual(1, tree.get_shelf_manager().last_shelf())
476
def test_unshelve_args_delete_only(self):
477
tree = self.make_branch_and_tree('tree')
478
manager = tree.get_shelf_manager()
479
shelf_file = manager.new_shelf()[1]
481
shelf_file.write('garbage')
484
unshelver = shelf_ui.Unshelver.from_args(directory='tree',
485
action='delete-only')
489
unshelver.tree.unlock()
490
self.assertIs(None, manager.last_shelf())
492
def test_unshelve_args_invalid_shelf_id(self):
493
tree = self.make_branch_and_tree('tree')
494
manager = tree.get_shelf_manager()
495
shelf_file = manager.new_shelf()[1]
497
shelf_file.write('garbage')
500
self.assertRaises(errors.InvalidShelfId,
501
shelf_ui.Unshelver.from_args, directory='tree',
502
action='delete-only', shelf_id='foo')