/brz/remove-bazaar

To get this branch, use:
bzr branch http://gegoxaren.bato24.eu/bzr/brz/remove-bazaar

« back to all changes in this revision

Viewing changes to breezy/tests/test_shelf.py

  • Committer: Gustav Hartvigsson
  • Date: 2021-01-09 21:36:27 UTC
  • Revision ID: gustav.hartvigsson@gmail.com-20210109213627-h1xwcutzy9m7a99b
Added 'Case Preserving Working Tree Use Cases' from Canonical Wiki

* Addod a page from the Canonical Bazaar wiki
  with information on the scmeatics of case
  perserving filesystems an a case insensitive
  filesystem works.
  
  * Needs re-work, but this will do as it is the
    same inforamoton as what was on the linked
    page in the currint documentation.

Show diffs side-by-side

added added

removed removed

Lines of Context:
1
 
# Copyright (C) 2008, 2009, 2010 Canonical Ltd
 
1
# Copyright (C) 2008-2011 Canonical Ltd
2
2
#
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
16
16
 
17
17
import os
18
18
 
19
 
from bzrlib import (
 
19
from .. import (
20
20
    errors,
 
21
    ignores,
21
22
    osutils,
22
 
    pack,
23
23
    shelf,
24
24
    tests,
25
25
    transform,
26
26
    workingtree,
27
27
    )
28
 
 
29
 
 
30
 
EMPTY_SHELF = ("Bazaar pack format 1 (introduced in 0.18)\n"
31
 
               "B23\n"
32
 
               "metadata\n\n"
33
 
               "d11:revision_id5:null:e"
34
 
               "B159\n"
35
 
               "attribs\n\n"
36
 
               "d10:_id_numberi0e18:_new_executabilityde7:_new_idde"
37
 
               "9:_new_namede11:_new_parentde16:_non_present_idsde"
38
 
               "17:_removed_contentsle11:_removed_idle14:_tree_path_idsdeeE")
 
28
from ..bzr import (
 
29
    pack,
 
30
    )
 
31
from . import (
 
32
    features,
 
33
    KnownFailure,
 
34
    )
 
35
 
 
36
 
 
37
EMPTY_SHELF = (b"Bazaar pack format 1 (introduced in 0.18)\n"
 
38
               b"B23\n"
 
39
               b"metadata\n\n"
 
40
               b"d11:revision_id5:null:e"
 
41
               b"B159\n"
 
42
               b"attribs\n\n"
 
43
               b"d10:_id_numberi0e18:_new_executabilityde7:_new_idde"
 
44
               b"9:_new_namede11:_new_parentde16:_non_present_idsde"
 
45
               b"17:_removed_contentsle11:_removed_idle14:_tree_path_idsdeeE")
 
46
 
 
47
 
 
48
class TestErrors(tests.TestCase):
 
49
 
 
50
    def test_invalid_shelf_id(self):
 
51
        invalid_id = "foo"
 
52
        err = shelf.InvalidShelfId(invalid_id)
 
53
        self.assertEqual('"foo" is not a valid shelf id, '
 
54
                         'try a number instead.', str(err))
39
55
 
40
56
 
41
57
class TestPrepareShelf(tests.TestCaseWithTransport):
43
59
    def prepare_shelve_rename(self):
44
60
        tree = self.make_branch_and_tree('.')
45
61
        self.build_tree(['foo'])
46
 
        tree.add(['foo'], ['foo-id'])
 
62
        tree.add(['foo'], [b'foo-id'])
47
63
        tree.commit('foo')
48
64
        tree.rename_one('foo', 'bar')
49
65
        tree.lock_tree_write()
50
66
        self.addCleanup(tree.unlock)
51
67
        creator = shelf.ShelfCreator(tree, tree.basis_tree())
52
68
        self.addCleanup(creator.finalize)
53
 
        self.assertEqual([('rename', 'foo-id', 'foo', 'bar')],
54
 
                          list(creator.iter_shelvable()))
 
69
        self.assertEqual([('rename', b'foo-id', 'foo', 'bar')],
 
70
                         list(creator.iter_shelvable()))
55
71
        return creator
56
72
 
57
73
    def check_shelve_rename(self, creator):
58
 
        work_trans_id = creator.work_transform.trans_id_file_id('foo-id')
 
74
        work_trans_id = creator.work_transform.trans_id_file_id(b'foo-id')
59
75
        self.assertEqual('foo', creator.work_transform.final_name(
60
76
                         work_trans_id))
61
 
        shelf_trans_id = creator.shelf_transform.trans_id_file_id('foo-id')
 
77
        shelf_trans_id = creator.shelf_transform.trans_id_file_id(b'foo-id')
62
78
        self.assertEqual('bar', creator.shelf_transform.final_name(
63
79
                         shelf_trans_id))
64
80
 
65
81
    def test_shelve_rename(self):
66
82
        creator = self.prepare_shelve_rename()
67
 
        creator.shelve_rename('foo-id')
 
83
        creator.shelve_rename(b'foo-id')
68
84
        self.check_shelve_rename(creator)
69
85
 
70
86
    def test_shelve_change_handles_rename(self):
71
87
        creator = self.prepare_shelve_rename()
72
 
        creator.shelve_change(('rename', 'foo-id', 'foo', 'bar'))
 
88
        creator.shelve_change(('rename', b'foo-id', 'foo', 'bar'))
73
89
        self.check_shelve_rename(creator)
74
90
 
75
91
    def prepare_shelve_move(self):
76
92
        tree = self.make_branch_and_tree('.')
77
93
        self.build_tree(['foo/', 'bar/', 'foo/baz'])
78
 
        tree.add(['foo', 'bar', 'foo/baz'], ['foo-id', 'bar-id', 'baz-id'])
 
94
        tree.add(['foo', 'bar', 'foo/baz'], [b'foo-id', b'bar-id', b'baz-id'])
79
95
        tree.commit('foo')
80
96
        tree.rename_one('foo/baz', 'bar/baz')
81
97
        tree.lock_tree_write()
82
98
        self.addCleanup(tree.unlock)
83
99
        creator = shelf.ShelfCreator(tree, tree.basis_tree())
84
100
        self.addCleanup(creator.finalize)
85
 
        self.assertEqual([('rename', 'baz-id', 'foo/baz', 'bar/baz')],
 
101
        self.assertEqual([('rename', b'baz-id', 'foo/baz', 'bar/baz')],
86
102
                         list(creator.iter_shelvable()))
87
103
        return creator, tree
88
104
 
89
105
    def check_shelve_move(self, creator, tree):
90
 
        work_trans_id = creator.work_transform.trans_id_file_id('baz-id')
91
 
        work_foo = creator.work_transform.trans_id_file_id('foo-id')
 
106
        work_trans_id = creator.work_transform.trans_id_file_id(b'baz-id')
 
107
        work_foo = creator.work_transform.trans_id_file_id(b'foo-id')
92
108
        self.assertEqual(work_foo, creator.work_transform.final_parent(
93
109
                         work_trans_id))
94
 
        shelf_trans_id = creator.shelf_transform.trans_id_file_id('baz-id')
95
 
        shelf_bar = creator.shelf_transform.trans_id_file_id('bar-id')
 
110
        shelf_trans_id = creator.shelf_transform.trans_id_file_id(b'baz-id')
 
111
        shelf_bar = creator.shelf_transform.trans_id_file_id(b'bar-id')
96
112
        self.assertEqual(shelf_bar, creator.shelf_transform.final_parent(
97
113
                         shelf_trans_id))
98
114
        creator.transform()
99
 
        self.assertEqual('foo/baz', tree.id2path('baz-id'))
 
115
        self.assertEqual('foo/baz', tree.id2path(b'baz-id'))
100
116
 
101
117
    def test_shelve_move(self):
102
118
        creator, tree = self.prepare_shelve_move()
103
 
        creator.shelve_rename('baz-id')
 
119
        creator.shelve_rename(b'baz-id')
104
120
        self.check_shelve_move(creator, tree)
105
121
 
106
122
    def test_shelve_change_handles_move(self):
107
123
        creator, tree = self.prepare_shelve_move()
108
 
        creator.shelve_change(('rename', 'baz-id', 'foo/baz', 'bar/baz'))
 
124
        creator.shelve_change(('rename', b'baz-id', 'foo/baz', 'bar/baz'))
109
125
        self.check_shelve_move(creator, tree)
110
126
 
111
127
    def test_shelve_changed_root_id(self):
112
128
        tree = self.make_branch_and_tree('.')
113
129
        self.build_tree(['foo'])
114
 
        tree.set_root_id('first-root-id')
115
 
        tree.add(['foo'], ['foo-id'])
 
130
        tree.set_root_id(b'first-root-id')
 
131
        tree.add(['foo'], [b'foo-id'])
116
132
        tree.commit('foo')
117
 
        tree.set_root_id('second-root-id')
 
133
        tree.set_root_id(b'second-root-id')
118
134
        tree.lock_tree_write()
119
135
        self.addCleanup(tree.unlock)
120
136
        creator = shelf.ShelfCreator(tree, tree.basis_tree())
121
137
        self.addCleanup(creator.finalize)
122
 
        self.expectFailure('shelf doesn\'t support shelving root changes yet',
 
138
        self.expectFailure(
 
139
            'shelf doesn\'t support shelving root changes yet',
123
140
            self.assertEqual, [
124
 
                ('delete file', 'first-root-id', 'directory', ''),
125
 
                ('add file', 'second-root-id', 'directory', ''),
126
 
                ('rename', 'foo-id', u'foo', u'foo'),
 
141
                ('delete file', b'first-root-id', 'directory', ''),
 
142
                ('add file', b'second-root-id', 'directory', ''),
 
143
                ('rename', b'foo-id', u'foo', u'foo'),
127
144
                ], list(creator.iter_shelvable()))
128
145
 
129
 
        self.assertEqual([('delete file', 'first-root-id', 'directory', ''),
130
 
                          ('add file', 'second-root-id', 'directory', ''),
131
 
                          ('rename', 'foo-id', u'foo', u'foo'),
132
 
                         ], list(creator.iter_shelvable()))
 
146
        self.assertEqual([('delete file', b'first-root-id', 'directory', ''),
 
147
                          ('add file', b'second-root-id', 'directory', ''),
 
148
                          ('rename', b'foo-id', u'foo', u'foo'),
 
149
                          ], list(creator.iter_shelvable()))
133
150
 
134
151
    def assertShelvedFileEqual(self, expected_content, creator, file_id):
135
152
        s_trans_id = creator.shelf_transform.trans_id_file_id(file_id)
140
157
        tree = self.make_branch_and_tree('.')
141
158
        tree.lock_write()
142
159
        self.addCleanup(tree.unlock)
143
 
        self.build_tree_contents([('foo', 'a\n')])
144
 
        tree.add('foo', 'foo-id')
 
160
        self.build_tree_contents([('foo', b'a\n')])
 
161
        tree.add('foo', b'foo-id')
145
162
        tree.commit('Committed foo')
146
 
        self.build_tree_contents([('foo', 'b\na\nc\n')])
 
163
        self.build_tree_contents([('foo', b'b\na\nc\n')])
147
164
        creator = shelf.ShelfCreator(tree, tree.basis_tree())
148
165
        self.addCleanup(creator.finalize)
149
166
        return creator
150
167
 
151
168
    def test_shelve_content_change(self):
152
169
        creator = self.prepare_content_change()
153
 
        self.assertEqual([('modify text', 'foo-id')],
 
170
        self.assertEqual([('modify text', b'foo-id')],
154
171
                         list(creator.iter_shelvable()))
155
 
        creator.shelve_lines('foo-id', ['a\n', 'c\n'])
 
172
        creator.shelve_lines(b'foo-id', [b'a\n', b'c\n'])
156
173
        creator.transform()
157
 
        self.assertFileEqual('a\nc\n', 'foo')
158
 
        self.assertShelvedFileEqual('b\na\n', creator, 'foo-id')
 
174
        self.assertFileEqual(b'a\nc\n', 'foo')
 
175
        self.assertShelvedFileEqual(b'b\na\n', creator, b'foo-id')
159
176
 
160
177
    def test_shelve_change_handles_modify_text(self):
161
178
        creator = self.prepare_content_change()
162
 
        creator.shelve_change(('modify text', 'foo-id'))
 
179
        creator.shelve_change(('modify text', b'foo-id'))
163
180
        creator.transform()
164
 
        self.assertFileEqual('a\n', 'foo')
165
 
        self.assertShelvedFileEqual('b\na\nc\n', creator, 'foo-id')
 
181
        self.assertFileEqual(b'a\n', 'foo')
 
182
        self.assertShelvedFileEqual(b'b\na\nc\n', creator, b'foo-id')
166
183
 
167
184
    def test_shelve_all(self):
168
185
        creator = self.prepare_content_change()
169
186
        creator.shelve_all()
170
187
        creator.transform()
171
 
        self.assertFileEqual('a\n', 'foo')
172
 
        self.assertShelvedFileEqual('b\na\nc\n', creator, 'foo-id')
 
188
        self.assertFileEqual(b'a\n', 'foo')
 
189
        self.assertShelvedFileEqual(b'b\na\nc\n', creator, b'foo-id')
173
190
 
174
191
    def prepare_shelve_creation(self):
175
192
        tree = self.make_branch_and_tree('.')
176
193
        tree.lock_write()
177
194
        self.addCleanup(tree.unlock)
178
195
        tree.commit('Empty tree')
179
 
        self.build_tree_contents([('foo', 'a\n'), ('bar/',)])
180
 
        tree.add(['foo', 'bar'], ['foo-id', 'bar-id'])
 
196
        self.build_tree_contents([('foo', b'a\n'), ('bar/',)])
 
197
        tree.add(['foo', 'bar'], [b'foo-id', b'bar-id'])
181
198
        creator = shelf.ShelfCreator(tree, tree.basis_tree())
182
199
        self.addCleanup(creator.finalize)
183
 
        self.assertEqual([('add file', 'bar-id', 'directory', 'bar'),
184
 
                          ('add file', 'foo-id', 'file', 'foo')],
185
 
                          sorted(list(creator.iter_shelvable())))
 
200
        self.assertEqual([('add file', b'bar-id', 'directory', 'bar'),
 
201
                          ('add file', b'foo-id', 'file', 'foo')],
 
202
                         sorted(list(creator.iter_shelvable())))
186
203
        return creator, tree
187
204
 
188
205
    def check_shelve_creation(self, creator, tree):
189
 
        self.assertRaises(StopIteration,
190
 
                          tree.iter_entries_by_dir(['foo-id']).next)
191
 
        s_trans_id = creator.shelf_transform.trans_id_file_id('foo-id')
192
 
        self.assertEqual('foo-id',
 
206
        self.assertRaises(
 
207
            StopIteration, next,
 
208
            tree.iter_entries_by_dir(specific_files=['foo']))
 
209
        s_trans_id = creator.shelf_transform.trans_id_file_id(b'foo-id')
 
210
        self.assertEqual(b'foo-id',
193
211
                         creator.shelf_transform.final_file_id(s_trans_id))
194
 
        self.failIfExists('foo')
195
 
        self.failIfExists('bar')
196
 
        self.assertShelvedFileEqual('a\n', creator, 'foo-id')
197
 
        s_bar_trans_id = creator.shelf_transform.trans_id_file_id('bar-id')
 
212
        self.assertPathDoesNotExist('foo')
 
213
        self.assertPathDoesNotExist('bar')
 
214
        self.assertShelvedFileEqual('a\n', creator, b'foo-id')
 
215
        s_bar_trans_id = creator.shelf_transform.trans_id_file_id(b'bar-id')
198
216
        self.assertEqual('directory',
199
 
            creator.shelf_transform.final_kind(s_bar_trans_id))
 
217
                         creator.shelf_transform.final_kind(s_bar_trans_id))
200
218
 
201
219
    def test_shelve_creation(self):
202
220
        creator, tree = self.prepare_shelve_creation()
203
 
        creator.shelve_creation('foo-id')
204
 
        creator.shelve_creation('bar-id')
 
221
        creator.shelve_creation(b'foo-id')
 
222
        creator.shelve_creation(b'bar-id')
205
223
        creator.transform()
206
224
        self.check_shelve_creation(creator, tree)
207
225
 
208
226
    def test_shelve_change_handles_creation(self):
209
227
        creator, tree = self.prepare_shelve_creation()
210
 
        creator.shelve_change(('add file', 'foo-id', 'file', 'foo'))
211
 
        creator.shelve_change(('add file', 'bar-id', 'directory', 'bar'))
 
228
        creator.shelve_change(('add file', b'foo-id', 'file', 'foo'))
 
229
        creator.shelve_change(('add file', b'bar-id', 'directory', 'bar'))
212
230
        creator.transform()
213
231
        self.check_shelve_creation(creator, tree)
214
232
 
 
233
    def test_shelve_directory_with_ignored(self):
 
234
        tree = self.make_branch_and_tree('.')
 
235
        tree.lock_write()
 
236
        self.addCleanup(tree.unlock)
 
237
        tree.commit('Empty tree')
 
238
        self.build_tree_contents(
 
239
            [('foo', b'a\n'), ('bar/',), ('bar/ignored', b'ign\n')])
 
240
        tree.add(['foo', 'bar'], [b'foo-id', b'bar-id'])
 
241
        creator = shelf.ShelfCreator(tree, tree.basis_tree())
 
242
        self.addCleanup(creator.finalize)
 
243
        self.assertEqual([('add file', b'bar-id', 'directory', 'bar'),
 
244
                          ('add file', b'foo-id', 'file', 'foo')],
 
245
                         sorted(list(creator.iter_shelvable())))
 
246
        ignores._set_user_ignores([])
 
247
        in_patterns = ['ignored', ]
 
248
        ignores.add_unique_user_ignores(in_patterns)
 
249
 
 
250
        creator.shelve_change(('add file', b'bar-id', 'directory', 'bar'))
 
251
        try:
 
252
            creator.transform()
 
253
            self.check_shelve_creation(creator, tree)
 
254
        except transform.MalformedTransform:
 
255
            raise KnownFailure(
 
256
                'shelving directory with ignored file: see bug #611739')
 
257
 
215
258
    def _test_shelve_symlink_creation(self, link_name, link_target,
216
259
                                      shelve_change=False):
217
 
        self.requireFeature(tests.SymlinkFeature)
 
260
        self.requireFeature(features.SymlinkFeature)
218
261
        tree = self.make_branch_and_tree('.')
219
262
        tree.lock_write()
220
263
        self.addCleanup(tree.unlock)
221
264
        tree.commit('Empty tree')
222
265
        os.symlink(link_target, link_name)
223
 
        tree.add(link_name, 'foo-id')
 
266
        tree.add(link_name, b'foo-id')
224
267
        creator = shelf.ShelfCreator(tree, tree.basis_tree())
225
268
        self.addCleanup(creator.finalize)
226
 
        self.assertEqual([('add file', 'foo-id', 'symlink', link_name)],
 
269
        self.assertEqual([('add file', b'foo-id', 'symlink', link_name)],
227
270
                         list(creator.iter_shelvable()))
228
271
        if shelve_change:
229
 
            creator.shelve_change(('add file', 'foo-id', 'symlink', link_name))
 
272
            creator.shelve_change(
 
273
                ('add file', b'foo-id', 'symlink', link_name))
230
274
        else:
231
 
            creator.shelve_creation('foo-id')
 
275
            creator.shelve_creation(b'foo-id')
232
276
        creator.transform()
233
 
        s_trans_id = creator.shelf_transform.trans_id_file_id('foo-id')
234
 
        self.failIfExists(link_name)
 
277
        s_trans_id = creator.shelf_transform.trans_id_file_id(b'foo-id')
 
278
        self.assertPathDoesNotExist(link_name)
235
279
        limbo_name = creator.shelf_transform._limbo_name(s_trans_id)
236
280
        self.assertEqual(link_target, osutils.readlink(limbo_name))
237
281
        ptree = creator.shelf_transform.get_preview_tree()
238
 
        self.assertEqual(link_target, ptree.get_symlink_target('foo-id'))
 
282
        self.assertEqual(
 
283
            link_target,
 
284
            ptree.get_symlink_target(ptree.id2path(b'foo-id')))
239
285
 
240
286
    def test_shelve_symlink_creation(self):
241
287
        self._test_shelve_symlink_creation('foo', 'bar')
242
288
 
243
289
    def test_shelve_unicode_symlink_creation(self):
244
 
        self.requireFeature(tests.UnicodeFilenameFeature)
 
290
        self.requireFeature(features.UnicodeFilenameFeature)
245
291
        self._test_shelve_symlink_creation(u'fo\N{Euro Sign}o',
246
292
                                           u'b\N{Euro Sign}ar')
247
293
 
251
297
    def _test_shelve_symlink_target_change(self, link_name,
252
298
                                           old_target, new_target,
253
299
                                           shelve_change=False):
254
 
        self.requireFeature(tests.SymlinkFeature)
 
300
        self.requireFeature(features.SymlinkFeature)
255
301
        tree = self.make_branch_and_tree('.')
256
302
        tree.lock_write()
257
303
        self.addCleanup(tree.unlock)
258
304
        os.symlink(old_target, link_name)
259
 
        tree.add(link_name, 'foo-id')
 
305
        tree.add(link_name, b'foo-id')
260
306
        tree.commit("commit symlink")
261
307
        os.unlink(link_name)
262
308
        os.symlink(new_target, link_name)
263
309
        creator = shelf.ShelfCreator(tree, tree.basis_tree())
264
310
        self.addCleanup(creator.finalize)
265
 
        self.assertEqual([('modify target', 'foo-id', link_name,
 
311
        self.assertEqual([('modify target', b'foo-id', link_name,
266
312
                           old_target, new_target)],
267
313
                         list(creator.iter_shelvable()))
268
314
        if shelve_change:
269
 
            creator.shelve_change(('modify target', 'foo-id', link_name,
 
315
            creator.shelve_change(('modify target', b'foo-id', link_name,
270
316
                                   old_target, new_target))
271
317
        else:
272
 
            creator.shelve_modify_target('foo-id')
 
318
            creator.shelve_modify_target(b'foo-id')
273
319
        creator.transform()
274
320
        self.assertEqual(old_target, osutils.readlink(link_name))
275
 
        s_trans_id = creator.shelf_transform.trans_id_file_id('foo-id')
 
321
        s_trans_id = creator.shelf_transform.trans_id_file_id(b'foo-id')
276
322
        limbo_name = creator.shelf_transform._limbo_name(s_trans_id)
277
323
        self.assertEqual(new_target, osutils.readlink(limbo_name))
278
324
        ptree = creator.shelf_transform.get_preview_tree()
279
 
        self.assertEqual(new_target, ptree.get_symlink_target('foo-id'))
 
325
        self.assertEqual(new_target, ptree.get_symlink_target(
 
326
            ptree.id2path(b'foo-id')))
280
327
 
281
328
    def test_shelve_symlink_target_change(self):
282
329
        self._test_shelve_symlink_target_change('foo', 'bar', 'baz')
283
330
 
284
331
    def test_shelve_unicode_symlink_target_change(self):
285
 
        self.requireFeature(tests.UnicodeFilenameFeature)
 
332
        self.requireFeature(features.UnicodeFilenameFeature)
286
333
        self._test_shelve_symlink_target_change(
287
334
            u'fo\N{Euro Sign}o', u'b\N{Euro Sign}ar', u'b\N{Euro Sign}az')
288
335
 
296
343
        self.addCleanup(tree.unlock)
297
344
        tree.commit('Empty tree')
298
345
        self.build_tree(['foo'])
299
 
        tree.add('foo', 'foo-id')
 
346
        tree.add('foo', b'foo-id')
300
347
        os.unlink('foo')
301
348
        creator = shelf.ShelfCreator(tree, tree.basis_tree())
302
349
        self.addCleanup(creator.finalize)
303
 
        self.assertEqual([('add file', 'foo-id', None, 'foo')],
 
350
        self.assertEqual([('add file', b'foo-id', None, 'foo')],
304
351
                         sorted(list(creator.iter_shelvable())))
305
 
        creator.shelve_creation('foo-id')
 
352
        creator.shelve_creation(b'foo-id')
306
353
        creator.transform()
307
 
        self.assertRaises(StopIteration,
308
 
                          tree.iter_entries_by_dir(['foo-id']).next)
309
 
        self.assertShelvedFileEqual('', creator, 'foo-id')
310
 
        s_trans_id = creator.shelf_transform.trans_id_file_id('foo-id')
311
 
        self.assertEqual('foo-id',
 
354
        self.assertRaises(
 
355
            StopIteration, next,
 
356
            tree.iter_entries_by_dir(specific_files=['foo']))
 
357
        self.assertShelvedFileEqual('', creator, b'foo-id')
 
358
        s_trans_id = creator.shelf_transform.trans_id_file_id(b'foo-id')
 
359
        self.assertEqual(b'foo-id',
312
360
                         creator.shelf_transform.final_file_id(s_trans_id))
313
 
        self.failIfExists('foo')
 
361
        self.assertPathDoesNotExist('foo')
314
362
 
315
363
    def prepare_shelve_deletion(self):
316
364
        tree = self.make_branch_and_tree('tree')
317
365
        tree.lock_write()
318
366
        self.addCleanup(tree.unlock)
319
 
        self.build_tree_contents([('tree/foo/',), ('tree/foo/bar', 'baz')])
320
 
        tree.add(['foo', 'foo/bar'], ['foo-id', 'bar-id'])
 
367
        self.build_tree_contents([('tree/foo/',), ('tree/foo/bar', b'baz')])
 
368
        tree.add(['foo', 'foo/bar'], [b'foo-id', b'bar-id'])
321
369
        tree.commit('Added file and directory')
322
 
        tree.unversion(['foo-id', 'bar-id'])
 
370
        tree.unversion(['foo', 'foo/bar'])
323
371
        os.unlink('tree/foo/bar')
324
372
        os.rmdir('tree/foo')
325
373
        creator = shelf.ShelfCreator(tree, tree.basis_tree())
326
374
        self.addCleanup(creator.finalize)
327
 
        self.assertEqual([('delete file', 'bar-id', 'file', 'foo/bar'),
328
 
                          ('delete file', 'foo-id', 'directory', 'foo')],
329
 
                          sorted(list(creator.iter_shelvable())))
 
375
        self.assertEqual([('delete file', b'bar-id', 'file', 'foo/bar'),
 
376
                          ('delete file', b'foo-id', 'directory', 'foo')],
 
377
                         sorted(list(creator.iter_shelvable())))
330
378
        return creator, tree
331
379
 
332
380
    def check_shelve_deletion(self, tree):
333
 
        self.assertTrue('foo-id' in tree)
334
 
        self.assertTrue('bar-id' in tree)
335
 
        self.assertFileEqual('baz', 'tree/foo/bar')
 
381
        self.assertEqual(tree.id2path(b'foo-id'), 'foo')
 
382
        self.assertEqual(tree.id2path(b'bar-id'), 'foo/bar')
 
383
        self.assertFileEqual(b'baz', 'tree/foo/bar')
336
384
 
337
385
    def test_shelve_deletion(self):
338
386
        creator, tree = self.prepare_shelve_deletion()
339
 
        creator.shelve_deletion('foo-id')
340
 
        creator.shelve_deletion('bar-id')
 
387
        creator.shelve_deletion(b'foo-id')
 
388
        creator.shelve_deletion(b'bar-id')
341
389
        creator.transform()
342
390
        self.check_shelve_deletion(tree)
343
391
 
344
392
    def test_shelve_change_handles_deletion(self):
345
393
        creator, tree = self.prepare_shelve_deletion()
346
 
        creator.shelve_change(('delete file', 'foo-id', 'directory', 'foo'))
347
 
        creator.shelve_change(('delete file', 'bar-id', 'file', 'foo/bar'))
 
394
        creator.shelve_change(('delete file', b'foo-id', 'directory', 'foo'))
 
395
        creator.shelve_change(('delete file', b'bar-id', 'file', 'foo/bar'))
348
396
        creator.transform()
349
397
        self.check_shelve_deletion(tree)
350
398
 
351
399
    def test_shelve_delete_contents(self):
352
400
        tree = self.make_branch_and_tree('tree')
353
 
        self.build_tree(['tree/foo',])
354
 
        tree.add('foo', 'foo-id')
 
401
        self.build_tree(['tree/foo', ])
 
402
        tree.add('foo', b'foo-id')
355
403
        tree.commit('Added file and directory')
356
404
        os.unlink('tree/foo')
357
405
        tree.lock_tree_write()
358
406
        self.addCleanup(tree.unlock)
359
407
        creator = shelf.ShelfCreator(tree, tree.basis_tree())
360
408
        self.addCleanup(creator.finalize)
361
 
        self.assertEqual([('delete file', 'foo-id', 'file', 'foo')],
 
409
        self.assertEqual([('delete file', b'foo-id', 'file', 'foo')],
362
410
                         sorted(list(creator.iter_shelvable())))
363
 
        creator.shelve_deletion('foo-id')
 
411
        creator.shelve_deletion(b'foo-id')
364
412
        creator.transform()
365
 
        self.failUnlessExists('tree/foo')
 
413
        self.assertPathExists('tree/foo')
366
414
 
367
415
    def prepare_shelve_change_kind(self):
368
416
        tree = self.make_branch_and_tree('tree')
369
 
        self.build_tree_contents([('tree/foo', 'bar')])
370
 
        tree.add('foo', 'foo-id')
 
417
        self.build_tree_contents([('tree/foo', b'bar')])
 
418
        tree.add('foo', b'foo-id')
371
419
        tree.commit('Added file and directory')
372
420
        os.unlink('tree/foo')
373
421
        os.mkdir('tree/foo')
375
423
        self.addCleanup(tree.unlock)
376
424
        creator = shelf.ShelfCreator(tree, tree.basis_tree())
377
425
        self.addCleanup(creator.finalize)
378
 
        self.assertEqual([('change kind', 'foo-id', 'file', 'directory',
 
426
        self.assertEqual([('change kind', b'foo-id', 'file', 'directory',
379
427
                           'foo')], sorted(list(creator.iter_shelvable())))
380
428
        return creator
381
429
 
382
430
    def check_shelve_change_kind(self, creator):
383
 
        self.assertFileEqual('bar', 'tree/foo')
384
 
        s_trans_id = creator.shelf_transform.trans_id_file_id('foo-id')
 
431
        self.assertFileEqual(b'bar', 'tree/foo')
 
432
        s_trans_id = creator.shelf_transform.trans_id_file_id(b'foo-id')
385
433
        self.assertEqual('directory',
386
434
                         creator.shelf_transform._new_contents[s_trans_id])
387
435
 
388
436
    def test_shelve_change_kind(self):
389
437
        creator = self.prepare_shelve_change_kind()
390
 
        creator.shelve_content_change('foo-id')
 
438
        creator.shelve_content_change(b'foo-id')
391
439
        creator.transform()
392
440
        self.check_shelve_change_kind(creator)
393
441
 
394
442
    def test_shelve_change_handles_change_kind(self):
395
443
        creator = self.prepare_shelve_change_kind()
396
 
        creator.shelve_change(('change kind', 'foo-id', 'file', 'directory',
 
444
        creator.shelve_change(('change kind', b'foo-id', 'file', 'directory',
397
445
                               'foo'))
398
446
        creator.transform()
399
447
        self.check_shelve_change_kind(creator)
409
457
 
410
458
    def test_shelve_unversion(self):
411
459
        tree = self.make_branch_and_tree('tree')
412
 
        self.build_tree(['tree/foo',])
413
 
        tree.add('foo', 'foo-id')
 
460
        self.build_tree(['tree/foo', ])
 
461
        tree.add('foo', b'foo-id')
414
462
        tree.commit('Added file and directory')
415
 
        tree.unversion(['foo-id'])
 
463
        tree.unversion(['foo'])
416
464
        tree.lock_tree_write()
417
465
        self.addCleanup(tree.unlock)
418
466
        creator = shelf.ShelfCreator(tree, tree.basis_tree())
419
467
        self.addCleanup(creator.finalize)
420
 
        self.assertEqual([('delete file', 'foo-id', 'file', 'foo')],
 
468
        self.assertEqual([('delete file', b'foo-id', 'file', 'foo')],
421
469
                         sorted(list(creator.iter_shelvable())))
422
 
        creator.shelve_deletion('foo-id')
 
470
        creator.shelve_deletion(b'foo-id')
423
471
        creator.transform()
424
 
        self.failUnlessExists('tree/foo')
 
472
        self.assertPathExists('tree/foo')
425
473
 
426
474
    def test_shelve_serialization(self):
427
475
        tree = self.make_branch_and_tree('.')
440
488
    def test_write_shelf(self):
441
489
        tree = self.make_branch_and_tree('tree')
442
490
        self.build_tree(['tree/foo'])
443
 
        tree.add('foo', 'foo-id')
 
491
        tree.add('foo', b'foo-id')
444
492
        tree.lock_tree_write()
445
493
        self.addCleanup(tree.unlock)
446
494
        creator = shelf.ShelfCreator(tree, tree.basis_tree())
447
495
        self.addCleanup(creator.finalize)
448
496
        list(creator.iter_shelvable())
449
 
        creator.shelve_creation('foo-id')
450
 
        shelf_file = open('shelf', 'wb')
451
 
        try:
 
497
        creator.shelve_creation(b'foo-id')
 
498
        with open('shelf', 'wb') as shelf_file:
452
499
            creator.write_shelf(shelf_file)
453
 
        finally:
454
 
            shelf_file.close()
455
500
        parser = pack.ContainerPushParser()
456
 
        shelf_file = open('shelf', 'rb')
457
 
        try:
 
501
        with open('shelf', 'rb') as shelf_file:
458
502
            parser.accept_bytes(shelf_file.read())
459
 
        finally:
460
 
            shelf_file.close()
461
 
        tt = transform.TransformPreview(tree)
 
503
        tt = tree.preview_transform()
462
504
        self.addCleanup(tt.finalize)
463
505
        records = iter(parser.read_pending_records())
464
 
        #skip revision-id
465
 
        records.next()
 
506
        # skip revision-id
 
507
        next(records)
466
508
        tt.deserialize(records)
467
509
 
468
510
    def test_shelve_unversioned(self):
469
511
        tree = self.make_branch_and_tree('tree')
470
 
        tree.lock_tree_write()
471
 
        try:
 
512
        with tree.lock_tree_write():
472
513
            self.assertRaises(errors.PathsNotVersionedError,
473
 
                              shelf.ShelfCreator, tree, tree.basis_tree(), ['foo'])
474
 
        finally:
475
 
            tree.unlock()
 
514
                              shelf.ShelfCreator, tree, tree.basis_tree(),
 
515
                              ['foo'])
476
516
        # We should be able to lock/unlock the tree if ShelfCreator cleaned
477
517
        # after itself.
478
518
        wt = workingtree.WorkingTree.open('tree')
480
520
        wt.unlock()
481
521
        # And a second tentative should raise the same error (no
482
522
        # limbo/pending_deletion leftovers).
483
 
        tree.lock_tree_write()
484
 
        try:
 
523
        with tree.lock_tree_write():
485
524
            self.assertRaises(errors.PathsNotVersionedError,
486
 
                              shelf.ShelfCreator, tree, tree.basis_tree(), ['foo'])
487
 
        finally:
488
 
            tree.unlock()
 
525
                              shelf.ShelfCreator, tree, tree.basis_tree(),
 
526
                              ['foo'])
489
527
 
490
528
    def test_shelve_skips_added_root(self):
491
529
        """Skip adds of the root when iterating through shelvable changes."""
511
549
    def test_make_merger(self):
512
550
        tree = self.make_branch_and_tree('tree')
513
551
        tree.commit('first commit')
514
 
        self.build_tree_contents([('tree/foo', 'bar')])
 
552
        self.build_tree_contents([('tree/foo', b'bar')])
515
553
        tree.lock_write()
516
554
        self.addCleanup(tree.unlock)
517
 
        tree.add('foo', 'foo-id')
 
555
        tree.add('foo', b'foo-id')
518
556
        creator = shelf.ShelfCreator(tree, tree.basis_tree())
519
557
        self.addCleanup(creator.finalize)
520
558
        list(creator.iter_shelvable())
521
 
        creator.shelve_creation('foo-id')
522
 
        shelf_file = open('shelf-file', 'w+b')
523
 
        try:
 
559
        creator.shelve_creation(b'foo-id')
 
560
        with open('shelf-file', 'w+b') as shelf_file:
524
561
            creator.write_shelf(shelf_file)
525
562
            creator.transform()
526
563
            shelf_file.seek(0)
527
564
            unshelver = shelf.Unshelver.from_tree_and_shelf(tree, shelf_file)
528
565
            unshelver.make_merger().do_merge()
529
566
            self.addCleanup(unshelver.finalize)
530
 
            self.assertFileEqual('bar', 'tree/foo')
531
 
        finally:
532
 
            shelf_file.close()
 
567
            self.assertFileEqual(b'bar', 'tree/foo')
533
568
 
534
569
    def test_unshelve_changed(self):
535
570
        tree = self.make_branch_and_tree('tree')
536
571
        tree.lock_write()
537
572
        self.addCleanup(tree.unlock)
538
 
        self.build_tree_contents([('tree/foo', 'a\nb\nc\n')])
539
 
        tree.add('foo', 'foo-id')
 
573
        self.build_tree_contents([('tree/foo', b'a\nb\nc\n')])
 
574
        tree.add('foo', b'foo-id')
540
575
        tree.commit('first commit')
541
 
        self.build_tree_contents([('tree/foo', 'a\nb\nd\n')])
 
576
        self.build_tree_contents([('tree/foo', b'a\nb\nd\n')])
542
577
        creator = shelf.ShelfCreator(tree, tree.basis_tree())
543
578
        self.addCleanup(creator.finalize)
544
579
        list(creator.iter_shelvable())
545
 
        creator.shelve_lines('foo-id', ['a\n', 'b\n', 'c\n'])
 
580
        creator.shelve_lines(b'foo-id', [b'a\n', b'b\n', b'c\n'])
546
581
        shelf_file = open('shelf', 'w+b')
547
582
        self.addCleanup(shelf_file.close)
548
583
        creator.write_shelf(shelf_file)
549
584
        creator.transform()
550
 
        self.build_tree_contents([('tree/foo', 'z\na\nb\nc\n')])
 
585
        self.build_tree_contents([('tree/foo', b'z\na\nb\nc\n')])
551
586
        shelf_file.seek(0)
552
587
        unshelver = shelf.Unshelver.from_tree_and_shelf(tree, shelf_file)
553
588
        self.addCleanup(unshelver.finalize)
554
589
        unshelver.make_merger().do_merge()
555
 
        self.assertFileEqual('z\na\nb\nd\n', 'tree/foo')
 
590
        self.assertFileEqual(b'z\na\nb\nd\n', 'tree/foo')
556
591
 
557
592
    def test_unshelve_deleted(self):
558
593
        tree = self.make_branch_and_tree('tree')
559
594
        tree.lock_write()
560
595
        self.addCleanup(tree.unlock)
561
 
        self.build_tree_contents([('tree/foo/',), ('tree/foo/bar', 'baz')])
562
 
        tree.add(['foo', 'foo/bar'], ['foo-id', 'bar-id'])
 
596
        self.build_tree_contents([('tree/foo/',), ('tree/foo/bar', b'baz')])
 
597
        tree.add(['foo', 'foo/bar'], [b'foo-id', b'bar-id'])
563
598
        tree.commit('Added file and directory')
564
 
        tree.unversion(['foo-id', 'bar-id'])
 
599
        tree.unversion(['foo', 'foo/bar'])
565
600
        os.unlink('tree/foo/bar')
566
601
        os.rmdir('tree/foo')
567
602
        creator = shelf.ShelfCreator(tree, tree.basis_tree())
568
603
        list(creator.iter_shelvable())
569
 
        creator.shelve_deletion('foo-id')
570
 
        creator.shelve_deletion('bar-id')
571
 
        shelf_file = open('shelf', 'w+b')
572
 
        self.addCleanup(shelf_file.close)
573
 
        creator.write_shelf(shelf_file)
574
 
        creator.transform()
575
 
        creator.finalize()
 
604
        creator.shelve_deletion(b'foo-id')
 
605
        creator.shelve_deletion(b'bar-id')
 
606
        with open('shelf', 'w+b') as shelf_file:
 
607
            creator.write_shelf(shelf_file)
 
608
            creator.transform()
 
609
            creator.finalize()
576
610
        # validate the test setup
577
 
        self.assertTrue('foo-id' in tree)
578
 
        self.assertTrue('bar-id' in tree)
579
 
        self.assertFileEqual('baz', 'tree/foo/bar')
580
 
        shelf_file.seek(0)
581
 
        unshelver = shelf.Unshelver.from_tree_and_shelf(tree, shelf_file)
582
 
        self.addCleanup(unshelver.finalize)
583
 
        unshelver.make_merger().do_merge()
584
 
        self.assertFalse('foo-id' in tree)
585
 
        self.assertFalse('bar-id' in tree)
 
611
        self.assertEqual(tree.id2path(b'foo-id'), 'foo')
 
612
        self.assertEqual(tree.id2path(b'bar-id'), 'foo/bar')
 
613
        self.assertFileEqual(b'baz', 'tree/foo/bar')
 
614
        with open('shelf', 'r+b') as shelf_file:
 
615
            unshelver = shelf.Unshelver.from_tree_and_shelf(tree, shelf_file)
 
616
            self.addCleanup(unshelver.finalize)
 
617
            unshelver.make_merger().do_merge()
 
618
        self.assertRaises(errors.NoSuchId, tree.id2path, b'foo-id')
 
619
        self.assertRaises(errors.NoSuchId, tree.id2path, b'bar-id')
586
620
 
587
621
    def test_unshelve_base(self):
588
622
        tree = self.make_branch_and_tree('tree')
589
623
        tree.lock_write()
590
624
        self.addCleanup(tree.unlock)
591
 
        tree.commit('rev1', rev_id='rev1')
 
625
        tree.commit('rev1', rev_id=b'rev1')
592
626
        creator = shelf.ShelfCreator(tree, tree.basis_tree())
593
627
        self.addCleanup(creator.finalize)
594
628
        manager = tree.get_shelf_manager()
597
631
            creator.write_shelf(shelf_file)
598
632
        finally:
599
633
            shelf_file.close()
600
 
        tree.commit('rev2', rev_id='rev2')
 
634
        tree.commit('rev2', rev_id=b'rev2')
601
635
        shelf_file = manager.read_shelf(1)
602
636
        self.addCleanup(shelf_file.close)
603
637
        unshelver = shelf.Unshelver.from_tree_and_shelf(tree, shelf_file)
604
638
        self.addCleanup(unshelver.finalize)
605
 
        self.assertEqual('rev1', unshelver.base_tree.get_revision_id())
 
639
        self.assertEqual(b'rev1', unshelver.base_tree.get_revision_id())
606
640
 
607
641
    def test_unshelve_serialization(self):
608
642
        tree = self.make_branch_and_tree('.')
614
648
 
615
649
    def test_corrupt_shelf(self):
616
650
        tree = self.make_branch_and_tree('.')
617
 
        self.build_tree_contents([('shelf', EMPTY_SHELF.replace('metadata',
618
 
                                                                'foo'))])
 
651
        self.build_tree_contents([('shelf', EMPTY_SHELF.replace(b'metadata',
 
652
                                                                b'foo'))])
619
653
        shelf_file = open('shelf', 'rb')
620
654
        self.addCleanup(shelf_file.close)
621
 
        e = self.assertRaises(errors.ShelfCorrupt,
 
655
        e = self.assertRaises(shelf.ShelfCorrupt,
622
656
                              shelf.Unshelver.from_tree_and_shelf, tree,
623
657
                              shelf_file)
624
658
        self.assertEqual('Shelf corrupt.', str(e))
625
659
 
 
660
    def test_unshelve_subdir_in_now_removed_dir(self):
 
661
        tree = self.make_branch_and_tree('.')
 
662
        self.addCleanup(tree.lock_write().unlock)
 
663
        self.build_tree(['dir/', 'dir/subdir/', 'dir/subdir/foo'])
 
664
        tree.add(['dir'], [b'dir-id'])
 
665
        tree.commit('versioned dir')
 
666
        tree.add(['dir/subdir', 'dir/subdir/foo'], [b'subdir-id', b'foo-id'])
 
667
        creator = shelf.ShelfCreator(tree, tree.basis_tree())
 
668
        self.addCleanup(creator.finalize)
 
669
        for change in creator.iter_shelvable():
 
670
            creator.shelve_change(change)
 
671
        shelf_manager = tree.get_shelf_manager()
 
672
        shelf_id = shelf_manager.shelve_changes(creator)
 
673
        self.assertPathDoesNotExist('dir/subdir')
 
674
        tree.remove(['dir'])
 
675
        unshelver = shelf_manager.get_unshelver(shelf_id)
 
676
        self.addCleanup(unshelver.finalize)
 
677
        unshelver.make_merger().do_merge()
 
678
        self.assertPathExists('dir/subdir/foo')
 
679
        self.assertEqual(b'dir-id', tree.path2id('dir'))
 
680
        self.assertEqual(b'subdir-id', tree.path2id('dir/subdir'))
 
681
        self.assertEqual(b'foo-id', tree.path2id('dir/subdir/foo'))
 
682
 
626
683
 
627
684
class TestShelfManager(tests.TestCaseWithTransport):
628
685
 
685
742
        manager = self.get_manager()
686
743
        shelf_id, shelf_file = manager.new_shelf()
687
744
        try:
688
 
            shelf_file.write('foo')
 
745
            shelf_file.write(b'foo')
689
746
        finally:
690
747
            shelf_file.close()
691
748
        shelf_id, shelf_file = manager.new_shelf()
692
749
        try:
693
 
            shelf_file.write('bar')
 
750
            shelf_file.write(b'bar')
694
751
        finally:
695
752
            shelf_file.close()
696
753
        shelf_file = manager.read_shelf(1)
697
754
        try:
698
 
            self.assertEqual('foo', shelf_file.read())
 
755
            self.assertEqual(b'foo', shelf_file.read())
699
756
        finally:
700
757
            shelf_file.close()
701
758
        shelf_file = manager.read_shelf(2)
702
759
        try:
703
 
            self.assertEqual('bar', shelf_file.read())
 
760
            self.assertEqual(b'bar', shelf_file.read())
704
761
        finally:
705
762
            shelf_file.close()
706
763
 
707
764
    def test_read_non_existant(self):
708
765
        manager = self.get_manager()
709
 
        e = self.assertRaises(errors.NoSuchShelfId, manager.read_shelf, 1)
 
766
        e = self.assertRaises(shelf.NoSuchShelfId, manager.read_shelf, 1)
710
767
        self.assertEqual('No changes are shelved with id "1".', str(e))
711
768
 
712
769
    def test_shelve_changes(self):
714
771
        tree.commit('no-change commit')
715
772
        tree.lock_write()
716
773
        self.addCleanup(tree.unlock)
717
 
        self.build_tree_contents([('tree/foo', 'bar')])
718
 
        self.assertFileEqual('bar', 'tree/foo')
719
 
        tree.add('foo', 'foo-id')
 
774
        self.build_tree_contents([('tree/foo', b'bar')])
 
775
        self.assertFileEqual(b'bar', 'tree/foo')
 
776
        tree.add('foo', b'foo-id')
720
777
        creator = shelf.ShelfCreator(tree, tree.basis_tree())
721
778
        self.addCleanup(creator.finalize)
722
779
        list(creator.iter_shelvable())
723
 
        creator.shelve_creation('foo-id')
 
780
        creator.shelve_creation(b'foo-id')
724
781
        shelf_manager = tree.get_shelf_manager()
725
782
        shelf_id = shelf_manager.shelve_changes(creator)
726
 
        self.failIfExists('tree/foo')
 
783
        self.assertPathDoesNotExist('tree/foo')
727
784
        unshelver = shelf_manager.get_unshelver(shelf_id)
728
785
        self.addCleanup(unshelver.finalize)
729
786
        unshelver.make_merger().do_merge()
730
 
        self.assertFileEqual('bar', 'tree/foo')
 
787
        self.assertFileEqual(b'bar', 'tree/foo')
731
788
 
732
789
    def test_get_metadata(self):
733
790
        tree = self.make_branch_and_tree('.')
738
795
        shelf_manager = tree.get_shelf_manager()
739
796
        shelf_id = shelf_manager.shelve_changes(creator, 'foo')
740
797
        metadata = shelf_manager.get_metadata(shelf_id)
741
 
        self.assertEqual('foo', metadata['message'])
742
 
        self.assertEqual('null:', metadata['revision_id'])
 
798
        self.assertEqual('foo', metadata[b'message'])
 
799
        self.assertEqual(b'null:', metadata[b'revision_id'])