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