41
41
class TestPrepareShelf(tests.TestCaseWithTransport):
43
def prepare_shelve_rename(self):
43
def test_shelve_rename(self):
44
44
tree = self.make_branch_and_tree('.')
45
45
self.build_tree(['foo'])
46
46
tree.add(['foo'], ['foo-id'])
48
48
tree.rename_one('foo', 'bar')
49
tree.lock_tree_write()
50
self.addCleanup(tree.unlock)
51
49
creator = shelf.ShelfCreator(tree, tree.basis_tree())
52
50
self.addCleanup(creator.finalize)
53
51
self.assertEqual([('rename', 'foo-id', 'foo', 'bar')],
54
52
list(creator.iter_shelvable()))
57
def check_shelve_rename(self, creator):
53
creator.shelve_rename('foo-id')
58
54
work_trans_id = creator.work_transform.trans_id_file_id('foo-id')
59
55
self.assertEqual('foo', creator.work_transform.final_name(
62
58
self.assertEqual('bar', creator.shelf_transform.final_name(
65
def test_shelve_rename(self):
66
creator = self.prepare_shelve_rename()
67
creator.shelve_rename('foo-id')
68
self.check_shelve_rename(creator)
70
def test_shelve_change_handles_rename(self):
71
creator = self.prepare_shelve_rename()
72
creator.shelve_change(('rename', 'foo-id', 'foo', 'bar'))
73
self.check_shelve_rename(creator)
75
def prepare_shelve_move(self):
61
def test_shelve_move(self):
76
62
tree = self.make_branch_and_tree('.')
77
63
self.build_tree(['foo/', 'bar/', 'foo/baz'])
78
64
tree.add(['foo', 'bar', 'foo/baz'], ['foo-id', 'bar-id', 'baz-id'])
80
66
tree.rename_one('foo/baz', 'bar/baz')
81
tree.lock_tree_write()
82
self.addCleanup(tree.unlock)
83
67
creator = shelf.ShelfCreator(tree, tree.basis_tree())
84
68
self.addCleanup(creator.finalize)
85
69
self.assertEqual([('rename', 'baz-id', 'foo/baz', 'bar/baz')],
86
70
list(creator.iter_shelvable()))
89
def check_shelve_move(self, creator, tree):
71
creator.shelve_rename('baz-id')
90
72
work_trans_id = creator.work_transform.trans_id_file_id('baz-id')
91
73
work_foo = creator.work_transform.trans_id_file_id('foo-id')
92
74
self.assertEqual(work_foo, creator.work_transform.final_parent(
98
80
creator.transform()
99
81
self.assertEqual('foo/baz', tree.id2path('baz-id'))
101
def test_shelve_move(self):
102
creator, tree = self.prepare_shelve_move()
103
creator.shelve_rename('baz-id')
104
self.check_shelve_move(creator, tree)
106
def test_shelve_change_handles_move(self):
107
creator, tree = self.prepare_shelve_move()
108
creator.shelve_change(('rename', 'baz-id', 'foo/baz', 'bar/baz'))
109
self.check_shelve_move(creator, tree)
111
83
def assertShelvedFileEqual(self, expected_content, creator, file_id):
112
84
s_trans_id = creator.shelf_transform.trans_id_file_id(file_id)
113
85
shelf_file = creator.shelf_transform._limbo_name(s_trans_id)
114
86
self.assertFileEqual(expected_content, shelf_file)
116
def prepare_content_change(self):
88
def test_shelve_content_change(self):
117
89
tree = self.make_branch_and_tree('.')
119
91
self.addCleanup(tree.unlock)
123
95
self.build_tree_contents([('foo', 'b\na\nc\n')])
124
96
creator = shelf.ShelfCreator(tree, tree.basis_tree())
125
97
self.addCleanup(creator.finalize)
128
def test_shelve_content_change(self):
129
creator = self.prepare_content_change()
130
98
self.assertEqual([('modify text', 'foo-id')],
131
99
list(creator.iter_shelvable()))
132
100
creator.shelve_lines('foo-id', ['a\n', 'c\n'])
134
102
self.assertFileEqual('a\nc\n', 'foo')
135
103
self.assertShelvedFileEqual('b\na\n', creator, 'foo-id')
137
def test_shelve_change_handles_modify_text(self):
138
creator = self.prepare_content_change()
139
creator.shelve_change(('modify text', 'foo-id'))
141
self.assertFileEqual('a\n', 'foo')
142
self.assertShelvedFileEqual('b\na\nc\n', creator, 'foo-id')
144
def test_shelve_all(self):
145
creator = self.prepare_content_change()
148
self.assertFileEqual('a\n', 'foo')
149
self.assertShelvedFileEqual('b\na\nc\n', creator, 'foo-id')
151
def prepare_shelve_creation(self):
105
def test_shelve_creation(self):
152
106
tree = self.make_branch_and_tree('.')
153
107
tree.lock_write()
154
108
self.addCleanup(tree.unlock)
160
114
self.assertEqual([('add file', 'bar-id', 'directory', 'bar'),
161
115
('add file', 'foo-id', 'file', 'foo')],
162
116
sorted(list(creator.iter_shelvable())))
165
def check_shelve_creation(self, creator, tree):
117
creator.shelve_creation('foo-id')
118
creator.shelve_creation('bar-id')
166
120
self.assertRaises(StopIteration,
167
121
tree.iter_entries_by_dir(['foo-id']).next)
168
122
s_trans_id = creator.shelf_transform.trans_id_file_id('foo-id')
175
129
self.assertEqual('directory',
176
130
creator.shelf_transform.final_kind(s_bar_trans_id))
178
def test_shelve_creation(self):
179
creator, tree = self.prepare_shelve_creation()
180
creator.shelve_creation('foo-id')
181
creator.shelve_creation('bar-id')
183
self.check_shelve_creation(creator, tree)
185
def test_shelve_change_handles_creation(self):
186
creator, tree = self.prepare_shelve_creation()
187
creator.shelve_change(('add file', 'foo-id', 'file', 'foo'))
188
creator.shelve_change(('add file', 'bar-id', 'directory', 'bar'))
190
self.check_shelve_creation(creator, tree)
192
def _test_shelve_symlink_creation(self, link_name, link_target,
193
shelve_change=False):
132
def _test_shelve_symlink_creation(self, link_name, link_target):
194
133
self.requireFeature(tests.SymlinkFeature)
195
134
tree = self.make_branch_and_tree('.')
196
135
tree.lock_write()
202
141
self.addCleanup(creator.finalize)
203
142
self.assertEqual([('add file', 'foo-id', 'symlink', link_name)],
204
143
list(creator.iter_shelvable()))
206
creator.shelve_change(('add file', 'foo-id', 'symlink', link_name))
208
creator.shelve_creation('foo-id')
144
creator.shelve_creation('foo-id')
209
145
creator.transform()
210
146
s_trans_id = creator.shelf_transform.trans_id_file_id('foo-id')
211
147
self.failIfExists(link_name)
222
158
self._test_shelve_symlink_creation(u'fo\N{Euro Sign}o',
223
159
u'b\N{Euro Sign}ar')
225
def test_shelve_change_handles_symlink_creation(self):
226
self._test_shelve_symlink_creation('foo', 'bar', shelve_change=True)
228
161
def _test_shelve_symlink_target_change(self, link_name,
229
old_target, new_target,
230
shelve_change=False):
162
old_target, new_target):
231
163
self.requireFeature(tests.SymlinkFeature)
232
164
tree = self.make_branch_and_tree('.')
233
165
tree.lock_write()
242
174
self.assertEqual([('modify target', 'foo-id', link_name,
243
175
old_target, new_target)],
244
176
list(creator.iter_shelvable()))
246
creator.shelve_change(('modify target', 'foo-id', link_name,
247
old_target, new_target))
249
creator.shelve_modify_target('foo-id')
177
creator.shelve_modify_target('foo-id')
250
178
creator.transform()
251
179
self.assertEqual(old_target, osutils.readlink(link_name))
252
180
s_trans_id = creator.shelf_transform.trans_id_file_id('foo-id')
263
191
self._test_shelve_symlink_target_change(
264
192
u'fo\N{Euro Sign}o', u'b\N{Euro Sign}ar', u'b\N{Euro Sign}az')
266
def test_shelve_change_handles_symlink_target_change(self):
267
self._test_shelve_symlink_target_change('foo', 'bar', 'baz',
270
194
def test_shelve_creation_no_contents(self):
271
195
tree = self.make_branch_and_tree('.')
272
196
tree.lock_write()
304
228
self.assertEqual([('delete file', 'bar-id', 'file', 'foo/bar'),
305
229
('delete file', 'foo-id', 'directory', 'foo')],
306
230
sorted(list(creator.iter_shelvable())))
309
def check_shelve_deletion(self, tree):
231
creator.shelve_deletion('foo-id')
232
creator.shelve_deletion('bar-id')
310
234
self.assertTrue('foo-id' in tree)
311
235
self.assertTrue('bar-id' in tree)
312
236
self.assertFileEqual('baz', 'tree/foo/bar')
314
def test_shelve_deletion(self):
315
creator, tree = self.prepare_shelve_deletion()
316
creator.shelve_deletion('foo-id')
317
creator.shelve_deletion('bar-id')
319
self.check_shelve_deletion(tree)
321
def test_shelve_change_handles_deletion(self):
322
creator, tree = self.prepare_shelve_deletion()
323
creator.shelve_change(('delete file', 'foo-id', 'directory', 'foo'))
324
creator.shelve_change(('delete file', 'bar-id', 'file', 'foo/bar'))
326
self.check_shelve_deletion(tree)
328
238
def test_shelve_delete_contents(self):
329
239
tree = self.make_branch_and_tree('tree')
330
240
self.build_tree(['tree/foo',])
331
241
tree.add('foo', 'foo-id')
332
242
tree.commit('Added file and directory')
333
243
os.unlink('tree/foo')
334
tree.lock_tree_write()
335
self.addCleanup(tree.unlock)
336
244
creator = shelf.ShelfCreator(tree, tree.basis_tree())
337
245
self.addCleanup(creator.finalize)
338
246
self.assertEqual([('delete file', 'foo-id', 'file', 'foo')],
341
249
creator.transform()
342
250
self.failUnlessExists('tree/foo')
344
def prepare_shelve_change_kind(self):
252
def test_shelve_change_kind(self):
345
253
tree = self.make_branch_and_tree('tree')
346
254
self.build_tree_contents([('tree/foo', 'bar')])
347
255
tree.add('foo', 'foo-id')
348
256
tree.commit('Added file and directory')
349
257
os.unlink('tree/foo')
350
258
os.mkdir('tree/foo')
351
tree.lock_tree_write()
352
self.addCleanup(tree.unlock)
353
259
creator = shelf.ShelfCreator(tree, tree.basis_tree())
354
260
self.addCleanup(creator.finalize)
355
261
self.assertEqual([('change kind', 'foo-id', 'file', 'directory',
356
262
'foo')], sorted(list(creator.iter_shelvable())))
359
def check_shelve_change_kind(self, creator):
263
creator.shelve_content_change('foo-id')
360
265
self.assertFileEqual('bar', 'tree/foo')
361
266
s_trans_id = creator.shelf_transform.trans_id_file_id('foo-id')
362
267
self.assertEqual('directory',
363
268
creator.shelf_transform._new_contents[s_trans_id])
365
def test_shelve_change_kind(self):
366
creator = self.prepare_shelve_change_kind()
367
creator.shelve_content_change('foo-id')
369
self.check_shelve_change_kind(creator)
371
def test_shelve_change_handles_change_kind(self):
372
creator = self.prepare_shelve_change_kind()
373
creator.shelve_change(('change kind', 'foo-id', 'file', 'directory',
376
self.check_shelve_change_kind(creator)
378
def test_shelve_change_unknown_change(self):
379
tree = self.make_branch_and_tree('tree')
380
tree.lock_tree_write()
381
self.addCleanup(tree.unlock)
382
creator = shelf.ShelfCreator(tree, tree.basis_tree())
383
self.addCleanup(creator.finalize)
384
e = self.assertRaises(ValueError, creator.shelve_change, ('unknown',))
385
self.assertEqual('Unknown change kind: "unknown"', str(e))
387
270
def test_shelve_unversion(self):
388
271
tree = self.make_branch_and_tree('tree')
389
272
self.build_tree(['tree/foo',])
390
273
tree.add('foo', 'foo-id')
391
274
tree.commit('Added file and directory')
392
275
tree.unversion(['foo-id'])
393
tree.lock_tree_write()
394
self.addCleanup(tree.unlock)
395
276
creator = shelf.ShelfCreator(tree, tree.basis_tree())
396
277
self.addCleanup(creator.finalize)
397
278
self.assertEqual([('delete file', 'foo-id', 'file', 'foo')],
418
297
tree = self.make_branch_and_tree('tree')
419
298
self.build_tree(['tree/foo'])
420
299
tree.add('foo', 'foo-id')
421
tree.lock_tree_write()
422
self.addCleanup(tree.unlock)
423
300
creator = shelf.ShelfCreator(tree, tree.basis_tree())
424
301
self.addCleanup(creator.finalize)
425
302
list(creator.iter_shelvable())
445
322
def test_shelve_unversioned(self):
446
323
tree = self.make_branch_and_tree('tree')
447
tree.lock_tree_write()
449
self.assertRaises(errors.PathsNotVersionedError,
450
shelf.ShelfCreator, tree, tree.basis_tree(), ['foo'])
324
self.assertRaises(errors.PathsNotVersionedError,
325
shelf.ShelfCreator, tree, tree.basis_tree(), ['foo'])
453
326
# We should be able to lock/unlock the tree if ShelfCreator cleaned
455
328
wt = workingtree.WorkingTree.open('tree')
458
331
# And a second tentative should raise the same error (no
459
332
# limbo/pending_deletion leftovers).
460
tree.lock_tree_write()
462
self.assertRaises(errors.PathsNotVersionedError,
463
shelf.ShelfCreator, tree, tree.basis_tree(), ['foo'])
467
def test_shelve_skips_added_root(self):
468
"""Skip adds of the root when iterating through shelvable changes."""
469
tree = self.make_branch_and_tree('tree')
470
tree.lock_tree_write()
471
self.addCleanup(tree.unlock)
472
creator = shelf.ShelfCreator(tree, tree.basis_tree())
473
self.addCleanup(creator.finalize)
474
self.assertEqual([], list(creator.iter_shelvable()))
476
def test_shelve_skips_added_root(self):
477
"""Skip adds of the root when iterating through shelvable changes."""
478
tree = self.make_branch_and_tree('tree')
479
tree.lock_tree_write()
480
self.addCleanup(tree.unlock)
481
creator = shelf.ShelfCreator(tree, tree.basis_tree())
482
self.addCleanup(creator.finalize)
483
self.assertEqual([], list(creator.iter_shelvable()))
333
self.assertRaises(errors.PathsNotVersionedError,
334
shelf.ShelfCreator, tree, tree.basis_tree(), ['foo'])
486
337
class TestUnshelver(tests.TestCaseWithTransport):
702
549
shelf_id = shelf_manager.shelve_changes(creator)
703
550
self.failIfExists('tree/foo')
704
551
unshelver = shelf_manager.get_unshelver(shelf_id)
705
self.addCleanup(unshelver.finalize)
706
552
unshelver.make_merger().do_merge()
707
553
self.assertFileEqual('bar', 'tree/foo')
709
555
def test_get_metadata(self):
710
556
tree = self.make_branch_and_tree('.')
711
tree.lock_tree_write()
712
self.addCleanup(tree.unlock)
713
557
creator = shelf.ShelfCreator(tree, tree.basis_tree())
714
self.addCleanup(creator.finalize)
715
558
shelf_manager = tree.get_shelf_manager()
716
559
shelf_id = shelf_manager.shelve_changes(creator, 'foo')
717
560
metadata = shelf_manager.get_metadata(shelf_id)