1
# Copyright (C) 2008, 2009, 2010 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
21
from textwrap import dedent
29
from bzrlib.tests import script
30
from bzrlib.tests import (
35
class ExpectShelver(shelf_ui.Shelver):
36
"""A variant of Shelver that intercepts console activity, for testing."""
38
def __init__(self, work_tree, target_tree, diff_writer=None,
39
auto=False, auto_apply=False, file_list=None, message=None,
40
destroy=False, reporter=None):
41
shelf_ui.Shelver.__init__(self, work_tree, target_tree, diff_writer,
42
auto, auto_apply, file_list, message,
43
destroy, reporter=reporter)
45
self.diff_writer = StringIO()
47
def expect(self, prompt, response):
48
self.expected.append((prompt, response))
50
def prompt(self, message):
52
prompt, response = self.expected.pop(0)
54
raise AssertionError('Unexpected prompt: %s' % message)
56
raise AssertionError('Wrong prompt: %s' % message)
60
LINES_AJ = 'a\nb\nc\nd\ne\nf\ng\nh\ni\nj\n'
63
LINES_ZY = 'z\nb\nc\nd\ne\nf\ng\nh\ni\ny\n'
66
LINES_AY = 'a\nb\nc\nd\ne\nf\ng\nh\ni\ny\n'
69
class ShelfTestCase(tests.TestCaseWithTransport):
71
def create_shelvable_tree(self):
72
tree = self.make_branch_and_tree('tree')
73
self.build_tree_contents([('tree/foo', LINES_AJ)])
74
tree.add('foo', 'foo-id')
75
tree.commit('added foo')
76
self.build_tree_contents([('tree/foo', LINES_ZY)])
80
class TestShelver(ShelfTestCase):
82
def test_unexpected_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
self.addCleanup(shelver.finalize)
88
e = self.assertRaises(AssertionError, shelver.run)
89
self.assertEqual('Unexpected prompt: Shelve? [yNfq?]', str(e))
91
def test_wrong_prompt_failure(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
self.addCleanup(shelver.finalize)
97
shelver.expect('foo', 'y')
98
e = self.assertRaises(AssertionError, shelver.run)
99
self.assertEqual('Wrong prompt: Shelve? [yNfq?]', str(e))
101
def test_shelve_not_diff(self):
102
tree = self.create_shelvable_tree()
103
tree.lock_tree_write()
104
self.addCleanup(tree.unlock)
105
shelver = ExpectShelver(tree, tree.basis_tree())
106
self.addCleanup(shelver.finalize)
107
shelver.expect('Shelve? [yNfq?]', 'n')
108
shelver.expect('Shelve? [yNfq?]', 'n')
109
# No final shelving prompt because no changes were selected
111
self.assertFileEqual(LINES_ZY, 'tree/foo')
113
def test_shelve_diff_no(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
self.addCleanup(shelver.finalize)
119
shelver.expect('Shelve? [yNfq?]', 'y')
120
shelver.expect('Shelve? [yNfq?]', 'y')
121
shelver.expect('Shelve 2 change(s)? [yNfq?]', 'n')
123
self.assertFileEqual(LINES_ZY, 'tree/foo')
125
def test_shelve_diff(self):
126
tree = self.create_shelvable_tree()
127
tree.lock_tree_write()
128
self.addCleanup(tree.unlock)
129
shelver = ExpectShelver(tree, tree.basis_tree())
130
self.addCleanup(shelver.finalize)
131
shelver.expect('Shelve? [yNfq?]', 'y')
132
shelver.expect('Shelve? [yNfq?]', 'y')
133
shelver.expect('Shelve 2 change(s)? [yNfq?]', 'y')
135
self.assertFileEqual(LINES_AJ, 'tree/foo')
137
def test_shelve_one_diff(self):
138
tree = self.create_shelvable_tree()
139
tree.lock_tree_write()
140
self.addCleanup(tree.unlock)
141
shelver = ExpectShelver(tree, tree.basis_tree())
142
self.addCleanup(shelver.finalize)
143
shelver.expect('Shelve? [yNfq?]', 'y')
144
shelver.expect('Shelve? [yNfq?]', 'n')
145
shelver.expect('Shelve 1 change(s)? [yNfq?]', 'y')
147
self.assertFileEqual(LINES_AY, 'tree/foo')
149
def test_shelve_binary_change(self):
150
tree = self.create_shelvable_tree()
151
self.build_tree_contents([('tree/foo', '\x00')])
152
tree.lock_tree_write()
153
self.addCleanup(tree.unlock)
154
shelver = ExpectShelver(tree, tree.basis_tree())
155
self.addCleanup(shelver.finalize)
156
shelver.expect('Shelve binary changes? [yNfq?]', 'y')
157
shelver.expect('Shelve 1 change(s)? [yNfq?]', 'y')
159
self.assertFileEqual(LINES_AJ, 'tree/foo')
161
def test_shelve_rename(self):
162
tree = self.create_shelvable_tree()
163
tree.rename_one('foo', 'bar')
164
tree.lock_tree_write()
165
self.addCleanup(tree.unlock)
166
shelver = ExpectShelver(tree, tree.basis_tree())
167
self.addCleanup(shelver.finalize)
168
shelver.expect('Shelve renaming "foo" => "bar"? [yNfq?]', 'y')
169
shelver.expect('Shelve? [yNfq?]', 'y')
170
shelver.expect('Shelve? [yNfq?]', 'y')
171
shelver.expect('Shelve 3 change(s)? [yNfq?]', 'y')
173
self.assertFileEqual(LINES_AJ, 'tree/foo')
175
def test_shelve_deletion(self):
176
tree = self.create_shelvable_tree()
177
os.unlink('tree/foo')
178
tree.lock_tree_write()
179
self.addCleanup(tree.unlock)
180
shelver = ExpectShelver(tree, tree.basis_tree())
181
self.addCleanup(shelver.finalize)
182
shelver.expect('Shelve removing file "foo"? [yNfq?]', 'y')
183
shelver.expect('Shelve 1 change(s)? [yNfq?]', 'y')
185
self.assertFileEqual(LINES_AJ, 'tree/foo')
187
def test_shelve_creation(self):
188
tree = self.make_branch_and_tree('tree')
189
tree.commit('add tree root')
190
self.build_tree(['tree/foo'])
192
tree.lock_tree_write()
193
self.addCleanup(tree.unlock)
194
shelver = ExpectShelver(tree, tree.basis_tree())
195
self.addCleanup(shelver.finalize)
196
shelver.expect('Shelve adding file "foo"? [yNfq?]', 'y')
197
shelver.expect('Shelve 1 change(s)? [yNfq?]', 'y')
199
self.assertPathDoesNotExist('tree/foo')
201
def test_shelve_kind_change(self):
202
tree = self.create_shelvable_tree()
203
os.unlink('tree/foo')
205
tree.lock_tree_write()
206
self.addCleanup(tree.unlock)
207
shelver = ExpectShelver(tree, tree.basis_tree())
208
self.addCleanup(shelver.finalize)
209
shelver.expect('Shelve changing "foo" from file to directory? [yNfq?]',
211
shelver.expect('Shelve 1 change(s)? [yNfq?]', 'y')
213
def test_shelve_modify_target(self):
214
self.requireFeature(features.SymlinkFeature)
215
tree = self.create_shelvable_tree()
216
os.symlink('bar', 'tree/baz')
217
tree.add('baz', 'baz-id')
218
tree.commit("Add symlink")
219
os.unlink('tree/baz')
220
os.symlink('vax', 'tree/baz')
221
tree.lock_tree_write()
222
self.addCleanup(tree.unlock)
223
shelver = ExpectShelver(tree, tree.basis_tree())
224
self.addCleanup(shelver.finalize)
225
shelver.expect('Shelve changing target of "baz" from "bar" to '
226
'"vax"? [yNfq?]', 'y')
227
shelver.expect('Shelve 1 change(s)? [yNfq?]', 'y')
229
self.assertEqual('bar', os.readlink('tree/baz'))
231
def test_shelve_finish(self):
232
tree = self.create_shelvable_tree()
233
tree.lock_tree_write()
234
self.addCleanup(tree.unlock)
235
shelver = ExpectShelver(tree, tree.basis_tree())
236
self.addCleanup(shelver.finalize)
237
shelver.expect('Shelve? [yNfq?]', 'f')
238
shelver.expect('Shelve 2 change(s)? [yNfq?]', 'y')
240
self.assertFileEqual(LINES_AJ, 'tree/foo')
242
def test_shelve_quit(self):
243
tree = self.create_shelvable_tree()
244
tree.lock_tree_write()
245
self.addCleanup(tree.unlock)
246
shelver = ExpectShelver(tree, tree.basis_tree())
247
self.addCleanup(shelver.finalize)
248
shelver.expect('Shelve? [yNfq?]', 'q')
249
self.assertRaises(errors.UserAbort, shelver.run)
250
self.assertFileEqual(LINES_ZY, 'tree/foo')
252
def test_shelve_all(self):
253
tree = self.create_shelvable_tree()
254
shelver = ExpectShelver.from_args(sys.stdout, all=True,
260
self.assertFileEqual(LINES_AJ, 'tree/foo')
262
def test_shelve_filename(self):
263
tree = self.create_shelvable_tree()
264
self.build_tree(['tree/bar'])
266
tree.lock_tree_write()
267
self.addCleanup(tree.unlock)
268
shelver = ExpectShelver(tree, tree.basis_tree(), file_list=['bar'])
269
self.addCleanup(shelver.finalize)
270
shelver.expect('Shelve adding file "bar"? [yNfq?]', 'y')
271
shelver.expect('Shelve 1 change(s)? [yNfq?]', 'y')
274
def test_shelve_help(self):
275
tree = self.create_shelvable_tree()
276
tree.lock_tree_write()
277
self.addCleanup(tree.unlock)
278
shelver = ExpectShelver(tree, tree.basis_tree())
279
self.addCleanup(shelver.finalize)
280
shelver.expect('Shelve? [yNfq?]', '?')
281
shelver.expect('Shelve? [(y)es, (N)o, (f)inish, or (q)uit]', 'f')
282
shelver.expect('Shelve 2 change(s)? [yNfq?]', 'y')
285
def test_shelve_destroy(self):
286
tree = self.create_shelvable_tree()
287
shelver = shelf_ui.Shelver.from_args(sys.stdout, all=True,
288
directory='tree', destroy=True)
289
self.addCleanup(shelver.finalize)
291
self.assertIs(None, tree.get_shelf_manager().last_shelf())
292
self.assertFileEqual(LINES_AJ, 'tree/foo')
295
def shelve_all(tree, target_revision_id):
298
target = tree.branch.repository.revision_tree(target_revision_id)
299
shelver = shelf_ui.Shelver(tree, target, auto=True,
308
def test_shelve_old_root_preserved(self):
309
tree1 = self.make_branch_and_tree('tree1')
310
tree1.commit('add root')
311
tree1_root_id = tree1.get_root_id()
312
tree2 = self.make_branch_and_tree('tree2')
313
rev2 = tree2.commit('add root')
314
self.assertNotEquals(tree1_root_id, tree2.get_root_id())
315
tree1.merge_from_branch(tree2.branch,
316
from_revision=revision.NULL_REVISION)
317
tree1.commit('merging in tree2')
318
self.assertEquals(tree1_root_id, tree1.get_root_id())
319
# This is essentially assertNotRaises(InconsistentDelta)
320
# With testtools 0.9.9, it can be rewritten as:
321
# with ExpectedException(AssertionError,
322
# 'InconsistentDelta not raised'):
323
# with ExpectedException(errors.InconsistentDelta, ''):
324
# self.shelve_all(tree1, rev2)
325
e = self.assertRaises(AssertionError, self.assertRaises,
326
errors.InconsistentDelta, self.shelve_all, tree1,
328
self.assertContainsRe('InconsistentDelta not raised', str(e))
330
def test_shelve_split(self):
331
outer_tree = self.make_branch_and_tree('outer')
332
outer_tree.commit('Add root')
333
inner_tree = self.make_branch_and_tree('outer/inner')
334
rev2 = inner_tree.commit('Add root')
335
outer_tree.subsume(inner_tree)
336
# This is essentially assertNotRaises(ValueError).
337
# The ValueError is 'None is not a valid file id'.
338
self.expectFailure('Cannot shelve a join back to the inner tree.',
339
self.assertRaises, AssertionError,
340
self.assertRaises, ValueError, self.shelve_all,
344
class TestApplyReporter(ShelfTestCase):
346
def test_shelve_not_diff(self):
347
tree = self.create_shelvable_tree()
348
tree.lock_tree_write()
349
self.addCleanup(tree.unlock)
350
shelver = ExpectShelver(tree, tree.basis_tree(),
351
reporter=shelf_ui.ApplyReporter())
352
self.addCleanup(shelver.finalize)
353
shelver.expect('Apply change? [yNfq?]', 'n')
354
shelver.expect('Apply change? [yNfq?]', 'n')
355
# No final shelving prompt because no changes were selected
357
self.assertFileEqual(LINES_ZY, 'tree/foo')
359
def test_shelve_diff_no(self):
360
tree = self.create_shelvable_tree()
361
tree.lock_tree_write()
362
self.addCleanup(tree.unlock)
363
shelver = ExpectShelver(tree, tree.basis_tree(),
364
reporter=shelf_ui.ApplyReporter())
365
self.addCleanup(shelver.finalize)
366
shelver.expect('Apply change? [yNfq?]', 'y')
367
shelver.expect('Apply change? [yNfq?]', 'y')
368
shelver.expect('Apply 2 change(s)? [yNfq?]', 'n')
370
self.assertFileEqual(LINES_ZY, 'tree/foo')
372
def test_shelve_diff(self):
373
tree = self.create_shelvable_tree()
374
tree.lock_tree_write()
375
self.addCleanup(tree.unlock)
376
shelver = ExpectShelver(tree, tree.basis_tree(),
377
reporter=shelf_ui.ApplyReporter())
378
self.addCleanup(shelver.finalize)
379
shelver.expect('Apply change? [yNfq?]', 'y')
380
shelver.expect('Apply change? [yNfq?]', 'y')
381
shelver.expect('Apply 2 change(s)? [yNfq?]', 'y')
383
self.assertFileEqual(LINES_AJ, 'tree/foo')
385
def test_shelve_binary_change(self):
386
tree = self.create_shelvable_tree()
387
self.build_tree_contents([('tree/foo', '\x00')])
388
tree.lock_tree_write()
389
self.addCleanup(tree.unlock)
390
shelver = ExpectShelver(tree, tree.basis_tree(),
391
reporter=shelf_ui.ApplyReporter())
392
self.addCleanup(shelver.finalize)
393
shelver.expect('Apply binary changes? [yNfq?]', 'y')
394
shelver.expect('Apply 1 change(s)? [yNfq?]', 'y')
396
self.assertFileEqual(LINES_AJ, 'tree/foo')
398
def test_shelve_rename(self):
399
tree = self.create_shelvable_tree()
400
tree.rename_one('foo', 'bar')
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('Rename "bar" => "foo"? [yNfq?]', 'y')
407
shelver.expect('Apply change? [yNfq?]', 'y')
408
shelver.expect('Apply change? [yNfq?]', 'y')
409
shelver.expect('Apply 3 change(s)? [yNfq?]', 'y')
411
self.assertFileEqual(LINES_AJ, 'tree/foo')
413
def test_shelve_deletion(self):
414
tree = self.create_shelvable_tree()
415
os.unlink('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('Add file "foo"? [yNfq?]', 'y')
422
shelver.expect('Apply 1 change(s)? [yNfq?]', 'y')
424
self.assertFileEqual(LINES_AJ, 'tree/foo')
426
def test_shelve_creation(self):
427
tree = self.make_branch_and_tree('tree')
428
tree.commit('add tree root')
429
self.build_tree(['tree/foo'])
431
tree.lock_tree_write()
432
self.addCleanup(tree.unlock)
433
shelver = ExpectShelver(tree, tree.basis_tree(),
434
reporter=shelf_ui.ApplyReporter())
435
self.addCleanup(shelver.finalize)
436
shelver.expect('Delete file "foo"? [yNfq?]', 'y')
437
shelver.expect('Apply 1 change(s)? [yNfq?]', 'y')
439
self.assertPathDoesNotExist('tree/foo')
441
def test_shelve_kind_change(self):
442
tree = self.create_shelvable_tree()
443
os.unlink('tree/foo')
445
tree.lock_tree_write()
446
self.addCleanup(tree.unlock)
447
shelver = ExpectShelver(tree, tree.basis_tree(),
448
reporter=shelf_ui.ApplyReporter())
449
self.addCleanup(shelver.finalize)
450
shelver.expect('Change "foo" from directory to a file? [yNfq?]', 'y')
451
shelver.expect('Apply 1 change(s)? [yNfq?]', 'y')
453
def test_shelve_modify_target(self):
454
self.requireFeature(features.SymlinkFeature)
455
tree = self.create_shelvable_tree()
456
os.symlink('bar', 'tree/baz')
457
tree.add('baz', 'baz-id')
458
tree.commit("Add symlink")
459
os.unlink('tree/baz')
460
os.symlink('vax', 'tree/baz')
461
tree.lock_tree_write()
462
self.addCleanup(tree.unlock)
463
shelver = ExpectShelver(tree, tree.basis_tree(),
464
reporter=shelf_ui.ApplyReporter())
465
self.addCleanup(shelver.finalize)
466
shelver.expect('Change target of "baz" from "vax" to "bar"? [yNfq?]',
468
shelver.expect('Apply 1 change(s)? [yNfq?]', 'y')
470
self.assertEqual('bar', os.readlink('tree/baz'))
473
class TestUnshelver(tests.TestCaseWithTransport):
475
def create_tree_with_shelf(self):
476
tree = self.make_branch_and_tree('tree')
479
self.build_tree_contents([('tree/foo', LINES_AJ)])
480
tree.add('foo', 'foo-id')
481
tree.commit('added foo')
482
self.build_tree_contents([('tree/foo', LINES_ZY)])
483
shelver = shelf_ui.Shelver(tree, tree.basis_tree(),
484
auto_apply=True, auto=True)
493
def test_unshelve(self):
494
tree = self.create_tree_with_shelf()
496
self.addCleanup(tree.unlock)
497
manager = tree.get_shelf_manager()
498
shelf_ui.Unshelver(tree, manager, 1, True, True, True).run()
499
self.assertFileEqual(LINES_ZY, 'tree/foo')
501
def test_unshelve_args(self):
502
tree = self.create_tree_with_shelf()
503
unshelver = shelf_ui.Unshelver.from_args(directory='tree')
507
unshelver.tree.unlock()
508
self.assertFileEqual(LINES_ZY, 'tree/foo')
509
self.assertIs(None, tree.get_shelf_manager().last_shelf())
511
def test_unshelve_args_dry_run(self):
512
tree = self.create_tree_with_shelf()
513
unshelver = shelf_ui.Unshelver.from_args(directory='tree',
518
unshelver.tree.unlock()
519
self.assertFileEqual(LINES_AJ, 'tree/foo')
520
self.assertEqual(1, tree.get_shelf_manager().last_shelf())
522
def test_unshelve_args_preview(self):
523
tree = self.create_tree_with_shelf()
524
write_diff_to = StringIO()
525
unshelver = shelf_ui.Unshelver.from_args(
526
directory='tree', action='preview', write_diff_to=write_diff_to)
530
unshelver.tree.unlock()
531
# The changes were not unshelved.
532
self.assertFileEqual(LINES_AJ, 'tree/foo')
533
self.assertEqual(1, tree.get_shelf_manager().last_shelf())
535
# But the diff was written to write_diff_to.
536
diff = write_diff_to.getvalue()
537
expected = dedent("""\
552
self.assertEqualDiff(expected, diff[-len(expected):])
554
def test_unshelve_args_delete_only(self):
555
tree = self.make_branch_and_tree('tree')
556
manager = tree.get_shelf_manager()
557
shelf_file = manager.new_shelf()[1]
559
shelf_file.write('garbage')
562
unshelver = shelf_ui.Unshelver.from_args(directory='tree',
563
action='delete-only')
567
unshelver.tree.unlock()
568
self.assertIs(None, manager.last_shelf())
570
def test_unshelve_args_invalid_shelf_id(self):
571
tree = self.make_branch_and_tree('tree')
572
manager = tree.get_shelf_manager()
573
shelf_file = manager.new_shelf()[1]
575
shelf_file.write('garbage')
578
self.assertRaises(errors.InvalidShelfId,
579
shelf_ui.Unshelver.from_args, directory='tree',
580
action='delete-only', shelf_id='foo')
583
class TestUnshelveScripts(TestUnshelver,
584
script.TestCaseWithTransportAndScript):
586
def test_unshelve_messages_keep(self):
587
self.create_tree_with_shelf()
590
$ bzr unshelve --keep
591
2>Using changes with id "1".
593
2>All changes applied successfully.
596
def test_unshelve_messages_delete(self):
597
self.create_tree_with_shelf()
600
$ bzr unshelve --delete-only
601
2>Deleted changes with id "1".
604
def test_unshelve_messages_apply(self):
605
self.create_tree_with_shelf()
608
$ bzr unshelve --apply
609
2>Using changes with id "1".
611
2>All changes applied successfully.
612
2>Deleted changes with id "1".
615
def test_unshelve_messages_dry_run(self):
616
self.create_tree_with_shelf()
619
$ bzr unshelve --dry-run
620
2>Using changes with id "1".