~bzr-pqm/bzr/bzr.dev

« back to all changes in this revision

Viewing changes to bzrlib/tests/test_shelf.py

  • Committer: Andrew Bennetts
  • Date: 2009-07-27 05:35:00 UTC
  • mfrom: (4570 +trunk)
  • mto: (4634.6.29 2.0)
  • mto: This revision was merged to the branch mainline in revision 4680.
  • Revision ID: andrew.bennetts@canonical.com-20090727053500-q76zsn2dx33jhmj5
Merge bzr.dev.

Show diffs side-by-side

added added

removed removed

Lines of Context:
12
12
#
13
13
# You should have received a copy of the GNU General Public License
14
14
# along with this program; if not, write to the Free Software
15
 
# Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307  USA
 
15
# Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
16
16
 
17
17
import os
18
18
 
19
 
from bzrlib import errors, pack, shelf, tests, transform
 
19
from bzrlib import (
 
20
    errors,
 
21
    osutils,
 
22
    pack,
 
23
    shelf,
 
24
    tests,
 
25
    transform,
 
26
    workingtree,
 
27
    )
20
28
 
21
29
 
22
30
EMPTY_SHELF = ("Bazaar pack format 1 (introduced in 0.18)\n"
32
40
 
33
41
class TestPrepareShelf(tests.TestCaseWithTransport):
34
42
 
35
 
    def test_shelve_rename(self):
 
43
    def prepare_shelve_rename(self):
36
44
        tree = self.make_branch_and_tree('.')
37
45
        self.build_tree(['foo'])
38
46
        tree.add(['foo'], ['foo-id'])
42
50
        self.addCleanup(creator.finalize)
43
51
        self.assertEqual([('rename', 'foo-id', 'foo', 'bar')],
44
52
                          list(creator.iter_shelvable()))
45
 
        creator.shelve_rename('foo-id')
 
53
        return creator
 
54
 
 
55
    def check_shelve_rename(self, creator):
46
56
        work_trans_id = creator.work_transform.trans_id_file_id('foo-id')
47
57
        self.assertEqual('foo', creator.work_transform.final_name(
48
58
                         work_trans_id))
50
60
        self.assertEqual('bar', creator.shelf_transform.final_name(
51
61
                         shelf_trans_id))
52
62
 
53
 
    def test_shelve_move(self):
 
63
    def test_shelve_rename(self):
 
64
        creator = self.prepare_shelve_rename()
 
65
        creator.shelve_rename('foo-id')
 
66
        self.check_shelve_rename(creator)
 
67
 
 
68
    def test_shelve_change_handles_rename(self):
 
69
        creator = self.prepare_shelve_rename()
 
70
        creator.shelve_change(('rename', 'foo-id', 'foo', 'bar'))
 
71
        self.check_shelve_rename(creator)
 
72
 
 
73
    def prepare_shelve_move(self):
54
74
        tree = self.make_branch_and_tree('.')
55
75
        self.build_tree(['foo/', 'bar/', 'foo/baz'])
56
76
        tree.add(['foo', 'bar', 'foo/baz'], ['foo-id', 'bar-id', 'baz-id'])
60
80
        self.addCleanup(creator.finalize)
61
81
        self.assertEqual([('rename', 'baz-id', 'foo/baz', 'bar/baz')],
62
82
                         list(creator.iter_shelvable()))
63
 
        creator.shelve_rename('baz-id')
 
83
        return creator, tree
 
84
 
 
85
    def check_shelve_move(self, creator, tree):
64
86
        work_trans_id = creator.work_transform.trans_id_file_id('baz-id')
65
87
        work_foo = creator.work_transform.trans_id_file_id('foo-id')
66
88
        self.assertEqual(work_foo, creator.work_transform.final_parent(
72
94
        creator.transform()
73
95
        self.assertEqual('foo/baz', tree.id2path('baz-id'))
74
96
 
 
97
    def test_shelve_move(self):
 
98
        creator, tree = self.prepare_shelve_move()
 
99
        creator.shelve_rename('baz-id')
 
100
        self.check_shelve_move(creator, tree)
 
101
 
 
102
    def test_shelve_change_handles_move(self):
 
103
        creator, tree = self.prepare_shelve_move()
 
104
        creator.shelve_change(('rename', 'baz-id', 'foo/baz', 'bar/baz'))
 
105
        self.check_shelve_move(creator, tree)
 
106
 
75
107
    def assertShelvedFileEqual(self, expected_content, creator, file_id):
76
108
        s_trans_id = creator.shelf_transform.trans_id_file_id(file_id)
77
109
        shelf_file = creator.shelf_transform._limbo_name(s_trans_id)
94
126
        self.assertFileEqual('a\nc\n', 'foo')
95
127
        self.assertShelvedFileEqual('b\na\n', creator, 'foo-id')
96
128
 
97
 
    def test_shelve_creation(self):
 
129
 
 
130
    def prepare_shelve_creation(self):
98
131
        tree = self.make_branch_and_tree('.')
99
132
        tree.lock_write()
100
133
        self.addCleanup(tree.unlock)
106
139
        self.assertEqual([('add file', 'bar-id', 'directory', 'bar'),
107
140
                          ('add file', 'foo-id', 'file', 'foo')],
108
141
                          sorted(list(creator.iter_shelvable())))
109
 
        creator.shelve_creation('foo-id')
110
 
        creator.shelve_creation('bar-id')
111
 
        creator.transform()
 
142
        return creator, tree
 
143
 
 
144
    def check_shelve_creation(self, creator, tree):
112
145
        self.assertRaises(StopIteration,
113
146
                          tree.iter_entries_by_dir(['foo-id']).next)
114
147
        s_trans_id = creator.shelf_transform.trans_id_file_id('foo-id')
121
154
        self.assertEqual('directory',
122
155
            creator.shelf_transform.final_kind(s_bar_trans_id))
123
156
 
 
157
    def test_shelve_creation(self):
 
158
        creator, tree = self.prepare_shelve_creation()
 
159
        creator.shelve_creation('foo-id')
 
160
        creator.shelve_creation('bar-id')
 
161
        creator.transform()
 
162
        self.check_shelve_creation(creator, tree)
 
163
 
 
164
    def test_shelve_change_handles_creation(self):
 
165
        creator, tree = self.prepare_shelve_creation()
 
166
        creator.shelve_change(('add file', 'foo-id', 'file', 'foo'))
 
167
        creator.shelve_change(('add file', 'bar-id', 'directory', 'bar'))
 
168
        creator.transform()
 
169
        self.check_shelve_creation(creator, tree)
 
170
 
 
171
    def _test_shelve_symlink_creation(self, link_name, link_target,
 
172
                                      shelve_change=False):
 
173
        self.requireFeature(tests.SymlinkFeature)
 
174
        tree = self.make_branch_and_tree('.')
 
175
        tree.lock_write()
 
176
        self.addCleanup(tree.unlock)
 
177
        tree.commit('Empty tree')
 
178
        os.symlink(link_target, link_name)
 
179
        tree.add(link_name, 'foo-id')
 
180
        creator = shelf.ShelfCreator(tree, tree.basis_tree())
 
181
        self.addCleanup(creator.finalize)
 
182
        self.assertEqual([('add file', 'foo-id', 'symlink', link_name)],
 
183
                         list(creator.iter_shelvable()))
 
184
        if shelve_change:
 
185
            creator.shelve_change(('add file', 'foo-id', 'symlink', link_name))
 
186
        else:
 
187
            creator.shelve_creation('foo-id')
 
188
        creator.transform()
 
189
        s_trans_id = creator.shelf_transform.trans_id_file_id('foo-id')
 
190
        self.failIfExists(link_name)
 
191
        limbo_name = creator.shelf_transform._limbo_name(s_trans_id)
 
192
        self.assertEqual(link_target, osutils.readlink(limbo_name))
 
193
        ptree = creator.shelf_transform.get_preview_tree()
 
194
        self.assertEqual(link_target, ptree.get_symlink_target('foo-id'))
 
195
 
124
196
    def test_shelve_symlink_creation(self):
 
197
        self._test_shelve_symlink_creation('foo', 'bar')
 
198
 
 
199
    def test_shelve_unicode_symlink_creation(self):
 
200
        self.requireFeature(tests.UnicodeFilenameFeature)
 
201
        self._test_shelve_symlink_creation(u'fo\N{Euro Sign}o',
 
202
                                           u'b\N{Euro Sign}ar')
 
203
 
 
204
    def test_shelve_change_handles_symlink_creation(self):
 
205
        self._test_shelve_symlink_creation('foo', 'bar', shelve_change=True)
 
206
 
 
207
    def _test_shelve_symlink_target_change(self, link_name,
 
208
                                           old_target, new_target,
 
209
                                           shelve_change=False):
125
210
        self.requireFeature(tests.SymlinkFeature)
126
211
        tree = self.make_branch_and_tree('.')
127
212
        tree.lock_write()
128
213
        self.addCleanup(tree.unlock)
129
 
        tree.commit('Empty tree')
130
 
        os.symlink('bar', 'foo')
131
 
        tree.add('foo', 'foo-id')
 
214
        os.symlink(old_target, link_name)
 
215
        tree.add(link_name, 'foo-id')
 
216
        tree.commit("commit symlink")
 
217
        os.unlink(link_name)
 
218
        os.symlink(new_target, link_name)
132
219
        creator = shelf.ShelfCreator(tree, tree.basis_tree())
133
220
        self.addCleanup(creator.finalize)
134
 
        self.assertEqual([('add file', 'foo-id', 'symlink', 'foo')],
 
221
        self.assertEqual([('modify target', 'foo-id', link_name,
 
222
                           old_target, new_target)],
135
223
                         list(creator.iter_shelvable()))
136
 
        creator.shelve_creation('foo-id')
 
224
        if shelve_change:
 
225
            creator.shelve_change(('modify target', 'foo-id', link_name,
 
226
                                   old_target, new_target))
 
227
        else:
 
228
            creator.shelve_modify_target('foo-id')
137
229
        creator.transform()
 
230
        self.assertEqual(old_target, osutils.readlink(link_name))
138
231
        s_trans_id = creator.shelf_transform.trans_id_file_id('foo-id')
139
 
        self.failIfExists('foo')
140
232
        limbo_name = creator.shelf_transform._limbo_name(s_trans_id)
141
 
        self.assertEqual('bar', os.readlink(limbo_name))
 
233
        self.assertEqual(new_target, osutils.readlink(limbo_name))
 
234
        ptree = creator.shelf_transform.get_preview_tree()
 
235
        self.assertEqual(new_target, ptree.get_symlink_target('foo-id'))
 
236
 
 
237
    def test_shelve_symlink_target_change(self):
 
238
        self._test_shelve_symlink_target_change('foo', 'bar', 'baz')
 
239
 
 
240
    def test_shelve_unicode_symlink_target_change(self):
 
241
        self.requireFeature(tests.UnicodeFilenameFeature)
 
242
        self._test_shelve_symlink_target_change(
 
243
            u'fo\N{Euro Sign}o', u'b\N{Euro Sign}ar', u'b\N{Euro Sign}az')
 
244
 
 
245
    def test_shelve_change_handles_symlink_target_change(self):
 
246
        self._test_shelve_symlink_target_change('foo', 'bar', 'baz',
 
247
                                                shelve_change=True)
142
248
 
143
249
    def test_shelve_creation_no_contents(self):
144
250
        tree = self.make_branch_and_tree('.')
162
268
                         creator.shelf_transform.final_file_id(s_trans_id))
163
269
        self.failIfExists('foo')
164
270
 
165
 
    def test_shelve_deletion(self):
 
271
    def prepare_shelve_deletion(self):
166
272
        tree = self.make_branch_and_tree('tree')
167
273
        tree.lock_write()
168
274
        self.addCleanup(tree.unlock)
177
283
        self.assertEqual([('delete file', 'bar-id', 'file', 'foo/bar'),
178
284
                          ('delete file', 'foo-id', 'directory', 'foo')],
179
285
                          sorted(list(creator.iter_shelvable())))
180
 
        creator.shelve_deletion('foo-id')
181
 
        creator.shelve_deletion('bar-id')
182
 
        creator.transform()
 
286
        return creator, tree
 
287
 
 
288
    def check_shelve_deletion(self, tree):
183
289
        self.assertTrue('foo-id' in tree)
184
290
        self.assertTrue('bar-id' in tree)
185
291
        self.assertFileEqual('baz', 'tree/foo/bar')
186
292
 
 
293
    def test_shelve_deletion(self):
 
294
        creator, tree = self.prepare_shelve_deletion()
 
295
        creator.shelve_deletion('foo-id')
 
296
        creator.shelve_deletion('bar-id')
 
297
        creator.transform()
 
298
        self.check_shelve_deletion(tree)
 
299
 
 
300
    def test_shelve_change_handles_deletion(self):
 
301
        creator, tree = self.prepare_shelve_deletion()
 
302
        creator.shelve_change(('delete file', 'foo-id', 'directory', 'foo'))
 
303
        creator.shelve_change(('delete file', 'bar-id', 'file', 'foo/bar'))
 
304
        creator.transform()
 
305
        self.check_shelve_deletion(tree)
 
306
 
187
307
    def test_shelve_delete_contents(self):
188
308
        tree = self.make_branch_and_tree('tree')
189
309
        self.build_tree(['tree/foo',])
198
318
        creator.transform()
199
319
        self.failUnlessExists('tree/foo')
200
320
 
201
 
    def test_shelve_change_kind(self):
 
321
    def prepare_shelve_change_kind(self):
202
322
        tree = self.make_branch_and_tree('tree')
203
323
        self.build_tree_contents([('tree/foo', 'bar')])
204
324
        tree.add('foo', 'foo-id')
209
329
        self.addCleanup(creator.finalize)
210
330
        self.assertEqual([('change kind', 'foo-id', 'file', 'directory',
211
331
                           'foo')], sorted(list(creator.iter_shelvable())))
 
332
        return creator
 
333
 
 
334
    def check_shelve_change_kind(self, creator):
 
335
        self.assertFileEqual('bar', 'tree/foo')
 
336
        s_trans_id = creator.shelf_transform.trans_id_file_id('foo-id')
 
337
        self.assertEqual('directory',
 
338
                         creator.shelf_transform._new_contents[s_trans_id])
 
339
 
 
340
    def test_shelve_change_kind(self):
 
341
        creator = self.prepare_shelve_change_kind()
212
342
        creator.shelve_content_change('foo-id')
213
343
        creator.transform()
214
 
        self.assertFileEqual('bar', 'tree/foo')
215
 
        s_trans_id = creator.shelf_transform.trans_id_file_id('foo-id')
216
 
        self.assertEqual('directory',
217
 
                         creator.shelf_transform._new_contents[s_trans_id])
 
344
        self.check_shelve_change_kind(creator)
 
345
 
 
346
    def test_shelve_change_handles_change_kind(self):
 
347
        creator = self.prepare_shelve_change_kind()
 
348
        creator.shelve_change(('change kind', 'foo-id', 'file', 'directory',
 
349
                               'foo'))
 
350
        creator.transform()
 
351
        self.check_shelve_change_kind(creator)
 
352
 
 
353
    def test_shelve_change_unknown_change(self):
 
354
        tree = self.make_branch_and_tree('tree')
 
355
        creator = shelf.ShelfCreator(tree, tree.basis_tree())
 
356
        self.addCleanup(creator.finalize)
 
357
        e = self.assertRaises(ValueError, creator.shelve_change, ('unknown',))
 
358
        self.assertEqual('Unknown change kind: "unknown"', str(e))
218
359
 
219
360
    def test_shelve_unversion(self):
220
361
        tree = self.make_branch_and_tree('tree')
268
409
        records.next()
269
410
        tt.deserialize(records)
270
411
 
 
412
    def test_shelve_unversioned(self):
 
413
        tree = self.make_branch_and_tree('tree')
 
414
        self.assertRaises(errors.PathsNotVersionedError,
 
415
                          shelf.ShelfCreator, tree, tree.basis_tree(), ['foo'])
 
416
        # We should be able to lock/unlock the tree if ShelfCreator cleaned
 
417
        # after itself.
 
418
        wt = workingtree.WorkingTree.open('tree')
 
419
        wt.lock_tree_write()
 
420
        wt.unlock()
 
421
        # And a second tentative should raise the same error (no
 
422
        # limbo/pending_deletion leftovers).
 
423
        self.assertRaises(errors.PathsNotVersionedError,
 
424
                          shelf.ShelfCreator, tree, tree.basis_tree(), ['foo'])
 
425
 
271
426
 
272
427
class TestUnshelver(tests.TestCaseWithTransport):
273
428