/brz/remove-bazaar

To get this branch, use:
bzr branch http://gegoxaren.bato24.eu/bzr/brz/remove-bazaar
4634.125.4 by John Arbash Meinel
Merge bzr/2.0@4725, resolve NEWS
1
# Copyright (C) 2006-2010 Canonical Ltd
1887.1.1 by Adeodato Simó
Do not separate paragraphs in the copyright statement with blank lines,
2
#
1534.7.106 by Aaron Bentley
Cleaned up imports, added copyright statements
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.
1887.1.1 by Adeodato Simó
Do not separate paragraphs in the copyright statement with blank lines,
7
#
1534.7.106 by Aaron Bentley
Cleaned up imports, added copyright statements
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.
1887.1.1 by Adeodato Simó
Do not separate paragraphs in the copyright statement with blank lines,
12
#
1534.7.106 by Aaron Bentley
Cleaned up imports, added copyright statements
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
1534.7.106 by Aaron Bentley
Cleaned up imports, added copyright statements
16
17
import os
3008.1.1 by Aaron Bentley
Start work allowing previews of transforms
18
from StringIO import StringIO
2027.1.1 by John Arbash Meinel
Fix bug #56549, and write a direct test that the right path is being statted
19
import sys
4934.1.1 by John Arbash Meinel
Basic implementation for windows and bug #488724.
20
import time
1558.1.3 by Aaron Bentley
Fixed deprecated op use in test suite
21
2027.1.1 by John Arbash Meinel
Fix bug #56549, and write a direct test that the right path is being statted
22
from bzrlib import (
2694.5.4 by Jelmer Vernooij
Move bzrlib.util.bencode to bzrlib._bencode_py.
23
    bencode,
2090.2.1 by Martin Pool
Fix some code which relies on assertions and breaks under python -O
24
    errors,
4826.1.5 by Andrew Bennetts
Add test that content filtered files are not hardlinked by build_tree.
25
    filters,
2116.4.1 by John Arbash Meinel
Update file and revision id generators.
26
    generate_ids,
3199.1.4 by Vincent Ladeuil
Fix 16 leaked tmp dirs. Probably indicates a lock handling problem with TransformPreview
27
    osutils,
3008.1.18 by Aaron Bentley
Get supported PreviewTree functionality under test
28
    revision as _mod_revision,
4826.1.5 by Andrew Bennetts
Add test that content filtered files are not hardlinked by build_tree.
29
    rules,
2027.1.1 by John Arbash Meinel
Fix bug #56549, and write a direct test that the right path is being statted
30
    tests,
31
    urlutils,
32
    )
1558.1.3 by Aaron Bentley
Fixed deprecated op use in test suite
33
from bzrlib.bzrdir import BzrDir
5162.4.1 by Aaron Bentley
TreeTransform supports normal commit parameters and includes branch nick.
34
from bzrlib.conflicts import (
35
    DeletingParent,
36
    DuplicateEntry,
37
    DuplicateID,
38
    MissingParent,
39
    NonDirectoryParent,
40
    ParentLoop,
41
    UnversionedParent,
42
)
3008.1.1 by Aaron Bentley
Start work allowing previews of transforms
43
from bzrlib.diff import show_diff_trees
5162.4.1 by Aaron Bentley
TreeTransform supports normal commit parameters and includes branch nick.
44
from bzrlib.errors import (
45
    DuplicateKey,
46
    ExistingLimbo,
47
    ExistingPendingDeletion,
48
    ImmortalLimbo,
49
    ImmortalPendingDeletion,
50
    LockError,
51
    MalformedTransform,
52
    NoSuchFile,
53
    ReusingTransform,
54
)
55
from bzrlib.osutils import (
56
    file_kind,
57
    pathjoin,
58
)
3363.17.1 by Aaron Bentley
Avoid inventory for merge and transform code
59
from bzrlib.merge import Merge3Merger, Merger
2949.5.1 by Alexander Belchenko
selftest: use SymlinkFeature instead of TestSkipped where appropriate
60
from bzrlib.tests import (
3136.1.1 by Aaron Bentley
Add support for hardlinks to TreeTransform
61
    HardlinkFeature,
2949.5.1 by Alexander Belchenko
selftest: use SymlinkFeature instead of TestSkipped where appropriate
62
    SymlinkFeature,
63
    TestCase,
64
    TestCaseInTempDir,
65
    TestSkipped,
5162.4.1 by Aaron Bentley
TreeTransform supports normal commit parameters and includes branch nick.
66
)
67
from bzrlib.transform import (
68
    build_tree,
69
    create_from_tree,
70
    cook_conflicts,
71
    _FileMover,
72
    FinalPaths,
73
    get_backup_name,
74
    resolve_conflicts,
75
    resolve_checkout,
76
    ROOT_PARENT,
77
    TransformPreview,
78
    TreeTransform,
79
)
0.13.13 by Aaron Bentley
Add direct test of serialization records
80
1534.7.1 by Aaron Bentley
Got creation of a versioned file working
81
2100.3.21 by Aaron Bentley
Work on checking out by-reference trees
82
class TestTreeTransform(tests.TestCaseWithTransport):
1740.2.4 by Aaron Bentley
Update transform tests and docs
83
1534.7.59 by Aaron Bentley
Simplified tests
84
    def setUp(self):
85
        super(TestTreeTransform, self).setUp()
2255.2.194 by Robert Collins
[BROKEN] Many updates to stop using experimental formats in tests.
86
        self.wt = self.make_branch_and_tree('.', format='dirstate-with-subtree')
1534.7.161 by Aaron Bentley
Used appropriate control_files
87
        os.chdir('..')
1534.7.59 by Aaron Bentley
Simplified tests
88
89
    def get_transform(self):
90
        transform = TreeTransform(self.wt)
3453.2.7 by Aaron Bentley
Remove test kipple
91
        self.addCleanup(transform.finalize)
1731.1.33 by Aaron Bentley
Revert no-special-root changes
92
        return transform, transform.root
1534.7.59 by Aaron Bentley
Simplified tests
93
1534.7.162 by Aaron Bentley
Handle failures creating/deleting the Limbo directory
94
    def test_existing_limbo(self):
95
        transform, root = self.get_transform()
2733.2.6 by Aaron Bentley
Make TreeTransform commits rollbackable
96
        limbo_name = transform._limbodir
97
        deletion_path = transform._deletiondir
1534.7.176 by abentley
Fixed up tests for Windows
98
        os.mkdir(pathjoin(limbo_name, 'hehe'))
1534.7.162 by Aaron Bentley
Handle failures creating/deleting the Limbo directory
99
        self.assertRaises(ImmortalLimbo, transform.apply)
100
        self.assertRaises(LockError, self.wt.unlock)
101
        self.assertRaises(ExistingLimbo, self.get_transform)
102
        self.assertRaises(LockError, self.wt.unlock)
1534.7.176 by abentley
Fixed up tests for Windows
103
        os.rmdir(pathjoin(limbo_name, 'hehe'))
1534.7.162 by Aaron Bentley
Handle failures creating/deleting the Limbo directory
104
        os.rmdir(limbo_name)
2733.2.6 by Aaron Bentley
Make TreeTransform commits rollbackable
105
        os.rmdir(deletion_path)
1534.7.162 by Aaron Bentley
Handle failures creating/deleting the Limbo directory
106
        transform, root = self.get_transform()
107
        transform.apply()
1534.7.59 by Aaron Bentley
Simplified tests
108
2733.2.11 by Aaron Bentley
Detect irregularities with the pending-deletion directory
109
    def test_existing_pending_deletion(self):
110
        transform, root = self.get_transform()
111
        deletion_path = self._limbodir = urlutils.local_path_from_url(
3407.2.8 by Martin Pool
Deprecate LockableFiles.controlfilename
112
            transform._tree._transport.abspath('pending-deletion'))
2733.2.11 by Aaron Bentley
Detect irregularities with the pending-deletion directory
113
        os.mkdir(pathjoin(deletion_path, 'blocking-directory'))
114
        self.assertRaises(ImmortalPendingDeletion, transform.apply)
115
        self.assertRaises(LockError, self.wt.unlock)
116
        self.assertRaises(ExistingPendingDeletion, self.get_transform)
117
1534.7.1 by Aaron Bentley
Got creation of a versioned file working
118
    def test_build(self):
3034.2.1 by Aaron Bentley
Fix is_executable tests for win32
119
        transform, root = self.get_transform()
120
        self.wt.lock_tree_write()
121
        self.addCleanup(self.wt.unlock)
1534.7.59 by Aaron Bentley
Simplified tests
122
        self.assertIs(transform.get_tree_parent(root), ROOT_PARENT)
1534.7.181 by Aaron Bentley
Renamed a bunch of functions
123
        imaginary_id = transform.trans_id_tree_path('imaginary')
1534.10.32 by Aaron Bentley
Test and fix case where name has trailing slash
124
        imaginary_id2 = transform.trans_id_tree_path('imaginary/')
125
        self.assertEqual(imaginary_id, imaginary_id2)
4597.9.7 by Vincent Ladeuil
transform.final_kind() now returns None instead of raising NoSuchFile.
126
        self.assertEqual(root, transform.get_tree_parent(imaginary_id))
127
        self.assertEqual('directory', transform.final_kind(root))
128
        self.assertEqual(self.wt.get_root_id(), transform.final_file_id(root))
1534.7.59 by Aaron Bentley
Simplified tests
129
        trans_id = transform.create_path('name', root)
130
        self.assertIs(transform.final_file_id(trans_id), None)
4597.9.7 by Vincent Ladeuil
transform.final_kind() now returns None instead of raising NoSuchFile.
131
        self.assertIs(None, transform.final_kind(trans_id))
1534.7.59 by Aaron Bentley
Simplified tests
132
        transform.create_file('contents', trans_id)
133
        transform.set_executability(True, trans_id)
134
        transform.version_file('my_pretties', trans_id)
135
        self.assertRaises(DuplicateKey, transform.version_file,
136
                          'my_pretties', trans_id)
137
        self.assertEqual(transform.final_file_id(trans_id), 'my_pretties')
138
        self.assertEqual(transform.final_parent(trans_id), root)
139
        self.assertIs(transform.final_parent(root), ROOT_PARENT)
140
        self.assertIs(transform.get_tree_parent(root), ROOT_PARENT)
141
        oz_id = transform.create_path('oz', root)
142
        transform.create_directory(oz_id)
143
        transform.version_file('ozzie', oz_id)
144
        trans_id2 = transform.create_path('name2', root)
145
        transform.create_file('contents', trans_id2)
146
        transform.set_executability(False, trans_id2)
147
        transform.version_file('my_pretties2', trans_id2)
1534.7.191 by Aaron Bentley
Got transform.apply to list modified paths
148
        modified_paths = transform.apply().modified_paths
1534.7.100 by Aaron Bentley
Fixed path-relative test cases
149
        self.assertEqual('contents', self.wt.get_file_byname('name').read())
1534.7.59 by Aaron Bentley
Simplified tests
150
        self.assertEqual(self.wt.path2id('name'), 'my_pretties')
151
        self.assertIs(self.wt.is_executable('my_pretties'), True)
152
        self.assertIs(self.wt.is_executable('my_pretties2'), False)
1534.7.100 by Aaron Bentley
Fixed path-relative test cases
153
        self.assertEqual('directory', file_kind(self.wt.abspath('oz')))
1534.7.191 by Aaron Bentley
Got transform.apply to list modified paths
154
        self.assertEqual(len(modified_paths), 3)
3943.8.1 by Marius Kruger
remove all trailing whitespace from bzr source
155
        tree_mod_paths = [self.wt.id2abspath(f) for f in
1534.7.191 by Aaron Bentley
Got transform.apply to list modified paths
156
                          ('ozzie', 'my_pretties', 'my_pretties2')]
157
        self.assertSubset(tree_mod_paths, modified_paths)
1534.7.1 by Aaron Bentley
Got creation of a versioned file working
158
        # is it safe to finalize repeatedly?
159
        transform.finalize()
1534.7.59 by Aaron Bentley
Simplified tests
160
        transform.finalize()
1534.7.2 by Aaron Bentley
Added convenience function
161
4934.1.1 by John Arbash Meinel
Basic implementation for windows and bug #488724.
162
    def test_create_files_same_timestamp(self):
163
        transform, root = self.get_transform()
164
        self.wt.lock_tree_write()
165
        self.addCleanup(self.wt.unlock)
166
        # Roll back the clock, so that we know everything is being set to the
167
        # exact time
4934.1.10 by John Arbash Meinel
Rework the tests a bit.
168
        transform._creation_mtime = creation_mtime = time.time() - 20.0
4934.1.1 by John Arbash Meinel
Basic implementation for windows and bug #488724.
169
        transform.create_file('content-one',
170
                              transform.create_path('one', root))
171
        time.sleep(1) # *ugly*
172
        transform.create_file('content-two',
173
                              transform.create_path('two', root))
174
        transform.apply()
175
        fo, st1 = self.wt.get_file_with_stat(None, path='one', filtered=False)
176
        fo.close()
177
        fo, st2 = self.wt.get_file_with_stat(None, path='two', filtered=False)
178
        fo.close()
4934.2.2 by Martin
Rearrange osutils.fset_mtime tests a little and make them robust on FAT filesystems
179
        # We only guarantee 2s resolution
4934.1.11 by John Arbash Meinel
2 => 2.0 because it looks better.
180
        self.assertTrue(abs(creation_mtime - st1.st_mtime) < 2.0,
4934.1.10 by John Arbash Meinel
Rework the tests a bit.
181
            "%s != %s within 2 seconds" % (creation_mtime, st1.st_mtime))
4934.1.6 by John Arbash Meinel
It turns out that with os/python/C buffering, we need to flush
182
        # But if we have more than that, all files should get the same result
4934.1.1 by John Arbash Meinel
Basic implementation for windows and bug #488724.
183
        self.assertEqual(st1.st_mtime, st2.st_mtime)
184
4634.122.2 by Aaron Bentley, John Arbash Meinel
Bring the fixup_new_roots code from the nested-trees code.
185
    def test_change_root_id(self):
186
        transform, root = self.get_transform()
187
        self.assertNotEqual('new-root-id', self.wt.get_root_id())
188
        transform.new_directory('', ROOT_PARENT, 'new-root-id')
189
        transform.delete_contents(root)
190
        transform.unversion_file(root)
191
        transform.fixup_new_roots()
192
        transform.apply()
193
        self.assertEqual('new-root-id', self.wt.get_root_id())
194
195
    def test_change_root_id_add_files(self):
196
        transform, root = self.get_transform()
197
        self.assertNotEqual('new-root-id', self.wt.get_root_id())
198
        new_trans_id = transform.new_directory('', ROOT_PARENT, 'new-root-id')
199
        transform.new_file('file', new_trans_id, ['new-contents\n'],
200
                           'new-file-id')
201
        transform.delete_contents(root)
202
        transform.unversion_file(root)
203
        transform.fixup_new_roots()
204
        transform.apply()
205
        self.assertEqual('new-root-id', self.wt.get_root_id())
206
        self.assertEqual('new-file-id', self.wt.path2id('file'))
207
        self.assertFileEqual('new-contents\n', self.wt.abspath('file'))
208
209
    def test_add_two_roots(self):
210
        transform, root = self.get_transform()
211
        new_trans_id = transform.new_directory('', ROOT_PARENT, 'new-root-id')
212
        new_trans_id = transform.new_directory('', ROOT_PARENT, 'alt-root-id')
213
        self.assertRaises(ValueError, transform.fixup_new_roots)
214
3136.1.1 by Aaron Bentley
Add support for hardlinks to TreeTransform
215
    def test_hardlink(self):
216
        self.requireFeature(HardlinkFeature)
217
        transform, root = self.get_transform()
218
        transform.new_file('file1', root, 'contents')
219
        transform.apply()
220
        target = self.make_branch_and_tree('target')
221
        target_transform = TreeTransform(target)
222
        trans_id = target_transform.create_path('file1', target_transform.root)
223
        target_transform.create_hardlink(self.wt.abspath('file1'), trans_id)
224
        target_transform.apply()
225
        self.failUnlessExists('target/file1')
226
        source_stat = os.stat(self.wt.abspath('file1'))
227
        target_stat = os.stat('target/file1')
228
        self.assertEqual(source_stat, target_stat)
229
1534.7.2 by Aaron Bentley
Added convenience function
230
    def test_convenience(self):
1534.7.59 by Aaron Bentley
Simplified tests
231
        transform, root = self.get_transform()
3034.2.1 by Aaron Bentley
Fix is_executable tests for win32
232
        self.wt.lock_tree_write()
233
        self.addCleanup(self.wt.unlock)
3943.8.1 by Marius Kruger
remove all trailing whitespace from bzr source
234
        trans_id = transform.new_file('name', root, 'contents',
1534.7.59 by Aaron Bentley
Simplified tests
235
                                      'my_pretties', True)
236
        oz = transform.new_directory('oz', root, 'oz-id')
237
        dorothy = transform.new_directory('dorothy', oz, 'dorothy-id')
3943.8.1 by Marius Kruger
remove all trailing whitespace from bzr source
238
        toto = transform.new_file('toto', dorothy, 'toto-contents',
1534.7.59 by Aaron Bentley
Simplified tests
239
                                  'toto-id', False)
240
241
        self.assertEqual(len(transform.find_conflicts()), 0)
242
        transform.apply()
243
        self.assertRaises(ReusingTransform, transform.find_conflicts)
1534.7.100 by Aaron Bentley
Fixed path-relative test cases
244
        self.assertEqual('contents', file(self.wt.abspath('name')).read())
1534.7.59 by Aaron Bentley
Simplified tests
245
        self.assertEqual(self.wt.path2id('name'), 'my_pretties')
246
        self.assertIs(self.wt.is_executable('my_pretties'), True)
247
        self.assertEqual(self.wt.path2id('oz'), 'oz-id')
248
        self.assertEqual(self.wt.path2id('oz/dorothy'), 'dorothy-id')
249
        self.assertEqual(self.wt.path2id('oz/dorothy/toto'), 'toto-id')
250
2255.2.194 by Robert Collins
[BROKEN] Many updates to stop using experimental formats in tests.
251
        self.assertEqual('toto-contents',
1534.7.100 by Aaron Bentley
Fixed path-relative test cases
252
                         self.wt.get_file_byname('oz/dorothy/toto').read())
1534.7.59 by Aaron Bentley
Simplified tests
253
        self.assertIs(self.wt.is_executable('toto-id'), False)
1534.7.6 by Aaron Bentley
Added conflict handling
254
2100.3.21 by Aaron Bentley
Work on checking out by-reference trees
255
    def test_tree_reference(self):
256
        transform, root = self.get_transform()
257
        tree = transform._tree
258
        trans_id = transform.new_directory('reference', root, 'subtree-id')
259
        transform.set_tree_reference('subtree-revision', trans_id)
260
        transform.apply()
2255.2.194 by Robert Collins
[BROKEN] Many updates to stop using experimental formats in tests.
261
        tree.lock_read()
262
        self.addCleanup(tree.unlock)
263
        self.assertEqual('subtree-revision',
2100.3.21 by Aaron Bentley
Work on checking out by-reference trees
264
                         tree.inventory['subtree-id'].reference_revision)
265
1534.7.6 by Aaron Bentley
Added conflict handling
266
    def test_conflicts(self):
1534.7.59 by Aaron Bentley
Simplified tests
267
        transform, root = self.get_transform()
3943.8.1 by Marius Kruger
remove all trailing whitespace from bzr source
268
        trans_id = transform.new_file('name', root, 'contents',
1534.7.59 by Aaron Bentley
Simplified tests
269
                                      'my_pretties')
270
        self.assertEqual(len(transform.find_conflicts()), 0)
271
        trans_id2 = transform.new_file('name', root, 'Crontents', 'toto')
3943.8.1 by Marius Kruger
remove all trailing whitespace from bzr source
272
        self.assertEqual(transform.find_conflicts(),
1534.7.59 by Aaron Bentley
Simplified tests
273
                         [('duplicate', trans_id, trans_id2, 'name')])
274
        self.assertRaises(MalformedTransform, transform.apply)
275
        transform.adjust_path('name', trans_id, trans_id2)
3943.8.1 by Marius Kruger
remove all trailing whitespace from bzr source
276
        self.assertEqual(transform.find_conflicts(),
1534.7.59 by Aaron Bentley
Simplified tests
277
                         [('non-directory parent', trans_id)])
1534.7.181 by Aaron Bentley
Renamed a bunch of functions
278
        tinman_id = transform.trans_id_tree_path('tinman')
1534.7.59 by Aaron Bentley
Simplified tests
279
        transform.adjust_path('name', tinman_id, trans_id2)
3943.8.1 by Marius Kruger
remove all trailing whitespace from bzr source
280
        self.assertEqual(transform.find_conflicts(),
281
                         [('unversioned parent', tinman_id),
1534.7.59 by Aaron Bentley
Simplified tests
282
                          ('missing parent', tinman_id)])
283
        lion_id = transform.create_path('lion', root)
3943.8.1 by Marius Kruger
remove all trailing whitespace from bzr source
284
        self.assertEqual(transform.find_conflicts(),
285
                         [('unversioned parent', tinman_id),
1534.7.59 by Aaron Bentley
Simplified tests
286
                          ('missing parent', tinman_id)])
287
        transform.adjust_path('name', lion_id, trans_id2)
3943.8.1 by Marius Kruger
remove all trailing whitespace from bzr source
288
        self.assertEqual(transform.find_conflicts(),
1534.7.59 by Aaron Bentley
Simplified tests
289
                         [('unversioned parent', lion_id),
290
                          ('missing parent', lion_id)])
291
        transform.version_file("Courage", lion_id)
3943.8.1 by Marius Kruger
remove all trailing whitespace from bzr source
292
        self.assertEqual(transform.find_conflicts(),
293
                         [('missing parent', lion_id),
1534.7.59 by Aaron Bentley
Simplified tests
294
                          ('versioning no contents', lion_id)])
295
        transform.adjust_path('name2', root, trans_id2)
3943.8.1 by Marius Kruger
remove all trailing whitespace from bzr source
296
        self.assertEqual(transform.find_conflicts(),
1534.7.59 by Aaron Bentley
Simplified tests
297
                         [('versioning no contents', lion_id)])
298
        transform.create_file('Contents, okay?', lion_id)
299
        transform.adjust_path('name2', trans_id2, trans_id2)
3943.8.1 by Marius Kruger
remove all trailing whitespace from bzr source
300
        self.assertEqual(transform.find_conflicts(),
301
                         [('parent loop', trans_id2),
1534.7.59 by Aaron Bentley
Simplified tests
302
                          ('non-directory parent', trans_id2)])
303
        transform.adjust_path('name2', root, trans_id2)
304
        oz_id = transform.new_directory('oz', root)
305
        transform.set_executability(True, oz_id)
3943.8.1 by Marius Kruger
remove all trailing whitespace from bzr source
306
        self.assertEqual(transform.find_conflicts(),
1534.7.59 by Aaron Bentley
Simplified tests
307
                         [('unversioned executability', oz_id)])
308
        transform.version_file('oz-id', oz_id)
3943.8.1 by Marius Kruger
remove all trailing whitespace from bzr source
309
        self.assertEqual(transform.find_conflicts(),
1534.7.59 by Aaron Bentley
Simplified tests
310
                         [('non-file executability', oz_id)])
311
        transform.set_executability(None, oz_id)
1534.7.71 by abentley
All tests pass under Windows
312
        tip_id = transform.new_file('tip', oz_id, 'ozma', 'tip-id')
1534.7.59 by Aaron Bentley
Simplified tests
313
        transform.apply()
314
        self.assertEqual(self.wt.path2id('name'), 'my_pretties')
315
        self.assertEqual('contents', file(self.wt.abspath('name')).read())
316
        transform2, root = self.get_transform()
1534.7.181 by Aaron Bentley
Renamed a bunch of functions
317
        oz_id = transform2.trans_id_tree_file_id('oz-id')
1534.7.59 by Aaron Bentley
Simplified tests
318
        newtip = transform2.new_file('tip', oz_id, 'other', 'tip-id')
319
        result = transform2.find_conflicts()
1534.7.135 by Aaron Bentley
Fixed deletion handling
320
        fp = FinalPaths(transform2)
1534.7.59 by Aaron Bentley
Simplified tests
321
        self.assert_('oz/tip' in transform2._tree_path_ids)
1534.7.176 by abentley
Fixed up tests for Windows
322
        self.assertEqual(fp.get_path(newtip), pathjoin('oz', 'tip'))
1534.7.59 by Aaron Bentley
Simplified tests
323
        self.assertEqual(len(result), 2)
3943.8.1 by Marius Kruger
remove all trailing whitespace from bzr source
324
        self.assertEqual((result[0][0], result[0][1]),
1534.7.59 by Aaron Bentley
Simplified tests
325
                         ('duplicate', newtip))
3943.8.1 by Marius Kruger
remove all trailing whitespace from bzr source
326
        self.assertEqual((result[1][0], result[1][2]),
1534.7.59 by Aaron Bentley
Simplified tests
327
                         ('duplicate id', newtip))
1534.7.73 by Aaron Bentley
Changed model again. Now iterator is used immediately.
328
        transform2.finalize()
1534.7.59 by Aaron Bentley
Simplified tests
329
        transform3 = TreeTransform(self.wt)
330
        self.addCleanup(transform3.finalize)
1534.7.181 by Aaron Bentley
Renamed a bunch of functions
331
        oz_id = transform3.trans_id_tree_file_id('oz-id')
1534.7.59 by Aaron Bentley
Simplified tests
332
        transform3.delete_contents(oz_id)
3943.8.1 by Marius Kruger
remove all trailing whitespace from bzr source
333
        self.assertEqual(transform3.find_conflicts(),
1534.7.59 by Aaron Bentley
Simplified tests
334
                         [('missing parent', oz_id)])
1731.1.33 by Aaron Bentley
Revert no-special-root changes
335
        root_id = transform3.root
1534.7.181 by Aaron Bentley
Renamed a bunch of functions
336
        tip_id = transform3.trans_id_tree_file_id('tip-id')
1534.7.59 by Aaron Bentley
Simplified tests
337
        transform3.adjust_path('tip', root_id, tip_id)
338
        transform3.apply()
1534.7.36 by Aaron Bentley
Added rename tests
339
3034.4.1 by Aaron Bentley
Start handling case-insensitivity
340
    def test_conflict_on_case_insensitive(self):
341
        tree = self.make_branch_and_tree('tree')
342
        # Don't try this at home, kids!
343
        # Force the tree to report that it is case sensitive, for conflict
344
        # resolution tests
345
        tree.case_sensitive = True
346
        transform = TreeTransform(tree)
347
        self.addCleanup(transform.finalize)
348
        transform.new_file('file', transform.root, 'content')
349
        transform.new_file('FiLe', transform.root, 'content')
350
        result = transform.find_conflicts()
351
        self.assertEqual([], result)
3008.1.15 by Aaron Bentley
Make case_sensitive an aspect of the transform, not the source tree
352
        transform.finalize()
3034.4.1 by Aaron Bentley
Start handling case-insensitivity
353
        # Force the tree to report that it is case insensitive, for conflict
354
        # generation tests
355
        tree.case_sensitive = False
3008.1.15 by Aaron Bentley
Make case_sensitive an aspect of the transform, not the source tree
356
        transform = TreeTransform(tree)
357
        self.addCleanup(transform.finalize)
358
        transform.new_file('file', transform.root, 'content')
359
        transform.new_file('FiLe', transform.root, 'content')
3034.4.1 by Aaron Bentley
Start handling case-insensitivity
360
        result = transform.find_conflicts()
361
        self.assertEqual([('duplicate', 'new-1', 'new-2', 'file')], result)
362
3034.4.7 by Aaron Bentley
Add test for filename conflicts with existing files
363
    def test_conflict_on_case_insensitive_existing(self):
364
        tree = self.make_branch_and_tree('tree')
365
        self.build_tree(['tree/FiLe'])
366
        # Don't try this at home, kids!
367
        # Force the tree to report that it is case sensitive, for conflict
368
        # resolution tests
369
        tree.case_sensitive = True
370
        transform = TreeTransform(tree)
371
        self.addCleanup(transform.finalize)
372
        transform.new_file('file', transform.root, 'content')
373
        result = transform.find_conflicts()
374
        self.assertEqual([], result)
3008.1.15 by Aaron Bentley
Make case_sensitive an aspect of the transform, not the source tree
375
        transform.finalize()
3034.4.7 by Aaron Bentley
Add test for filename conflicts with existing files
376
        # Force the tree to report that it is case insensitive, for conflict
377
        # generation tests
378
        tree.case_sensitive = False
3008.1.15 by Aaron Bentley
Make case_sensitive an aspect of the transform, not the source tree
379
        transform = TreeTransform(tree)
380
        self.addCleanup(transform.finalize)
381
        transform.new_file('file', transform.root, 'content')
3034.4.7 by Aaron Bentley
Add test for filename conflicts with existing files
382
        result = transform.find_conflicts()
383
        self.assertEqual([('duplicate', 'new-1', 'new-2', 'file')], result)
384
3034.4.1 by Aaron Bentley
Start handling case-insensitivity
385
    def test_resolve_case_insensitive_conflict(self):
386
        tree = self.make_branch_and_tree('tree')
387
        # Don't try this at home, kids!
388
        # Force the tree to report that it is case insensitive, for conflict
389
        # resolution tests
390
        tree.case_sensitive = False
391
        transform = TreeTransform(tree)
392
        self.addCleanup(transform.finalize)
393
        transform.new_file('file', transform.root, 'content')
394
        transform.new_file('FiLe', transform.root, 'content')
395
        resolve_conflicts(transform)
396
        transform.apply()
397
        self.failUnlessExists('tree/file')
398
        self.failUnlessExists('tree/FiLe.moved')
399
3034.4.2 by Aaron Bentley
Get conflict handling and case-insensitive tree creation under test
400
    def test_resolve_checkout_case_conflict(self):
3034.4.1 by Aaron Bentley
Start handling case-insensitivity
401
        tree = self.make_branch_and_tree('tree')
402
        # Don't try this at home, kids!
403
        # Force the tree to report that it is case insensitive, for conflict
404
        # resolution tests
405
        tree.case_sensitive = False
406
        transform = TreeTransform(tree)
407
        self.addCleanup(transform.finalize)
408
        transform.new_file('file', transform.root, 'content')
409
        transform.new_file('FiLe', transform.root, 'content')
3034.4.2 by Aaron Bentley
Get conflict handling and case-insensitive tree creation under test
410
        resolve_conflicts(transform,
411
                          pass_func=lambda t, c: resolve_checkout(t, c, []))
3034.4.1 by Aaron Bentley
Start handling case-insensitivity
412
        transform.apply()
413
        self.failUnlessExists('tree/file')
414
        self.failUnlessExists('tree/FiLe.moved')
415
3034.4.2 by Aaron Bentley
Get conflict handling and case-insensitive tree creation under test
416
    def test_apply_case_conflict(self):
3034.4.6 by Aaron Bentley
Update apply_case_conflict tests
417
        """Ensure that a transform with case conflicts can always be applied"""
3034.4.2 by Aaron Bentley
Get conflict handling and case-insensitive tree creation under test
418
        tree = self.make_branch_and_tree('tree')
419
        transform = TreeTransform(tree)
420
        self.addCleanup(transform.finalize)
421
        transform.new_file('file', transform.root, 'content')
422
        transform.new_file('FiLe', transform.root, 'content')
3034.4.6 by Aaron Bentley
Update apply_case_conflict tests
423
        dir = transform.new_directory('dir', transform.root)
424
        transform.new_file('dirfile', dir, 'content')
425
        transform.new_file('dirFiLe', dir, 'content')
426
        resolve_conflicts(transform)
3034.4.2 by Aaron Bentley
Get conflict handling and case-insensitive tree creation under test
427
        transform.apply()
3034.4.3 by Aaron Bentley
Add case-sensitivity handling to WorkingTree
428
        self.failUnlessExists('tree/file')
429
        if not os.path.exists('tree/FiLe.moved'):
430
            self.failUnlessExists('tree/FiLe')
3034.4.6 by Aaron Bentley
Update apply_case_conflict tests
431
        self.failUnlessExists('tree/dir/dirfile')
432
        if not os.path.exists('tree/dir/dirFiLe.moved'):
433
            self.failUnlessExists('tree/dir/dirFiLe')
3034.4.2 by Aaron Bentley
Get conflict handling and case-insensitive tree creation under test
434
3034.4.1 by Aaron Bentley
Start handling case-insensitivity
435
    def test_case_insensitive_limbo(self):
436
        tree = self.make_branch_and_tree('tree')
437
        # Don't try this at home, kids!
438
        # Force the tree to report that it is case insensitive
439
        tree.case_sensitive = False
440
        transform = TreeTransform(tree)
441
        self.addCleanup(transform.finalize)
442
        dir = transform.new_directory('dir', transform.root)
443
        first = transform.new_file('file', dir, 'content')
444
        second = transform.new_file('FiLe', dir, 'content')
445
        self.assertContainsRe(transform._limbo_name(first), 'new-1/file')
446
        self.assertNotContainsRe(transform._limbo_name(second), 'new-1/FiLe')
447
4634.78.1 by Aaron Bentley
adjust_path updatest limbo paths.
448
    def test_adjust_path_updates_child_limbo_names(self):
449
        tree = self.make_branch_and_tree('tree')
450
        transform = TreeTransform(tree)
451
        self.addCleanup(transform.finalize)
452
        foo_id = transform.new_directory('foo', transform.root)
453
        bar_id = transform.new_directory('bar', foo_id)
454
        baz_id = transform.new_directory('baz', bar_id)
455
        qux_id = transform.new_directory('qux', baz_id)
456
        transform.adjust_path('quxx', foo_id, bar_id)
457
        self.assertStartsWith(transform._limbo_name(qux_id),
458
                              transform._limbo_name(bar_id))
459
1558.7.11 by Aaron Bentley
Avoid spurious conflict on add/delete
460
    def test_add_del(self):
461
        start, root = self.get_transform()
462
        start.new_directory('a', root, 'a')
463
        start.apply()
464
        transform, root = self.get_transform()
465
        transform.delete_versioned(transform.trans_id_tree_file_id('a'))
466
        transform.new_directory('a', root, 'a')
467
        transform.apply()
468
1534.7.46 by Aaron Bentley
Ensured a conflict when parents of versioned files are unversioned
469
    def test_unversioning(self):
1534.7.59 by Aaron Bentley
Simplified tests
470
        create_tree, root = self.get_transform()
471
        parent_id = create_tree.new_directory('parent', root, 'parent-id')
472
        create_tree.new_file('child', parent_id, 'child', 'child-id')
473
        create_tree.apply()
474
        unversion = TreeTransform(self.wt)
475
        self.addCleanup(unversion.finalize)
1534.7.181 by Aaron Bentley
Renamed a bunch of functions
476
        parent = unversion.trans_id_tree_path('parent')
1534.7.59 by Aaron Bentley
Simplified tests
477
        unversion.unversion_file(parent)
3943.8.1 by Marius Kruger
remove all trailing whitespace from bzr source
478
        self.assertEqual(unversion.find_conflicts(),
1534.7.59 by Aaron Bentley
Simplified tests
479
                         [('unversioned parent', parent_id)])
1534.7.181 by Aaron Bentley
Renamed a bunch of functions
480
        file_id = unversion.trans_id_tree_file_id('child-id')
1534.7.59 by Aaron Bentley
Simplified tests
481
        unversion.unversion_file(file_id)
482
        unversion.apply()
1534.7.46 by Aaron Bentley
Ensured a conflict when parents of versioned files are unversioned
483
1534.7.36 by Aaron Bentley
Added rename tests
484
    def test_name_invariants(self):
1534.7.59 by Aaron Bentley
Simplified tests
485
        create_tree, root = self.get_transform()
486
        # prepare tree
1731.1.33 by Aaron Bentley
Revert no-special-root changes
487
        root = create_tree.root
1534.7.59 by Aaron Bentley
Simplified tests
488
        create_tree.new_file('name1', root, 'hello1', 'name1')
489
        create_tree.new_file('name2', root, 'hello2', 'name2')
490
        ddir = create_tree.new_directory('dying_directory', root, 'ddir')
491
        create_tree.new_file('dying_file', ddir, 'goodbye1', 'dfile')
492
        create_tree.new_file('moving_file', ddir, 'later1', 'mfile')
493
        create_tree.new_file('moving_file2', root, 'later2', 'mfile2')
494
        create_tree.apply()
495
496
        mangle_tree,root = self.get_transform()
1731.1.33 by Aaron Bentley
Revert no-special-root changes
497
        root = mangle_tree.root
1534.7.59 by Aaron Bentley
Simplified tests
498
        #swap names
1534.7.181 by Aaron Bentley
Renamed a bunch of functions
499
        name1 = mangle_tree.trans_id_tree_file_id('name1')
500
        name2 = mangle_tree.trans_id_tree_file_id('name2')
1534.7.59 by Aaron Bentley
Simplified tests
501
        mangle_tree.adjust_path('name2', root, name1)
502
        mangle_tree.adjust_path('name1', root, name2)
503
3943.8.1 by Marius Kruger
remove all trailing whitespace from bzr source
504
        #tests for deleting parent directories
1534.7.181 by Aaron Bentley
Renamed a bunch of functions
505
        ddir = mangle_tree.trans_id_tree_file_id('ddir')
1534.7.59 by Aaron Bentley
Simplified tests
506
        mangle_tree.delete_contents(ddir)
1534.7.181 by Aaron Bentley
Renamed a bunch of functions
507
        dfile = mangle_tree.trans_id_tree_file_id('dfile')
1534.7.59 by Aaron Bentley
Simplified tests
508
        mangle_tree.delete_versioned(dfile)
509
        mangle_tree.unversion_file(dfile)
1534.7.181 by Aaron Bentley
Renamed a bunch of functions
510
        mfile = mangle_tree.trans_id_tree_file_id('mfile')
1534.7.59 by Aaron Bentley
Simplified tests
511
        mangle_tree.adjust_path('mfile', root, mfile)
512
513
        #tests for adding parent directories
514
        newdir = mangle_tree.new_directory('new_directory', root, 'newdir')
1534.7.181 by Aaron Bentley
Renamed a bunch of functions
515
        mfile2 = mangle_tree.trans_id_tree_file_id('mfile2')
1534.7.59 by Aaron Bentley
Simplified tests
516
        mangle_tree.adjust_path('mfile2', newdir, mfile2)
517
        mangle_tree.new_file('newfile', newdir, 'hello3', 'dfile')
518
        self.assertEqual(mangle_tree.final_file_id(mfile2), 'mfile2')
519
        self.assertEqual(mangle_tree.final_parent(mfile2), newdir)
520
        self.assertEqual(mangle_tree.final_file_id(mfile2), 'mfile2')
521
        mangle_tree.apply()
522
        self.assertEqual(file(self.wt.abspath('name1')).read(), 'hello2')
523
        self.assertEqual(file(self.wt.abspath('name2')).read(), 'hello1')
1534.7.176 by abentley
Fixed up tests for Windows
524
        mfile2_path = self.wt.abspath(pathjoin('new_directory','mfile2'))
1534.7.41 by Aaron Bentley
Got inventory ID movement working
525
        self.assertEqual(mangle_tree.final_parent(mfile2), newdir)
1534.7.38 by Aaron Bentley
Tested adding paths
526
        self.assertEqual(file(mfile2_path).read(), 'later2')
1534.7.59 by Aaron Bentley
Simplified tests
527
        self.assertEqual(self.wt.id2path('mfile2'), 'new_directory/mfile2')
528
        self.assertEqual(self.wt.path2id('new_directory/mfile2'), 'mfile2')
1534.7.176 by abentley
Fixed up tests for Windows
529
        newfile_path = self.wt.abspath(pathjoin('new_directory','newfile'))
1534.7.38 by Aaron Bentley
Tested adding paths
530
        self.assertEqual(file(newfile_path).read(), 'hello3')
1534.7.59 by Aaron Bentley
Simplified tests
531
        self.assertEqual(self.wt.path2id('dying_directory'), 'ddir')
532
        self.assertIs(self.wt.path2id('dying_directory/dying_file'), None)
1534.7.176 by abentley
Fixed up tests for Windows
533
        mfile2_path = self.wt.abspath(pathjoin('new_directory','mfile2'))
1534.7.43 by abentley
Fixed some Windows bugs, introduced a conflicts bug
534
1534.7.150 by Aaron Bentley
Handled simultaneous renames of parent and child better
535
    def test_both_rename(self):
536
        create_tree,root = self.get_transform()
537
        newdir = create_tree.new_directory('selftest', root, 'selftest-id')
538
        create_tree.new_file('blackbox.py', newdir, 'hello1', 'blackbox-id')
3943.8.1 by Marius Kruger
remove all trailing whitespace from bzr source
539
        create_tree.apply()
1534.7.150 by Aaron Bentley
Handled simultaneous renames of parent and child better
540
        mangle_tree,root = self.get_transform()
1534.7.181 by Aaron Bentley
Renamed a bunch of functions
541
        selftest = mangle_tree.trans_id_tree_file_id('selftest-id')
542
        blackbox = mangle_tree.trans_id_tree_file_id('blackbox-id')
1534.7.150 by Aaron Bentley
Handled simultaneous renames of parent and child better
543
        mangle_tree.adjust_path('test', root, selftest)
544
        mangle_tree.adjust_path('test_too_much', root, selftest)
545
        mangle_tree.set_executability(True, blackbox)
546
        mangle_tree.apply()
547
548
    def test_both_rename2(self):
549
        create_tree,root = self.get_transform()
550
        bzrlib = create_tree.new_directory('bzrlib', root, 'bzrlib-id')
551
        tests = create_tree.new_directory('tests', bzrlib, 'tests-id')
552
        blackbox = create_tree.new_directory('blackbox', tests, 'blackbox-id')
3943.8.1 by Marius Kruger
remove all trailing whitespace from bzr source
553
        create_tree.new_file('test_too_much.py', blackbox, 'hello1',
1534.7.150 by Aaron Bentley
Handled simultaneous renames of parent and child better
554
                             'test_too_much-id')
3943.8.1 by Marius Kruger
remove all trailing whitespace from bzr source
555
        create_tree.apply()
1534.7.150 by Aaron Bentley
Handled simultaneous renames of parent and child better
556
        mangle_tree,root = self.get_transform()
1534.7.181 by Aaron Bentley
Renamed a bunch of functions
557
        bzrlib = mangle_tree.trans_id_tree_file_id('bzrlib-id')
558
        tests = mangle_tree.trans_id_tree_file_id('tests-id')
559
        test_too_much = mangle_tree.trans_id_tree_file_id('test_too_much-id')
1534.7.150 by Aaron Bentley
Handled simultaneous renames of parent and child better
560
        mangle_tree.adjust_path('selftest', bzrlib, tests)
3943.8.1 by Marius Kruger
remove all trailing whitespace from bzr source
561
        mangle_tree.adjust_path('blackbox.py', tests, test_too_much)
1534.7.150 by Aaron Bentley
Handled simultaneous renames of parent and child better
562
        mangle_tree.set_executability(True, test_too_much)
563
        mangle_tree.apply()
564
565
    def test_both_rename3(self):
566
        create_tree,root = self.get_transform()
567
        tests = create_tree.new_directory('tests', root, 'tests-id')
3943.8.1 by Marius Kruger
remove all trailing whitespace from bzr source
568
        create_tree.new_file('test_too_much.py', tests, 'hello1',
1534.7.150 by Aaron Bentley
Handled simultaneous renames of parent and child better
569
                             'test_too_much-id')
3943.8.1 by Marius Kruger
remove all trailing whitespace from bzr source
570
        create_tree.apply()
1534.7.150 by Aaron Bentley
Handled simultaneous renames of parent and child better
571
        mangle_tree,root = self.get_transform()
1534.7.181 by Aaron Bentley
Renamed a bunch of functions
572
        tests = mangle_tree.trans_id_tree_file_id('tests-id')
573
        test_too_much = mangle_tree.trans_id_tree_file_id('test_too_much-id')
1534.7.150 by Aaron Bentley
Handled simultaneous renames of parent and child better
574
        mangle_tree.adjust_path('selftest', root, tests)
3943.8.1 by Marius Kruger
remove all trailing whitespace from bzr source
575
        mangle_tree.adjust_path('blackbox.py', tests, test_too_much)
1534.7.150 by Aaron Bentley
Handled simultaneous renames of parent and child better
576
        mangle_tree.set_executability(True, test_too_much)
577
        mangle_tree.apply()
578
1534.7.48 by Aaron Bentley
Ensured we can move/rename dangling inventory entries
579
    def test_move_dangling_ie(self):
1534.7.59 by Aaron Bentley
Simplified tests
580
        create_tree, root = self.get_transform()
581
        # prepare tree
1731.1.33 by Aaron Bentley
Revert no-special-root changes
582
        root = create_tree.root
1534.7.59 by Aaron Bentley
Simplified tests
583
        create_tree.new_file('name1', root, 'hello1', 'name1')
584
        create_tree.apply()
585
        delete_contents, root = self.get_transform()
1534.7.181 by Aaron Bentley
Renamed a bunch of functions
586
        file = delete_contents.trans_id_tree_file_id('name1')
1534.7.59 by Aaron Bentley
Simplified tests
587
        delete_contents.delete_contents(file)
588
        delete_contents.apply()
589
        move_id, root = self.get_transform()
1534.7.181 by Aaron Bentley
Renamed a bunch of functions
590
        name1 = move_id.trans_id_tree_file_id('name1')
1534.7.59 by Aaron Bentley
Simplified tests
591
        newdir = move_id.new_directory('dir', root, 'newdir')
592
        move_id.adjust_path('name2', newdir, name1)
593
        move_id.apply()
3943.8.1 by Marius Kruger
remove all trailing whitespace from bzr source
594
1534.7.50 by Aaron Bentley
Detect duplicate inventory ids
595
    def test_replace_dangling_ie(self):
1534.7.59 by Aaron Bentley
Simplified tests
596
        create_tree, root = self.get_transform()
597
        # prepare tree
1731.1.33 by Aaron Bentley
Revert no-special-root changes
598
        root = create_tree.root
1534.7.59 by Aaron Bentley
Simplified tests
599
        create_tree.new_file('name1', root, 'hello1', 'name1')
600
        create_tree.apply()
601
        delete_contents = TreeTransform(self.wt)
602
        self.addCleanup(delete_contents.finalize)
1534.7.181 by Aaron Bentley
Renamed a bunch of functions
603
        file = delete_contents.trans_id_tree_file_id('name1')
1534.7.59 by Aaron Bentley
Simplified tests
604
        delete_contents.delete_contents(file)
605
        delete_contents.apply()
606
        delete_contents.finalize()
607
        replace = TreeTransform(self.wt)
608
        self.addCleanup(replace.finalize)
609
        name2 = replace.new_file('name2', root, 'hello2', 'name1')
610
        conflicts = replace.find_conflicts()
1534.7.181 by Aaron Bentley
Renamed a bunch of functions
611
        name1 = replace.trans_id_tree_file_id('name1')
1534.7.59 by Aaron Bentley
Simplified tests
612
        self.assertEqual(conflicts, [('duplicate id', name1, name2)])
613
        resolve_conflicts(replace)
614
        replace.apply()
1534.7.48 by Aaron Bentley
Ensured we can move/rename dangling inventory entries
615
4241.14.17 by Vincent Ladeuil
Add more tests for unicode symlinks to test_transform.
616
    def _test_symlinks(self, link_name1,link_target1,
617
                       link_name2, link_target2):
618
619
        def ozpath(p): return 'oz/' + p
620
2949.5.1 by Alexander Belchenko
selftest: use SymlinkFeature instead of TestSkipped where appropriate
621
        self.requireFeature(SymlinkFeature)
4241.14.12 by Vincent Ladeuil
Far too many modifications for a single commit, need to restart.
622
        transform, root = self.get_transform()
1534.7.59 by Aaron Bentley
Simplified tests
623
        oz_id = transform.new_directory('oz', root, 'oz-id')
4241.14.17 by Vincent Ladeuil
Add more tests for unicode symlinks to test_transform.
624
        wizard = transform.new_symlink(link_name1, oz_id, link_target1,
1534.7.59 by Aaron Bentley
Simplified tests
625
                                       'wizard-id')
4241.14.17 by Vincent Ladeuil
Add more tests for unicode symlinks to test_transform.
626
        wiz_id = transform.create_path(link_name2, oz_id)
627
        transform.create_symlink(link_target2, wiz_id)
3943.8.1 by Marius Kruger
remove all trailing whitespace from bzr source
628
        transform.version_file('wiz-id2', wiz_id)
1534.7.71 by abentley
All tests pass under Windows
629
        transform.set_executability(True, wiz_id)
3943.8.1 by Marius Kruger
remove all trailing whitespace from bzr source
630
        self.assertEqual(transform.find_conflicts(),
1534.7.71 by abentley
All tests pass under Windows
631
                         [('non-file executability', wiz_id)])
632
        transform.set_executability(None, wiz_id)
1534.7.59 by Aaron Bentley
Simplified tests
633
        transform.apply()
4241.14.17 by Vincent Ladeuil
Add more tests for unicode symlinks to test_transform.
634
        self.assertEqual(self.wt.path2id(ozpath(link_name1)), 'wizard-id')
635
        self.assertEqual('symlink',
636
                         file_kind(self.wt.abspath(ozpath(link_name1))))
637
        self.assertEqual(link_target2,
638
                         osutils.readlink(self.wt.abspath(ozpath(link_name2))))
639
        self.assertEqual(link_target1,
640
                         osutils.readlink(self.wt.abspath(ozpath(link_name1))))
641
642
    def test_symlinks(self):
643
        self._test_symlinks('wizard', 'wizard-target',
644
                            'wizard2', 'behind_curtain')
645
646
    def test_symlinks_unicode(self):
647
        self.requireFeature(tests.UnicodeFilenameFeature)
648
        self._test_symlinks(u'\N{Euro Sign}wizard',
649
                            u'wizard-targ\N{Euro Sign}t',
650
                            u'\N{Euro Sign}wizard2',
651
                            u'b\N{Euro Sign}hind_curtain')
1534.7.60 by Aaron Bentley
Tested existing conflict resolution functionality
652
3006.2.2 by Alexander Belchenko
tests added.
653
    def test_unable_create_symlink(self):
654
        def tt_helper():
655
            wt = self.make_branch_and_tree('.')
656
            tt = TreeTransform(wt)  # TreeTransform obtains write lock
657
            try:
658
                tt.new_symlink('foo', tt.root, 'bar')
659
                tt.apply()
660
            finally:
661
                wt.unlock()
662
        os_symlink = getattr(os, 'symlink', None)
663
        os.symlink = None
664
        try:
665
            err = self.assertRaises(errors.UnableCreateSymlink, tt_helper)
666
            self.assertEquals(
667
                "Unable to create symlink 'foo' on this platform",
668
                str(err))
669
        finally:
670
            if os_symlink:
671
                os.symlink = os_symlink
672
1534.7.170 by Aaron Bentley
Cleaned up filesystem conflict handling
673
    def get_conflicted(self):
1534.7.60 by Aaron Bentley
Tested existing conflict resolution functionality
674
        create,root = self.get_transform()
675
        create.new_file('dorothy', root, 'dorothy', 'dorothy-id')
1534.7.61 by Aaron Bentley
Handled parent loops, missing parents, unversioned parents
676
        oz = create.new_directory('oz', root, 'oz-id')
677
        create.new_directory('emeraldcity', oz, 'emerald-id')
1534.7.60 by Aaron Bentley
Tested existing conflict resolution functionality
678
        create.apply()
679
        conflicts,root = self.get_transform()
1534.7.65 by Aaron Bentley
Text cleaup/docs
680
        # set up duplicate entry, duplicate id
3943.8.1 by Marius Kruger
remove all trailing whitespace from bzr source
681
        new_dorothy = conflicts.new_file('dorothy', root, 'dorothy',
1534.7.60 by Aaron Bentley
Tested existing conflict resolution functionality
682
                                         'dorothy-id')
1534.7.181 by Aaron Bentley
Renamed a bunch of functions
683
        old_dorothy = conflicts.trans_id_tree_file_id('dorothy-id')
684
        oz = conflicts.trans_id_tree_file_id('oz-id')
1551.8.22 by Aaron Bentley
Improve message when OTHER deletes an in-use tree
685
        # set up DeletedParent parent conflict
1534.7.65 by Aaron Bentley
Text cleaup/docs
686
        conflicts.delete_versioned(oz)
1534.7.181 by Aaron Bentley
Renamed a bunch of functions
687
        emerald = conflicts.trans_id_tree_file_id('emerald-id')
1551.8.22 by Aaron Bentley
Improve message when OTHER deletes an in-use tree
688
        # set up MissingParent conflict
689
        munchkincity = conflicts.trans_id_file_id('munchkincity-id')
690
        conflicts.adjust_path('munchkincity', root, munchkincity)
691
        conflicts.new_directory('auntem', munchkincity, 'auntem-id')
1534.7.65 by Aaron Bentley
Text cleaup/docs
692
        # set up parent loop
1534.7.61 by Aaron Bentley
Handled parent loops, missing parents, unversioned parents
693
        conflicts.adjust_path('emeraldcity', emerald, emerald)
1534.7.170 by Aaron Bentley
Cleaned up filesystem conflict handling
694
        return conflicts, emerald, oz, old_dorothy, new_dorothy
695
696
    def test_conflict_resolution(self):
697
        conflicts, emerald, oz, old_dorothy, new_dorothy =\
698
            self.get_conflicted()
1534.7.60 by Aaron Bentley
Tested existing conflict resolution functionality
699
        resolve_conflicts(conflicts)
700
        self.assertEqual(conflicts.final_name(old_dorothy), 'dorothy.moved')
701
        self.assertIs(conflicts.final_file_id(old_dorothy), None)
702
        self.assertEqual(conflicts.final_name(new_dorothy), 'dorothy')
1534.7.170 by Aaron Bentley
Cleaned up filesystem conflict handling
703
        self.assertEqual(conflicts.final_file_id(new_dorothy), 'dorothy-id')
1534.7.64 by Aaron Bentley
Extra testing
704
        self.assertEqual(conflicts.final_parent(emerald), oz)
1534.7.63 by Aaron Bentley
Ensure transform can be applied after resolution
705
        conflicts.apply()
1534.7.62 by Aaron Bentley
Fixed moving versioned directories
706
1534.7.170 by Aaron Bentley
Cleaned up filesystem conflict handling
707
    def test_cook_conflicts(self):
708
        tt, emerald, oz, old_dorothy, new_dorothy = self.get_conflicted()
709
        raw_conflicts = resolve_conflicts(tt)
710
        cooked_conflicts = cook_conflicts(raw_conflicts, tt)
3943.8.1 by Marius Kruger
remove all trailing whitespace from bzr source
711
        duplicate = DuplicateEntry('Moved existing file to', 'dorothy.moved',
1534.10.20 by Aaron Bentley
Got all tests passing
712
                                   'dorothy', None, 'dorothy-id')
1534.7.170 by Aaron Bentley
Cleaned up filesystem conflict handling
713
        self.assertEqual(cooked_conflicts[0], duplicate)
3943.8.1 by Marius Kruger
remove all trailing whitespace from bzr source
714
        duplicate_id = DuplicateID('Unversioned existing file',
1534.10.20 by Aaron Bentley
Got all tests passing
715
                                   'dorothy.moved', 'dorothy', None,
716
                                   'dorothy-id')
1534.7.170 by Aaron Bentley
Cleaned up filesystem conflict handling
717
        self.assertEqual(cooked_conflicts[1], duplicate_id)
1551.8.22 by Aaron Bentley
Improve message when OTHER deletes an in-use tree
718
        missing_parent = MissingParent('Created directory', 'munchkincity',
719
                                       'munchkincity-id')
720
        deleted_parent = DeletingParent('Not deleting', 'oz', 'oz-id')
1534.7.170 by Aaron Bentley
Cleaned up filesystem conflict handling
721
        self.assertEqual(cooked_conflicts[2], missing_parent)
1551.8.22 by Aaron Bentley
Improve message when OTHER deletes an in-use tree
722
        unversioned_parent = UnversionedParent('Versioned directory',
723
                                               'munchkincity',
724
                                               'munchkincity-id')
725
        unversioned_parent2 = UnversionedParent('Versioned directory', 'oz',
1534.10.20 by Aaron Bentley
Got all tests passing
726
                                               'oz-id')
1534.7.170 by Aaron Bentley
Cleaned up filesystem conflict handling
727
        self.assertEqual(cooked_conflicts[3], unversioned_parent)
3943.8.1 by Marius Kruger
remove all trailing whitespace from bzr source
728
        parent_loop = ParentLoop('Cancelled move', 'oz/emeraldcity',
1534.10.20 by Aaron Bentley
Got all tests passing
729
                                 'oz/emeraldcity', 'emerald-id', 'emerald-id')
1551.8.22 by Aaron Bentley
Improve message when OTHER deletes an in-use tree
730
        self.assertEqual(cooked_conflicts[4], deleted_parent)
731
        self.assertEqual(cooked_conflicts[5], unversioned_parent2)
732
        self.assertEqual(cooked_conflicts[6], parent_loop)
733
        self.assertEqual(len(cooked_conflicts), 7)
1534.7.170 by Aaron Bentley
Cleaned up filesystem conflict handling
734
        tt.finalize()
735
736
    def test_string_conflicts(self):
737
        tt, emerald, oz, old_dorothy, new_dorothy = self.get_conflicted()
738
        raw_conflicts = resolve_conflicts(tt)
739
        cooked_conflicts = cook_conflicts(raw_conflicts, tt)
740
        tt.finalize()
1534.10.24 by Aaron Bentley
Eliminated conflicts_to_strings, made remove_files a ConflictList member
741
        conflicts_s = [str(c) for c in cooked_conflicts]
1534.7.171 by Aaron Bentley
Implemented stringifying filesystem conflicts
742
        self.assertEqual(len(cooked_conflicts), len(conflicts_s))
743
        self.assertEqual(conflicts_s[0], 'Conflict adding file dorothy.  '
744
                                         'Moved existing file to '
745
                                         'dorothy.moved.')
746
        self.assertEqual(conflicts_s[1], 'Conflict adding id to dorothy.  '
747
                                         'Unversioned existing file '
748
                                         'dorothy.moved.')
1551.8.22 by Aaron Bentley
Improve message when OTHER deletes an in-use tree
749
        self.assertEqual(conflicts_s[2], 'Conflict adding files to'
750
                                         ' munchkincity.  Created directory.')
751
        self.assertEqual(conflicts_s[3], 'Conflict because munchkincity is not'
752
                                         ' versioned, but has versioned'
753
                                         ' children.  Versioned directory.')
1551.8.23 by Aaron Bentley
Tweaked conflict message to be more understandable
754
        self.assertEqualDiff(conflicts_s[4], "Conflict: can't delete oz because it"
755
                                         " is not empty.  Not deleting.")
1551.8.22 by Aaron Bentley
Improve message when OTHER deletes an in-use tree
756
        self.assertEqual(conflicts_s[5], 'Conflict because oz is not'
757
                                         ' versioned, but has versioned'
758
                                         ' children.  Versioned directory.')
759
        self.assertEqual(conflicts_s[6], 'Conflict moving oz/emeraldcity into'
4597.8.11 by Vincent Ladeuil
The ParentLoop.format has been updated, fix fallouts.
760
                                         ' oz/emeraldcity. Cancelled move.')
1534.7.170 by Aaron Bentley
Cleaned up filesystem conflict handling
761
3144.4.2 by Aaron Bentley
Handle non-directory parent conflicts (abentley, #177390)
762
    def prepare_wrong_parent_kind(self):
763
        tt, root = self.get_transform()
764
        tt.new_file('parent', root, 'contents', 'parent-id')
765
        tt.apply()
766
        tt, root = self.get_transform()
767
        parent_id = tt.trans_id_file_id('parent-id')
768
        tt.new_file('child,', parent_id, 'contents2', 'file-id')
769
        return tt
770
3144.4.1 by Aaron Bentley
Handle trying to list parents of a non-directory
771
    def test_find_conflicts_wrong_parent_kind(self):
3144.4.2 by Aaron Bentley
Handle non-directory parent conflicts (abentley, #177390)
772
        tt = self.prepare_wrong_parent_kind()
3144.4.1 by Aaron Bentley
Handle trying to list parents of a non-directory
773
        tt.find_conflicts()
774
3144.4.2 by Aaron Bentley
Handle non-directory parent conflicts (abentley, #177390)
775
    def test_resolve_conflicts_wrong_existing_parent_kind(self):
776
        tt = self.prepare_wrong_parent_kind()
777
        raw_conflicts = resolve_conflicts(tt)
778
        self.assertEqual(set([('non-directory parent', 'Created directory',
779
                         'new-3')]), raw_conflicts)
780
        cooked_conflicts = cook_conflicts(raw_conflicts, tt)
781
        self.assertEqual([NonDirectoryParent('Created directory', 'parent.new',
782
        'parent-id')], cooked_conflicts)
783
        tt.apply()
784
        self.assertEqual(None, self.wt.path2id('parent'))
785
        self.assertEqual('parent-id', self.wt.path2id('parent.new'))
786
787
    def test_resolve_conflicts_wrong_new_parent_kind(self):
788
        tt, root = self.get_transform()
789
        parent_id = tt.new_directory('parent', root, 'parent-id')
790
        tt.new_file('child,', parent_id, 'contents2', 'file-id')
791
        tt.apply()
792
        tt, root = self.get_transform()
793
        parent_id = tt.trans_id_file_id('parent-id')
794
        tt.delete_contents(parent_id)
795
        tt.create_file('contents', parent_id)
796
        raw_conflicts = resolve_conflicts(tt)
797
        self.assertEqual(set([('non-directory parent', 'Created directory',
798
                         'new-3')]), raw_conflicts)
799
        tt.apply()
800
        self.assertEqual(None, self.wt.path2id('parent'))
801
        self.assertEqual('parent-id', self.wt.path2id('parent.new'))
802
803
    def test_resolve_conflicts_wrong_parent_kind_unversioned(self):
804
        tt, root = self.get_transform()
805
        parent_id = tt.new_directory('parent', root)
806
        tt.new_file('child,', parent_id, 'contents2')
807
        tt.apply()
808
        tt, root = self.get_transform()
809
        parent_id = tt.trans_id_tree_path('parent')
810
        tt.delete_contents(parent_id)
811
        tt.create_file('contents', parent_id)
812
        resolve_conflicts(tt)
813
        tt.apply()
814
        self.assertIs(None, self.wt.path2id('parent'))
815
        self.assertIs(None, self.wt.path2id('parent.new'))
816
5409.5.1 by Vincent Ladeuil
Tweak test_resolve_conflicts_missing_parent (renamed from test_resolve_no_parent), puttinh it in the right place.
817
    def test_resolve_conflicts_missing_parent(self):
818
        wt = self.make_branch_and_tree('.')
819
        tt = TreeTransform(wt)
820
        self.addCleanup(tt.finalize)
821
        parent = tt.trans_id_file_id('parent-id')
822
        tt.new_file('file', parent, 'Contents')
823
        raw_conflicts = resolve_conflicts(tt)
5409.6.1 by Vincent Ladeuil
Clarify test intent and behaviour.
824
        # Since the directory doesn't exist it's seen as missing to resolve
825
        # create a conflict asking for it to be created.
5409.5.1 by Vincent Ladeuil
Tweak test_resolve_conflicts_missing_parent (renamed from test_resolve_no_parent), puttinh it in the right place.
826
        self.assertLength(1, raw_conflicts)
827
        self.assertEqual(('missing parent', 'Created directory', 'new-1'),
828
                         raw_conflicts.pop())
5409.6.1 by Vincent Ladeuil
Clarify test intent and behaviour.
829
        # apply fail since the missing directory doesn't exist
830
        self.assertRaises(errors.NoFinalPath, tt.apply)
5409.5.1 by Vincent Ladeuil
Tweak test_resolve_conflicts_missing_parent (renamed from test_resolve_no_parent), puttinh it in the right place.
831
1534.7.62 by Aaron Bentley
Fixed moving versioned directories
832
    def test_moving_versioned_directories(self):
833
        create, root = self.get_transform()
834
        kansas = create.new_directory('kansas', root, 'kansas-id')
835
        create.new_directory('house', kansas, 'house-id')
836
        create.new_directory('oz', root, 'oz-id')
837
        create.apply()
838
        cyclone, root = self.get_transform()
1534.7.181 by Aaron Bentley
Renamed a bunch of functions
839
        oz = cyclone.trans_id_tree_file_id('oz-id')
840
        house = cyclone.trans_id_tree_file_id('house-id')
1534.7.62 by Aaron Bentley
Fixed moving versioned directories
841
        cyclone.adjust_path('house', oz, house)
842
        cyclone.apply()
1534.7.66 by Aaron Bentley
Ensured we don't accidentally move the root directory
843
844
    def test_moving_root(self):
1534.7.68 by Aaron Bentley
Got semi-reasonable root directory renaming working
845
        create, root = self.get_transform()
846
        fun = create.new_directory('fun', root, 'fun-id')
847
        create.new_directory('sun', root, 'sun-id')
848
        create.new_directory('moon', root, 'moon')
849
        create.apply()
1534.7.66 by Aaron Bentley
Ensured we don't accidentally move the root directory
850
        transform, root = self.get_transform()
1534.7.68 by Aaron Bentley
Got semi-reasonable root directory renaming working
851
        transform.adjust_root_path('oldroot', fun)
4634.122.2 by Aaron Bentley, John Arbash Meinel
Bring the fixup_new_roots code from the nested-trees code.
852
        new_root = transform.trans_id_tree_path('')
1534.7.69 by Aaron Bentley
Got real root moves working
853
        transform.version_file('new-root', new_root)
1534.7.68 by Aaron Bentley
Got semi-reasonable root directory renaming working
854
        transform.apply()
1534.7.93 by Aaron Bentley
Added text merge test
855
1534.7.114 by Aaron Bentley
Added file renaming test case
856
    def test_renames(self):
857
        create, root = self.get_transform()
858
        old = create.new_directory('old-parent', root, 'old-id')
859
        intermediate = create.new_directory('intermediate', old, 'im-id')
860
        myfile = create.new_file('myfile', intermediate, 'myfile-text',
861
                                 'myfile-id')
862
        create.apply()
863
        rename, root = self.get_transform()
1534.7.181 by Aaron Bentley
Renamed a bunch of functions
864
        old = rename.trans_id_file_id('old-id')
1534.7.114 by Aaron Bentley
Added file renaming test case
865
        rename.adjust_path('new', root, old)
1534.7.181 by Aaron Bentley
Renamed a bunch of functions
866
        myfile = rename.trans_id_file_id('myfile-id')
1534.7.114 by Aaron Bentley
Added file renaming test case
867
        rename.set_executability(True, myfile)
868
        rename.apply()
869
5186.2.4 by Martin Pool
Add failing specific test for 491763
870
    def test_rename_fails(self):
871
        # see https://bugs.launchpad.net/bzr/+bug/491763
872
        create, root_id = self.get_transform()
873
        first_dir = create.new_directory('first-dir', root_id, 'first-id')
874
        myfile = create.new_file('myfile', root_id, 'myfile-text',
875
                                 'myfile-id')
876
        create.apply()
5050.15.2 by Martin
Correct bt.test_transform.TestTreeTransform.test_rename_fails on non-posix platforms
877
        if os.name == "posix" and sys.platform != "cygwin":
878
            # posix filesystems fail on renaming if the readonly bit is set
879
            osutils.make_readonly(self.wt.abspath('first-dir'))
880
        elif os.name == "nt":
881
            # windows filesystems fail on renaming open files
882
            self.addCleanup(file(self.wt.abspath('myfile')).close)
883
        else:
884
            self.skip("Don't know how to force a permissions error on rename")
5186.2.4 by Martin Pool
Add failing specific test for 491763
885
        # now transform to rename
886
        rename_transform, root_id = self.get_transform()
887
        file_trans_id = rename_transform.trans_id_file_id('myfile-id')
888
        dir_id = rename_transform.trans_id_file_id('first-id')
889
        rename_transform.adjust_path('newname', dir_id, file_trans_id)
5186.2.5 by Martin Pool
Raise a specific clearer error when a rename fails inside transform
890
        e = self.assertRaises(errors.TransformRenameFailed,
891
            rename_transform.apply)
5050.15.2 by Martin
Correct bt.test_transform.TestTreeTransform.test_rename_fails on non-posix platforms
892
        # On nix looks like: 
5186.2.5 by Martin Pool
Raise a specific clearer error when a rename fails inside transform
893
        # "Failed to rename .../work/.bzr/checkout/limbo/new-1
894
        # to .../first-dir/newname: [Errno 13] Permission denied"
5050.15.2 by Martin
Correct bt.test_transform.TestTreeTransform.test_rename_fails on non-posix platforms
895
        # On windows looks like:
896
        # "Failed to rename .../work/myfile to 
897
        # .../work/.bzr/checkout/limbo/new-1: [Errno 13] Permission denied"
898
        # The strerror will vary per OS and language so it's not checked here
5186.2.5 by Martin Pool
Raise a specific clearer error when a rename fails inside transform
899
        self.assertContainsRe(str(e),
5050.15.2 by Martin
Correct bt.test_transform.TestTreeTransform.test_rename_fails on non-posix platforms
900
            "Failed to rename .*(first-dir.newname:|myfile)")
5186.2.4 by Martin Pool
Add failing specific test for 491763
901
1740.2.4 by Aaron Bentley
Update transform tests and docs
902
    def test_set_executability_order(self):
903
        """Ensure that executability behaves the same, no matter what order.
3943.8.1 by Marius Kruger
remove all trailing whitespace from bzr source
904
1740.2.4 by Aaron Bentley
Update transform tests and docs
905
        - create file and set executability simultaneously
906
        - create file and set executability afterward
907
        - unsetting the executability of a file whose executability has not been
908
        declared should throw an exception (this may happen when a
909
        merge attempts to create a file with a duplicate ID)
910
        """
911
        transform, root = self.get_transform()
912
        wt = transform._tree
3034.2.1 by Aaron Bentley
Fix is_executable tests for win32
913
        wt.lock_read()
914
        self.addCleanup(wt.unlock)
1740.2.4 by Aaron Bentley
Update transform tests and docs
915
        transform.new_file('set_on_creation', root, 'Set on creation', 'soc',
916
                           True)
2502.1.1 by Aaron Bentley
Ensure renames only root children are renamed when building trees
917
        sac = transform.new_file('set_after_creation', root,
918
                                 'Set after creation', 'sac')
1740.2.4 by Aaron Bentley
Update transform tests and docs
919
        transform.set_executability(True, sac)
2502.1.1 by Aaron Bentley
Ensure renames only root children are renamed when building trees
920
        uws = transform.new_file('unset_without_set', root, 'Unset badly',
921
                                 'uws')
1740.2.4 by Aaron Bentley
Update transform tests and docs
922
        self.assertRaises(KeyError, transform.set_executability, None, uws)
923
        transform.apply()
924
        self.assertTrue(wt.is_executable('soc'))
925
        self.assertTrue(wt.is_executable('sac'))
926
1534.12.2 by Aaron Bentley
Added test for preserving file mode
927
    def test_preserve_mode(self):
928
        """File mode is preserved when replacing content"""
929
        if sys.platform == 'win32':
930
            raise TestSkipped('chmod has no effect on win32')
931
        transform, root = self.get_transform()
932
        transform.new_file('file1', root, 'contents', 'file1-id', True)
933
        transform.apply()
3146.4.12 by Aaron Bentley
Add needed write lock to test
934
        self.wt.lock_write()
935
        self.addCleanup(self.wt.unlock)
1534.12.2 by Aaron Bentley
Added test for preserving file mode
936
        self.assertTrue(self.wt.is_executable('file1-id'))
937
        transform, root = self.get_transform()
938
        file1_id = transform.trans_id_tree_file_id('file1-id')
939
        transform.delete_contents(file1_id)
940
        transform.create_file('contents2', file1_id)
941
        transform.apply()
942
        self.assertTrue(self.wt.is_executable('file1-id'))
943
2027.1.1 by John Arbash Meinel
Fix bug #56549, and write a direct test that the right path is being statted
944
    def test__set_mode_stats_correctly(self):
945
        """_set_mode stats to determine file mode."""
946
        if sys.platform == 'win32':
947
            raise TestSkipped('chmod has no effect on win32')
948
949
        stat_paths = []
950
        real_stat = os.stat
951
        def instrumented_stat(path):
952
            stat_paths.append(path)
953
            return real_stat(path)
954
955
        transform, root = self.get_transform()
956
957
        bar1_id = transform.new_file('bar', root, 'bar contents 1\n',
958
                                     file_id='bar-id-1', executable=False)
959
        transform.apply()
960
961
        transform, root = self.get_transform()
962
        bar1_id = transform.trans_id_tree_path('bar')
963
        bar2_id = transform.trans_id_tree_path('bar2')
964
        try:
965
            os.stat = instrumented_stat
966
            transform.create_file('bar2 contents\n', bar2_id, mode_id=bar1_id)
967
        finally:
968
            os.stat = real_stat
969
            transform.finalize()
970
971
        bar1_abspath = self.wt.abspath('bar')
972
        self.assertEqual([bar1_abspath], stat_paths)
973
1551.11.1 by Aaron Bentley
Initial work on converting TreeTransform to iter_changes format
974
    def test_iter_changes(self):
2100.3.33 by Aaron Bentley
Handle unique roots in tests for TreeTransform.iter_changes
975
        self.wt.set_root_id('eert_toor')
1551.11.1 by Aaron Bentley
Initial work on converting TreeTransform to iter_changes format
976
        transform, root = self.get_transform()
977
        transform.new_file('old', root, 'blah', 'id-1', True)
978
        transform.apply()
979
        transform, root = self.get_transform()
980
        try:
3254.1.1 by Aaron Bentley
Make Tree.iter_changes a public method
981
            self.assertEqual([], list(transform.iter_changes()))
1551.11.1 by Aaron Bentley
Initial work on converting TreeTransform to iter_changes format
982
            old = transform.trans_id_tree_file_id('id-1')
983
            transform.unversion_file(old)
2255.7.96 by Robert Collins
Change _iter_changes interface to yield both old and new paths.
984
            self.assertEqual([('id-1', ('old', None), False, (True, False),
2100.3.33 by Aaron Bentley
Handle unique roots in tests for TreeTransform.iter_changes
985
                ('eert_toor', 'eert_toor'), ('old', 'old'), ('file', 'file'),
3254.1.1 by Aaron Bentley
Make Tree.iter_changes a public method
986
                (True, True))], list(transform.iter_changes()))
1551.11.1 by Aaron Bentley
Initial work on converting TreeTransform to iter_changes format
987
            transform.new_directory('new', root, 'id-1')
2255.7.96 by Robert Collins
Change _iter_changes interface to yield both old and new paths.
988
            self.assertEqual([('id-1', ('old', 'new'), True, (True, True),
2100.3.33 by Aaron Bentley
Handle unique roots in tests for TreeTransform.iter_changes
989
                ('eert_toor', 'eert_toor'), ('old', 'new'),
1551.11.1 by Aaron Bentley
Initial work on converting TreeTransform to iter_changes format
990
                ('file', 'directory'),
3254.1.1 by Aaron Bentley
Make Tree.iter_changes a public method
991
                (True, False))], list(transform.iter_changes()))
1551.11.1 by Aaron Bentley
Initial work on converting TreeTransform to iter_changes format
992
        finally:
993
            transform.finalize()
994
995
    def test_iter_changes_new(self):
2100.3.33 by Aaron Bentley
Handle unique roots in tests for TreeTransform.iter_changes
996
        self.wt.set_root_id('eert_toor')
1551.11.1 by Aaron Bentley
Initial work on converting TreeTransform to iter_changes format
997
        transform, root = self.get_transform()
998
        transform.new_file('old', root, 'blah')
999
        transform.apply()
1000
        transform, root = self.get_transform()
1001
        try:
1002
            old = transform.trans_id_tree_path('old')
1003
            transform.version_file('id-1', old)
2255.7.96 by Robert Collins
Change _iter_changes interface to yield both old and new paths.
1004
            self.assertEqual([('id-1', (None, 'old'), False, (False, True),
2100.3.33 by Aaron Bentley
Handle unique roots in tests for TreeTransform.iter_changes
1005
                ('eert_toor', 'eert_toor'), ('old', 'old'), ('file', 'file'),
3254.1.1 by Aaron Bentley
Make Tree.iter_changes a public method
1006
                (False, False))], list(transform.iter_changes()))
1551.11.1 by Aaron Bentley
Initial work on converting TreeTransform to iter_changes format
1007
        finally:
1008
            transform.finalize()
1009
1551.11.2 by Aaron Bentley
Get kind change detection working for iter_changes
1010
    def test_iter_changes_modifications(self):
2100.3.33 by Aaron Bentley
Handle unique roots in tests for TreeTransform.iter_changes
1011
        self.wt.set_root_id('eert_toor')
1551.11.2 by Aaron Bentley
Get kind change detection working for iter_changes
1012
        transform, root = self.get_transform()
1013
        transform.new_file('old', root, 'blah', 'id-1')
1014
        transform.new_file('new', root, 'blah')
1015
        transform.new_directory('subdir', root, 'subdir-id')
1016
        transform.apply()
1017
        transform, root = self.get_transform()
1018
        try:
1019
            old = transform.trans_id_tree_path('old')
1020
            subdir = transform.trans_id_tree_file_id('subdir-id')
1021
            new = transform.trans_id_tree_path('new')
3254.1.1 by Aaron Bentley
Make Tree.iter_changes a public method
1022
            self.assertEqual([], list(transform.iter_changes()))
1551.11.2 by Aaron Bentley
Get kind change detection working for iter_changes
1023
1024
            #content deletion
1025
            transform.delete_contents(old)
2255.7.96 by Robert Collins
Change _iter_changes interface to yield both old and new paths.
1026
            self.assertEqual([('id-1', ('old', 'old'), True, (True, True),
2100.3.33 by Aaron Bentley
Handle unique roots in tests for TreeTransform.iter_changes
1027
                ('eert_toor', 'eert_toor'), ('old', 'old'), ('file', None),
3254.1.1 by Aaron Bentley
Make Tree.iter_changes a public method
1028
                (False, False))], list(transform.iter_changes()))
1551.11.2 by Aaron Bentley
Get kind change detection working for iter_changes
1029
1030
            #content change
1031
            transform.create_file('blah', old)
2255.7.96 by Robert Collins
Change _iter_changes interface to yield both old and new paths.
1032
            self.assertEqual([('id-1', ('old', 'old'), True, (True, True),
2100.3.33 by Aaron Bentley
Handle unique roots in tests for TreeTransform.iter_changes
1033
                ('eert_toor', 'eert_toor'), ('old', 'old'), ('file', 'file'),
3254.1.1 by Aaron Bentley
Make Tree.iter_changes a public method
1034
                (False, False))], list(transform.iter_changes()))
1551.11.2 by Aaron Bentley
Get kind change detection working for iter_changes
1035
            transform.cancel_deletion(old)
2255.7.96 by Robert Collins
Change _iter_changes interface to yield both old and new paths.
1036
            self.assertEqual([('id-1', ('old', 'old'), True, (True, True),
2100.3.33 by Aaron Bentley
Handle unique roots in tests for TreeTransform.iter_changes
1037
                ('eert_toor', 'eert_toor'), ('old', 'old'), ('file', 'file'),
3254.1.1 by Aaron Bentley
Make Tree.iter_changes a public method
1038
                (False, False))], list(transform.iter_changes()))
1551.11.2 by Aaron Bentley
Get kind change detection working for iter_changes
1039
            transform.cancel_creation(old)
1040
1041
            # move file_id to a different file
3254.1.1 by Aaron Bentley
Make Tree.iter_changes a public method
1042
            self.assertEqual([], list(transform.iter_changes()))
1551.11.2 by Aaron Bentley
Get kind change detection working for iter_changes
1043
            transform.unversion_file(old)
1044
            transform.version_file('id-1', new)
1045
            transform.adjust_path('old', root, new)
2255.7.96 by Robert Collins
Change _iter_changes interface to yield both old and new paths.
1046
            self.assertEqual([('id-1', ('old', 'old'), True, (True, True),
2100.3.33 by Aaron Bentley
Handle unique roots in tests for TreeTransform.iter_changes
1047
                ('eert_toor', 'eert_toor'), ('old', 'old'), ('file', 'file'),
3254.1.1 by Aaron Bentley
Make Tree.iter_changes a public method
1048
                (False, False))], list(transform.iter_changes()))
1551.11.2 by Aaron Bentley
Get kind change detection working for iter_changes
1049
            transform.cancel_versioning(new)
1050
            transform._removed_id = set()
1051
1052
            #execute bit
3254.1.1 by Aaron Bentley
Make Tree.iter_changes a public method
1053
            self.assertEqual([], list(transform.iter_changes()))
1551.11.2 by Aaron Bentley
Get kind change detection working for iter_changes
1054
            transform.set_executability(True, old)
2255.7.96 by Robert Collins
Change _iter_changes interface to yield both old and new paths.
1055
            self.assertEqual([('id-1', ('old', 'old'), False, (True, True),
2100.3.33 by Aaron Bentley
Handle unique roots in tests for TreeTransform.iter_changes
1056
                ('eert_toor', 'eert_toor'), ('old', 'old'), ('file', 'file'),
3254.1.1 by Aaron Bentley
Make Tree.iter_changes a public method
1057
                (False, True))], list(transform.iter_changes()))
1551.11.2 by Aaron Bentley
Get kind change detection working for iter_changes
1058
            transform.set_executability(None, old)
1059
1060
            # filename
3254.1.1 by Aaron Bentley
Make Tree.iter_changes a public method
1061
            self.assertEqual([], list(transform.iter_changes()))
1551.11.2 by Aaron Bentley
Get kind change detection working for iter_changes
1062
            transform.adjust_path('new', root, old)
1063
            transform._new_parent = {}
2255.7.96 by Robert Collins
Change _iter_changes interface to yield both old and new paths.
1064
            self.assertEqual([('id-1', ('old', 'new'), False, (True, True),
2100.3.33 by Aaron Bentley
Handle unique roots in tests for TreeTransform.iter_changes
1065
                ('eert_toor', 'eert_toor'), ('old', 'new'), ('file', 'file'),
3254.1.1 by Aaron Bentley
Make Tree.iter_changes a public method
1066
                (False, False))], list(transform.iter_changes()))
1551.11.2 by Aaron Bentley
Get kind change detection working for iter_changes
1067
            transform._new_name = {}
1068
1069
            # parent directory
3254.1.1 by Aaron Bentley
Make Tree.iter_changes a public method
1070
            self.assertEqual([], list(transform.iter_changes()))
1551.11.2 by Aaron Bentley
Get kind change detection working for iter_changes
1071
            transform.adjust_path('new', subdir, old)
1072
            transform._new_name = {}
2255.7.96 by Robert Collins
Change _iter_changes interface to yield both old and new paths.
1073
            self.assertEqual([('id-1', ('old', 'subdir/old'), False,
2255.2.180 by Martin Pool
merge dirstate
1074
                (True, True), ('eert_toor', 'subdir-id'), ('old', 'old'),
2255.7.96 by Robert Collins
Change _iter_changes interface to yield both old and new paths.
1075
                ('file', 'file'), (False, False))],
3254.1.1 by Aaron Bentley
Make Tree.iter_changes a public method
1076
                list(transform.iter_changes()))
1551.11.2 by Aaron Bentley
Get kind change detection working for iter_changes
1077
            transform._new_path = {}
1078
1079
        finally:
1080
            transform.finalize()
1081
1551.11.7 by Aaron Bentley
Stop modified flag bleeding into later changes
1082
    def test_iter_changes_modified_bleed(self):
2100.3.33 by Aaron Bentley
Handle unique roots in tests for TreeTransform.iter_changes
1083
        self.wt.set_root_id('eert_toor')
1551.11.7 by Aaron Bentley
Stop modified flag bleeding into later changes
1084
        """Modified flag should not bleed from one change to another"""
1085
        # unfortunately, we have no guarantee that file1 (which is modified)
1086
        # will be applied before file2.  And if it's applied after file2, it
1087
        # obviously can't bleed into file2's change output.  But for now, it
1088
        # works.
1089
        transform, root = self.get_transform()
1090
        transform.new_file('file1', root, 'blah', 'id-1')
1091
        transform.new_file('file2', root, 'blah', 'id-2')
1092
        transform.apply()
1093
        transform, root = self.get_transform()
1094
        try:
1095
            transform.delete_contents(transform.trans_id_file_id('id-1'))
1096
            transform.set_executability(True,
1097
            transform.trans_id_file_id('id-2'))
2255.7.96 by Robert Collins
Change _iter_changes interface to yield both old and new paths.
1098
            self.assertEqual([('id-1', (u'file1', u'file1'), True, (True, True),
2100.3.33 by Aaron Bentley
Handle unique roots in tests for TreeTransform.iter_changes
1099
                ('eert_toor', 'eert_toor'), ('file1', u'file1'),
1551.11.7 by Aaron Bentley
Stop modified flag bleeding into later changes
1100
                ('file', None), (False, False)),
2255.7.96 by Robert Collins
Change _iter_changes interface to yield both old and new paths.
1101
                ('id-2', (u'file2', u'file2'), False, (True, True),
2100.3.33 by Aaron Bentley
Handle unique roots in tests for TreeTransform.iter_changes
1102
                ('eert_toor', 'eert_toor'), ('file2', u'file2'),
1551.11.7 by Aaron Bentley
Stop modified flag bleeding into later changes
1103
                ('file', 'file'), (False, True))],
3254.1.1 by Aaron Bentley
Make Tree.iter_changes a public method
1104
                list(transform.iter_changes()))
1551.11.7 by Aaron Bentley
Stop modified flag bleeding into later changes
1105
        finally:
1106
            transform.finalize()
1107
1551.10.37 by Aaron Bentley
recommit of TreeTransform._iter_changes fix with missing files
1108
    def test_iter_changes_move_missing(self):
1109
        """Test moving ids with no files around"""
1110
        self.wt.set_root_id('toor_eert')
1111
        # Need two steps because versioning a non-existant file is a conflict.
1112
        transform, root = self.get_transform()
1113
        transform.new_directory('floater', root, 'floater-id')
1114
        transform.apply()
1115
        transform, root = self.get_transform()
1116
        transform.delete_contents(transform.trans_id_tree_path('floater'))
1117
        transform.apply()
1118
        transform, root = self.get_transform()
1119
        floater = transform.trans_id_tree_path('floater')
1120
        try:
1121
            transform.adjust_path('flitter', root, floater)
1122
            self.assertEqual([('floater-id', ('floater', 'flitter'), False,
1123
            (True, True), ('toor_eert', 'toor_eert'), ('floater', 'flitter'),
3254.1.1 by Aaron Bentley
Make Tree.iter_changes a public method
1124
            (None, None), (False, False))], list(transform.iter_changes()))
1551.10.37 by Aaron Bentley
recommit of TreeTransform._iter_changes fix with missing files
1125
        finally:
1126
            transform.finalize()
1127
1551.11.2 by Aaron Bentley
Get kind change detection working for iter_changes
1128
    def test_iter_changes_pointless(self):
1129
        """Ensure that no-ops are not treated as modifications"""
2100.3.33 by Aaron Bentley
Handle unique roots in tests for TreeTransform.iter_changes
1130
        self.wt.set_root_id('eert_toor')
1551.11.2 by Aaron Bentley
Get kind change detection working for iter_changes
1131
        transform, root = self.get_transform()
1132
        transform.new_file('old', root, 'blah', 'id-1')
1133
        transform.new_directory('subdir', root, 'subdir-id')
1134
        transform.apply()
1135
        transform, root = self.get_transform()
1136
        try:
1137
            old = transform.trans_id_tree_path('old')
1138
            subdir = transform.trans_id_tree_file_id('subdir-id')
3254.1.1 by Aaron Bentley
Make Tree.iter_changes a public method
1139
            self.assertEqual([], list(transform.iter_changes()))
1551.11.2 by Aaron Bentley
Get kind change detection working for iter_changes
1140
            transform.delete_contents(subdir)
1141
            transform.create_directory(subdir)
1142
            transform.set_executability(False, old)
1143
            transform.unversion_file(old)
1144
            transform.version_file('id-1', old)
1145
            transform.adjust_path('old', root, old)
3254.1.1 by Aaron Bentley
Make Tree.iter_changes a public method
1146
            self.assertEqual([], list(transform.iter_changes()))
1551.11.2 by Aaron Bentley
Get kind change detection working for iter_changes
1147
        finally:
1148
            transform.finalize()
1534.7.93 by Aaron Bentley
Added text merge test
1149
2502.1.1 by Aaron Bentley
Ensure renames only root children are renamed when building trees
1150
    def test_rename_count(self):
1151
        transform, root = self.get_transform()
2502.1.8 by Aaron Bentley
Updates from review comments
1152
        transform.new_file('name1', root, 'contents')
2502.1.1 by Aaron Bentley
Ensure renames only root children are renamed when building trees
1153
        self.assertEqual(transform.rename_count, 0)
1154
        transform.apply()
1155
        self.assertEqual(transform.rename_count, 1)
1156
        transform2, root = self.get_transform()
2502.1.8 by Aaron Bentley
Updates from review comments
1157
        transform2.adjust_path('name2', root,
1158
                               transform2.trans_id_tree_path('name1'))
2502.1.1 by Aaron Bentley
Ensure renames only root children are renamed when building trees
1159
        self.assertEqual(transform2.rename_count, 0)
1160
        transform2.apply()
1161
        self.assertEqual(transform2.rename_count, 2)
1162
2502.1.2 by Aaron Bentley
Make the limited-renames functionality safer in the general case
1163
    def test_change_parent(self):
2502.1.8 by Aaron Bentley
Updates from review comments
1164
        """Ensure that after we change a parent, the results are still right.
1165
1166
        Renames and parent changes on pending transforms can happen as part
1167
        of conflict resolution, and are explicitly permitted by the
1168
        TreeTransform API.
1169
1170
        This test ensures they work correctly with the rename-avoidance
1171
        optimization.
1172
        """
2502.1.2 by Aaron Bentley
Make the limited-renames functionality safer in the general case
1173
        transform, root = self.get_transform()
2502.1.8 by Aaron Bentley
Updates from review comments
1174
        parent1 = transform.new_directory('parent1', root)
1175
        child1 = transform.new_file('child1', parent1, 'contents')
1176
        parent2 = transform.new_directory('parent2', root)
1177
        transform.adjust_path('child1', parent2, child1)
2502.1.2 by Aaron Bentley
Make the limited-renames functionality safer in the general case
1178
        transform.apply()
2502.1.8 by Aaron Bentley
Updates from review comments
1179
        self.failIfExists(self.wt.abspath('parent1/child1'))
1180
        self.failUnlessExists(self.wt.abspath('parent2/child1'))
1181
        # rename limbo/new-1 => parent1, rename limbo/new-3 => parent2
1182
        # no rename for child1 (counting only renames during apply)
2502.1.2 by Aaron Bentley
Make the limited-renames functionality safer in the general case
1183
        self.failUnlessEqual(2, transform.rename_count)
1184
1185
    def test_cancel_parent(self):
1186
        """Cancelling a parent doesn't cause deletion of a non-empty directory
1187
1188
        This is like the test_change_parent, except that we cancel the parent
1189
        before adjusting the path.  The transform must detect that the
1190
        directory is non-empty, and move children to safe locations.
1191
        """
1192
        transform, root = self.get_transform()
2502.1.8 by Aaron Bentley
Updates from review comments
1193
        parent1 = transform.new_directory('parent1', root)
1194
        child1 = transform.new_file('child1', parent1, 'contents')
1195
        child2 = transform.new_file('child2', parent1, 'contents')
1196
        try:
1197
            transform.cancel_creation(parent1)
1198
        except OSError:
1199
            self.fail('Failed to move child1 before deleting parent1')
1200
        transform.cancel_creation(child2)
1201
        transform.create_directory(parent1)
1202
        try:
1203
            transform.cancel_creation(parent1)
1204
        # If the transform incorrectly believes that child2 is still in
1205
        # parent1's limbo directory, it will try to rename it and fail
1206
        # because was already moved by the first cancel_creation.
1207
        except OSError:
1208
            self.fail('Transform still thinks child2 is a child of parent1')
1209
        parent2 = transform.new_directory('parent2', root)
1210
        transform.adjust_path('child1', parent2, child1)
2502.1.2 by Aaron Bentley
Make the limited-renames functionality safer in the general case
1211
        transform.apply()
2502.1.8 by Aaron Bentley
Updates from review comments
1212
        self.failIfExists(self.wt.abspath('parent1'))
1213
        self.failUnlessExists(self.wt.abspath('parent2/child1'))
1214
        # rename limbo/new-3 => parent2, rename limbo/new-2 => child1
2502.1.2 by Aaron Bentley
Make the limited-renames functionality safer in the general case
1215
        self.failUnlessEqual(2, transform.rename_count)
1216
1217
    def test_adjust_and_cancel(self):
2502.1.8 by Aaron Bentley
Updates from review comments
1218
        """Make sure adjust_path keeps track of limbo children properly"""
2502.1.2 by Aaron Bentley
Make the limited-renames functionality safer in the general case
1219
        transform, root = self.get_transform()
2502.1.8 by Aaron Bentley
Updates from review comments
1220
        parent1 = transform.new_directory('parent1', root)
1221
        child1 = transform.new_file('child1', parent1, 'contents')
1222
        parent2 = transform.new_directory('parent2', root)
1223
        transform.adjust_path('child1', parent2, child1)
1224
        transform.cancel_creation(child1)
2502.1.2 by Aaron Bentley
Make the limited-renames functionality safer in the general case
1225
        try:
2502.1.8 by Aaron Bentley
Updates from review comments
1226
            transform.cancel_creation(parent1)
1227
        # if the transform thinks child1 is still in parent1's limbo
1228
        # directory, it will attempt to move it and fail.
2502.1.2 by Aaron Bentley
Make the limited-renames functionality safer in the general case
1229
        except OSError:
2502.1.8 by Aaron Bentley
Updates from review comments
1230
            self.fail('Transform still thinks child1 is a child of parent1')
2502.1.2 by Aaron Bentley
Make the limited-renames functionality safer in the general case
1231
        transform.finalize()
1232
2502.1.3 by Aaron Bentley
Don't cause errors when creating contents for trans_ids with no parent/name
1233
    def test_noname_contents(self):
2502.1.8 by Aaron Bentley
Updates from review comments
1234
        """TreeTransform should permit deferring naming files."""
2502.1.3 by Aaron Bentley
Don't cause errors when creating contents for trans_ids with no parent/name
1235
        transform, root = self.get_transform()
2502.1.8 by Aaron Bentley
Updates from review comments
1236
        parent = transform.trans_id_file_id('parent-id')
2502.1.3 by Aaron Bentley
Don't cause errors when creating contents for trans_ids with no parent/name
1237
        try:
2502.1.8 by Aaron Bentley
Updates from review comments
1238
            transform.create_directory(parent)
2502.1.3 by Aaron Bentley
Don't cause errors when creating contents for trans_ids with no parent/name
1239
        except KeyError:
1240
            self.fail("Can't handle contents with no name")
1241
        transform.finalize()
1242
2502.1.9 by Aaron Bentley
Add additional test for no-name contents
1243
    def test_noname_contents_nested(self):
1244
        """TreeTransform should permit deferring naming files."""
1245
        transform, root = self.get_transform()
1246
        parent = transform.trans_id_file_id('parent-id')
1247
        try:
1248
            transform.create_directory(parent)
1249
        except KeyError:
1250
            self.fail("Can't handle contents with no name")
1251
        child = transform.new_directory('child', parent)
1252
        transform.adjust_path('parent', root, parent)
1253
        transform.apply()
1254
        self.failUnlessExists(self.wt.abspath('parent/child'))
1255
        self.assertEqual(1, transform.rename_count)
1256
2502.1.4 by Aaron Bentley
Ensure we only reuse limbo names appropriately
1257
    def test_reuse_name(self):
1258
        """Avoid reusing the same limbo name for different files"""
1259
        transform, root = self.get_transform()
2502.1.8 by Aaron Bentley
Updates from review comments
1260
        parent = transform.new_directory('parent', root)
1261
        child1 = transform.new_directory('child', parent)
2502.1.4 by Aaron Bentley
Ensure we only reuse limbo names appropriately
1262
        try:
2502.1.8 by Aaron Bentley
Updates from review comments
1263
            child2 = transform.new_directory('child', parent)
2502.1.4 by Aaron Bentley
Ensure we only reuse limbo names appropriately
1264
        except OSError:
1265
            self.fail('Tranform tried to use the same limbo name twice')
2502.1.8 by Aaron Bentley
Updates from review comments
1266
        transform.adjust_path('child2', parent, child2)
2502.1.4 by Aaron Bentley
Ensure we only reuse limbo names appropriately
1267
        transform.apply()
2502.1.8 by Aaron Bentley
Updates from review comments
1268
        # limbo/new-1 => parent, limbo/new-3 => parent/child2
1269
        # child2 is put into top-level limbo because child1 has already
1270
        # claimed the direct limbo path when child2 is created.  There is no
1271
        # advantage in renaming files once they're in top-level limbo, except
1272
        # as part of apply.
2502.1.4 by Aaron Bentley
Ensure we only reuse limbo names appropriately
1273
        self.assertEqual(2, transform.rename_count)
1274
1275
    def test_reuse_when_first_moved(self):
1276
        """Don't avoid direct paths when it is safe to use them"""
1277
        transform, root = self.get_transform()
2502.1.8 by Aaron Bentley
Updates from review comments
1278
        parent = transform.new_directory('parent', root)
1279
        child1 = transform.new_directory('child', parent)
1280
        transform.adjust_path('child1', parent, child1)
1281
        child2 = transform.new_directory('child', parent)
2502.1.4 by Aaron Bentley
Ensure we only reuse limbo names appropriately
1282
        transform.apply()
2502.1.8 by Aaron Bentley
Updates from review comments
1283
        # limbo/new-1 => parent
2502.1.4 by Aaron Bentley
Ensure we only reuse limbo names appropriately
1284
        self.assertEqual(1, transform.rename_count)
1285
1286
    def test_reuse_after_cancel(self):
1287
        """Don't avoid direct paths when it is safe to use them"""
1288
        transform, root = self.get_transform()
2502.1.8 by Aaron Bentley
Updates from review comments
1289
        parent2 = transform.new_directory('parent2', root)
1290
        child1 = transform.new_directory('child1', parent2)
1291
        transform.cancel_creation(parent2)
1292
        transform.create_directory(parent2)
1293
        child2 = transform.new_directory('child1', parent2)
1294
        transform.adjust_path('child2', parent2, child1)
2502.1.4 by Aaron Bentley
Ensure we only reuse limbo names appropriately
1295
        transform.apply()
2502.1.8 by Aaron Bentley
Updates from review comments
1296
        # limbo/new-1 => parent2, limbo/new-2 => parent2/child1
2502.1.4 by Aaron Bentley
Ensure we only reuse limbo names appropriately
1297
        self.assertEqual(2, transform.rename_count)
1298
2502.1.7 by Aaron Bentley
Fix finalize deletion ordering
1299
    def test_finalize_order(self):
2502.1.8 by Aaron Bentley
Updates from review comments
1300
        """Finalize must be done in child-to-parent order"""
2502.1.7 by Aaron Bentley
Fix finalize deletion ordering
1301
        transform, root = self.get_transform()
2502.1.8 by Aaron Bentley
Updates from review comments
1302
        parent = transform.new_directory('parent', root)
1303
        child = transform.new_directory('child', parent)
2502.1.7 by Aaron Bentley
Fix finalize deletion ordering
1304
        try:
1305
            transform.finalize()
1306
        except OSError:
2502.1.8 by Aaron Bentley
Updates from review comments
1307
            self.fail('Tried to remove parent before child1')
2502.1.7 by Aaron Bentley
Fix finalize deletion ordering
1308
2502.1.13 by Aaron Bentley
Updates from review
1309
    def test_cancel_with_cancelled_child_should_succeed(self):
2502.1.12 by Aaron Bentley
Avoid renaming children with no content
1310
        transform, root = self.get_transform()
1311
        parent = transform.new_directory('parent', root)
1312
        child = transform.new_directory('child', parent)
1313
        transform.cancel_creation(child)
2502.1.13 by Aaron Bentley
Updates from review
1314
        transform.cancel_creation(parent)
2502.1.12 by Aaron Bentley
Avoid renaming children with no content
1315
        transform.finalize()
1316
3638.3.15 by Vincent Ladeuil
Fix test_case_insensitive_clash to pass on all platforms (renamed too).
1317
    def test_rollback_on_directory_clash(self):
3063.1.1 by Alexander Belchenko
Catch OSError 17 (file exists) in final phase of tree transform and show filename to user (#111758).
1318
        def tt_helper():
3638.3.17 by Vincent Ladeuil
Fixed as per Aaron's review.
1319
            wt = self.make_branch_and_tree('.')
3063.1.1 by Alexander Belchenko
Catch OSError 17 (file exists) in final phase of tree transform and show filename to user (#111758).
1320
            tt = TreeTransform(wt)  # TreeTransform obtains write lock
1321
            try:
3638.3.15 by Vincent Ladeuil
Fix test_case_insensitive_clash to pass on all platforms (renamed too).
1322
                foo = tt.new_directory('foo', tt.root)
1323
                tt.new_file('bar', foo, 'foobar')
1324
                baz = tt.new_directory('baz', tt.root)
1325
                tt.new_file('qux', baz, 'quux')
1326
                # Ask for a rename 'foo' -> 'baz'
1327
                tt.adjust_path('baz', tt.root, foo)
3063.1.3 by Aaron Bentley
Update for Linux
1328
                # Lie to tt that we've already resolved all conflicts.
3063.1.1 by Alexander Belchenko
Catch OSError 17 (file exists) in final phase of tree transform and show filename to user (#111758).
1329
                tt.apply(no_conflicts=True)
3063.1.3 by Aaron Bentley
Update for Linux
1330
            except:
3063.1.1 by Alexander Belchenko
Catch OSError 17 (file exists) in final phase of tree transform and show filename to user (#111758).
1331
                wt.unlock()
3063.1.3 by Aaron Bentley
Update for Linux
1332
                raise
3638.3.17 by Vincent Ladeuil
Fixed as per Aaron's review.
1333
        # The rename will fail because the target directory is not empty (but
1334
        # raises FileExists anyway).
3063.1.1 by Alexander Belchenko
Catch OSError 17 (file exists) in final phase of tree transform and show filename to user (#111758).
1335
        err = self.assertRaises(errors.FileExists, tt_helper)
1336
        self.assertContainsRe(str(err),
3638.3.15 by Vincent Ladeuil
Fix test_case_insensitive_clash to pass on all platforms (renamed too).
1337
            "^File exists: .+/baz")
3063.1.1 by Alexander Belchenko
Catch OSError 17 (file exists) in final phase of tree transform and show filename to user (#111758).
1338
3063.1.2 by Alexander Belchenko
test for two directories clash
1339
    def test_two_directories_clash(self):
1340
        def tt_helper():
1341
            wt = self.make_branch_and_tree('.')
1342
            tt = TreeTransform(wt)  # TreeTransform obtains write lock
1343
            try:
3063.1.3 by Aaron Bentley
Update for Linux
1344
                foo_1 = tt.new_directory('foo', tt.root)
1345
                tt.new_directory('bar', foo_1)
3638.3.15 by Vincent Ladeuil
Fix test_case_insensitive_clash to pass on all platforms (renamed too).
1346
                # Adding the same directory with a different content
3063.1.3 by Aaron Bentley
Update for Linux
1347
                foo_2 = tt.new_directory('foo', tt.root)
1348
                tt.new_directory('baz', foo_2)
1349
                # Lie to tt that we've already resolved all conflicts.
3063.1.2 by Alexander Belchenko
test for two directories clash
1350
                tt.apply(no_conflicts=True)
3063.1.3 by Aaron Bentley
Update for Linux
1351
            except:
3063.1.2 by Alexander Belchenko
test for two directories clash
1352
                wt.unlock()
3063.1.3 by Aaron Bentley
Update for Linux
1353
                raise
3063.1.2 by Alexander Belchenko
test for two directories clash
1354
        err = self.assertRaises(errors.FileExists, tt_helper)
1355
        self.assertContainsRe(str(err),
1356
            "^File exists: .+/foo")
1357
3100.1.1 by Aaron Bentley
Fix ImmortalLimbo errors when transforms fail
1358
    def test_two_directories_clash_finalize(self):
1359
        def tt_helper():
1360
            wt = self.make_branch_and_tree('.')
1361
            tt = TreeTransform(wt)  # TreeTransform obtains write lock
1362
            try:
1363
                foo_1 = tt.new_directory('foo', tt.root)
1364
                tt.new_directory('bar', foo_1)
3638.3.15 by Vincent Ladeuil
Fix test_case_insensitive_clash to pass on all platforms (renamed too).
1365
                # Adding the same directory with a different content
3100.1.1 by Aaron Bentley
Fix ImmortalLimbo errors when transforms fail
1366
                foo_2 = tt.new_directory('foo', tt.root)
1367
                tt.new_directory('baz', foo_2)
1368
                # Lie to tt that we've already resolved all conflicts.
1369
                tt.apply(no_conflicts=True)
1370
            except:
1371
                tt.finalize()
1372
                raise
1373
        err = self.assertRaises(errors.FileExists, tt_helper)
1374
        self.assertContainsRe(str(err),
1375
            "^File exists: .+/foo")
1376
3535.6.1 by James Westby
Handle a file turning in to a directory in TreeTransform.
1377
    def test_file_to_directory(self):
1378
        wt = self.make_branch_and_tree('.')
3535.6.2 by James Westby
Fixes from review. Thanks Aaron and John.
1379
        self.build_tree(['foo'])
3535.6.1 by James Westby
Handle a file turning in to a directory in TreeTransform.
1380
        wt.add(['foo'])
3590.3.1 by James Westby
Make TreeTransform update the inventory with new kind information.
1381
        wt.commit("one")
3535.6.1 by James Westby
Handle a file turning in to a directory in TreeTransform.
1382
        tt = TreeTransform(wt)
3535.6.2 by James Westby
Fixes from review. Thanks Aaron and John.
1383
        self.addCleanup(tt.finalize)
3535.6.3 by James Westby
Fix the test to not create transform conflicts.
1384
        foo_trans_id = tt.trans_id_tree_path("foo")
1385
        tt.delete_contents(foo_trans_id)
1386
        tt.create_directory(foo_trans_id)
1387
        bar_trans_id = tt.trans_id_tree_path("foo/bar")
1388
        tt.create_file(["aa\n"], bar_trans_id)
1389
        tt.version_file("bar-1", bar_trans_id)
3535.6.2 by James Westby
Fixes from review. Thanks Aaron and John.
1390
        tt.apply()
3535.6.1 by James Westby
Handle a file turning in to a directory in TreeTransform.
1391
        self.failUnlessExists("foo/bar")
3590.3.2 by James Westby
Handle ->symlink changes as well.
1392
        wt.lock_read()
1393
        try:
1394
            self.assertEqual(wt.inventory.get_file_kind(wt.path2id("foo")),
1395
                    "directory")
1396
        finally:
1397
            wt.unlock()
3590.3.1 by James Westby
Make TreeTransform update the inventory with new kind information.
1398
        wt.commit("two")
1399
        changes = wt.changes_from(wt.basis_tree())
1400
        self.assertFalse(changes.has_changed(), changes)
3535.6.1 by James Westby
Handle a file turning in to a directory in TreeTransform.
1401
3590.3.2 by James Westby
Handle ->symlink changes as well.
1402
    def test_file_to_symlink(self):
3590.3.3 by James Westby
Make ->file changes work as well.
1403
        self.requireFeature(SymlinkFeature)
3590.3.2 by James Westby
Handle ->symlink changes as well.
1404
        wt = self.make_branch_and_tree('.')
1405
        self.build_tree(['foo'])
1406
        wt.add(['foo'])
1407
        wt.commit("one")
1408
        tt = TreeTransform(wt)
1409
        self.addCleanup(tt.finalize)
1410
        foo_trans_id = tt.trans_id_tree_path("foo")
1411
        tt.delete_contents(foo_trans_id)
1412
        tt.create_symlink("bar", foo_trans_id)
1413
        tt.apply()
1414
        self.failUnlessExists("foo")
1415
        wt.lock_read()
1416
        self.addCleanup(wt.unlock)
1417
        self.assertEqual(wt.inventory.get_file_kind(wt.path2id("foo")),
1418
                "symlink")
1419
3590.3.3 by James Westby
Make ->file changes work as well.
1420
    def test_dir_to_file(self):
1421
        wt = self.make_branch_and_tree('.')
1422
        self.build_tree(['foo/', 'foo/bar'])
1423
        wt.add(['foo', 'foo/bar'])
1424
        wt.commit("one")
1425
        tt = TreeTransform(wt)
1426
        self.addCleanup(tt.finalize)
1427
        foo_trans_id = tt.trans_id_tree_path("foo")
1428
        bar_trans_id = tt.trans_id_tree_path("foo/bar")
1429
        tt.delete_contents(foo_trans_id)
1430
        tt.delete_versioned(bar_trans_id)
1431
        tt.create_file(["aa\n"], foo_trans_id)
1432
        tt.apply()
1433
        self.failUnlessExists("foo")
1434
        wt.lock_read()
1435
        self.addCleanup(wt.unlock)
1436
        self.assertEqual(wt.inventory.get_file_kind(wt.path2id("foo")),
1437
                "file")
1438
3590.3.4 by James Westby
Add a test for creating hardlinks as well.
1439
    def test_dir_to_hardlink(self):
3590.3.5 by James Westby
Use HardlinkFeature for the hardlink test.
1440
        self.requireFeature(HardlinkFeature)
3590.3.4 by James Westby
Add a test for creating hardlinks as well.
1441
        wt = self.make_branch_and_tree('.')
1442
        self.build_tree(['foo/', 'foo/bar'])
1443
        wt.add(['foo', 'foo/bar'])
1444
        wt.commit("one")
1445
        tt = TreeTransform(wt)
1446
        self.addCleanup(tt.finalize)
1447
        foo_trans_id = tt.trans_id_tree_path("foo")
1448
        bar_trans_id = tt.trans_id_tree_path("foo/bar")
1449
        tt.delete_contents(foo_trans_id)
1450
        tt.delete_versioned(bar_trans_id)
1451
        self.build_tree(['baz'])
1452
        tt.create_hardlink("baz", foo_trans_id)
1453
        tt.apply()
1454
        self.failUnlessExists("foo")
1455
        self.failUnlessExists("baz")
1456
        wt.lock_read()
1457
        self.addCleanup(wt.unlock)
1458
        self.assertEqual(wt.inventory.get_file_kind(wt.path2id("foo")),
1459
                "file")
1460
3619.2.10 by Aaron Bentley
Compensate for stale entries in TT._needs_rename
1461
    def test_no_final_path(self):
1462
        transform, root = self.get_transform()
1463
        trans_id = transform.trans_id_file_id('foo')
1464
        transform.create_file('bar', trans_id)
1465
        transform.cancel_creation(trans_id)
1466
        transform.apply()
1467
3363.17.24 by Aaron Bentley
Implement create_by_tree
1468
    def test_create_from_tree(self):
1469
        tree1 = self.make_branch_and_tree('tree1')
1470
        self.build_tree_contents([('tree1/foo/',), ('tree1/bar', 'baz')])
1471
        tree1.add(['foo', 'bar'], ['foo-id', 'bar-id'])
1472
        tree2 = self.make_branch_and_tree('tree2')
1473
        tt = TreeTransform(tree2)
1474
        foo_trans_id = tt.create_path('foo', tt.root)
1475
        create_from_tree(tt, foo_trans_id, tree1, 'foo-id')
1476
        bar_trans_id = tt.create_path('bar', tt.root)
1477
        create_from_tree(tt, bar_trans_id, tree1, 'bar-id')
1478
        tt.apply()
1479
        self.assertEqual('directory', osutils.file_kind('tree2/foo'))
1480
        self.assertFileEqual('baz', 'tree2/bar')
1481
3363.17.25 by Aaron Bentley
remove get_inventory_entry, replace with create_from_tree
1482
    def test_create_from_tree_bytes(self):
1483
        """Provided lines are used instead of tree content."""
1484
        tree1 = self.make_branch_and_tree('tree1')
1485
        self.build_tree_contents([('tree1/foo', 'bar'),])
1486
        tree1.add('foo', 'foo-id')
1487
        tree2 = self.make_branch_and_tree('tree2')
1488
        tt = TreeTransform(tree2)
1489
        foo_trans_id = tt.create_path('foo', tt.root)
1490
        create_from_tree(tt, foo_trans_id, tree1, 'foo-id', bytes='qux')
1491
        tt.apply()
1492
        self.assertFileEqual('qux', 'tree2/foo')
1493
1494
    def test_create_from_tree_symlink(self):
3363.17.24 by Aaron Bentley
Implement create_by_tree
1495
        self.requireFeature(SymlinkFeature)
1496
        tree1 = self.make_branch_and_tree('tree1')
1497
        os.symlink('bar', 'tree1/foo')
1498
        tree1.add('foo', 'foo-id')
1499
        tt = TreeTransform(self.make_branch_and_tree('tree2'))
1500
        foo_trans_id = tt.create_path('foo', tt.root)
1501
        create_from_tree(tt, foo_trans_id, tree1, 'foo-id')
1502
        tt.apply()
1503
        self.assertEqual('bar', os.readlink('tree2/foo'))
1504
2502.1.3 by Aaron Bentley
Don't cause errors when creating contents for trans_ids with no parent/name
1505
1534.7.93 by Aaron Bentley
Added text merge test
1506
class TransformGroup(object):
3063.1.1 by Alexander Belchenko
Catch OSError 17 (file exists) in final phase of tree transform and show filename to user (#111758).
1507
1731.1.33 by Aaron Bentley
Revert no-special-root changes
1508
    def __init__(self, dirname, root_id):
1534.7.101 by Aaron Bentley
Got conflicts on symlinks working properly
1509
        self.name = dirname
1534.7.93 by Aaron Bentley
Added text merge test
1510
        os.mkdir(dirname)
1558.1.3 by Aaron Bentley
Fixed deprecated op use in test suite
1511
        self.wt = BzrDir.create_standalone_workingtree(dirname)
1731.1.33 by Aaron Bentley
Revert no-special-root changes
1512
        self.wt.set_root_id(root_id)
1558.1.3 by Aaron Bentley
Fixed deprecated op use in test suite
1513
        self.b = self.wt.branch
1534.7.93 by Aaron Bentley
Added text merge test
1514
        self.tt = TreeTransform(self.wt)
1534.7.181 by Aaron Bentley
Renamed a bunch of functions
1515
        self.root = self.tt.trans_id_tree_file_id(self.wt.get_root_id())
1534.7.93 by Aaron Bentley
Added text merge test
1516
1551.11.1 by Aaron Bentley
Initial work on converting TreeTransform to iter_changes format
1517
1534.7.95 by Aaron Bentley
Added more text merge tests
1518
def conflict_text(tree, merge):
1519
    template = '%s TREE\n%s%s\n%s%s MERGE-SOURCE\n'
1520
    return template % ('<' * 7, tree, '=' * 7, merge, '>' * 7)
1521
1534.7.93 by Aaron Bentley
Added text merge test
1522
1523
class TestTransformMerge(TestCaseInTempDir):
3199.1.5 by Vincent Ladeuil
Fix two more leaking tmp dirs, by reworking TransformPreview lock handling.
1524
1534.7.93 by Aaron Bentley
Added text merge test
1525
    def test_text_merge(self):
2116.4.1 by John Arbash Meinel
Update file and revision id generators.
1526
        root_id = generate_ids.gen_root_id()
1731.1.33 by Aaron Bentley
Revert no-special-root changes
1527
        base = TransformGroup("base", root_id)
1534.7.93 by Aaron Bentley
Added text merge test
1528
        base.tt.new_file('a', base.root, 'a\nb\nc\nd\be\n', 'a')
1534.7.95 by Aaron Bentley
Added more text merge tests
1529
        base.tt.new_file('b', base.root, 'b1', 'b')
1530
        base.tt.new_file('c', base.root, 'c', 'c')
1531
        base.tt.new_file('d', base.root, 'd', 'd')
1532
        base.tt.new_file('e', base.root, 'e', 'e')
1533
        base.tt.new_file('f', base.root, 'f', 'f')
1534.7.96 by Aaron Bentley
Tested with BASE as directory
1534
        base.tt.new_directory('g', base.root, 'g')
1534.7.97 by Aaron Bentley
Ensured foo.BASE is a directory if there's a conflict
1535
        base.tt.new_directory('h', base.root, 'h')
1534.7.93 by Aaron Bentley
Added text merge test
1536
        base.tt.apply()
1731.1.33 by Aaron Bentley
Revert no-special-root changes
1537
        other = TransformGroup("other", root_id)
1534.7.93 by Aaron Bentley
Added text merge test
1538
        other.tt.new_file('a', other.root, 'y\nb\nc\nd\be\n', 'a')
1534.7.95 by Aaron Bentley
Added more text merge tests
1539
        other.tt.new_file('b', other.root, 'b2', 'b')
1540
        other.tt.new_file('c', other.root, 'c2', 'c')
1541
        other.tt.new_file('d', other.root, 'd', 'd')
1542
        other.tt.new_file('e', other.root, 'e2', 'e')
1543
        other.tt.new_file('f', other.root, 'f', 'f')
1534.7.96 by Aaron Bentley
Tested with BASE as directory
1544
        other.tt.new_file('g', other.root, 'g', 'g')
1534.7.97 by Aaron Bentley
Ensured foo.BASE is a directory if there's a conflict
1545
        other.tt.new_file('h', other.root, 'h\ni\nj\nk\n', 'h')
1534.7.99 by Aaron Bentley
Handle non-existent BASE properly
1546
        other.tt.new_file('i', other.root, 'h\ni\nj\nk\n', 'i')
1534.7.93 by Aaron Bentley
Added text merge test
1547
        other.tt.apply()
1731.1.33 by Aaron Bentley
Revert no-special-root changes
1548
        this = TransformGroup("this", root_id)
1534.7.93 by Aaron Bentley
Added text merge test
1549
        this.tt.new_file('a', this.root, 'a\nb\nc\nd\bz\n', 'a')
1534.7.95 by Aaron Bentley
Added more text merge tests
1550
        this.tt.new_file('b', this.root, 'b', 'b')
1551
        this.tt.new_file('c', this.root, 'c', 'c')
1552
        this.tt.new_file('d', this.root, 'd2', 'd')
1553
        this.tt.new_file('e', this.root, 'e2', 'e')
1554
        this.tt.new_file('f', this.root, 'f', 'f')
1534.7.96 by Aaron Bentley
Tested with BASE as directory
1555
        this.tt.new_file('g', this.root, 'g', 'g')
1534.7.97 by Aaron Bentley
Ensured foo.BASE is a directory if there's a conflict
1556
        this.tt.new_file('h', this.root, '1\n2\n3\n4\n', 'h')
1534.7.99 by Aaron Bentley
Handle non-existent BASE properly
1557
        this.tt.new_file('i', this.root, '1\n2\n3\n4\n', 'i')
1534.7.93 by Aaron Bentley
Added text merge test
1558
        this.tt.apply()
3008.1.11 by Michael Hudson
restore the default behaviour of Merge3Merger.__init__().
1559
        Merge3Merger(this.wt, this.wt, base.wt, other.wt)
3008.1.6 by Michael Hudson
chop up Merge3Merger.__init__ into pieces
1560
1534.7.95 by Aaron Bentley
Added more text merge tests
1561
        # textual merge
1534.7.93 by Aaron Bentley
Added text merge test
1562
        self.assertEqual(this.wt.get_file('a').read(), 'y\nb\nc\nd\bz\n')
1534.7.95 by Aaron Bentley
Added more text merge tests
1563
        # three-way text conflict
3943.8.1 by Marius Kruger
remove all trailing whitespace from bzr source
1564
        self.assertEqual(this.wt.get_file('b').read(),
1534.7.95 by Aaron Bentley
Added more text merge tests
1565
                         conflict_text('b', 'b2'))
1566
        # OTHER wins
1567
        self.assertEqual(this.wt.get_file('c').read(), 'c2')
1568
        # THIS wins
1569
        self.assertEqual(this.wt.get_file('d').read(), 'd2')
1570
        # Ambigious clean merge
1571
        self.assertEqual(this.wt.get_file('e').read(), 'e2')
1572
        # No change
1573
        self.assertEqual(this.wt.get_file('f').read(), 'f')
3943.8.1 by Marius Kruger
remove all trailing whitespace from bzr source
1574
        # Correct correct results when THIS == OTHER
1534.7.96 by Aaron Bentley
Tested with BASE as directory
1575
        self.assertEqual(this.wt.get_file('g').read(), 'g')
1534.7.97 by Aaron Bentley
Ensured foo.BASE is a directory if there's a conflict
1576
        # Text conflict when THIS & OTHER are text and BASE is dir
3943.8.1 by Marius Kruger
remove all trailing whitespace from bzr source
1577
        self.assertEqual(this.wt.get_file('h').read(),
1534.7.97 by Aaron Bentley
Ensured foo.BASE is a directory if there's a conflict
1578
                         conflict_text('1\n2\n3\n4\n', 'h\ni\nj\nk\n'))
1579
        self.assertEqual(this.wt.get_file_byname('h.THIS').read(),
1580
                         '1\n2\n3\n4\n')
1581
        self.assertEqual(this.wt.get_file_byname('h.OTHER').read(),
1582
                         'h\ni\nj\nk\n')
1583
        self.assertEqual(file_kind(this.wt.abspath('h.BASE')), 'directory')
3943.8.1 by Marius Kruger
remove all trailing whitespace from bzr source
1584
        self.assertEqual(this.wt.get_file('i').read(),
1534.7.99 by Aaron Bentley
Handle non-existent BASE properly
1585
                         conflict_text('1\n2\n3\n4\n', 'h\ni\nj\nk\n'))
1586
        self.assertEqual(this.wt.get_file_byname('i.THIS').read(),
1587
                         '1\n2\n3\n4\n')
1588
        self.assertEqual(this.wt.get_file_byname('i.OTHER').read(),
1589
                         'h\ni\nj\nk\n')
1590
        self.assertEqual(os.path.exists(this.wt.abspath('i.BASE')), False)
1534.7.192 by Aaron Bentley
Record hashes produced by merges
1591
        modified = ['a', 'b', 'c', 'h', 'i']
1592
        merge_modified = this.wt.merge_modified()
1593
        self.assertSubset(merge_modified, modified)
1594
        self.assertEqual(len(merge_modified), len(modified))
1595
        file(this.wt.id2abspath('a'), 'wb').write('booga')
1596
        modified.pop(0)
1597
        merge_modified = this.wt.merge_modified()
1598
        self.assertSubset(merge_modified, modified)
1599
        self.assertEqual(len(merge_modified), len(modified))
1558.12.10 by Aaron Bentley
Be robust when merge_hash file_id not in inventory
1600
        this.wt.remove('b')
2796.1.4 by Aaron Bentley
Fix up various test cases
1601
        this.wt.revert()
1534.7.101 by Aaron Bentley
Got conflicts on symlinks working properly
1602
1603
    def test_file_merge(self):
2949.5.1 by Alexander Belchenko
selftest: use SymlinkFeature instead of TestSkipped where appropriate
1604
        self.requireFeature(SymlinkFeature)
2116.4.1 by John Arbash Meinel
Update file and revision id generators.
1605
        root_id = generate_ids.gen_root_id()
1731.1.33 by Aaron Bentley
Revert no-special-root changes
1606
        base = TransformGroup("BASE", root_id)
1607
        this = TransformGroup("THIS", root_id)
1608
        other = TransformGroup("OTHER", root_id)
1534.7.101 by Aaron Bentley
Got conflicts on symlinks working properly
1609
        for tg in this, base, other:
1610
            tg.tt.new_directory('a', tg.root, 'a')
1611
            tg.tt.new_symlink('b', tg.root, 'b', 'b')
1612
            tg.tt.new_file('c', tg.root, 'c', 'c')
1613
            tg.tt.new_symlink('d', tg.root, tg.name, 'd')
3943.8.1 by Marius Kruger
remove all trailing whitespace from bzr source
1614
        targets = ((base, 'base-e', 'base-f', None, None),
1615
                   (this, 'other-e', 'this-f', 'other-g', 'this-h'),
1534.7.104 by Aaron Bentley
Fixed set_versioned, enhanced conflict testing
1616
                   (other, 'other-e', None, 'other-g', 'other-h'))
1617
        for tg, e_target, f_target, g_target, h_target in targets:
3943.8.1 by Marius Kruger
remove all trailing whitespace from bzr source
1618
            for link, target in (('e', e_target), ('f', f_target),
1534.7.104 by Aaron Bentley
Fixed set_versioned, enhanced conflict testing
1619
                                 ('g', g_target), ('h', h_target)):
1620
                if target is not None:
1621
                    tg.tt.new_symlink(link, tg.root, target, link)
1534.7.102 by Aaron Bentley
Deleted old pre-conflict contents
1622
1623
        for tg in this, base, other:
1534.7.101 by Aaron Bentley
Got conflicts on symlinks working properly
1624
            tg.tt.apply()
3008.1.11 by Michael Hudson
restore the default behaviour of Merge3Merger.__init__().
1625
        Merge3Merger(this.wt, this.wt, base.wt, other.wt)
1534.7.101 by Aaron Bentley
Got conflicts on symlinks working properly
1626
        self.assertIs(os.path.isdir(this.wt.abspath('a')), True)
1627
        self.assertIs(os.path.islink(this.wt.abspath('b')), True)
1628
        self.assertIs(os.path.isfile(this.wt.abspath('c')), True)
1629
        for suffix in ('THIS', 'BASE', 'OTHER'):
1630
            self.assertEqual(os.readlink(this.wt.abspath('d.'+suffix)), suffix)
1534.7.102 by Aaron Bentley
Deleted old pre-conflict contents
1631
        self.assertIs(os.path.lexists(this.wt.abspath('d')), False)
1534.7.104 by Aaron Bentley
Fixed set_versioned, enhanced conflict testing
1632
        self.assertEqual(this.wt.id2path('d'), 'd.OTHER')
1633
        self.assertEqual(this.wt.id2path('f'), 'f.THIS')
1534.7.102 by Aaron Bentley
Deleted old pre-conflict contents
1634
        self.assertEqual(os.readlink(this.wt.abspath('e')), 'other-e')
1635
        self.assertIs(os.path.lexists(this.wt.abspath('e.THIS')), False)
1636
        self.assertIs(os.path.lexists(this.wt.abspath('e.OTHER')), False)
1637
        self.assertIs(os.path.lexists(this.wt.abspath('e.BASE')), False)
1534.7.104 by Aaron Bentley
Fixed set_versioned, enhanced conflict testing
1638
        self.assertIs(os.path.lexists(this.wt.abspath('g')), True)
1639
        self.assertIs(os.path.lexists(this.wt.abspath('g.BASE')), False)
1640
        self.assertIs(os.path.lexists(this.wt.abspath('h')), False)
1641
        self.assertIs(os.path.lexists(this.wt.abspath('h.BASE')), False)
1642
        self.assertIs(os.path.lexists(this.wt.abspath('h.THIS')), True)
1643
        self.assertIs(os.path.lexists(this.wt.abspath('h.OTHER')), True)
1534.7.105 by Aaron Bentley
Got merge with rename working
1644
1645
    def test_filename_merge(self):
2116.4.1 by John Arbash Meinel
Update file and revision id generators.
1646
        root_id = generate_ids.gen_root_id()
1731.1.33 by Aaron Bentley
Revert no-special-root changes
1647
        base = TransformGroup("BASE", root_id)
1648
        this = TransformGroup("THIS", root_id)
1649
        other = TransformGroup("OTHER", root_id)
3943.8.1 by Marius Kruger
remove all trailing whitespace from bzr source
1650
        base_a, this_a, other_a = [t.tt.new_directory('a', t.root, 'a')
1534.7.105 by Aaron Bentley
Got merge with rename working
1651
                                   for t in [base, this, other]]
3943.8.1 by Marius Kruger
remove all trailing whitespace from bzr source
1652
        base_b, this_b, other_b = [t.tt.new_directory('b', t.root, 'b')
1534.7.105 by Aaron Bentley
Got merge with rename working
1653
                                   for t in [base, this, other]]
1654
        base.tt.new_directory('c', base_a, 'c')
1655
        this.tt.new_directory('c1', this_a, 'c')
1656
        other.tt.new_directory('c', other_b, 'c')
1657
1658
        base.tt.new_directory('d', base_a, 'd')
1659
        this.tt.new_directory('d1', this_b, 'd')
1660
        other.tt.new_directory('d', other_a, 'd')
1661
1662
        base.tt.new_directory('e', base_a, 'e')
1663
        this.tt.new_directory('e', this_a, 'e')
1664
        other.tt.new_directory('e1', other_b, 'e')
1665
1666
        base.tt.new_directory('f', base_a, 'f')
1667
        this.tt.new_directory('f1', this_b, 'f')
1668
        other.tt.new_directory('f1', other_b, 'f')
1669
1670
        for tg in [this, base, other]:
1671
            tg.tt.apply()
3008.1.11 by Michael Hudson
restore the default behaviour of Merge3Merger.__init__().
1672
        Merge3Merger(this.wt, this.wt, base.wt, other.wt)
1534.7.176 by abentley
Fixed up tests for Windows
1673
        self.assertEqual(this.wt.id2path('c'), pathjoin('b/c1'))
1674
        self.assertEqual(this.wt.id2path('d'), pathjoin('b/d1'))
1675
        self.assertEqual(this.wt.id2path('e'), pathjoin('b/e1'))
1676
        self.assertEqual(this.wt.id2path('f'), pathjoin('b/f1'))
1534.7.105 by Aaron Bentley
Got merge with rename working
1677
1678
    def test_filename_merge_conflicts(self):
2116.4.1 by John Arbash Meinel
Update file and revision id generators.
1679
        root_id = generate_ids.gen_root_id()
1731.1.33 by Aaron Bentley
Revert no-special-root changes
1680
        base = TransformGroup("BASE", root_id)
1681
        this = TransformGroup("THIS", root_id)
1682
        other = TransformGroup("OTHER", root_id)
3943.8.1 by Marius Kruger
remove all trailing whitespace from bzr source
1683
        base_a, this_a, other_a = [t.tt.new_directory('a', t.root, 'a')
1534.7.105 by Aaron Bentley
Got merge with rename working
1684
                                   for t in [base, this, other]]
3943.8.1 by Marius Kruger
remove all trailing whitespace from bzr source
1685
        base_b, this_b, other_b = [t.tt.new_directory('b', t.root, 'b')
1534.7.105 by Aaron Bentley
Got merge with rename working
1686
                                   for t in [base, this, other]]
1687
1688
        base.tt.new_file('g', base_a, 'g', 'g')
1689
        other.tt.new_file('g1', other_b, 'g1', 'g')
1690
1691
        base.tt.new_file('h', base_a, 'h', 'h')
1692
        this.tt.new_file('h1', this_b, 'h1', 'h')
1693
1694
        base.tt.new_file('i', base.root, 'i', 'i')
1534.7.153 by Aaron Bentley
Handled test cases involving symlinks
1695
        other.tt.new_directory('i1', this_b, 'i')
1534.7.105 by Aaron Bentley
Got merge with rename working
1696
1697
        for tg in [this, base, other]:
1698
            tg.tt.apply()
3008.1.11 by Michael Hudson
restore the default behaviour of Merge3Merger.__init__().
1699
        Merge3Merger(this.wt, this.wt, base.wt, other.wt)
1534.7.105 by Aaron Bentley
Got merge with rename working
1700
1534.7.176 by abentley
Fixed up tests for Windows
1701
        self.assertEqual(this.wt.id2path('g'), pathjoin('b/g1.OTHER'))
1534.7.105 by Aaron Bentley
Got merge with rename working
1702
        self.assertIs(os.path.lexists(this.wt.abspath('b/g1.BASE')), True)
1703
        self.assertIs(os.path.lexists(this.wt.abspath('b/g1.THIS')), False)
1534.7.176 by abentley
Fixed up tests for Windows
1704
        self.assertEqual(this.wt.id2path('h'), pathjoin('b/h1.THIS'))
1534.7.105 by Aaron Bentley
Got merge with rename working
1705
        self.assertIs(os.path.lexists(this.wt.abspath('b/h1.BASE')), True)
1706
        self.assertIs(os.path.lexists(this.wt.abspath('b/h1.OTHER')), False)
1534.7.176 by abentley
Fixed up tests for Windows
1707
        self.assertEqual(this.wt.id2path('i'), pathjoin('b/i1.OTHER'))
1534.7.183 by Aaron Bentley
Fixed build_tree with symlinks
1708
2027.1.1 by John Arbash Meinel
Fix bug #56549, and write a direct test that the right path is being statted
1709
1966.1.1 by Aaron Bentley
Implement disk-content merge and conflict resolution for build_tree
1710
class TestBuildTree(tests.TestCaseWithTransport):
1711
3006.2.2 by Alexander Belchenko
tests added.
1712
    def test_build_tree_with_symlinks(self):
2949.5.1 by Alexander Belchenko
selftest: use SymlinkFeature instead of TestSkipped where appropriate
1713
        self.requireFeature(SymlinkFeature)
1534.7.183 by Aaron Bentley
Fixed build_tree with symlinks
1714
        os.mkdir('a')
1715
        a = BzrDir.create_standalone_workingtree('a')
1716
        os.mkdir('a/foo')
1717
        file('a/foo/bar', 'wb').write('contents')
1718
        os.symlink('a/foo/bar', 'a/foo/baz')
1719
        a.add(['foo', 'foo/bar', 'foo/baz'])
1720
        a.commit('initial commit')
1721
        b = BzrDir.create_standalone_workingtree('b')
2255.2.194 by Robert Collins
[BROKEN] Many updates to stop using experimental formats in tests.
1722
        basis = a.basis_tree()
1723
        basis.lock_read()
1724
        self.addCleanup(basis.unlock)
1725
        build_tree(basis, b)
1534.7.183 by Aaron Bentley
Fixed build_tree with symlinks
1726
        self.assertIs(os.path.isdir('b/foo'), True)
1727
        self.assertEqual(file('b/foo/bar', 'rb').read(), "contents")
1728
        self.assertEqual(os.readlink('b/foo/baz'), 'a/foo/bar')
1966.1.1 by Aaron Bentley
Implement disk-content merge and conflict resolution for build_tree
1729
2100.3.21 by Aaron Bentley
Work on checking out by-reference trees
1730
    def test_build_with_references(self):
2255.2.194 by Robert Collins
[BROKEN] Many updates to stop using experimental formats in tests.
1731
        tree = self.make_branch_and_tree('source',
1732
            format='dirstate-with-subtree')
1733
        subtree = self.make_branch_and_tree('source/subtree',
1734
            format='dirstate-with-subtree')
2100.3.21 by Aaron Bentley
Work on checking out by-reference trees
1735
        tree.add_reference(subtree)
1736
        tree.commit('a revision')
1737
        tree.branch.create_checkout('target')
1738
        self.failUnlessExists('target')
1739
        self.failUnlessExists('target/subtree')
1740
1966.1.1 by Aaron Bentley
Implement disk-content merge and conflict resolution for build_tree
1741
    def test_file_conflict_handling(self):
1742
        """Ensure that when building trees, conflict handling is done"""
1743
        source = self.make_branch_and_tree('source')
1744
        target = self.make_branch_and_tree('target')
1745
        self.build_tree(['source/file', 'target/file'])
1966.1.2 by Aaron Bentley
Divert files instead of failing to create them, update from review
1746
        source.add('file', 'new-file')
1966.1.1 by Aaron Bentley
Implement disk-content merge and conflict resolution for build_tree
1747
        source.commit('added file')
1748
        build_tree(source.basis_tree(), target)
1966.1.2 by Aaron Bentley
Divert files instead of failing to create them, update from review
1749
        self.assertEqual([DuplicateEntry('Moved existing file to',
1750
                          'file.moved', 'file', None, 'new-file')],
1751
                         target.conflicts())
1966.1.1 by Aaron Bentley
Implement disk-content merge and conflict resolution for build_tree
1752
        target2 = self.make_branch_and_tree('target2')
1753
        target_file = file('target2/file', 'wb')
1754
        try:
1966.1.2 by Aaron Bentley
Divert files instead of failing to create them, update from review
1755
            source_file = file('source/file', 'rb')
1756
            try:
1757
                target_file.write(source_file.read())
1758
            finally:
1759
                source_file.close()
1966.1.1 by Aaron Bentley
Implement disk-content merge and conflict resolution for build_tree
1760
        finally:
1761
            target_file.close()
1762
        build_tree(source.basis_tree(), target2)
1966.1.2 by Aaron Bentley
Divert files instead of failing to create them, update from review
1763
        self.assertEqual([], target2.conflicts())
1966.1.1 by Aaron Bentley
Implement disk-content merge and conflict resolution for build_tree
1764
1765
    def test_symlink_conflict_handling(self):
1766
        """Ensure that when building trees, conflict handling is done"""
2949.5.1 by Alexander Belchenko
selftest: use SymlinkFeature instead of TestSkipped where appropriate
1767
        self.requireFeature(SymlinkFeature)
1966.1.1 by Aaron Bentley
Implement disk-content merge and conflict resolution for build_tree
1768
        source = self.make_branch_and_tree('source')
1769
        os.symlink('foo', 'source/symlink')
1966.1.2 by Aaron Bentley
Divert files instead of failing to create them, update from review
1770
        source.add('symlink', 'new-symlink')
1966.1.1 by Aaron Bentley
Implement disk-content merge and conflict resolution for build_tree
1771
        source.commit('added file')
1772
        target = self.make_branch_and_tree('target')
1773
        os.symlink('bar', 'target/symlink')
1774
        build_tree(source.basis_tree(), target)
1966.1.2 by Aaron Bentley
Divert files instead of failing to create them, update from review
1775
        self.assertEqual([DuplicateEntry('Moved existing file to',
1776
            'symlink.moved', 'symlink', None, 'new-symlink')],
1777
            target.conflicts())
1966.1.1 by Aaron Bentley
Implement disk-content merge and conflict resolution for build_tree
1778
        target = self.make_branch_and_tree('target2')
1779
        os.symlink('foo', 'target2/symlink')
1780
        build_tree(source.basis_tree(), target)
1966.1.2 by Aaron Bentley
Divert files instead of failing to create them, update from review
1781
        self.assertEqual([], target.conflicts())
3943.8.1 by Marius Kruger
remove all trailing whitespace from bzr source
1782
1966.1.1 by Aaron Bentley
Implement disk-content merge and conflict resolution for build_tree
1783
    def test_directory_conflict_handling(self):
1784
        """Ensure that when building trees, conflict handling is done"""
1785
        source = self.make_branch_and_tree('source')
1786
        target = self.make_branch_and_tree('target')
1787
        self.build_tree(['source/dir1/', 'source/dir1/file', 'target/dir1/'])
1966.1.2 by Aaron Bentley
Divert files instead of failing to create them, update from review
1788
        source.add(['dir1', 'dir1/file'], ['new-dir1', 'new-file'])
1966.1.1 by Aaron Bentley
Implement disk-content merge and conflict resolution for build_tree
1789
        source.commit('added file')
1790
        build_tree(source.basis_tree(), target)
1966.1.2 by Aaron Bentley
Divert files instead of failing to create them, update from review
1791
        self.assertEqual([], target.conflicts())
1966.1.1 by Aaron Bentley
Implement disk-content merge and conflict resolution for build_tree
1792
        self.failUnlessExists('target/dir1/file')
1793
1794
        # Ensure contents are merged
1795
        target = self.make_branch_and_tree('target2')
1796
        self.build_tree(['target2/dir1/', 'target2/dir1/file2'])
1797
        build_tree(source.basis_tree(), target)
1966.1.2 by Aaron Bentley
Divert files instead of failing to create them, update from review
1798
        self.assertEqual([], target.conflicts())
1966.1.1 by Aaron Bentley
Implement disk-content merge and conflict resolution for build_tree
1799
        self.failUnlessExists('target2/dir1/file2')
1800
        self.failUnlessExists('target2/dir1/file')
1801
1802
        # Ensure new contents are suppressed for existing branches
1966.1.2 by Aaron Bentley
Divert files instead of failing to create them, update from review
1803
        target = self.make_branch_and_tree('target3')
1804
        self.make_branch('target3/dir1')
1966.1.1 by Aaron Bentley
Implement disk-content merge and conflict resolution for build_tree
1805
        self.build_tree(['target3/dir1/file2'])
1806
        build_tree(source.basis_tree(), target)
1966.1.2 by Aaron Bentley
Divert files instead of failing to create them, update from review
1807
        self.failIfExists('target3/dir1/file')
1966.1.1 by Aaron Bentley
Implement disk-content merge and conflict resolution for build_tree
1808
        self.failUnlessExists('target3/dir1/file2')
1966.1.2 by Aaron Bentley
Divert files instead of failing to create them, update from review
1809
        self.failUnlessExists('target3/dir1.diverted/file')
1810
        self.assertEqual([DuplicateEntry('Diverted to',
1811
            'dir1.diverted', 'dir1', 'new-dir1', None)],
1812
            target.conflicts())
1813
1814
        target = self.make_branch_and_tree('target4')
1815
        self.build_tree(['target4/dir1/'])
1816
        self.make_branch('target4/dir1/file')
1817
        build_tree(source.basis_tree(), target)
1818
        self.failUnlessExists('target4/dir1/file')
1819
        self.assertEqual('directory', file_kind('target4/dir1/file'))
1820
        self.failUnlessExists('target4/dir1/file.diverted')
1821
        self.assertEqual([DuplicateEntry('Diverted to',
1822
            'dir1/file.diverted', 'dir1/file', 'new-file', None)],
1823
            target.conflicts())
1966.1.1 by Aaron Bentley
Implement disk-content merge and conflict resolution for build_tree
1824
1825
    def test_mixed_conflict_handling(self):
1826
        """Ensure that when building trees, conflict handling is done"""
1827
        source = self.make_branch_and_tree('source')
1828
        target = self.make_branch_and_tree('target')
1829
        self.build_tree(['source/name', 'target/name/'])
1966.1.2 by Aaron Bentley
Divert files instead of failing to create them, update from review
1830
        source.add('name', 'new-name')
1831
        source.commit('added file')
1832
        build_tree(source.basis_tree(), target)
1833
        self.assertEqual([DuplicateEntry('Moved existing file to',
1834
            'name.moved', 'name', None, 'new-name')], target.conflicts())
1835
1836
    def test_raises_in_populated(self):
1837
        source = self.make_branch_and_tree('source')
1838
        self.build_tree(['source/name'])
1966.1.1 by Aaron Bentley
Implement disk-content merge and conflict resolution for build_tree
1839
        source.add('name')
1966.1.2 by Aaron Bentley
Divert files instead of failing to create them, update from review
1840
        source.commit('added name')
1841
        target = self.make_branch_and_tree('target')
1842
        self.build_tree(['target/name'])
1843
        target.add('name')
3943.8.1 by Marius Kruger
remove all trailing whitespace from bzr source
1844
        self.assertRaises(errors.WorkingTreeAlreadyPopulated,
2090.2.1 by Martin Pool
Fix some code which relies on assertions and breaks under python -O
1845
            build_tree, source.basis_tree(), target)
1966.1.1 by Aaron Bentley
Implement disk-content merge and conflict resolution for build_tree
1846
2502.1.1 by Aaron Bentley
Ensure renames only root children are renamed when building trees
1847
    def test_build_tree_rename_count(self):
1848
        source = self.make_branch_and_tree('source')
1849
        self.build_tree(['source/file1', 'source/dir1/'])
1850
        source.add(['file1', 'dir1'])
1851
        source.commit('add1')
1852
        target1 = self.make_branch_and_tree('target1')
2502.1.6 by Aaron Bentley
Update from review comments
1853
        transform_result = build_tree(source.basis_tree(), target1)
1854
        self.assertEqual(2, transform_result.rename_count)
2502.1.1 by Aaron Bentley
Ensure renames only root children are renamed when building trees
1855
1856
        self.build_tree(['source/dir1/file2'])
1857
        source.add(['dir1/file2'])
1858
        source.commit('add3')
1859
        target2 = self.make_branch_and_tree('target2')
2502.1.6 by Aaron Bentley
Update from review comments
1860
        transform_result = build_tree(source.basis_tree(), target2)
2502.1.1 by Aaron Bentley
Ensure renames only root children are renamed when building trees
1861
        # children of non-root directories should not be renamed
2502.1.6 by Aaron Bentley
Update from review comments
1862
        self.assertEqual(2, transform_result.rename_count)
2502.1.1 by Aaron Bentley
Ensure renames only root children are renamed when building trees
1863
3136.1.2 by Aaron Bentley
Implement hard-linking for build_tree
1864
    def create_ab_tree(self):
1865
        """Create a committed test tree with two files"""
1866
        source = self.make_branch_and_tree('source')
1867
        self.build_tree_contents([('source/file1', 'A')])
1868
        self.build_tree_contents([('source/file2', 'B')])
1869
        source.add(['file1', 'file2'], ['file1-id', 'file2-id'])
1870
        source.commit('commit files')
1871
        source.lock_write()
1872
        self.addCleanup(source.unlock)
1873
        return source
1874
3123.5.1 by Aaron Bentley
Make build-tree able to use an additional 'accelerator' tree
1875
    def test_build_tree_accelerator_tree(self):
3136.1.2 by Aaron Bentley
Implement hard-linking for build_tree
1876
        source = self.create_ab_tree()
3123.5.1 by Aaron Bentley
Make build-tree able to use an additional 'accelerator' tree
1877
        self.build_tree_contents([('source/file2', 'C')])
1878
        calls = []
1879
        real_source_get_file = source.get_file
1880
        def get_file(file_id, path=None):
1881
            calls.append(file_id)
1882
            return real_source_get_file(file_id, path)
1883
        source.get_file = get_file
1884
        target = self.make_branch_and_tree('target')
3123.5.19 by Aaron Bentley
Ensure content is exactly the same, when accelerator used
1885
        revision_tree = source.basis_tree()
1886
        revision_tree.lock_read()
1887
        self.addCleanup(revision_tree.unlock)
1888
        build_tree(revision_tree, target, source)
3123.5.1 by Aaron Bentley
Make build-tree able to use an additional 'accelerator' tree
1889
        self.assertEqual(['file1-id'], calls)
3123.5.19 by Aaron Bentley
Ensure content is exactly the same, when accelerator used
1890
        target.lock_read()
1891
        self.addCleanup(target.unlock)
3254.1.1 by Aaron Bentley
Make Tree.iter_changes a public method
1892
        self.assertEqual([], list(target.iter_changes(revision_tree)))
3123.5.1 by Aaron Bentley
Make build-tree able to use an additional 'accelerator' tree
1893
3123.5.4 by Aaron Bentley
Use an accelerator tree when branching, handle no-such-id correctly
1894
    def test_build_tree_accelerator_tree_missing_file(self):
3136.1.2 by Aaron Bentley
Implement hard-linking for build_tree
1895
        source = self.create_ab_tree()
3123.5.4 by Aaron Bentley
Use an accelerator tree when branching, handle no-such-id correctly
1896
        os.unlink('source/file1')
1897
        source.remove(['file2'])
1898
        target = self.make_branch_and_tree('target')
3123.5.19 by Aaron Bentley
Ensure content is exactly the same, when accelerator used
1899
        revision_tree = source.basis_tree()
1900
        revision_tree.lock_read()
1901
        self.addCleanup(revision_tree.unlock)
1902
        build_tree(revision_tree, target, source)
1903
        target.lock_read()
1904
        self.addCleanup(target.unlock)
3254.1.1 by Aaron Bentley
Make Tree.iter_changes a public method
1905
        self.assertEqual([], list(target.iter_changes(revision_tree)))
3123.5.4 by Aaron Bentley
Use an accelerator tree when branching, handle no-such-id correctly
1906
3123.5.16 by Aaron Bentley
Test handling of conversion to non-file
1907
    def test_build_tree_accelerator_wrong_kind(self):
3146.4.8 by Aaron Bentley
Add missing symlink requirement
1908
        self.requireFeature(SymlinkFeature)
3123.5.16 by Aaron Bentley
Test handling of conversion to non-file
1909
        source = self.make_branch_and_tree('source')
1910
        self.build_tree_contents([('source/file1', '')])
1911
        self.build_tree_contents([('source/file2', '')])
1912
        source.add(['file1', 'file2'], ['file1-id', 'file2-id'])
1913
        source.commit('commit files')
1914
        os.unlink('source/file2')
1915
        self.build_tree_contents([('source/file2/', 'C')])
1916
        os.unlink('source/file1')
1917
        os.symlink('file2', 'source/file1')
1918
        calls = []
1919
        real_source_get_file = source.get_file
1920
        def get_file(file_id, path=None):
1921
            calls.append(file_id)
1922
            return real_source_get_file(file_id, path)
1923
        source.get_file = get_file
1924
        target = self.make_branch_and_tree('target')
3123.5.19 by Aaron Bentley
Ensure content is exactly the same, when accelerator used
1925
        revision_tree = source.basis_tree()
1926
        revision_tree.lock_read()
1927
        self.addCleanup(revision_tree.unlock)
1928
        build_tree(revision_tree, target, source)
3123.5.16 by Aaron Bentley
Test handling of conversion to non-file
1929
        self.assertEqual([], calls)
3123.5.19 by Aaron Bentley
Ensure content is exactly the same, when accelerator used
1930
        target.lock_read()
1931
        self.addCleanup(target.unlock)
3254.1.1 by Aaron Bentley
Make Tree.iter_changes a public method
1932
        self.assertEqual([], list(target.iter_changes(revision_tree)))
3123.5.16 by Aaron Bentley
Test handling of conversion to non-file
1933
3136.1.2 by Aaron Bentley
Implement hard-linking for build_tree
1934
    def test_build_tree_hardlink(self):
1935
        self.requireFeature(HardlinkFeature)
1936
        source = self.create_ab_tree()
1937
        target = self.make_branch_and_tree('target')
1938
        revision_tree = source.basis_tree()
1939
        revision_tree.lock_read()
1940
        self.addCleanup(revision_tree.unlock)
1941
        build_tree(revision_tree, target, source, hardlink=True)
1942
        target.lock_read()
1943
        self.addCleanup(target.unlock)
3254.1.1 by Aaron Bentley
Make Tree.iter_changes a public method
1944
        self.assertEqual([], list(target.iter_changes(revision_tree)))
3136.1.2 by Aaron Bentley
Implement hard-linking for build_tree
1945
        source_stat = os.stat('source/file1')
1946
        target_stat = os.stat('target/file1')
1947
        self.assertEqual(source_stat, target_stat)
1948
1949
        # Explicitly disallowing hardlinks should prevent them.
1950
        target2 = self.make_branch_and_tree('target2')
1951
        build_tree(revision_tree, target2, source, hardlink=False)
1952
        target2.lock_read()
1953
        self.addCleanup(target2.unlock)
3254.1.1 by Aaron Bentley
Make Tree.iter_changes a public method
1954
        self.assertEqual([], list(target2.iter_changes(revision_tree)))
3136.1.2 by Aaron Bentley
Implement hard-linking for build_tree
1955
        source_stat = os.stat('source/file1')
1956
        target2_stat = os.stat('target2/file1')
1957
        self.assertNotEqual(source_stat, target2_stat)
1958
3137.1.1 by Aaron Bentley
Fix build_tree acceleration when file is moved in accelerator_tree
1959
    def test_build_tree_accelerator_tree_moved(self):
1960
        source = self.make_branch_and_tree('source')
1961
        self.build_tree_contents([('source/file1', 'A')])
1962
        source.add(['file1'], ['file1-id'])
1963
        source.commit('commit files')
1964
        source.rename_one('file1', 'file2')
1965
        source.lock_read()
1966
        self.addCleanup(source.unlock)
1967
        target = self.make_branch_and_tree('target')
1968
        revision_tree = source.basis_tree()
1969
        revision_tree.lock_read()
1970
        self.addCleanup(revision_tree.unlock)
1971
        build_tree(revision_tree, target, source)
1972
        target.lock_read()
1973
        self.addCleanup(target.unlock)
3254.1.1 by Aaron Bentley
Make Tree.iter_changes a public method
1974
        self.assertEqual([], list(target.iter_changes(revision_tree)))
3137.1.1 by Aaron Bentley
Fix build_tree acceleration when file is moved in accelerator_tree
1975
3136.1.2 by Aaron Bentley
Implement hard-linking for build_tree
1976
    def test_build_tree_hardlinks_preserve_execute(self):
1977
        self.requireFeature(HardlinkFeature)
1978
        source = self.create_ab_tree()
1979
        tt = TreeTransform(source)
1980
        trans_id = tt.trans_id_tree_file_id('file1-id')
1981
        tt.set_executability(True, trans_id)
1982
        tt.apply()
1983
        self.assertTrue(source.is_executable('file1-id'))
1984
        target = self.make_branch_and_tree('target')
1985
        revision_tree = source.basis_tree()
1986
        revision_tree.lock_read()
1987
        self.addCleanup(revision_tree.unlock)
1988
        build_tree(revision_tree, target, source, hardlink=True)
1989
        target.lock_read()
1990
        self.addCleanup(target.unlock)
3254.1.1 by Aaron Bentley
Make Tree.iter_changes a public method
1991
        self.assertEqual([], list(target.iter_changes(revision_tree)))
3136.1.2 by Aaron Bentley
Implement hard-linking for build_tree
1992
        self.assertTrue(source.is_executable('file1-id'))
1993
4826.1.5 by Andrew Bennetts
Add test that content filtered files are not hardlinked by build_tree.
1994
    def install_rot13_content_filter(self, pattern):
4985.2.1 by Vincent Ladeuil
Deploy addAttrCleanup on the whole test suite.
1995
        # We could use
1996
        # self.addCleanup(filters._reset_registry, filters._reset_registry())
1997
        # below, but that looks a bit... hard to read even if it's exactly
1998
        # the same thing.
4826.1.5 by Andrew Bennetts
Add test that content filtered files are not hardlinked by build_tree.
1999
        original_registry = filters._reset_registry()
2000
        def restore_registry():
2001
            filters._reset_registry(original_registry)
2002
        self.addCleanup(restore_registry)
2003
        def rot13(chunks, context=None):
2004
            return [''.join(chunks).encode('rot13')]
2005
        rot13filter = filters.ContentFilter(rot13, rot13)
2006
        filters.register_filter_stack_map('rot13', {'yes': [rot13filter]}.get)
2007
        os.mkdir(self.test_home_dir + '/.bazaar')
2008
        rules_filename = self.test_home_dir + '/.bazaar/rules'
4826.1.8 by Andrew Bennetts
Tweaks suggested by John.
2009
        f = open(rules_filename, 'wb')
4826.1.5 by Andrew Bennetts
Add test that content filtered files are not hardlinked by build_tree.
2010
        f.write('[name %s]\nrot13=yes\n' % (pattern,))
2011
        f.close()
2012
        def uninstall_rules():
2013
            os.remove(rules_filename)
2014
            rules.reset_rules()
2015
        self.addCleanup(uninstall_rules)
2016
        rules.reset_rules()
2017
2018
    def test_build_tree_content_filtered_files_are_not_hardlinked(self):
2019
        """build_tree will not hardlink files that have content filtering rules
2020
        applied to them (but will still hardlink other files from the same tree
2021
        if it can).
2022
        """
2023
        self.requireFeature(HardlinkFeature)
2024
        self.install_rot13_content_filter('file1')
2025
        source = self.create_ab_tree()
2026
        target = self.make_branch_and_tree('target')
2027
        revision_tree = source.basis_tree()
2028
        revision_tree.lock_read()
2029
        self.addCleanup(revision_tree.unlock)
2030
        build_tree(revision_tree, target, source, hardlink=True)
2031
        target.lock_read()
2032
        self.addCleanup(target.unlock)
2033
        self.assertEqual([], list(target.iter_changes(revision_tree)))
2034
        source_stat = os.stat('source/file1')
2035
        target_stat = os.stat('target/file1')
2036
        self.assertNotEqual(source_stat, target_stat)
2037
        source_stat = os.stat('source/file2')
2038
        target_stat = os.stat('target/file2')
4826.1.8 by Andrew Bennetts
Tweaks suggested by John.
2039
        self.assertEqualStat(source_stat, target_stat)
4826.1.5 by Andrew Bennetts
Add test that content filtered files are not hardlinked by build_tree.
2040
3453.2.4 by Aaron Bentley
Disable fast-path when conflicts are encountered
2041
    def test_case_insensitive_build_tree_inventory(self):
4241.14.12 by Vincent Ladeuil
Far too many modifications for a single commit, need to restart.
2042
        if (tests.CaseInsensitiveFilesystemFeature.available()
2043
            or tests.CaseInsCasePresFilenameFeature.available()):
4241.9.4 by Vincent Ladeuil
Fix test_case_insensitive_build_tree_inventory failure on OSX.
2044
            raise tests.UnavailableFeature('Fully case sensitive filesystem')
3453.2.4 by Aaron Bentley
Disable fast-path when conflicts are encountered
2045
        source = self.make_branch_and_tree('source')
2046
        self.build_tree(['source/file', 'source/FILE'])
2047
        source.add(['file', 'FILE'], ['lower-id', 'upper-id'])
2048
        source.commit('added files')
2049
        # Don't try this at home, kids!
2050
        # Force the tree to report that it is case insensitive
2051
        target = self.make_branch_and_tree('target')
2052
        target.case_sensitive = False
3453.2.6 by Aaron Bentley
Rename mutate_tree to delta_from_tree, add comment
2053
        build_tree(source.basis_tree(), target, source, delta_from_tree=True)
3453.2.4 by Aaron Bentley
Disable fast-path when conflicts are encountered
2054
        self.assertEqual('file.moved', target.id2path('lower-id'))
2055
        self.assertEqual('FILE', target.id2path('upper-id'))
2056
1966.1.1 by Aaron Bentley
Implement disk-content merge and conflict resolution for build_tree
2057
4526.8.1 by Aaron Bentley
Support committing a TreeTransform to a branch.
2058
class TestCommitTransform(tests.TestCaseWithTransport):
2059
2060
    def get_branch(self):
2061
        tree = self.make_branch_and_tree('tree')
2062
        tree.lock_write()
2063
        self.addCleanup(tree.unlock)
2064
        tree.commit('empty commit')
2065
        return tree.branch
2066
2067
    def get_branch_and_transform(self):
2068
        branch = self.get_branch()
2069
        tt = TransformPreview(branch.basis_tree())
2070
        self.addCleanup(tt.finalize)
2071
        return branch, tt
2072
2073
    def test_commit_wrong_basis(self):
2074
        branch = self.get_branch()
2075
        basis = branch.repository.revision_tree(
2076
            _mod_revision.NULL_REVISION)
2077
        tt = TransformPreview(basis)
2078
        self.addCleanup(tt.finalize)
4526.8.5 by Aaron Bentley
Updates from review.
2079
        e = self.assertRaises(ValueError, tt.commit, branch, '')
4526.8.1 by Aaron Bentley
Support committing a TreeTransform to a branch.
2080
        self.assertEqual('TreeTransform not based on branch basis: null:',
2081
                         str(e))
2082
2083
    def test_empy_commit(self):
2084
        branch, tt = self.get_branch_and_transform()
2085
        rev = tt.commit(branch, 'my message')
2086
        self.assertEqual(2, branch.revno())
2087
        repo = branch.repository
2088
        self.assertEqual('my message', repo.get_revision(rev).message)
2089
2090
    def test_merge_parents(self):
2091
        branch, tt = self.get_branch_and_transform()
2092
        rev = tt.commit(branch, 'my message', ['rev1b', 'rev1c'])
2093
        self.assertEqual(['rev1b', 'rev1c'],
2094
                         branch.basis_tree().get_parent_ids()[1:])
2095
4526.8.3 by Aaron Bentley
Clean up merge parent handling.
2096
    def test_first_commit(self):
2097
        branch = self.make_branch('branch')
2098
        branch.lock_write()
2099
        self.addCleanup(branch.unlock)
2100
        tt = TransformPreview(branch.basis_tree())
4659.2.4 by Vincent Ladeuil
Cleanup remaining bzr-limbo-XXXXXX leaks in /tmp during selftest.
2101
        self.addCleanup(tt.finalize)
4526.9.23 by Robert Collins
Change the tree transform test_first_commit test to set a root id in the new tree, and workaround an apparent bug in TreeTransform._determine_path.
2102
        tt.new_directory('', ROOT_PARENT, 'TREE_ROOT')
4526.8.3 by Aaron Bentley
Clean up merge parent handling.
2103
        rev = tt.commit(branch, 'my message')
2104
        self.assertEqual([], branch.basis_tree().get_parent_ids())
4526.8.5 by Aaron Bentley
Updates from review.
2105
        self.assertNotEqual(_mod_revision.NULL_REVISION,
2106
                            branch.last_revision())
4526.8.3 by Aaron Bentley
Clean up merge parent handling.
2107
4526.8.5 by Aaron Bentley
Updates from review.
2108
    def test_first_commit_with_merge_parents(self):
4526.8.3 by Aaron Bentley
Clean up merge parent handling.
2109
        branch = self.make_branch('branch')
2110
        branch.lock_write()
2111
        self.addCleanup(branch.unlock)
2112
        tt = TransformPreview(branch.basis_tree())
4659.2.4 by Vincent Ladeuil
Cleanup remaining bzr-limbo-XXXXXX leaks in /tmp during selftest.
2113
        self.addCleanup(tt.finalize)
4526.8.5 by Aaron Bentley
Updates from review.
2114
        e = self.assertRaises(ValueError, tt.commit, branch,
4526.8.3 by Aaron Bentley
Clean up merge parent handling.
2115
                          'my message', ['rev1b-id'])
4526.8.5 by Aaron Bentley
Updates from review.
2116
        self.assertEqual('Cannot supply merge parents for first commit.',
2117
                         str(e))
2118
        self.assertEqual(_mod_revision.NULL_REVISION, branch.last_revision())
4526.8.3 by Aaron Bentley
Clean up merge parent handling.
2119
4526.8.1 by Aaron Bentley
Support committing a TreeTransform to a branch.
2120
    def test_add_files(self):
2121
        branch, tt = self.get_branch_and_transform()
2122
        tt.new_file('file', tt.root, 'contents', 'file-id')
2123
        trans_id = tt.new_directory('dir', tt.root, 'dir-id')
4789.25.2 by John Arbash Meinel
Fix 3 tests that meant to skip exec or symlink testing on win32
2124
        if SymlinkFeature.available():
2125
            tt.new_symlink('symlink', trans_id, 'target', 'symlink-id')
4526.8.1 by Aaron Bentley
Support committing a TreeTransform to a branch.
2126
        rev = tt.commit(branch, 'message')
2127
        tree = branch.basis_tree()
2128
        self.assertEqual('file', tree.id2path('file-id'))
2129
        self.assertEqual('contents', tree.get_file_text('file-id'))
2130
        self.assertEqual('dir', tree.id2path('dir-id'))
4789.25.2 by John Arbash Meinel
Fix 3 tests that meant to skip exec or symlink testing on win32
2131
        if SymlinkFeature.available():
2132
            self.assertEqual('dir/symlink', tree.id2path('symlink-id'))
2133
            self.assertEqual('target', tree.get_symlink_target('symlink-id'))
4526.8.1 by Aaron Bentley
Support committing a TreeTransform to a branch.
2134
4526.8.2 by Aaron Bentley
Proved strict commit handling.
2135
    def test_add_unversioned(self):
2136
        branch, tt = self.get_branch_and_transform()
2137
        tt.new_file('file', tt.root, 'contents')
2138
        self.assertRaises(errors.StrictCommitFailed, tt.commit, branch,
2139
                          'message', strict=True)
2140
2141
    def test_modify_strict(self):
2142
        branch, tt = self.get_branch_and_transform()
2143
        tt.new_file('file', tt.root, 'contents', 'file-id')
2144
        tt.commit(branch, 'message', strict=True)
2145
        tt = TransformPreview(branch.basis_tree())
4659.2.4 by Vincent Ladeuil
Cleanup remaining bzr-limbo-XXXXXX leaks in /tmp during selftest.
2146
        self.addCleanup(tt.finalize)
4526.8.2 by Aaron Bentley
Proved strict commit handling.
2147
        trans_id = tt.trans_id_file_id('file-id')
2148
        tt.delete_contents(trans_id)
2149
        tt.create_file('contents', trans_id)
2150
        tt.commit(branch, 'message', strict=True)
2151
4526.8.6 by Aaron Bentley
Check for malformed transforms before committing.
2152
    def test_commit_malformed(self):
2153
        """Committing a malformed transform should raise an exception.
2154
2155
        In this case, we are adding a file without adding its parent.
2156
        """
2157
        branch, tt = self.get_branch_and_transform()
2158
        parent_id = tt.trans_id_file_id('parent-id')
2159
        tt.new_file('file', parent_id, 'contents', 'file-id')
2160
        self.assertRaises(errors.MalformedTransform, tt.commit, branch,
2161
                          'message')
2162
5162.4.1 by Aaron Bentley
TreeTransform supports normal commit parameters and includes branch nick.
2163
    def test_commit_rich_revision_data(self):
2164
        branch, tt = self.get_branch_and_transform()
5162.4.3 by Aaron Bentley
Fix failing test.
2165
        rev_id = tt.commit(branch, 'message', timestamp=1, timezone=43201,
5162.4.1 by Aaron Bentley
TreeTransform supports normal commit parameters and includes branch nick.
2166
                           committer='me <me@example.com>',
2167
                           revprops={'foo': 'bar'}, revision_id='revid-1',
2168
                           authors=['Author1 <author1@example.com>',
2169
                              'Author2 <author2@example.com>',
2170
                               ])
2171
        self.assertEqual('revid-1', rev_id)
2172
        revision = branch.repository.get_revision(rev_id)
2173
        self.assertEqual(1, revision.timestamp)
5162.4.3 by Aaron Bentley
Fix failing test.
2174
        self.assertEqual(43201, revision.timezone)
5162.4.1 by Aaron Bentley
TreeTransform supports normal commit parameters and includes branch nick.
2175
        self.assertEqual('me <me@example.com>', revision.committer)
2176
        self.assertEqual(['Author1 <author1@example.com>',
2177
                          'Author2 <author2@example.com>'],
2178
                         revision.get_apparent_authors())
2179
        del revision.properties['authors']
2180
        self.assertEqual({'foo': 'bar',
2181
                          'branch-nick': 'tree'},
2182
                         revision.properties)
2183
2184
    def test_no_explicit_revprops(self):
2185
        branch, tt = self.get_branch_and_transform()
2186
        rev_id = tt.commit(branch, 'message', authors=[
2187
            'Author1 <author1@example.com>',
2188
            'Author2 <author2@example.com>', ])
2189
        revision = branch.repository.get_revision(rev_id)
2190
        self.assertEqual(['Author1 <author1@example.com>',
2191
                          'Author2 <author2@example.com>'],
2192
                         revision.get_apparent_authors())
2193
        self.assertEqual('tree', revision.properties['branch-nick'])
2194
4526.8.1 by Aaron Bentley
Support committing a TreeTransform to a branch.
2195
1534.10.28 by Aaron Bentley
Use numbered backup files
2196
class MockTransform(object):
2197
2198
    def has_named_child(self, by_parent, parent_id, name):
2199
        for child_id in by_parent[parent_id]:
2200
            if child_id == '0':
2201
                if name == "name~":
2202
                    return True
1534.10.29 by Aaron Bentley
Fixed backup numbering to match GNU standard better
2203
            elif name == "name.~%s~" % child_id:
1534.10.28 by Aaron Bentley
Use numbered backup files
2204
                return True
2205
        return False
2206
2502.1.1 by Aaron Bentley
Ensure renames only root children are renamed when building trees
2207
1534.10.28 by Aaron Bentley
Use numbered backup files
2208
class MockEntry(object):
2209
    def __init__(self):
2210
        object.__init__(self)
2211
        self.name = "name"
2212
3063.1.1 by Alexander Belchenko
Catch OSError 17 (file exists) in final phase of tree transform and show filename to user (#111758).
2213
1534.10.28 by Aaron Bentley
Use numbered backup files
2214
class TestGetBackupName(TestCase):
2215
    def test_get_backup_name(self):
2216
        tt = MockTransform()
2217
        name = get_backup_name(MockEntry(), {'a':[]}, 'a', tt)
1534.10.29 by Aaron Bentley
Fixed backup numbering to match GNU standard better
2218
        self.assertEqual(name, 'name.~1~')
2219
        name = get_backup_name(MockEntry(), {'a':['1']}, 'a', tt)
2220
        self.assertEqual(name, 'name.~2~')
1534.10.28 by Aaron Bentley
Use numbered backup files
2221
        name = get_backup_name(MockEntry(), {'a':['2']}, 'a', tt)
1534.10.29 by Aaron Bentley
Fixed backup numbering to match GNU standard better
2222
        self.assertEqual(name, 'name.~1~')
1534.10.28 by Aaron Bentley
Use numbered backup files
2223
        name = get_backup_name(MockEntry(), {'a':['2'], 'b':[]}, 'b', tt)
1534.10.29 by Aaron Bentley
Fixed backup numbering to match GNU standard better
2224
        self.assertEqual(name, 'name.~1~')
2225
        name = get_backup_name(MockEntry(), {'a':['1', '2', '3']}, 'a', tt)
2226
        self.assertEqual(name, 'name.~4~')
2733.2.1 by Aaron Bentley
Implement FileMover, to support TreeTransform rollback
2227
2228
2229
class TestFileMover(tests.TestCaseWithTransport):
2230
2231
    def test_file_mover(self):
2232
        self.build_tree(['a/', 'a/b', 'c/', 'c/d'])
2233
        mover = _FileMover()
2234
        mover.rename('a', 'q')
2235
        self.failUnlessExists('q')
2236
        self.failIfExists('a')
2733.2.12 by Aaron Bentley
Updates from review
2237
        self.failUnlessExists('q/b')
2238
        self.failUnlessExists('c')
2239
        self.failUnlessExists('c/d')
2733.2.1 by Aaron Bentley
Implement FileMover, to support TreeTransform rollback
2240
2733.2.5 by Aaron Bentley
Implement FileMover.pre_delete and FileMover.apply_deletions
2241
    def test_pre_delete_rollback(self):
2242
        self.build_tree(['a/'])
2243
        mover = _FileMover()
2244
        mover.pre_delete('a', 'q')
2245
        self.failUnlessExists('q')
2246
        self.failIfExists('a')
2247
        mover.rollback()
2248
        self.failIfExists('q')
2249
        self.failUnlessExists('a')
2250
2251
    def test_apply_deletions(self):
2733.2.12 by Aaron Bentley
Updates from review
2252
        self.build_tree(['a/', 'b/'])
2733.2.5 by Aaron Bentley
Implement FileMover.pre_delete and FileMover.apply_deletions
2253
        mover = _FileMover()
2254
        mover.pre_delete('a', 'q')
2733.2.12 by Aaron Bentley
Updates from review
2255
        mover.pre_delete('b', 'r')
2733.2.5 by Aaron Bentley
Implement FileMover.pre_delete and FileMover.apply_deletions
2256
        self.failUnlessExists('q')
2733.2.12 by Aaron Bentley
Updates from review
2257
        self.failUnlessExists('r')
2733.2.5 by Aaron Bentley
Implement FileMover.pre_delete and FileMover.apply_deletions
2258
        self.failIfExists('a')
2733.2.12 by Aaron Bentley
Updates from review
2259
        self.failIfExists('b')
2733.2.5 by Aaron Bentley
Implement FileMover.pre_delete and FileMover.apply_deletions
2260
        mover.apply_deletions()
2261
        self.failIfExists('q')
2733.2.12 by Aaron Bentley
Updates from review
2262
        self.failIfExists('r')
2733.2.5 by Aaron Bentley
Implement FileMover.pre_delete and FileMover.apply_deletions
2263
        self.failIfExists('a')
2733.2.12 by Aaron Bentley
Updates from review
2264
        self.failIfExists('b')
2733.2.5 by Aaron Bentley
Implement FileMover.pre_delete and FileMover.apply_deletions
2265
2733.2.1 by Aaron Bentley
Implement FileMover, to support TreeTransform rollback
2266
    def test_file_mover_rollback(self):
2267
        self.build_tree(['a/', 'a/b', 'c/', 'c/d/', 'c/e/'])
2268
        mover = _FileMover()
2269
        mover.rename('c/d', 'c/f')
2270
        mover.rename('c/e', 'c/d')
2271
        try:
2272
            mover.rename('a', 'c')
3063.1.3 by Aaron Bentley
Update for Linux
2273
        except errors.FileExists, e:
2733.2.1 by Aaron Bentley
Implement FileMover, to support TreeTransform rollback
2274
            mover.rollback()
2275
        self.failUnlessExists('a')
2276
        self.failUnlessExists('c/d')
2733.2.3 by Aaron Bentley
Test tranform rollback
2277
2278
2279
class Bogus(Exception):
2280
    pass
2281
2282
2283
class TestTransformRollback(tests.TestCaseWithTransport):
2284
2285
    class ExceptionFileMover(_FileMover):
2286
2733.2.4 by Aaron Bentley
Test transform rollback when renaming into place
2287
        def __init__(self, bad_source=None, bad_target=None):
2288
            _FileMover.__init__(self)
2289
            self.bad_source = bad_source
2290
            self.bad_target = bad_target
2291
2733.2.3 by Aaron Bentley
Test tranform rollback
2292
        def rename(self, source, target):
2733.2.4 by Aaron Bentley
Test transform rollback when renaming into place
2293
            if (self.bad_source is not None and
2294
                source.endswith(self.bad_source)):
2295
                raise Bogus
2296
            elif (self.bad_target is not None and
2297
                target.endswith(self.bad_target)):
2733.2.3 by Aaron Bentley
Test tranform rollback
2298
                raise Bogus
2299
            else:
2300
                _FileMover.rename(self, source, target)
2301
2302
    def test_rollback_rename(self):
2303
        tree = self.make_branch_and_tree('.')
2304
        self.build_tree(['a/', 'a/b'])
2305
        tt = TreeTransform(tree)
2306
        self.addCleanup(tt.finalize)
2307
        a_id = tt.trans_id_tree_path('a')
2308
        tt.adjust_path('c', tt.root, a_id)
2309
        tt.adjust_path('d', a_id, tt.trans_id_tree_path('a/b'))
2733.2.4 by Aaron Bentley
Test transform rollback when renaming into place
2310
        self.assertRaises(Bogus, tt.apply,
2311
                          _mover=self.ExceptionFileMover(bad_source='a'))
2312
        self.failUnlessExists('a')
2313
        self.failUnlessExists('a/b')
2314
        tt.apply()
2315
        self.failUnlessExists('c')
2316
        self.failUnlessExists('c/d')
2317
2318
    def test_rollback_rename_into_place(self):
2319
        tree = self.make_branch_and_tree('.')
2320
        self.build_tree(['a/', 'a/b'])
2321
        tt = TreeTransform(tree)
2322
        self.addCleanup(tt.finalize)
2323
        a_id = tt.trans_id_tree_path('a')
2324
        tt.adjust_path('c', tt.root, a_id)
2325
        tt.adjust_path('d', a_id, tt.trans_id_tree_path('a/b'))
2326
        self.assertRaises(Bogus, tt.apply,
2327
                          _mover=self.ExceptionFileMover(bad_target='c/d'))
2328
        self.failUnlessExists('a')
2329
        self.failUnlessExists('a/b')
2330
        tt.apply()
2331
        self.failUnlessExists('c')
2332
        self.failUnlessExists('c/d')
2733.2.6 by Aaron Bentley
Make TreeTransform commits rollbackable
2333
2334
    def test_rollback_deletion(self):
2335
        tree = self.make_branch_and_tree('.')
2336
        self.build_tree(['a/', 'a/b'])
2337
        tt = TreeTransform(tree)
2338
        self.addCleanup(tt.finalize)
2339
        a_id = tt.trans_id_tree_path('a')
2340
        tt.delete_contents(a_id)
2341
        tt.adjust_path('d', tt.root, tt.trans_id_tree_path('a/b'))
2342
        self.assertRaises(Bogus, tt.apply,
2343
                          _mover=self.ExceptionFileMover(bad_target='d'))
2344
        self.failUnlessExists('a')
2345
        self.failUnlessExists('a/b')
3008.1.1 by Aaron Bentley
Start work allowing previews of transforms
2346
2347
3363.19.1 by Aaron Bentley
Make PreviewTree.iter_changes accept all options.
2348
A_ENTRY = ('a-id', ('a', 'a'), True, (True, True),
2349
                  ('TREE_ROOT', 'TREE_ROOT'), ('a', 'a'), ('file', 'file'),
2350
                  (False, False))
2351
ROOT_ENTRY = ('TREE_ROOT', ('', ''), False, (True, True), (None, None),
2352
              ('', ''), ('directory', 'directory'), (False, None))
2353
2354
3008.1.1 by Aaron Bentley
Start work allowing previews of transforms
2355
class TestTransformPreview(tests.TestCaseWithTransport):
2356
2357
    def create_tree(self):
2358
        tree = self.make_branch_and_tree('.')
2359
        self.build_tree_contents([('a', 'content 1')])
4600.3.1 by Robert Collins
Set tree root ID in tree transform tests that don't care about the root id.
2360
        tree.set_root_id('TREE_ROOT')
3008.2.1 by Aaron Bentley
Ensure conflict resolution works
2361
        tree.add('a', 'a-id')
3008.1.1 by Aaron Bentley
Start work allowing previews of transforms
2362
        tree.commit('rev1', rev_id='rev1')
2363
        return tree.branch.repository.revision_tree('rev1')
2364
3008.1.18 by Aaron Bentley
Get supported PreviewTree functionality under test
2365
    def get_empty_preview(self):
2366
        repository = self.make_repository('repo')
2367
        tree = repository.revision_tree(_mod_revision.NULL_REVISION)
3199.1.4 by Vincent Ladeuil
Fix 16 leaked tmp dirs. Probably indicates a lock handling problem with TransformPreview
2368
        preview = TransformPreview(tree)
3199.1.5 by Vincent Ladeuil
Fix two more leaking tmp dirs, by reworking TransformPreview lock handling.
2369
        self.addCleanup(preview.finalize)
3199.1.4 by Vincent Ladeuil
Fix 16 leaked tmp dirs. Probably indicates a lock handling problem with TransformPreview
2370
        return preview
2371
3008.1.1 by Aaron Bentley
Start work allowing previews of transforms
2372
    def test_transform_preview(self):
2373
        revision_tree = self.create_tree()
2374
        preview = TransformPreview(revision_tree)
3199.1.5 by Vincent Ladeuil
Fix two more leaking tmp dirs, by reworking TransformPreview lock handling.
2375
        self.addCleanup(preview.finalize)
3008.1.1 by Aaron Bentley
Start work allowing previews of transforms
2376
2377
    def test_transform_preview_tree(self):
2378
        revision_tree = self.create_tree()
2379
        preview = TransformPreview(revision_tree)
3199.1.5 by Vincent Ladeuil
Fix two more leaking tmp dirs, by reworking TransformPreview lock handling.
2380
        self.addCleanup(preview.finalize)
3008.1.1 by Aaron Bentley
Start work allowing previews of transforms
2381
        preview.get_preview_tree()
2382
3008.1.5 by Michael Hudson
a more precise test
2383
    def test_transform_new_file(self):
2384
        revision_tree = self.create_tree()
2385
        preview = TransformPreview(revision_tree)
3199.1.5 by Vincent Ladeuil
Fix two more leaking tmp dirs, by reworking TransformPreview lock handling.
2386
        self.addCleanup(preview.finalize)
3008.1.5 by Michael Hudson
a more precise test
2387
        preview.new_file('file2', preview.root, 'content B\n', 'file2-id')
2388
        preview_tree = preview.get_preview_tree()
2389
        self.assertEqual(preview_tree.kind('file2-id'), 'file')
2390
        self.assertEqual(
2391
            preview_tree.get_file('file2-id').read(), 'content B\n')
2392
3008.1.1 by Aaron Bentley
Start work allowing previews of transforms
2393
    def test_diff_preview_tree(self):
2394
        revision_tree = self.create_tree()
2395
        preview = TransformPreview(revision_tree)
3199.1.5 by Vincent Ladeuil
Fix two more leaking tmp dirs, by reworking TransformPreview lock handling.
2396
        self.addCleanup(preview.finalize)
3008.1.4 by Michael Hudson
Merge test enhancements
2397
        preview.new_file('file2', preview.root, 'content B\n', 'file2-id')
3008.1.1 by Aaron Bentley
Start work allowing previews of transforms
2398
        preview_tree = preview.get_preview_tree()
2399
        out = StringIO()
2400
        show_diff_trees(revision_tree, preview_tree, out)
3008.1.4 by Michael Hudson
Merge test enhancements
2401
        lines = out.getvalue().splitlines()
2402
        self.assertEqual(lines[0], "=== added file 'file2'")
2403
        # 3 lines of diff administrivia
2404
        self.assertEqual(lines[4], "+content B")
3008.2.1 by Aaron Bentley
Ensure conflict resolution works
2405
2406
    def test_transform_conflicts(self):
2407
        revision_tree = self.create_tree()
2408
        preview = TransformPreview(revision_tree)
3199.1.5 by Vincent Ladeuil
Fix two more leaking tmp dirs, by reworking TransformPreview lock handling.
2409
        self.addCleanup(preview.finalize)
3008.2.1 by Aaron Bentley
Ensure conflict resolution works
2410
        preview.new_file('a', preview.root, 'content 2')
2411
        resolve_conflicts(preview)
2412
        trans_id = preview.trans_id_file_id('a-id')
2413
        self.assertEqual('a.moved', preview.final_name(trans_id))
3008.1.17 by Aaron Bentley
Test unused parameters of preview_tree._iter_changes
2414
3008.1.31 by Aaron Bentley
Split PreviewTree._iter_changes parameter tests into smaller tests
2415
    def get_tree_and_preview_tree(self):
3008.1.17 by Aaron Bentley
Test unused parameters of preview_tree._iter_changes
2416
        revision_tree = self.create_tree()
2417
        preview = TransformPreview(revision_tree)
3199.1.5 by Vincent Ladeuil
Fix two more leaking tmp dirs, by reworking TransformPreview lock handling.
2418
        self.addCleanup(preview.finalize)
3008.1.17 by Aaron Bentley
Test unused parameters of preview_tree._iter_changes
2419
        a_trans_id = preview.trans_id_file_id('a-id')
2420
        preview.delete_contents(a_trans_id)
2421
        preview.create_file('b content', a_trans_id)
2422
        preview_tree = preview.get_preview_tree()
3008.1.31 by Aaron Bentley
Split PreviewTree._iter_changes parameter tests into smaller tests
2423
        return revision_tree, preview_tree
2424
2425
    def test_iter_changes(self):
2426
        revision_tree, preview_tree = self.get_tree_and_preview_tree()
2427
        root = revision_tree.inventory.root.file_id
3008.1.17 by Aaron Bentley
Test unused parameters of preview_tree._iter_changes
2428
        self.assertEqual([('a-id', ('a', 'a'), True, (True, True),
2429
                          (root, root), ('a', 'a'), ('file', 'file'),
2430
                          (False, False))],
3254.1.1 by Aaron Bentley
Make Tree.iter_changes a public method
2431
                          list(preview_tree.iter_changes(revision_tree)))
3008.1.31 by Aaron Bentley
Split PreviewTree._iter_changes parameter tests into smaller tests
2432
3363.19.1 by Aaron Bentley
Make PreviewTree.iter_changes accept all options.
2433
    def test_include_unchanged_succeeds(self):
3008.1.31 by Aaron Bentley
Split PreviewTree._iter_changes parameter tests into smaller tests
2434
        revision_tree, preview_tree = self.get_tree_and_preview_tree()
3363.19.1 by Aaron Bentley
Make PreviewTree.iter_changes accept all options.
2435
        changes = preview_tree.iter_changes(revision_tree,
2436
                                            include_unchanged=True)
2437
        root = revision_tree.inventory.root.file_id
2438
2439
        self.assertEqual([ROOT_ENTRY, A_ENTRY], list(changes))
3008.1.31 by Aaron Bentley
Split PreviewTree._iter_changes parameter tests into smaller tests
2440
2441
    def test_specific_files(self):
2442
        revision_tree, preview_tree = self.get_tree_and_preview_tree()
3363.19.1 by Aaron Bentley
Make PreviewTree.iter_changes accept all options.
2443
        changes = preview_tree.iter_changes(revision_tree,
2444
                                            specific_files=[''])
2445
        self.assertEqual([ROOT_ENTRY, A_ENTRY], list(changes))
3008.1.31 by Aaron Bentley
Split PreviewTree._iter_changes parameter tests into smaller tests
2446
3363.19.1 by Aaron Bentley
Make PreviewTree.iter_changes accept all options.
2447
    def test_want_unversioned(self):
3008.1.31 by Aaron Bentley
Split PreviewTree._iter_changes parameter tests into smaller tests
2448
        revision_tree, preview_tree = self.get_tree_and_preview_tree()
3363.19.1 by Aaron Bentley
Make PreviewTree.iter_changes accept all options.
2449
        changes = preview_tree.iter_changes(revision_tree,
2450
                                            want_unversioned=True)
2451
        self.assertEqual([ROOT_ENTRY, A_ENTRY], list(changes))
3008.1.17 by Aaron Bentley
Test unused parameters of preview_tree._iter_changes
2452
3008.1.31 by Aaron Bentley
Split PreviewTree._iter_changes parameter tests into smaller tests
2453
    def test_ignore_extra_trees_no_specific_files(self):
3008.1.17 by Aaron Bentley
Test unused parameters of preview_tree._iter_changes
2454
        # extra_trees is harmless without specific_files, so we'll silently
2455
        # accept it, even though we won't use it.
3008.1.31 by Aaron Bentley
Split PreviewTree._iter_changes parameter tests into smaller tests
2456
        revision_tree, preview_tree = self.get_tree_and_preview_tree()
3254.1.1 by Aaron Bentley
Make Tree.iter_changes a public method
2457
        preview_tree.iter_changes(revision_tree, extra_trees=[preview_tree])
3008.1.31 by Aaron Bentley
Split PreviewTree._iter_changes parameter tests into smaller tests
2458
2459
    def test_ignore_require_versioned_no_specific_files(self):
3008.1.17 by Aaron Bentley
Test unused parameters of preview_tree._iter_changes
2460
        # require_versioned is meaningless without specific_files.
3008.1.31 by Aaron Bentley
Split PreviewTree._iter_changes parameter tests into smaller tests
2461
        revision_tree, preview_tree = self.get_tree_and_preview_tree()
3254.1.1 by Aaron Bentley
Make Tree.iter_changes a public method
2462
        preview_tree.iter_changes(revision_tree, require_versioned=False)
3008.1.31 by Aaron Bentley
Split PreviewTree._iter_changes parameter tests into smaller tests
2463
2464
    def test_ignore_pb(self):
3008.1.17 by Aaron Bentley
Test unused parameters of preview_tree._iter_changes
2465
        # pb could be supported, but TT.iter_changes doesn't support it.
3008.1.31 by Aaron Bentley
Split PreviewTree._iter_changes parameter tests into smaller tests
2466
        revision_tree, preview_tree = self.get_tree_and_preview_tree()
4961.2.9 by Martin Pool
Rip out most remaining uses of DummyProgressBar
2467
        preview_tree.iter_changes(revision_tree)
3008.1.18 by Aaron Bentley
Get supported PreviewTree functionality under test
2468
2469
    def test_kind(self):
2470
        revision_tree = self.create_tree()
2471
        preview = TransformPreview(revision_tree)
3199.1.5 by Vincent Ladeuil
Fix two more leaking tmp dirs, by reworking TransformPreview lock handling.
2472
        self.addCleanup(preview.finalize)
3008.1.18 by Aaron Bentley
Get supported PreviewTree functionality under test
2473
        preview.new_file('file', preview.root, 'contents', 'file-id')
2474
        preview.new_directory('directory', preview.root, 'dir-id')
2475
        preview_tree = preview.get_preview_tree()
2476
        self.assertEqual('file', preview_tree.kind('file-id'))
2477
        self.assertEqual('directory', preview_tree.kind('dir-id'))
2478
2479
    def test_get_file_mtime(self):
2480
        preview = self.get_empty_preview()
2481
        file_trans_id = preview.new_file('file', preview.root, 'contents',
2482
                                         'file-id')
2483
        limbo_path = preview._limbo_name(file_trans_id)
2484
        preview_tree = preview.get_preview_tree()
2485
        self.assertEqual(os.stat(limbo_path).st_mtime,
2486
                         preview_tree.get_file_mtime('file-id'))
2487
4635.1.1 by Aaron Bentley
Fix OSError with renamed files in PreviewTree.
2488
    def test_get_file_mtime_renamed(self):
2489
        work_tree = self.make_branch_and_tree('tree')
2490
        self.build_tree(['tree/file'])
2491
        work_tree.add('file', 'file-id')
2492
        preview = TransformPreview(work_tree)
2493
        self.addCleanup(preview.finalize)
2494
        file_trans_id = preview.trans_id_tree_file_id('file-id')
2495
        preview.adjust_path('renamed', preview.root, file_trans_id)
2496
        preview_tree = preview.get_preview_tree()
2497
        preview_mtime = preview_tree.get_file_mtime('file-id', 'renamed')
2498
        work_mtime = work_tree.get_file_mtime('file-id', 'file')
2499
3008.1.18 by Aaron Bentley
Get supported PreviewTree functionality under test
2500
    def test_get_file(self):
2501
        preview = self.get_empty_preview()
2502
        preview.new_file('file', preview.root, 'contents', 'file-id')
2503
        preview_tree = preview.get_preview_tree()
2504
        tree_file = preview_tree.get_file('file-id')
2505
        try:
2506
            self.assertEqual('contents', tree_file.read())
2507
        finally:
2508
            tree_file.close()
3228.1.2 by James Henstridge
Simplify test, and move it down to be next to the other _PreviewTree tests.
2509
2510
    def test_get_symlink_target(self):
2511
        self.requireFeature(SymlinkFeature)
2512
        preview = self.get_empty_preview()
2513
        preview.new_symlink('symlink', preview.root, 'target', 'symlink-id')
2514
        preview_tree = preview.get_preview_tree()
2515
        self.assertEqual('target',
2516
                         preview_tree.get_symlink_target('symlink-id'))
3363.2.18 by Aaron Bentley
Implement correct all_file_ids for PreviewTree
2517
2518
    def test_all_file_ids(self):
2519
        tree = self.make_branch_and_tree('tree')
2520
        self.build_tree(['tree/a', 'tree/b', 'tree/c'])
2521
        tree.add(['a', 'b', 'c'], ['a-id', 'b-id', 'c-id'])
2522
        preview = TransformPreview(tree)
2523
        self.addCleanup(preview.finalize)
2524
        preview.unversion_file(preview.trans_id_file_id('b-id'))
2525
        c_trans_id = preview.trans_id_file_id('c-id')
2526
        preview.unversion_file(c_trans_id)
2527
        preview.version_file('c-id', c_trans_id)
2528
        preview_tree = preview.get_preview_tree()
2529
        self.assertEqual(set(['a-id', 'c-id', tree.get_root_id()]),
2530
                         preview_tree.all_file_ids())
3363.2.19 by Aaron Bentley
Make PreviewTree.path2id correct
2531
2532
    def test_path2id_deleted_unchanged(self):
2533
        tree = self.make_branch_and_tree('tree')
2534
        self.build_tree(['tree/unchanged', 'tree/deleted'])
2535
        tree.add(['unchanged', 'deleted'], ['unchanged-id', 'deleted-id'])
2536
        preview = TransformPreview(tree)
2537
        self.addCleanup(preview.finalize)
2538
        preview.unversion_file(preview.trans_id_file_id('deleted-id'))
2539
        preview_tree = preview.get_preview_tree()
2540
        self.assertEqual('unchanged-id', preview_tree.path2id('unchanged'))
2541
        self.assertIs(None, preview_tree.path2id('deleted'))
2542
2543
    def test_path2id_created(self):
2544
        tree = self.make_branch_and_tree('tree')
2545
        self.build_tree(['tree/unchanged'])
2546
        tree.add(['unchanged'], ['unchanged-id'])
2547
        preview = TransformPreview(tree)
2548
        self.addCleanup(preview.finalize)
2549
        preview.new_file('new', preview.trans_id_file_id('unchanged-id'),
2550
            'contents', 'new-id')
2551
        preview_tree = preview.get_preview_tree()
2552
        self.assertEqual('new-id', preview_tree.path2id('unchanged/new'))
2553
2554
    def test_path2id_moved(self):
2555
        tree = self.make_branch_and_tree('tree')
2556
        self.build_tree(['tree/old_parent/', 'tree/old_parent/child'])
2557
        tree.add(['old_parent', 'old_parent/child'],
2558
                 ['old_parent-id', 'child-id'])
2559
        preview = TransformPreview(tree)
2560
        self.addCleanup(preview.finalize)
2561
        new_parent = preview.new_directory('new_parent', preview.root,
2562
                                           'new_parent-id')
2563
        preview.adjust_path('child', new_parent,
2564
                            preview.trans_id_file_id('child-id'))
2565
        preview_tree = preview.get_preview_tree()
2566
        self.assertIs(None, preview_tree.path2id('old_parent/child'))
2567
        self.assertEqual('child-id', preview_tree.path2id('new_parent/child'))
2568
2569
    def test_path2id_renamed_parent(self):
2570
        tree = self.make_branch_and_tree('tree')
2571
        self.build_tree(['tree/old_name/', 'tree/old_name/child'])
2572
        tree.add(['old_name', 'old_name/child'],
2573
                 ['parent-id', 'child-id'])
2574
        preview = TransformPreview(tree)
2575
        self.addCleanup(preview.finalize)
2576
        preview.adjust_path('new_name', preview.root,
2577
                            preview.trans_id_file_id('parent-id'))
2578
        preview_tree = preview.get_preview_tree()
2579
        self.assertIs(None, preview_tree.path2id('old_name/child'))
2580
        self.assertEqual('child-id', preview_tree.path2id('new_name/child'))
3363.2.21 by Aaron Bentley
Implement iter_entries_by_dir
2581
2582
    def assertMatchingIterEntries(self, tt, specific_file_ids=None):
2583
        preview_tree = tt.get_preview_tree()
2584
        preview_result = list(preview_tree.iter_entries_by_dir(
2585
                              specific_file_ids))
2586
        tree = tt._tree
2587
        tt.apply()
2588
        actual_result = list(tree.iter_entries_by_dir(specific_file_ids))
2589
        self.assertEqual(actual_result, preview_result)
2590
2591
    def test_iter_entries_by_dir_new(self):
2592
        tree = self.make_branch_and_tree('tree')
2593
        tt = TreeTransform(tree)
2594
        tt.new_file('new', tt.root, 'contents', 'new-id')
2595
        self.assertMatchingIterEntries(tt)
2596
2597
    def test_iter_entries_by_dir_deleted(self):
2598
        tree = self.make_branch_and_tree('tree')
2599
        self.build_tree(['tree/deleted'])
2600
        tree.add('deleted', 'deleted-id')
2601
        tt = TreeTransform(tree)
2602
        tt.delete_contents(tt.trans_id_file_id('deleted-id'))
2603
        self.assertMatchingIterEntries(tt)
2604
2605
    def test_iter_entries_by_dir_unversioned(self):
2606
        tree = self.make_branch_and_tree('tree')
2607
        self.build_tree(['tree/removed'])
2608
        tree.add('removed', 'removed-id')
2609
        tt = TreeTransform(tree)
2610
        tt.unversion_file(tt.trans_id_file_id('removed-id'))
2611
        self.assertMatchingIterEntries(tt)
2612
2613
    def test_iter_entries_by_dir_moved(self):
2614
        tree = self.make_branch_and_tree('tree')
2615
        self.build_tree(['tree/moved', 'tree/new_parent/'])
2616
        tree.add(['moved', 'new_parent'], ['moved-id', 'new_parent-id'])
2617
        tt = TreeTransform(tree)
2618
        tt.adjust_path('moved', tt.trans_id_file_id('new_parent-id'),
2619
                       tt.trans_id_file_id('moved-id'))
2620
        self.assertMatchingIterEntries(tt)
2621
2622
    def test_iter_entries_by_dir_specific_file_ids(self):
2623
        tree = self.make_branch_and_tree('tree')
2624
        tree.set_root_id('tree-root-id')
2625
        self.build_tree(['tree/parent/', 'tree/parent/child'])
2626
        tree.add(['parent', 'parent/child'], ['parent-id', 'child-id'])
2627
        tt = TreeTransform(tree)
2628
        self.assertMatchingIterEntries(tt, ['tree-root-id', 'child-id'])
3363.2.26 by Aaron Bentley
Get symlinks working
2629
2630
    def test_symlink_content_summary(self):
2631
        self.requireFeature(SymlinkFeature)
2632
        preview = self.get_empty_preview()
2633
        preview.new_symlink('path', preview.root, 'target', 'path-id')
2634
        summary = preview.get_preview_tree().path_content_summary('path')
2635
        self.assertEqual(('symlink', None, None, 'target'), summary)
3363.2.27 by Aaron Bentley
Make path_content_summary a core API
2636
2637
    def test_missing_content_summary(self):
2638
        preview = self.get_empty_preview()
2639
        summary = preview.get_preview_tree().path_content_summary('path')
2640
        self.assertEqual(('missing', None, None, None), summary)
2641
2642
    def test_deleted_content_summary(self):
2643
        tree = self.make_branch_and_tree('tree')
2644
        self.build_tree(['tree/path/'])
2645
        tree.add('path')
2646
        preview = TransformPreview(tree)
2647
        self.addCleanup(preview.finalize)
2648
        preview.delete_contents(preview.trans_id_tree_path('path'))
2649
        summary = preview.get_preview_tree().path_content_summary('path')
2650
        self.assertEqual(('missing', None, None, None), summary)
2651
3363.2.30 by Aaron Bentley
Improve execute bit testing
2652
    def test_file_content_summary_executable(self):
2653
        preview = self.get_empty_preview()
2654
        path_id = preview.new_file('path', preview.root, 'contents', 'path-id')
2655
        preview.set_executability(True, path_id)
2656
        summary = preview.get_preview_tree().path_content_summary('path')
2657
        self.assertEqual(4, len(summary))
2658
        self.assertEqual('file', summary[0])
2659
        # size must be known
2660
        self.assertEqual(len('contents'), summary[1])
2661
        # executable
2662
        self.assertEqual(True, summary[2])
3363.2.31 by Aaron Bentley
Tweak tests
2663
        # will not have hash (not cheap to determine)
2664
        self.assertIs(None, summary[3])
3363.2.30 by Aaron Bentley
Improve execute bit testing
2665
2666
    def test_change_executability(self):
2667
        tree = self.make_branch_and_tree('tree')
2668
        self.build_tree(['tree/path'])
2669
        tree.add('path')
2670
        preview = TransformPreview(tree)
2671
        self.addCleanup(preview.finalize)
2672
        path_id = preview.trans_id_tree_path('path')
2673
        preview.set_executability(True, path_id)
2674
        summary = preview.get_preview_tree().path_content_summary('path')
2675
        self.assertEqual(True, summary[2])
2676
3363.2.27 by Aaron Bentley
Make path_content_summary a core API
2677
    def test_file_content_summary_non_exec(self):
2678
        preview = self.get_empty_preview()
2679
        preview.new_file('path', preview.root, 'contents', 'path-id')
2680
        summary = preview.get_preview_tree().path_content_summary('path')
2681
        self.assertEqual(4, len(summary))
2682
        self.assertEqual('file', summary[0])
2683
        # size must be known
3363.2.30 by Aaron Bentley
Improve execute bit testing
2684
        self.assertEqual(len('contents'), summary[1])
3363.2.27 by Aaron Bentley
Make path_content_summary a core API
2685
        # not executable
4789.15.3 by John Arbash Meinel
PreviewTree now returns False for exec bit properly.
2686
        self.assertEqual(False, summary[2])
3363.2.31 by Aaron Bentley
Tweak tests
2687
        # will not have hash (not cheap to determine)
2688
        self.assertIs(None, summary[3])
3363.2.27 by Aaron Bentley
Make path_content_summary a core API
2689
2690
    def test_dir_content_summary(self):
2691
        preview = self.get_empty_preview()
2692
        preview.new_directory('path', preview.root, 'path-id')
2693
        summary = preview.get_preview_tree().path_content_summary('path')
2694
        self.assertEqual(('directory', None, None, None), summary)
2695
2696
    def test_tree_content_summary(self):
2697
        preview = self.get_empty_preview()
2698
        path = preview.new_directory('path', preview.root, 'path-id')
2699
        preview.set_tree_reference('rev-1', path)
2700
        summary = preview.get_preview_tree().path_content_summary('path')
2701
        self.assertEqual(4, len(summary))
2702
        self.assertEqual('tree-reference', summary[0])
3363.2.33 by Aaron Bentley
Implement PreviewTree.annotate_iter
2703
2704
    def test_annotate(self):
2705
        tree = self.make_branch_and_tree('tree')
2706
        self.build_tree_contents([('tree/file', 'a\n')])
2707
        tree.add('file', 'file-id')
2708
        tree.commit('a', rev_id='one')
2709
        self.build_tree_contents([('tree/file', 'a\nb\n')])
2710
        preview = TransformPreview(tree)
2711
        self.addCleanup(preview.finalize)
2712
        file_trans_id = preview.trans_id_file_id('file-id')
2713
        preview.delete_contents(file_trans_id)
2714
        preview.create_file('a\nb\nc\n', file_trans_id)
2715
        preview_tree = preview.get_preview_tree()
2716
        expected = [
2717
            ('one', 'a\n'),
2718
            ('me:', 'b\n'),
2719
            ('me:', 'c\n'),
2720
        ]
2721
        annotation = preview_tree.annotate_iter('file-id', 'me:')
2722
        self.assertEqual(expected, annotation)
2723
2724
    def test_annotate_missing(self):
2725
        preview = self.get_empty_preview()
2726
        preview.new_file('file', preview.root, 'a\nb\nc\n', 'file-id')
2727
        preview_tree = preview.get_preview_tree()
2728
        expected = [
2729
            ('me:', 'a\n'),
2730
            ('me:', 'b\n'),
2731
            ('me:', 'c\n'),
2732
         ]
2733
        annotation = preview_tree.annotate_iter('file-id', 'me:')
2734
        self.assertEqual(expected, annotation)
2735
3363.7.3 by Aaron Bentley
Add test that annotate correctly handles renames
2736
    def test_annotate_rename(self):
2737
        tree = self.make_branch_and_tree('tree')
2738
        self.build_tree_contents([('tree/file', 'a\n')])
2739
        tree.add('file', 'file-id')
2740
        tree.commit('a', rev_id='one')
2741
        preview = TransformPreview(tree)
2742
        self.addCleanup(preview.finalize)
2743
        file_trans_id = preview.trans_id_file_id('file-id')
2744
        preview.adjust_path('newname', preview.root, file_trans_id)
2745
        preview_tree = preview.get_preview_tree()
2746
        expected = [
2747
            ('one', 'a\n'),
2748
        ]
2749
        annotation = preview_tree.annotate_iter('file-id', 'me:')
2750
        self.assertEqual(expected, annotation)
2751
3363.2.33 by Aaron Bentley
Implement PreviewTree.annotate_iter
2752
    def test_annotate_deleted(self):
2753
        tree = self.make_branch_and_tree('tree')
2754
        self.build_tree_contents([('tree/file', 'a\n')])
2755
        tree.add('file', 'file-id')
2756
        tree.commit('a', rev_id='one')
2757
        self.build_tree_contents([('tree/file', 'a\nb\n')])
2758
        preview = TransformPreview(tree)
2759
        self.addCleanup(preview.finalize)
2760
        file_trans_id = preview.trans_id_file_id('file-id')
2761
        preview.delete_contents(file_trans_id)
2762
        preview_tree = preview.get_preview_tree()
2763
        annotation = preview_tree.annotate_iter('file-id', 'me:')
2764
        self.assertIs(None, annotation)
2765
3363.2.36 by Aaron Bentley
Fix PreviewTree.stored_kind
2766
    def test_stored_kind(self):
2767
        preview = self.get_empty_preview()
2768
        preview.new_file('file', preview.root, 'a\nb\nc\n', 'file-id')
2769
        preview_tree = preview.get_preview_tree()
2770
        self.assertEqual('file', preview_tree.stored_kind('file-id'))
3363.2.37 by Aaron Bentley
Fix is_executable
2771
2772
    def test_is_executable(self):
2773
        preview = self.get_empty_preview()
2774
        preview.new_file('file', preview.root, 'a\nb\nc\n', 'file-id')
2775
        preview.set_executability(True, preview.trans_id_file_id('file-id'))
2776
        preview_tree = preview.get_preview_tree()
2777
        self.assertEqual(True, preview_tree.is_executable('file-id'))
3363.9.1 by Aaron Bentley
Implement plan_merge, refactoring various bits
2778
3571.1.1 by Aaron Bentley
Allow set/get of parent_ids in PreviewTree
2779
    def test_get_set_parent_ids(self):
2780
        revision_tree, preview_tree = self.get_tree_and_preview_tree()
2781
        self.assertEqual([], preview_tree.get_parent_ids())
2782
        preview_tree.set_parent_ids(['rev-1'])
2783
        self.assertEqual(['rev-1'], preview_tree.get_parent_ids())
2784
3363.9.1 by Aaron Bentley
Implement plan_merge, refactoring various bits
2785
    def test_plan_file_merge(self):
2786
        work_a = self.make_branch_and_tree('wta')
2787
        self.build_tree_contents([('wta/file', 'a\nb\nc\nd\n')])
2788
        work_a.add('file', 'file-id')
3363.9.7 by Aaron Bentley
Fix up to use set_parent_ids
2789
        base_id = work_a.commit('base version')
3363.9.1 by Aaron Bentley
Implement plan_merge, refactoring various bits
2790
        tree_b = work_a.bzrdir.sprout('wtb').open_workingtree()
2791
        preview = TransformPreview(work_a)
2792
        self.addCleanup(preview.finalize)
2793
        trans_id = preview.trans_id_file_id('file-id')
2794
        preview.delete_contents(trans_id)
2795
        preview.create_file('b\nc\nd\ne\n', trans_id)
2796
        self.build_tree_contents([('wtb/file', 'a\nc\nd\nf\n')])
2797
        tree_a = preview.get_preview_tree()
3363.9.7 by Aaron Bentley
Fix up to use set_parent_ids
2798
        tree_a.set_parent_ids([base_id])
3363.9.1 by Aaron Bentley
Implement plan_merge, refactoring various bits
2799
        self.assertEqual([
3363.9.5 by Aaron Bentley
Move killed-a from top to bottom
2800
            ('killed-a', 'a\n'),
3363.9.1 by Aaron Bentley
Implement plan_merge, refactoring various bits
2801
            ('killed-b', 'b\n'),
2802
            ('unchanged', 'c\n'),
2803
            ('unchanged', 'd\n'),
2804
            ('new-a', 'e\n'),
2805
            ('new-b', 'f\n'),
2806
        ], list(tree_a.plan_file_merge('file-id', tree_b)))
3363.9.8 by Aaron Bentley
Ensure plan_file_merge works with a RevisionTree as the basis
2807
2808
    def test_plan_file_merge_revision_tree(self):
2809
        work_a = self.make_branch_and_tree('wta')
2810
        self.build_tree_contents([('wta/file', 'a\nb\nc\nd\n')])
2811
        work_a.add('file', 'file-id')
2812
        base_id = work_a.commit('base version')
2813
        tree_b = work_a.bzrdir.sprout('wtb').open_workingtree()
2814
        preview = TransformPreview(work_a.basis_tree())
2815
        self.addCleanup(preview.finalize)
2816
        trans_id = preview.trans_id_file_id('file-id')
2817
        preview.delete_contents(trans_id)
2818
        preview.create_file('b\nc\nd\ne\n', trans_id)
2819
        self.build_tree_contents([('wtb/file', 'a\nc\nd\nf\n')])
2820
        tree_a = preview.get_preview_tree()
2821
        tree_a.set_parent_ids([base_id])
2822
        self.assertEqual([
2823
            ('killed-a', 'a\n'),
2824
            ('killed-b', 'b\n'),
2825
            ('unchanged', 'c\n'),
2826
            ('unchanged', 'd\n'),
2827
            ('new-a', 'e\n'),
2828
            ('new-b', 'f\n'),
2829
        ], list(tree_a.plan_file_merge('file-id', tree_b)))
3363.9.9 by Aaron Bentley
Implement walkdirs in terms of TreeTransform
2830
2831
    def test_walkdirs(self):
2832
        preview = self.get_empty_preview()
4634.57.3 by Aaron Bentley
Fix failing test.
2833
        root = preview.new_directory('', ROOT_PARENT, 'tree-root')
2834
        # FIXME: new_directory should mark root.
4634.122.6 by John Arbash Meinel
Fix a test that used 'adjust_path' to set the root.
2835
        preview.fixup_new_roots()
3363.9.9 by Aaron Bentley
Implement walkdirs in terms of TreeTransform
2836
        preview_tree = preview.get_preview_tree()
2837
        file_trans_id = preview.new_file('a', preview.root, 'contents',
2838
                                         'a-id')
2839
        expected = [(('', 'tree-root'),
2840
                    [('a', 'a', 'file', None, 'a-id', 'file')])]
2841
        self.assertEqual(expected, list(preview_tree.walkdirs()))
3363.13.2 by Aaron Bentley
Test specific cases for PreviewTree.extras
2842
2843
    def test_extras(self):
2844
        work_tree = self.make_branch_and_tree('tree')
2845
        self.build_tree(['tree/removed-file', 'tree/existing-file',
2846
                         'tree/not-removed-file'])
2847
        work_tree.add(['removed-file', 'not-removed-file'])
2848
        preview = TransformPreview(work_tree)
3363.13.3 by Aaron Bentley
Add cleanup
2849
        self.addCleanup(preview.finalize)
3363.13.2 by Aaron Bentley
Test specific cases for PreviewTree.extras
2850
        preview.new_file('new-file', preview.root, 'contents')
2851
        preview.new_file('new-versioned-file', preview.root, 'contents',
2852
                         'new-versioned-id')
2853
        tree = preview.get_preview_tree()
2854
        preview.unversion_file(preview.trans_id_tree_path('removed-file'))
2855
        self.assertEqual(set(['new-file', 'removed-file', 'existing-file']),
2856
                         set(tree.extras()))
3363.17.1 by Aaron Bentley
Avoid inventory for merge and transform code
2857
3363.17.2 by Aaron Bentley
Add text checking
2858
    def test_merge_into_preview(self):
3363.17.1 by Aaron Bentley
Avoid inventory for merge and transform code
2859
        work_tree = self.make_branch_and_tree('tree')
3363.17.2 by Aaron Bentley
Add text checking
2860
        self.build_tree_contents([('tree/file','b\n')])
2861
        work_tree.add('file', 'file-id')
3363.17.1 by Aaron Bentley
Avoid inventory for merge and transform code
2862
        work_tree.commit('first commit')
2863
        child_tree = work_tree.bzrdir.sprout('child').open_workingtree()
3363.17.2 by Aaron Bentley
Add text checking
2864
        self.build_tree_contents([('child/file','b\nc\n')])
3363.17.1 by Aaron Bentley
Avoid inventory for merge and transform code
2865
        child_tree.commit('child commit')
2866
        child_tree.lock_write()
2867
        self.addCleanup(child_tree.unlock)
2868
        work_tree.lock_write()
2869
        self.addCleanup(work_tree.unlock)
2870
        preview = TransformPreview(work_tree)
2871
        self.addCleanup(preview.finalize)
3363.17.6 by Aaron Bentley
Improve test scenario
2872
        file_trans_id = preview.trans_id_file_id('file-id')
2873
        preview.delete_contents(file_trans_id)
2874
        preview.create_file('a\nb\n', file_trans_id)
4634.57.2 by Aaron Bentley
Fix failing test.
2875
        preview_tree = preview.get_preview_tree()
4961.2.9 by Martin Pool
Rip out most remaining uses of DummyProgressBar
2876
        merger = Merger.from_revision_ids(None, preview_tree,
3363.17.1 by Aaron Bentley
Avoid inventory for merge and transform code
2877
                                          child_tree.branch.last_revision(),
2878
                                          other_branch=child_tree.branch,
2879
                                          tree_branch=work_tree.branch)
2880
        merger.merge_type = Merge3Merger
2881
        tt = merger.make_merger().make_preview_transform()
3363.17.2 by Aaron Bentley
Add text checking
2882
        self.addCleanup(tt.finalize)
2883
        final_tree = tt.get_preview_tree()
2884
        self.assertEqual('a\nb\nc\n', final_tree.get_file_text('file-id'))
3363.17.17 by Aaron Bentley
Start testing merging PreviewTree as OTHER
2885
2886
    def test_merge_preview_into_workingtree(self):
2887
        tree = self.make_branch_and_tree('tree')
4600.3.1 by Robert Collins
Set tree root ID in tree transform tests that don't care about the root id.
2888
        tree.set_root_id('TREE_ROOT')
3363.17.17 by Aaron Bentley
Start testing merging PreviewTree as OTHER
2889
        tt = TransformPreview(tree)
2890
        self.addCleanup(tt.finalize)
2891
        tt.new_file('name', tt.root, 'content', 'file-id')
2892
        tree2 = self.make_branch_and_tree('tree2')
4600.3.1 by Robert Collins
Set tree root ID in tree transform tests that don't care about the root id.
2893
        tree2.set_root_id('TREE_ROOT')
3363.17.17 by Aaron Bentley
Start testing merging PreviewTree as OTHER
2894
        merger = Merger.from_uncommitted(tree2, tt.get_preview_tree(),
4961.2.9 by Martin Pool
Rip out most remaining uses of DummyProgressBar
2895
                                         None, tree.basis_tree())
3363.17.17 by Aaron Bentley
Start testing merging PreviewTree as OTHER
2896
        merger.merge_type = Merge3Merger
2897
        merger.do_merge()
3363.17.18 by Aaron Bentley
Fix is_executable for PreviewTree
2898
3363.17.21 by Aaron Bentley
Conflicts are handled when merging from preview trees
2899
    def test_merge_preview_into_workingtree_handles_conflicts(self):
2900
        tree = self.make_branch_and_tree('tree')
2901
        self.build_tree_contents([('tree/foo', 'bar')])
2902
        tree.add('foo', 'foo-id')
2903
        tree.commit('foo')
2904
        tt = TransformPreview(tree)
2905
        self.addCleanup(tt.finalize)
2906
        trans_id = tt.trans_id_file_id('foo-id')
2907
        tt.delete_contents(trans_id)
2908
        tt.create_file('baz', trans_id)
2909
        tree2 = tree.bzrdir.sprout('tree2').open_workingtree()
2910
        self.build_tree_contents([('tree2/foo', 'qux')])
4961.2.9 by Martin Pool
Rip out most remaining uses of DummyProgressBar
2911
        pb = None
3363.17.21 by Aaron Bentley
Conflicts are handled when merging from preview trees
2912
        merger = Merger.from_uncommitted(tree2, tt.get_preview_tree(),
2913
                                         pb, tree.basis_tree())
2914
        merger.merge_type = Merge3Merger
2915
        merger.do_merge()
2916
3363.17.18 by Aaron Bentley
Fix is_executable for PreviewTree
2917
    def test_is_executable(self):
2918
        tree = self.make_branch_and_tree('tree')
2919
        preview = TransformPreview(tree)
2920
        self.addCleanup(preview.finalize)
2921
        preview.new_file('foo', preview.root, 'bar', 'baz-id')
2922
        preview_tree = preview.get_preview_tree()
2923
        self.assertEqual(False, preview_tree.is_executable('baz-id',
2924
                                                           'tree/foo'))
2925
        self.assertEqual(False, preview_tree.is_executable('baz-id'))
0.13.8 by Aaron Bentley
Integrate serialization into TreeTransforms
2926
4354.4.4 by Aaron Bentley
Simplify by using CommitBuilder directly
2927
    def test_commit_preview_tree(self):
2928
        tree = self.make_branch_and_tree('tree')
2929
        rev_id = tree.commit('rev1')
2930
        tree.branch.lock_write()
2931
        self.addCleanup(tree.branch.unlock)
2932
        tt = TransformPreview(tree)
2933
        tt.new_file('file', tt.root, 'contents', 'file_id')
2934
        self.addCleanup(tt.finalize)
2935
        preview = tt.get_preview_tree()
2936
        preview.set_parent_ids([rev_id])
2937
        builder = tree.branch.get_commit_builder([rev_id])
2938
        list(builder.record_iter_changes(preview, rev_id, tt.iter_changes()))
2939
        builder.finish_inventory()
2940
        rev2_id = builder.commit('rev2')
2941
        rev2_tree = tree.branch.repository.revision_tree(rev2_id)
2942
        self.assertEqual('contents', rev2_tree.get_file_text('file_id'))
2943
4634.79.1 by Aaron Bentley
TransformPreview uses ascii-only filenames.
2944
    def test_ascii_limbo_paths(self):
4634.79.2 by Aaron Bentley
Avoid runing test on non-unicode filesystems.
2945
        self.requireFeature(tests.UnicodeFilenameFeature)
4634.79.1 by Aaron Bentley
TransformPreview uses ascii-only filenames.
2946
        branch = self.make_branch('any')
2947
        tree = branch.repository.revision_tree(_mod_revision.NULL_REVISION)
2948
        tt = TransformPreview(tree)
4789.25.7 by John Arbash Meinel
We can run the executable tests.
2949
        self.addCleanup(tt.finalize)
4634.79.1 by Aaron Bentley
TransformPreview uses ascii-only filenames.
2950
        foo_id = tt.new_directory('', ROOT_PARENT)
2951
        bar_id = tt.new_file(u'\u1234bar', foo_id, 'contents')
2952
        limbo_path = tt._limbo_name(bar_id)
2953
        self.assertEqual(limbo_path.encode('ascii', 'replace'), limbo_path)
2954
0.13.8 by Aaron Bentley
Integrate serialization into TreeTransforms
2955
0.13.13 by Aaron Bentley
Add direct test of serialization records
2956
class FakeSerializer(object):
2957
    """Serializer implementation that simply returns the input.
2958
2959
    The input is returned in the order used by pack.ContainerPushParser.
2960
    """
2961
    @staticmethod
2962
    def bytes_record(bytes, names):
2963
        return names, bytes
2964
2965
0.13.8 by Aaron Bentley
Integrate serialization into TreeTransforms
2966
class TestSerializeTransform(tests.TestCaseWithTransport):
2967
0.13.22 by Aaron Bentley
More unicodeness for Shelf tests
2968
    _test_needs_features = [tests.UnicodeFilenameFeature]
2969
0.13.17 by Aaron Bentley
Convert roundtrip destruction test to serialization/deserialization pair
2970
    def get_preview(self, tree=None):
2971
        if tree is None:
2972
            tree = self.make_branch_and_tree('tree')
0.13.14 by Aaron Bentley
Add deserialization test, remove roundtrip test.
2973
        tt = TransformPreview(tree)
2974
        self.addCleanup(tt.finalize)
2975
        return tt
2976
0.13.18 by Aaron Bentley
Finish converting tests to direct serialize/deserialize tests, clean up
2977
    def assertSerializesTo(self, expected, tt):
2978
        records = list(tt.serialize(FakeSerializer()))
2979
        self.assertEqual(expected, records)
0.13.8 by Aaron Bentley
Integrate serialization into TreeTransforms
2980
0.13.13 by Aaron Bentley
Add direct test of serialization records
2981
    @staticmethod
2982
    def default_attribs():
2983
        return {
0.13.15 by Aaron Bentley
Convert symlink tests to avoid roundtripping
2984
            '_id_number': 1,
0.13.13 by Aaron Bentley
Add direct test of serialization records
2985
            '_new_name': {},
2986
            '_new_parent': {},
2987
            '_new_executability': {},
2988
            '_new_id': {},
0.13.15 by Aaron Bentley
Convert symlink tests to avoid roundtripping
2989
            '_tree_path_ids': {'': 'new-0'},
0.13.13 by Aaron Bentley
Add direct test of serialization records
2990
            '_removed_id': [],
2991
            '_removed_contents': [],
2992
            '_non_present_ids': {},
2993
            }
2994
0.13.18 by Aaron Bentley
Finish converting tests to direct serialize/deserialize tests, clean up
2995
    def make_records(self, attribs, contents):
2996
        records = [
2997
            (((('attribs'),),), bencode.bencode(attribs))]
2998
        records.extend([(((n, k),), c) for n, k, c in contents])
2999
        return records
3000
0.13.13 by Aaron Bentley
Add direct test of serialization records
3001
    def creation_records(self):
3002
        attribs = self.default_attribs()
3003
        attribs['_id_number'] = 3
3004
        attribs['_new_name'] = {
3005
            'new-1': u'foo\u1234'.encode('utf-8'), 'new-2': 'qux'}
3006
        attribs['_new_id'] = {'new-1': 'baz', 'new-2': 'quxx'}
3007
        attribs['_new_parent'] = {'new-1': 'new-0', 'new-2': 'new-0'}
3008
        attribs['_new_executability'] = {'new-1': 1}
3009
        contents = [
3010
            ('new-1', 'file', 'i 1\nbar\n'),
3011
            ('new-2', 'directory', ''),
3012
            ]
3013
        return self.make_records(attribs, contents)
3014
3015
    def test_serialize_creation(self):
0.13.14 by Aaron Bentley
Add deserialization test, remove roundtrip test.
3016
        tt = self.get_preview()
0.13.13 by Aaron Bentley
Add direct test of serialization records
3017
        tt.new_file(u'foo\u1234', tt.root, 'bar', 'baz', True)
3018
        tt.new_directory('qux', tt.root, 'quxx')
0.13.21 by Aaron Bentley
Use assertSerializesTo in more places
3019
        self.assertSerializesTo(self.creation_records(), tt)
0.13.13 by Aaron Bentley
Add direct test of serialization records
3020
0.13.14 by Aaron Bentley
Add deserialization test, remove roundtrip test.
3021
    def test_deserialize_creation(self):
3022
        tt = self.get_preview()
3023
        tt.deserialize(iter(self.creation_records()))
3024
        self.assertEqual(3, tt._id_number)
3025
        self.assertEqual({'new-1': u'foo\u1234',
3026
                          'new-2': 'qux'}, tt._new_name)
3027
        self.assertEqual({'new-1': 'baz', 'new-2': 'quxx'}, tt._new_id)
3028
        self.assertEqual({'new-1': tt.root, 'new-2': tt.root}, tt._new_parent)
3029
        self.assertEqual({'baz': 'new-1', 'quxx': 'new-2'}, tt._r_new_id)
3030
        self.assertEqual({'new-1': True}, tt._new_executability)
3031
        self.assertEqual({'new-1': 'file',
3032
                          'new-2': 'directory'}, tt._new_contents)
3033
        foo_limbo = open(tt._limbo_name('new-1'), 'rb')
3034
        try:
3035
            foo_content = foo_limbo.read()
3036
        finally:
3037
            foo_limbo.close()
3038
        self.assertEqual('bar', foo_content)
3039
0.13.18 by Aaron Bentley
Finish converting tests to direct serialize/deserialize tests, clean up
3040
    def symlink_creation_records(self):
3041
        attribs = self.default_attribs()
3042
        attribs['_id_number'] = 2
3043
        attribs['_new_name'] = {'new-1': u'foo\u1234'.encode('utf-8')}
3044
        attribs['_new_parent'] = {'new-1': 'new-0'}
3045
        contents = [('new-1', 'symlink', u'bar\u1234'.encode('utf-8'))]
3046
        return self.make_records(attribs, contents)
3047
0.13.15 by Aaron Bentley
Convert symlink tests to avoid roundtripping
3048
    def test_serialize_symlink_creation(self):
0.13.8 by Aaron Bentley
Integrate serialization into TreeTransforms
3049
        self.requireFeature(tests.SymlinkFeature)
0.13.15 by Aaron Bentley
Convert symlink tests to avoid roundtripping
3050
        tt = self.get_preview()
0.13.16 by Aaron Bentley
Add unicode symlink targets to tests
3051
        tt.new_symlink(u'foo\u1234', tt.root, u'bar\u1234')
0.13.21 by Aaron Bentley
Use assertSerializesTo in more places
3052
        self.assertSerializesTo(self.symlink_creation_records(), tt)
0.13.15 by Aaron Bentley
Convert symlink tests to avoid roundtripping
3053
3054
    def test_deserialize_symlink_creation(self):
4241.14.12 by Vincent Ladeuil
Far too many modifications for a single commit, need to restart.
3055
        self.requireFeature(tests.SymlinkFeature)
0.13.15 by Aaron Bentley
Convert symlink tests to avoid roundtripping
3056
        tt = self.get_preview()
3057
        tt.deserialize(iter(self.symlink_creation_records()))
4241.14.12 by Vincent Ladeuil
Far too many modifications for a single commit, need to restart.
3058
        abspath = tt._limbo_name('new-1')
4241.14.17 by Vincent Ladeuil
Add more tests for unicode symlinks to test_transform.
3059
        foo_content = osutils.readlink(abspath)
0.13.22 by Aaron Bentley
More unicodeness for Shelf tests
3060
        self.assertEqual(u'bar\u1234', foo_content)
0.13.8 by Aaron Bentley
Integrate serialization into TreeTransforms
3061
0.13.19 by Aaron Bentley
Clean up serialization tests
3062
    def make_destruction_preview(self):
0.13.8 by Aaron Bentley
Integrate serialization into TreeTransforms
3063
        tree = self.make_branch_and_tree('.')
3064
        self.build_tree([u'foo\u1234', 'bar'])
3065
        tree.add([u'foo\u1234', 'bar'], ['foo-id', 'bar-id'])
0.13.19 by Aaron Bentley
Clean up serialization tests
3066
        return self.get_preview(tree)
0.13.17 by Aaron Bentley
Convert roundtrip destruction test to serialization/deserialization pair
3067
0.13.18 by Aaron Bentley
Finish converting tests to direct serialize/deserialize tests, clean up
3068
    def destruction_records(self):
3069
        attribs = self.default_attribs()
3070
        attribs['_id_number'] = 3
3071
        attribs['_removed_id'] = ['new-1']
3072
        attribs['_removed_contents'] = ['new-2']
3073
        attribs['_tree_path_ids'] = {
3074
            '': 'new-0',
3075
            u'foo\u1234'.encode('utf-8'): 'new-1',
3076
            'bar': 'new-2',
3077
            }
3078
        return self.make_records(attribs, [])
3079
0.13.17 by Aaron Bentley
Convert roundtrip destruction test to serialization/deserialization pair
3080
    def test_serialize_destruction(self):
0.13.19 by Aaron Bentley
Clean up serialization tests
3081
        tt = self.make_destruction_preview()
0.13.8 by Aaron Bentley
Integrate serialization into TreeTransforms
3082
        foo_trans_id = tt.trans_id_tree_file_id('foo-id')
3083
        tt.unversion_file(foo_trans_id)
3084
        bar_trans_id = tt.trans_id_tree_file_id('bar-id')
3085
        tt.delete_contents(bar_trans_id)
0.13.18 by Aaron Bentley
Finish converting tests to direct serialize/deserialize tests, clean up
3086
        self.assertSerializesTo(self.destruction_records(), tt)
0.13.17 by Aaron Bentley
Convert roundtrip destruction test to serialization/deserialization pair
3087
3088
    def test_deserialize_destruction(self):
0.13.19 by Aaron Bentley
Clean up serialization tests
3089
        tt = self.make_destruction_preview()
0.13.17 by Aaron Bentley
Convert roundtrip destruction test to serialization/deserialization pair
3090
        tt.deserialize(iter(self.destruction_records()))
3091
        self.assertEqual({u'foo\u1234': 'new-1',
3092
                          'bar': 'new-2',
3093
                          '': tt.root}, tt._tree_path_ids)
3094
        self.assertEqual({'new-1': u'foo\u1234',
3095
                          'new-2': 'bar',
3096
                          tt.root: ''}, tt._tree_id_paths)
3097
        self.assertEqual(set(['new-1']), tt._removed_id)
3098
        self.assertEqual(set(['new-2']), tt._removed_contents)
0.13.8 by Aaron Bentley
Integrate serialization into TreeTransforms
3099
0.13.18 by Aaron Bentley
Finish converting tests to direct serialize/deserialize tests, clean up
3100
    def missing_records(self):
3101
        attribs = self.default_attribs()
3102
        attribs['_id_number'] = 2
3103
        attribs['_non_present_ids'] = {
3104
            'boo': 'new-1',}
3105
        return self.make_records(attribs, [])
3106
3107
    def test_serialize_missing(self):
3108
        tt = self.get_preview()
0.13.8 by Aaron Bentley
Integrate serialization into TreeTransforms
3109
        boo_trans_id = tt.trans_id_file_id('boo')
0.13.18 by Aaron Bentley
Finish converting tests to direct serialize/deserialize tests, clean up
3110
        self.assertSerializesTo(self.missing_records(), tt)
3111
3112
    def test_deserialize_missing(self):
3113
        tt = self.get_preview()
3114
        tt.deserialize(iter(self.missing_records()))
3115
        self.assertEqual({'boo': 'new-1'}, tt._non_present_ids)
3116
3117
    def make_modification_preview(self):
0.13.8 by Aaron Bentley
Integrate serialization into TreeTransforms
3118
        LINES_ONE = 'aa\nbb\ncc\ndd\n'
3119
        LINES_TWO = 'z\nbb\nx\ndd\n'
3120
        tree = self.make_branch_and_tree('tree')
3121
        self.build_tree_contents([('tree/file', LINES_ONE)])
3122
        tree.add('file', 'file-id')
0.13.18 by Aaron Bentley
Finish converting tests to direct serialize/deserialize tests, clean up
3123
        return self.get_preview(tree), LINES_TWO
3124
3125
    def modification_records(self):
3126
        attribs = self.default_attribs()
3127
        attribs['_id_number'] = 2
3128
        attribs['_tree_path_ids'] = {
3129
            'file': 'new-1',
3130
            '': 'new-0',}
3131
        attribs['_removed_contents'] = ['new-1']
3132
        contents = [('new-1', 'file',
3133
                     'i 1\nz\n\nc 0 1 1 1\ni 1\nx\n\nc 0 3 3 1\n')]
3134
        return self.make_records(attribs, contents)
3135
3136
    def test_serialize_modification(self):
3137
        tt, LINES = self.make_modification_preview()
0.13.8 by Aaron Bentley
Integrate serialization into TreeTransforms
3138
        trans_id = tt.trans_id_file_id('file-id')
3139
        tt.delete_contents(trans_id)
0.13.18 by Aaron Bentley
Finish converting tests to direct serialize/deserialize tests, clean up
3140
        tt.create_file(LINES, trans_id)
3141
        self.assertSerializesTo(self.modification_records(), tt)
3142
3143
    def test_deserialize_modification(self):
3144
        tt, LINES = self.make_modification_preview()
3145
        tt.deserialize(iter(self.modification_records()))
3146
        self.assertFileEqual(LINES, tt._limbo_name('new-1'))
3147
3148
    def make_kind_change_preview(self):
3149
        LINES = 'a\nb\nc\nd\n'
0.13.8 by Aaron Bentley
Integrate serialization into TreeTransforms
3150
        tree = self.make_branch_and_tree('tree')
3151
        self.build_tree(['tree/foo/'])
3152
        tree.add('foo', 'foo-id')
0.13.18 by Aaron Bentley
Finish converting tests to direct serialize/deserialize tests, clean up
3153
        return self.get_preview(tree), LINES
3154
3155
    def kind_change_records(self):
3156
        attribs = self.default_attribs()
3157
        attribs['_id_number'] = 2
3158
        attribs['_tree_path_ids'] = {
3159
            'foo': 'new-1',
3160
            '': 'new-0',}
3161
        attribs['_removed_contents'] = ['new-1']
3162
        contents = [('new-1', 'file',
3163
                     'i 4\na\nb\nc\nd\n\n')]
3164
        return self.make_records(attribs, contents)
3165
3166
    def test_serialize_kind_change(self):
3167
        tt, LINES = self.make_kind_change_preview()
0.13.8 by Aaron Bentley
Integrate serialization into TreeTransforms
3168
        trans_id = tt.trans_id_file_id('foo-id')
3169
        tt.delete_contents(trans_id)
0.13.18 by Aaron Bentley
Finish converting tests to direct serialize/deserialize tests, clean up
3170
        tt.create_file(LINES, trans_id)
3171
        self.assertSerializesTo(self.kind_change_records(), tt)
3172
3173
    def test_deserialize_kind_change(self):
3174
        tt, LINES = self.make_kind_change_preview()
3175
        tt.deserialize(iter(self.kind_change_records()))
3176
        self.assertFileEqual(LINES, tt._limbo_name('new-1'))
3177
3178
    def make_add_contents_preview(self):
3179
        LINES = 'a\nb\nc\nd\n'
0.13.8 by Aaron Bentley
Integrate serialization into TreeTransforms
3180
        tree = self.make_branch_and_tree('tree')
3181
        self.build_tree(['tree/foo'])
3182
        tree.add('foo')
3183
        os.unlink('tree/foo')
0.13.18 by Aaron Bentley
Finish converting tests to direct serialize/deserialize tests, clean up
3184
        return self.get_preview(tree), LINES
3185
3186
    def add_contents_records(self):
3187
        attribs = self.default_attribs()
3188
        attribs['_id_number'] = 2
3189
        attribs['_tree_path_ids'] = {
3190
            'foo': 'new-1',
3191
            '': 'new-0',}
3192
        contents = [('new-1', 'file',
3193
                     'i 4\na\nb\nc\nd\n\n')]
3194
        return self.make_records(attribs, contents)
3195
3196
    def test_serialize_add_contents(self):
3197
        tt, LINES = self.make_add_contents_preview()
0.13.8 by Aaron Bentley
Integrate serialization into TreeTransforms
3198
        trans_id = tt.trans_id_tree_path('foo')
0.13.18 by Aaron Bentley
Finish converting tests to direct serialize/deserialize tests, clean up
3199
        tt.create_file(LINES, trans_id)
3200
        self.assertSerializesTo(self.add_contents_records(), tt)
3201
3202
    def test_deserialize_add_contents(self):
3203
        tt, LINES = self.make_add_contents_preview()
3204
        tt.deserialize(iter(self.add_contents_records()))
3205
        self.assertFileEqual(LINES, tt._limbo_name('new-1'))
0.13.8 by Aaron Bentley
Integrate serialization into TreeTransforms
3206
3207
    def test_get_parents_lines(self):
3208
        LINES_ONE = 'aa\nbb\ncc\ndd\n'
3209
        LINES_TWO = 'z\nbb\nx\ndd\n'
3210
        tree = self.make_branch_and_tree('tree')
3211
        self.build_tree_contents([('tree/file', LINES_ONE)])
3212
        tree.add('file', 'file-id')
0.13.18 by Aaron Bentley
Finish converting tests to direct serialize/deserialize tests, clean up
3213
        tt = self.get_preview(tree)
0.13.8 by Aaron Bentley
Integrate serialization into TreeTransforms
3214
        trans_id = tt.trans_id_tree_path('file')
3215
        self.assertEqual((['aa\n', 'bb\n', 'cc\n', 'dd\n'],),
3216
            tt._get_parents_lines(trans_id))
3217
3218
    def test_get_parents_texts(self):
3219
        LINES_ONE = 'aa\nbb\ncc\ndd\n'
3220
        LINES_TWO = 'z\nbb\nx\ndd\n'
3221
        tree = self.make_branch_and_tree('tree')
3222
        self.build_tree_contents([('tree/file', LINES_ONE)])
3223
        tree.add('file', 'file-id')
0.13.18 by Aaron Bentley
Finish converting tests to direct serialize/deserialize tests, clean up
3224
        tt = self.get_preview(tree)
0.13.8 by Aaron Bentley
Integrate serialization into TreeTransforms
3225
        trans_id = tt.trans_id_tree_path('file')
3226
        self.assertEqual((LINES_ONE,),
3227
            tt._get_parents_texts(trans_id))