/brz/remove-bazaar

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