~bzr-pqm/bzr/bzr.dev

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