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
28
from bzrlib.tests import script
31
class ExpectShelver(shelf_ui.Shelver):
32
"""A variant of Shelver that intercepts console activity, for testing."""
34
def __init__(self, work_tree, target_tree, diff_writer=None,
35
auto=False, auto_apply=False, file_list=None, message=None,
36
destroy=False, reporter=None):
37
shelf_ui.Shelver.__init__(self, work_tree, target_tree, diff_writer,
38
auto, auto_apply, file_list, message,
39
destroy, reporter=reporter)
41
self.diff_writer = StringIO()
43
def expect(self, prompt, response):
44
self.expected.append((prompt, response))
46
def prompt(self, message):
48
prompt, response = self.expected.pop(0)
50
raise AssertionError('Unexpected prompt: %s' % message)
52
raise AssertionError('Wrong prompt: %s' % message)
56
LINES_AJ = 'a\nb\nc\nd\ne\nf\ng\nh\ni\nj\n'
59
LINES_ZY = 'z\nb\nc\nd\ne\nf\ng\nh\ni\ny\n'
62
LINES_AY = 'a\nb\nc\nd\ne\nf\ng\nh\ni\ny\n'
65
class TestShelver(tests.TestCaseWithTransport):
67
def create_shelvable_tree(self):
68
tree = self.make_branch_and_tree('tree')
69
self.build_tree_contents([('tree/foo', LINES_AJ)])
70
tree.add('foo', 'foo-id')
71
tree.commit('added foo')
72
self.build_tree_contents([('tree/foo', LINES_ZY)])
75
def test_unexpected_prompt_failure(self):
76
tree = self.create_shelvable_tree()
77
tree.lock_tree_write()
78
self.addCleanup(tree.unlock)
79
shelver = ExpectShelver(tree, tree.basis_tree())
80
self.addCleanup(shelver.finalize)
81
e = self.assertRaises(AssertionError, shelver.run)
82
self.assertEqual('Unexpected prompt: Shelve? [yNfq?]', str(e))
84
def test_wrong_prompt_failure(self):
85
tree = self.create_shelvable_tree()
86
tree.lock_tree_write()
87
self.addCleanup(tree.unlock)
88
shelver = ExpectShelver(tree, tree.basis_tree())
89
self.addCleanup(shelver.finalize)
90
shelver.expect('foo', 'y')
91
e = self.assertRaises(AssertionError, shelver.run)
92
self.assertEqual('Wrong prompt: Shelve? [yNfq?]', str(e))
94
def test_shelve_not_diff(self):
95
tree = self.create_shelvable_tree()
96
tree.lock_tree_write()
97
self.addCleanup(tree.unlock)
98
shelver = ExpectShelver(tree, tree.basis_tree())
99
self.addCleanup(shelver.finalize)
100
shelver.expect('Shelve? [yNfq?]', 'n')
101
shelver.expect('Shelve? [yNfq?]', 'n')
102
# No final shelving prompt because no changes were selected
104
self.assertFileEqual(LINES_ZY, 'tree/foo')
106
def test_shelve_diff_no(self):
107
tree = self.create_shelvable_tree()
108
tree.lock_tree_write()
109
self.addCleanup(tree.unlock)
110
shelver = ExpectShelver(tree, tree.basis_tree())
111
self.addCleanup(shelver.finalize)
112
shelver.expect('Shelve? [yNfq?]', 'y')
113
shelver.expect('Shelve? [yNfq?]', 'y')
114
shelver.expect('Shelve 2 change(s)? [yNfq?]', 'n')
116
self.assertFileEqual(LINES_ZY, 'tree/foo')
118
def test_shelve_diff(self):
119
tree = self.create_shelvable_tree()
120
tree.lock_tree_write()
121
self.addCleanup(tree.unlock)
122
shelver = ExpectShelver(tree, tree.basis_tree())
123
self.addCleanup(shelver.finalize)
124
shelver.expect('Shelve? [yNfq?]', 'y')
125
shelver.expect('Shelve? [yNfq?]', 'y')
126
shelver.expect('Shelve 2 change(s)? [yNfq?]', 'y')
128
self.assertFileEqual(LINES_AJ, 'tree/foo')
130
def test_shelve_one_diff(self):
131
tree = self.create_shelvable_tree()
132
tree.lock_tree_write()
133
self.addCleanup(tree.unlock)
134
shelver = ExpectShelver(tree, tree.basis_tree())
135
self.addCleanup(shelver.finalize)
136
shelver.expect('Shelve? [yNfq?]', 'y')
137
shelver.expect('Shelve? [yNfq?]', 'n')
138
shelver.expect('Shelve 1 change(s)? [yNfq?]', 'y')
140
self.assertFileEqual(LINES_AY, 'tree/foo')
142
def test_shelve_binary_change(self):
143
tree = self.create_shelvable_tree()
144
self.build_tree_contents([('tree/foo', '\x00')])
145
tree.lock_tree_write()
146
self.addCleanup(tree.unlock)
147
shelver = ExpectShelver(tree, tree.basis_tree())
148
self.addCleanup(shelver.finalize)
149
shelver.expect('Shelve binary changes? [yNfq?]', 'y')
150
shelver.expect('Shelve 1 change(s)? [yNfq?]', 'y')
152
self.assertFileEqual(LINES_AJ, 'tree/foo')
154
def test_shelve_rename(self):
155
tree = self.create_shelvable_tree()
156
tree.rename_one('foo', 'bar')
157
tree.lock_tree_write()
158
self.addCleanup(tree.unlock)
159
shelver = ExpectShelver(tree, tree.basis_tree())
160
self.addCleanup(shelver.finalize)
161
shelver.expect('Shelve renaming "foo" => "bar"? [yNfq?]', 'y')
162
shelver.expect('Shelve? [yNfq?]', 'y')
163
shelver.expect('Shelve? [yNfq?]', 'y')
164
shelver.expect('Shelve 3 change(s)? [yNfq?]', 'y')
166
self.assertFileEqual(LINES_AJ, 'tree/foo')
168
def test_shelve_deletion(self):
169
tree = self.create_shelvable_tree()
170
os.unlink('tree/foo')
171
tree.lock_tree_write()
172
self.addCleanup(tree.unlock)
173
shelver = ExpectShelver(tree, tree.basis_tree())
174
self.addCleanup(shelver.finalize)
175
shelver.expect('Shelve removing file "foo"? [yNfq?]', 'y')
176
shelver.expect('Shelve 1 change(s)? [yNfq?]', 'y')
178
self.assertFileEqual(LINES_AJ, 'tree/foo')
180
def test_shelve_creation(self):
181
tree = self.make_branch_and_tree('tree')
182
tree.commit('add tree root')
183
self.build_tree(['tree/foo'])
185
tree.lock_tree_write()
186
self.addCleanup(tree.unlock)
187
shelver = ExpectShelver(tree, tree.basis_tree())
188
self.addCleanup(shelver.finalize)
189
shelver.expect('Shelve adding file "foo"? [yNfq?]', 'y')
190
shelver.expect('Shelve 1 change(s)? [yNfq?]', 'y')
192
self.failIfExists('tree/foo')
194
def test_shelve_kind_change(self):
195
tree = self.create_shelvable_tree()
196
os.unlink('tree/foo')
198
tree.lock_tree_write()
199
self.addCleanup(tree.unlock)
200
shelver = ExpectShelver(tree, tree.basis_tree())
201
self.addCleanup(shelver.finalize)
202
shelver.expect('Shelve changing "foo" from file to directory? [yNfq?]',
204
shelver.expect('Shelve 1 change(s)? [yNfq?]', 'y')
206
def test_shelve_modify_target(self):
207
self.requireFeature(tests.SymlinkFeature)
208
tree = self.create_shelvable_tree()
209
os.symlink('bar', 'tree/baz')
210
tree.add('baz', 'baz-id')
211
tree.commit("Add symlink")
212
os.unlink('tree/baz')
213
os.symlink('vax', 'tree/baz')
214
tree.lock_tree_write()
215
self.addCleanup(tree.unlock)
216
shelver = ExpectShelver(tree, tree.basis_tree())
217
self.addCleanup(shelver.finalize)
218
shelver.expect('Shelve changing target of "baz" from "bar" to '
219
'"vax"? [yNfq?]', 'y')
220
shelver.expect('Shelve 1 change(s)? [yNfq?]', 'y')
222
self.assertEqual('bar', os.readlink('tree/baz'))
224
def test_shelve_finish(self):
225
tree = self.create_shelvable_tree()
226
tree.lock_tree_write()
227
self.addCleanup(tree.unlock)
228
shelver = ExpectShelver(tree, tree.basis_tree())
229
self.addCleanup(shelver.finalize)
230
shelver.expect('Shelve? [yNfq?]', 'f')
231
shelver.expect('Shelve 2 change(s)? [yNfq?]', 'y')
233
self.assertFileEqual(LINES_AJ, 'tree/foo')
235
def test_shelve_quit(self):
236
tree = self.create_shelvable_tree()
237
tree.lock_tree_write()
238
self.addCleanup(tree.unlock)
239
shelver = ExpectShelver(tree, tree.basis_tree())
240
self.addCleanup(shelver.finalize)
241
shelver.expect('Shelve? [yNfq?]', 'q')
242
self.assertRaises(errors.UserAbort, shelver.run)
243
self.assertFileEqual(LINES_ZY, 'tree/foo')
245
def test_shelve_all(self):
246
tree = self.create_shelvable_tree()
247
shelver = ExpectShelver.from_args(sys.stdout, all=True,
253
self.assertFileEqual(LINES_AJ, 'tree/foo')
255
def test_shelve_filename(self):
256
tree = self.create_shelvable_tree()
257
self.build_tree(['tree/bar'])
259
tree.lock_tree_write()
260
self.addCleanup(tree.unlock)
261
shelver = ExpectShelver(tree, tree.basis_tree(), file_list=['bar'])
262
self.addCleanup(shelver.finalize)
263
shelver.expect('Shelve adding file "bar"? [yNfq?]', 'y')
264
shelver.expect('Shelve 1 change(s)? [yNfq?]', 'y')
267
def test_shelve_help(self):
268
tree = self.create_shelvable_tree()
269
tree.lock_tree_write()
270
self.addCleanup(tree.unlock)
271
shelver = ExpectShelver(tree, tree.basis_tree())
272
self.addCleanup(shelver.finalize)
273
shelver.expect('Shelve? [yNfq?]', '?')
274
shelver.expect('Shelve? [(y)es, (N)o, (f)inish, or (q)uit]', 'f')
275
shelver.expect('Shelve 2 change(s)? [yNfq?]', 'y')
278
def test_shelve_destroy(self):
279
tree = self.create_shelvable_tree()
280
shelver = shelf_ui.Shelver.from_args(sys.stdout, all=True,
281
directory='tree', destroy=True)
282
self.addCleanup(shelver.finalize)
284
self.assertIs(None, tree.get_shelf_manager().last_shelf())
285
self.assertFileEqual(LINES_AJ, 'tree/foo')
288
def shelve_all(tree, target_revision_id):
291
target = tree.branch.repository.revision_tree(target_revision_id)
292
shelver = shelf_ui.Shelver(tree, target, auto=True,
301
def test_shelve_old_root_deleted(self):
302
tree1 = self.make_branch_and_tree('tree1')
303
tree1.commit('add root')
304
tree2 = self.make_branch_and_tree('tree2')
305
rev2 = tree2.commit('add root')
306
tree1.merge_from_branch(tree2.branch,
307
from_revision=revision.NULL_REVISION)
308
tree1.commit('Replaced root entry')
309
# This is essentially assertNotRaises(InconsistentDelta)
310
self.expectFailure('Cannot shelve replacing a root entry',
311
self.assertRaises, AssertionError,
312
self.assertRaises, errors.InconsistentDelta,
313
self.shelve_all, tree1, rev2)
315
def test_shelve_split(self):
316
outer_tree = self.make_branch_and_tree('outer')
317
outer_tree.commit('Add root')
318
inner_tree = self.make_branch_and_tree('outer/inner')
319
rev2 = inner_tree.commit('Add root')
320
outer_tree.subsume(inner_tree)
321
# This is essentially assertNotRaises(ValueError).
322
# The ValueError is 'None is not a valid file id'.
323
self.expectFailure('Cannot shelve a join back to the inner tree.',
324
self.assertRaises, AssertionError,
325
self.assertRaises, ValueError, self.shelve_all,
329
class TestApplyReporter(TestShelver):
331
def test_shelve_not_diff(self):
332
tree = self.create_shelvable_tree()
333
tree.lock_tree_write()
334
self.addCleanup(tree.unlock)
335
shelver = ExpectShelver(tree, tree.basis_tree(),
336
reporter=shelf_ui.ApplyReporter())
337
self.addCleanup(shelver.finalize)
338
shelver.expect('Apply change? [yNfq?]', 'n')
339
shelver.expect('Apply change? [yNfq?]', 'n')
340
# No final shelving prompt because no changes were selected
342
self.assertFileEqual(LINES_ZY, 'tree/foo')
344
def test_shelve_diff_no(self):
345
tree = self.create_shelvable_tree()
346
tree.lock_tree_write()
347
self.addCleanup(tree.unlock)
348
shelver = ExpectShelver(tree, tree.basis_tree(),
349
reporter=shelf_ui.ApplyReporter())
350
self.addCleanup(shelver.finalize)
351
shelver.expect('Apply change? [yNfq?]', 'y')
352
shelver.expect('Apply change? [yNfq?]', 'y')
353
shelver.expect('Apply 2 change(s)? [yNfq?]', 'n')
355
self.assertFileEqual(LINES_ZY, 'tree/foo')
357
def test_shelve_diff(self):
358
tree = self.create_shelvable_tree()
359
tree.lock_tree_write()
360
self.addCleanup(tree.unlock)
361
shelver = ExpectShelver(tree, tree.basis_tree(),
362
reporter=shelf_ui.ApplyReporter())
363
self.addCleanup(shelver.finalize)
364
shelver.expect('Apply change? [yNfq?]', 'y')
365
shelver.expect('Apply change? [yNfq?]', 'y')
366
shelver.expect('Apply 2 change(s)? [yNfq?]', 'y')
368
self.assertFileEqual(LINES_AJ, 'tree/foo')
370
def test_shelve_binary_change(self):
371
tree = self.create_shelvable_tree()
372
self.build_tree_contents([('tree/foo', '\x00')])
373
tree.lock_tree_write()
374
self.addCleanup(tree.unlock)
375
shelver = ExpectShelver(tree, tree.basis_tree(),
376
reporter=shelf_ui.ApplyReporter())
377
self.addCleanup(shelver.finalize)
378
shelver.expect('Apply binary changes? [yNfq?]', 'y')
379
shelver.expect('Apply 1 change(s)? [yNfq?]', 'y')
381
self.assertFileEqual(LINES_AJ, 'tree/foo')
383
def test_shelve_rename(self):
384
tree = self.create_shelvable_tree()
385
tree.rename_one('foo', 'bar')
386
tree.lock_tree_write()
387
self.addCleanup(tree.unlock)
388
shelver = ExpectShelver(tree, tree.basis_tree(),
389
reporter=shelf_ui.ApplyReporter())
390
self.addCleanup(shelver.finalize)
391
shelver.expect('Rename "bar" => "foo"? [yNfq?]', 'y')
392
shelver.expect('Apply change? [yNfq?]', 'y')
393
shelver.expect('Apply change? [yNfq?]', 'y')
394
shelver.expect('Apply 3 change(s)? [yNfq?]', 'y')
396
self.assertFileEqual(LINES_AJ, 'tree/foo')
398
def test_shelve_deletion(self):
399
tree = self.create_shelvable_tree()
400
os.unlink('tree/foo')
401
tree.lock_tree_write()
402
self.addCleanup(tree.unlock)
403
shelver = ExpectShelver(tree, tree.basis_tree(),
404
reporter=shelf_ui.ApplyReporter())
405
self.addCleanup(shelver.finalize)
406
shelver.expect('Add file "foo"? [yNfq?]', 'y')
407
shelver.expect('Apply 1 change(s)? [yNfq?]', 'y')
409
self.assertFileEqual(LINES_AJ, 'tree/foo')
411
def test_shelve_creation(self):
412
tree = self.make_branch_and_tree('tree')
413
tree.commit('add tree root')
414
self.build_tree(['tree/foo'])
416
tree.lock_tree_write()
417
self.addCleanup(tree.unlock)
418
shelver = ExpectShelver(tree, tree.basis_tree(),
419
reporter=shelf_ui.ApplyReporter())
420
self.addCleanup(shelver.finalize)
421
shelver.expect('Delete file "foo"? [yNfq?]', 'y')
422
shelver.expect('Apply 1 change(s)? [yNfq?]', 'y')
424
self.failIfExists('tree/foo')
426
def test_shelve_kind_change(self):
427
tree = self.create_shelvable_tree()
428
os.unlink('tree/foo')
430
tree.lock_tree_write()
431
self.addCleanup(tree.unlock)
432
shelver = ExpectShelver(tree, tree.basis_tree(),
433
reporter=shelf_ui.ApplyReporter())
434
self.addCleanup(shelver.finalize)
435
shelver.expect('Change "foo" from directory to a file? [yNfq?]', 'y')
436
shelver.expect('Apply 1 change(s)? [yNfq?]', 'y')
438
def test_shelve_modify_target(self):
439
self.requireFeature(tests.SymlinkFeature)
440
tree = self.create_shelvable_tree()
441
os.symlink('bar', 'tree/baz')
442
tree.add('baz', 'baz-id')
443
tree.commit("Add symlink")
444
os.unlink('tree/baz')
445
os.symlink('vax', 'tree/baz')
446
tree.lock_tree_write()
447
self.addCleanup(tree.unlock)
448
shelver = ExpectShelver(tree, tree.basis_tree(),
449
reporter=shelf_ui.ApplyReporter())
450
self.addCleanup(shelver.finalize)
451
shelver.expect('Change target of "baz" from "vax" to "bar"? [yNfq?]',
453
shelver.expect('Apply 1 change(s)? [yNfq?]', 'y')
455
self.assertEqual('bar', os.readlink('tree/baz'))
458
class TestUnshelver(tests.TestCaseWithTransport):
460
def create_tree_with_shelf(self):
461
tree = self.make_branch_and_tree('tree')
464
self.build_tree_contents([('tree/foo', LINES_AJ)])
465
tree.add('foo', 'foo-id')
466
tree.commit('added foo')
467
self.build_tree_contents([('tree/foo', LINES_ZY)])
468
shelver = shelf_ui.Shelver(tree, tree.basis_tree(),
469
auto_apply=True, auto=True)
478
def test_unshelve(self):
479
tree = self.create_tree_with_shelf()
481
self.addCleanup(tree.unlock)
482
manager = tree.get_shelf_manager()
483
shelf_ui.Unshelver(tree, manager, 1, True, True, True).run()
484
self.assertFileEqual(LINES_ZY, 'tree/foo')
486
def test_unshelve_args(self):
487
tree = self.create_tree_with_shelf()
488
unshelver = shelf_ui.Unshelver.from_args(directory='tree')
492
unshelver.tree.unlock()
493
self.assertFileEqual(LINES_ZY, 'tree/foo')
494
self.assertIs(None, tree.get_shelf_manager().last_shelf())
496
def test_unshelve_args_dry_run(self):
497
tree = self.create_tree_with_shelf()
498
unshelver = shelf_ui.Unshelver.from_args(directory='tree',
503
unshelver.tree.unlock()
504
self.assertFileEqual(LINES_AJ, 'tree/foo')
505
self.assertEqual(1, tree.get_shelf_manager().last_shelf())
507
def test_unshelve_args_delete_only(self):
508
tree = self.make_branch_and_tree('tree')
509
manager = tree.get_shelf_manager()
510
shelf_file = manager.new_shelf()[1]
512
shelf_file.write('garbage')
515
unshelver = shelf_ui.Unshelver.from_args(directory='tree',
516
action='delete-only')
520
unshelver.tree.unlock()
521
self.assertIs(None, manager.last_shelf())
523
def test_unshelve_args_invalid_shelf_id(self):
524
tree = self.make_branch_and_tree('tree')
525
manager = tree.get_shelf_manager()
526
shelf_file = manager.new_shelf()[1]
528
shelf_file.write('garbage')
531
self.assertRaises(errors.InvalidShelfId,
532
shelf_ui.Unshelver.from_args, directory='tree',
533
action='delete-only', shelf_id='foo')
536
class TestUnshelveScripts(TestUnshelver,
537
script.TestCaseWithTransportAndScript):
539
def test_unshelve_messages_keep(self):
540
self.create_tree_with_shelf()
543
$ bzr unshelve --keep
544
2>Using changes with id "1".
546
2>All changes applied successfully.
549
def test_unshelve_messages_delete(self):
550
self.create_tree_with_shelf()
553
$ bzr unshelve --delete-only
554
2>Deleted changes with id "1".
557
def test_unshelve_messages_apply(self):
558
self.create_tree_with_shelf()
561
$ bzr unshelve --apply
562
2>Using changes with id "1".
564
2>All changes applied successfully.
565
2>Deleted changes with id "1".
568
def test_unshelve_messages_dry_run(self):
569
self.create_tree_with_shelf()
572
$ bzr unshelve --dry-run
573
2>Using changes with id "1".