/brz/remove-bazaar

To get this branch, use:
bzr branch http://gegoxaren.bato24.eu/bzr/brz/remove-bazaar
5599.1.1 by John Arbash Meinel
Move away from using Tree.inventory[] just to check if this is a root.
1
# Copyright (C) 2008-2011 Canonical Ltd
0.12.12 by Aaron Bentley
Implement shelf creator
2
#
0.12.80 by Aaron Bentley
Re-format GPL notifications
3
# This program is free software; you can redistribute it and/or modify
4
# it under the terms of the GNU General Public License as published by
5
# the Free Software Foundation; either version 2 of the License, or
6
# (at your option) any later version.
7
#
8
# This program is distributed in the hope that it will be useful,
9
# but WITHOUT ANY WARRANTY; without even the implied warranty of
10
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
11
# GNU General Public License for more details.
12
#
13
# You should have received a copy of the GNU General Public License
14
# along with this program; if not, write to the Free Software
4183.7.1 by Sabin Iacob
update FSF mailing address
15
# Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
0.12.12 by Aaron Bentley
Implement shelf creator
16
0.12.17 by Aaron Bentley
Handle creating symlinks
17
import os
18
6670.4.1 by Jelmer Vernooij
Update imports.
19
from .. import (
4241.14.12 by Vincent Ladeuil
Far too many modifications for a single commit, need to restart.
20
    errors,
6571.3.1 by Daniel Clemente
test for bug #611739 (shelving directory with ignored file). Known failure
21
    ignores,
4241.14.12 by Vincent Ladeuil
Far too many modifications for a single commit, need to restart.
22
    osutils,
23
    shelf,
24
    tests,
25
    transform,
26
    workingtree,
27
    )
6670.4.1 by Jelmer Vernooij
Update imports.
28
from ..bzr import (
29
    pack,
30
    )
31
from . import (
5967.12.1 by Martin Pool
Move all test features into bzrlib.tests.features
32
    features,
6571.3.1 by Daniel Clemente
test for bug #611739 (shelving directory with ignored file). Known failure
33
    KnownFailure,
34
    )
0.12.12 by Aaron Bentley
Implement shelf creator
35
36
6855.4.1 by Jelmer Vernooij
Yet more bees.
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")
0.14.34 by Aaron Bentley
Factor out the empty shelf
46
47
6734.1.1 by Jelmer Vernooij
Fix more imports.
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))
55
56
0.12.12 by Aaron Bentley
Implement shelf creator
57
class TestPrepareShelf(tests.TestCaseWithTransport):
58
4526.7.3 by Aaron Bentley
Test shelve_change.
59
    def prepare_shelve_rename(self):
0.12.12 by Aaron Bentley
Implement shelf creator
60
        tree = self.make_branch_and_tree('.')
61
        self.build_tree(['foo'])
6855.4.1 by Jelmer Vernooij
Yet more bees.
62
        tree.add(['foo'], [b'foo-id'])
0.12.12 by Aaron Bentley
Implement shelf creator
63
        tree.commit('foo')
64
        tree.rename_one('foo', 'bar')
4595.13.2 by Alexander Belchenko
[cherrypick revno 4650 from bzr.dev] Fix shelve on windows. (Robert Collins, #305006)
65
        tree.lock_tree_write()
66
        self.addCleanup(tree.unlock)
0.14.7 by Aaron Bentley
Misc test cleanup
67
        creator = shelf.ShelfCreator(tree, tree.basis_tree())
0.12.12 by Aaron Bentley
Implement shelf creator
68
        self.addCleanup(creator.finalize)
6855.4.1 by Jelmer Vernooij
Yet more bees.
69
        self.assertEqual([('rename', b'foo-id', 'foo', 'bar')],
7143.15.2 by Jelmer Vernooij
Run autopep8.
70
                         list(creator.iter_shelvable()))
4526.7.3 by Aaron Bentley
Test shelve_change.
71
        return creator
72
73
    def check_shelve_rename(self, creator):
6855.4.1 by Jelmer Vernooij
Yet more bees.
74
        work_trans_id = creator.work_transform.trans_id_file_id(b'foo-id')
0.12.12 by Aaron Bentley
Implement shelf creator
75
        self.assertEqual('foo', creator.work_transform.final_name(
76
                         work_trans_id))
6855.4.1 by Jelmer Vernooij
Yet more bees.
77
        shelf_trans_id = creator.shelf_transform.trans_id_file_id(b'foo-id')
0.12.12 by Aaron Bentley
Implement shelf creator
78
        self.assertEqual('bar', creator.shelf_transform.final_name(
79
                         shelf_trans_id))
80
4526.7.3 by Aaron Bentley
Test shelve_change.
81
    def test_shelve_rename(self):
82
        creator = self.prepare_shelve_rename()
6855.4.1 by Jelmer Vernooij
Yet more bees.
83
        creator.shelve_rename(b'foo-id')
4526.7.3 by Aaron Bentley
Test shelve_change.
84
        self.check_shelve_rename(creator)
85
86
    def test_shelve_change_handles_rename(self):
87
        creator = self.prepare_shelve_rename()
6855.4.1 by Jelmer Vernooij
Yet more bees.
88
        creator.shelve_change(('rename', b'foo-id', 'foo', 'bar'))
4526.7.3 by Aaron Bentley
Test shelve_change.
89
        self.check_shelve_rename(creator)
90
91
    def prepare_shelve_move(self):
0.12.12 by Aaron Bentley
Implement shelf creator
92
        tree = self.make_branch_and_tree('.')
93
        self.build_tree(['foo/', 'bar/', 'foo/baz'])
6855.4.1 by Jelmer Vernooij
Yet more bees.
94
        tree.add(['foo', 'bar', 'foo/baz'], [b'foo-id', b'bar-id', b'baz-id'])
0.12.12 by Aaron Bentley
Implement shelf creator
95
        tree.commit('foo')
96
        tree.rename_one('foo/baz', 'bar/baz')
4595.13.2 by Alexander Belchenko
[cherrypick revno 4650 from bzr.dev] Fix shelve on windows. (Robert Collins, #305006)
97
        tree.lock_tree_write()
98
        self.addCleanup(tree.unlock)
0.14.7 by Aaron Bentley
Misc test cleanup
99
        creator = shelf.ShelfCreator(tree, tree.basis_tree())
0.12.12 by Aaron Bentley
Implement shelf creator
100
        self.addCleanup(creator.finalize)
6855.4.1 by Jelmer Vernooij
Yet more bees.
101
        self.assertEqual([('rename', b'baz-id', 'foo/baz', 'bar/baz')],
0.14.32 by Aaron Bentley
Replace ShelfCreator.__iter__ with ShelfCreator.iter_shelvable
102
                         list(creator.iter_shelvable()))
4526.7.3 by Aaron Bentley
Test shelve_change.
103
        return creator, tree
104
105
    def check_shelve_move(self, creator, tree):
6855.4.1 by Jelmer Vernooij
Yet more bees.
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')
0.12.12 by Aaron Bentley
Implement shelf creator
108
        self.assertEqual(work_foo, creator.work_transform.final_parent(
109
                         work_trans_id))
6855.4.1 by Jelmer Vernooij
Yet more bees.
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')
0.12.12 by Aaron Bentley
Implement shelf creator
112
        self.assertEqual(shelf_bar, creator.shelf_transform.final_parent(
113
                         shelf_trans_id))
0.12.13 by Aaron Bentley
Implement shelving content
114
        creator.transform()
6855.4.1 by Jelmer Vernooij
Yet more bees.
115
        self.assertEqual('foo/baz', tree.id2path(b'baz-id'))
0.12.13 by Aaron Bentley
Implement shelving content
116
4526.7.3 by Aaron Bentley
Test shelve_change.
117
    def test_shelve_move(self):
118
        creator, tree = self.prepare_shelve_move()
6855.4.1 by Jelmer Vernooij
Yet more bees.
119
        creator.shelve_rename(b'baz-id')
4526.7.3 by Aaron Bentley
Test shelve_change.
120
        self.check_shelve_move(creator, tree)
121
122
    def test_shelve_change_handles_move(self):
123
        creator, tree = self.prepare_shelve_move()
6973.13.2 by Jelmer Vernooij
Fix some more tests.
124
        creator.shelve_change(('rename', b'baz-id', 'foo/baz', 'bar/baz'))
4526.7.3 by Aaron Bentley
Test shelve_change.
125
        self.check_shelve_move(creator, tree)
126
4634.123.6 by John Arbash Meinel
Add a direct ShelfCreator test for changing root id.
127
    def test_shelve_changed_root_id(self):
128
        tree = self.make_branch_and_tree('.')
129
        self.build_tree(['foo'])
6855.4.1 by Jelmer Vernooij
Yet more bees.
130
        tree.set_root_id(b'first-root-id')
131
        tree.add(['foo'], [b'foo-id'])
4634.123.6 by John Arbash Meinel
Add a direct ShelfCreator test for changing root id.
132
        tree.commit('foo')
6855.4.1 by Jelmer Vernooij
Yet more bees.
133
        tree.set_root_id(b'second-root-id')
4634.123.6 by John Arbash Meinel
Add a direct ShelfCreator test for changing root id.
134
        tree.lock_tree_write()
135
        self.addCleanup(tree.unlock)
136
        creator = shelf.ShelfCreator(tree, tree.basis_tree())
137
        self.addCleanup(creator.finalize)
7143.15.5 by Jelmer Vernooij
More PEP8 fixes.
138
        self.expectFailure(
139
            'shelf doesn\'t support shelving root changes yet',
140
            self.assertEqual, [
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'),
144
                ], list(creator.iter_shelvable()))
4634.123.6 by John Arbash Meinel
Add a direct ShelfCreator test for changing root id.
145
6855.4.1 by Jelmer Vernooij
Yet more bees.
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'),
7143.15.2 by Jelmer Vernooij
Run autopep8.
149
                          ], list(creator.iter_shelvable()))
4634.123.6 by John Arbash Meinel
Add a direct ShelfCreator test for changing root id.
150
0.12.14 by Aaron Bentley
Add shelving of created files
151
    def assertShelvedFileEqual(self, expected_content, creator, file_id):
152
        s_trans_id = creator.shelf_transform.trans_id_file_id(file_id)
153
        shelf_file = creator.shelf_transform._limbo_name(s_trans_id)
154
        self.assertFileEqual(expected_content, shelf_file)
155
4595.8.1 by Aaron Bentley
shelve_change handles text modification.
156
    def prepare_content_change(self):
0.12.13 by Aaron Bentley
Implement shelving content
157
        tree = self.make_branch_and_tree('.')
158
        tree.lock_write()
159
        self.addCleanup(tree.unlock)
6855.4.1 by Jelmer Vernooij
Yet more bees.
160
        self.build_tree_contents([('foo', b'a\n')])
161
        tree.add('foo', b'foo-id')
0.12.13 by Aaron Bentley
Implement shelving content
162
        tree.commit('Committed foo')
6855.4.1 by Jelmer Vernooij
Yet more bees.
163
        self.build_tree_contents([('foo', b'b\na\nc\n')])
0.14.7 by Aaron Bentley
Misc test cleanup
164
        creator = shelf.ShelfCreator(tree, tree.basis_tree())
0.12.13 by Aaron Bentley
Implement shelving content
165
        self.addCleanup(creator.finalize)
4595.8.2 by Aaron Bentley
Implement shelve_all
166
        return creator
167
168
    def test_shelve_content_change(self):
169
        creator = self.prepare_content_change()
6855.4.1 by Jelmer Vernooij
Yet more bees.
170
        self.assertEqual([('modify text', b'foo-id')],
0.14.32 by Aaron Bentley
Replace ShelfCreator.__iter__ with ShelfCreator.iter_shelvable
171
                         list(creator.iter_shelvable()))
6973.6.1 by Jelmer Vernooij
More bees.
172
        creator.shelve_lines(b'foo-id', [b'a\n', b'c\n'])
0.12.13 by Aaron Bentley
Implement shelving content
173
        creator.transform()
6973.6.1 by Jelmer Vernooij
More bees.
174
        self.assertFileEqual(b'a\nc\n', 'foo')
175
        self.assertShelvedFileEqual(b'b\na\n', creator, b'foo-id')
0.12.14 by Aaron Bentley
Add shelving of created files
176
4595.8.1 by Aaron Bentley
shelve_change handles text modification.
177
    def test_shelve_change_handles_modify_text(self):
178
        creator = self.prepare_content_change()
6855.4.1 by Jelmer Vernooij
Yet more bees.
179
        creator.shelve_change(('modify text', b'foo-id'))
4595.8.1 by Aaron Bentley
shelve_change handles text modification.
180
        creator.transform()
6973.6.1 by Jelmer Vernooij
More bees.
181
        self.assertFileEqual(b'a\n', 'foo')
182
        self.assertShelvedFileEqual(b'b\na\nc\n', creator, b'foo-id')
4526.7.3 by Aaron Bentley
Test shelve_change.
183
4595.8.2 by Aaron Bentley
Implement shelve_all
184
    def test_shelve_all(self):
185
        creator = self.prepare_content_change()
186
        creator.shelve_all()
187
        creator.transform()
6973.6.1 by Jelmer Vernooij
More bees.
188
        self.assertFileEqual(b'a\n', 'foo')
189
        self.assertShelvedFileEqual(b'b\na\nc\n', creator, b'foo-id')
4595.8.2 by Aaron Bentley
Implement shelve_all
190
4526.7.3 by Aaron Bentley
Test shelve_change.
191
    def prepare_shelve_creation(self):
0.12.14 by Aaron Bentley
Add shelving of created files
192
        tree = self.make_branch_and_tree('.')
193
        tree.lock_write()
194
        self.addCleanup(tree.unlock)
195
        tree.commit('Empty tree')
6855.4.1 by Jelmer Vernooij
Yet more bees.
196
        self.build_tree_contents([('foo', b'a\n'), ('bar/',)])
197
        tree.add(['foo', 'bar'], [b'foo-id', b'bar-id'])
0.14.7 by Aaron Bentley
Misc test cleanup
198
        creator = shelf.ShelfCreator(tree, tree.basis_tree())
0.12.14 by Aaron Bentley
Add shelving of created files
199
        self.addCleanup(creator.finalize)
7045.1.1 by Jelmer Vernooij
Fix another 300 tests.
200
        self.assertEqual([('add file', b'bar-id', 'directory', 'bar'),
201
                          ('add file', b'foo-id', 'file', 'foo')],
7143.15.2 by Jelmer Vernooij
Run autopep8.
202
                         sorted(list(creator.iter_shelvable())))
4526.7.3 by Aaron Bentley
Test shelve_change.
203
        return creator, tree
204
205
    def check_shelve_creation(self, creator, tree):
7143.15.5 by Jelmer Vernooij
More PEP8 fixes.
206
        self.assertRaises(
207
            StopIteration, next,
208
            tree.iter_entries_by_dir(specific_files=['foo']))
6855.4.1 by Jelmer Vernooij
Yet more bees.
209
        s_trans_id = creator.shelf_transform.trans_id_file_id(b'foo-id')
210
        self.assertEqual(b'foo-id',
0.12.15 by Aaron Bentley
Handle file-id when shelving creation
211
                         creator.shelf_transform.final_file_id(s_trans_id))
5784.1.3 by Martin Pool
Switch away from using failUnlessExists and failIfExists
212
        self.assertPathDoesNotExist('foo')
213
        self.assertPathDoesNotExist('bar')
6855.4.1 by Jelmer Vernooij
Yet more bees.
214
        self.assertShelvedFileEqual('a\n', creator, b'foo-id')
215
        s_bar_trans_id = creator.shelf_transform.trans_id_file_id(b'bar-id')
0.12.16 by Aaron Bentley
Handle shelving directory creation
216
        self.assertEqual('directory',
7143.15.2 by Jelmer Vernooij
Run autopep8.
217
                         creator.shelf_transform.final_kind(s_bar_trans_id))
0.12.17 by Aaron Bentley
Handle creating symlinks
218
4526.7.3 by Aaron Bentley
Test shelve_change.
219
    def test_shelve_creation(self):
220
        creator, tree = self.prepare_shelve_creation()
6855.4.1 by Jelmer Vernooij
Yet more bees.
221
        creator.shelve_creation(b'foo-id')
222
        creator.shelve_creation(b'bar-id')
4526.7.3 by Aaron Bentley
Test shelve_change.
223
        creator.transform()
224
        self.check_shelve_creation(creator, tree)
225
226
    def test_shelve_change_handles_creation(self):
227
        creator, tree = self.prepare_shelve_creation()
6855.4.1 by Jelmer Vernooij
Yet more bees.
228
        creator.shelve_change(('add file', b'foo-id', 'file', 'foo'))
229
        creator.shelve_change(('add file', b'bar-id', 'directory', 'bar'))
4526.7.3 by Aaron Bentley
Test shelve_change.
230
        creator.transform()
231
        self.check_shelve_creation(creator, tree)
232
6571.3.1 by Daniel Clemente
test for bug #611739 (shelving directory with ignored file). Known failure
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')
7143.15.2 by Jelmer Vernooij
Run autopep8.
238
        self.build_tree_contents(
239
            [('foo', b'a\n'), ('bar/',), ('bar/ignored', b'ign\n')])
6855.4.1 by Jelmer Vernooij
Yet more bees.
240
        tree.add(['foo', 'bar'], [b'foo-id', b'bar-id'])
6571.3.1 by Daniel Clemente
test for bug #611739 (shelving directory with ignored file). Known failure
241
        creator = shelf.ShelfCreator(tree, tree.basis_tree())
242
        self.addCleanup(creator.finalize)
6855.4.1 by Jelmer Vernooij
Yet more bees.
243
        self.assertEqual([('add file', b'bar-id', 'directory', 'bar'),
244
                          ('add file', b'foo-id', 'file', 'foo')],
7143.15.2 by Jelmer Vernooij
Run autopep8.
245
                         sorted(list(creator.iter_shelvable())))
6571.3.1 by Daniel Clemente
test for bug #611739 (shelving directory with ignored file). Known failure
246
        ignores._set_user_ignores([])
7143.15.2 by Jelmer Vernooij
Run autopep8.
247
        in_patterns = ['ignored', ]
6571.3.1 by Daniel Clemente
test for bug #611739 (shelving directory with ignored file). Known failure
248
        ignores.add_unique_user_ignores(in_patterns)
249
6855.4.1 by Jelmer Vernooij
Yet more bees.
250
        creator.shelve_change(('add file', b'bar-id', 'directory', 'bar'))
6571.3.1 by Daniel Clemente
test for bug #611739 (shelving directory with ignored file). Known failure
251
        try:
252
            creator.transform()
253
            self.check_shelve_creation(creator, tree)
7490.69.1 by Jelmer Vernooij
Move some more tests to breezy.bzr.
254
        except transform.MalformedTransform:
7143.15.2 by Jelmer Vernooij
Run autopep8.
255
            raise KnownFailure(
256
                'shelving directory with ignored file: see bug #611739')
6571.3.1 by Daniel Clemente
test for bug #611739 (shelving directory with ignored file). Known failure
257
4526.7.3 by Aaron Bentley
Test shelve_change.
258
    def _test_shelve_symlink_creation(self, link_name, link_target,
259
                                      shelve_change=False):
5967.12.1 by Martin Pool
Move all test features into bzrlib.tests.features
260
        self.requireFeature(features.SymlinkFeature)
4241.14.16 by Vincent Ladeuil
Fix _PreviewTree.get_symlink_target for unicode symlinks.
261
        tree = self.make_branch_and_tree('.')
262
        tree.lock_write()
263
        self.addCleanup(tree.unlock)
264
        tree.commit('Empty tree')
265
        os.symlink(link_target, link_name)
6855.4.1 by Jelmer Vernooij
Yet more bees.
266
        tree.add(link_name, b'foo-id')
4241.14.16 by Vincent Ladeuil
Fix _PreviewTree.get_symlink_target for unicode symlinks.
267
        creator = shelf.ShelfCreator(tree, tree.basis_tree())
268
        self.addCleanup(creator.finalize)
6855.4.1 by Jelmer Vernooij
Yet more bees.
269
        self.assertEqual([('add file', b'foo-id', 'symlink', link_name)],
4241.14.16 by Vincent Ladeuil
Fix _PreviewTree.get_symlink_target for unicode symlinks.
270
                         list(creator.iter_shelvable()))
4526.7.3 by Aaron Bentley
Test shelve_change.
271
        if shelve_change:
7143.15.2 by Jelmer Vernooij
Run autopep8.
272
            creator.shelve_change(
273
                ('add file', b'foo-id', 'symlink', link_name))
4526.7.3 by Aaron Bentley
Test shelve_change.
274
        else:
6855.4.1 by Jelmer Vernooij
Yet more bees.
275
            creator.shelve_creation(b'foo-id')
4241.14.16 by Vincent Ladeuil
Fix _PreviewTree.get_symlink_target for unicode symlinks.
276
        creator.transform()
6855.4.1 by Jelmer Vernooij
Yet more bees.
277
        s_trans_id = creator.shelf_transform.trans_id_file_id(b'foo-id')
5784.1.3 by Martin Pool
Switch away from using failUnlessExists and failIfExists
278
        self.assertPathDoesNotExist(link_name)
4241.14.16 by Vincent Ladeuil
Fix _PreviewTree.get_symlink_target for unicode symlinks.
279
        limbo_name = creator.shelf_transform._limbo_name(s_trans_id)
280
        self.assertEqual(link_target, osutils.readlink(limbo_name))
281
        ptree = creator.shelf_transform.get_preview_tree()
6809.4.15 by Jelmer Vernooij
Fix some more tests.
282
        self.assertEqual(
7143.15.2 by Jelmer Vernooij
Run autopep8.
283
            link_target,
7143.15.15 by Jelmer Vernooij
Merge trunk.
284
            ptree.get_symlink_target(ptree.id2path(b'foo-id')))
4241.14.16 by Vincent Ladeuil
Fix _PreviewTree.get_symlink_target for unicode symlinks.
285
0.12.17 by Aaron Bentley
Handle creating symlinks
286
    def test_shelve_symlink_creation(self):
4241.14.16 by Vincent Ladeuil
Fix _PreviewTree.get_symlink_target for unicode symlinks.
287
        self._test_shelve_symlink_creation('foo', 'bar')
288
289
    def test_shelve_unicode_symlink_creation(self):
5967.12.1 by Martin Pool
Move all test features into bzrlib.tests.features
290
        self.requireFeature(features.UnicodeFilenameFeature)
4241.14.16 by Vincent Ladeuil
Fix _PreviewTree.get_symlink_target for unicode symlinks.
291
        self._test_shelve_symlink_creation(u'fo\N{Euro Sign}o',
292
                                           u'b\N{Euro Sign}ar')
0.12.19 by Aaron Bentley
Add support for writing shelves
293
4526.7.3 by Aaron Bentley
Test shelve_change.
294
    def test_shelve_change_handles_symlink_creation(self):
295
        self._test_shelve_symlink_creation('foo', 'bar', shelve_change=True)
296
4241.14.12 by Vincent Ladeuil
Far too many modifications for a single commit, need to restart.
297
    def _test_shelve_symlink_target_change(self, link_name,
4526.7.3 by Aaron Bentley
Test shelve_change.
298
                                           old_target, new_target,
299
                                           shelve_change=False):
5967.12.1 by Martin Pool
Move all test features into bzrlib.tests.features
300
        self.requireFeature(features.SymlinkFeature)
4119.5.1 by James Westby
Shelve can now shelve changes to a symlink's target.
301
        tree = self.make_branch_and_tree('.')
302
        tree.lock_write()
303
        self.addCleanup(tree.unlock)
4241.14.12 by Vincent Ladeuil
Far too many modifications for a single commit, need to restart.
304
        os.symlink(old_target, link_name)
6855.4.1 by Jelmer Vernooij
Yet more bees.
305
        tree.add(link_name, b'foo-id')
4119.5.1 by James Westby
Shelve can now shelve changes to a symlink's target.
306
        tree.commit("commit symlink")
4241.14.12 by Vincent Ladeuil
Far too many modifications for a single commit, need to restart.
307
        os.unlink(link_name)
308
        os.symlink(new_target, link_name)
4119.5.1 by James Westby
Shelve can now shelve changes to a symlink's target.
309
        creator = shelf.ShelfCreator(tree, tree.basis_tree())
310
        self.addCleanup(creator.finalize)
6855.4.1 by Jelmer Vernooij
Yet more bees.
311
        self.assertEqual([('modify target', b'foo-id', link_name,
4241.14.12 by Vincent Ladeuil
Far too many modifications for a single commit, need to restart.
312
                           old_target, new_target)],
4119.5.1 by James Westby
Shelve can now shelve changes to a symlink's target.
313
                         list(creator.iter_shelvable()))
4526.7.3 by Aaron Bentley
Test shelve_change.
314
        if shelve_change:
6855.4.1 by Jelmer Vernooij
Yet more bees.
315
            creator.shelve_change(('modify target', b'foo-id', link_name,
4526.7.3 by Aaron Bentley
Test shelve_change.
316
                                   old_target, new_target))
317
        else:
6855.4.1 by Jelmer Vernooij
Yet more bees.
318
            creator.shelve_modify_target(b'foo-id')
4119.5.1 by James Westby
Shelve can now shelve changes to a symlink's target.
319
        creator.transform()
4241.14.21 by Vincent Ladeuil
More cleanup.
320
        self.assertEqual(old_target, osutils.readlink(link_name))
6855.4.1 by Jelmer Vernooij
Yet more bees.
321
        s_trans_id = creator.shelf_transform.trans_id_file_id(b'foo-id')
4119.5.1 by James Westby
Shelve can now shelve changes to a symlink's target.
322
        limbo_name = creator.shelf_transform._limbo_name(s_trans_id)
4241.14.21 by Vincent Ladeuil
More cleanup.
323
        self.assertEqual(new_target, osutils.readlink(limbo_name))
4241.14.16 by Vincent Ladeuil
Fix _PreviewTree.get_symlink_target for unicode symlinks.
324
        ptree = creator.shelf_transform.get_preview_tree()
7143.15.2 by Jelmer Vernooij
Run autopep8.
325
        self.assertEqual(new_target, ptree.get_symlink_target(
326
            ptree.id2path(b'foo-id')))
4241.14.12 by Vincent Ladeuil
Far too many modifications for a single commit, need to restart.
327
328
    def test_shelve_symlink_target_change(self):
329
        self._test_shelve_symlink_target_change('foo', 'bar', 'baz')
330
331
    def test_shelve_unicode_symlink_target_change(self):
5967.12.1 by Martin Pool
Move all test features into bzrlib.tests.features
332
        self.requireFeature(features.UnicodeFilenameFeature)
4241.14.12 by Vincent Ladeuil
Far too many modifications for a single commit, need to restart.
333
        self._test_shelve_symlink_target_change(
334
            u'fo\N{Euro Sign}o', u'b\N{Euro Sign}ar', u'b\N{Euro Sign}az')
4119.5.1 by James Westby
Shelve can now shelve changes to a symlink's target.
335
4526.7.3 by Aaron Bentley
Test shelve_change.
336
    def test_shelve_change_handles_symlink_target_change(self):
337
        self._test_shelve_symlink_target_change('foo', 'bar', 'baz',
338
                                                shelve_change=True)
339
0.14.12 by Aaron Bentley
Handle new dangling ids
340
    def test_shelve_creation_no_contents(self):
341
        tree = self.make_branch_and_tree('.')
342
        tree.lock_write()
343
        self.addCleanup(tree.unlock)
344
        tree.commit('Empty tree')
345
        self.build_tree(['foo'])
6855.4.1 by Jelmer Vernooij
Yet more bees.
346
        tree.add('foo', b'foo-id')
0.14.12 by Aaron Bentley
Handle new dangling ids
347
        os.unlink('foo')
348
        creator = shelf.ShelfCreator(tree, tree.basis_tree())
349
        self.addCleanup(creator.finalize)
6855.4.1 by Jelmer Vernooij
Yet more bees.
350
        self.assertEqual([('add file', b'foo-id', None, 'foo')],
0.14.32 by Aaron Bentley
Replace ShelfCreator.__iter__ with ShelfCreator.iter_shelvable
351
                         sorted(list(creator.iter_shelvable())))
6855.4.1 by Jelmer Vernooij
Yet more bees.
352
        creator.shelve_creation(b'foo-id')
0.14.12 by Aaron Bentley
Handle new dangling ids
353
        creator.transform()
7143.15.5 by Jelmer Vernooij
More PEP8 fixes.
354
        self.assertRaises(
355
            StopIteration, next,
356
            tree.iter_entries_by_dir(specific_files=['foo']))
6855.4.1 by Jelmer Vernooij
Yet more bees.
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',
0.14.12 by Aaron Bentley
Handle new dangling ids
360
                         creator.shelf_transform.final_file_id(s_trans_id))
5784.1.3 by Martin Pool
Switch away from using failUnlessExists and failIfExists
361
        self.assertPathDoesNotExist('foo')
0.14.12 by Aaron Bentley
Handle new dangling ids
362
4526.7.3 by Aaron Bentley
Test shelve_change.
363
    def prepare_shelve_deletion(self):
0.14.4 by Aaron Bentley
Implement shelving deletion
364
        tree = self.make_branch_and_tree('tree')
0.14.11 by Aaron Bentley
Fix re-versioning
365
        tree.lock_write()
366
        self.addCleanup(tree.unlock)
6855.4.1 by Jelmer Vernooij
Yet more bees.
367
        self.build_tree_contents([('tree/foo/',), ('tree/foo/bar', b'baz')])
368
        tree.add(['foo', 'foo/bar'], [b'foo-id', b'bar-id'])
0.14.4 by Aaron Bentley
Implement shelving deletion
369
        tree.commit('Added file and directory')
6809.4.25 by Jelmer Vernooij
Add paths argument to .unversion.
370
        tree.unversion(['foo', 'foo/bar'])
0.14.4 by Aaron Bentley
Implement shelving deletion
371
        os.unlink('tree/foo/bar')
372
        os.rmdir('tree/foo')
0.14.7 by Aaron Bentley
Misc test cleanup
373
        creator = shelf.ShelfCreator(tree, tree.basis_tree())
0.14.4 by Aaron Bentley
Implement shelving deletion
374
        self.addCleanup(creator.finalize)
6855.4.1 by Jelmer Vernooij
Yet more bees.
375
        self.assertEqual([('delete file', b'bar-id', 'file', 'foo/bar'),
376
                          ('delete file', b'foo-id', 'directory', 'foo')],
7143.15.2 by Jelmer Vernooij
Run autopep8.
377
                         sorted(list(creator.iter_shelvable())))
4526.7.3 by Aaron Bentley
Test shelve_change.
378
        return creator, tree
379
380
    def check_shelve_deletion(self, tree):
7397.4.7 by Jelmer Vernooij
Remove Tree.has_id.
381
        self.assertEqual(tree.id2path(b'foo-id'), 'foo')
382
        self.assertEqual(tree.id2path(b'bar-id'), 'foo/bar')
7045.1.20 by Jelmer Vernooij
Fix per_pack_repository tests.
383
        self.assertFileEqual(b'baz', 'tree/foo/bar')
0.14.4 by Aaron Bentley
Implement shelving deletion
384
4526.7.3 by Aaron Bentley
Test shelve_change.
385
    def test_shelve_deletion(self):
386
        creator, tree = self.prepare_shelve_deletion()
6855.4.1 by Jelmer Vernooij
Yet more bees.
387
        creator.shelve_deletion(b'foo-id')
388
        creator.shelve_deletion(b'bar-id')
4526.7.3 by Aaron Bentley
Test shelve_change.
389
        creator.transform()
390
        self.check_shelve_deletion(tree)
391
392
    def test_shelve_change_handles_deletion(self):
393
        creator, tree = self.prepare_shelve_deletion()
6855.4.1 by Jelmer Vernooij
Yet more bees.
394
        creator.shelve_change(('delete file', b'foo-id', 'directory', 'foo'))
395
        creator.shelve_change(('delete file', b'bar-id', 'file', 'foo/bar'))
4526.7.3 by Aaron Bentley
Test shelve_change.
396
        creator.transform()
397
        self.check_shelve_deletion(tree)
398
0.14.10 by Aaron Bentley
Fix behavior with deletions, unversioning, ...
399
    def test_shelve_delete_contents(self):
400
        tree = self.make_branch_and_tree('tree')
7143.15.2 by Jelmer Vernooij
Run autopep8.
401
        self.build_tree(['tree/foo', ])
6855.4.1 by Jelmer Vernooij
Yet more bees.
402
        tree.add('foo', b'foo-id')
0.14.10 by Aaron Bentley
Fix behavior with deletions, unversioning, ...
403
        tree.commit('Added file and directory')
404
        os.unlink('tree/foo')
4595.13.2 by Alexander Belchenko
[cherrypick revno 4650 from bzr.dev] Fix shelve on windows. (Robert Collins, #305006)
405
        tree.lock_tree_write()
406
        self.addCleanup(tree.unlock)
0.14.10 by Aaron Bentley
Fix behavior with deletions, unversioning, ...
407
        creator = shelf.ShelfCreator(tree, tree.basis_tree())
408
        self.addCleanup(creator.finalize)
6855.4.1 by Jelmer Vernooij
Yet more bees.
409
        self.assertEqual([('delete file', b'foo-id', 'file', 'foo')],
0.14.32 by Aaron Bentley
Replace ShelfCreator.__iter__ with ShelfCreator.iter_shelvable
410
                         sorted(list(creator.iter_shelvable())))
6855.4.1 by Jelmer Vernooij
Yet more bees.
411
        creator.shelve_deletion(b'foo-id')
0.14.10 by Aaron Bentley
Fix behavior with deletions, unversioning, ...
412
        creator.transform()
5784.1.3 by Martin Pool
Switch away from using failUnlessExists and failIfExists
413
        self.assertPathExists('tree/foo')
0.14.10 by Aaron Bentley
Fix behavior with deletions, unversioning, ...
414
4526.7.3 by Aaron Bentley
Test shelve_change.
415
    def prepare_shelve_change_kind(self):
0.14.23 by Aaron Bentley
Allow shelving kind change
416
        tree = self.make_branch_and_tree('tree')
6855.4.1 by Jelmer Vernooij
Yet more bees.
417
        self.build_tree_contents([('tree/foo', b'bar')])
418
        tree.add('foo', b'foo-id')
0.14.23 by Aaron Bentley
Allow shelving kind change
419
        tree.commit('Added file and directory')
420
        os.unlink('tree/foo')
421
        os.mkdir('tree/foo')
4595.13.2 by Alexander Belchenko
[cherrypick revno 4650 from bzr.dev] Fix shelve on windows. (Robert Collins, #305006)
422
        tree.lock_tree_write()
423
        self.addCleanup(tree.unlock)
0.14.23 by Aaron Bentley
Allow shelving kind change
424
        creator = shelf.ShelfCreator(tree, tree.basis_tree())
425
        self.addCleanup(creator.finalize)
6855.4.1 by Jelmer Vernooij
Yet more bees.
426
        self.assertEqual([('change kind', b'foo-id', 'file', 'directory',
0.14.32 by Aaron Bentley
Replace ShelfCreator.__iter__ with ShelfCreator.iter_shelvable
427
                           'foo')], sorted(list(creator.iter_shelvable())))
4526.7.3 by Aaron Bentley
Test shelve_change.
428
        return creator
429
430
    def check_shelve_change_kind(self, creator):
7045.1.20 by Jelmer Vernooij
Fix per_pack_repository tests.
431
        self.assertFileEqual(b'bar', 'tree/foo')
6855.4.1 by Jelmer Vernooij
Yet more bees.
432
        s_trans_id = creator.shelf_transform.trans_id_file_id(b'foo-id')
4526.7.3 by Aaron Bentley
Test shelve_change.
433
        self.assertEqual('directory',
434
                         creator.shelf_transform._new_contents[s_trans_id])
435
436
    def test_shelve_change_kind(self):
437
        creator = self.prepare_shelve_change_kind()
6855.4.1 by Jelmer Vernooij
Yet more bees.
438
        creator.shelve_content_change(b'foo-id')
0.14.23 by Aaron Bentley
Allow shelving kind change
439
        creator.transform()
4526.7.3 by Aaron Bentley
Test shelve_change.
440
        self.check_shelve_change_kind(creator)
441
442
    def test_shelve_change_handles_change_kind(self):
443
        creator = self.prepare_shelve_change_kind()
6855.4.1 by Jelmer Vernooij
Yet more bees.
444
        creator.shelve_change(('change kind', b'foo-id', 'file', 'directory',
4526.7.3 by Aaron Bentley
Test shelve_change.
445
                               'foo'))
446
        creator.transform()
447
        self.check_shelve_change_kind(creator)
448
449
    def test_shelve_change_unknown_change(self):
450
        tree = self.make_branch_and_tree('tree')
4595.13.2 by Alexander Belchenko
[cherrypick revno 4650 from bzr.dev] Fix shelve on windows. (Robert Collins, #305006)
451
        tree.lock_tree_write()
452
        self.addCleanup(tree.unlock)
4526.7.3 by Aaron Bentley
Test shelve_change.
453
        creator = shelf.ShelfCreator(tree, tree.basis_tree())
454
        self.addCleanup(creator.finalize)
455
        e = self.assertRaises(ValueError, creator.shelve_change, ('unknown',))
456
        self.assertEqual('Unknown change kind: "unknown"', str(e))
0.14.23 by Aaron Bentley
Allow shelving kind change
457
0.14.10 by Aaron Bentley
Fix behavior with deletions, unversioning, ...
458
    def test_shelve_unversion(self):
459
        tree = self.make_branch_and_tree('tree')
7143.15.2 by Jelmer Vernooij
Run autopep8.
460
        self.build_tree(['tree/foo', ])
6855.4.1 by Jelmer Vernooij
Yet more bees.
461
        tree.add('foo', b'foo-id')
0.14.10 by Aaron Bentley
Fix behavior with deletions, unversioning, ...
462
        tree.commit('Added file and directory')
6809.4.25 by Jelmer Vernooij
Add paths argument to .unversion.
463
        tree.unversion(['foo'])
4595.13.2 by Alexander Belchenko
[cherrypick revno 4650 from bzr.dev] Fix shelve on windows. (Robert Collins, #305006)
464
        tree.lock_tree_write()
465
        self.addCleanup(tree.unlock)
0.14.10 by Aaron Bentley
Fix behavior with deletions, unversioning, ...
466
        creator = shelf.ShelfCreator(tree, tree.basis_tree())
467
        self.addCleanup(creator.finalize)
6855.4.1 by Jelmer Vernooij
Yet more bees.
468
        self.assertEqual([('delete file', b'foo-id', 'file', 'foo')],
0.14.32 by Aaron Bentley
Replace ShelfCreator.__iter__ with ShelfCreator.iter_shelvable
469
                         sorted(list(creator.iter_shelvable())))
6855.4.1 by Jelmer Vernooij
Yet more bees.
470
        creator.shelve_deletion(b'foo-id')
0.14.10 by Aaron Bentley
Fix behavior with deletions, unversioning, ...
471
        creator.transform()
5784.1.3 by Martin Pool
Switch away from using failUnlessExists and failIfExists
472
        self.assertPathExists('tree/foo')
0.14.10 by Aaron Bentley
Fix behavior with deletions, unversioning, ...
473
0.14.33 by Aaron Bentley
Add explicit test of shelf on-disk format
474
    def test_shelve_serialization(self):
475
        tree = self.make_branch_and_tree('.')
4595.13.2 by Alexander Belchenko
[cherrypick revno 4650 from bzr.dev] Fix shelve on windows. (Robert Collins, #305006)
476
        tree.lock_tree_write()
477
        self.addCleanup(tree.unlock)
0.14.33 by Aaron Bentley
Add explicit test of shelf on-disk format
478
        creator = shelf.ShelfCreator(tree, tree.basis_tree())
479
        self.addCleanup(creator.finalize)
0.12.76 by Aaron Bentley
Convert failing tests
480
        shelf_file = open('shelf', 'wb')
481
        self.addCleanup(shelf_file.close)
482
        try:
483
            creator.write_shelf(shelf_file)
484
        finally:
485
            shelf_file.close()
486
        self.assertFileEqual(EMPTY_SHELF, 'shelf')
0.14.33 by Aaron Bentley
Add explicit test of shelf on-disk format
487
0.12.19 by Aaron Bentley
Add support for writing shelves
488
    def test_write_shelf(self):
489
        tree = self.make_branch_and_tree('tree')
490
        self.build_tree(['tree/foo'])
6855.4.1 by Jelmer Vernooij
Yet more bees.
491
        tree.add('foo', b'foo-id')
4595.13.2 by Alexander Belchenko
[cherrypick revno 4650 from bzr.dev] Fix shelve on windows. (Robert Collins, #305006)
492
        tree.lock_tree_write()
493
        self.addCleanup(tree.unlock)
0.14.7 by Aaron Bentley
Misc test cleanup
494
        creator = shelf.ShelfCreator(tree, tree.basis_tree())
495
        self.addCleanup(creator.finalize)
0.14.32 by Aaron Bentley
Replace ShelfCreator.__iter__ with ShelfCreator.iter_shelvable
496
        list(creator.iter_shelvable())
6855.4.1 by Jelmer Vernooij
Yet more bees.
497
        creator.shelve_creation(b'foo-id')
498
        with open('shelf', 'wb') as shelf_file:
0.12.61 by Aaron Bentley
Stop assigning result of write_shelf
499
            creator.write_shelf(shelf_file)
0.12.19 by Aaron Bentley
Add support for writing shelves
500
        parser = pack.ContainerPushParser()
6855.4.1 by Jelmer Vernooij
Yet more bees.
501
        with open('shelf', 'rb') as shelf_file:
0.12.19 by Aaron Bentley
Add support for writing shelves
502
            parser.accept_bytes(shelf_file.read())
7490.77.2 by Jelmer Vernooij
Split out git and bzr-specific transforms.
503
        tt = tree.preview_transform()
0.14.7 by Aaron Bentley
Misc test cleanup
504
        self.addCleanup(tt.finalize)
0.12.29 by Aaron Bentley
Update failing tests
505
        records = iter(parser.read_pending_records())
7143.15.2 by Jelmer Vernooij
Run autopep8.
506
        # skip revision-id
6634.2.1 by Martin
Apply 2to3 next fixer and make compatible
507
        next(records)
0.15.26 by Aaron Bentley
Merge with prepare-shelf
508
        tt.deserialize(records)
0.12.21 by Aaron Bentley
Add failing test of unshelver
509
3873.2.4 by Benoît Pierre
Add a test: test_shelve_unversioned; check if tree is in a clean state
510
    def test_shelve_unversioned(self):
511
        tree = self.make_branch_and_tree('tree')
7143.15.5 by Jelmer Vernooij
More PEP8 fixes.
512
        with tree.lock_tree_write():
4595.13.2 by Alexander Belchenko
[cherrypick revno 4650 from bzr.dev] Fix shelve on windows. (Robert Collins, #305006)
513
            self.assertRaises(errors.PathsNotVersionedError,
7143.15.5 by Jelmer Vernooij
More PEP8 fixes.
514
                              shelf.ShelfCreator, tree, tree.basis_tree(),
515
                              ['foo'])
3873.2.4 by Benoît Pierre
Add a test: test_shelve_unversioned; check if tree is in a clean state
516
        # We should be able to lock/unlock the tree if ShelfCreator cleaned
517
        # after itself.
518
        wt = workingtree.WorkingTree.open('tree')
519
        wt.lock_tree_write()
520
        wt.unlock()
521
        # And a second tentative should raise the same error (no
522
        # limbo/pending_deletion leftovers).
7143.15.5 by Jelmer Vernooij
More PEP8 fixes.
523
        with tree.lock_tree_write():
4595.13.2 by Alexander Belchenko
[cherrypick revno 4650 from bzr.dev] Fix shelve on windows. (Robert Collins, #305006)
524
            self.assertRaises(errors.PathsNotVersionedError,
7143.15.5 by Jelmer Vernooij
More PEP8 fixes.
525
                              shelf.ShelfCreator, tree, tree.basis_tree(),
526
                              ['foo'])
4595.13.2 by Alexander Belchenko
[cherrypick revno 4650 from bzr.dev] Fix shelve on windows. (Robert Collins, #305006)
527
4595.9.1 by Aaron Bentley
Fix shelve in uncommitted trees.
528
    def test_shelve_skips_added_root(self):
529
        """Skip adds of the root when iterating through shelvable changes."""
530
        tree = self.make_branch_and_tree('tree')
4595.13.2 by Alexander Belchenko
[cherrypick revno 4650 from bzr.dev] Fix shelve on windows. (Robert Collins, #305006)
531
        tree.lock_tree_write()
532
        self.addCleanup(tree.unlock)
4595.9.1 by Aaron Bentley
Fix shelve in uncommitted trees.
533
        creator = shelf.ShelfCreator(tree, tree.basis_tree())
534
        self.addCleanup(creator.finalize)
535
        self.assertEqual([], list(creator.iter_shelvable()))
536
537
    def test_shelve_skips_added_root(self):
538
        """Skip adds of the root when iterating through shelvable changes."""
539
        tree = self.make_branch_and_tree('tree')
4596.1.6 by Martin Pool
merge trunk
540
        tree.lock_tree_write()
541
        self.addCleanup(tree.unlock)
4595.9.1 by Aaron Bentley
Fix shelve in uncommitted trees.
542
        creator = shelf.ShelfCreator(tree, tree.basis_tree())
543
        self.addCleanup(creator.finalize)
544
        self.assertEqual([], list(creator.iter_shelvable()))
545
0.12.21 by Aaron Bentley
Add failing test of unshelver
546
547
class TestUnshelver(tests.TestCaseWithTransport):
548
0.15.31 by Aaron Bentley
Remove 'unshelve' method, test make_merger
549
    def test_make_merger(self):
0.12.21 by Aaron Bentley
Add failing test of unshelver
550
        tree = self.make_branch_and_tree('tree')
0.12.30 by Aaron Bentley
Fix test by using non NULL base tree
551
        tree.commit('first commit')
6855.4.1 by Jelmer Vernooij
Yet more bees.
552
        self.build_tree_contents([('tree/foo', b'bar')])
0.12.24 by Aaron Bentley
Get unshelve using merge codepath, not applying transform directly
553
        tree.lock_write()
554
        self.addCleanup(tree.unlock)
6855.4.1 by Jelmer Vernooij
Yet more bees.
555
        tree.add('foo', b'foo-id')
0.15.5 by Aaron Bentley
Rename to shelf
556
        creator = shelf.ShelfCreator(tree, tree.basis_tree())
557
        self.addCleanup(creator.finalize)
0.12.73 by Aaron Bentley
Merge unshelve into shelf-manager
558
        list(creator.iter_shelvable())
6855.4.1 by Jelmer Vernooij
Yet more bees.
559
        creator.shelve_creation(b'foo-id')
560
        with open('shelf-file', 'w+b') as shelf_file:
0.12.61 by Aaron Bentley
Stop assigning result of write_shelf
561
            creator.write_shelf(shelf_file)
0.12.29 by Aaron Bentley
Update failing tests
562
            creator.transform()
563
            shelf_file.seek(0)
0.12.34 by Aaron Bentley
merge with unshelve
564
            unshelver = shelf.Unshelver.from_tree_and_shelf(tree, shelf_file)
0.12.66 by Aaron Bentley
Merge with unshelve
565
            unshelver.make_merger().do_merge()
4659.2.3 by Vincent Ladeuil
Cleanup more bzr-limbo-XXXXXX leaks in /tmp during selftest.
566
            self.addCleanup(unshelver.finalize)
7045.1.20 by Jelmer Vernooij
Fix per_pack_repository tests.
567
            self.assertFileEqual(b'bar', 'tree/foo')
0.12.26 by Aaron Bentley
Use correct base for shelving
568
0.15.23 by Aaron Bentley
Use correct tree for desrializing transform
569
    def test_unshelve_changed(self):
570
        tree = self.make_branch_and_tree('tree')
571
        tree.lock_write()
572
        self.addCleanup(tree.unlock)
6855.4.1 by Jelmer Vernooij
Yet more bees.
573
        self.build_tree_contents([('tree/foo', b'a\nb\nc\n')])
574
        tree.add('foo', b'foo-id')
0.15.23 by Aaron Bentley
Use correct tree for desrializing transform
575
        tree.commit('first commit')
6855.4.1 by Jelmer Vernooij
Yet more bees.
576
        self.build_tree_contents([('tree/foo', b'a\nb\nd\n')])
0.15.23 by Aaron Bentley
Use correct tree for desrializing transform
577
        creator = shelf.ShelfCreator(tree, tree.basis_tree())
578
        self.addCleanup(creator.finalize)
0.12.73 by Aaron Bentley
Merge unshelve into shelf-manager
579
        list(creator.iter_shelvable())
7045.1.20 by Jelmer Vernooij
Fix per_pack_repository tests.
580
        creator.shelve_lines(b'foo-id', [b'a\n', b'b\n', b'c\n'])
0.12.57 by Aaron Bentley
Update for new Shelf API
581
        shelf_file = open('shelf', 'w+b')
582
        self.addCleanup(shelf_file.close)
583
        creator.write_shelf(shelf_file)
0.15.23 by Aaron Bentley
Use correct tree for desrializing transform
584
        creator.transform()
6855.4.1 by Jelmer Vernooij
Yet more bees.
585
        self.build_tree_contents([('tree/foo', b'z\na\nb\nc\n')])
0.12.57 by Aaron Bentley
Update for new Shelf API
586
        shelf_file.seek(0)
587
        unshelver = shelf.Unshelver.from_tree_and_shelf(tree, shelf_file)
4659.2.3 by Vincent Ladeuil
Cleanup more bzr-limbo-XXXXXX leaks in /tmp during selftest.
588
        self.addCleanup(unshelver.finalize)
0.15.31 by Aaron Bentley
Remove 'unshelve' method, test make_merger
589
        unshelver.make_merger().do_merge()
7045.1.20 by Jelmer Vernooij
Fix per_pack_repository tests.
590
        self.assertFileEqual(b'z\na\nb\nd\n', 'tree/foo')
0.15.23 by Aaron Bentley
Use correct tree for desrializing transform
591
3981.1.1 by Robert Collins
Fix bug 319790 - unshelve of deleted paths failing.
592
    def test_unshelve_deleted(self):
593
        tree = self.make_branch_and_tree('tree')
594
        tree.lock_write()
595
        self.addCleanup(tree.unlock)
6855.4.1 by Jelmer Vernooij
Yet more bees.
596
        self.build_tree_contents([('tree/foo/',), ('tree/foo/bar', b'baz')])
597
        tree.add(['foo', 'foo/bar'], [b'foo-id', b'bar-id'])
3981.1.1 by Robert Collins
Fix bug 319790 - unshelve of deleted paths failing.
598
        tree.commit('Added file and directory')
6809.4.25 by Jelmer Vernooij
Add paths argument to .unversion.
599
        tree.unversion(['foo', 'foo/bar'])
3981.1.1 by Robert Collins
Fix bug 319790 - unshelve of deleted paths failing.
600
        os.unlink('tree/foo/bar')
601
        os.rmdir('tree/foo')
602
        creator = shelf.ShelfCreator(tree, tree.basis_tree())
603
        list(creator.iter_shelvable())
6855.4.1 by Jelmer Vernooij
Yet more bees.
604
        creator.shelve_deletion(b'foo-id')
605
        creator.shelve_deletion(b'bar-id')
5954.5.1 by Vincent Ladeuil
Tweak comment in Merge3Merger._merge_names and the corresponding test.
606
        with open('shelf', 'w+b') as shelf_file:
607
            creator.write_shelf(shelf_file)
608
            creator.transform()
609
            creator.finalize()
3981.1.1 by Robert Collins
Fix bug 319790 - unshelve of deleted paths failing.
610
        # validate the test setup
7397.4.7 by Jelmer Vernooij
Remove Tree.has_id.
611
        self.assertEqual(tree.id2path(b'foo-id'), 'foo')
612
        self.assertEqual(tree.id2path(b'bar-id'), 'foo/bar')
7045.1.20 by Jelmer Vernooij
Fix per_pack_repository tests.
613
        self.assertFileEqual(b'baz', 'tree/foo/bar')
5954.5.1 by Vincent Ladeuil
Tweak comment in Merge3Merger._merge_names and the corresponding test.
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()
7397.4.7 by Jelmer Vernooij
Remove Tree.has_id.
618
        self.assertRaises(errors.NoSuchId, tree.id2path, b'foo-id')
619
        self.assertRaises(errors.NoSuchId, tree.id2path, b'bar-id')
3981.1.1 by Robert Collins
Fix bug 319790 - unshelve of deleted paths failing.
620
0.12.26 by Aaron Bentley
Use correct base for shelving
621
    def test_unshelve_base(self):
622
        tree = self.make_branch_and_tree('tree')
623
        tree.lock_write()
624
        self.addCleanup(tree.unlock)
6855.4.1 by Jelmer Vernooij
Yet more bees.
625
        tree.commit('rev1', rev_id=b'rev1')
0.15.5 by Aaron Bentley
Rename to shelf
626
        creator = shelf.ShelfCreator(tree, tree.basis_tree())
0.12.59 by Aaron Bentley
Fix locking bugs in tests
627
        self.addCleanup(creator.finalize)
0.12.42 by Aaron Bentley
Get shelf from tree
628
        manager = tree.get_shelf_manager()
0.12.29 by Aaron Bentley
Update failing tests
629
        shelf_id, shelf_file = manager.new_shelf()
630
        try:
0.12.61 by Aaron Bentley
Stop assigning result of write_shelf
631
            creator.write_shelf(shelf_file)
0.12.29 by Aaron Bentley
Update failing tests
632
        finally:
633
            shelf_file.close()
6855.4.1 by Jelmer Vernooij
Yet more bees.
634
        tree.commit('rev2', rev_id=b'rev2')
0.12.29 by Aaron Bentley
Update failing tests
635
        shelf_file = manager.read_shelf(1)
0.12.59 by Aaron Bentley
Fix locking bugs in tests
636
        self.addCleanup(shelf_file.close)
637
        unshelver = shelf.Unshelver.from_tree_and_shelf(tree, shelf_file)
638
        self.addCleanup(unshelver.finalize)
6855.4.1 by Jelmer Vernooij
Yet more bees.
639
        self.assertEqual(b'rev1', unshelver.base_tree.get_revision_id())
0.12.27 by Aaron Bentley
Implement shelf manager
640
0.15.41 by Aaron Bentley
Replace assert with proper error handling
641
    def test_unshelve_serialization(self):
642
        tree = self.make_branch_and_tree('.')
643
        self.build_tree_contents([('shelf', EMPTY_SHELF)])
0.12.76 by Aaron Bentley
Convert failing tests
644
        shelf_file = open('shelf', 'rb')
645
        self.addCleanup(shelf_file.close)
646
        unshelver = shelf.Unshelver.from_tree_and_shelf(tree, shelf_file)
4595.13.2 by Alexander Belchenko
[cherrypick revno 4650 from bzr.dev] Fix shelve on windows. (Robert Collins, #305006)
647
        unshelver.finalize()
0.15.41 by Aaron Bentley
Replace assert with proper error handling
648
649
    def test_corrupt_shelf(self):
650
        tree = self.make_branch_and_tree('.')
6855.4.1 by Jelmer Vernooij
Yet more bees.
651
        self.build_tree_contents([('shelf', EMPTY_SHELF.replace(b'metadata',
652
                                                                b'foo'))])
0.12.76 by Aaron Bentley
Convert failing tests
653
        shelf_file = open('shelf', 'rb')
654
        self.addCleanup(shelf_file.close)
6734.1.1 by Jelmer Vernooij
Fix more imports.
655
        e = self.assertRaises(shelf.ShelfCorrupt,
0.15.41 by Aaron Bentley
Replace assert with proper error handling
656
                              shelf.Unshelver.from_tree_and_shelf, tree,
0.12.76 by Aaron Bentley
Convert failing tests
657
                              shelf_file)
0.15.41 by Aaron Bentley
Replace assert with proper error handling
658
        self.assertEqual('Shelf corrupt.', str(e))
0.12.75 by Aaron Bentley
Merge unshelve into shelf-manager
659
5599.1.1 by John Arbash Meinel
Move away from using Tree.inventory[] just to check if this is a root.
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'])
6855.4.1 by Jelmer Vernooij
Yet more bees.
664
        tree.add(['dir'], [b'dir-id'])
5599.1.1 by John Arbash Meinel
Move away from using Tree.inventory[] just to check if this is a root.
665
        tree.commit('versioned dir')
6855.4.1 by Jelmer Vernooij
Yet more bees.
666
        tree.add(['dir/subdir', 'dir/subdir/foo'], [b'subdir-id', b'foo-id'])
5599.1.1 by John Arbash Meinel
Move away from using Tree.inventory[] just to check if this is a root.
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)
5784.1.3 by Martin Pool
Switch away from using failUnlessExists and failIfExists
673
        self.assertPathDoesNotExist('dir/subdir')
5599.1.1 by John Arbash Meinel
Move away from using Tree.inventory[] just to check if this is a root.
674
        tree.remove(['dir'])
675
        unshelver = shelf_manager.get_unshelver(shelf_id)
676
        self.addCleanup(unshelver.finalize)
677
        unshelver.make_merger().do_merge()
5784.1.3 by Martin Pool
Switch away from using failUnlessExists and failIfExists
678
        self.assertPathExists('dir/subdir/foo')
6855.4.1 by Jelmer Vernooij
Yet more bees.
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'))
5599.1.1 by John Arbash Meinel
Move away from using Tree.inventory[] just to check if this is a root.
682
0.12.27 by Aaron Bentley
Implement shelf manager
683
684
class TestShelfManager(tests.TestCaseWithTransport):
685
0.12.42 by Aaron Bentley
Get shelf from tree
686
    def test_get_shelf_manager(self):
0.12.27 by Aaron Bentley
Implement shelf manager
687
        tree = self.make_branch_and_tree('.')
0.12.42 by Aaron Bentley
Get shelf from tree
688
        manager = tree.get_shelf_manager()
0.12.41 by Aaron Bentley
Change shelf to use WT control dir for shelves
689
        self.assertEqual(tree._transport.base + 'shelf/',
0.12.27 by Aaron Bentley
Implement shelf manager
690
                         manager.transport.base)
691
692
    def get_manager(self):
0.12.42 by Aaron Bentley
Get shelf from tree
693
        return self.make_branch_and_tree('.').get_shelf_manager()
0.12.27 by Aaron Bentley
Implement shelf manager
694
0.12.77 by Aaron Bentley
Use names of the form shelf-5 for shelves
695
    def test_get_shelf_filename(self):
696
        tree = self.make_branch_and_tree('.')
697
        manager = tree.get_shelf_manager()
698
        self.assertEqual('shelf-1', manager.get_shelf_filename(1))
699
700
    def test_get_shelf_ids(self):
701
        tree = self.make_branch_and_tree('.')
702
        manager = tree.get_shelf_manager()
703
        self.assertEqual([1, 3], manager.get_shelf_ids(
704
                         ['shelf-1', 'shelf-02', 'shelf-3']))
705
0.12.27 by Aaron Bentley
Implement shelf manager
706
    def test_new_shelf(self):
707
        manager = self.get_manager()
708
        shelf_id, shelf_file = manager.new_shelf()
709
        shelf_file.close()
710
        self.assertEqual(1, shelf_id)
711
        shelf_id, shelf_file = manager.new_shelf()
712
        shelf_file.close()
713
        self.assertEqual(2, shelf_id)
714
        manager.delete_shelf(1)
715
        shelf_id, shelf_file = manager.new_shelf()
716
        shelf_file.close()
717
        self.assertEqual(3, shelf_id)
718
719
    def test_active_shelves(self):
720
        manager = self.get_manager()
721
        self.assertEqual([], manager.active_shelves())
722
        shelf_id, shelf_file = manager.new_shelf()
723
        shelf_file.close()
724
        self.assertEqual([1], manager.active_shelves())
725
726
    def test_delete_shelf(self):
727
        manager = self.get_manager()
728
        shelf_id, shelf_file = manager.new_shelf()
729
        shelf_file.close()
730
        self.assertEqual([1], manager.active_shelves())
731
        manager.delete_shelf(1)
732
        self.assertEqual([], manager.active_shelves())
733
734
    def test_last_shelf(self):
735
        manager = self.get_manager()
736
        self.assertIs(None, manager.last_shelf())
737
        shelf_id, shelf_file = manager.new_shelf()
738
        shelf_file.close()
739
        self.assertEqual(1, manager.last_shelf())
740
741
    def test_read_shelf(self):
742
        manager = self.get_manager()
743
        shelf_id, shelf_file = manager.new_shelf()
744
        try:
6973.7.5 by Jelmer Vernooij
s/file/open.
745
            shelf_file.write(b'foo')
0.12.27 by Aaron Bentley
Implement shelf manager
746
        finally:
747
            shelf_file.close()
748
        shelf_id, shelf_file = manager.new_shelf()
749
        try:
6973.7.5 by Jelmer Vernooij
s/file/open.
750
            shelf_file.write(b'bar')
0.12.27 by Aaron Bentley
Implement shelf manager
751
        finally:
752
            shelf_file.close()
753
        shelf_file = manager.read_shelf(1)
754
        try:
6973.7.5 by Jelmer Vernooij
s/file/open.
755
            self.assertEqual(b'foo', shelf_file.read())
0.12.27 by Aaron Bentley
Implement shelf manager
756
        finally:
757
            shelf_file.close()
758
        shelf_file = manager.read_shelf(2)
759
        try:
6973.7.5 by Jelmer Vernooij
s/file/open.
760
            self.assertEqual(b'bar', shelf_file.read())
0.12.27 by Aaron Bentley
Implement shelf manager
761
        finally:
762
            shelf_file.close()
0.12.43 by Aaron Bentley
Make ShelfManager consume ShelfCreator and produce Unshelver
763
0.12.50 by Aaron Bentley
Improve error handling for non-existant shelf-ids
764
    def test_read_non_existant(self):
765
        manager = self.get_manager()
6734.1.1 by Jelmer Vernooij
Fix more imports.
766
        e = self.assertRaises(shelf.NoSuchShelfId, manager.read_shelf, 1)
0.12.50 by Aaron Bentley
Improve error handling for non-existant shelf-ids
767
        self.assertEqual('No changes are shelved with id "1".', str(e))
768
0.12.43 by Aaron Bentley
Make ShelfManager consume ShelfCreator and produce Unshelver
769
    def test_shelve_changes(self):
770
        tree = self.make_branch_and_tree('tree')
0.12.44 by Aaron Bentley
Give manager responsibility for applying transform
771
        tree.commit('no-change commit')
772
        tree.lock_write()
773
        self.addCleanup(tree.unlock)
6855.4.1 by Jelmer Vernooij
Yet more bees.
774
        self.build_tree_contents([('tree/foo', b'bar')])
7045.1.20 by Jelmer Vernooij
Fix per_pack_repository tests.
775
        self.assertFileEqual(b'bar', 'tree/foo')
6855.4.1 by Jelmer Vernooij
Yet more bees.
776
        tree.add('foo', b'foo-id')
0.12.43 by Aaron Bentley
Make ShelfManager consume ShelfCreator and produce Unshelver
777
        creator = shelf.ShelfCreator(tree, tree.basis_tree())
778
        self.addCleanup(creator.finalize)
0.12.74 by Aaron Bentley
Update to use iter_shelvable
779
        list(creator.iter_shelvable())
6855.4.1 by Jelmer Vernooij
Yet more bees.
780
        creator.shelve_creation(b'foo-id')
0.12.43 by Aaron Bentley
Make ShelfManager consume ShelfCreator and produce Unshelver
781
        shelf_manager = tree.get_shelf_manager()
782
        shelf_id = shelf_manager.shelve_changes(creator)
5784.1.3 by Martin Pool
Switch away from using failUnlessExists and failIfExists
783
        self.assertPathDoesNotExist('tree/foo')
0.12.43 by Aaron Bentley
Make ShelfManager consume ShelfCreator and produce Unshelver
784
        unshelver = shelf_manager.get_unshelver(shelf_id)
4659.2.3 by Vincent Ladeuil
Cleanup more bzr-limbo-XXXXXX leaks in /tmp during selftest.
785
        self.addCleanup(unshelver.finalize)
0.12.67 by Aaron Bentley
Update for new Unshelver API
786
        unshelver.make_merger().do_merge()
7045.1.20 by Jelmer Vernooij
Fix per_pack_repository tests.
787
        self.assertFileEqual(b'bar', 'tree/foo')
0.16.112 by Aaron Bentley
Add tests
788
789
    def test_get_metadata(self):
790
        tree = self.make_branch_and_tree('.')
4595.13.2 by Alexander Belchenko
[cherrypick revno 4650 from bzr.dev] Fix shelve on windows. (Robert Collins, #305006)
791
        tree.lock_tree_write()
792
        self.addCleanup(tree.unlock)
0.16.112 by Aaron Bentley
Add tests
793
        creator = shelf.ShelfCreator(tree, tree.basis_tree())
4595.13.2 by Alexander Belchenko
[cherrypick revno 4650 from bzr.dev] Fix shelve on windows. (Robert Collins, #305006)
794
        self.addCleanup(creator.finalize)
0.16.112 by Aaron Bentley
Add tests
795
        shelf_manager = tree.get_shelf_manager()
796
        shelf_id = shelf_manager.shelve_changes(creator, 'foo')
797
        metadata = shelf_manager.get_metadata(shelf_id)
7045.3.1 by Jelmer Vernooij
Fix another ~500 tests.
798
        self.assertEqual('foo', metadata[b'message'])
799
        self.assertEqual(b'null:', metadata[b'revision_id'])