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'])
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()))
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(
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(
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)
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)
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'])
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
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(
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(
98
114
creator.transform()
99
self.assertEqual('foo/baz', tree.id2path('baz-id'))
115
self.assertEqual('foo/baz', tree.id2path(b'baz-id'))
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)
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)
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',
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()))
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()))
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)
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')
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')
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')
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
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',
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))
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)
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)
233
def test_shelve_directory_with_ignored(self):
234
tree = self.make_branch_and_tree('.')
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)
250
creator.shelve_change(('add file', b'bar-id', 'directory', 'bar'))
253
self.check_shelve_creation(creator, tree)
254
except transform.MalformedTransform:
256
'shelving directory with ignored file: see bug #611739')
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))
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'))
284
ptree.get_symlink_target(ptree.id2path(b'foo-id')))
240
286
def test_shelve_symlink_creation(self):
241
287
self._test_shelve_symlink_creation('foo', 'bar')
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')
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))
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')))
281
328
def test_shelve_symlink_target_change(self):
282
329
self._test_shelve_symlink_target_change('foo', 'bar', 'baz')
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')
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')
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',
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')
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
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')
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)
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)
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')
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')
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')
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')
567
self.assertFileEqual(b'bar', 'tree/foo')
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')
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)
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)
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')
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')
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()
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',
651
self.build_tree_contents([('shelf', EMPTY_SHELF.replace(b'metadata',
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,
624
658
self.assertEqual('Shelf corrupt.', str(e))
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')
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'))
627
684
class TestShelfManager(tests.TestCaseWithTransport):