~bzr-pqm/bzr/bzr.dev

« back to all changes in this revision

Viewing changes to bzrlib/tests/test_shelf.py

  • Committer: Matt Nordhoff
  • Date: 2009-06-23 05:12:07 UTC
  • mto: This revision was merged to the branch mainline in revision 4474.
  • Revision ID: mnordhoff@mattnordhoff.com-20090623051207-fksdtbzkwtnrw9dd
Update _add_text docstrings that still referred to add_text.

Show diffs side-by-side

added added

removed removed

Lines of Context:
40
40
 
41
41
class TestPrepareShelf(tests.TestCaseWithTransport):
42
42
 
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'])
47
47
        tree.commit('foo')
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()))
55
 
        return creator
56
 
 
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(
60
56
                         work_trans_id))
62
58
        self.assertEqual('bar', creator.shelf_transform.final_name(
63
59
                         shelf_trans_id))
64
60
 
65
 
    def test_shelve_rename(self):
66
 
        creator = self.prepare_shelve_rename()
67
 
        creator.shelve_rename('foo-id')
68
 
        self.check_shelve_rename(creator)
69
 
 
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)
74
 
 
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'])
79
65
        tree.commit('foo')
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()))
87
 
        return creator, tree
88
 
 
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'))
100
82
 
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)
105
 
 
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)
110
 
 
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)
115
87
 
116
 
    def prepare_content_change(self):
 
88
    def test_shelve_content_change(self):
117
89
        tree = self.make_branch_and_tree('.')
118
90
        tree.lock_write()
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)
126
 
        return creator
127
 
 
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')
136
104
 
137
 
    def test_shelve_change_handles_modify_text(self):
138
 
        creator = self.prepare_content_change()
139
 
        creator.shelve_change(('modify text', 'foo-id'))
140
 
        creator.transform()
141
 
        self.assertFileEqual('a\n', 'foo')
142
 
        self.assertShelvedFileEqual('b\na\nc\n', creator, 'foo-id')
143
 
 
144
 
    def test_shelve_all(self):
145
 
        creator = self.prepare_content_change()
146
 
        creator.shelve_all()
147
 
        creator.transform()
148
 
        self.assertFileEqual('a\n', 'foo')
149
 
        self.assertShelvedFileEqual('b\na\nc\n', creator, 'foo-id')
150
 
 
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())))
163
 
        return creator, tree
164
 
 
165
 
    def check_shelve_creation(self, creator, tree):
 
117
        creator.shelve_creation('foo-id')
 
118
        creator.shelve_creation('bar-id')
 
119
        creator.transform()
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))
177
131
 
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')
182
 
        creator.transform()
183
 
        self.check_shelve_creation(creator, tree)
184
 
 
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'))
189
 
        creator.transform()
190
 
        self.check_shelve_creation(creator, tree)
191
 
 
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()))
205
 
        if shelve_change:
206
 
            creator.shelve_change(('add file', 'foo-id', 'symlink', link_name))
207
 
        else:
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')
224
160
 
225
 
    def test_shelve_change_handles_symlink_creation(self):
226
 
        self._test_shelve_symlink_creation('foo', 'bar', shelve_change=True)
227
 
 
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()))
245
 
        if shelve_change:
246
 
            creator.shelve_change(('modify target', 'foo-id', link_name,
247
 
                                   old_target, new_target))
248
 
        else:
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')
265
193
 
266
 
    def test_shelve_change_handles_symlink_target_change(self):
267
 
        self._test_shelve_symlink_target_change('foo', 'bar', 'baz',
268
 
                                                shelve_change=True)
269
 
 
270
194
    def test_shelve_creation_no_contents(self):
271
195
        tree = self.make_branch_and_tree('.')
272
196
        tree.lock_write()
289
213
                         creator.shelf_transform.final_file_id(s_trans_id))
290
214
        self.failIfExists('foo')
291
215
 
292
 
    def prepare_shelve_deletion(self):
 
216
    def test_shelve_deletion(self):
293
217
        tree = self.make_branch_and_tree('tree')
294
218
        tree.lock_write()
295
219
        self.addCleanup(tree.unlock)
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())))
307
 
        return creator, tree
308
 
 
309
 
    def check_shelve_deletion(self, tree):
 
231
        creator.shelve_deletion('foo-id')
 
232
        creator.shelve_deletion('bar-id')
 
233
        creator.transform()
310
234
        self.assertTrue('foo-id' in tree)
311
235
        self.assertTrue('bar-id' in tree)
312
236
        self.assertFileEqual('baz', 'tree/foo/bar')
313
237
 
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')
318
 
        creator.transform()
319
 
        self.check_shelve_deletion(tree)
320
 
 
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'))
325
 
        creator.transform()
326
 
        self.check_shelve_deletion(tree)
327
 
 
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')
343
251
 
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())))
357
 
        return creator
358
 
 
359
 
    def check_shelve_change_kind(self, creator):
 
263
        creator.shelve_content_change('foo-id')
 
264
        creator.transform()
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])
364
269
 
365
 
    def test_shelve_change_kind(self):
366
 
        creator = self.prepare_shelve_change_kind()
367
 
        creator.shelve_content_change('foo-id')
368
 
        creator.transform()
369
 
        self.check_shelve_change_kind(creator)
370
 
 
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',
374
 
                               'foo'))
375
 
        creator.transform()
376
 
        self.check_shelve_change_kind(creator)
377
 
 
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))
386
 
 
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')],
402
283
 
403
284
    def test_shelve_serialization(self):
404
285
        tree = self.make_branch_and_tree('.')
405
 
        tree.lock_tree_write()
406
 
        self.addCleanup(tree.unlock)
407
286
        creator = shelf.ShelfCreator(tree, tree.basis_tree())
408
287
        self.addCleanup(creator.finalize)
409
288
        shelf_file = open('shelf', 'wb')
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())
444
321
 
445
322
    def test_shelve_unversioned(self):
446
323
        tree = self.make_branch_and_tree('tree')
447
 
        tree.lock_tree_write()
448
 
        try:
449
 
            self.assertRaises(errors.PathsNotVersionedError,
450
 
                              shelf.ShelfCreator, tree, tree.basis_tree(), ['foo'])
451
 
        finally:
452
 
            tree.unlock()
 
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
454
327
        # after itself.
455
328
        wt = workingtree.WorkingTree.open('tree')
457
330
        wt.unlock()
458
331
        # And a second tentative should raise the same error (no
459
332
        # limbo/pending_deletion leftovers).
460
 
        tree.lock_tree_write()
461
 
        try:
462
 
            self.assertRaises(errors.PathsNotVersionedError,
463
 
                              shelf.ShelfCreator, tree, tree.basis_tree(), ['foo'])
464
 
        finally:
465
 
            tree.unlock()
466
 
 
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()))
475
 
 
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'])
484
335
 
485
336
 
486
337
class TestUnshelver(tests.TestCaseWithTransport):
503
354
            shelf_file.seek(0)
504
355
            unshelver = shelf.Unshelver.from_tree_and_shelf(tree, shelf_file)
505
356
            unshelver.make_merger().do_merge()
506
 
            self.addCleanup(unshelver.finalize)
507
357
            self.assertFileEqual('bar', 'tree/foo')
508
358
        finally:
509
359
            shelf_file.close()
527
377
        self.build_tree_contents([('tree/foo', 'z\na\nb\nc\n')])
528
378
        shelf_file.seek(0)
529
379
        unshelver = shelf.Unshelver.from_tree_and_shelf(tree, shelf_file)
530
 
        self.addCleanup(unshelver.finalize)
531
380
        unshelver.make_merger().do_merge()
532
381
        self.assertFileEqual('z\na\nb\nd\n', 'tree/foo')
533
382
 
556
405
        self.assertFileEqual('baz', 'tree/foo/bar')
557
406
        shelf_file.seek(0)
558
407
        unshelver = shelf.Unshelver.from_tree_and_shelf(tree, shelf_file)
559
 
        self.addCleanup(unshelver.finalize)
560
408
        unshelver.make_merger().do_merge()
561
409
        self.assertFalse('foo-id' in tree)
562
410
        self.assertFalse('bar-id' in tree)
587
435
        shelf_file = open('shelf', 'rb')
588
436
        self.addCleanup(shelf_file.close)
589
437
        unshelver = shelf.Unshelver.from_tree_and_shelf(tree, shelf_file)
590
 
        unshelver.finalize()
591
438
 
592
439
    def test_corrupt_shelf(self):
593
440
        tree = self.make_branch_and_tree('.')
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')
708
554
 
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)