1
# Copyright (C) 2008, 2009, 2010 Canonical Ltd
1
# Copyright (C) 2008 Canonical Ltd
3
3
# This program is free software; you can redistribute it and/or modify
4
4
# it under the terms of the GNU General Public License as published by
66
61
LINES_AY = 'a\nb\nc\nd\ne\nf\ng\nh\ni\ny\n'
69
class ShelfTestCase(tests.TestCaseWithTransport):
64
class TestShelver(tests.TestCaseWithTransport):
71
66
def create_shelvable_tree(self):
72
67
tree = self.make_branch_and_tree('tree')
76
71
self.build_tree_contents([('tree/foo', LINES_ZY)])
80
class TestShelver(ShelfTestCase):
82
74
def test_unexpected_prompt_failure(self):
83
75
tree = self.create_shelvable_tree()
84
76
tree.lock_tree_write()
85
77
self.addCleanup(tree.unlock)
86
78
shelver = ExpectShelver(tree, tree.basis_tree())
87
self.addCleanup(shelver.finalize)
88
79
e = self.assertRaises(AssertionError, shelver.run)
89
80
self.assertEqual('Unexpected prompt: Shelve? [yNfq?]', str(e))
93
84
tree.lock_tree_write()
94
85
self.addCleanup(tree.unlock)
95
86
shelver = ExpectShelver(tree, tree.basis_tree())
96
self.addCleanup(shelver.finalize)
97
87
shelver.expect('foo', 'y')
98
88
e = self.assertRaises(AssertionError, shelver.run)
99
89
self.assertEqual('Wrong prompt: Shelve? [yNfq?]', str(e))
103
93
tree.lock_tree_write()
104
94
self.addCleanup(tree.unlock)
105
95
shelver = ExpectShelver(tree, tree.basis_tree())
106
self.addCleanup(shelver.finalize)
107
96
shelver.expect('Shelve? [yNfq?]', 'n')
108
97
shelver.expect('Shelve? [yNfq?]', 'n')
109
98
# No final shelving prompt because no changes were selected
115
104
tree.lock_tree_write()
116
105
self.addCleanup(tree.unlock)
117
106
shelver = ExpectShelver(tree, tree.basis_tree())
118
self.addCleanup(shelver.finalize)
119
107
shelver.expect('Shelve? [yNfq?]', 'y')
120
108
shelver.expect('Shelve? [yNfq?]', 'y')
121
109
shelver.expect('Shelve 2 change(s)? [yNfq?]', 'n')
127
115
tree.lock_tree_write()
128
116
self.addCleanup(tree.unlock)
129
117
shelver = ExpectShelver(tree, tree.basis_tree())
130
self.addCleanup(shelver.finalize)
131
118
shelver.expect('Shelve? [yNfq?]', 'y')
132
119
shelver.expect('Shelve? [yNfq?]', 'y')
133
120
shelver.expect('Shelve 2 change(s)? [yNfq?]', 'y')
139
126
tree.lock_tree_write()
140
127
self.addCleanup(tree.unlock)
141
128
shelver = ExpectShelver(tree, tree.basis_tree())
142
self.addCleanup(shelver.finalize)
143
129
shelver.expect('Shelve? [yNfq?]', 'y')
144
130
shelver.expect('Shelve? [yNfq?]', 'n')
145
131
shelver.expect('Shelve 1 change(s)? [yNfq?]', 'y')
152
138
tree.lock_tree_write()
153
139
self.addCleanup(tree.unlock)
154
140
shelver = ExpectShelver(tree, tree.basis_tree())
155
self.addCleanup(shelver.finalize)
156
141
shelver.expect('Shelve binary changes? [yNfq?]', 'y')
157
142
shelver.expect('Shelve 1 change(s)? [yNfq?]', 'y')
164
149
tree.lock_tree_write()
165
150
self.addCleanup(tree.unlock)
166
151
shelver = ExpectShelver(tree, tree.basis_tree())
167
self.addCleanup(shelver.finalize)
168
152
shelver.expect('Shelve renaming "foo" => "bar"? [yNfq?]', 'y')
169
153
shelver.expect('Shelve? [yNfq?]', 'y')
170
154
shelver.expect('Shelve? [yNfq?]', 'y')
178
162
tree.lock_tree_write()
179
163
self.addCleanup(tree.unlock)
180
164
shelver = ExpectShelver(tree, tree.basis_tree())
181
self.addCleanup(shelver.finalize)
182
165
shelver.expect('Shelve removing file "foo"? [yNfq?]', 'y')
183
166
shelver.expect('Shelve 1 change(s)? [yNfq?]', 'y')
192
175
tree.lock_tree_write()
193
176
self.addCleanup(tree.unlock)
194
177
shelver = ExpectShelver(tree, tree.basis_tree())
195
self.addCleanup(shelver.finalize)
196
178
shelver.expect('Shelve adding file "foo"? [yNfq?]', 'y')
197
179
shelver.expect('Shelve 1 change(s)? [yNfq?]', 'y')
199
self.assertPathDoesNotExist('tree/foo')
181
self.failIfExists('tree/foo')
201
183
def test_shelve_kind_change(self):
202
184
tree = self.create_shelvable_tree()
205
187
tree.lock_tree_write()
206
188
self.addCleanup(tree.unlock)
207
189
shelver = ExpectShelver(tree, tree.basis_tree())
208
self.addCleanup(shelver.finalize)
209
190
shelver.expect('Shelve changing "foo" from file to directory? [yNfq?]',
211
192
shelver.expect('Shelve 1 change(s)? [yNfq?]', 'y')
213
194
def test_shelve_modify_target(self):
214
self.requireFeature(features.SymlinkFeature)
195
self.requireFeature(tests.SymlinkFeature)
215
196
tree = self.create_shelvable_tree()
216
197
os.symlink('bar', 'tree/baz')
217
198
tree.add('baz', 'baz-id')
221
202
tree.lock_tree_write()
222
203
self.addCleanup(tree.unlock)
223
204
shelver = ExpectShelver(tree, tree.basis_tree())
224
self.addCleanup(shelver.finalize)
225
205
shelver.expect('Shelve changing target of "baz" from "bar" to '
226
206
'"vax"? [yNfq?]', 'y')
227
207
shelver.expect('Shelve 1 change(s)? [yNfq?]', 'y')
233
213
tree.lock_tree_write()
234
214
self.addCleanup(tree.unlock)
235
215
shelver = ExpectShelver(tree, tree.basis_tree())
236
self.addCleanup(shelver.finalize)
237
216
shelver.expect('Shelve? [yNfq?]', 'f')
238
217
shelver.expect('Shelve 2 change(s)? [yNfq?]', 'y')
244
223
tree.lock_tree_write()
245
224
self.addCleanup(tree.unlock)
246
225
shelver = ExpectShelver(tree, tree.basis_tree())
247
self.addCleanup(shelver.finalize)
248
226
shelver.expect('Shelve? [yNfq?]', 'q')
249
227
self.assertRaises(errors.UserAbort, shelver.run)
250
228
self.assertFileEqual(LINES_ZY, 'tree/foo')
266
244
tree.lock_tree_write()
267
245
self.addCleanup(tree.unlock)
268
246
shelver = ExpectShelver(tree, tree.basis_tree(), file_list=['bar'])
269
self.addCleanup(shelver.finalize)
270
247
shelver.expect('Shelve adding file "bar"? [yNfq?]', 'y')
271
248
shelver.expect('Shelve 1 change(s)? [yNfq?]', 'y')
276
253
tree.lock_tree_write()
277
254
self.addCleanup(tree.unlock)
278
255
shelver = ExpectShelver(tree, tree.basis_tree())
279
self.addCleanup(shelver.finalize)
280
256
shelver.expect('Shelve? [yNfq?]', '?')
281
257
shelver.expect('Shelve? [(y)es, (N)o, (f)inish, or (q)uit]', 'f')
282
258
shelver.expect('Shelve 2 change(s)? [yNfq?]', 'y')
285
def test_shelve_destroy(self):
261
def test_shelve_distroy(self):
286
262
tree = self.create_shelvable_tree()
287
263
shelver = shelf_ui.Shelver.from_args(sys.stdout, all=True,
288
264
directory='tree', destroy=True)
289
self.addCleanup(shelver.finalize)
268
shelver.work_tree.unlock()
291
269
self.assertIs(None, tree.get_shelf_manager().last_shelf())
292
270
self.assertFileEqual(LINES_AJ, 'tree/foo')
298
276
target = tree.branch.repository.revision_tree(target_revision_id)
299
277
shelver = shelf_ui.Shelver(tree, target, auto=True,
308
def test_shelve_old_root_preserved(self):
283
def test_shelve_old_root_deleted(self):
309
284
tree1 = self.make_branch_and_tree('tree1')
310
285
tree1.commit('add root')
311
tree1_root_id = tree1.get_root_id()
312
286
tree2 = self.make_branch_and_tree('tree2')
313
287
rev2 = tree2.commit('add root')
314
self.assertNotEquals(tree1_root_id, tree2.get_root_id())
315
288
tree1.merge_from_branch(tree2.branch,
316
289
from_revision=revision.NULL_REVISION)
317
tree1.commit('merging in tree2')
318
self.assertEquals(tree1_root_id, tree1.get_root_id())
290
tree1.commit('Replaced root entry')
319
291
# 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))
292
self.expectFailure('Cannot shelve replacing a root entry',
293
self.assertRaises, AssertionError,
294
self.assertRaises, errors.InconsistentDelta,
295
self.shelve_all, tree1, rev2)
330
297
def test_shelve_split(self):
331
298
outer_tree = self.make_branch_and_tree('outer')
349
316
self.addCleanup(tree.unlock)
350
317
shelver = ExpectShelver(tree, tree.basis_tree(),
351
318
reporter=shelf_ui.ApplyReporter())
352
self.addCleanup(shelver.finalize)
353
319
shelver.expect('Apply change? [yNfq?]', 'n')
354
320
shelver.expect('Apply change? [yNfq?]', 'n')
355
321
# No final shelving prompt because no changes were selected
362
328
self.addCleanup(tree.unlock)
363
329
shelver = ExpectShelver(tree, tree.basis_tree(),
364
330
reporter=shelf_ui.ApplyReporter())
365
self.addCleanup(shelver.finalize)
366
331
shelver.expect('Apply change? [yNfq?]', 'y')
367
332
shelver.expect('Apply change? [yNfq?]', 'y')
368
333
shelver.expect('Apply 2 change(s)? [yNfq?]', 'n')
375
340
self.addCleanup(tree.unlock)
376
341
shelver = ExpectShelver(tree, tree.basis_tree(),
377
342
reporter=shelf_ui.ApplyReporter())
378
self.addCleanup(shelver.finalize)
379
343
shelver.expect('Apply change? [yNfq?]', 'y')
380
344
shelver.expect('Apply change? [yNfq?]', 'y')
381
345
shelver.expect('Apply 2 change(s)? [yNfq?]', 'y')
389
353
self.addCleanup(tree.unlock)
390
354
shelver = ExpectShelver(tree, tree.basis_tree(),
391
355
reporter=shelf_ui.ApplyReporter())
392
self.addCleanup(shelver.finalize)
393
356
shelver.expect('Apply binary changes? [yNfq?]', 'y')
394
357
shelver.expect('Apply 1 change(s)? [yNfq?]', 'y')
402
365
self.addCleanup(tree.unlock)
403
366
shelver = ExpectShelver(tree, tree.basis_tree(),
404
367
reporter=shelf_ui.ApplyReporter())
405
self.addCleanup(shelver.finalize)
406
368
shelver.expect('Rename "bar" => "foo"? [yNfq?]', 'y')
407
369
shelver.expect('Apply change? [yNfq?]', 'y')
408
370
shelver.expect('Apply change? [yNfq?]', 'y')
417
379
self.addCleanup(tree.unlock)
418
380
shelver = ExpectShelver(tree, tree.basis_tree(),
419
381
reporter=shelf_ui.ApplyReporter())
420
self.addCleanup(shelver.finalize)
421
382
shelver.expect('Add file "foo"? [yNfq?]', 'y')
422
383
shelver.expect('Apply 1 change(s)? [yNfq?]', 'y')
432
393
self.addCleanup(tree.unlock)
433
394
shelver = ExpectShelver(tree, tree.basis_tree(),
434
395
reporter=shelf_ui.ApplyReporter())
435
self.addCleanup(shelver.finalize)
436
396
shelver.expect('Delete file "foo"? [yNfq?]', 'y')
437
397
shelver.expect('Apply 1 change(s)? [yNfq?]', 'y')
439
self.assertPathDoesNotExist('tree/foo')
399
self.failIfExists('tree/foo')
441
401
def test_shelve_kind_change(self):
442
402
tree = self.create_shelvable_tree()
446
406
self.addCleanup(tree.unlock)
447
407
shelver = ExpectShelver(tree, tree.basis_tree(),
448
408
reporter=shelf_ui.ApplyReporter())
449
self.addCleanup(shelver.finalize)
450
409
shelver.expect('Change "foo" from directory to a file? [yNfq?]', 'y')
451
410
shelver.expect('Apply 1 change(s)? [yNfq?]', 'y')
453
412
def test_shelve_modify_target(self):
454
self.requireFeature(features.SymlinkFeature)
413
self.requireFeature(tests.SymlinkFeature)
455
414
tree = self.create_shelvable_tree()
456
415
os.symlink('bar', 'tree/baz')
457
416
tree.add('baz', 'baz-id')
462
421
self.addCleanup(tree.unlock)
463
422
shelver = ExpectShelver(tree, tree.basis_tree(),
464
423
reporter=shelf_ui.ApplyReporter())
465
self.addCleanup(shelver.finalize)
466
424
shelver.expect('Change target of "baz" from "vax" to "bar"? [yNfq?]',
468
426
shelver.expect('Apply 1 change(s)? [yNfq?]', 'y')
480
438
tree.add('foo', 'foo-id')
481
439
tree.commit('added foo')
482
440
self.build_tree_contents([('tree/foo', LINES_ZY)])
483
shelver = shelf_ui.Shelver(tree, tree.basis_tree(),
484
auto_apply=True, auto=True)
441
shelf_ui.Shelver(tree, tree.basis_tree(), auto_apply=True,
519
473
self.assertFileEqual(LINES_AJ, 'tree/foo')
520
474
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
476
def test_unshelve_args_delete_only(self):
555
477
tree = self.make_branch_and_tree('tree')
556
478
manager = tree.get_shelf_manager()
578
500
self.assertRaises(errors.InvalidShelfId,
579
501
shelf_ui.Unshelver.from_args, directory='tree',
580
502
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".