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