/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))
150
        self.assertEqual(self.wt.get_root_id(), 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()
6855.4.1 by Jelmer Vernooij
Yet more bees.
273
        self.assertNotEqual(b'new-root-id', self.wt.get_root_id())
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()
6855.4.1 by Jelmer Vernooij
Yet more bees.
279
        self.assertEqual(b'new-root-id', self.wt.get_root_id())
4634.122.2 by Aaron Bentley, John Arbash Meinel
Bring the fixup_new_roots code from the nested-trees code.
280
281
    def test_change_root_id_add_files(self):
282
        transform, root = self.get_transform()
6855.4.1 by Jelmer Vernooij
Yet more bees.
283
        self.assertNotEqual(b'new-root-id', self.wt.get_root_id())
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()
6855.4.1 by Jelmer Vernooij
Yet more bees.
291
        self.assertEqual(b'new-root-id', self.wt.get_root_id())
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()
324
        old_root_id = self.wt.get_root_id()
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()
330
        self.assertEqual(old_root_id, self.wt.get_root_id())
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()
361
        self.assertEqual(old_root_id, self.wt.get_root_id())
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'),
7143.15.2 by Jelmer Vernooij
Run autopep8.
1150
                               (True, True))], list(transform.iter_changes()))
6973.10.1 by Jelmer Vernooij
Fix some tests.
1151
            transform.new_directory('new', root, b'id-1')
1152
            self.assertEqual([(b'id-1', ('old', 'new'), True, (True, True),
7143.15.2 by Jelmer Vernooij
Run autopep8.
1153
                               (b'eert_toor', b'eert_toor'), ('old', 'new'),
1154
                               ('file', 'directory'),
1155
                               (True, False))], list(transform.iter_changes()))
1551.11.1 by Aaron Bentley
Initial work on converting TreeTransform to iter_changes format
1156
        finally:
1157
            transform.finalize()
1158
1159
    def test_iter_changes_new(self):
6855.4.1 by Jelmer Vernooij
Yet more bees.
1160
        self.wt.set_root_id(b'eert_toor')
1551.11.1 by Aaron Bentley
Initial work on converting TreeTransform to iter_changes format
1161
        transform, root = self.get_transform()
6973.10.1 by Jelmer Vernooij
Fix some tests.
1162
        transform.new_file('old', root, [b'blah'])
1551.11.1 by Aaron Bentley
Initial work on converting TreeTransform to iter_changes format
1163
        transform.apply()
1164
        transform, root = self.get_transform()
1165
        try:
1166
            old = transform.trans_id_tree_path('old')
6973.10.1 by Jelmer Vernooij
Fix some tests.
1167
            transform.version_file(b'id-1', old)
1168
            self.assertEqual([(b'id-1', (None, 'old'), False, (False, True),
7143.15.5 by Jelmer Vernooij
More PEP8 fixes.
1169
                               (b'eert_toor', b'eert_toor'),
1170
                               ('old', 'old'), ('file', 'file'),
1171
                               (False, False))],
1172
                             list(transform.iter_changes()))
1551.11.1 by Aaron Bentley
Initial work on converting TreeTransform to iter_changes format
1173
        finally:
1174
            transform.finalize()
1175
1551.11.2 by Aaron Bentley
Get kind change detection working for iter_changes
1176
    def test_iter_changes_modifications(self):
6855.4.1 by Jelmer Vernooij
Yet more bees.
1177
        self.wt.set_root_id(b'eert_toor')
1551.11.2 by Aaron Bentley
Get kind change detection working for iter_changes
1178
        transform, root = self.get_transform()
6973.13.2 by Jelmer Vernooij
Fix some more tests.
1179
        transform.new_file('old', root, [b'blah'], b'id-1')
6973.10.1 by Jelmer Vernooij
Fix some tests.
1180
        transform.new_file('new', root, [b'blah'])
6973.13.2 by Jelmer Vernooij
Fix some more tests.
1181
        transform.new_directory('subdir', root, b'subdir-id')
1551.11.2 by Aaron Bentley
Get kind change detection working for iter_changes
1182
        transform.apply()
1183
        transform, root = self.get_transform()
1184
        try:
1185
            old = transform.trans_id_tree_path('old')
6885.1.1 by Jelmer Vernooij
Get rid of TreeTransform.trans_id_tree_file_id.
1186
            subdir = transform.trans_id_tree_path('subdir')
1551.11.2 by Aaron Bentley
Get kind change detection working for iter_changes
1187
            new = transform.trans_id_tree_path('new')
3254.1.1 by Aaron Bentley
Make Tree.iter_changes a public method
1188
            self.assertEqual([], list(transform.iter_changes()))
1551.11.2 by Aaron Bentley
Get kind change detection working for iter_changes
1189
7143.15.2 by Jelmer Vernooij
Run autopep8.
1190
            # content deletion
1551.11.2 by Aaron Bentley
Get kind change detection working for iter_changes
1191
            transform.delete_contents(old)
6973.10.1 by Jelmer Vernooij
Fix some tests.
1192
            self.assertEqual([(b'id-1', ('old', 'old'), True, (True, True),
7143.15.5 by Jelmer Vernooij
More PEP8 fixes.
1193
                               (b'eert_toor', b'eert_toor'),
1194
                               ('old', 'old'), ('file', None),
1195
                               (False, False))],
1196
                             list(transform.iter_changes()))
1551.11.2 by Aaron Bentley
Get kind change detection working for iter_changes
1197
7143.15.2 by Jelmer Vernooij
Run autopep8.
1198
            # content change
6973.5.10 by Jelmer Vernooij
Random bunch of python3 bee-improvements.
1199
            transform.create_file([b'blah'], old)
6973.10.1 by Jelmer Vernooij
Fix some tests.
1200
            self.assertEqual([(b'id-1', ('old', 'old'), True, (True, True),
7143.15.5 by Jelmer Vernooij
More PEP8 fixes.
1201
                               (b'eert_toor', b'eert_toor'),
1202
                               ('old', 'old'), ('file', 'file'),
1203
                               (False, False))],
1204
                             list(transform.iter_changes()))
1551.11.2 by Aaron Bentley
Get kind change detection working for iter_changes
1205
            transform.cancel_deletion(old)
7045.1.1 by Jelmer Vernooij
Fix another 300 tests.
1206
            self.assertEqual([(b'id-1', ('old', 'old'), True, (True, True),
7143.15.5 by Jelmer Vernooij
More PEP8 fixes.
1207
                               (b'eert_toor', b'eert_toor'),
1208
                               ('old', 'old'), ('file', 'file'),
1209
                               (False, False))],
1210
                             list(transform.iter_changes()))
1551.11.2 by Aaron Bentley
Get kind change detection working for iter_changes
1211
            transform.cancel_creation(old)
1212
1213
            # move file_id to a different file
3254.1.1 by Aaron Bentley
Make Tree.iter_changes a public method
1214
            self.assertEqual([], list(transform.iter_changes()))
1551.11.2 by Aaron Bentley
Get kind change detection working for iter_changes
1215
            transform.unversion_file(old)
6973.10.1 by Jelmer Vernooij
Fix some tests.
1216
            transform.version_file(b'id-1', new)
1551.11.2 by Aaron Bentley
Get kind change detection working for iter_changes
1217
            transform.adjust_path('old', root, new)
6973.10.1 by Jelmer Vernooij
Fix some tests.
1218
            self.assertEqual([(b'id-1', ('old', 'old'), True, (True, True),
7143.15.5 by Jelmer Vernooij
More PEP8 fixes.
1219
                               (b'eert_toor', b'eert_toor'),
1220
                               ('old', 'old'), ('file', 'file'),
1221
                               (False, False))],
1222
                             list(transform.iter_changes()))
1551.11.2 by Aaron Bentley
Get kind change detection working for iter_changes
1223
            transform.cancel_versioning(new)
1224
            transform._removed_id = set()
1225
7143.15.2 by Jelmer Vernooij
Run autopep8.
1226
            # execute bit
3254.1.1 by Aaron Bentley
Make Tree.iter_changes a public method
1227
            self.assertEqual([], list(transform.iter_changes()))
1551.11.2 by Aaron Bentley
Get kind change detection working for iter_changes
1228
            transform.set_executability(True, old)
6973.10.1 by Jelmer Vernooij
Fix some tests.
1229
            self.assertEqual([(b'id-1', ('old', 'old'), False, (True, True),
7143.15.5 by Jelmer Vernooij
More PEP8 fixes.
1230
                               (b'eert_toor', b'eert_toor'),
1231
                               ('old', 'old'), ('file', 'file'),
1232
                               (False, True))],
1233
                             list(transform.iter_changes()))
1551.11.2 by Aaron Bentley
Get kind change detection working for iter_changes
1234
            transform.set_executability(None, old)
1235
1236
            # filename
3254.1.1 by Aaron Bentley
Make Tree.iter_changes a public method
1237
            self.assertEqual([], list(transform.iter_changes()))
1551.11.2 by Aaron Bentley
Get kind change detection working for iter_changes
1238
            transform.adjust_path('new', root, old)
1239
            transform._new_parent = {}
6973.10.1 by Jelmer Vernooij
Fix some tests.
1240
            self.assertEqual([(b'id-1', ('old', 'new'), False, (True, True),
7143.15.5 by Jelmer Vernooij
More PEP8 fixes.
1241
                               (b'eert_toor', b'eert_toor'),
1242
                               ('old', 'new'), ('file', 'file'),
1243
                               (False, False))],
1244
                             list(transform.iter_changes()))
1551.11.2 by Aaron Bentley
Get kind change detection working for iter_changes
1245
            transform._new_name = {}
1246
1247
            # parent directory
3254.1.1 by Aaron Bentley
Make Tree.iter_changes a public method
1248
            self.assertEqual([], list(transform.iter_changes()))
1551.11.2 by Aaron Bentley
Get kind change detection working for iter_changes
1249
            transform.adjust_path('new', subdir, old)
1250
            transform._new_name = {}
6973.10.1 by Jelmer Vernooij
Fix some tests.
1251
            self.assertEqual([(b'id-1', ('old', 'subdir/old'), False,
7143.15.2 by Jelmer Vernooij
Run autopep8.
1252
                               (True, True), (b'eert_toor',
1253
                                              b'subdir-id'), ('old', 'old'),
1254
                               ('file', 'file'), (False, False))],
1255
                             list(transform.iter_changes()))
1551.11.2 by Aaron Bentley
Get kind change detection working for iter_changes
1256
            transform._new_path = {}
1257
1258
        finally:
1259
            transform.finalize()
1260
1551.11.7 by Aaron Bentley
Stop modified flag bleeding into later changes
1261
    def test_iter_changes_modified_bleed(self):
6855.4.1 by Jelmer Vernooij
Yet more bees.
1262
        self.wt.set_root_id(b'eert_toor')
1551.11.7 by Aaron Bentley
Stop modified flag bleeding into later changes
1263
        """Modified flag should not bleed from one change to another"""
1264
        # unfortunately, we have no guarantee that file1 (which is modified)
1265
        # will be applied before file2.  And if it's applied after file2, it
1266
        # obviously can't bleed into file2's change output.  But for now, it
1267
        # works.
1268
        transform, root = self.get_transform()
6973.10.1 by Jelmer Vernooij
Fix some tests.
1269
        transform.new_file('file1', root, [b'blah'], b'id-1')
1270
        transform.new_file('file2', root, [b'blah'], b'id-2')
1551.11.7 by Aaron Bentley
Stop modified flag bleeding into later changes
1271
        transform.apply()
1272
        transform, root = self.get_transform()
1273
        try:
6973.10.1 by Jelmer Vernooij
Fix some tests.
1274
            transform.delete_contents(transform.trans_id_file_id(b'id-1'))
1551.11.7 by Aaron Bentley
Stop modified flag bleeding into later changes
1275
            transform.set_executability(True,
7143.15.2 by Jelmer Vernooij
Run autopep8.
1276
                                        transform.trans_id_file_id(b'id-2'))
7143.15.5 by Jelmer Vernooij
More PEP8 fixes.
1277
            self.assertEqual(
1278
                [(b'id-1', (u'file1', u'file1'), True, (True, True),
1279
                 (b'eert_toor', b'eert_toor'), ('file1', u'file1'),
1280
                 ('file', None), (False, False)),
1281
                 (b'id-2', (u'file2', u'file2'), False, (True, True),
1282
                 (b'eert_toor', b'eert_toor'), ('file2', u'file2'),
1283
                 ('file', 'file'), (False, True))],
1284
                list(transform.iter_changes()))
1551.11.7 by Aaron Bentley
Stop modified flag bleeding into later changes
1285
        finally:
1286
            transform.finalize()
1287
1551.10.37 by Aaron Bentley
recommit of TreeTransform._iter_changes fix with missing files
1288
    def test_iter_changes_move_missing(self):
1289
        """Test moving ids with no files around"""
6855.4.1 by Jelmer Vernooij
Yet more bees.
1290
        self.wt.set_root_id(b'toor_eert')
1551.10.37 by Aaron Bentley
recommit of TreeTransform._iter_changes fix with missing files
1291
        # Need two steps because versioning a non-existant file is a conflict.
1292
        transform, root = self.get_transform()
6973.10.1 by Jelmer Vernooij
Fix some tests.
1293
        transform.new_directory('floater', root, b'floater-id')
1551.10.37 by Aaron Bentley
recommit of TreeTransform._iter_changes fix with missing files
1294
        transform.apply()
1295
        transform, root = self.get_transform()
1296
        transform.delete_contents(transform.trans_id_tree_path('floater'))
1297
        transform.apply()
1298
        transform, root = self.get_transform()
1299
        floater = transform.trans_id_tree_path('floater')
1300
        try:
1301
            transform.adjust_path('flitter', root, floater)
6973.10.1 by Jelmer Vernooij
Fix some tests.
1302
            self.assertEqual([(b'floater-id', ('floater', 'flitter'), False,
7143.15.5 by Jelmer Vernooij
More PEP8 fixes.
1303
                               (True, True),
1304
                               (b'toor_eert', b'toor_eert'),
1305
                               ('floater', 'flitter'),
1306
                               (None, None), (False, False))],
1307
                             list(transform.iter_changes()))
1551.10.37 by Aaron Bentley
recommit of TreeTransform._iter_changes fix with missing files
1308
        finally:
1309
            transform.finalize()
1310
1551.11.2 by Aaron Bentley
Get kind change detection working for iter_changes
1311
    def test_iter_changes_pointless(self):
1312
        """Ensure that no-ops are not treated as modifications"""
6855.4.1 by Jelmer Vernooij
Yet more bees.
1313
        self.wt.set_root_id(b'eert_toor')
1551.11.2 by Aaron Bentley
Get kind change detection working for iter_changes
1314
        transform, root = self.get_transform()
6973.10.1 by Jelmer Vernooij
Fix some tests.
1315
        transform.new_file('old', root, [b'blah'], b'id-1')
6973.5.10 by Jelmer Vernooij
Random bunch of python3 bee-improvements.
1316
        transform.new_directory('subdir', root, b'subdir-id')
1551.11.2 by Aaron Bentley
Get kind change detection working for iter_changes
1317
        transform.apply()
1318
        transform, root = self.get_transform()
1319
        try:
1320
            old = transform.trans_id_tree_path('old')
6885.1.1 by Jelmer Vernooij
Get rid of TreeTransform.trans_id_tree_file_id.
1321
            subdir = transform.trans_id_tree_path('subdir')
3254.1.1 by Aaron Bentley
Make Tree.iter_changes a public method
1322
            self.assertEqual([], list(transform.iter_changes()))
1551.11.2 by Aaron Bentley
Get kind change detection working for iter_changes
1323
            transform.delete_contents(subdir)
1324
            transform.create_directory(subdir)
1325
            transform.set_executability(False, old)
1326
            transform.unversion_file(old)
6973.10.1 by Jelmer Vernooij
Fix some tests.
1327
            transform.version_file(b'id-1', old)
1551.11.2 by Aaron Bentley
Get kind change detection working for iter_changes
1328
            transform.adjust_path('old', root, old)
3254.1.1 by Aaron Bentley
Make Tree.iter_changes a public method
1329
            self.assertEqual([], list(transform.iter_changes()))
1551.11.2 by Aaron Bentley
Get kind change detection working for iter_changes
1330
        finally:
1331
            transform.finalize()
1534.7.93 by Aaron Bentley
Added text merge test
1332
2502.1.1 by Aaron Bentley
Ensure renames only root children are renamed when building trees
1333
    def test_rename_count(self):
1334
        transform, root = self.get_transform()
6973.10.1 by Jelmer Vernooij
Fix some tests.
1335
        transform.new_file('name1', root, [b'contents'])
2502.1.1 by Aaron Bentley
Ensure renames only root children are renamed when building trees
1336
        self.assertEqual(transform.rename_count, 0)
1337
        transform.apply()
1338
        self.assertEqual(transform.rename_count, 1)
1339
        transform2, root = self.get_transform()
2502.1.8 by Aaron Bentley
Updates from review comments
1340
        transform2.adjust_path('name2', root,
1341
                               transform2.trans_id_tree_path('name1'))
2502.1.1 by Aaron Bentley
Ensure renames only root children are renamed when building trees
1342
        self.assertEqual(transform2.rename_count, 0)
1343
        transform2.apply()
1344
        self.assertEqual(transform2.rename_count, 2)
1345
2502.1.2 by Aaron Bentley
Make the limited-renames functionality safer in the general case
1346
    def test_change_parent(self):
2502.1.8 by Aaron Bentley
Updates from review comments
1347
        """Ensure that after we change a parent, the results are still right.
1348
1349
        Renames and parent changes on pending transforms can happen as part
1350
        of conflict resolution, and are explicitly permitted by the
1351
        TreeTransform API.
1352
1353
        This test ensures they work correctly with the rename-avoidance
1354
        optimization.
1355
        """
2502.1.2 by Aaron Bentley
Make the limited-renames functionality safer in the general case
1356
        transform, root = self.get_transform()
2502.1.8 by Aaron Bentley
Updates from review comments
1357
        parent1 = transform.new_directory('parent1', root)
6973.10.1 by Jelmer Vernooij
Fix some tests.
1358
        child1 = transform.new_file('child1', parent1, [b'contents'])
2502.1.8 by Aaron Bentley
Updates from review comments
1359
        parent2 = transform.new_directory('parent2', root)
1360
        transform.adjust_path('child1', parent2, child1)
2502.1.2 by Aaron Bentley
Make the limited-renames functionality safer in the general case
1361
        transform.apply()
5784.1.3 by Martin Pool
Switch away from using failUnlessExists and failIfExists
1362
        self.assertPathDoesNotExist(self.wt.abspath('parent1/child1'))
1363
        self.assertPathExists(self.wt.abspath('parent2/child1'))
2502.1.8 by Aaron Bentley
Updates from review comments
1364
        # rename limbo/new-1 => parent1, rename limbo/new-3 => parent2
1365
        # no rename for child1 (counting only renames during apply)
5784.1.1 by Martin Pool
Stop using failIf, failUnless, etc
1366
        self.assertEqual(2, transform.rename_count)
2502.1.2 by Aaron Bentley
Make the limited-renames functionality safer in the general case
1367
1368
    def test_cancel_parent(self):
1369
        """Cancelling a parent doesn't cause deletion of a non-empty directory
1370
1371
        This is like the test_change_parent, except that we cancel the parent
1372
        before adjusting the path.  The transform must detect that the
1373
        directory is non-empty, and move children to safe locations.
1374
        """
1375
        transform, root = self.get_transform()
2502.1.8 by Aaron Bentley
Updates from review comments
1376
        parent1 = transform.new_directory('parent1', root)
6973.10.1 by Jelmer Vernooij
Fix some tests.
1377
        child1 = transform.new_file('child1', parent1, [b'contents'])
1378
        child2 = transform.new_file('child2', parent1, [b'contents'])
2502.1.8 by Aaron Bentley
Updates from review comments
1379
        try:
1380
            transform.cancel_creation(parent1)
1381
        except OSError:
1382
            self.fail('Failed to move child1 before deleting parent1')
1383
        transform.cancel_creation(child2)
1384
        transform.create_directory(parent1)
1385
        try:
1386
            transform.cancel_creation(parent1)
1387
        # If the transform incorrectly believes that child2 is still in
1388
        # parent1's limbo directory, it will try to rename it and fail
1389
        # because was already moved by the first cancel_creation.
1390
        except OSError:
1391
            self.fail('Transform still thinks child2 is a child of parent1')
1392
        parent2 = transform.new_directory('parent2', root)
1393
        transform.adjust_path('child1', parent2, child1)
2502.1.2 by Aaron Bentley
Make the limited-renames functionality safer in the general case
1394
        transform.apply()
5784.1.3 by Martin Pool
Switch away from using failUnlessExists and failIfExists
1395
        self.assertPathDoesNotExist(self.wt.abspath('parent1'))
1396
        self.assertPathExists(self.wt.abspath('parent2/child1'))
2502.1.8 by Aaron Bentley
Updates from review comments
1397
        # rename limbo/new-3 => parent2, rename limbo/new-2 => child1
5784.1.1 by Martin Pool
Stop using failIf, failUnless, etc
1398
        self.assertEqual(2, transform.rename_count)
2502.1.2 by Aaron Bentley
Make the limited-renames functionality safer in the general case
1399
1400
    def test_adjust_and_cancel(self):
2502.1.8 by Aaron Bentley
Updates from review comments
1401
        """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
1402
        transform, root = self.get_transform()
2502.1.8 by Aaron Bentley
Updates from review comments
1403
        parent1 = transform.new_directory('parent1', root)
6973.10.1 by Jelmer Vernooij
Fix some tests.
1404
        child1 = transform.new_file('child1', parent1, [b'contents'])
2502.1.8 by Aaron Bentley
Updates from review comments
1405
        parent2 = transform.new_directory('parent2', root)
1406
        transform.adjust_path('child1', parent2, child1)
1407
        transform.cancel_creation(child1)
2502.1.2 by Aaron Bentley
Make the limited-renames functionality safer in the general case
1408
        try:
2502.1.8 by Aaron Bentley
Updates from review comments
1409
            transform.cancel_creation(parent1)
1410
        # if the transform thinks child1 is still in parent1's limbo
1411
        # 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
1412
        except OSError:
2502.1.8 by Aaron Bentley
Updates from review comments
1413
            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
1414
        transform.finalize()
1415
2502.1.3 by Aaron Bentley
Don't cause errors when creating contents for trans_ids with no parent/name
1416
    def test_noname_contents(self):
2502.1.8 by Aaron Bentley
Updates from review comments
1417
        """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
1418
        transform, root = self.get_transform()
6973.10.1 by Jelmer Vernooij
Fix some tests.
1419
        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
1420
        try:
2502.1.8 by Aaron Bentley
Updates from review comments
1421
            transform.create_directory(parent)
2502.1.3 by Aaron Bentley
Don't cause errors when creating contents for trans_ids with no parent/name
1422
        except KeyError:
1423
            self.fail("Can't handle contents with no name")
1424
        transform.finalize()
1425
2502.1.9 by Aaron Bentley
Add additional test for no-name contents
1426
    def test_noname_contents_nested(self):
1427
        """TreeTransform should permit deferring naming files."""
1428
        transform, root = self.get_transform()
6973.10.1 by Jelmer Vernooij
Fix some tests.
1429
        parent = transform.trans_id_file_id(b'parent-id')
2502.1.9 by Aaron Bentley
Add additional test for no-name contents
1430
        try:
1431
            transform.create_directory(parent)
1432
        except KeyError:
1433
            self.fail("Can't handle contents with no name")
7143.15.5 by Jelmer Vernooij
More PEP8 fixes.
1434
        transform.new_directory('child', parent)
2502.1.9 by Aaron Bentley
Add additional test for no-name contents
1435
        transform.adjust_path('parent', root, parent)
1436
        transform.apply()
5784.1.3 by Martin Pool
Switch away from using failUnlessExists and failIfExists
1437
        self.assertPathExists(self.wt.abspath('parent/child'))
2502.1.9 by Aaron Bentley
Add additional test for no-name contents
1438
        self.assertEqual(1, transform.rename_count)
1439
2502.1.4 by Aaron Bentley
Ensure we only reuse limbo names appropriately
1440
    def test_reuse_name(self):
1441
        """Avoid reusing the same limbo name for different files"""
1442
        transform, root = self.get_transform()
2502.1.8 by Aaron Bentley
Updates from review comments
1443
        parent = transform.new_directory('parent', root)
7143.15.5 by Jelmer Vernooij
More PEP8 fixes.
1444
        transform.new_directory('child', parent)
2502.1.4 by Aaron Bentley
Ensure we only reuse limbo names appropriately
1445
        try:
2502.1.8 by Aaron Bentley
Updates from review comments
1446
            child2 = transform.new_directory('child', parent)
2502.1.4 by Aaron Bentley
Ensure we only reuse limbo names appropriately
1447
        except OSError:
1448
            self.fail('Tranform tried to use the same limbo name twice')
2502.1.8 by Aaron Bentley
Updates from review comments
1449
        transform.adjust_path('child2', parent, child2)
2502.1.4 by Aaron Bentley
Ensure we only reuse limbo names appropriately
1450
        transform.apply()
2502.1.8 by Aaron Bentley
Updates from review comments
1451
        # limbo/new-1 => parent, limbo/new-3 => parent/child2
1452
        # child2 is put into top-level limbo because child1 has already
1453
        # claimed the direct limbo path when child2 is created.  There is no
1454
        # advantage in renaming files once they're in top-level limbo, except
1455
        # as part of apply.
2502.1.4 by Aaron Bentley
Ensure we only reuse limbo names appropriately
1456
        self.assertEqual(2, transform.rename_count)
1457
1458
    def test_reuse_when_first_moved(self):
1459
        """Don't avoid direct paths when it is safe to use them"""
1460
        transform, root = self.get_transform()
2502.1.8 by Aaron Bentley
Updates from review comments
1461
        parent = transform.new_directory('parent', root)
1462
        child1 = transform.new_directory('child', parent)
1463
        transform.adjust_path('child1', parent, child1)
7143.15.5 by Jelmer Vernooij
More PEP8 fixes.
1464
        transform.new_directory('child', parent)
2502.1.4 by Aaron Bentley
Ensure we only reuse limbo names appropriately
1465
        transform.apply()
2502.1.8 by Aaron Bentley
Updates from review comments
1466
        # limbo/new-1 => parent
2502.1.4 by Aaron Bentley
Ensure we only reuse limbo names appropriately
1467
        self.assertEqual(1, transform.rename_count)
1468
1469
    def test_reuse_after_cancel(self):
1470
        """Don't avoid direct paths when it is safe to use them"""
1471
        transform, root = self.get_transform()
2502.1.8 by Aaron Bentley
Updates from review comments
1472
        parent2 = transform.new_directory('parent2', root)
1473
        child1 = transform.new_directory('child1', parent2)
1474
        transform.cancel_creation(parent2)
1475
        transform.create_directory(parent2)
7143.15.5 by Jelmer Vernooij
More PEP8 fixes.
1476
        transform.new_directory('child1', parent2)
2502.1.8 by Aaron Bentley
Updates from review comments
1477
        transform.adjust_path('child2', parent2, child1)
2502.1.4 by Aaron Bentley
Ensure we only reuse limbo names appropriately
1478
        transform.apply()
2502.1.8 by Aaron Bentley
Updates from review comments
1479
        # limbo/new-1 => parent2, limbo/new-2 => parent2/child1
2502.1.4 by Aaron Bentley
Ensure we only reuse limbo names appropriately
1480
        self.assertEqual(2, transform.rename_count)
1481
2502.1.7 by Aaron Bentley
Fix finalize deletion ordering
1482
    def test_finalize_order(self):
2502.1.8 by Aaron Bentley
Updates from review comments
1483
        """Finalize must be done in child-to-parent order"""
2502.1.7 by Aaron Bentley
Fix finalize deletion ordering
1484
        transform, root = self.get_transform()
2502.1.8 by Aaron Bentley
Updates from review comments
1485
        parent = transform.new_directory('parent', root)
7143.15.5 by Jelmer Vernooij
More PEP8 fixes.
1486
        transform.new_directory('child', parent)
2502.1.7 by Aaron Bentley
Fix finalize deletion ordering
1487
        try:
1488
            transform.finalize()
1489
        except OSError:
2502.1.8 by Aaron Bentley
Updates from review comments
1490
            self.fail('Tried to remove parent before child1')
2502.1.7 by Aaron Bentley
Fix finalize deletion ordering
1491
2502.1.13 by Aaron Bentley
Updates from review
1492
    def test_cancel_with_cancelled_child_should_succeed(self):
2502.1.12 by Aaron Bentley
Avoid renaming children with no content
1493
        transform, root = self.get_transform()
1494
        parent = transform.new_directory('parent', root)
1495
        child = transform.new_directory('child', parent)
1496
        transform.cancel_creation(child)
2502.1.13 by Aaron Bentley
Updates from review
1497
        transform.cancel_creation(parent)
2502.1.12 by Aaron Bentley
Avoid renaming children with no content
1498
        transform.finalize()
1499
3638.3.15 by Vincent Ladeuil
Fix test_case_insensitive_clash to pass on all platforms (renamed too).
1500
    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).
1501
        def tt_helper():
3638.3.17 by Vincent Ladeuil
Fixed as per Aaron's review.
1502
            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).
1503
            tt = TreeTransform(wt)  # TreeTransform obtains write lock
1504
            try:
3638.3.15 by Vincent Ladeuil
Fix test_case_insensitive_clash to pass on all platforms (renamed too).
1505
                foo = tt.new_directory('foo', tt.root)
6973.10.1 by Jelmer Vernooij
Fix some tests.
1506
                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).
1507
                baz = tt.new_directory('baz', tt.root)
6973.10.1 by Jelmer Vernooij
Fix some tests.
1508
                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).
1509
                # Ask for a rename 'foo' -> 'baz'
1510
                tt.adjust_path('baz', tt.root, foo)
3063.1.3 by Aaron Bentley
Update for Linux
1511
                # 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).
1512
                tt.apply(no_conflicts=True)
7143.15.5 by Jelmer Vernooij
More PEP8 fixes.
1513
            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).
1514
                wt.unlock()
3063.1.3 by Aaron Bentley
Update for Linux
1515
                raise
3638.3.17 by Vincent Ladeuil
Fixed as per Aaron's review.
1516
        # The rename will fail because the target directory is not empty (but
1517
        # 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).
1518
        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
1519
        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).
1520
3063.1.2 by Alexander Belchenko
test for two directories clash
1521
    def test_two_directories_clash(self):
1522
        def tt_helper():
1523
            wt = self.make_branch_and_tree('.')
1524
            tt = TreeTransform(wt)  # TreeTransform obtains write lock
1525
            try:
3063.1.3 by Aaron Bentley
Update for Linux
1526
                foo_1 = tt.new_directory('foo', tt.root)
1527
                tt.new_directory('bar', foo_1)
3638.3.15 by Vincent Ladeuil
Fix test_case_insensitive_clash to pass on all platforms (renamed too).
1528
                # Adding the same directory with a different content
3063.1.3 by Aaron Bentley
Update for Linux
1529
                foo_2 = tt.new_directory('foo', tt.root)
1530
                tt.new_directory('baz', foo_2)
1531
                # Lie to tt that we've already resolved all conflicts.
3063.1.2 by Alexander Belchenko
test for two directories clash
1532
                tt.apply(no_conflicts=True)
7143.15.5 by Jelmer Vernooij
More PEP8 fixes.
1533
            except BaseException:
3063.1.2 by Alexander Belchenko
test for two directories clash
1534
                wt.unlock()
3063.1.3 by Aaron Bentley
Update for Linux
1535
                raise
3063.1.2 by Alexander Belchenko
test for two directories clash
1536
        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
1537
        self.assertEndsWith(err.path, "/foo")
3063.1.2 by Alexander Belchenko
test for two directories clash
1538
3100.1.1 by Aaron Bentley
Fix ImmortalLimbo errors when transforms fail
1539
    def test_two_directories_clash_finalize(self):
1540
        def tt_helper():
1541
            wt = self.make_branch_and_tree('.')
1542
            tt = TreeTransform(wt)  # TreeTransform obtains write lock
1543
            try:
1544
                foo_1 = tt.new_directory('foo', tt.root)
1545
                tt.new_directory('bar', foo_1)
3638.3.15 by Vincent Ladeuil
Fix test_case_insensitive_clash to pass on all platforms (renamed too).
1546
                # Adding the same directory with a different content
3100.1.1 by Aaron Bentley
Fix ImmortalLimbo errors when transforms fail
1547
                foo_2 = tt.new_directory('foo', tt.root)
1548
                tt.new_directory('baz', foo_2)
1549
                # Lie to tt that we've already resolved all conflicts.
1550
                tt.apply(no_conflicts=True)
7143.15.5 by Jelmer Vernooij
More PEP8 fixes.
1551
            except BaseException:
3100.1.1 by Aaron Bentley
Fix ImmortalLimbo errors when transforms fail
1552
                tt.finalize()
1553
                raise
1554
        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
1555
        self.assertEndsWith(err.path, "/foo")
3100.1.1 by Aaron Bentley
Fix ImmortalLimbo errors when transforms fail
1556
3535.6.1 by James Westby
Handle a file turning in to a directory in TreeTransform.
1557
    def test_file_to_directory(self):
1558
        wt = self.make_branch_and_tree('.')
3535.6.2 by James Westby
Fixes from review. Thanks Aaron and John.
1559
        self.build_tree(['foo'])
3535.6.1 by James Westby
Handle a file turning in to a directory in TreeTransform.
1560
        wt.add(['foo'])
3590.3.1 by James Westby
Make TreeTransform update the inventory with new kind information.
1561
        wt.commit("one")
3535.6.1 by James Westby
Handle a file turning in to a directory in TreeTransform.
1562
        tt = TreeTransform(wt)
3535.6.2 by James Westby
Fixes from review. Thanks Aaron and John.
1563
        self.addCleanup(tt.finalize)
3535.6.3 by James Westby
Fix the test to not create transform conflicts.
1564
        foo_trans_id = tt.trans_id_tree_path("foo")
1565
        tt.delete_contents(foo_trans_id)
1566
        tt.create_directory(foo_trans_id)
1567
        bar_trans_id = tt.trans_id_tree_path("foo/bar")
6973.5.10 by Jelmer Vernooij
Random bunch of python3 bee-improvements.
1568
        tt.create_file([b"aa\n"], bar_trans_id)
7045.4.7 by Jelmer Vernooij
Fix remaining tests.
1569
        tt.version_file(b"bar-1", bar_trans_id)
3535.6.2 by James Westby
Fixes from review. Thanks Aaron and John.
1570
        tt.apply()
5784.1.3 by Martin Pool
Switch away from using failUnlessExists and failIfExists
1571
        self.assertPathExists("foo/bar")
3590.3.2 by James Westby
Handle ->symlink changes as well.
1572
        wt.lock_read()
1573
        try:
6809.4.7 by Jelmer Vernooij
Swap arguments for get_symlink_target and kind/stored_kind.
1574
            self.assertEqual(wt.kind("foo"), "directory")
3590.3.2 by James Westby
Handle ->symlink changes as well.
1575
        finally:
1576
            wt.unlock()
3590.3.1 by James Westby
Make TreeTransform update the inventory with new kind information.
1577
        wt.commit("two")
1578
        changes = wt.changes_from(wt.basis_tree())
1579
        self.assertFalse(changes.has_changed(), changes)
3535.6.1 by James Westby
Handle a file turning in to a directory in TreeTransform.
1580
3590.3.2 by James Westby
Handle ->symlink changes as well.
1581
    def test_file_to_symlink(self):
3590.3.3 by James Westby
Make ->file changes work as well.
1582
        self.requireFeature(SymlinkFeature)
3590.3.2 by James Westby
Handle ->symlink changes as well.
1583
        wt = self.make_branch_and_tree('.')
1584
        self.build_tree(['foo'])
1585
        wt.add(['foo'])
1586
        wt.commit("one")
1587
        tt = TreeTransform(wt)
1588
        self.addCleanup(tt.finalize)
1589
        foo_trans_id = tt.trans_id_tree_path("foo")
1590
        tt.delete_contents(foo_trans_id)
1591
        tt.create_symlink("bar", foo_trans_id)
1592
        tt.apply()
5784.1.3 by Martin Pool
Switch away from using failUnlessExists and failIfExists
1593
        self.assertPathExists("foo")
3590.3.2 by James Westby
Handle ->symlink changes as well.
1594
        wt.lock_read()
1595
        self.addCleanup(wt.unlock)
6809.4.7 by Jelmer Vernooij
Swap arguments for get_symlink_target and kind/stored_kind.
1596
        self.assertEqual(wt.kind("foo"), "symlink")
3590.3.2 by James Westby
Handle ->symlink changes as well.
1597
6469.1.15 by Parth Malwankar
added test. warning now explicit
1598
    def test_file_to_symlink_unsupported(self):
1599
        wt = self.make_branch_and_tree('.')
1600
        self.build_tree(['foo'])
1601
        wt.add(['foo'])
1602
        wt.commit("one")
7122.6.6 by Jelmer Vernooij
Fix more tests.
1603
        self.overrideAttr(osutils, 'supports_symlinks', lambda p: False)
6469.1.15 by Parth Malwankar
added test. warning now explicit
1604
        tt = TreeTransform(wt)
1605
        self.addCleanup(tt.finalize)
1606
        foo_trans_id = tt.trans_id_tree_path("foo")
1607
        tt.delete_contents(foo_trans_id)
7122.6.1 by Jelmer Vernooij
merge Parth's work into Breezy
1608
        log = BytesIO()
6469.1.15 by Parth Malwankar
added test. warning now explicit
1609
        trace.push_log_file(log)
7122.6.6 by Jelmer Vernooij
Fix more tests.
1610
        tt.create_symlink("bar", foo_trans_id)
1611
        tt.apply()
6469.1.16 by Parth Malwankar
added diff test
1612
        self.assertContainsRe(
1613
            log.getvalue(),
7122.6.12 by Jelmer Vernooij
Fix more python3 tests.
1614
            b'Unable to create symlink "foo" on this filesystem')
6469.1.15 by Parth Malwankar
added test. warning now explicit
1615
3590.3.3 by James Westby
Make ->file changes work as well.
1616
    def test_dir_to_file(self):
1617
        wt = self.make_branch_and_tree('.')
1618
        self.build_tree(['foo/', 'foo/bar'])
1619
        wt.add(['foo', 'foo/bar'])
1620
        wt.commit("one")
1621
        tt = TreeTransform(wt)
1622
        self.addCleanup(tt.finalize)
1623
        foo_trans_id = tt.trans_id_tree_path("foo")
1624
        bar_trans_id = tt.trans_id_tree_path("foo/bar")
1625
        tt.delete_contents(foo_trans_id)
1626
        tt.delete_versioned(bar_trans_id)
6973.5.10 by Jelmer Vernooij
Random bunch of python3 bee-improvements.
1627
        tt.create_file([b"aa\n"], foo_trans_id)
3590.3.3 by James Westby
Make ->file changes work as well.
1628
        tt.apply()
5784.1.3 by Martin Pool
Switch away from using failUnlessExists and failIfExists
1629
        self.assertPathExists("foo")
3590.3.3 by James Westby
Make ->file changes work as well.
1630
        wt.lock_read()
1631
        self.addCleanup(wt.unlock)
6809.4.7 by Jelmer Vernooij
Swap arguments for get_symlink_target and kind/stored_kind.
1632
        self.assertEqual(wt.kind("foo"), "file")
3590.3.3 by James Westby
Make ->file changes work as well.
1633
3590.3.4 by James Westby
Add a test for creating hardlinks as well.
1634
    def test_dir_to_hardlink(self):
3590.3.5 by James Westby
Use HardlinkFeature for the hardlink test.
1635
        self.requireFeature(HardlinkFeature)
3590.3.4 by James Westby
Add a test for creating hardlinks as well.
1636
        wt = self.make_branch_and_tree('.')
1637
        self.build_tree(['foo/', 'foo/bar'])
1638
        wt.add(['foo', 'foo/bar'])
1639
        wt.commit("one")
1640
        tt = TreeTransform(wt)
1641
        self.addCleanup(tt.finalize)
1642
        foo_trans_id = tt.trans_id_tree_path("foo")
1643
        bar_trans_id = tt.trans_id_tree_path("foo/bar")
1644
        tt.delete_contents(foo_trans_id)
1645
        tt.delete_versioned(bar_trans_id)
1646
        self.build_tree(['baz'])
1647
        tt.create_hardlink("baz", foo_trans_id)
1648
        tt.apply()
5784.1.3 by Martin Pool
Switch away from using failUnlessExists and failIfExists
1649
        self.assertPathExists("foo")
1650
        self.assertPathExists("baz")
3590.3.4 by James Westby
Add a test for creating hardlinks as well.
1651
        wt.lock_read()
1652
        self.addCleanup(wt.unlock)
6809.4.7 by Jelmer Vernooij
Swap arguments for get_symlink_target and kind/stored_kind.
1653
        self.assertEqual(wt.kind("foo"), "file")
3590.3.4 by James Westby
Add a test for creating hardlinks as well.
1654
3619.2.10 by Aaron Bentley
Compensate for stale entries in TT._needs_rename
1655
    def test_no_final_path(self):
1656
        transform, root = self.get_transform()
6973.10.1 by Jelmer Vernooij
Fix some tests.
1657
        trans_id = transform.trans_id_file_id(b'foo')
6973.5.10 by Jelmer Vernooij
Random bunch of python3 bee-improvements.
1658
        transform.create_file([b'bar'], trans_id)
3619.2.10 by Aaron Bentley
Compensate for stale entries in TT._needs_rename
1659
        transform.cancel_creation(trans_id)
1660
        transform.apply()
1661
3363.17.24 by Aaron Bentley
Implement create_by_tree
1662
    def test_create_from_tree(self):
1663
        tree1 = self.make_branch_and_tree('tree1')
6855.4.1 by Jelmer Vernooij
Yet more bees.
1664
        self.build_tree_contents([('tree1/foo/',), ('tree1/bar', b'baz')])
1665
        tree1.add(['foo', 'bar'], [b'foo-id', b'bar-id'])
3363.17.24 by Aaron Bentley
Implement create_by_tree
1666
        tree2 = self.make_branch_and_tree('tree2')
1667
        tt = TreeTransform(tree2)
1668
        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.
1669
        create_from_tree(tt, foo_trans_id, tree1, 'foo')
3363.17.24 by Aaron Bentley
Implement create_by_tree
1670
        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.
1671
        create_from_tree(tt, bar_trans_id, tree1, 'bar')
3363.17.24 by Aaron Bentley
Implement create_by_tree
1672
        tt.apply()
1673
        self.assertEqual('directory', osutils.file_kind('tree2/foo'))
6973.10.1 by Jelmer Vernooij
Fix some tests.
1674
        self.assertFileEqual(b'baz', 'tree2/bar')
3363.17.24 by Aaron Bentley
Implement create_by_tree
1675
3363.17.25 by Aaron Bentley
remove get_inventory_entry, replace with create_from_tree
1676
    def test_create_from_tree_bytes(self):
1677
        """Provided lines are used instead of tree content."""
1678
        tree1 = self.make_branch_and_tree('tree1')
7143.15.2 by Jelmer Vernooij
Run autopep8.
1679
        self.build_tree_contents([('tree1/foo', b'bar'), ])
6855.4.1 by Jelmer Vernooij
Yet more bees.
1680
        tree1.add('foo', b'foo-id')
3363.17.25 by Aaron Bentley
remove get_inventory_entry, replace with create_from_tree
1681
        tree2 = self.make_branch_and_tree('tree2')
1682
        tt = TreeTransform(tree2)
1683
        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.
1684
        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
1685
        tt.apply()
6973.10.1 by Jelmer Vernooij
Fix some tests.
1686
        self.assertFileEqual(b'qux', 'tree2/foo')
3363.17.25 by Aaron Bentley
remove get_inventory_entry, replace with create_from_tree
1687
1688
    def test_create_from_tree_symlink(self):
3363.17.24 by Aaron Bentley
Implement create_by_tree
1689
        self.requireFeature(SymlinkFeature)
1690
        tree1 = self.make_branch_and_tree('tree1')
1691
        os.symlink('bar', 'tree1/foo')
6855.4.1 by Jelmer Vernooij
Yet more bees.
1692
        tree1.add('foo', b'foo-id')
3363.17.24 by Aaron Bentley
Implement create_by_tree
1693
        tt = TreeTransform(self.make_branch_and_tree('tree2'))
1694
        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.
1695
        create_from_tree(tt, foo_trans_id, tree1, 'foo')
3363.17.24 by Aaron Bentley
Implement create_by_tree
1696
        tt.apply()
1697
        self.assertEqual('bar', os.readlink('tree2/foo'))
1698
2502.1.3 by Aaron Bentley
Don't cause errors when creating contents for trans_ids with no parent/name
1699
1534.7.93 by Aaron Bentley
Added text merge test
1700
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).
1701
1731.1.33 by Aaron Bentley
Revert no-special-root changes
1702
    def __init__(self, dirname, root_id):
1534.7.101 by Aaron Bentley
Got conflicts on symlinks working properly
1703
        self.name = dirname
1534.7.93 by Aaron Bentley
Added text merge test
1704
        os.mkdir(dirname)
6472.2.2 by Jelmer Vernooij
Use controldir rather than bzrdir in a couple more places.
1705
        self.wt = ControlDir.create_standalone_workingtree(dirname)
1731.1.33 by Aaron Bentley
Revert no-special-root changes
1706
        self.wt.set_root_id(root_id)
1558.1.3 by Aaron Bentley
Fixed deprecated op use in test suite
1707
        self.b = self.wt.branch
1534.7.93 by Aaron Bentley
Added text merge test
1708
        self.tt = TreeTransform(self.wt)
6885.1.1 by Jelmer Vernooij
Get rid of TreeTransform.trans_id_tree_file_id.
1709
        self.root = self.tt.trans_id_tree_path('')
1534.7.93 by Aaron Bentley
Added text merge test
1710
1551.11.1 by Aaron Bentley
Initial work on converting TreeTransform to iter_changes format
1711
1534.7.95 by Aaron Bentley
Added more text merge tests
1712
def conflict_text(tree, merge):
6973.10.1 by Jelmer Vernooij
Fix some tests.
1713
    template = b'%s TREE\n%s%s\n%s%s MERGE-SOURCE\n'
1714
    return template % (b'<' * 7, tree, b'=' * 7, merge, b'>' * 7)
1534.7.95 by Aaron Bentley
Added more text merge tests
1715
1534.7.93 by Aaron Bentley
Added text merge test
1716
5954.2.2 by Aaron Bentley
Include parent id changes for changed inventory entries.
1717
class TestInventoryAltered(tests.TestCaseWithTransport):
1718
1719
    def test_inventory_altered_unchanged(self):
1720
        tree = self.make_branch_and_tree('tree')
1721
        self.build_tree(['tree/foo'])
6855.4.1 by Jelmer Vernooij
Yet more bees.
1722
        tree.add('foo', b'foo-id')
5954.2.2 by Aaron Bentley
Include parent id changes for changed inventory entries.
1723
        with TransformPreview(tree) as tt:
1724
            self.assertEqual([], tt._inventory_altered())
1725
1726
    def test_inventory_altered_changed_parent_id(self):
1727
        tree = self.make_branch_and_tree('tree')
1728
        self.build_tree(['tree/foo'])
6855.4.1 by Jelmer Vernooij
Yet more bees.
1729
        tree.add('foo', b'foo-id')
5954.2.2 by Aaron Bentley
Include parent id changes for changed inventory entries.
1730
        with TransformPreview(tree) as tt:
1731
            tt.unversion_file(tt.root)
6855.4.1 by Jelmer Vernooij
Yet more bees.
1732
            tt.version_file(b'new-id', tt.root)
6885.1.1 by Jelmer Vernooij
Get rid of TreeTransform.trans_id_tree_file_id.
1733
            foo_trans_id = tt.trans_id_tree_path('foo')
5954.2.2 by Aaron Bentley
Include parent id changes for changed inventory entries.
1734
            foo_tuple = ('foo', foo_trans_id)
1735
            root_tuple = ('', tt.root)
1736
            self.assertEqual([root_tuple, foo_tuple], tt._inventory_altered())
1737
1738
    def test_inventory_altered_noop_changed_parent_id(self):
1739
        tree = self.make_branch_and_tree('tree')
1740
        self.build_tree(['tree/foo'])
6855.4.1 by Jelmer Vernooij
Yet more bees.
1741
        tree.add('foo', b'foo-id')
5954.2.2 by Aaron Bentley
Include parent id changes for changed inventory entries.
1742
        with TransformPreview(tree) as tt:
1743
            tt.unversion_file(tt.root)
1744
            tt.version_file(tree.get_root_id(), tt.root)
7143.15.5 by Jelmer Vernooij
More PEP8 fixes.
1745
            tt.trans_id_tree_path('foo')
5954.2.2 by Aaron Bentley
Include parent id changes for changed inventory entries.
1746
            self.assertEqual([], tt._inventory_altered())
1747
1748
1534.7.93 by Aaron Bentley
Added text merge test
1749
class TestTransformMerge(TestCaseInTempDir):
3199.1.5 by Vincent Ladeuil
Fix two more leaking tmp dirs, by reworking TransformPreview lock handling.
1750
1534.7.93 by Aaron Bentley
Added text merge test
1751
    def test_text_merge(self):
2116.4.1 by John Arbash Meinel
Update file and revision id generators.
1752
        root_id = generate_ids.gen_root_id()
1731.1.33 by Aaron Bentley
Revert no-special-root changes
1753
        base = TransformGroup("base", root_id)
6973.10.1 by Jelmer Vernooij
Fix some tests.
1754
        base.tt.new_file('a', base.root, [b'a\nb\nc\nd\be\n'], b'a')
1755
        base.tt.new_file('b', base.root, [b'b1'], b'b')
1756
        base.tt.new_file('c', base.root, [b'c'], b'c')
1757
        base.tt.new_file('d', base.root, [b'd'], b'd')
1758
        base.tt.new_file('e', base.root, [b'e'], b'e')
1759
        base.tt.new_file('f', base.root, [b'f'], b'f')
6973.5.10 by Jelmer Vernooij
Random bunch of python3 bee-improvements.
1760
        base.tt.new_directory('g', base.root, b'g')
1761
        base.tt.new_directory('h', base.root, b'h')
1534.7.93 by Aaron Bentley
Added text merge test
1762
        base.tt.apply()
1731.1.33 by Aaron Bentley
Revert no-special-root changes
1763
        other = TransformGroup("other", root_id)
6973.10.1 by Jelmer Vernooij
Fix some tests.
1764
        other.tt.new_file('a', other.root, [b'y\nb\nc\nd\be\n'], b'a')
1765
        other.tt.new_file('b', other.root, [b'b2'], b'b')
1766
        other.tt.new_file('c', other.root, [b'c2'], b'c')
1767
        other.tt.new_file('d', other.root, [b'd'], b'd')
1768
        other.tt.new_file('e', other.root, [b'e2'], b'e')
1769
        other.tt.new_file('f', other.root, [b'f'], b'f')
1770
        other.tt.new_file('g', other.root, [b'g'], b'g')
1771
        other.tt.new_file('h', other.root, [b'h\ni\nj\nk\n'], b'h')
1772
        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
1773
        other.tt.apply()
1731.1.33 by Aaron Bentley
Revert no-special-root changes
1774
        this = TransformGroup("this", root_id)
6973.10.1 by Jelmer Vernooij
Fix some tests.
1775
        this.tt.new_file('a', this.root, [b'a\nb\nc\nd\bz\n'], b'a')
1776
        this.tt.new_file('b', this.root, [b'b'], b'b')
1777
        this.tt.new_file('c', this.root, [b'c'], b'c')
1778
        this.tt.new_file('d', this.root, [b'd2'], b'd')
1779
        this.tt.new_file('e', this.root, [b'e2'], b'e')
1780
        this.tt.new_file('f', this.root, [b'f'], b'f')
1781
        this.tt.new_file('g', this.root, [b'g'], b'g')
1782
        this.tt.new_file('h', this.root, [b'1\n2\n3\n4\n'], b'h')
1783
        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
1784
        this.tt.apply()
3008.1.11 by Michael Hudson
restore the default behaviour of Merge3Merger.__init__().
1785
        Merge3Merger(this.wt, this.wt, base.wt, other.wt)
3008.1.6 by Michael Hudson
chop up Merge3Merger.__init__ into pieces
1786
1534.7.95 by Aaron Bentley
Added more text merge tests
1787
        # textual merge
6973.10.1 by Jelmer Vernooij
Fix some tests.
1788
        with this.wt.get_file(this.wt.id2path(b'a')) as f:
1789
            self.assertEqual(f.read(), b'y\nb\nc\nd\bz\n')
1534.7.95 by Aaron Bentley
Added more text merge tests
1790
        # three-way text conflict
6973.10.1 by Jelmer Vernooij
Fix some tests.
1791
        with this.wt.get_file(this.wt.id2path(b'b')) as f:
7029.4.2 by Jelmer Vernooij
Fix more merge tests.
1792
            self.assertEqual(f.read(), conflict_text(b'b', b'b2'))
1534.7.95 by Aaron Bentley
Added more text merge tests
1793
        # OTHER wins
6973.5.10 by Jelmer Vernooij
Random bunch of python3 bee-improvements.
1794
        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
1795
        # THIS wins
6973.5.10 by Jelmer Vernooij
Random bunch of python3 bee-improvements.
1796
        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
1797
        # Ambigious clean merge
6973.5.10 by Jelmer Vernooij
Random bunch of python3 bee-improvements.
1798
        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
1799
        # No change
6973.5.10 by Jelmer Vernooij
Random bunch of python3 bee-improvements.
1800
        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
1801
        # Correct correct results when THIS == OTHER
6973.5.10 by Jelmer Vernooij
Random bunch of python3 bee-improvements.
1802
        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
1803
        # Text conflict when THIS & OTHER are text and BASE is dir
6855.4.1 by Jelmer Vernooij
Yet more bees.
1804
        self.assertEqual(this.wt.get_file(this.wt.id2path(b'h')).read(),
6973.10.1 by Jelmer Vernooij
Fix some tests.
1805
                         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_*.
1806
        self.assertEqual(this.wt.get_file('h.THIS').read(),
6973.5.10 by Jelmer Vernooij
Random bunch of python3 bee-improvements.
1807
                         b'1\n2\n3\n4\n')
6809.4.5 by Jelmer Vernooij
Swap arguments for get_file_*.
1808
        self.assertEqual(this.wt.get_file('h.OTHER').read(),
6973.5.10 by Jelmer Vernooij
Random bunch of python3 bee-improvements.
1809
                         b'h\ni\nj\nk\n')
1534.7.97 by Aaron Bentley
Ensured foo.BASE is a directory if there's a conflict
1810
        self.assertEqual(file_kind(this.wt.abspath('h.BASE')), 'directory')
6855.4.1 by Jelmer Vernooij
Yet more bees.
1811
        self.assertEqual(this.wt.get_file(this.wt.id2path(b'i')).read(),
6973.5.10 by Jelmer Vernooij
Random bunch of python3 bee-improvements.
1812
                         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_*.
1813
        self.assertEqual(this.wt.get_file('i.THIS').read(),
6973.5.10 by Jelmer Vernooij
Random bunch of python3 bee-improvements.
1814
                         b'1\n2\n3\n4\n')
6809.4.5 by Jelmer Vernooij
Swap arguments for get_file_*.
1815
        self.assertEqual(this.wt.get_file('i.OTHER').read(),
6973.5.10 by Jelmer Vernooij
Random bunch of python3 bee-improvements.
1816
                         b'h\ni\nj\nk\n')
1534.7.99 by Aaron Bentley
Handle non-existent BASE properly
1817
        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.
1818
        modified = ['a', 'b', 'c', 'h', 'i']
1534.7.192 by Aaron Bentley
Record hashes produced by merges
1819
        merge_modified = this.wt.merge_modified()
1820
        self.assertSubset(merge_modified, modified)
1821
        self.assertEqual(len(merge_modified), len(modified))
7143.15.2 by Jelmer Vernooij
Run autopep8.
1822
        with open(this.wt.abspath(this.wt.id2path(b'a')), 'wb') as f:
1823
            f.write(b'booga')
1534.7.192 by Aaron Bentley
Record hashes produced by merges
1824
        modified.pop(0)
1825
        merge_modified = this.wt.merge_modified()
1826
        self.assertSubset(merge_modified, modified)
1827
        self.assertEqual(len(merge_modified), len(modified))
1558.12.10 by Aaron Bentley
Be robust when merge_hash file_id not in inventory
1828
        this.wt.remove('b')
2796.1.4 by Aaron Bentley
Fix up various test cases
1829
        this.wt.revert()
1534.7.101 by Aaron Bentley
Got conflicts on symlinks working properly
1830
1831
    def test_file_merge(self):
2949.5.1 by Alexander Belchenko
selftest: use SymlinkFeature instead of TestSkipped where appropriate
1832
        self.requireFeature(SymlinkFeature)
2116.4.1 by John Arbash Meinel
Update file and revision id generators.
1833
        root_id = generate_ids.gen_root_id()
1731.1.33 by Aaron Bentley
Revert no-special-root changes
1834
        base = TransformGroup("BASE", root_id)
1835
        this = TransformGroup("THIS", root_id)
1836
        other = TransformGroup("OTHER", root_id)
1534.7.101 by Aaron Bentley
Got conflicts on symlinks working properly
1837
        for tg in this, base, other:
6973.5.10 by Jelmer Vernooij
Random bunch of python3 bee-improvements.
1838
            tg.tt.new_directory('a', tg.root, b'a')
6973.10.1 by Jelmer Vernooij
Fix some tests.
1839
            tg.tt.new_symlink('b', tg.root, 'b', b'b')
1840
            tg.tt.new_file('c', tg.root, [b'c'], b'c')
6973.5.10 by Jelmer Vernooij
Random bunch of python3 bee-improvements.
1841
            tg.tt.new_symlink('d', tg.root, tg.name, b'd')
3943.8.1 by Marius Kruger
remove all trailing whitespace from bzr source
1842
        targets = ((base, 'base-e', 'base-f', None, None),
1843
                   (this, 'other-e', 'this-f', 'other-g', 'this-h'),
1534.7.104 by Aaron Bentley
Fixed set_versioned, enhanced conflict testing
1844
                   (other, 'other-e', None, 'other-g', 'other-h'))
1845
        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
1846
            for link, target in (('e', e_target), ('f', f_target),
1534.7.104 by Aaron Bentley
Fixed set_versioned, enhanced conflict testing
1847
                                 ('g', g_target), ('h', h_target)):
1848
                if target is not None:
7143.15.2 by Jelmer Vernooij
Run autopep8.
1849
                    tg.tt.new_symlink(link, tg.root, target,
1850
                                      link.encode('ascii'))
1534.7.102 by Aaron Bentley
Deleted old pre-conflict contents
1851
1852
        for tg in this, base, other:
1534.7.101 by Aaron Bentley
Got conflicts on symlinks working properly
1853
            tg.tt.apply()
3008.1.11 by Michael Hudson
restore the default behaviour of Merge3Merger.__init__().
1854
        Merge3Merger(this.wt, this.wt, base.wt, other.wt)
1534.7.101 by Aaron Bentley
Got conflicts on symlinks working properly
1855
        self.assertIs(os.path.isdir(this.wt.abspath('a')), True)
1856
        self.assertIs(os.path.islink(this.wt.abspath('b')), True)
1857
        self.assertIs(os.path.isfile(this.wt.abspath('c')), True)
1858
        for suffix in ('THIS', 'BASE', 'OTHER'):
7143.15.2 by Jelmer Vernooij
Run autopep8.
1859
            self.assertEqual(os.readlink(
1860
                this.wt.abspath('d.' + suffix)), suffix)
1534.7.102 by Aaron Bentley
Deleted old pre-conflict contents
1861
        self.assertIs(os.path.lexists(this.wt.abspath('d')), False)
6855.4.1 by Jelmer Vernooij
Yet more bees.
1862
        self.assertEqual(this.wt.id2path(b'd'), 'd.OTHER')
1863
        self.assertEqual(this.wt.id2path(b'f'), 'f.THIS')
1534.7.102 by Aaron Bentley
Deleted old pre-conflict contents
1864
        self.assertEqual(os.readlink(this.wt.abspath('e')), 'other-e')
1865
        self.assertIs(os.path.lexists(this.wt.abspath('e.THIS')), False)
1866
        self.assertIs(os.path.lexists(this.wt.abspath('e.OTHER')), False)
1867
        self.assertIs(os.path.lexists(this.wt.abspath('e.BASE')), False)
1534.7.104 by Aaron Bentley
Fixed set_versioned, enhanced conflict testing
1868
        self.assertIs(os.path.lexists(this.wt.abspath('g')), True)
1869
        self.assertIs(os.path.lexists(this.wt.abspath('g.BASE')), False)
1870
        self.assertIs(os.path.lexists(this.wt.abspath('h')), False)
1871
        self.assertIs(os.path.lexists(this.wt.abspath('h.BASE')), False)
1872
        self.assertIs(os.path.lexists(this.wt.abspath('h.THIS')), True)
1873
        self.assertIs(os.path.lexists(this.wt.abspath('h.OTHER')), True)
1534.7.105 by Aaron Bentley
Got merge with rename working
1874
1875
    def test_filename_merge(self):
2116.4.1 by John Arbash Meinel
Update file and revision id generators.
1876
        root_id = generate_ids.gen_root_id()
1731.1.33 by Aaron Bentley
Revert no-special-root changes
1877
        base = TransformGroup("BASE", root_id)
1878
        this = TransformGroup("THIS", root_id)
1879
        other = TransformGroup("OTHER", root_id)
7029.4.2 by Jelmer Vernooij
Fix more merge tests.
1880
        base_a, this_a, other_a = [t.tt.new_directory('a', t.root, b'a')
1881
                                   for t in [base, this, other]]
1882
        base_b, this_b, other_b = [t.tt.new_directory('b', t.root, b'b')
1883
                                   for t in [base, this, other]]
1884
        base.tt.new_directory('c', base_a, b'c')
1885
        this.tt.new_directory('c1', this_a, b'c')
1886
        other.tt.new_directory('c', other_b, b'c')
1887
1888
        base.tt.new_directory('d', base_a, b'd')
1889
        this.tt.new_directory('d1', this_b, b'd')
1890
        other.tt.new_directory('d', other_a, b'd')
1891
1892
        base.tt.new_directory('e', base_a, b'e')
1893
        this.tt.new_directory('e', this_a, b'e')
1894
        other.tt.new_directory('e1', other_b, b'e')
1895
1896
        base.tt.new_directory('f', base_a, b'f')
1897
        this.tt.new_directory('f1', this_b, b'f')
1898
        other.tt.new_directory('f1', other_b, b'f')
1534.7.105 by Aaron Bentley
Got merge with rename working
1899
1900
        for tg in [this, base, other]:
1901
            tg.tt.apply()
3008.1.11 by Michael Hudson
restore the default behaviour of Merge3Merger.__init__().
1902
        Merge3Merger(this.wt, this.wt, base.wt, other.wt)
6855.4.1 by Jelmer Vernooij
Yet more bees.
1903
        self.assertEqual(this.wt.id2path(b'c'), pathjoin('b/c1'))
1904
        self.assertEqual(this.wt.id2path(b'd'), pathjoin('b/d1'))
1905
        self.assertEqual(this.wt.id2path(b'e'), pathjoin('b/e1'))
1906
        self.assertEqual(this.wt.id2path(b'f'), pathjoin('b/f1'))
1534.7.105 by Aaron Bentley
Got merge with rename working
1907
1908
    def test_filename_merge_conflicts(self):
2116.4.1 by John Arbash Meinel
Update file and revision id generators.
1909
        root_id = generate_ids.gen_root_id()
1731.1.33 by Aaron Bentley
Revert no-special-root changes
1910
        base = TransformGroup("BASE", root_id)
1911
        this = TransformGroup("THIS", root_id)
1912
        other = TransformGroup("OTHER", root_id)
7029.4.2 by Jelmer Vernooij
Fix more merge tests.
1913
        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
1914
                                   for t in [base, this, other]]
7029.4.2 by Jelmer Vernooij
Fix more merge tests.
1915
        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
1916
                                   for t in [base, this, other]]
1917
6973.10.1 by Jelmer Vernooij
Fix some tests.
1918
        base.tt.new_file('g', base_a, [b'g'], b'g')
1919
        other.tt.new_file('g1', other_b, [b'g1'], b'g')
1920
1921
        base.tt.new_file('h', base_a, [b'h'], b'h')
1922
        this.tt.new_file('h1', this_b, [b'h1'], b'h')
1923
1924
        base.tt.new_file('i', base.root, [b'i'], b'i')
1925
        other.tt.new_directory('i1', this_b, b'i')
1534.7.105 by Aaron Bentley
Got merge with rename working
1926
1927
        for tg in [this, base, other]:
1928
            tg.tt.apply()
3008.1.11 by Michael Hudson
restore the default behaviour of Merge3Merger.__init__().
1929
        Merge3Merger(this.wt, this.wt, base.wt, other.wt)
1534.7.105 by Aaron Bentley
Got merge with rename working
1930
6855.4.1 by Jelmer Vernooij
Yet more bees.
1931
        self.assertEqual(this.wt.id2path(b'g'), pathjoin('b/g1.OTHER'))
1534.7.105 by Aaron Bentley
Got merge with rename working
1932
        self.assertIs(os.path.lexists(this.wt.abspath('b/g1.BASE')), True)
1933
        self.assertIs(os.path.lexists(this.wt.abspath('b/g1.THIS')), False)
6855.4.1 by Jelmer Vernooij
Yet more bees.
1934
        self.assertEqual(this.wt.id2path(b'h'), pathjoin('b/h1.THIS'))
1534.7.105 by Aaron Bentley
Got merge with rename working
1935
        self.assertIs(os.path.lexists(this.wt.abspath('b/h1.BASE')), True)
1936
        self.assertIs(os.path.lexists(this.wt.abspath('b/h1.OTHER')), False)
6855.4.1 by Jelmer Vernooij
Yet more bees.
1937
        self.assertEqual(this.wt.id2path(b'i'), pathjoin('b/i1.OTHER'))
1534.7.183 by Aaron Bentley
Fixed build_tree with symlinks
1938
2027.1.1 by John Arbash Meinel
Fix bug #56549, and write a direct test that the right path is being statted
1939
1966.1.1 by Aaron Bentley
Implement disk-content merge and conflict resolution for build_tree
1940
class TestBuildTree(tests.TestCaseWithTransport):
1941
3006.2.2 by Alexander Belchenko
tests added.
1942
    def test_build_tree_with_symlinks(self):
2949.5.1 by Alexander Belchenko
selftest: use SymlinkFeature instead of TestSkipped where appropriate
1943
        self.requireFeature(SymlinkFeature)
1534.7.183 by Aaron Bentley
Fixed build_tree with symlinks
1944
        os.mkdir('a')
6472.2.2 by Jelmer Vernooij
Use controldir rather than bzrdir in a couple more places.
1945
        a = ControlDir.create_standalone_workingtree('a')
1534.7.183 by Aaron Bentley
Fixed build_tree with symlinks
1946
        os.mkdir('a/foo')
7143.15.2 by Jelmer Vernooij
Run autopep8.
1947
        with open('a/foo/bar', 'wb') as f:
1948
            f.write(b'contents')
1534.7.183 by Aaron Bentley
Fixed build_tree with symlinks
1949
        os.symlink('a/foo/bar', 'a/foo/baz')
1950
        a.add(['foo', 'foo/bar', 'foo/baz'])
1951
        a.commit('initial commit')
6472.2.2 by Jelmer Vernooij
Use controldir rather than bzrdir in a couple more places.
1952
        b = ControlDir.create_standalone_workingtree('b')
2255.2.194 by Robert Collins
[BROKEN] Many updates to stop using experimental formats in tests.
1953
        basis = a.basis_tree()
1954
        basis.lock_read()
1955
        self.addCleanup(basis.unlock)
1956
        build_tree(basis, b)
1534.7.183 by Aaron Bentley
Fixed build_tree with symlinks
1957
        self.assertIs(os.path.isdir('b/foo'), True)
6973.7.5 by Jelmer Vernooij
s/file/open.
1958
        with open('b/foo/bar', 'rb') as f:
7045.4.7 by Jelmer Vernooij
Fix remaining tests.
1959
            self.assertEqual(f.read(), b"contents")
1534.7.183 by Aaron Bentley
Fixed build_tree with symlinks
1960
        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
1961
2100.3.21 by Aaron Bentley
Work on checking out by-reference trees
1962
    def test_build_with_references(self):
2255.2.194 by Robert Collins
[BROKEN] Many updates to stop using experimental formats in tests.
1963
        tree = self.make_branch_and_tree('source',
7143.15.2 by Jelmer Vernooij
Run autopep8.
1964
                                         format='development-subtree')
2255.2.194 by Robert Collins
[BROKEN] Many updates to stop using experimental formats in tests.
1965
        subtree = self.make_branch_and_tree('source/subtree',
7143.15.2 by Jelmer Vernooij
Run autopep8.
1966
                                            format='development-subtree')
2100.3.21 by Aaron Bentley
Work on checking out by-reference trees
1967
        tree.add_reference(subtree)
1968
        tree.commit('a revision')
1969
        tree.branch.create_checkout('target')
5784.1.3 by Martin Pool
Switch away from using failUnlessExists and failIfExists
1970
        self.assertPathExists('target')
1971
        self.assertPathExists('target/subtree')
2100.3.21 by Aaron Bentley
Work on checking out by-reference trees
1972
1966.1.1 by Aaron Bentley
Implement disk-content merge and conflict resolution for build_tree
1973
    def test_file_conflict_handling(self):
1974
        """Ensure that when building trees, conflict handling is done"""
1975
        source = self.make_branch_and_tree('source')
1976
        target = self.make_branch_and_tree('target')
1977
        self.build_tree(['source/file', 'target/file'])
6973.5.10 by Jelmer Vernooij
Random bunch of python3 bee-improvements.
1978
        source.add('file', b'new-file')
1966.1.1 by Aaron Bentley
Implement disk-content merge and conflict resolution for build_tree
1979
        source.commit('added file')
1980
        build_tree(source.basis_tree(), target)
7143.15.5 by Jelmer Vernooij
More PEP8 fixes.
1981
        self.assertEqual(
1982
            [DuplicateEntry('Moved existing file to', 'file.moved',
1983
                            'file', None, 'new-file')],
1984
            target.conflicts())
1966.1.1 by Aaron Bentley
Implement disk-content merge and conflict resolution for build_tree
1985
        target2 = self.make_branch_and_tree('target2')
6973.7.5 by Jelmer Vernooij
s/file/open.
1986
        with open('target2/file', 'wb') as target_file, \
1987
                open('source/file', 'rb') as source_file:
1988
            target_file.write(source_file.read())
1966.1.1 by Aaron Bentley
Implement disk-content merge and conflict resolution for build_tree
1989
        build_tree(source.basis_tree(), target2)
1966.1.2 by Aaron Bentley
Divert files instead of failing to create them, update from review
1990
        self.assertEqual([], target2.conflicts())
1966.1.1 by Aaron Bentley
Implement disk-content merge and conflict resolution for build_tree
1991
1992
    def test_symlink_conflict_handling(self):
1993
        """Ensure that when building trees, conflict handling is done"""
2949.5.1 by Alexander Belchenko
selftest: use SymlinkFeature instead of TestSkipped where appropriate
1994
        self.requireFeature(SymlinkFeature)
1966.1.1 by Aaron Bentley
Implement disk-content merge and conflict resolution for build_tree
1995
        source = self.make_branch_and_tree('source')
1996
        os.symlink('foo', 'source/symlink')
6973.5.10 by Jelmer Vernooij
Random bunch of python3 bee-improvements.
1997
        source.add('symlink', b'new-symlink')
1966.1.1 by Aaron Bentley
Implement disk-content merge and conflict resolution for build_tree
1998
        source.commit('added file')
1999
        target = self.make_branch_and_tree('target')
2000
        os.symlink('bar', 'target/symlink')
2001
        build_tree(source.basis_tree(), target)
7143.15.5 by Jelmer Vernooij
More PEP8 fixes.
2002
        self.assertEqual(
2003
            [DuplicateEntry('Moved existing file to', 'symlink.moved',
2004
                            'symlink', None, 'new-symlink')],
2005
            target.conflicts())
1966.1.1 by Aaron Bentley
Implement disk-content merge and conflict resolution for build_tree
2006
        target = self.make_branch_and_tree('target2')
2007
        os.symlink('foo', 'target2/symlink')
2008
        build_tree(source.basis_tree(), target)
1966.1.2 by Aaron Bentley
Divert files instead of failing to create them, update from review
2009
        self.assertEqual([], target.conflicts())
3943.8.1 by Marius Kruger
remove all trailing whitespace from bzr source
2010
1966.1.1 by Aaron Bentley
Implement disk-content merge and conflict resolution for build_tree
2011
    def test_directory_conflict_handling(self):
2012
        """Ensure that when building trees, conflict handling is done"""
2013
        source = self.make_branch_and_tree('source')
2014
        target = self.make_branch_and_tree('target')
2015
        self.build_tree(['source/dir1/', 'source/dir1/file', 'target/dir1/'])
6973.5.10 by Jelmer Vernooij
Random bunch of python3 bee-improvements.
2016
        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
2017
        source.commit('added file')
2018
        build_tree(source.basis_tree(), target)
1966.1.2 by Aaron Bentley
Divert files instead of failing to create them, update from review
2019
        self.assertEqual([], target.conflicts())
5784.1.3 by Martin Pool
Switch away from using failUnlessExists and failIfExists
2020
        self.assertPathExists('target/dir1/file')
1966.1.1 by Aaron Bentley
Implement disk-content merge and conflict resolution for build_tree
2021
2022
        # Ensure contents are merged
2023
        target = self.make_branch_and_tree('target2')
2024
        self.build_tree(['target2/dir1/', 'target2/dir1/file2'])
2025
        build_tree(source.basis_tree(), target)
1966.1.2 by Aaron Bentley
Divert files instead of failing to create them, update from review
2026
        self.assertEqual([], target.conflicts())
5784.1.3 by Martin Pool
Switch away from using failUnlessExists and failIfExists
2027
        self.assertPathExists('target2/dir1/file2')
2028
        self.assertPathExists('target2/dir1/file')
1966.1.1 by Aaron Bentley
Implement disk-content merge and conflict resolution for build_tree
2029
2030
        # 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
2031
        target = self.make_branch_and_tree('target3')
2032
        self.make_branch('target3/dir1')
1966.1.1 by Aaron Bentley
Implement disk-content merge and conflict resolution for build_tree
2033
        self.build_tree(['target3/dir1/file2'])
2034
        build_tree(source.basis_tree(), target)
5784.1.3 by Martin Pool
Switch away from using failUnlessExists and failIfExists
2035
        self.assertPathDoesNotExist('target3/dir1/file')
2036
        self.assertPathExists('target3/dir1/file2')
2037
        self.assertPathExists('target3/dir1.diverted/file')
7143.15.5 by Jelmer Vernooij
More PEP8 fixes.
2038
        self.assertEqual(
2039
            [DuplicateEntry('Diverted to', 'dir1.diverted',
2040
                            'dir1', 'new-dir1', None)],
2041
            target.conflicts())
1966.1.2 by Aaron Bentley
Divert files instead of failing to create them, update from review
2042
2043
        target = self.make_branch_and_tree('target4')
2044
        self.build_tree(['target4/dir1/'])
2045
        self.make_branch('target4/dir1/file')
2046
        build_tree(source.basis_tree(), target)
5784.1.3 by Martin Pool
Switch away from using failUnlessExists and failIfExists
2047
        self.assertPathExists('target4/dir1/file')
1966.1.2 by Aaron Bentley
Divert files instead of failing to create them, update from review
2048
        self.assertEqual('directory', file_kind('target4/dir1/file'))
5784.1.3 by Martin Pool
Switch away from using failUnlessExists and failIfExists
2049
        self.assertPathExists('target4/dir1/file.diverted')
7143.15.5 by Jelmer Vernooij
More PEP8 fixes.
2050
        self.assertEqual(
2051
            [DuplicateEntry('Diverted to', 'dir1/file.diverted',
2052
                            'dir1/file', 'new-file', None)],
2053
            target.conflicts())
1966.1.1 by Aaron Bentley
Implement disk-content merge and conflict resolution for build_tree
2054
2055
    def test_mixed_conflict_handling(self):
2056
        """Ensure that when building trees, conflict handling is done"""
2057
        source = self.make_branch_and_tree('source')
2058
        target = self.make_branch_and_tree('target')
2059
        self.build_tree(['source/name', 'target/name/'])
6973.5.10 by Jelmer Vernooij
Random bunch of python3 bee-improvements.
2060
        source.add('name', b'new-name')
1966.1.2 by Aaron Bentley
Divert files instead of failing to create them, update from review
2061
        source.commit('added file')
2062
        build_tree(source.basis_tree(), target)
7143.15.5 by Jelmer Vernooij
More PEP8 fixes.
2063
        self.assertEqual(
2064
            [DuplicateEntry('Moved existing file to',
2065
                            'name.moved', 'name', None, 'new-name')],
2066
            target.conflicts())
1966.1.2 by Aaron Bentley
Divert files instead of failing to create them, update from review
2067
2068
    def test_raises_in_populated(self):
2069
        source = self.make_branch_and_tree('source')
2070
        self.build_tree(['source/name'])
1966.1.1 by Aaron Bentley
Implement disk-content merge and conflict resolution for build_tree
2071
        source.add('name')
1966.1.2 by Aaron Bentley
Divert files instead of failing to create them, update from review
2072
        source.commit('added name')
2073
        target = self.make_branch_and_tree('target')
2074
        self.build_tree(['target/name'])
2075
        target.add('name')
3943.8.1 by Marius Kruger
remove all trailing whitespace from bzr source
2076
        self.assertRaises(errors.WorkingTreeAlreadyPopulated,
7143.15.2 by Jelmer Vernooij
Run autopep8.
2077
                          build_tree, source.basis_tree(), target)
1966.1.1 by Aaron Bentley
Implement disk-content merge and conflict resolution for build_tree
2078
2502.1.1 by Aaron Bentley
Ensure renames only root children are renamed when building trees
2079
    def test_build_tree_rename_count(self):
2080
        source = self.make_branch_and_tree('source')
2081
        self.build_tree(['source/file1', 'source/dir1/'])
2082
        source.add(['file1', 'dir1'])
2083
        source.commit('add1')
2084
        target1 = self.make_branch_and_tree('target1')
2502.1.6 by Aaron Bentley
Update from review comments
2085
        transform_result = build_tree(source.basis_tree(), target1)
2086
        self.assertEqual(2, transform_result.rename_count)
2502.1.1 by Aaron Bentley
Ensure renames only root children are renamed when building trees
2087
2088
        self.build_tree(['source/dir1/file2'])
2089
        source.add(['dir1/file2'])
2090
        source.commit('add3')
2091
        target2 = self.make_branch_and_tree('target2')
2502.1.6 by Aaron Bentley
Update from review comments
2092
        transform_result = build_tree(source.basis_tree(), target2)
2502.1.1 by Aaron Bentley
Ensure renames only root children are renamed when building trees
2093
        # children of non-root directories should not be renamed
2502.1.6 by Aaron Bentley
Update from review comments
2094
        self.assertEqual(2, transform_result.rename_count)
2502.1.1 by Aaron Bentley
Ensure renames only root children are renamed when building trees
2095
3136.1.2 by Aaron Bentley
Implement hard-linking for build_tree
2096
    def create_ab_tree(self):
2097
        """Create a committed test tree with two files"""
2098
        source = self.make_branch_and_tree('source')
6855.4.1 by Jelmer Vernooij
Yet more bees.
2099
        self.build_tree_contents([('source/file1', b'A')])
2100
        self.build_tree_contents([('source/file2', b'B')])
2101
        source.add(['file1', 'file2'], [b'file1-id', b'file2-id'])
3136.1.2 by Aaron Bentley
Implement hard-linking for build_tree
2102
        source.commit('commit files')
2103
        source.lock_write()
2104
        self.addCleanup(source.unlock)
2105
        return source
2106
3123.5.1 by Aaron Bentley
Make build-tree able to use an additional 'accelerator' tree
2107
    def test_build_tree_accelerator_tree(self):
3136.1.2 by Aaron Bentley
Implement hard-linking for build_tree
2108
        source = self.create_ab_tree()
6855.4.1 by Jelmer Vernooij
Yet more bees.
2109
        self.build_tree_contents([('source/file2', b'C')])
3123.5.1 by Aaron Bentley
Make build-tree able to use an additional 'accelerator' tree
2110
        calls = []
2111
        real_source_get_file = source.get_file
7143.15.2 by Jelmer Vernooij
Run autopep8.
2112
7192.5.1 by Jelmer Vernooij
Remove more file ids.
2113
        def get_file(path):
2114
            calls.append(path)
2115
            return real_source_get_file(path)
3123.5.1 by Aaron Bentley
Make build-tree able to use an additional 'accelerator' tree
2116
        source.get_file = get_file
2117
        target = self.make_branch_and_tree('target')
3123.5.19 by Aaron Bentley
Ensure content is exactly the same, when accelerator used
2118
        revision_tree = source.basis_tree()
2119
        revision_tree.lock_read()
2120
        self.addCleanup(revision_tree.unlock)
2121
        build_tree(revision_tree, target, source)
7192.5.1 by Jelmer Vernooij
Remove more file ids.
2122
        self.assertEqual(['file1'], calls)
3123.5.19 by Aaron Bentley
Ensure content is exactly the same, when accelerator used
2123
        target.lock_read()
2124
        self.addCleanup(target.unlock)
3254.1.1 by Aaron Bentley
Make Tree.iter_changes a public method
2125
        self.assertEqual([], list(target.iter_changes(revision_tree)))
3123.5.1 by Aaron Bentley
Make build-tree able to use an additional 'accelerator' tree
2126
5752.5.1 by John Arbash Meinel
Merge up transform-create-file
2127
    def test_build_tree_accelerator_tree_observes_sha1(self):
2128
        source = self.create_ab_tree()
6973.10.1 by Jelmer Vernooij
Fix some tests.
2129
        sha1 = osutils.sha_string(b'A')
5752.5.1 by John Arbash Meinel
Merge up transform-create-file
2130
        target = self.make_branch_and_tree('target')
2131
        target.lock_write()
2132
        self.addCleanup(target.unlock)
2133
        state = target.current_dirstate()
2134
        state._cutoff_time = time.time() + 60
2135
        build_tree(source.basis_tree(), target, source)
6973.10.1 by Jelmer Vernooij
Fix some tests.
2136
        entry = state._get_entry(0, path_utf8=b'file1')
5752.5.1 by John Arbash Meinel
Merge up transform-create-file
2137
        self.assertEqual(sha1, entry[1][0][1])
2138
3123.5.4 by Aaron Bentley
Use an accelerator tree when branching, handle no-such-id correctly
2139
    def test_build_tree_accelerator_tree_missing_file(self):
3136.1.2 by Aaron Bentley
Implement hard-linking for build_tree
2140
        source = self.create_ab_tree()
3123.5.4 by Aaron Bentley
Use an accelerator tree when branching, handle no-such-id correctly
2141
        os.unlink('source/file1')
2142
        source.remove(['file2'])
2143
        target = self.make_branch_and_tree('target')
3123.5.19 by Aaron Bentley
Ensure content is exactly the same, when accelerator used
2144
        revision_tree = source.basis_tree()
2145
        revision_tree.lock_read()
2146
        self.addCleanup(revision_tree.unlock)
2147
        build_tree(revision_tree, target, source)
2148
        target.lock_read()
2149
        self.addCleanup(target.unlock)
3254.1.1 by Aaron Bentley
Make Tree.iter_changes a public method
2150
        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
2151
3123.5.16 by Aaron Bentley
Test handling of conversion to non-file
2152
    def test_build_tree_accelerator_wrong_kind(self):
3146.4.8 by Aaron Bentley
Add missing symlink requirement
2153
        self.requireFeature(SymlinkFeature)
3123.5.16 by Aaron Bentley
Test handling of conversion to non-file
2154
        source = self.make_branch_and_tree('source')
6855.4.1 by Jelmer Vernooij
Yet more bees.
2155
        self.build_tree_contents([('source/file1', b'')])
2156
        self.build_tree_contents([('source/file2', b'')])
2157
        source.add(['file1', 'file2'], [b'file1-id', b'file2-id'])
3123.5.16 by Aaron Bentley
Test handling of conversion to non-file
2158
        source.commit('commit files')
2159
        os.unlink('source/file2')
6855.4.1 by Jelmer Vernooij
Yet more bees.
2160
        self.build_tree_contents([('source/file2/', b'C')])
3123.5.16 by Aaron Bentley
Test handling of conversion to non-file
2161
        os.unlink('source/file1')
2162
        os.symlink('file2', 'source/file1')
2163
        calls = []
2164
        real_source_get_file = source.get_file
7143.15.2 by Jelmer Vernooij
Run autopep8.
2165
7192.5.1 by Jelmer Vernooij
Remove more file ids.
2166
        def get_file(path):
2167
            calls.append(path)
2168
            return real_source_get_file(path)
3123.5.16 by Aaron Bentley
Test handling of conversion to non-file
2169
        source.get_file = get_file
2170
        target = self.make_branch_and_tree('target')
3123.5.19 by Aaron Bentley
Ensure content is exactly the same, when accelerator used
2171
        revision_tree = source.basis_tree()
2172
        revision_tree.lock_read()
2173
        self.addCleanup(revision_tree.unlock)
2174
        build_tree(revision_tree, target, source)
3123.5.16 by Aaron Bentley
Test handling of conversion to non-file
2175
        self.assertEqual([], calls)
3123.5.19 by Aaron Bentley
Ensure content is exactly the same, when accelerator used
2176
        target.lock_read()
2177
        self.addCleanup(target.unlock)
3254.1.1 by Aaron Bentley
Make Tree.iter_changes a public method
2178
        self.assertEqual([], list(target.iter_changes(revision_tree)))
3123.5.16 by Aaron Bentley
Test handling of conversion to non-file
2179
3136.1.2 by Aaron Bentley
Implement hard-linking for build_tree
2180
    def test_build_tree_hardlink(self):
2181
        self.requireFeature(HardlinkFeature)
2182
        source = self.create_ab_tree()
2183
        target = self.make_branch_and_tree('target')
2184
        revision_tree = source.basis_tree()
2185
        revision_tree.lock_read()
2186
        self.addCleanup(revision_tree.unlock)
2187
        build_tree(revision_tree, target, source, hardlink=True)
2188
        target.lock_read()
2189
        self.addCleanup(target.unlock)
3254.1.1 by Aaron Bentley
Make Tree.iter_changes a public method
2190
        self.assertEqual([], list(target.iter_changes(revision_tree)))
3136.1.2 by Aaron Bentley
Implement hard-linking for build_tree
2191
        source_stat = os.stat('source/file1')
2192
        target_stat = os.stat('target/file1')
2193
        self.assertEqual(source_stat, target_stat)
2194
2195
        # Explicitly disallowing hardlinks should prevent them.
2196
        target2 = self.make_branch_and_tree('target2')
2197
        build_tree(revision_tree, target2, source, hardlink=False)
2198
        target2.lock_read()
2199
        self.addCleanup(target2.unlock)
3254.1.1 by Aaron Bentley
Make Tree.iter_changes a public method
2200
        self.assertEqual([], list(target2.iter_changes(revision_tree)))
3136.1.2 by Aaron Bentley
Implement hard-linking for build_tree
2201
        source_stat = os.stat('source/file1')
2202
        target2_stat = os.stat('target2/file1')
2203
        self.assertNotEqual(source_stat, target2_stat)
2204
3137.1.1 by Aaron Bentley
Fix build_tree acceleration when file is moved in accelerator_tree
2205
    def test_build_tree_accelerator_tree_moved(self):
2206
        source = self.make_branch_and_tree('source')
6855.4.1 by Jelmer Vernooij
Yet more bees.
2207
        self.build_tree_contents([('source/file1', b'A')])
2208
        source.add(['file1'], [b'file1-id'])
3137.1.1 by Aaron Bentley
Fix build_tree acceleration when file is moved in accelerator_tree
2209
        source.commit('commit files')
2210
        source.rename_one('file1', 'file2')
2211
        source.lock_read()
2212
        self.addCleanup(source.unlock)
2213
        target = self.make_branch_and_tree('target')
2214
        revision_tree = source.basis_tree()
2215
        revision_tree.lock_read()
2216
        self.addCleanup(revision_tree.unlock)
2217
        build_tree(revision_tree, target, source)
2218
        target.lock_read()
2219
        self.addCleanup(target.unlock)
3254.1.1 by Aaron Bentley
Make Tree.iter_changes a public method
2220
        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
2221
3136.1.2 by Aaron Bentley
Implement hard-linking for build_tree
2222
    def test_build_tree_hardlinks_preserve_execute(self):
2223
        self.requireFeature(HardlinkFeature)
2224
        source = self.create_ab_tree()
2225
        tt = TreeTransform(source)
6885.1.1 by Jelmer Vernooij
Get rid of TreeTransform.trans_id_tree_file_id.
2226
        trans_id = tt.trans_id_tree_path('file1')
3136.1.2 by Aaron Bentley
Implement hard-linking for build_tree
2227
        tt.set_executability(True, trans_id)
2228
        tt.apply()
6809.4.4 by Jelmer Vernooij
Swap arguments for Tree.is_executable.
2229
        self.assertTrue(source.is_executable('file1'))
3136.1.2 by Aaron Bentley
Implement hard-linking for build_tree
2230
        target = self.make_branch_and_tree('target')
2231
        revision_tree = source.basis_tree()
2232
        revision_tree.lock_read()
2233
        self.addCleanup(revision_tree.unlock)
2234
        build_tree(revision_tree, target, source, hardlink=True)
2235
        target.lock_read()
2236
        self.addCleanup(target.unlock)
3254.1.1 by Aaron Bentley
Make Tree.iter_changes a public method
2237
        self.assertEqual([], list(target.iter_changes(revision_tree)))
6809.4.4 by Jelmer Vernooij
Swap arguments for Tree.is_executable.
2238
        self.assertTrue(source.is_executable('file1'))
3136.1.2 by Aaron Bentley
Implement hard-linking for build_tree
2239
4826.1.5 by Andrew Bennetts
Add test that content filtered files are not hardlinked by build_tree.
2240
    def install_rot13_content_filter(self, pattern):
4985.2.1 by Vincent Ladeuil
Deploy addAttrCleanup on the whole test suite.
2241
        # We could use
2242
        # self.addCleanup(filters._reset_registry, filters._reset_registry())
2243
        # below, but that looks a bit... hard to read even if it's exactly
2244
        # the same thing.
4826.1.5 by Andrew Bennetts
Add test that content filtered files are not hardlinked by build_tree.
2245
        original_registry = filters._reset_registry()
7143.15.2 by Jelmer Vernooij
Run autopep8.
2246
4826.1.5 by Andrew Bennetts
Add test that content filtered files are not hardlinked by build_tree.
2247
        def restore_registry():
2248
            filters._reset_registry(original_registry)
2249
        self.addCleanup(restore_registry)
7143.15.2 by Jelmer Vernooij
Run autopep8.
2250
4826.1.5 by Andrew Bennetts
Add test that content filtered files are not hardlinked by build_tree.
2251
        def rot13(chunks, context=None):
7143.15.5 by Jelmer Vernooij
More PEP8 fixes.
2252
            return [
2253
                codecs.encode(chunk.decode('ascii'), 'rot13').encode('ascii')
2254
                for chunk in chunks]
4826.1.5 by Andrew Bennetts
Add test that content filtered files are not hardlinked by build_tree.
2255
        rot13filter = filters.ContentFilter(rot13, rot13)
6498.5.3 by Jelmer Vernooij
Fix tests.
2256
        filters.filter_stacks_registry.register(
2257
            'rot13', {'yes': [rot13filter]}.get)
4826.1.5 by Andrew Bennetts
Add test that content filtered files are not hardlinked by build_tree.
2258
        os.mkdir(self.test_home_dir + '/.bazaar')
2259
        rules_filename = self.test_home_dir + '/.bazaar/rules'
6973.7.5 by Jelmer Vernooij
s/file/open.
2260
        with open(rules_filename, 'wb') as f:
2261
            f.write(b'[name %s]\nrot13=yes\n' % (pattern,))
7143.15.2 by Jelmer Vernooij
Run autopep8.
2262
4826.1.5 by Andrew Bennetts
Add test that content filtered files are not hardlinked by build_tree.
2263
        def uninstall_rules():
2264
            os.remove(rules_filename)
2265
            rules.reset_rules()
2266
        self.addCleanup(uninstall_rules)
2267
        rules.reset_rules()
2268
2269
    def test_build_tree_content_filtered_files_are_not_hardlinked(self):
2270
        """build_tree will not hardlink files that have content filtering rules
2271
        applied to them (but will still hardlink other files from the same tree
2272
        if it can).
2273
        """
2274
        self.requireFeature(HardlinkFeature)
6973.10.1 by Jelmer Vernooij
Fix some tests.
2275
        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.
2276
        source = self.create_ab_tree()
2277
        target = self.make_branch_and_tree('target')
2278
        revision_tree = source.basis_tree()
2279
        revision_tree.lock_read()
2280
        self.addCleanup(revision_tree.unlock)
2281
        build_tree(revision_tree, target, source, hardlink=True)
2282
        target.lock_read()
2283
        self.addCleanup(target.unlock)
2284
        self.assertEqual([], list(target.iter_changes(revision_tree)))
2285
        source_stat = os.stat('source/file1')
2286
        target_stat = os.stat('target/file1')
2287
        self.assertNotEqual(source_stat, target_stat)
2288
        source_stat = os.stat('source/file2')
2289
        target_stat = os.stat('target/file2')
4826.1.8 by Andrew Bennetts
Tweaks suggested by John.
2290
        self.assertEqualStat(source_stat, target_stat)
4826.1.5 by Andrew Bennetts
Add test that content filtered files are not hardlinked by build_tree.
2291
3453.2.4 by Aaron Bentley
Disable fast-path when conflicts are encountered
2292
    def test_case_insensitive_build_tree_inventory(self):
5967.12.1 by Martin Pool
Move all test features into bzrlib.tests.features
2293
        if (features.CaseInsensitiveFilesystemFeature.available()
7143.15.2 by Jelmer Vernooij
Run autopep8.
2294
                or features.CaseInsCasePresFilenameFeature.available()):
4241.9.4 by Vincent Ladeuil
Fix test_case_insensitive_build_tree_inventory failure on OSX.
2295
            raise tests.UnavailableFeature('Fully case sensitive filesystem')
3453.2.4 by Aaron Bentley
Disable fast-path when conflicts are encountered
2296
        source = self.make_branch_and_tree('source')
2297
        self.build_tree(['source/file', 'source/FILE'])
6855.4.1 by Jelmer Vernooij
Yet more bees.
2298
        source.add(['file', 'FILE'], [b'lower-id', b'upper-id'])
3453.2.4 by Aaron Bentley
Disable fast-path when conflicts are encountered
2299
        source.commit('added files')
2300
        # Don't try this at home, kids!
2301
        # Force the tree to report that it is case insensitive
2302
        target = self.make_branch_and_tree('target')
2303
        target.case_sensitive = False
3453.2.6 by Aaron Bentley
Rename mutate_tree to delta_from_tree, add comment
2304
        build_tree(source.basis_tree(), target, source, delta_from_tree=True)
6855.4.1 by Jelmer Vernooij
Yet more bees.
2305
        self.assertEqual('file.moved', target.id2path(b'lower-id'))
2306
        self.assertEqual('FILE', target.id2path(b'upper-id'))
3453.2.4 by Aaron Bentley
Disable fast-path when conflicts are encountered
2307
5752.5.1 by John Arbash Meinel
Merge up transform-create-file
2308
    def test_build_tree_observes_sha(self):
2309
        source = self.make_branch_and_tree('source')
2310
        self.build_tree(['source/file1', 'source/dir/', 'source/dir/file2'])
2311
        source.add(['file1', 'dir', 'dir/file2'],
6855.4.1 by Jelmer Vernooij
Yet more bees.
2312
                   [b'file1-id', b'dir-id', b'file2-id'])
5752.5.1 by John Arbash Meinel
Merge up transform-create-file
2313
        source.commit('new files')
2314
        target = self.make_branch_and_tree('target')
2315
        target.lock_write()
2316
        self.addCleanup(target.unlock)
2317
        # We make use of the fact that DirState caches its cutoff time. So we
2318
        # set the 'safe' time to one minute in the future.
2319
        state = target.current_dirstate()
2320
        state._cutoff_time = time.time() + 60
2321
        build_tree(source.basis_tree(), target)
2322
        entry1_sha = osutils.sha_file_by_name('source/file1')
2323
        entry2_sha = osutils.sha_file_by_name('source/dir/file2')
2324
        # entry[1] is the state information, entry[1][0] is the state of the
2325
        # working tree, entry[1][0][1] is the sha value for the current working
2326
        # tree
7045.4.7 by Jelmer Vernooij
Fix remaining tests.
2327
        entry1 = state._get_entry(0, path_utf8=b'file1')
5752.5.1 by John Arbash Meinel
Merge up transform-create-file
2328
        self.assertEqual(entry1_sha, entry1[1][0][1])
2329
        # The 'size' field must also be set.
2330
        self.assertEqual(25, entry1[1][0][2])
2331
        entry1_state = entry1[1][0]
7045.4.7 by Jelmer Vernooij
Fix remaining tests.
2332
        entry2 = state._get_entry(0, path_utf8=b'dir/file2')
5752.5.1 by John Arbash Meinel
Merge up transform-create-file
2333
        self.assertEqual(entry2_sha, entry2[1][0][1])
2334
        self.assertEqual(29, entry2[1][0][2])
2335
        entry2_state = entry2[1][0]
2336
        # Now, make sure that we don't have to re-read the content. The
2337
        # packed_stat should match exactly.
7141.7.2 by Jelmer Vernooij
Fix more tests.
2338
        self.assertEqual(entry1_sha, target.get_file_sha1('file1'))
2339
        self.assertEqual(entry2_sha, target.get_file_sha1('dir/file2'))
5752.5.1 by John Arbash Meinel
Merge up transform-create-file
2340
        self.assertEqual(entry1_state, entry1[1][0])
2341
        self.assertEqual(entry2_state, entry2[1][0])
2342
1966.1.1 by Aaron Bentley
Implement disk-content merge and conflict resolution for build_tree
2343
4526.8.1 by Aaron Bentley
Support committing a TreeTransform to a branch.
2344
class TestCommitTransform(tests.TestCaseWithTransport):
2345
2346
    def get_branch(self):
2347
        tree = self.make_branch_and_tree('tree')
2348
        tree.lock_write()
2349
        self.addCleanup(tree.unlock)
2350
        tree.commit('empty commit')
2351
        return tree.branch
2352
2353
    def get_branch_and_transform(self):
2354
        branch = self.get_branch()
2355
        tt = TransformPreview(branch.basis_tree())
2356
        self.addCleanup(tt.finalize)
2357
        return branch, tt
2358
2359
    def test_commit_wrong_basis(self):
2360
        branch = self.get_branch()
2361
        basis = branch.repository.revision_tree(
2362
            _mod_revision.NULL_REVISION)
2363
        tt = TransformPreview(basis)
2364
        self.addCleanup(tt.finalize)
4526.8.5 by Aaron Bentley
Updates from review.
2365
        e = self.assertRaises(ValueError, tt.commit, branch, '')
4526.8.1 by Aaron Bentley
Support committing a TreeTransform to a branch.
2366
        self.assertEqual('TreeTransform not based on branch basis: null:',
2367
                         str(e))
2368
2369
    def test_empy_commit(self):
2370
        branch, tt = self.get_branch_and_transform()
2371
        rev = tt.commit(branch, 'my message')
2372
        self.assertEqual(2, branch.revno())
2373
        repo = branch.repository
2374
        self.assertEqual('my message', repo.get_revision(rev).message)
2375
2376
    def test_merge_parents(self):
2377
        branch, tt = self.get_branch_and_transform()
7143.15.5 by Jelmer Vernooij
More PEP8 fixes.
2378
        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.
2379
        self.assertEqual([b'rev1b', b'rev1c'],
4526.8.1 by Aaron Bentley
Support committing a TreeTransform to a branch.
2380
                         branch.basis_tree().get_parent_ids()[1:])
2381
4526.8.3 by Aaron Bentley
Clean up merge parent handling.
2382
    def test_first_commit(self):
2383
        branch = self.make_branch('branch')
2384
        branch.lock_write()
2385
        self.addCleanup(branch.unlock)
2386
        tt = TransformPreview(branch.basis_tree())
4659.2.4 by Vincent Ladeuil
Cleanup remaining bzr-limbo-XXXXXX leaks in /tmp during selftest.
2387
        self.addCleanup(tt.finalize)
6973.13.2 by Jelmer Vernooij
Fix some more tests.
2388
        tt.new_directory('', ROOT_PARENT, b'TREE_ROOT')
7143.15.5 by Jelmer Vernooij
More PEP8 fixes.
2389
        tt.commit(branch, 'my message')
4526.8.3 by Aaron Bentley
Clean up merge parent handling.
2390
        self.assertEqual([], branch.basis_tree().get_parent_ids())
4526.8.5 by Aaron Bentley
Updates from review.
2391
        self.assertNotEqual(_mod_revision.NULL_REVISION,
2392
                            branch.last_revision())
4526.8.3 by Aaron Bentley
Clean up merge parent handling.
2393
4526.8.5 by Aaron Bentley
Updates from review.
2394
    def test_first_commit_with_merge_parents(self):
4526.8.3 by Aaron Bentley
Clean up merge parent handling.
2395
        branch = self.make_branch('branch')
2396
        branch.lock_write()
2397
        self.addCleanup(branch.unlock)
2398
        tt = TransformPreview(branch.basis_tree())
4659.2.4 by Vincent Ladeuil
Cleanup remaining bzr-limbo-XXXXXX leaks in /tmp during selftest.
2399
        self.addCleanup(tt.finalize)
4526.8.5 by Aaron Bentley
Updates from review.
2400
        e = self.assertRaises(ValueError, tt.commit, branch,
7143.15.2 by Jelmer Vernooij
Run autopep8.
2401
                              'my message', [b'rev1b-id'])
4526.8.5 by Aaron Bentley
Updates from review.
2402
        self.assertEqual('Cannot supply merge parents for first commit.',
2403
                         str(e))
2404
        self.assertEqual(_mod_revision.NULL_REVISION, branch.last_revision())
4526.8.3 by Aaron Bentley
Clean up merge parent handling.
2405
4526.8.1 by Aaron Bentley
Support committing a TreeTransform to a branch.
2406
    def test_add_files(self):
2407
        branch, tt = self.get_branch_and_transform()
6973.10.1 by Jelmer Vernooij
Fix some tests.
2408
        tt.new_file('file', tt.root, [b'contents'], b'file-id')
2409
        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
2410
        if SymlinkFeature.available():
6973.10.1 by Jelmer Vernooij
Fix some tests.
2411
            tt.new_symlink('symlink', trans_id, 'target', b'symlink-id')
7143.15.5 by Jelmer Vernooij
More PEP8 fixes.
2412
        tt.commit(branch, 'message')
4526.8.1 by Aaron Bentley
Support committing a TreeTransform to a branch.
2413
        tree = branch.basis_tree()
6855.4.1 by Jelmer Vernooij
Yet more bees.
2414
        self.assertEqual('file', tree.id2path(b'file-id'))
7141.7.1 by Jelmer Vernooij
Get rid of file_ids in most of Tree.
2415
        self.assertEqual(b'contents', tree.get_file_text('file'))
6855.4.1 by Jelmer Vernooij
Yet more bees.
2416
        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
2417
        if SymlinkFeature.available():
6855.4.1 by Jelmer Vernooij
Yet more bees.
2418
            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.
2419
            self.assertEqual('target', tree.get_symlink_target('dir/symlink'))
4526.8.1 by Aaron Bentley
Support committing a TreeTransform to a branch.
2420
4526.8.2 by Aaron Bentley
Proved strict commit handling.
2421
    def test_add_unversioned(self):
2422
        branch, tt = self.get_branch_and_transform()
6973.10.1 by Jelmer Vernooij
Fix some tests.
2423
        tt.new_file('file', tt.root, [b'contents'])
4526.8.2 by Aaron Bentley
Proved strict commit handling.
2424
        self.assertRaises(errors.StrictCommitFailed, tt.commit, branch,
2425
                          'message', strict=True)
2426
2427
    def test_modify_strict(self):
2428
        branch, tt = self.get_branch_and_transform()
6973.10.1 by Jelmer Vernooij
Fix some tests.
2429
        tt.new_file('file', tt.root, [b'contents'], b'file-id')
4526.8.2 by Aaron Bentley
Proved strict commit handling.
2430
        tt.commit(branch, 'message', strict=True)
2431
        tt = TransformPreview(branch.basis_tree())
4659.2.4 by Vincent Ladeuil
Cleanup remaining bzr-limbo-XXXXXX leaks in /tmp during selftest.
2432
        self.addCleanup(tt.finalize)
6973.10.1 by Jelmer Vernooij
Fix some tests.
2433
        trans_id = tt.trans_id_file_id(b'file-id')
4526.8.2 by Aaron Bentley
Proved strict commit handling.
2434
        tt.delete_contents(trans_id)
6973.5.10 by Jelmer Vernooij
Random bunch of python3 bee-improvements.
2435
        tt.create_file([b'contents'], trans_id)
4526.8.2 by Aaron Bentley
Proved strict commit handling.
2436
        tt.commit(branch, 'message', strict=True)
2437
4526.8.6 by Aaron Bentley
Check for malformed transforms before committing.
2438
    def test_commit_malformed(self):
2439
        """Committing a malformed transform should raise an exception.
2440
2441
        In this case, we are adding a file without adding its parent.
2442
        """
2443
        branch, tt = self.get_branch_and_transform()
6973.10.1 by Jelmer Vernooij
Fix some tests.
2444
        parent_id = tt.trans_id_file_id(b'parent-id')
2445
        tt.new_file('file', parent_id, [b'contents'], b'file-id')
4526.8.6 by Aaron Bentley
Check for malformed transforms before committing.
2446
        self.assertRaises(errors.MalformedTransform, tt.commit, branch,
2447
                          'message')
2448
5162.4.1 by Aaron Bentley
TreeTransform supports normal commit parameters and includes branch nick.
2449
    def test_commit_rich_revision_data(self):
2450
        branch, tt = self.get_branch_and_transform()
5162.4.3 by Aaron Bentley
Fix failing test.
2451
        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.
2452
                           committer='me <me@example.com>',
6973.12.5 by Jelmer Vernooij
Add some u's for revision property names.
2453
                           revprops={u'foo': 'bar'}, revision_id=b'revid-1',
5162.4.1 by Aaron Bentley
TreeTransform supports normal commit parameters and includes branch nick.
2454
                           authors=['Author1 <author1@example.com>',
7143.15.2 by Jelmer Vernooij
Run autopep8.
2455
                                    'Author2 <author2@example.com>',
2456
                                    ])
6973.10.1 by Jelmer Vernooij
Fix some tests.
2457
        self.assertEqual(b'revid-1', rev_id)
5162.4.1 by Aaron Bentley
TreeTransform supports normal commit parameters and includes branch nick.
2458
        revision = branch.repository.get_revision(rev_id)
2459
        self.assertEqual(1, revision.timestamp)
5162.4.3 by Aaron Bentley
Fix failing test.
2460
        self.assertEqual(43201, revision.timezone)
5162.4.1 by Aaron Bentley
TreeTransform supports normal commit parameters and includes branch nick.
2461
        self.assertEqual('me <me@example.com>', revision.committer)
2462
        self.assertEqual(['Author1 <author1@example.com>',
2463
                          'Author2 <author2@example.com>'],
2464
                         revision.get_apparent_authors())
2465
        del revision.properties['authors']
2466
        self.assertEqual({'foo': 'bar',
2467
                          'branch-nick': 'tree'},
2468
                         revision.properties)
2469
2470
    def test_no_explicit_revprops(self):
2471
        branch, tt = self.get_branch_and_transform()
2472
        rev_id = tt.commit(branch, 'message', authors=[
2473
            'Author1 <author1@example.com>',
2474
            'Author2 <author2@example.com>', ])
2475
        revision = branch.repository.get_revision(rev_id)
2476
        self.assertEqual(['Author1 <author1@example.com>',
2477
                          'Author2 <author2@example.com>'],
2478
                         revision.get_apparent_authors())
2479
        self.assertEqual('tree', revision.properties['branch-nick'])
2480
4526.8.1 by Aaron Bentley
Support committing a TreeTransform to a branch.
2481
2733.2.1 by Aaron Bentley
Implement FileMover, to support TreeTransform rollback
2482
class TestFileMover(tests.TestCaseWithTransport):
2483
2484
    def test_file_mover(self):
2485
        self.build_tree(['a/', 'a/b', 'c/', 'c/d'])
2486
        mover = _FileMover()
2487
        mover.rename('a', 'q')
5784.1.3 by Martin Pool
Switch away from using failUnlessExists and failIfExists
2488
        self.assertPathExists('q')
2489
        self.assertPathDoesNotExist('a')
2490
        self.assertPathExists('q/b')
2491
        self.assertPathExists('c')
2492
        self.assertPathExists('c/d')
2733.2.1 by Aaron Bentley
Implement FileMover, to support TreeTransform rollback
2493
2733.2.5 by Aaron Bentley
Implement FileMover.pre_delete and FileMover.apply_deletions
2494
    def test_pre_delete_rollback(self):
2495
        self.build_tree(['a/'])
2496
        mover = _FileMover()
2497
        mover.pre_delete('a', 'q')
5784.1.3 by Martin Pool
Switch away from using failUnlessExists and failIfExists
2498
        self.assertPathExists('q')
2499
        self.assertPathDoesNotExist('a')
2733.2.5 by Aaron Bentley
Implement FileMover.pre_delete and FileMover.apply_deletions
2500
        mover.rollback()
5784.1.3 by Martin Pool
Switch away from using failUnlessExists and failIfExists
2501
        self.assertPathDoesNotExist('q')
2502
        self.assertPathExists('a')
2733.2.5 by Aaron Bentley
Implement FileMover.pre_delete and FileMover.apply_deletions
2503
2504
    def test_apply_deletions(self):
2733.2.12 by Aaron Bentley
Updates from review
2505
        self.build_tree(['a/', 'b/'])
2733.2.5 by Aaron Bentley
Implement FileMover.pre_delete and FileMover.apply_deletions
2506
        mover = _FileMover()
2507
        mover.pre_delete('a', 'q')
2733.2.12 by Aaron Bentley
Updates from review
2508
        mover.pre_delete('b', 'r')
5784.1.3 by Martin Pool
Switch away from using failUnlessExists and failIfExists
2509
        self.assertPathExists('q')
2510
        self.assertPathExists('r')
2511
        self.assertPathDoesNotExist('a')
2512
        self.assertPathDoesNotExist('b')
2733.2.5 by Aaron Bentley
Implement FileMover.pre_delete and FileMover.apply_deletions
2513
        mover.apply_deletions()
5784.1.3 by Martin Pool
Switch away from using failUnlessExists and failIfExists
2514
        self.assertPathDoesNotExist('q')
2515
        self.assertPathDoesNotExist('r')
2516
        self.assertPathDoesNotExist('a')
2517
        self.assertPathDoesNotExist('b')
2733.2.5 by Aaron Bentley
Implement FileMover.pre_delete and FileMover.apply_deletions
2518
2733.2.1 by Aaron Bentley
Implement FileMover, to support TreeTransform rollback
2519
    def test_file_mover_rollback(self):
2520
        self.build_tree(['a/', 'a/b', 'c/', 'c/d/', 'c/e/'])
2521
        mover = _FileMover()
2522
        mover.rename('c/d', 'c/f')
2523
        mover.rename('c/e', 'c/d')
2524
        try:
2525
            mover.rename('a', 'c')
7143.15.5 by Jelmer Vernooij
More PEP8 fixes.
2526
        except errors.FileExists:
2733.2.1 by Aaron Bentley
Implement FileMover, to support TreeTransform rollback
2527
            mover.rollback()
5784.1.3 by Martin Pool
Switch away from using failUnlessExists and failIfExists
2528
        self.assertPathExists('a')
2529
        self.assertPathExists('c/d')
2733.2.3 by Aaron Bentley
Test tranform rollback
2530
2531
2532
class Bogus(Exception):
2533
    pass
2534
2535
2536
class TestTransformRollback(tests.TestCaseWithTransport):
2537
2538
    class ExceptionFileMover(_FileMover):
2539
2733.2.4 by Aaron Bentley
Test transform rollback when renaming into place
2540
        def __init__(self, bad_source=None, bad_target=None):
2541
            _FileMover.__init__(self)
2542
            self.bad_source = bad_source
2543
            self.bad_target = bad_target
2544
2733.2.3 by Aaron Bentley
Test tranform rollback
2545
        def rename(self, source, target):
2733.2.4 by Aaron Bentley
Test transform rollback when renaming into place
2546
            if (self.bad_source is not None and
7143.15.2 by Jelmer Vernooij
Run autopep8.
2547
                    source.endswith(self.bad_source)):
2733.2.4 by Aaron Bentley
Test transform rollback when renaming into place
2548
                raise Bogus
2549
            elif (self.bad_target is not None and
7143.15.2 by Jelmer Vernooij
Run autopep8.
2550
                  target.endswith(self.bad_target)):
2733.2.3 by Aaron Bentley
Test tranform rollback
2551
                raise Bogus
2552
            else:
2553
                _FileMover.rename(self, source, target)
2554
2555
    def test_rollback_rename(self):
2556
        tree = self.make_branch_and_tree('.')
2557
        self.build_tree(['a/', 'a/b'])
2558
        tt = TreeTransform(tree)
2559
        self.addCleanup(tt.finalize)
2560
        a_id = tt.trans_id_tree_path('a')
2561
        tt.adjust_path('c', tt.root, a_id)
2562
        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
2563
        self.assertRaises(Bogus, tt.apply,
2564
                          _mover=self.ExceptionFileMover(bad_source='a'))
5784.1.3 by Martin Pool
Switch away from using failUnlessExists and failIfExists
2565
        self.assertPathExists('a')
2566
        self.assertPathExists('a/b')
2733.2.4 by Aaron Bentley
Test transform rollback when renaming into place
2567
        tt.apply()
5784.1.3 by Martin Pool
Switch away from using failUnlessExists and failIfExists
2568
        self.assertPathExists('c')
2569
        self.assertPathExists('c/d')
2733.2.4 by Aaron Bentley
Test transform rollback when renaming into place
2570
2571
    def test_rollback_rename_into_place(self):
2572
        tree = self.make_branch_and_tree('.')
2573
        self.build_tree(['a/', 'a/b'])
2574
        tt = TreeTransform(tree)
2575
        self.addCleanup(tt.finalize)
2576
        a_id = tt.trans_id_tree_path('a')
2577
        tt.adjust_path('c', tt.root, a_id)
2578
        tt.adjust_path('d', a_id, tt.trans_id_tree_path('a/b'))
2579
        self.assertRaises(Bogus, tt.apply,
2580
                          _mover=self.ExceptionFileMover(bad_target='c/d'))
5784.1.3 by Martin Pool
Switch away from using failUnlessExists and failIfExists
2581
        self.assertPathExists('a')
2582
        self.assertPathExists('a/b')
2733.2.4 by Aaron Bentley
Test transform rollback when renaming into place
2583
        tt.apply()
5784.1.3 by Martin Pool
Switch away from using failUnlessExists and failIfExists
2584
        self.assertPathExists('c')
2585
        self.assertPathExists('c/d')
2733.2.6 by Aaron Bentley
Make TreeTransform commits rollbackable
2586
2587
    def test_rollback_deletion(self):
2588
        tree = self.make_branch_and_tree('.')
2589
        self.build_tree(['a/', 'a/b'])
2590
        tt = TreeTransform(tree)
2591
        self.addCleanup(tt.finalize)
2592
        a_id = tt.trans_id_tree_path('a')
2593
        tt.delete_contents(a_id)
2594
        tt.adjust_path('d', tt.root, tt.trans_id_tree_path('a/b'))
2595
        self.assertRaises(Bogus, tt.apply,
2596
                          _mover=self.ExceptionFileMover(bad_target='d'))
5784.1.3 by Martin Pool
Switch away from using failUnlessExists and failIfExists
2597
        self.assertPathExists('a')
2598
        self.assertPathExists('a/b')
3008.1.1 by Aaron Bentley
Start work allowing previews of transforms
2599
2600
5876.3.3 by Martin
Use better test class name
2601
class TestFinalizeRobustness(tests.TestCaseWithTransport):
2602
    """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
2603
2604
    def _override_globals_in_method(self, instance, method_name, globals):
2605
        """Replace method on instance with one with updated globals"""
2606
        import types
6619.3.24 by Jelmer Vernooij
Run 2to3 methodattrs fixer.
2607
        func = getattr(instance, method_name).__func__
6619.3.16 by Jelmer Vernooij
Run 2to3 funcattrs fixer.
2608
        new_globals = dict(func.__globals__)
5876.3.1 by Martin
Tests for DiskTreeTransform.finalize issues when create_file is interrupted
2609
        new_globals.update(globals)
6619.3.16 by Jelmer Vernooij
Run 2to3 funcattrs fixer.
2610
        new_func = types.FunctionType(func.__code__, new_globals,
7143.15.2 by Jelmer Vernooij
Run autopep8.
2611
                                      func.__name__, func.__defaults__)
7058.4.14 by Jelmer Vernooij
Fix test_transform tests.
2612
        if PY3:
2613
            setattr(instance, method_name,
7143.15.2 by Jelmer Vernooij
Run autopep8.
2614
                    types.MethodType(new_func, instance))
7058.4.14 by Jelmer Vernooij
Fix test_transform tests.
2615
        else:
2616
            setattr(instance, method_name,
7143.15.2 by Jelmer Vernooij
Run autopep8.
2617
                    types.MethodType(new_func, instance, instance.__class__))
5876.3.1 by Martin
Tests for DiskTreeTransform.finalize issues when create_file is interrupted
2618
        self.addCleanup(delattr, instance, method_name)
2619
2620
    @staticmethod
2621
    def _fake_open_raises_before(name, mode):
2622
        """Like open() but raises before doing anything"""
2623
        raise RuntimeError
2624
2625
    @staticmethod
2626
    def _fake_open_raises_after(name, mode):
2627
        """Like open() but raises after creating file without returning"""
2628
        open(name, mode).close()
2629
        raise RuntimeError
2630
2631
    def create_transform_and_root_trans_id(self):
2632
        """Setup a transform creating a file in limbo"""
2633
        tree = self.make_branch_and_tree('.')
2634
        tt = TreeTransform(tree)
2635
        return tt, tt.create_path("a", tt.root)
2636
2637
    def create_transform_and_subdir_trans_id(self):
2638
        """Setup a transform creating a directory containing a file in limbo"""
2639
        tree = self.make_branch_and_tree('.')
2640
        tt = TreeTransform(tree)
2641
        d_trans_id = tt.create_path("d", tt.root)
2642
        tt.create_directory(d_trans_id)
2643
        f_trans_id = tt.create_path("a", d_trans_id)
2644
        tt.adjust_path("a", d_trans_id, f_trans_id)
2645
        return tt, f_trans_id
2646
2647
    def test_root_create_file_open_raises_before_creation(self):
2648
        tt, trans_id = self.create_transform_and_root_trans_id()
7143.15.5 by Jelmer Vernooij
More PEP8 fixes.
2649
        self._override_globals_in_method(
2650
            tt, "create_file", {"open": self._fake_open_raises_before})
7143.15.2 by Jelmer Vernooij
Run autopep8.
2651
        self.assertRaises(RuntimeError, tt.create_file,
2652
                          [b"contents"], trans_id)
5876.3.1 by Martin
Tests for DiskTreeTransform.finalize issues when create_file is interrupted
2653
        path = tt._limbo_name(trans_id)
2654
        self.assertPathDoesNotExist(path)
2655
        tt.finalize()
2656
        self.assertPathDoesNotExist(tt._limbodir)
2657
2658
    def test_root_create_file_open_raises_after_creation(self):
2659
        tt, trans_id = self.create_transform_and_root_trans_id()
7143.15.5 by Jelmer Vernooij
More PEP8 fixes.
2660
        self._override_globals_in_method(
2661
            tt, "create_file", {"open": self._fake_open_raises_after})
7143.15.2 by Jelmer Vernooij
Run autopep8.
2662
        self.assertRaises(RuntimeError, tt.create_file,
2663
                          [b"contents"], trans_id)
5876.3.1 by Martin
Tests for DiskTreeTransform.finalize issues when create_file is interrupted
2664
        path = tt._limbo_name(trans_id)
2665
        self.assertPathExists(path)
5876.3.2 by Martin
Change test that expects ImmortalLimbo to want the sensible thing instead, and fail for now
2666
        tt.finalize()
2667
        self.assertPathDoesNotExist(path)
2668
        self.assertPathDoesNotExist(tt._limbodir)
5876.3.1 by Martin
Tests for DiskTreeTransform.finalize issues when create_file is interrupted
2669
2670
    def test_subdir_create_file_open_raises_before_creation(self):
2671
        tt, trans_id = self.create_transform_and_subdir_trans_id()
7143.15.5 by Jelmer Vernooij
More PEP8 fixes.
2672
        self._override_globals_in_method(
2673
            tt, "create_file", {"open": self._fake_open_raises_before})
7143.15.2 by Jelmer Vernooij
Run autopep8.
2674
        self.assertRaises(RuntimeError, tt.create_file,
2675
                          [b"contents"], trans_id)
5876.3.1 by Martin
Tests for DiskTreeTransform.finalize issues when create_file is interrupted
2676
        path = tt._limbo_name(trans_id)
2677
        self.assertPathDoesNotExist(path)
2678
        tt.finalize()
2679
        self.assertPathDoesNotExist(tt._limbodir)
2680
2681
    def test_subdir_create_file_open_raises_after_creation(self):
2682
        tt, trans_id = self.create_transform_and_subdir_trans_id()
7143.15.5 by Jelmer Vernooij
More PEP8 fixes.
2683
        self._override_globals_in_method(
2684
            tt, "create_file", {"open": self._fake_open_raises_after})
7143.15.2 by Jelmer Vernooij
Run autopep8.
2685
        self.assertRaises(RuntimeError, tt.create_file,
2686
                          [b"contents"], trans_id)
5876.3.1 by Martin
Tests for DiskTreeTransform.finalize issues when create_file is interrupted
2687
        path = tt._limbo_name(trans_id)
2688
        self.assertPathExists(path)
2689
        tt.finalize()
2690
        self.assertPathDoesNotExist(path)
2691
        self.assertPathDoesNotExist(tt._limbodir)
2692
5876.4.4 by Andrew Bennetts
Fix edge cases in handling interrupts during _rename_in_limbo, with messy tests
2693
    def test_rename_in_limbo_rename_raises_after_rename(self):
2694
        tt, trans_id = self.create_transform_and_root_trans_id()
2695
        parent1 = tt.new_directory('parent1', tt.root)
6973.10.1 by Jelmer Vernooij
Fix some tests.
2696
        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
2697
        parent2 = tt.new_directory('parent2', tt.root)
2698
2699
        class FakeOSModule(object):
2700
            def rename(self, old, new):
2701
                os.rename(old, new)
2702
                raise RuntimeError
2703
        self._override_globals_in_method(tt, "_rename_in_limbo",
7143.15.2 by Jelmer Vernooij
Run autopep8.
2704
                                         {"os": FakeOSModule()})
5876.4.4 by Andrew Bennetts
Fix edge cases in handling interrupts during _rename_in_limbo, with messy tests
2705
        self.assertRaises(
2706
            RuntimeError, tt.adjust_path, "child1", parent2, child1)
2707
        path = osutils.pathjoin(tt._limbo_name(parent2), "child1")
2708
        self.assertPathExists(path)
2709
        tt.finalize()
2710
        self.assertPathDoesNotExist(path)
2711
        self.assertPathDoesNotExist(tt._limbodir)
2712
2713
    def test_rename_in_limbo_rename_raises_before_rename(self):
2714
        tt, trans_id = self.create_transform_and_root_trans_id()
2715
        parent1 = tt.new_directory('parent1', tt.root)
6973.10.1 by Jelmer Vernooij
Fix some tests.
2716
        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
2717
        parent2 = tt.new_directory('parent2', tt.root)
2718
2719
        class FakeOSModule(object):
2720
            def rename(self, old, new):
2721
                raise RuntimeError
2722
        self._override_globals_in_method(tt, "_rename_in_limbo",
7143.15.2 by Jelmer Vernooij
Run autopep8.
2723
                                         {"os": FakeOSModule()})
5876.4.4 by Andrew Bennetts
Fix edge cases in handling interrupts during _rename_in_limbo, with messy tests
2724
        self.assertRaises(
2725
            RuntimeError, tt.adjust_path, "child1", parent2, child1)
2726
        path = osutils.pathjoin(tt._limbo_name(parent1), "child1")
2727
        self.assertPathExists(path)
2728
        tt.finalize()
2729
        self.assertPathDoesNotExist(path)
2730
        self.assertPathDoesNotExist(tt._limbodir)
2731
5876.3.1 by Martin
Tests for DiskTreeTransform.finalize issues when create_file is interrupted
2732
5409.1.6 by Vincent Ladeuil
Add failing tests for bug #323111.
2733
class TestTransformMissingParent(tests.TestCaseWithTransport):
2734
5409.7.4 by Vincent Ladeuil
Take jam's review comments into account.
2735
    def make_tt_with_versioned_dir(self):
5409.1.6 by Vincent Ladeuil
Add failing tests for bug #323111.
2736
        wt = self.make_branch_and_tree('.')
7143.15.2 by Jelmer Vernooij
Run autopep8.
2737
        self.build_tree(['dir/', ])
6855.4.1 by Jelmer Vernooij
Yet more bees.
2738
        wt.add(['dir'], [b'dir-id'])
5409.1.6 by Vincent Ladeuil
Add failing tests for bug #323111.
2739
        wt.commit('Create dir')
2740
        tt = TreeTransform(wt)
2741
        self.addCleanup(tt.finalize)
2742
        return wt, tt
2743
2744
    def test_resolve_create_parent_for_versioned_file(self):
5409.7.4 by Vincent Ladeuil
Take jam's review comments into account.
2745
        wt, tt = self.make_tt_with_versioned_dir()
6885.1.1 by Jelmer Vernooij
Get rid of TreeTransform.trans_id_tree_file_id.
2746
        dir_tid = tt.trans_id_tree_path('dir')
7143.15.5 by Jelmer Vernooij
More PEP8 fixes.
2747
        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.
2748
        tt.delete_contents(dir_tid)
2749
        tt.unversion_file(dir_tid)
2750
        conflicts = resolve_conflicts(tt)
2751
        # one conflict for the missing directory, one for the unversioned
2752
        # parent
2753
        self.assertLength(2, conflicts)
2754
5409.1.20 by Vincent Ladeuil
Revert to 'conflict' being the default orphaning policy and fix fallouts.
2755
    def test_non_versioned_file_create_conflict(self):
5409.7.4 by Vincent Ladeuil
Take jam's review comments into account.
2756
        wt, tt = self.make_tt_with_versioned_dir()
6885.1.1 by Jelmer Vernooij
Get rid of TreeTransform.trans_id_tree_file_id.
2757
        dir_tid = tt.trans_id_tree_path('dir')
6973.10.1 by Jelmer Vernooij
Fix some tests.
2758
        tt.new_file('file', dir_tid, [b'Contents'])
5409.1.6 by Vincent Ladeuil
Add failing tests for bug #323111.
2759
        tt.delete_contents(dir_tid)
2760
        tt.unversion_file(dir_tid)
2761
        conflicts = resolve_conflicts(tt)
5409.7.2 by Vincent Ladeuil
Add NEWS entry, a missing test and some cleanup.
2762
        # 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.
2763
        self.assertLength(1, conflicts)
2764
        self.assertEqual(('deleting parent', 'Not deleting', 'new-1'),
2765
                         conflicts.pop())
5409.1.6 by Vincent Ladeuil
Add failing tests for bug #323111.
2766
2767
6973.10.1 by Jelmer Vernooij
Fix some tests.
2768
A_ENTRY = (b'a-id', ('a', 'a'), True, (True, True),
7143.15.2 by Jelmer Vernooij
Run autopep8.
2769
           (b'TREE_ROOT', b'TREE_ROOT'), ('a', 'a'), ('file', 'file'),
2770
           (False, False))
6973.10.1 by Jelmer Vernooij
Fix some tests.
2771
ROOT_ENTRY = (b'TREE_ROOT', ('', ''), False, (True, True), (None, None),
5050.57.1 by Aaron Bentley
Make is_executable treat symlinks and directories the same across tree types.
2772
              ('', ''), ('directory', 'directory'), (False, False))
3363.19.1 by Aaron Bentley
Make PreviewTree.iter_changes accept all options.
2773
2774
3008.1.1 by Aaron Bentley
Start work allowing previews of transforms
2775
class TestTransformPreview(tests.TestCaseWithTransport):
2776
2777
    def create_tree(self):
2778
        tree = self.make_branch_and_tree('.')
6855.4.1 by Jelmer Vernooij
Yet more bees.
2779
        self.build_tree_contents([('a', b'content 1')])
2780
        tree.set_root_id(b'TREE_ROOT')
2781
        tree.add('a', b'a-id')
2782
        tree.commit('rev1', rev_id=b'rev1')
6973.5.2 by Jelmer Vernooij
Add more bees.
2783
        return tree.branch.repository.revision_tree(b'rev1')
3008.1.1 by Aaron Bentley
Start work allowing previews of transforms
2784
3008.1.18 by Aaron Bentley
Get supported PreviewTree functionality under test
2785
    def get_empty_preview(self):
2786
        repository = self.make_repository('repo')
2787
        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
2788
        preview = TransformPreview(tree)
3199.1.5 by Vincent Ladeuil
Fix two more leaking tmp dirs, by reworking TransformPreview lock handling.
2789
        self.addCleanup(preview.finalize)
3199.1.4 by Vincent Ladeuil
Fix 16 leaked tmp dirs. Probably indicates a lock handling problem with TransformPreview
2790
        return preview
2791
3008.1.1 by Aaron Bentley
Start work allowing previews of transforms
2792
    def test_transform_preview(self):
2793
        revision_tree = self.create_tree()
2794
        preview = TransformPreview(revision_tree)
3199.1.5 by Vincent Ladeuil
Fix two more leaking tmp dirs, by reworking TransformPreview lock handling.
2795
        self.addCleanup(preview.finalize)
3008.1.1 by Aaron Bentley
Start work allowing previews of transforms
2796
2797
    def test_transform_preview_tree(self):
2798
        revision_tree = self.create_tree()
2799
        preview = TransformPreview(revision_tree)
3199.1.5 by Vincent Ladeuil
Fix two more leaking tmp dirs, by reworking TransformPreview lock handling.
2800
        self.addCleanup(preview.finalize)
3008.1.1 by Aaron Bentley
Start work allowing previews of transforms
2801
        preview.get_preview_tree()
2802
3008.1.5 by Michael Hudson
a more precise test
2803
    def test_transform_new_file(self):
2804
        revision_tree = self.create_tree()
2805
        preview = TransformPreview(revision_tree)
3199.1.5 by Vincent Ladeuil
Fix two more leaking tmp dirs, by reworking TransformPreview lock handling.
2806
        self.addCleanup(preview.finalize)
6973.10.1 by Jelmer Vernooij
Fix some tests.
2807
        preview.new_file('file2', preview.root, [b'content B\n'], b'file2-id')
3008.1.5 by Michael Hudson
a more precise test
2808
        preview_tree = preview.get_preview_tree()
6809.4.7 by Jelmer Vernooij
Swap arguments for get_symlink_target and kind/stored_kind.
2809
        self.assertEqual(preview_tree.kind('file2'), 'file')
7141.7.1 by Jelmer Vernooij
Get rid of file_ids in most of Tree.
2810
        with preview_tree.get_file('file2') as f:
6973.10.1 by Jelmer Vernooij
Fix some tests.
2811
            self.assertEqual(f.read(), b'content B\n')
3008.1.5 by Michael Hudson
a more precise test
2812
3008.1.1 by Aaron Bentley
Start work allowing previews of transforms
2813
    def test_diff_preview_tree(self):
2814
        revision_tree = self.create_tree()
2815
        preview = TransformPreview(revision_tree)
3199.1.5 by Vincent Ladeuil
Fix two more leaking tmp dirs, by reworking TransformPreview lock handling.
2816
        self.addCleanup(preview.finalize)
6973.10.1 by Jelmer Vernooij
Fix some tests.
2817
        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
2818
        preview_tree = preview.get_preview_tree()
6621.22.2 by Martin
Use BytesIO or StringIO from bzrlib.sixish
2819
        out = BytesIO()
3008.1.1 by Aaron Bentley
Start work allowing previews of transforms
2820
        show_diff_trees(revision_tree, preview_tree, out)
3008.1.4 by Michael Hudson
Merge test enhancements
2821
        lines = out.getvalue().splitlines()
7045.4.1 by Jelmer Vernooij
Some brz-git fixes.
2822
        self.assertEqual(lines[0], b"=== added file 'file2'")
3008.1.4 by Michael Hudson
Merge test enhancements
2823
        # 3 lines of diff administrivia
7045.4.1 by Jelmer Vernooij
Some brz-git fixes.
2824
        self.assertEqual(lines[4], b"+content B")
3008.2.1 by Aaron Bentley
Ensure conflict resolution works
2825
6469.1.18 by Parth Malwankar
renamed test
2826
    def test_unsupported_symlink_diff(self):
6469.1.16 by Parth Malwankar
added diff test
2827
        self.requireFeature(SymlinkFeature)
2828
        tree = self.make_branch_and_tree('.')
2829
        self.build_tree_contents([('a', 'content 1')])
7122.6.12 by Jelmer Vernooij
Fix more python3 tests.
2830
        tree.set_root_id(b'TREE_ROOT')
2831
        tree.add('a', b'a-id')
6469.1.16 by Parth Malwankar
added diff test
2832
        os.symlink('a', 'foo')
7122.6.12 by Jelmer Vernooij
Fix more python3 tests.
2833
        tree.add('foo', b'foo-id')
2834
        tree.commit('rev1', rev_id=b'rev1')
2835
        revision_tree = tree.branch.repository.revision_tree(b'rev1')
6469.1.16 by Parth Malwankar
added diff test
2836
        preview = TransformPreview(revision_tree)
2837
        self.addCleanup(preview.finalize)
2838
        preview.delete_versioned(preview.trans_id_tree_path('foo'))
2839
        preview_tree = preview.get_preview_tree()
7322.1.7 by Jelmer Vernooij
Fix remaining tests.
2840
        out = BytesIO()
7122.6.1 by Jelmer Vernooij
merge Parth's work into Breezy
2841
        log = BytesIO()
6469.1.16 by Parth Malwankar
added diff test
2842
        trace.push_log_file(log)
2843
        os_symlink = getattr(os, 'symlink', None)
2844
        os.symlink = None
2845
        try:
2846
            show_diff_trees(revision_tree, preview_tree, out)
2847
            lines = out.getvalue().splitlines()
2848
        finally:
7322.1.7 by Jelmer Vernooij
Fix remaining tests.
2849
            os.symlink = os_symlink
6469.1.16 by Parth Malwankar
added diff test
2850
        self.assertContainsRe(
2851
            log.getvalue(),
7122.6.12 by Jelmer Vernooij
Fix more python3 tests.
2852
            b'Ignoring "foo" as symlinks are not supported on this filesystem')
6469.1.16 by Parth Malwankar
added diff test
2853
3008.2.1 by Aaron Bentley
Ensure conflict resolution works
2854
    def test_transform_conflicts(self):
2855
        revision_tree = self.create_tree()
2856
        preview = TransformPreview(revision_tree)
3199.1.5 by Vincent Ladeuil
Fix two more leaking tmp dirs, by reworking TransformPreview lock handling.
2857
        self.addCleanup(preview.finalize)
6973.10.1 by Jelmer Vernooij
Fix some tests.
2858
        preview.new_file('a', preview.root, [b'content 2'])
3008.2.1 by Aaron Bentley
Ensure conflict resolution works
2859
        resolve_conflicts(preview)
6973.10.1 by Jelmer Vernooij
Fix some tests.
2860
        trans_id = preview.trans_id_file_id(b'a-id')
3008.2.1 by Aaron Bentley
Ensure conflict resolution works
2861
        self.assertEqual('a.moved', preview.final_name(trans_id))
3008.1.17 by Aaron Bentley
Test unused parameters of preview_tree._iter_changes
2862
3008.1.31 by Aaron Bentley
Split PreviewTree._iter_changes parameter tests into smaller tests
2863
    def get_tree_and_preview_tree(self):
3008.1.17 by Aaron Bentley
Test unused parameters of preview_tree._iter_changes
2864
        revision_tree = self.create_tree()
2865
        preview = TransformPreview(revision_tree)
3199.1.5 by Vincent Ladeuil
Fix two more leaking tmp dirs, by reworking TransformPreview lock handling.
2866
        self.addCleanup(preview.finalize)
6973.10.1 by Jelmer Vernooij
Fix some tests.
2867
        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
2868
        preview.delete_contents(a_trans_id)
6973.5.10 by Jelmer Vernooij
Random bunch of python3 bee-improvements.
2869
        preview.create_file([b'b content'], a_trans_id)
3008.1.17 by Aaron Bentley
Test unused parameters of preview_tree._iter_changes
2870
        preview_tree = preview.get_preview_tree()
3008.1.31 by Aaron Bentley
Split PreviewTree._iter_changes parameter tests into smaller tests
2871
        return revision_tree, preview_tree
2872
2873
    def test_iter_changes(self):
2874
        revision_tree, preview_tree = self.get_tree_and_preview_tree()
6405.2.10 by Jelmer Vernooij
Fix more tests.
2875
        root = revision_tree.get_root_id()
6973.10.1 by Jelmer Vernooij
Fix some tests.
2876
        self.assertEqual([(b'a-id', ('a', 'a'), True, (True, True),
7143.15.2 by Jelmer Vernooij
Run autopep8.
2877
                           (root, root), ('a', 'a'), ('file', 'file'),
2878
                           (False, False))],
2879
                         list(preview_tree.iter_changes(revision_tree)))
3008.1.31 by Aaron Bentley
Split PreviewTree._iter_changes parameter tests into smaller tests
2880
3363.19.1 by Aaron Bentley
Make PreviewTree.iter_changes accept all options.
2881
    def test_include_unchanged_succeeds(self):
3008.1.31 by Aaron Bentley
Split PreviewTree._iter_changes parameter tests into smaller tests
2882
        revision_tree, preview_tree = self.get_tree_and_preview_tree()
3363.19.1 by Aaron Bentley
Make PreviewTree.iter_changes accept all options.
2883
        changes = preview_tree.iter_changes(revision_tree,
2884
                                            include_unchanged=True)
2885
        self.assertEqual([ROOT_ENTRY, A_ENTRY], list(changes))
3008.1.31 by Aaron Bentley
Split PreviewTree._iter_changes parameter tests into smaller tests
2886
2887
    def test_specific_files(self):
2888
        revision_tree, preview_tree = self.get_tree_and_preview_tree()
3363.19.1 by Aaron Bentley
Make PreviewTree.iter_changes accept all options.
2889
        changes = preview_tree.iter_changes(revision_tree,
2890
                                            specific_files=[''])
5050.57.1 by Aaron Bentley
Make is_executable treat symlinks and directories the same across tree types.
2891
        self.assertEqual([A_ENTRY], list(changes))
3008.1.31 by Aaron Bentley
Split PreviewTree._iter_changes parameter tests into smaller tests
2892
3363.19.1 by Aaron Bentley
Make PreviewTree.iter_changes accept all options.
2893
    def test_want_unversioned(self):
3008.1.31 by Aaron Bentley
Split PreviewTree._iter_changes parameter tests into smaller tests
2894
        revision_tree, preview_tree = self.get_tree_and_preview_tree()
3363.19.1 by Aaron Bentley
Make PreviewTree.iter_changes accept all options.
2895
        changes = preview_tree.iter_changes(revision_tree,
2896
                                            want_unversioned=True)
5050.57.1 by Aaron Bentley
Make is_executable treat symlinks and directories the same across tree types.
2897
        self.assertEqual([A_ENTRY], list(changes))
3008.1.17 by Aaron Bentley
Test unused parameters of preview_tree._iter_changes
2898
3008.1.31 by Aaron Bentley
Split PreviewTree._iter_changes parameter tests into smaller tests
2899
    def test_ignore_extra_trees_no_specific_files(self):
3008.1.17 by Aaron Bentley
Test unused parameters of preview_tree._iter_changes
2900
        # extra_trees is harmless without specific_files, so we'll silently
2901
        # accept it, even though we won't use it.
3008.1.31 by Aaron Bentley
Split PreviewTree._iter_changes parameter tests into smaller tests
2902
        revision_tree, preview_tree = self.get_tree_and_preview_tree()
3254.1.1 by Aaron Bentley
Make Tree.iter_changes a public method
2903
        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
2904
2905
    def test_ignore_require_versioned_no_specific_files(self):
3008.1.17 by Aaron Bentley
Test unused parameters of preview_tree._iter_changes
2906
        # require_versioned is meaningless without specific_files.
3008.1.31 by Aaron Bentley
Split PreviewTree._iter_changes parameter tests into smaller tests
2907
        revision_tree, preview_tree = self.get_tree_and_preview_tree()
3254.1.1 by Aaron Bentley
Make Tree.iter_changes a public method
2908
        preview_tree.iter_changes(revision_tree, require_versioned=False)
3008.1.31 by Aaron Bentley
Split PreviewTree._iter_changes parameter tests into smaller tests
2909
2910
    def test_ignore_pb(self):
3008.1.17 by Aaron Bentley
Test unused parameters of preview_tree._iter_changes
2911
        # 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
2912
        revision_tree, preview_tree = self.get_tree_and_preview_tree()
4961.2.9 by Martin Pool
Rip out most remaining uses of DummyProgressBar
2913
        preview_tree.iter_changes(revision_tree)
3008.1.18 by Aaron Bentley
Get supported PreviewTree functionality under test
2914
2915
    def test_kind(self):
2916
        revision_tree = self.create_tree()
2917
        preview = TransformPreview(revision_tree)
3199.1.5 by Vincent Ladeuil
Fix two more leaking tmp dirs, by reworking TransformPreview lock handling.
2918
        self.addCleanup(preview.finalize)
6973.10.1 by Jelmer Vernooij
Fix some tests.
2919
        preview.new_file('file', preview.root, [b'contents'], b'file-id')
6973.13.2 by Jelmer Vernooij
Fix some more tests.
2920
        preview.new_directory('directory', preview.root, b'dir-id')
3008.1.18 by Aaron Bentley
Get supported PreviewTree functionality under test
2921
        preview_tree = preview.get_preview_tree()
6809.4.7 by Jelmer Vernooij
Swap arguments for get_symlink_target and kind/stored_kind.
2922
        self.assertEqual('file', preview_tree.kind('file'))
2923
        self.assertEqual('directory', preview_tree.kind('directory'))
3008.1.18 by Aaron Bentley
Get supported PreviewTree functionality under test
2924
2925
    def test_get_file_mtime(self):
2926
        preview = self.get_empty_preview()
6973.10.1 by Jelmer Vernooij
Fix some tests.
2927
        file_trans_id = preview.new_file('file', preview.root, [b'contents'],
6855.4.2 by Jelmer Vernooij
Fix test.
2928
                                         b'file-id')
3008.1.18 by Aaron Bentley
Get supported PreviewTree functionality under test
2929
        limbo_path = preview._limbo_name(file_trans_id)
2930
        preview_tree = preview.get_preview_tree()
2931
        self.assertEqual(os.stat(limbo_path).st_mtime,
7141.7.1 by Jelmer Vernooij
Get rid of file_ids in most of Tree.
2932
                         preview_tree.get_file_mtime('file'))
3008.1.18 by Aaron Bentley
Get supported PreviewTree functionality under test
2933
4635.1.1 by Aaron Bentley
Fix OSError with renamed files in PreviewTree.
2934
    def test_get_file_mtime_renamed(self):
2935
        work_tree = self.make_branch_and_tree('tree')
2936
        self.build_tree(['tree/file'])
6855.4.1 by Jelmer Vernooij
Yet more bees.
2937
        work_tree.add('file', b'file-id')
4635.1.1 by Aaron Bentley
Fix OSError with renamed files in PreviewTree.
2938
        preview = TransformPreview(work_tree)
2939
        self.addCleanup(preview.finalize)
6885.1.1 by Jelmer Vernooij
Get rid of TreeTransform.trans_id_tree_file_id.
2940
        file_trans_id = preview.trans_id_tree_path('file')
4635.1.1 by Aaron Bentley
Fix OSError with renamed files in PreviewTree.
2941
        preview.adjust_path('renamed', preview.root, file_trans_id)
2942
        preview_tree = preview.get_preview_tree()
7141.7.1 by Jelmer Vernooij
Get rid of file_ids in most of Tree.
2943
        preview_mtime = preview_tree.get_file_mtime('renamed')
7141.7.2 by Jelmer Vernooij
Fix more tests.
2944
        work_mtime = work_tree.get_file_mtime('file')
4635.1.1 by Aaron Bentley
Fix OSError with renamed files in PreviewTree.
2945
5809.3.6 by Aaron Bentley
Fix PreviewTree.get_file_size.
2946
    def test_get_file_size(self):
2947
        work_tree = self.make_branch_and_tree('tree')
6855.4.1 by Jelmer Vernooij
Yet more bees.
2948
        self.build_tree_contents([('tree/old', b'old')])
2949
        work_tree.add('old', b'old-id')
5809.3.6 by Aaron Bentley
Fix PreviewTree.get_file_size.
2950
        preview = TransformPreview(work_tree)
2951
        self.addCleanup(preview.finalize)
7143.15.5 by Jelmer Vernooij
More PEP8 fixes.
2952
        preview.new_file('name', preview.root, [b'contents'], b'new-id',
2953
                         'executable')
5809.3.6 by Aaron Bentley
Fix PreviewTree.get_file_size.
2954
        tree = preview.get_preview_tree()
6809.4.5 by Jelmer Vernooij
Swap arguments for get_file_*.
2955
        self.assertEqual(len('old'), tree.get_file_size('old'))
2956
        self.assertEqual(len('contents'), tree.get_file_size('name'))
5809.3.6 by Aaron Bentley
Fix PreviewTree.get_file_size.
2957
3008.1.18 by Aaron Bentley
Get supported PreviewTree functionality under test
2958
    def test_get_file(self):
2959
        preview = self.get_empty_preview()
6973.10.1 by Jelmer Vernooij
Fix some tests.
2960
        preview.new_file('file', preview.root, [b'contents'], b'file-id')
3008.1.18 by Aaron Bentley
Get supported PreviewTree functionality under test
2961
        preview_tree = preview.get_preview_tree()
6973.10.1 by Jelmer Vernooij
Fix some tests.
2962
        with preview_tree.get_file('file') as tree_file:
2963
            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.
2964
2965
    def test_get_symlink_target(self):
2966
        self.requireFeature(SymlinkFeature)
2967
        preview = self.get_empty_preview()
6973.13.2 by Jelmer Vernooij
Fix some more tests.
2968
        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.
2969
        preview_tree = preview.get_preview_tree()
2970
        self.assertEqual('target',
6809.4.7 by Jelmer Vernooij
Swap arguments for get_symlink_target and kind/stored_kind.
2971
                         preview_tree.get_symlink_target('symlink'))
3363.2.18 by Aaron Bentley
Implement correct all_file_ids for PreviewTree
2972
2973
    def test_all_file_ids(self):
2974
        tree = self.make_branch_and_tree('tree')
2975
        self.build_tree(['tree/a', 'tree/b', 'tree/c'])
6855.4.1 by Jelmer Vernooij
Yet more bees.
2976
        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
2977
        preview = TransformPreview(tree)
2978
        self.addCleanup(preview.finalize)
6855.4.1 by Jelmer Vernooij
Yet more bees.
2979
        preview.unversion_file(preview.trans_id_file_id(b'b-id'))
2980
        c_trans_id = preview.trans_id_file_id(b'c-id')
3363.2.18 by Aaron Bentley
Implement correct all_file_ids for PreviewTree
2981
        preview.unversion_file(c_trans_id)
6855.4.1 by Jelmer Vernooij
Yet more bees.
2982
        preview.version_file(b'c-id', c_trans_id)
3363.2.18 by Aaron Bentley
Implement correct all_file_ids for PreviewTree
2983
        preview_tree = preview.get_preview_tree()
6855.4.1 by Jelmer Vernooij
Yet more bees.
2984
        self.assertEqual({b'a-id', b'c-id', tree.get_root_id()},
3363.2.18 by Aaron Bentley
Implement correct all_file_ids for PreviewTree
2985
                         preview_tree.all_file_ids())
3363.2.19 by Aaron Bentley
Make PreviewTree.path2id correct
2986
2987
    def test_path2id_deleted_unchanged(self):
2988
        tree = self.make_branch_and_tree('tree')
2989
        self.build_tree(['tree/unchanged', 'tree/deleted'])
6855.4.1 by Jelmer Vernooij
Yet more bees.
2990
        tree.add(['unchanged', 'deleted'], [b'unchanged-id', b'deleted-id'])
3363.2.19 by Aaron Bentley
Make PreviewTree.path2id correct
2991
        preview = TransformPreview(tree)
2992
        self.addCleanup(preview.finalize)
6855.4.1 by Jelmer Vernooij
Yet more bees.
2993
        preview.unversion_file(preview.trans_id_file_id(b'deleted-id'))
3363.2.19 by Aaron Bentley
Make PreviewTree.path2id correct
2994
        preview_tree = preview.get_preview_tree()
6855.4.1 by Jelmer Vernooij
Yet more bees.
2995
        self.assertEqual(b'unchanged-id', preview_tree.path2id('unchanged'))
6852.3.1 by Jelmer Vernooij
add Tree.is_versioned.
2996
        self.assertFalse(preview_tree.is_versioned('deleted'))
3363.2.19 by Aaron Bentley
Make PreviewTree.path2id correct
2997
2998
    def test_path2id_created(self):
2999
        tree = self.make_branch_and_tree('tree')
3000
        self.build_tree(['tree/unchanged'])
6855.4.1 by Jelmer Vernooij
Yet more bees.
3001
        tree.add(['unchanged'], [b'unchanged-id'])
3363.2.19 by Aaron Bentley
Make PreviewTree.path2id correct
3002
        preview = TransformPreview(tree)
3003
        self.addCleanup(preview.finalize)
6855.4.1 by Jelmer Vernooij
Yet more bees.
3004
        preview.new_file('new', preview.trans_id_file_id(b'unchanged-id'),
7143.15.2 by Jelmer Vernooij
Run autopep8.
3005
                         [b'contents'], b'new-id')
3363.2.19 by Aaron Bentley
Make PreviewTree.path2id correct
3006
        preview_tree = preview.get_preview_tree()
6855.4.1 by Jelmer Vernooij
Yet more bees.
3007
        self.assertEqual(b'new-id', preview_tree.path2id('unchanged/new'))
3363.2.19 by Aaron Bentley
Make PreviewTree.path2id correct
3008
3009
    def test_path2id_moved(self):
3010
        tree = self.make_branch_and_tree('tree')
3011
        self.build_tree(['tree/old_parent/', 'tree/old_parent/child'])
3012
        tree.add(['old_parent', 'old_parent/child'],
6855.4.1 by Jelmer Vernooij
Yet more bees.
3013
                 [b'old_parent-id', b'child-id'])
3363.2.19 by Aaron Bentley
Make PreviewTree.path2id correct
3014
        preview = TransformPreview(tree)
3015
        self.addCleanup(preview.finalize)
3016
        new_parent = preview.new_directory('new_parent', preview.root,
6855.4.1 by Jelmer Vernooij
Yet more bees.
3017
                                           b'new_parent-id')
3363.2.19 by Aaron Bentley
Make PreviewTree.path2id correct
3018
        preview.adjust_path('child', new_parent,
6855.4.1 by Jelmer Vernooij
Yet more bees.
3019
                            preview.trans_id_file_id(b'child-id'))
3363.2.19 by Aaron Bentley
Make PreviewTree.path2id correct
3020
        preview_tree = preview.get_preview_tree()
6852.3.1 by Jelmer Vernooij
add Tree.is_versioned.
3021
        self.assertFalse(preview_tree.is_versioned('old_parent/child'))
6855.4.1 by Jelmer Vernooij
Yet more bees.
3022
        self.assertEqual(b'child-id', preview_tree.path2id('new_parent/child'))
3363.2.19 by Aaron Bentley
Make PreviewTree.path2id correct
3023
3024
    def test_path2id_renamed_parent(self):
3025
        tree = self.make_branch_and_tree('tree')
3026
        self.build_tree(['tree/old_name/', 'tree/old_name/child'])
3027
        tree.add(['old_name', 'old_name/child'],
6855.4.1 by Jelmer Vernooij
Yet more bees.
3028
                 [b'parent-id', b'child-id'])
3363.2.19 by Aaron Bentley
Make PreviewTree.path2id correct
3029
        preview = TransformPreview(tree)
3030
        self.addCleanup(preview.finalize)
3031
        preview.adjust_path('new_name', preview.root,
6855.4.1 by Jelmer Vernooij
Yet more bees.
3032
                            preview.trans_id_file_id(b'parent-id'))
3363.2.19 by Aaron Bentley
Make PreviewTree.path2id correct
3033
        preview_tree = preview.get_preview_tree()
6852.3.1 by Jelmer Vernooij
add Tree.is_versioned.
3034
        self.assertFalse(preview_tree.is_versioned('old_name/child'))
6855.4.1 by Jelmer Vernooij
Yet more bees.
3035
        self.assertEqual(b'child-id', preview_tree.path2id('new_name/child'))
3363.2.21 by Aaron Bentley
Implement iter_entries_by_dir
3036
6885.6.1 by Jelmer Vernooij
Support specific_files argument to Tree.iter_entries_by_dir.
3037
    def assertMatchingIterEntries(self, tt, specific_files=None):
3363.2.21 by Aaron Bentley
Implement iter_entries_by_dir
3038
        preview_tree = tt.get_preview_tree()
3039
        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.
3040
                              specific_files=specific_files))
3363.2.21 by Aaron Bentley
Implement iter_entries_by_dir
3041
        tree = tt._tree
3042
        tt.apply()
6885.6.2 by Jelmer Vernooij
Use specific_files.
3043
        actual_result = list(tree.iter_entries_by_dir(
3044
            specific_files=specific_files))
3363.2.21 by Aaron Bentley
Implement iter_entries_by_dir
3045
        self.assertEqual(actual_result, preview_result)
3046
3047
    def test_iter_entries_by_dir_new(self):
3048
        tree = self.make_branch_and_tree('tree')
3049
        tt = TreeTransform(tree)
6973.10.1 by Jelmer Vernooij
Fix some tests.
3050
        tt.new_file('new', tt.root, [b'contents'], b'new-id')
3363.2.21 by Aaron Bentley
Implement iter_entries_by_dir
3051
        self.assertMatchingIterEntries(tt)
3052
3053
    def test_iter_entries_by_dir_deleted(self):
3054
        tree = self.make_branch_and_tree('tree')
3055
        self.build_tree(['tree/deleted'])
6855.4.1 by Jelmer Vernooij
Yet more bees.
3056
        tree.add('deleted', b'deleted-id')
3363.2.21 by Aaron Bentley
Implement iter_entries_by_dir
3057
        tt = TreeTransform(tree)
6855.4.1 by Jelmer Vernooij
Yet more bees.
3058
        tt.delete_contents(tt.trans_id_file_id(b'deleted-id'))
3363.2.21 by Aaron Bentley
Implement iter_entries_by_dir
3059
        self.assertMatchingIterEntries(tt)
3060
3061
    def test_iter_entries_by_dir_unversioned(self):
3062
        tree = self.make_branch_and_tree('tree')
3063
        self.build_tree(['tree/removed'])
6855.4.1 by Jelmer Vernooij
Yet more bees.
3064
        tree.add('removed', b'removed-id')
3363.2.21 by Aaron Bentley
Implement iter_entries_by_dir
3065
        tt = TreeTransform(tree)
6855.4.1 by Jelmer Vernooij
Yet more bees.
3066
        tt.unversion_file(tt.trans_id_file_id(b'removed-id'))
3363.2.21 by Aaron Bentley
Implement iter_entries_by_dir
3067
        self.assertMatchingIterEntries(tt)
3068
3069
    def test_iter_entries_by_dir_moved(self):
3070
        tree = self.make_branch_and_tree('tree')
3071
        self.build_tree(['tree/moved', 'tree/new_parent/'])
6855.4.1 by Jelmer Vernooij
Yet more bees.
3072
        tree.add(['moved', 'new_parent'], [b'moved-id', b'new_parent-id'])
3363.2.21 by Aaron Bentley
Implement iter_entries_by_dir
3073
        tt = TreeTransform(tree)
6855.4.1 by Jelmer Vernooij
Yet more bees.
3074
        tt.adjust_path('moved', tt.trans_id_file_id(b'new_parent-id'),
3075
                       tt.trans_id_file_id(b'moved-id'))
3363.2.21 by Aaron Bentley
Implement iter_entries_by_dir
3076
        self.assertMatchingIterEntries(tt)
3077
6885.6.4 by Jelmer Vernooij
Tree transform - avoid iter_entries_by_dir(specific_file_ids=).
3078
    def test_iter_entries_by_dir_specific_files(self):
3363.2.21 by Aaron Bentley
Implement iter_entries_by_dir
3079
        tree = self.make_branch_and_tree('tree')
6855.4.1 by Jelmer Vernooij
Yet more bees.
3080
        tree.set_root_id(b'tree-root-id')
3363.2.21 by Aaron Bentley
Implement iter_entries_by_dir
3081
        self.build_tree(['tree/parent/', 'tree/parent/child'])
6855.4.1 by Jelmer Vernooij
Yet more bees.
3082
        tree.add(['parent', 'parent/child'], [b'parent-id', b'child-id'])
3363.2.21 by Aaron Bentley
Implement iter_entries_by_dir
3083
        tt = TreeTransform(tree)
6885.6.1 by Jelmer Vernooij
Support specific_files argument to Tree.iter_entries_by_dir.
3084
        self.assertMatchingIterEntries(tt, ['', 'parent/child'])
3363.2.26 by Aaron Bentley
Get symlinks working
3085
3086
    def test_symlink_content_summary(self):
3087
        self.requireFeature(SymlinkFeature)
3088
        preview = self.get_empty_preview()
6855.4.1 by Jelmer Vernooij
Yet more bees.
3089
        preview.new_symlink('path', preview.root, 'target', b'path-id')
3363.2.26 by Aaron Bentley
Get symlinks working
3090
        summary = preview.get_preview_tree().path_content_summary('path')
3091
        self.assertEqual(('symlink', None, None, 'target'), summary)
3363.2.27 by Aaron Bentley
Make path_content_summary a core API
3092
3093
    def test_missing_content_summary(self):
3094
        preview = self.get_empty_preview()
3095
        summary = preview.get_preview_tree().path_content_summary('path')
3096
        self.assertEqual(('missing', None, None, None), summary)
3097
3098
    def test_deleted_content_summary(self):
3099
        tree = self.make_branch_and_tree('tree')
3100
        self.build_tree(['tree/path/'])
3101
        tree.add('path')
3102
        preview = TransformPreview(tree)
3103
        self.addCleanup(preview.finalize)
3104
        preview.delete_contents(preview.trans_id_tree_path('path'))
3105
        summary = preview.get_preview_tree().path_content_summary('path')
3106
        self.assertEqual(('missing', None, None, None), summary)
3107
3363.2.30 by Aaron Bentley
Improve execute bit testing
3108
    def test_file_content_summary_executable(self):
3109
        preview = self.get_empty_preview()
7143.15.2 by Jelmer Vernooij
Run autopep8.
3110
        path_id = preview.new_file('path', preview.root, [
3111
                                   b'contents'], b'path-id')
3363.2.30 by Aaron Bentley
Improve execute bit testing
3112
        preview.set_executability(True, path_id)
3113
        summary = preview.get_preview_tree().path_content_summary('path')
3114
        self.assertEqual(4, len(summary))
3115
        self.assertEqual('file', summary[0])
3116
        # size must be known
3117
        self.assertEqual(len('contents'), summary[1])
3118
        # executable
3119
        self.assertEqual(True, summary[2])
3363.2.31 by Aaron Bentley
Tweak tests
3120
        # will not have hash (not cheap to determine)
3121
        self.assertIs(None, summary[3])
3363.2.30 by Aaron Bentley
Improve execute bit testing
3122
3123
    def test_change_executability(self):
3124
        tree = self.make_branch_and_tree('tree')
3125
        self.build_tree(['tree/path'])
3126
        tree.add('path')
3127
        preview = TransformPreview(tree)
3128
        self.addCleanup(preview.finalize)
3129
        path_id = preview.trans_id_tree_path('path')
3130
        preview.set_executability(True, path_id)
3131
        summary = preview.get_preview_tree().path_content_summary('path')
3132
        self.assertEqual(True, summary[2])
3133
3363.2.27 by Aaron Bentley
Make path_content_summary a core API
3134
    def test_file_content_summary_non_exec(self):
3135
        preview = self.get_empty_preview()
6973.10.1 by Jelmer Vernooij
Fix some tests.
3136
        preview.new_file('path', preview.root, [b'contents'], b'path-id')
3363.2.27 by Aaron Bentley
Make path_content_summary a core API
3137
        summary = preview.get_preview_tree().path_content_summary('path')
3138
        self.assertEqual(4, len(summary))
3139
        self.assertEqual('file', summary[0])
3140
        # size must be known
3363.2.30 by Aaron Bentley
Improve execute bit testing
3141
        self.assertEqual(len('contents'), summary[1])
3363.2.27 by Aaron Bentley
Make path_content_summary a core API
3142
        # not executable
4789.15.3 by John Arbash Meinel
PreviewTree now returns False for exec bit properly.
3143
        self.assertEqual(False, summary[2])
3363.2.31 by Aaron Bentley
Tweak tests
3144
        # will not have hash (not cheap to determine)
3145
        self.assertIs(None, summary[3])
3363.2.27 by Aaron Bentley
Make path_content_summary a core API
3146
3147
    def test_dir_content_summary(self):
3148
        preview = self.get_empty_preview()
6973.10.1 by Jelmer Vernooij
Fix some tests.
3149
        preview.new_directory('path', preview.root, b'path-id')
3363.2.27 by Aaron Bentley
Make path_content_summary a core API
3150
        summary = preview.get_preview_tree().path_content_summary('path')
3151
        self.assertEqual(('directory', None, None, None), summary)
3152
3153
    def test_tree_content_summary(self):
3154
        preview = self.get_empty_preview()
6973.10.1 by Jelmer Vernooij
Fix some tests.
3155
        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.
3156
        preview.set_tree_reference(b'rev-1', path)
3363.2.27 by Aaron Bentley
Make path_content_summary a core API
3157
        summary = preview.get_preview_tree().path_content_summary('path')
3158
        self.assertEqual(4, len(summary))
3159
        self.assertEqual('tree-reference', summary[0])
3363.2.33 by Aaron Bentley
Implement PreviewTree.annotate_iter
3160
3161
    def test_annotate(self):
3162
        tree = self.make_branch_and_tree('tree')
6855.4.1 by Jelmer Vernooij
Yet more bees.
3163
        self.build_tree_contents([('tree/file', b'a\n')])
3164
        tree.add('file', b'file-id')
3165
        tree.commit('a', rev_id=b'one')
3166
        self.build_tree_contents([('tree/file', b'a\nb\n')])
3363.2.33 by Aaron Bentley
Implement PreviewTree.annotate_iter
3167
        preview = TransformPreview(tree)
3168
        self.addCleanup(preview.finalize)
6855.4.1 by Jelmer Vernooij
Yet more bees.
3169
        file_trans_id = preview.trans_id_file_id(b'file-id')
3363.2.33 by Aaron Bentley
Implement PreviewTree.annotate_iter
3170
        preview.delete_contents(file_trans_id)
6973.5.10 by Jelmer Vernooij
Random bunch of python3 bee-improvements.
3171
        preview.create_file([b'a\nb\nc\n'], file_trans_id)
3363.2.33 by Aaron Bentley
Implement PreviewTree.annotate_iter
3172
        preview_tree = preview.get_preview_tree()
3173
        expected = [
6973.10.1 by Jelmer Vernooij
Fix some tests.
3174
            (b'one', b'a\n'),
3175
            (b'me:', b'b\n'),
3176
            (b'me:', b'c\n'),
3363.2.33 by Aaron Bentley
Implement PreviewTree.annotate_iter
3177
        ]
7143.15.2 by Jelmer Vernooij
Run autopep8.
3178
        annotation = preview_tree.annotate_iter(
3179
            'file', default_revision=b'me:')
3363.2.33 by Aaron Bentley
Implement PreviewTree.annotate_iter
3180
        self.assertEqual(expected, annotation)
3181
3182
    def test_annotate_missing(self):
3183
        preview = self.get_empty_preview()
6973.10.1 by Jelmer Vernooij
Fix some tests.
3184
        preview.new_file('file', preview.root, [b'a\nb\nc\n'], b'file-id')
3363.2.33 by Aaron Bentley
Implement PreviewTree.annotate_iter
3185
        preview_tree = preview.get_preview_tree()
3186
        expected = [
6973.10.1 by Jelmer Vernooij
Fix some tests.
3187
            (b'me:', b'a\n'),
3188
            (b'me:', b'b\n'),
3189
            (b'me:', b'c\n'),
7143.15.2 by Jelmer Vernooij
Run autopep8.
3190
            ]
3191
        annotation = preview_tree.annotate_iter(
3192
            'file', default_revision=b'me:')
3363.2.33 by Aaron Bentley
Implement PreviewTree.annotate_iter
3193
        self.assertEqual(expected, annotation)
3194
3363.7.3 by Aaron Bentley
Add test that annotate correctly handles renames
3195
    def test_annotate_rename(self):
3196
        tree = self.make_branch_and_tree('tree')
6855.4.1 by Jelmer Vernooij
Yet more bees.
3197
        self.build_tree_contents([('tree/file', b'a\n')])
3198
        tree.add('file', b'file-id')
3199
        tree.commit('a', rev_id=b'one')
3363.7.3 by Aaron Bentley
Add test that annotate correctly handles renames
3200
        preview = TransformPreview(tree)
3201
        self.addCleanup(preview.finalize)
6855.4.1 by Jelmer Vernooij
Yet more bees.
3202
        file_trans_id = preview.trans_id_file_id(b'file-id')
3363.7.3 by Aaron Bentley
Add test that annotate correctly handles renames
3203
        preview.adjust_path('newname', preview.root, file_trans_id)
3204
        preview_tree = preview.get_preview_tree()
3205
        expected = [
7027.3.3 by Jelmer Vernooij
Add some more bees; support writing both bytes and unicode strings in build_tree_contents.
3206
            (b'one', b'a\n'),
3363.7.3 by Aaron Bentley
Add test that annotate correctly handles renames
3207
        ]
7143.15.2 by Jelmer Vernooij
Run autopep8.
3208
        annotation = preview_tree.annotate_iter(
3209
            'file', default_revision=b'me:')
3363.7.3 by Aaron Bentley
Add test that annotate correctly handles renames
3210
        self.assertEqual(expected, annotation)
3211
3363.2.33 by Aaron Bentley
Implement PreviewTree.annotate_iter
3212
    def test_annotate_deleted(self):
3213
        tree = self.make_branch_and_tree('tree')
6855.4.1 by Jelmer Vernooij
Yet more bees.
3214
        self.build_tree_contents([('tree/file', b'a\n')])
3215
        tree.add('file', b'file-id')
3216
        tree.commit('a', rev_id=b'one')
3217
        self.build_tree_contents([('tree/file', b'a\nb\n')])
3363.2.33 by Aaron Bentley
Implement PreviewTree.annotate_iter
3218
        preview = TransformPreview(tree)
3219
        self.addCleanup(preview.finalize)
6855.4.1 by Jelmer Vernooij
Yet more bees.
3220
        file_trans_id = preview.trans_id_file_id(b'file-id')
3363.2.33 by Aaron Bentley
Implement PreviewTree.annotate_iter
3221
        preview.delete_contents(file_trans_id)
3222
        preview_tree = preview.get_preview_tree()
7143.15.2 by Jelmer Vernooij
Run autopep8.
3223
        annotation = preview_tree.annotate_iter(
3224
            'file', default_revision=b'me:')
3363.2.33 by Aaron Bentley
Implement PreviewTree.annotate_iter
3225
        self.assertIs(None, annotation)
3226
3363.2.36 by Aaron Bentley
Fix PreviewTree.stored_kind
3227
    def test_stored_kind(self):
3228
        preview = self.get_empty_preview()
6973.10.1 by Jelmer Vernooij
Fix some tests.
3229
        preview.new_file('file', preview.root, [b'a\nb\nc\n'], b'file-id')
3363.2.36 by Aaron Bentley
Fix PreviewTree.stored_kind
3230
        preview_tree = preview.get_preview_tree()
6809.4.7 by Jelmer Vernooij
Swap arguments for get_symlink_target and kind/stored_kind.
3231
        self.assertEqual('file', preview_tree.stored_kind('file'))
3363.2.37 by Aaron Bentley
Fix is_executable
3232
3233
    def test_is_executable(self):
3234
        preview = self.get_empty_preview()
6973.10.1 by Jelmer Vernooij
Fix some tests.
3235
        preview.new_file('file', preview.root, [b'a\nb\nc\n'], b'file-id')
6855.4.1 by Jelmer Vernooij
Yet more bees.
3236
        preview.set_executability(True, preview.trans_id_file_id(b'file-id'))
3363.2.37 by Aaron Bentley
Fix is_executable
3237
        preview_tree = preview.get_preview_tree()
6809.4.4 by Jelmer Vernooij
Swap arguments for Tree.is_executable.
3238
        self.assertEqual(True, preview_tree.is_executable('file'))
3363.9.1 by Aaron Bentley
Implement plan_merge, refactoring various bits
3239
3571.1.1 by Aaron Bentley
Allow set/get of parent_ids in PreviewTree
3240
    def test_get_set_parent_ids(self):
3241
        revision_tree, preview_tree = self.get_tree_and_preview_tree()
3242
        self.assertEqual([], preview_tree.get_parent_ids())
6855.4.1 by Jelmer Vernooij
Yet more bees.
3243
        preview_tree.set_parent_ids([b'rev-1'])
3244
        self.assertEqual([b'rev-1'], preview_tree.get_parent_ids())
3571.1.1 by Aaron Bentley
Allow set/get of parent_ids in PreviewTree
3245
3363.9.1 by Aaron Bentley
Implement plan_merge, refactoring various bits
3246
    def test_plan_file_merge(self):
3247
        work_a = self.make_branch_and_tree('wta')
6855.4.1 by Jelmer Vernooij
Yet more bees.
3248
        self.build_tree_contents([('wta/file', b'a\nb\nc\nd\n')])
3249
        work_a.add('file', b'file-id')
3363.9.7 by Aaron Bentley
Fix up to use set_parent_ids
3250
        base_id = work_a.commit('base version')
6653.6.1 by Jelmer Vernooij
Rename a number of attributes from bzrdir to controldir.
3251
        tree_b = work_a.controldir.sprout('wtb').open_workingtree()
3363.9.1 by Aaron Bentley
Implement plan_merge, refactoring various bits
3252
        preview = TransformPreview(work_a)
3253
        self.addCleanup(preview.finalize)
6855.4.1 by Jelmer Vernooij
Yet more bees.
3254
        trans_id = preview.trans_id_file_id(b'file-id')
3363.9.1 by Aaron Bentley
Implement plan_merge, refactoring various bits
3255
        preview.delete_contents(trans_id)
6973.5.10 by Jelmer Vernooij
Random bunch of python3 bee-improvements.
3256
        preview.create_file([b'b\nc\nd\ne\n'], trans_id)
6855.4.1 by Jelmer Vernooij
Yet more bees.
3257
        self.build_tree_contents([('wtb/file', b'a\nc\nd\nf\n')])
3363.9.1 by Aaron Bentley
Implement plan_merge, refactoring various bits
3258
        tree_a = preview.get_preview_tree()
3363.9.7 by Aaron Bentley
Fix up to use set_parent_ids
3259
        tree_a.set_parent_ids([base_id])
3363.9.1 by Aaron Bentley
Implement plan_merge, refactoring various bits
3260
        self.assertEqual([
7027.3.3 by Jelmer Vernooij
Add some more bees; support writing both bytes and unicode strings in build_tree_contents.
3261
            ('killed-a', b'a\n'),
3262
            ('killed-b', b'b\n'),
3263
            ('unchanged', b'c\n'),
3264
            ('unchanged', b'd\n'),
3265
            ('new-a', b'e\n'),
3266
            ('new-b', b'f\n'),
7350.6.4 by Jelmer Vernooij
Drop file_id attribute from MergeHookParams and Tree.plan_file_merge.
3267
        ], 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
3268
3269
    def test_plan_file_merge_revision_tree(self):
3270
        work_a = self.make_branch_and_tree('wta')
6855.4.1 by Jelmer Vernooij
Yet more bees.
3271
        self.build_tree_contents([('wta/file', b'a\nb\nc\nd\n')])
3272
        work_a.add('file', b'file-id')
3363.9.8 by Aaron Bentley
Ensure plan_file_merge works with a RevisionTree as the basis
3273
        base_id = work_a.commit('base version')
6653.6.1 by Jelmer Vernooij
Rename a number of attributes from bzrdir to controldir.
3274
        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
3275
        preview = TransformPreview(work_a.basis_tree())
3276
        self.addCleanup(preview.finalize)
6855.4.1 by Jelmer Vernooij
Yet more bees.
3277
        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
3278
        preview.delete_contents(trans_id)
6973.5.10 by Jelmer Vernooij
Random bunch of python3 bee-improvements.
3279
        preview.create_file([b'b\nc\nd\ne\n'], trans_id)
6855.4.1 by Jelmer Vernooij
Yet more bees.
3280
        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
3281
        tree_a = preview.get_preview_tree()
3282
        tree_a.set_parent_ids([base_id])
3283
        self.assertEqual([
7027.3.3 by Jelmer Vernooij
Add some more bees; support writing both bytes and unicode strings in build_tree_contents.
3284
            ('killed-a', b'a\n'),
3285
            ('killed-b', b'b\n'),
3286
            ('unchanged', b'c\n'),
3287
            ('unchanged', b'd\n'),
3288
            ('new-a', b'e\n'),
3289
            ('new-b', b'f\n'),
7350.6.4 by Jelmer Vernooij
Drop file_id attribute from MergeHookParams and Tree.plan_file_merge.
3290
        ], list(tree_a.plan_file_merge('file', tree_b)))
3363.9.9 by Aaron Bentley
Implement walkdirs in terms of TreeTransform
3291
3292
    def test_walkdirs(self):
3293
        preview = self.get_empty_preview()
7143.15.5 by Jelmer Vernooij
More PEP8 fixes.
3294
        preview.new_directory('', ROOT_PARENT, b'tree-root')
4634.57.3 by Aaron Bentley
Fix failing test.
3295
        # FIXME: new_directory should mark root.
4634.122.6 by John Arbash Meinel
Fix a test that used 'adjust_path' to set the root.
3296
        preview.fixup_new_roots()
3363.9.9 by Aaron Bentley
Implement walkdirs in terms of TreeTransform
3297
        preview_tree = preview.get_preview_tree()
7143.15.5 by Jelmer Vernooij
More PEP8 fixes.
3298
        preview.new_file('a', preview.root, [b'contents'], b'a-id')
6855.4.1 by Jelmer Vernooij
Yet more bees.
3299
        expected = [(('', b'tree-root'),
7143.15.2 by Jelmer Vernooij
Run autopep8.
3300
                     [('a', 'a', 'file', None, b'a-id', 'file')])]
3363.9.9 by Aaron Bentley
Implement walkdirs in terms of TreeTransform
3301
        self.assertEqual(expected, list(preview_tree.walkdirs()))
3363.13.2 by Aaron Bentley
Test specific cases for PreviewTree.extras
3302
3303
    def test_extras(self):
3304
        work_tree = self.make_branch_and_tree('tree')
3305
        self.build_tree(['tree/removed-file', 'tree/existing-file',
3306
                         'tree/not-removed-file'])
3307
        work_tree.add(['removed-file', 'not-removed-file'])
3308
        preview = TransformPreview(work_tree)
3363.13.3 by Aaron Bentley
Add cleanup
3309
        self.addCleanup(preview.finalize)
6973.10.1 by Jelmer Vernooij
Fix some tests.
3310
        preview.new_file('new-file', preview.root, [b'contents'])
3311
        preview.new_file('new-versioned-file', preview.root, [b'contents'],
6855.4.1 by Jelmer Vernooij
Yet more bees.
3312
                         b'new-versioned-id')
3363.13.2 by Aaron Bentley
Test specific cases for PreviewTree.extras
3313
        tree = preview.get_preview_tree()
3314
        preview.unversion_file(preview.trans_id_tree_path('removed-file'))
6619.3.12 by Jelmer Vernooij
Use 2to3 set_literal fixer.
3315
        self.assertEqual({'new-file', 'removed-file', 'existing-file'},
3363.13.2 by Aaron Bentley
Test specific cases for PreviewTree.extras
3316
                         set(tree.extras()))
3363.17.1 by Aaron Bentley
Avoid inventory for merge and transform code
3317
3363.17.2 by Aaron Bentley
Add text checking
3318
    def test_merge_into_preview(self):
3363.17.1 by Aaron Bentley
Avoid inventory for merge and transform code
3319
        work_tree = self.make_branch_and_tree('tree')
6855.4.1 by Jelmer Vernooij
Yet more bees.
3320
        self.build_tree_contents([('tree/file', b'b\n')])
3321
        work_tree.add('file', b'file-id')
3363.17.1 by Aaron Bentley
Avoid inventory for merge and transform code
3322
        work_tree.commit('first commit')
6653.6.1 by Jelmer Vernooij
Rename a number of attributes from bzrdir to controldir.
3323
        child_tree = work_tree.controldir.sprout('child').open_workingtree()
6855.4.1 by Jelmer Vernooij
Yet more bees.
3324
        self.build_tree_contents([('child/file', b'b\nc\n')])
3363.17.1 by Aaron Bentley
Avoid inventory for merge and transform code
3325
        child_tree.commit('child commit')
3326
        child_tree.lock_write()
3327
        self.addCleanup(child_tree.unlock)
3328
        work_tree.lock_write()
3329
        self.addCleanup(work_tree.unlock)
3330
        preview = TransformPreview(work_tree)
3331
        self.addCleanup(preview.finalize)
6855.4.1 by Jelmer Vernooij
Yet more bees.
3332
        file_trans_id = preview.trans_id_file_id(b'file-id')
3363.17.6 by Aaron Bentley
Improve test scenario
3333
        preview.delete_contents(file_trans_id)
6973.5.10 by Jelmer Vernooij
Random bunch of python3 bee-improvements.
3334
        preview.create_file([b'a\nb\n'], file_trans_id)
4634.57.2 by Aaron Bentley
Fix failing test.
3335
        preview_tree = preview.get_preview_tree()
6719.1.4 by Jelmer Vernooij
Fix remaining tests.
3336
        merger = Merger.from_revision_ids(preview_tree,
3363.17.1 by Aaron Bentley
Avoid inventory for merge and transform code
3337
                                          child_tree.branch.last_revision(),
3338
                                          other_branch=child_tree.branch,
3339
                                          tree_branch=work_tree.branch)
3340
        merger.merge_type = Merge3Merger
3341
        tt = merger.make_merger().make_preview_transform()
3363.17.2 by Aaron Bentley
Add text checking
3342
        self.addCleanup(tt.finalize)
3343
        final_tree = tt.get_preview_tree()
6809.4.5 by Jelmer Vernooij
Swap arguments for get_file_*.
3344
        self.assertEqual(
7143.15.2 by Jelmer Vernooij
Run autopep8.
3345
            b'a\nb\nc\n',
3346
            final_tree.get_file_text(final_tree.id2path(b'file-id')))
3363.17.17 by Aaron Bentley
Start testing merging PreviewTree as OTHER
3347
3348
    def test_merge_preview_into_workingtree(self):
3349
        tree = self.make_branch_and_tree('tree')
6855.4.1 by Jelmer Vernooij
Yet more bees.
3350
        tree.set_root_id(b'TREE_ROOT')
3363.17.17 by Aaron Bentley
Start testing merging PreviewTree as OTHER
3351
        tt = TransformPreview(tree)
3352
        self.addCleanup(tt.finalize)
6973.10.1 by Jelmer Vernooij
Fix some tests.
3353
        tt.new_file('name', tt.root, [b'content'], b'file-id')
3363.17.17 by Aaron Bentley
Start testing merging PreviewTree as OTHER
3354
        tree2 = self.make_branch_and_tree('tree2')
6855.4.1 by Jelmer Vernooij
Yet more bees.
3355
        tree2.set_root_id(b'TREE_ROOT')
3363.17.17 by Aaron Bentley
Start testing merging PreviewTree as OTHER
3356
        merger = Merger.from_uncommitted(tree2, tt.get_preview_tree(),
6719.1.4 by Jelmer Vernooij
Fix remaining tests.
3357
                                         tree.basis_tree())
3363.17.17 by Aaron Bentley
Start testing merging PreviewTree as OTHER
3358
        merger.merge_type = Merge3Merger
3359
        merger.do_merge()
3363.17.18 by Aaron Bentley
Fix is_executable for PreviewTree
3360
3363.17.21 by Aaron Bentley
Conflicts are handled when merging from preview trees
3361
    def test_merge_preview_into_workingtree_handles_conflicts(self):
3362
        tree = self.make_branch_and_tree('tree')
6855.4.1 by Jelmer Vernooij
Yet more bees.
3363
        self.build_tree_contents([('tree/foo', b'bar')])
3364
        tree.add('foo', b'foo-id')
3363.17.21 by Aaron Bentley
Conflicts are handled when merging from preview trees
3365
        tree.commit('foo')
3366
        tt = TransformPreview(tree)
3367
        self.addCleanup(tt.finalize)
6855.4.1 by Jelmer Vernooij
Yet more bees.
3368
        trans_id = tt.trans_id_file_id(b'foo-id')
3363.17.21 by Aaron Bentley
Conflicts are handled when merging from preview trees
3369
        tt.delete_contents(trans_id)
6973.5.10 by Jelmer Vernooij
Random bunch of python3 bee-improvements.
3370
        tt.create_file([b'baz'], trans_id)
6653.6.1 by Jelmer Vernooij
Rename a number of attributes from bzrdir to controldir.
3371
        tree2 = tree.controldir.sprout('tree2').open_workingtree()
6855.4.1 by Jelmer Vernooij
Yet more bees.
3372
        self.build_tree_contents([('tree2/foo', b'qux')])
3363.17.21 by Aaron Bentley
Conflicts are handled when merging from preview trees
3373
        merger = Merger.from_uncommitted(tree2, tt.get_preview_tree(),
6719.1.4 by Jelmer Vernooij
Fix remaining tests.
3374
                                         tree.basis_tree())
3363.17.21 by Aaron Bentley
Conflicts are handled when merging from preview trees
3375
        merger.merge_type = Merge3Merger
3376
        merger.do_merge()
3377
5809.3.2 by Aaron Bentley
Support PreviewTree.has_filename.
3378
    def test_has_filename(self):
3379
        wt = self.make_branch_and_tree('tree')
3380
        self.build_tree(['tree/unmodified', 'tree/removed', 'tree/modified'])
3381
        tt = TransformPreview(wt)
3382
        removed_id = tt.trans_id_tree_path('removed')
3383
        tt.delete_contents(removed_id)
6973.10.1 by Jelmer Vernooij
Fix some tests.
3384
        tt.new_file('new', tt.root, [b'contents'])
5809.3.2 by Aaron Bentley
Support PreviewTree.has_filename.
3385
        modified_id = tt.trans_id_tree_path('modified')
3386
        tt.delete_contents(modified_id)
6973.5.10 by Jelmer Vernooij
Random bunch of python3 bee-improvements.
3387
        tt.create_file([b'modified-contents'], modified_id)
5809.3.2 by Aaron Bentley
Support PreviewTree.has_filename.
3388
        self.addCleanup(tt.finalize)
3389
        tree = tt.get_preview_tree()
3390
        self.assertTrue(tree.has_filename('unmodified'))
3391
        self.assertFalse(tree.has_filename('not-present'))
3392
        self.assertFalse(tree.has_filename('removed'))
3393
        self.assertTrue(tree.has_filename('new'))
3394
        self.assertTrue(tree.has_filename('modified'))
3395
3363.17.18 by Aaron Bentley
Fix is_executable for PreviewTree
3396
    def test_is_executable(self):
3397
        tree = self.make_branch_and_tree('tree')
3398
        preview = TransformPreview(tree)
3399
        self.addCleanup(preview.finalize)
6973.10.1 by Jelmer Vernooij
Fix some tests.
3400
        preview.new_file('foo', preview.root, [b'bar'], b'baz-id')
3363.17.18 by Aaron Bentley
Fix is_executable for PreviewTree
3401
        preview_tree = preview.get_preview_tree()
6809.4.4 by Jelmer Vernooij
Swap arguments for Tree.is_executable.
3402
        self.assertEqual(False, preview_tree.is_executable('tree/foo'))
0.13.8 by Aaron Bentley
Integrate serialization into TreeTransforms
3403
4354.4.4 by Aaron Bentley
Simplify by using CommitBuilder directly
3404
    def test_commit_preview_tree(self):
3405
        tree = self.make_branch_and_tree('tree')
3406
        rev_id = tree.commit('rev1')
3407
        tree.branch.lock_write()
3408
        self.addCleanup(tree.branch.unlock)
3409
        tt = TransformPreview(tree)
6973.10.1 by Jelmer Vernooij
Fix some tests.
3410
        tt.new_file('file', tt.root, [b'contents'], b'file_id')
4354.4.4 by Aaron Bentley
Simplify by using CommitBuilder directly
3411
        self.addCleanup(tt.finalize)
3412
        preview = tt.get_preview_tree()
3413
        preview.set_parent_ids([rev_id])
3414
        builder = tree.branch.get_commit_builder([rev_id])
3415
        list(builder.record_iter_changes(preview, rev_id, tt.iter_changes()))
3416
        builder.finish_inventory()
3417
        rev2_id = builder.commit('rev2')
3418
        rev2_tree = tree.branch.repository.revision_tree(rev2_id)
6973.7.3 by Jelmer Vernooij
Fix some more tests.
3419
        self.assertEqual(b'contents', rev2_tree.get_file_text('file'))
4354.4.4 by Aaron Bentley
Simplify by using CommitBuilder directly
3420
4634.79.1 by Aaron Bentley
TransformPreview uses ascii-only filenames.
3421
    def test_ascii_limbo_paths(self):
5967.12.1 by Martin Pool
Move all test features into bzrlib.tests.features
3422
        self.requireFeature(features.UnicodeFilenameFeature)
4634.79.1 by Aaron Bentley
TransformPreview uses ascii-only filenames.
3423
        branch = self.make_branch('any')
3424
        tree = branch.repository.revision_tree(_mod_revision.NULL_REVISION)
3425
        tt = TransformPreview(tree)
4789.25.7 by John Arbash Meinel
We can run the executable tests.
3426
        self.addCleanup(tt.finalize)
4634.79.1 by Aaron Bentley
TransformPreview uses ascii-only filenames.
3427
        foo_id = tt.new_directory('', ROOT_PARENT)
6973.10.1 by Jelmer Vernooij
Fix some tests.
3428
        bar_id = tt.new_file(u'\u1234bar', foo_id, [b'contents'])
4634.79.1 by Aaron Bentley
TransformPreview uses ascii-only filenames.
3429
        limbo_path = tt._limbo_name(bar_id)
6973.10.1 by Jelmer Vernooij
Fix some tests.
3430
        self.assertEqual(limbo_path, limbo_path)
4634.79.1 by Aaron Bentley
TransformPreview uses ascii-only filenames.
3431
0.13.8 by Aaron Bentley
Integrate serialization into TreeTransforms
3432
0.13.13 by Aaron Bentley
Add direct test of serialization records
3433
class FakeSerializer(object):
3434
    """Serializer implementation that simply returns the input.
3435
3436
    The input is returned in the order used by pack.ContainerPushParser.
3437
    """
3438
    @staticmethod
3439
    def bytes_record(bytes, names):
3440
        return names, bytes
3441
3442
0.13.8 by Aaron Bentley
Integrate serialization into TreeTransforms
3443
class TestSerializeTransform(tests.TestCaseWithTransport):
3444
5967.12.1 by Martin Pool
Move all test features into bzrlib.tests.features
3445
    _test_needs_features = [features.UnicodeFilenameFeature]
0.13.22 by Aaron Bentley
More unicodeness for Shelf tests
3446
0.13.17 by Aaron Bentley
Convert roundtrip destruction test to serialization/deserialization pair
3447
    def get_preview(self, tree=None):
3448
        if tree is None:
3449
            tree = self.make_branch_and_tree('tree')
0.13.14 by Aaron Bentley
Add deserialization test, remove roundtrip test.
3450
        tt = TransformPreview(tree)
3451
        self.addCleanup(tt.finalize)
3452
        return tt
3453
0.13.18 by Aaron Bentley
Finish converting tests to direct serialize/deserialize tests, clean up
3454
    def assertSerializesTo(self, expected, tt):
3455
        records = list(tt.serialize(FakeSerializer()))
3456
        self.assertEqual(expected, records)
0.13.8 by Aaron Bentley
Integrate serialization into TreeTransforms
3457
0.13.13 by Aaron Bentley
Add direct test of serialization records
3458
    @staticmethod
3459
    def default_attribs():
3460
        return {
6973.10.1 by Jelmer Vernooij
Fix some tests.
3461
            b'_id_number': 1,
3462
            b'_new_name': {},
3463
            b'_new_parent': {},
3464
            b'_new_executability': {},
3465
            b'_new_id': {},
3466
            b'_tree_path_ids': {b'': b'new-0'},
3467
            b'_removed_id': [],
3468
            b'_removed_contents': [],
3469
            b'_non_present_ids': {},
0.13.13 by Aaron Bentley
Add direct test of serialization records
3470
            }
3471
0.13.18 by Aaron Bentley
Finish converting tests to direct serialize/deserialize tests, clean up
3472
    def make_records(self, attribs, contents):
3473
        records = [
7045.4.1 by Jelmer Vernooij
Some brz-git fixes.
3474
            ((((b'attribs'),),), bencode.bencode(attribs))]
0.13.18 by Aaron Bentley
Finish converting tests to direct serialize/deserialize tests, clean up
3475
        records.extend([(((n, k),), c) for n, k, c in contents])
3476
        return records
3477
0.13.13 by Aaron Bentley
Add direct test of serialization records
3478
    def creation_records(self):
3479
        attribs = self.default_attribs()
6973.10.1 by Jelmer Vernooij
Fix some tests.
3480
        attribs[b'_id_number'] = 3
3481
        attribs[b'_new_name'] = {
3482
            b'new-1': u'foo\u1234'.encode('utf-8'), b'new-2': b'qux'}
3483
        attribs[b'_new_id'] = {b'new-1': b'baz', b'new-2': b'quxx'}
3484
        attribs[b'_new_parent'] = {b'new-1': b'new-0', b'new-2': b'new-0'}
3485
        attribs[b'_new_executability'] = {b'new-1': 1}
0.13.13 by Aaron Bentley
Add direct test of serialization records
3486
        contents = [
7045.4.7 by Jelmer Vernooij
Fix remaining tests.
3487
            (b'new-1', b'file', b'i 1\nbar\n'),
3488
            (b'new-2', b'directory', b''),
0.13.13 by Aaron Bentley
Add direct test of serialization records
3489
            ]
3490
        return self.make_records(attribs, contents)
3491
3492
    def test_serialize_creation(self):
0.13.14 by Aaron Bentley
Add deserialization test, remove roundtrip test.
3493
        tt = self.get_preview()
6973.10.1 by Jelmer Vernooij
Fix some tests.
3494
        tt.new_file(u'foo\u1234', tt.root, [b'bar'], b'baz', True)
7045.4.7 by Jelmer Vernooij
Fix remaining tests.
3495
        tt.new_directory('qux', tt.root, b'quxx')
0.13.21 by Aaron Bentley
Use assertSerializesTo in more places
3496
        self.assertSerializesTo(self.creation_records(), tt)
0.13.13 by Aaron Bentley
Add direct test of serialization records
3497
0.13.14 by Aaron Bentley
Add deserialization test, remove roundtrip test.
3498
    def test_deserialize_creation(self):
3499
        tt = self.get_preview()
3500
        tt.deserialize(iter(self.creation_records()))
3501
        self.assertEqual(3, tt._id_number)
3502
        self.assertEqual({'new-1': u'foo\u1234',
3503
                          'new-2': 'qux'}, tt._new_name)
7045.4.7 by Jelmer Vernooij
Fix remaining tests.
3504
        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.
3505
        self.assertEqual({'new-1': tt.root, 'new-2': tt.root}, tt._new_parent)
7045.4.7 by Jelmer Vernooij
Fix remaining tests.
3506
        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.
3507
        self.assertEqual({'new-1': True}, tt._new_executability)
3508
        self.assertEqual({'new-1': 'file',
3509
                          'new-2': 'directory'}, tt._new_contents)
3510
        foo_limbo = open(tt._limbo_name('new-1'), 'rb')
3511
        try:
3512
            foo_content = foo_limbo.read()
3513
        finally:
3514
            foo_limbo.close()
7045.4.7 by Jelmer Vernooij
Fix remaining tests.
3515
        self.assertEqual(b'bar', foo_content)
0.13.14 by Aaron Bentley
Add deserialization test, remove roundtrip test.
3516
0.13.18 by Aaron Bentley
Finish converting tests to direct serialize/deserialize tests, clean up
3517
    def symlink_creation_records(self):
3518
        attribs = self.default_attribs()
7045.4.9 by Jelmer Vernooij
Fix some transform tests.
3519
        attribs[b'_id_number'] = 2
3520
        attribs[b'_new_name'] = {b'new-1': u'foo\u1234'.encode('utf-8')}
3521
        attribs[b'_new_parent'] = {b'new-1': b'new-0'}
7045.4.1 by Jelmer Vernooij
Some brz-git fixes.
3522
        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
3523
        return self.make_records(attribs, contents)
3524
0.13.15 by Aaron Bentley
Convert symlink tests to avoid roundtripping
3525
    def test_serialize_symlink_creation(self):
5967.12.1 by Martin Pool
Move all test features into bzrlib.tests.features
3526
        self.requireFeature(features.SymlinkFeature)
0.13.15 by Aaron Bentley
Convert symlink tests to avoid roundtripping
3527
        tt = self.get_preview()
0.13.16 by Aaron Bentley
Add unicode symlink targets to tests
3528
        tt.new_symlink(u'foo\u1234', tt.root, u'bar\u1234')
0.13.21 by Aaron Bentley
Use assertSerializesTo in more places
3529
        self.assertSerializesTo(self.symlink_creation_records(), tt)
0.13.15 by Aaron Bentley
Convert symlink tests to avoid roundtripping
3530
3531
    def test_deserialize_symlink_creation(self):
5967.12.1 by Martin Pool
Move all test features into bzrlib.tests.features
3532
        self.requireFeature(features.SymlinkFeature)
0.13.15 by Aaron Bentley
Convert symlink tests to avoid roundtripping
3533
        tt = self.get_preview()
3534
        tt.deserialize(iter(self.symlink_creation_records()))
4241.14.12 by Vincent Ladeuil
Far too many modifications for a single commit, need to restart.
3535
        abspath = tt._limbo_name('new-1')
4241.14.17 by Vincent Ladeuil
Add more tests for unicode symlinks to test_transform.
3536
        foo_content = osutils.readlink(abspath)
0.13.22 by Aaron Bentley
More unicodeness for Shelf tests
3537
        self.assertEqual(u'bar\u1234', foo_content)
0.13.8 by Aaron Bentley
Integrate serialization into TreeTransforms
3538
0.13.19 by Aaron Bentley
Clean up serialization tests
3539
    def make_destruction_preview(self):
0.13.8 by Aaron Bentley
Integrate serialization into TreeTransforms
3540
        tree = self.make_branch_and_tree('.')
3541
        self.build_tree([u'foo\u1234', 'bar'])
6855.4.1 by Jelmer Vernooij
Yet more bees.
3542
        tree.add([u'foo\u1234', 'bar'], [b'foo-id', b'bar-id'])
0.13.19 by Aaron Bentley
Clean up serialization tests
3543
        return self.get_preview(tree)
0.13.17 by Aaron Bentley
Convert roundtrip destruction test to serialization/deserialization pair
3544
0.13.18 by Aaron Bentley
Finish converting tests to direct serialize/deserialize tests, clean up
3545
    def destruction_records(self):
3546
        attribs = self.default_attribs()
7045.4.9 by Jelmer Vernooij
Fix some transform tests.
3547
        attribs[b'_id_number'] = 3
3548
        attribs[b'_removed_id'] = [b'new-1']
3549
        attribs[b'_removed_contents'] = [b'new-2']
3550
        attribs[b'_tree_path_ids'] = {
7045.4.7 by Jelmer Vernooij
Fix remaining tests.
3551
            b'': b'new-0',
3552
            u'foo\u1234'.encode('utf-8'): b'new-1',
3553
            b'bar': b'new-2',
0.13.18 by Aaron Bentley
Finish converting tests to direct serialize/deserialize tests, clean up
3554
            }
3555
        return self.make_records(attribs, [])
3556
0.13.17 by Aaron Bentley
Convert roundtrip destruction test to serialization/deserialization pair
3557
    def test_serialize_destruction(self):
0.13.19 by Aaron Bentley
Clean up serialization tests
3558
        tt = self.make_destruction_preview()
6885.1.1 by Jelmer Vernooij
Get rid of TreeTransform.trans_id_tree_file_id.
3559
        foo_trans_id = tt.trans_id_tree_path(u'foo\u1234')
0.13.8 by Aaron Bentley
Integrate serialization into TreeTransforms
3560
        tt.unversion_file(foo_trans_id)
6885.1.1 by Jelmer Vernooij
Get rid of TreeTransform.trans_id_tree_file_id.
3561
        bar_trans_id = tt.trans_id_tree_path('bar')
0.13.8 by Aaron Bentley
Integrate serialization into TreeTransforms
3562
        tt.delete_contents(bar_trans_id)
0.13.18 by Aaron Bentley
Finish converting tests to direct serialize/deserialize tests, clean up
3563
        self.assertSerializesTo(self.destruction_records(), tt)
0.13.17 by Aaron Bentley
Convert roundtrip destruction test to serialization/deserialization pair
3564
3565
    def test_deserialize_destruction(self):
0.13.19 by Aaron Bentley
Clean up serialization tests
3566
        tt = self.make_destruction_preview()
0.13.17 by Aaron Bentley
Convert roundtrip destruction test to serialization/deserialization pair
3567
        tt.deserialize(iter(self.destruction_records()))
3568
        self.assertEqual({u'foo\u1234': 'new-1',
3569
                          'bar': 'new-2',
3570
                          '': tt.root}, tt._tree_path_ids)
3571
        self.assertEqual({'new-1': u'foo\u1234',
3572
                          'new-2': 'bar',
3573
                          tt.root: ''}, tt._tree_id_paths)
6619.3.12 by Jelmer Vernooij
Use 2to3 set_literal fixer.
3574
        self.assertEqual({'new-1'}, tt._removed_id)
3575
        self.assertEqual({'new-2'}, tt._removed_contents)
0.13.8 by Aaron Bentley
Integrate serialization into TreeTransforms
3576
0.13.18 by Aaron Bentley
Finish converting tests to direct serialize/deserialize tests, clean up
3577
    def missing_records(self):
3578
        attribs = self.default_attribs()
6973.10.1 by Jelmer Vernooij
Fix some tests.
3579
        attribs[b'_id_number'] = 2
3580
        attribs[b'_non_present_ids'] = {
7143.15.2 by Jelmer Vernooij
Run autopep8.
3581
            b'boo': b'new-1', }
0.13.18 by Aaron Bentley
Finish converting tests to direct serialize/deserialize tests, clean up
3582
        return self.make_records(attribs, [])
3583
3584
    def test_serialize_missing(self):
3585
        tt = self.get_preview()
7143.15.5 by Jelmer Vernooij
More PEP8 fixes.
3586
        tt.trans_id_file_id(b'boo')
0.13.18 by Aaron Bentley
Finish converting tests to direct serialize/deserialize tests, clean up
3587
        self.assertSerializesTo(self.missing_records(), tt)
3588
3589
    def test_deserialize_missing(self):
3590
        tt = self.get_preview()
3591
        tt.deserialize(iter(self.missing_records()))
7045.3.1 by Jelmer Vernooij
Fix another ~500 tests.
3592
        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
3593
3594
    def make_modification_preview(self):
6855.4.1 by Jelmer Vernooij
Yet more bees.
3595
        LINES_ONE = b'aa\nbb\ncc\ndd\n'
3596
        LINES_TWO = b'z\nbb\nx\ndd\n'
0.13.8 by Aaron Bentley
Integrate serialization into TreeTransforms
3597
        tree = self.make_branch_and_tree('tree')
3598
        self.build_tree_contents([('tree/file', LINES_ONE)])
6855.4.1 by Jelmer Vernooij
Yet more bees.
3599
        tree.add('file', b'file-id')
7045.4.1 by Jelmer Vernooij
Some brz-git fixes.
3600
        return self.get_preview(tree), [LINES_TWO]
0.13.18 by Aaron Bentley
Finish converting tests to direct serialize/deserialize tests, clean up
3601
3602
    def modification_records(self):
3603
        attribs = self.default_attribs()
7045.4.9 by Jelmer Vernooij
Fix some transform tests.
3604
        attribs[b'_id_number'] = 2
3605
        attribs[b'_tree_path_ids'] = {
7045.4.7 by Jelmer Vernooij
Fix remaining tests.
3606
            b'file': b'new-1',
7143.15.2 by Jelmer Vernooij
Run autopep8.
3607
            b'': b'new-0', }
7045.4.9 by Jelmer Vernooij
Fix some transform tests.
3608
        attribs[b'_removed_contents'] = [b'new-1']
7045.4.7 by Jelmer Vernooij
Fix remaining tests.
3609
        contents = [(b'new-1', b'file',
7045.4.1 by Jelmer Vernooij
Some brz-git fixes.
3610
                     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
3611
        return self.make_records(attribs, contents)
3612
3613
    def test_serialize_modification(self):
3614
        tt, LINES = self.make_modification_preview()
6973.10.1 by Jelmer Vernooij
Fix some tests.
3615
        trans_id = tt.trans_id_file_id(b'file-id')
0.13.8 by Aaron Bentley
Integrate serialization into TreeTransforms
3616
        tt.delete_contents(trans_id)
0.13.18 by Aaron Bentley
Finish converting tests to direct serialize/deserialize tests, clean up
3617
        tt.create_file(LINES, trans_id)
3618
        self.assertSerializesTo(self.modification_records(), tt)
3619
3620
    def test_deserialize_modification(self):
3621
        tt, LINES = self.make_modification_preview()
3622
        tt.deserialize(iter(self.modification_records()))
7045.4.7 by Jelmer Vernooij
Fix remaining tests.
3623
        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
3624
3625
    def make_kind_change_preview(self):
6973.10.1 by Jelmer Vernooij
Fix some tests.
3626
        LINES = b'a\nb\nc\nd\n'
0.13.8 by Aaron Bentley
Integrate serialization into TreeTransforms
3627
        tree = self.make_branch_and_tree('tree')
3628
        self.build_tree(['tree/foo/'])
6855.4.1 by Jelmer Vernooij
Yet more bees.
3629
        tree.add('foo', b'foo-id')
7045.4.7 by Jelmer Vernooij
Fix remaining tests.
3630
        return self.get_preview(tree), [LINES]
0.13.18 by Aaron Bentley
Finish converting tests to direct serialize/deserialize tests, clean up
3631
3632
    def kind_change_records(self):
3633
        attribs = self.default_attribs()
6973.10.1 by Jelmer Vernooij
Fix some tests.
3634
        attribs[b'_id_number'] = 2
3635
        attribs[b'_tree_path_ids'] = {
3636
            b'foo': b'new-1',
7143.15.2 by Jelmer Vernooij
Run autopep8.
3637
            b'': b'new-0', }
6973.10.1 by Jelmer Vernooij
Fix some tests.
3638
        attribs[b'_removed_contents'] = [b'new-1']
7045.3.1 by Jelmer Vernooij
Fix another ~500 tests.
3639
        contents = [(b'new-1', b'file',
6973.10.1 by Jelmer Vernooij
Fix some tests.
3640
                     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
3641
        return self.make_records(attribs, contents)
3642
3643
    def test_serialize_kind_change(self):
3644
        tt, LINES = self.make_kind_change_preview()
6855.4.1 by Jelmer Vernooij
Yet more bees.
3645
        trans_id = tt.trans_id_file_id(b'foo-id')
0.13.8 by Aaron Bentley
Integrate serialization into TreeTransforms
3646
        tt.delete_contents(trans_id)
0.13.18 by Aaron Bentley
Finish converting tests to direct serialize/deserialize tests, clean up
3647
        tt.create_file(LINES, trans_id)
3648
        self.assertSerializesTo(self.kind_change_records(), tt)
3649
3650
    def test_deserialize_kind_change(self):
3651
        tt, LINES = self.make_kind_change_preview()
3652
        tt.deserialize(iter(self.kind_change_records()))
7045.4.7 by Jelmer Vernooij
Fix remaining tests.
3653
        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
3654
3655
    def make_add_contents_preview(self):
6973.10.1 by Jelmer Vernooij
Fix some tests.
3656
        LINES = b'a\nb\nc\nd\n'
0.13.8 by Aaron Bentley
Integrate serialization into TreeTransforms
3657
        tree = self.make_branch_and_tree('tree')
3658
        self.build_tree(['tree/foo'])
3659
        tree.add('foo')
3660
        os.unlink('tree/foo')
0.13.18 by Aaron Bentley
Finish converting tests to direct serialize/deserialize tests, clean up
3661
        return self.get_preview(tree), LINES
3662
3663
    def add_contents_records(self):
3664
        attribs = self.default_attribs()
6973.10.1 by Jelmer Vernooij
Fix some tests.
3665
        attribs[b'_id_number'] = 2
3666
        attribs[b'_tree_path_ids'] = {
3667
            b'foo': b'new-1',
7143.15.2 by Jelmer Vernooij
Run autopep8.
3668
            b'': b'new-0', }
7045.3.1 by Jelmer Vernooij
Fix another ~500 tests.
3669
        contents = [(b'new-1', b'file',
6973.10.1 by Jelmer Vernooij
Fix some tests.
3670
                     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
3671
        return self.make_records(attribs, contents)
3672
3673
    def test_serialize_add_contents(self):
3674
        tt, LINES = self.make_add_contents_preview()
0.13.8 by Aaron Bentley
Integrate serialization into TreeTransforms
3675
        trans_id = tt.trans_id_tree_path('foo')
6973.10.1 by Jelmer Vernooij
Fix some tests.
3676
        tt.create_file([LINES], trans_id)
0.13.18 by Aaron Bentley
Finish converting tests to direct serialize/deserialize tests, clean up
3677
        self.assertSerializesTo(self.add_contents_records(), tt)
3678
3679
    def test_deserialize_add_contents(self):
3680
        tt, LINES = self.make_add_contents_preview()
3681
        tt.deserialize(iter(self.add_contents_records()))
3682
        self.assertFileEqual(LINES, tt._limbo_name('new-1'))
0.13.8 by Aaron Bentley
Integrate serialization into TreeTransforms
3683
3684
    def test_get_parents_lines(self):
6855.4.1 by Jelmer Vernooij
Yet more bees.
3685
        LINES_ONE = b'aa\nbb\ncc\ndd\n'
0.13.8 by Aaron Bentley
Integrate serialization into TreeTransforms
3686
        tree = self.make_branch_and_tree('tree')
3687
        self.build_tree_contents([('tree/file', LINES_ONE)])
6855.4.1 by Jelmer Vernooij
Yet more bees.
3688
        tree.add('file', b'file-id')
0.13.18 by Aaron Bentley
Finish converting tests to direct serialize/deserialize tests, clean up
3689
        tt = self.get_preview(tree)
0.13.8 by Aaron Bentley
Integrate serialization into TreeTransforms
3690
        trans_id = tt.trans_id_tree_path('file')
6973.10.1 by Jelmer Vernooij
Fix some tests.
3691
        self.assertEqual(([b'aa\n', b'bb\n', b'cc\n', b'dd\n'],),
7143.15.2 by Jelmer Vernooij
Run autopep8.
3692
                         tt._get_parents_lines(trans_id))
0.13.8 by Aaron Bentley
Integrate serialization into TreeTransforms
3693
3694
    def test_get_parents_texts(self):
6855.4.1 by Jelmer Vernooij
Yet more bees.
3695
        LINES_ONE = b'aa\nbb\ncc\ndd\n'
0.13.8 by Aaron Bentley
Integrate serialization into TreeTransforms
3696
        tree = self.make_branch_and_tree('tree')
3697
        self.build_tree_contents([('tree/file', LINES_ONE)])
6855.4.1 by Jelmer Vernooij
Yet more bees.
3698
        tree.add('file', b'file-id')
0.13.18 by Aaron Bentley
Finish converting tests to direct serialize/deserialize tests, clean up
3699
        tt = self.get_preview(tree)
0.13.8 by Aaron Bentley
Integrate serialization into TreeTransforms
3700
        trans_id = tt.trans_id_tree_path('file')
3701
        self.assertEqual((LINES_ONE,),
7143.15.2 by Jelmer Vernooij
Run autopep8.
3702
                         tt._get_parents_texts(trans_id))
5409.1.7 by Vincent Ladeuil
First orphaning implementation (some tests lacking).
3703
3704
3705
class TestOrphan(tests.TestCaseWithTransport):
3706
3707
    def test_no_orphan_for_transform_preview(self):
3708
        tree = self.make_branch_and_tree('tree')
5409.7.2 by Vincent Ladeuil
Add NEWS entry, a missing test and some cleanup.
3709
        tt = transform.TransformPreview(tree)
5409.1.7 by Vincent Ladeuil
First orphaning implementation (some tests lacking).
3710
        self.addCleanup(tt.finalize)
3711
        self.assertRaises(NotImplementedError, tt.new_orphan, 'foo', 'bar')
3712
5409.1.16 by Vincent Ladeuil
Add ``bzrlib.transform.orphan_policy`` and allows ``never`` to restore the previous behaviour.
3713
    def _set_orphan_policy(self, wt, policy):
6883.13.1 by Jelmer Vernooij
Rename bzr.transform.orphan_policy -> transform.orphan_policy.
3714
        wt.branch.get_config_stack().set('transform.orphan_policy',
7143.15.2 by Jelmer Vernooij
Run autopep8.
3715
                                         policy)
5409.1.16 by Vincent Ladeuil
Add ``bzrlib.transform.orphan_policy`` and allows ``never`` to restore the previous behaviour.
3716
3717
    def _prepare_orphan(self, wt):
5540.3.1 by Vincent Ladeuil
Fix spurious orphan reports.
3718
        self.build_tree(['dir/', 'dir/file', 'dir/foo'])
6855.4.1 by Jelmer Vernooij
Yet more bees.
3719
        wt.add(['dir', 'dir/file'], [b'dir-id', b'file-id'])
5540.3.1 by Vincent Ladeuil
Fix spurious orphan reports.
3720
        wt.commit('add dir and file ignoring foo')
5409.7.2 by Vincent Ladeuil
Add NEWS entry, a missing test and some cleanup.
3721
        tt = transform.TreeTransform(wt)
3722
        self.addCleanup(tt.finalize)
5540.3.1 by Vincent Ladeuil
Fix spurious orphan reports.
3723
        # dir and bar are deleted
5409.7.2 by Vincent Ladeuil
Add NEWS entry, a missing test and some cleanup.
3724
        dir_tid = tt.trans_id_tree_path('dir')
5540.3.1 by Vincent Ladeuil
Fix spurious orphan reports.
3725
        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.
3726
        orphan_tid = tt.trans_id_tree_path('dir/foo')
5540.3.1 by Vincent Ladeuil
Fix spurious orphan reports.
3727
        tt.delete_contents(file_tid)
3728
        tt.unversion_file(file_tid)
5409.7.2 by Vincent Ladeuil
Add NEWS entry, a missing test and some cleanup.
3729
        tt.delete_contents(dir_tid)
3730
        tt.unversion_file(dir_tid)
5540.3.1 by Vincent Ladeuil
Fix spurious orphan reports.
3731
        # 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.
3732
        raw_conflicts = tt.find_conflicts()
3733
        self.assertLength(1, raw_conflicts)
3734
        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.
3735
        return tt, orphan_tid
3736
3737
    def test_new_orphan_created(self):
3738
        wt = self.make_branch_and_tree('.')
5409.1.20 by Vincent Ladeuil
Revert to 'conflict' being the default orphaning policy and fix fallouts.
3739
        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.
3740
        tt, orphan_tid = self._prepare_orphan(wt)
5540.3.1 by Vincent Ladeuil
Fix spurious orphan reports.
3741
        warnings = []
7143.15.2 by Jelmer Vernooij
Run autopep8.
3742
5540.3.1 by Vincent Ladeuil
Fix spurious orphan reports.
3743
        def warning(*args):
3744
            warnings.append(args[0] % args[1:])
3745
        self.overrideAttr(trace, 'warning', warning)
5409.7.2 by Vincent Ladeuil
Add NEWS entry, a missing test and some cleanup.
3746
        remaining_conflicts = resolve_conflicts(tt)
6681.2.10 by Jelmer Vernooij
Fix failures.
3747
        self.assertEqual(['dir/foo has been orphaned in brz-orphans'],
7143.15.2 by Jelmer Vernooij
Run autopep8.
3748
                         warnings)
5409.7.2 by Vincent Ladeuil
Add NEWS entry, a missing test and some cleanup.
3749
        # Yeah for resolved conflicts !
3750
        self.assertLength(0, remaining_conflicts)
3751
        # We have a new orphan
6614.1.3 by Vincent Ladeuil
Fix assertEquals being deprecated by using assertEqual.
3752
        self.assertEqual('foo.~1~', tt.final_name(orphan_tid))
6681.2.10 by Jelmer Vernooij
Fix failures.
3753
        self.assertEqual('brz-orphans',
7143.15.2 by Jelmer Vernooij
Run autopep8.
3754
                         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.
3755
3756
    def test_never_orphan(self):
3757
        wt = self.make_branch_and_tree('.')
5409.1.20 by Vincent Ladeuil
Revert to 'conflict' being the default orphaning policy and fix fallouts.
3758
        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.
3759
        tt, orphan_tid = self._prepare_orphan(wt)
3760
        remaining_conflicts = resolve_conflicts(tt)
3761
        self.assertLength(1, remaining_conflicts)
3762
        self.assertEqual(('deleting parent', 'Not deleting', 'new-1'),
3763
                         remaining_conflicts.pop())
3764
3765
    def test_orphan_error(self):
3766
        def bogus_orphan(tt, orphan_id, parent_id):
3767
            raise transform.OrphaningError(tt.final_name(orphan_id),
3768
                                           tt.final_name(parent_id))
3769
        transform.orphaning_registry.register('bogus', bogus_orphan,
3770
                                              'Raise an error when orphaning')
3771
        wt = self.make_branch_and_tree('.')
3772
        self._set_orphan_policy(wt, 'bogus')
3773
        tt, orphan_tid = self._prepare_orphan(wt)
3774
        remaining_conflicts = resolve_conflicts(tt)
3775
        self.assertLength(1, remaining_conflicts)
3776
        self.assertEqual(('deleting parent', 'Not deleting', 'new-1'),
3777
                         remaining_conflicts.pop())
5409.1.17 by Vincent Ladeuil
Ensures we fallback to the default policy if a bogus one is specified.
3778
3779
    def test_unknown_orphan_policy(self):
3780
        wt = self.make_branch_and_tree('.')
3781
        # Set a fictional policy nobody ever implemented
3782
        self._set_orphan_policy(wt, 'donttouchmypreciouuus')
3783
        tt, orphan_tid = self._prepare_orphan(wt)
3784
        warnings = []
7143.15.2 by Jelmer Vernooij
Run autopep8.
3785
5409.1.17 by Vincent Ladeuil
Ensures we fallback to the default policy if a bogus one is specified.
3786
        def warning(*args):
3787
            warnings.append(args[0] % args[1:])
3788
        self.overrideAttr(trace, 'warning', warning)
3789
        remaining_conflicts = resolve_conflicts(tt)
5409.1.20 by Vincent Ladeuil
Revert to 'conflict' being the default orphaning policy and fix fallouts.
3790
        # We fallback to the default policy which create a conflict
3791
        self.assertLength(1, remaining_conflicts)
3792
        self.assertEqual(('deleting parent', 'Not deleting', 'new-1'),
3793
                         remaining_conflicts.pop())
3794
        self.assertLength(1, warnings)
6449.6.7 by Jelmer Vernooij
Fix tests.
3795
        self.assertStartsWith(warnings[0], 'Value "donttouchmypreciouuus" ')
6434.2.2 by Jelmer Vernooij
Add hooks for tree transforms.
3796
3797
3798
class TestTransformHooks(tests.TestCaseWithTransport):
3799
3800
    def setUp(self):
3801
        super(TestTransformHooks, self).setUp()
3802
        self.wt = self.make_branch_and_tree('.')
3803
        os.chdir('..')
3804
3805
    def get_transform(self):
3806
        transform = TreeTransform(self.wt)
3807
        self.addCleanup(transform.finalize)
3808
        return transform, transform.root
3809
3810
    def test_pre_commit_hooks(self):
3811
        calls = []
7143.15.2 by Jelmer Vernooij
Run autopep8.
3812
6434.2.2 by Jelmer Vernooij
Add hooks for tree transforms.
3813
        def record_pre_transform(tree, tt):
3814
            calls.append((tree, tt))
7143.15.5 by Jelmer Vernooij
More PEP8 fixes.
3815
        MutableTree.hooks.install_named_hook(
3816
            'pre_transform', record_pre_transform, "Pre transform")
6434.2.2 by Jelmer Vernooij
Add hooks for tree transforms.
3817
        transform, root = self.get_transform()
3818
        old_root_id = transform.tree_file_id(root)
3819
        transform.apply()
3820
        self.assertEqual(old_root_id, self.wt.get_root_id())
6614.1.3 by Vincent Ladeuil
Fix assertEquals being deprecated by using assertEqual.
3821
        self.assertEqual([(self.wt, transform)], calls)
6434.2.2 by Jelmer Vernooij
Add hooks for tree transforms.
3822
3823
    def test_post_commit_hooks(self):
3824
        calls = []
7143.15.2 by Jelmer Vernooij
Run autopep8.
3825
6434.2.2 by Jelmer Vernooij
Add hooks for tree transforms.
3826
        def record_post_transform(tree, tt):
3827
            calls.append((tree, tt))
7143.15.5 by Jelmer Vernooij
More PEP8 fixes.
3828
        MutableTree.hooks.install_named_hook(
3829
            'post_transform', record_post_transform, "Post transform")
6434.2.2 by Jelmer Vernooij
Add hooks for tree transforms.
3830
        transform, root = self.get_transform()
3831
        old_root_id = transform.tree_file_id(root)
3832
        transform.apply()
3833
        self.assertEqual(old_root_id, self.wt.get_root_id())
6614.1.3 by Vincent Ladeuil
Fix assertEquals being deprecated by using assertEqual.
3834
        self.assertEqual([(self.wt, transform)], calls)
6652.1.1 by Jelmer Vernooij
Bundle the link-tree command.
3835
3836
3837
class TestLinkTree(tests.TestCaseWithTransport):
3838
3839
    _test_needs_features = [HardlinkFeature]
3840
3841
    def setUp(self):
3842
        tests.TestCaseWithTransport.setUp(self)
3843
        self.parent_tree = self.make_branch_and_tree('parent')
3844
        self.parent_tree.lock_write()
3845
        self.addCleanup(self.parent_tree.unlock)
6855.4.1 by Jelmer Vernooij
Yet more bees.
3846
        self.build_tree_contents([('parent/foo', b'bar')])
6885.1.1 by Jelmer Vernooij
Get rid of TreeTransform.trans_id_tree_file_id.
3847
        self.parent_tree.add('foo')
6652.1.1 by Jelmer Vernooij
Bundle the link-tree command.
3848
        self.parent_tree.commit('added foo')
6652.1.4 by Jelmer Vernooij
Fix test.
3849
        child_controldir = self.parent_tree.controldir.sprout('child')
3850
        self.child_tree = child_controldir.open_workingtree()
6652.1.1 by Jelmer Vernooij
Bundle the link-tree command.
3851
3852
    def hardlinked(self):
3853
        parent_stat = os.lstat(self.parent_tree.abspath('foo'))
3854
        child_stat = os.lstat(self.child_tree.abspath('foo'))
3855
        return parent_stat.st_ino == child_stat.st_ino
3856
3857
    def test_link_fails_if_modified(self):
3858
        """If the file to be linked has modified text, don't link."""
6855.4.1 by Jelmer Vernooij
Yet more bees.
3859
        self.build_tree_contents([('child/foo', b'baz')])
6652.1.1 by Jelmer Vernooij
Bundle the link-tree command.
3860
        transform.link_tree(self.child_tree, self.parent_tree)
3861
        self.assertFalse(self.hardlinked())
3862
3863
    def test_link_fails_if_execute_bit_changed(self):
3864
        """If the file to be linked has modified execute bit, don't link."""
3865
        tt = TreeTransform(self.child_tree)
3866
        try:
6885.1.1 by Jelmer Vernooij
Get rid of TreeTransform.trans_id_tree_file_id.
3867
            trans_id = tt.trans_id_tree_path('foo')
6652.1.1 by Jelmer Vernooij
Bundle the link-tree command.
3868
            tt.set_executability(True, trans_id)
3869
            tt.apply()
3870
        finally:
3871
            tt.finalize()
3872
        transform.link_tree(self.child_tree, self.parent_tree)
3873
        self.assertFalse(self.hardlinked())
3874
3875
    def test_link_succeeds_if_unmodified(self):
3876
        """If the file to be linked is unmodified, link"""
3877
        transform.link_tree(self.child_tree, self.parent_tree)
3878
        self.assertTrue(self.hardlinked())