~bzr-pqm/bzr/bzr.dev

« back to all changes in this revision

Viewing changes to bzrlib/tests/test_transform.py

  • Committer: Canonical.com Patch Queue Manager
  • Date: 2008-05-29 15:57:16 UTC
  • mfrom: (3427.5.9 dep_warnings)
  • Revision ID: pqm@pqm.ubuntu.com-20080529155716-0w3kic8lioa63231
(jam) Enable Deprecation Warnings when running -Werror and when
        running selftest

Show diffs side-by-side

added added

removed removed

Lines of Context:
1
1
# Copyright (C) 2006 Canonical Ltd
2
 
 
 
2
#
3
3
# This program is free software; you can redistribute it and/or modify
4
4
# it under the terms of the GNU General Public License as published by
5
5
# the Free Software Foundation; either version 2 of the License, or
6
6
# (at your option) any later version.
7
 
 
 
7
#
8
8
# This program is distributed in the hope that it will be useful,
9
9
# but WITHOUT ANY WARRANTY; without even the implied warranty of
10
10
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
11
11
# GNU General Public License for more details.
12
 
 
 
12
#
13
13
# You should have received a copy of the GNU General Public License
14
14
# along with this program; if not, write to the Free Software
15
15
# Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307  USA
16
16
 
17
17
import os
 
18
import stat
 
19
from StringIO import StringIO
 
20
import sys
18
21
 
 
22
from bzrlib import (
 
23
    errors,
 
24
    generate_ids,
 
25
    osutils,
 
26
    progress,
 
27
    revision as _mod_revision,
 
28
    symbol_versioning,
 
29
    tests,
 
30
    urlutils,
 
31
    )
19
32
from bzrlib.bzrdir import BzrDir
20
33
from bzrlib.conflicts import (DuplicateEntry, DuplicateID, MissingParent,
21
 
                              UnversionedParent, ParentLoop)
 
34
                              UnversionedParent, ParentLoop, DeletingParent,
 
35
                              NonDirectoryParent)
 
36
from bzrlib.diff import show_diff_trees
22
37
from bzrlib.errors import (DuplicateKey, MalformedTransform, NoSuchFile,
23
 
                           ReusingTransform, CantMoveRoot, NotVersionedError,
24
 
                           ExistingLimbo, ImmortalLimbo, LockError)
25
 
from bzrlib.osutils import file_kind, has_symlinks, pathjoin
 
38
                           ReusingTransform, CantMoveRoot, 
 
39
                           PathsNotVersionedError, ExistingLimbo,
 
40
                           ExistingPendingDeletion, ImmortalLimbo,
 
41
                           ImmortalPendingDeletion, LockError)
 
42
from bzrlib.osutils import file_kind, pathjoin
26
43
from bzrlib.merge import Merge3Merger
27
 
from bzrlib.tests import TestCaseInTempDir, TestSkipped, TestCase
 
44
from bzrlib.tests import (
 
45
    CaseInsensitiveFilesystemFeature,
 
46
    HardlinkFeature,
 
47
    SymlinkFeature,
 
48
    TestCase,
 
49
    TestCaseInTempDir,
 
50
    TestSkipped,
 
51
    )
28
52
from bzrlib.transform import (TreeTransform, ROOT_PARENT, FinalPaths, 
29
53
                              resolve_conflicts, cook_conflicts, 
30
 
                              find_interesting, build_tree, get_backup_name)
31
 
 
32
 
class TestTreeTransform(TestCaseInTempDir):
 
54
                              build_tree, get_backup_name,
 
55
                              _FileMover, resolve_checkout,
 
56
                              TransformPreview)
 
57
 
 
58
class TestTreeTransform(tests.TestCaseWithTransport):
 
59
 
33
60
    def setUp(self):
34
61
        super(TestTreeTransform, self).setUp()
35
 
        self.wt = BzrDir.create_standalone_workingtree('.')
 
62
        self.wt = self.make_branch_and_tree('.', format='dirstate-with-subtree')
36
63
        os.chdir('..')
37
64
 
38
65
    def get_transform(self):
39
66
        transform = TreeTransform(self.wt)
40
67
        #self.addCleanup(transform.finalize)
41
 
        return transform, transform.trans_id_tree_file_id(self.wt.get_root_id())
 
68
        return transform, transform.root
42
69
 
43
70
    def test_existing_limbo(self):
44
 
        limbo_name = self.wt._control_files.controlfilename('limbo')
45
71
        transform, root = self.get_transform()
 
72
        limbo_name = transform._limbodir
 
73
        deletion_path = transform._deletiondir
46
74
        os.mkdir(pathjoin(limbo_name, 'hehe'))
47
75
        self.assertRaises(ImmortalLimbo, transform.apply)
48
76
        self.assertRaises(LockError, self.wt.unlock)
50
78
        self.assertRaises(LockError, self.wt.unlock)
51
79
        os.rmdir(pathjoin(limbo_name, 'hehe'))
52
80
        os.rmdir(limbo_name)
 
81
        os.rmdir(deletion_path)
53
82
        transform, root = self.get_transform()
54
83
        transform.apply()
55
84
 
 
85
    def test_existing_pending_deletion(self):
 
86
        transform, root = self.get_transform()
 
87
        deletion_path = self._limbodir = urlutils.local_path_from_url(
 
88
            transform._tree._transport.abspath('pending-deletion'))
 
89
        os.mkdir(pathjoin(deletion_path, 'blocking-directory'))
 
90
        self.assertRaises(ImmortalPendingDeletion, transform.apply)
 
91
        self.assertRaises(LockError, self.wt.unlock)
 
92
        self.assertRaises(ExistingPendingDeletion, self.get_transform)
 
93
 
56
94
    def test_build(self):
57
 
        transform, root = self.get_transform() 
 
95
        transform, root = self.get_transform()
 
96
        self.wt.lock_tree_write()
 
97
        self.addCleanup(self.wt.unlock)
58
98
        self.assertIs(transform.get_tree_parent(root), ROOT_PARENT)
59
99
        imaginary_id = transform.trans_id_tree_path('imaginary')
 
100
        imaginary_id2 = transform.trans_id_tree_path('imaginary/')
 
101
        self.assertEqual(imaginary_id, imaginary_id2)
60
102
        self.assertEqual(transform.get_tree_parent(imaginary_id), root)
61
103
        self.assertEqual(transform.final_kind(root), 'directory')
62
104
        self.assertEqual(transform.final_file_id(root), self.wt.get_root_id())
93
135
        transform.finalize()
94
136
        transform.finalize()
95
137
 
 
138
    def test_hardlink(self):
 
139
        self.requireFeature(HardlinkFeature)
 
140
        transform, root = self.get_transform()
 
141
        transform.new_file('file1', root, 'contents')
 
142
        transform.apply()
 
143
        target = self.make_branch_and_tree('target')
 
144
        target_transform = TreeTransform(target)
 
145
        trans_id = target_transform.create_path('file1', target_transform.root)
 
146
        target_transform.create_hardlink(self.wt.abspath('file1'), trans_id)
 
147
        target_transform.apply()
 
148
        self.failUnlessExists('target/file1')
 
149
        source_stat = os.stat(self.wt.abspath('file1'))
 
150
        target_stat = os.stat('target/file1')
 
151
        self.assertEqual(source_stat, target_stat)
 
152
 
96
153
    def test_convenience(self):
97
154
        transform, root = self.get_transform()
 
155
        self.wt.lock_tree_write()
 
156
        self.addCleanup(self.wt.unlock)
98
157
        trans_id = transform.new_file('name', root, 'contents', 
99
158
                                      'my_pretties', True)
100
159
        oz = transform.new_directory('oz', root, 'oz-id')
112
171
        self.assertEqual(self.wt.path2id('oz/dorothy'), 'dorothy-id')
113
172
        self.assertEqual(self.wt.path2id('oz/dorothy/toto'), 'toto-id')
114
173
 
115
 
        self.assertEqual('toto-contents', 
 
174
        self.assertEqual('toto-contents',
116
175
                         self.wt.get_file_byname('oz/dorothy/toto').read())
117
176
        self.assertIs(self.wt.is_executable('toto-id'), False)
118
177
 
 
178
    def test_tree_reference(self):
 
179
        transform, root = self.get_transform()
 
180
        tree = transform._tree
 
181
        trans_id = transform.new_directory('reference', root, 'subtree-id')
 
182
        transform.set_tree_reference('subtree-revision', trans_id)
 
183
        transform.apply()
 
184
        tree.lock_read()
 
185
        self.addCleanup(tree.unlock)
 
186
        self.assertEqual('subtree-revision',
 
187
                         tree.inventory['subtree-id'].reference_revision)
 
188
 
119
189
    def test_conflicts(self):
120
190
        transform, root = self.get_transform()
121
191
        trans_id = transform.new_file('name', root, 'contents', 
185
255
        transform3.delete_contents(oz_id)
186
256
        self.assertEqual(transform3.find_conflicts(), 
187
257
                         [('missing parent', oz_id)])
188
 
        root_id = transform3.trans_id_tree_file_id('TREE_ROOT')
 
258
        root_id = transform3.root
189
259
        tip_id = transform3.trans_id_tree_file_id('tip-id')
190
260
        transform3.adjust_path('tip', root_id, tip_id)
191
261
        transform3.apply()
192
262
 
 
263
    def test_conflict_on_case_insensitive(self):
 
264
        tree = self.make_branch_and_tree('tree')
 
265
        # Don't try this at home, kids!
 
266
        # Force the tree to report that it is case sensitive, for conflict
 
267
        # resolution tests
 
268
        tree.case_sensitive = True
 
269
        transform = TreeTransform(tree)
 
270
        self.addCleanup(transform.finalize)
 
271
        transform.new_file('file', transform.root, 'content')
 
272
        transform.new_file('FiLe', transform.root, 'content')
 
273
        result = transform.find_conflicts()
 
274
        self.assertEqual([], result)
 
275
        transform.finalize()
 
276
        # Force the tree to report that it is case insensitive, for conflict
 
277
        # generation tests
 
278
        tree.case_sensitive = False
 
279
        transform = TreeTransform(tree)
 
280
        self.addCleanup(transform.finalize)
 
281
        transform.new_file('file', transform.root, 'content')
 
282
        transform.new_file('FiLe', transform.root, 'content')
 
283
        result = transform.find_conflicts()
 
284
        self.assertEqual([('duplicate', 'new-1', 'new-2', 'file')], result)
 
285
 
 
286
    def test_conflict_on_case_insensitive_existing(self):
 
287
        tree = self.make_branch_and_tree('tree')
 
288
        self.build_tree(['tree/FiLe'])
 
289
        # Don't try this at home, kids!
 
290
        # Force the tree to report that it is case sensitive, for conflict
 
291
        # resolution tests
 
292
        tree.case_sensitive = True
 
293
        transform = TreeTransform(tree)
 
294
        self.addCleanup(transform.finalize)
 
295
        transform.new_file('file', transform.root, 'content')
 
296
        result = transform.find_conflicts()
 
297
        self.assertEqual([], result)
 
298
        transform.finalize()
 
299
        # Force the tree to report that it is case insensitive, for conflict
 
300
        # generation tests
 
301
        tree.case_sensitive = False
 
302
        transform = TreeTransform(tree)
 
303
        self.addCleanup(transform.finalize)
 
304
        transform.new_file('file', transform.root, 'content')
 
305
        result = transform.find_conflicts()
 
306
        self.assertEqual([('duplicate', 'new-1', 'new-2', 'file')], result)
 
307
 
 
308
    def test_resolve_case_insensitive_conflict(self):
 
309
        tree = self.make_branch_and_tree('tree')
 
310
        # Don't try this at home, kids!
 
311
        # Force the tree to report that it is case insensitive, for conflict
 
312
        # resolution tests
 
313
        tree.case_sensitive = False
 
314
        transform = TreeTransform(tree)
 
315
        self.addCleanup(transform.finalize)
 
316
        transform.new_file('file', transform.root, 'content')
 
317
        transform.new_file('FiLe', transform.root, 'content')
 
318
        resolve_conflicts(transform)
 
319
        transform.apply()
 
320
        self.failUnlessExists('tree/file')
 
321
        self.failUnlessExists('tree/FiLe.moved')
 
322
 
 
323
    def test_resolve_checkout_case_conflict(self):
 
324
        tree = self.make_branch_and_tree('tree')
 
325
        # Don't try this at home, kids!
 
326
        # Force the tree to report that it is case insensitive, for conflict
 
327
        # resolution tests
 
328
        tree.case_sensitive = False
 
329
        transform = TreeTransform(tree)
 
330
        self.addCleanup(transform.finalize)
 
331
        transform.new_file('file', transform.root, 'content')
 
332
        transform.new_file('FiLe', transform.root, 'content')
 
333
        resolve_conflicts(transform,
 
334
                          pass_func=lambda t, c: resolve_checkout(t, c, []))
 
335
        transform.apply()
 
336
        self.failUnlessExists('tree/file')
 
337
        self.failUnlessExists('tree/FiLe.moved')
 
338
 
 
339
    def test_apply_case_conflict(self):
 
340
        """Ensure that a transform with case conflicts can always be applied"""
 
341
        tree = self.make_branch_and_tree('tree')
 
342
        transform = TreeTransform(tree)
 
343
        self.addCleanup(transform.finalize)
 
344
        transform.new_file('file', transform.root, 'content')
 
345
        transform.new_file('FiLe', transform.root, 'content')
 
346
        dir = transform.new_directory('dir', transform.root)
 
347
        transform.new_file('dirfile', dir, 'content')
 
348
        transform.new_file('dirFiLe', dir, 'content')
 
349
        resolve_conflicts(transform)
 
350
        transform.apply()
 
351
        self.failUnlessExists('tree/file')
 
352
        if not os.path.exists('tree/FiLe.moved'):
 
353
            self.failUnlessExists('tree/FiLe')
 
354
        self.failUnlessExists('tree/dir/dirfile')
 
355
        if not os.path.exists('tree/dir/dirFiLe.moved'):
 
356
            self.failUnlessExists('tree/dir/dirFiLe')
 
357
 
 
358
    def test_case_insensitive_limbo(self):
 
359
        tree = self.make_branch_and_tree('tree')
 
360
        # Don't try this at home, kids!
 
361
        # Force the tree to report that it is case insensitive
 
362
        tree.case_sensitive = False
 
363
        transform = TreeTransform(tree)
 
364
        self.addCleanup(transform.finalize)
 
365
        dir = transform.new_directory('dir', transform.root)
 
366
        first = transform.new_file('file', dir, 'content')
 
367
        second = transform.new_file('FiLe', dir, 'content')
 
368
        self.assertContainsRe(transform._limbo_name(first), 'new-1/file')
 
369
        self.assertNotContainsRe(transform._limbo_name(second), 'new-1/FiLe')
 
370
 
193
371
    def test_add_del(self):
194
372
        start, root = self.get_transform()
195
373
        start.new_directory('a', root, 'a')
217
395
    def test_name_invariants(self):
218
396
        create_tree, root = self.get_transform()
219
397
        # prepare tree
220
 
        root = create_tree.trans_id_tree_file_id('TREE_ROOT')
 
398
        root = create_tree.root
221
399
        create_tree.new_file('name1', root, 'hello1', 'name1')
222
400
        create_tree.new_file('name2', root, 'hello2', 'name2')
223
401
        ddir = create_tree.new_directory('dying_directory', root, 'ddir')
227
405
        create_tree.apply()
228
406
 
229
407
        mangle_tree,root = self.get_transform()
230
 
        root = mangle_tree.trans_id_tree_file_id('TREE_ROOT')
 
408
        root = mangle_tree.root
231
409
        #swap names
232
410
        name1 = mangle_tree.trans_id_tree_file_id('name1')
233
411
        name2 = mangle_tree.trans_id_tree_file_id('name2')
312
490
    def test_move_dangling_ie(self):
313
491
        create_tree, root = self.get_transform()
314
492
        # prepare tree
315
 
        root = create_tree.trans_id_tree_file_id('TREE_ROOT')
 
493
        root = create_tree.root
316
494
        create_tree.new_file('name1', root, 'hello1', 'name1')
317
495
        create_tree.apply()
318
496
        delete_contents, root = self.get_transform()
328
506
    def test_replace_dangling_ie(self):
329
507
        create_tree, root = self.get_transform()
330
508
        # prepare tree
331
 
        root = create_tree.trans_id_tree_file_id('TREE_ROOT')
 
509
        root = create_tree.root
332
510
        create_tree.new_file('name1', root, 'hello1', 'name1')
333
511
        create_tree.apply()
334
512
        delete_contents = TreeTransform(self.wt)
347
525
        replace.apply()
348
526
 
349
527
    def test_symlinks(self):
350
 
        if not has_symlinks():
351
 
            raise TestSkipped('Symlinks are not supported on this platform')
 
528
        self.requireFeature(SymlinkFeature)
352
529
        transform,root = self.get_transform()
353
530
        oz_id = transform.new_directory('oz', root, 'oz-id')
354
531
        wizard = transform.new_symlink('wizard', oz_id, 'wizard-target', 
368
545
        self.assertEqual(os.readlink(self.wt.abspath('oz/wizard')),
369
546
                         'wizard-target')
370
547
 
 
548
    def test_unable_create_symlink(self):
 
549
        def tt_helper():
 
550
            wt = self.make_branch_and_tree('.')
 
551
            tt = TreeTransform(wt)  # TreeTransform obtains write lock
 
552
            try:
 
553
                tt.new_symlink('foo', tt.root, 'bar')
 
554
                tt.apply()
 
555
            finally:
 
556
                wt.unlock()
 
557
        os_symlink = getattr(os, 'symlink', None)
 
558
        os.symlink = None
 
559
        try:
 
560
            err = self.assertRaises(errors.UnableCreateSymlink, tt_helper)
 
561
            self.assertEquals(
 
562
                "Unable to create symlink 'foo' on this platform",
 
563
                str(err))
 
564
        finally:
 
565
            if os_symlink:
 
566
                os.symlink = os_symlink
371
567
 
372
568
    def get_conflicted(self):
373
569
        create,root = self.get_transform()
381
577
                                         'dorothy-id')
382
578
        old_dorothy = conflicts.trans_id_tree_file_id('dorothy-id')
383
579
        oz = conflicts.trans_id_tree_file_id('oz-id')
384
 
        # set up missing, unversioned parent
 
580
        # set up DeletedParent parent conflict
385
581
        conflicts.delete_versioned(oz)
386
582
        emerald = conflicts.trans_id_tree_file_id('emerald-id')
 
583
        # set up MissingParent conflict
 
584
        munchkincity = conflicts.trans_id_file_id('munchkincity-id')
 
585
        conflicts.adjust_path('munchkincity', root, munchkincity)
 
586
        conflicts.new_directory('auntem', munchkincity, 'auntem-id')
387
587
        # set up parent loop
388
588
        conflicts.adjust_path('emeraldcity', emerald, emerald)
389
589
        return conflicts, emerald, oz, old_dorothy, new_dorothy
410
610
                                   'dorothy.moved', 'dorothy', None,
411
611
                                   'dorothy-id')
412
612
        self.assertEqual(cooked_conflicts[1], duplicate_id)
413
 
        missing_parent = MissingParent('Not deleting', 'oz', 'oz-id')
 
613
        missing_parent = MissingParent('Created directory', 'munchkincity',
 
614
                                       'munchkincity-id')
 
615
        deleted_parent = DeletingParent('Not deleting', 'oz', 'oz-id')
414
616
        self.assertEqual(cooked_conflicts[2], missing_parent)
415
 
        unversioned_parent = UnversionedParent('Versioned directory', 'oz',
 
617
        unversioned_parent = UnversionedParent('Versioned directory',
 
618
                                               'munchkincity',
 
619
                                               'munchkincity-id')
 
620
        unversioned_parent2 = UnversionedParent('Versioned directory', 'oz',
416
621
                                               'oz-id')
417
622
        self.assertEqual(cooked_conflicts[3], unversioned_parent)
418
623
        parent_loop = ParentLoop('Cancelled move', 'oz/emeraldcity', 
419
624
                                 'oz/emeraldcity', 'emerald-id', 'emerald-id')
420
 
        self.assertEqual(cooked_conflicts[4], parent_loop)
421
 
        self.assertEqual(len(cooked_conflicts), 5)
 
625
        self.assertEqual(cooked_conflicts[4], deleted_parent)
 
626
        self.assertEqual(cooked_conflicts[5], unversioned_parent2)
 
627
        self.assertEqual(cooked_conflicts[6], parent_loop)
 
628
        self.assertEqual(len(cooked_conflicts), 7)
422
629
        tt.finalize()
423
630
 
424
631
    def test_string_conflicts(self):
434
641
        self.assertEqual(conflicts_s[1], 'Conflict adding id to dorothy.  '
435
642
                                         'Unversioned existing file '
436
643
                                         'dorothy.moved.')
437
 
        self.assertEqual(conflicts_s[2], 'Conflict adding files to oz.  '
438
 
                                         'Not deleting.')
439
 
        self.assertEqual(conflicts_s[3], 'Conflict adding versioned files to '
440
 
                                         'oz.  Versioned directory.')
441
 
        self.assertEqual(conflicts_s[4], 'Conflict moving oz/emeraldcity into'
 
644
        self.assertEqual(conflicts_s[2], 'Conflict adding files to'
 
645
                                         ' munchkincity.  Created directory.')
 
646
        self.assertEqual(conflicts_s[3], 'Conflict because munchkincity is not'
 
647
                                         ' versioned, but has versioned'
 
648
                                         ' children.  Versioned directory.')
 
649
        self.assertEqualDiff(conflicts_s[4], "Conflict: can't delete oz because it"
 
650
                                         " is not empty.  Not deleting.")
 
651
        self.assertEqual(conflicts_s[5], 'Conflict because oz is not'
 
652
                                         ' versioned, but has versioned'
 
653
                                         ' children.  Versioned directory.')
 
654
        self.assertEqual(conflicts_s[6], 'Conflict moving oz/emeraldcity into'
442
655
                                         ' oz/emeraldcity.  Cancelled move.')
443
656
 
 
657
    def prepare_wrong_parent_kind(self):
 
658
        tt, root = self.get_transform()
 
659
        tt.new_file('parent', root, 'contents', 'parent-id')
 
660
        tt.apply()
 
661
        tt, root = self.get_transform()
 
662
        parent_id = tt.trans_id_file_id('parent-id')
 
663
        tt.new_file('child,', parent_id, 'contents2', 'file-id')
 
664
        return tt
 
665
 
 
666
    def test_find_conflicts_wrong_parent_kind(self):
 
667
        tt = self.prepare_wrong_parent_kind()
 
668
        tt.find_conflicts()
 
669
 
 
670
    def test_resolve_conflicts_wrong_existing_parent_kind(self):
 
671
        tt = self.prepare_wrong_parent_kind()
 
672
        raw_conflicts = resolve_conflicts(tt)
 
673
        self.assertEqual(set([('non-directory parent', 'Created directory',
 
674
                         'new-3')]), raw_conflicts)
 
675
        cooked_conflicts = cook_conflicts(raw_conflicts, tt)
 
676
        self.assertEqual([NonDirectoryParent('Created directory', 'parent.new',
 
677
        'parent-id')], cooked_conflicts)
 
678
        tt.apply()
 
679
        self.assertEqual(None, self.wt.path2id('parent'))
 
680
        self.assertEqual('parent-id', self.wt.path2id('parent.new'))
 
681
 
 
682
    def test_resolve_conflicts_wrong_new_parent_kind(self):
 
683
        tt, root = self.get_transform()
 
684
        parent_id = tt.new_directory('parent', root, 'parent-id')
 
685
        tt.new_file('child,', parent_id, 'contents2', 'file-id')
 
686
        tt.apply()
 
687
        tt, root = self.get_transform()
 
688
        parent_id = tt.trans_id_file_id('parent-id')
 
689
        tt.delete_contents(parent_id)
 
690
        tt.create_file('contents', parent_id)
 
691
        raw_conflicts = resolve_conflicts(tt)
 
692
        self.assertEqual(set([('non-directory parent', 'Created directory',
 
693
                         'new-3')]), raw_conflicts)
 
694
        tt.apply()
 
695
        self.assertEqual(None, self.wt.path2id('parent'))
 
696
        self.assertEqual('parent-id', self.wt.path2id('parent.new'))
 
697
 
 
698
    def test_resolve_conflicts_wrong_parent_kind_unversioned(self):
 
699
        tt, root = self.get_transform()
 
700
        parent_id = tt.new_directory('parent', root)
 
701
        tt.new_file('child,', parent_id, 'contents2')
 
702
        tt.apply()
 
703
        tt, root = self.get_transform()
 
704
        parent_id = tt.trans_id_tree_path('parent')
 
705
        tt.delete_contents(parent_id)
 
706
        tt.create_file('contents', parent_id)
 
707
        resolve_conflicts(tt)
 
708
        tt.apply()
 
709
        self.assertIs(None, self.wt.path2id('parent'))
 
710
        self.assertIs(None, self.wt.path2id('parent.new'))
 
711
 
444
712
    def test_moving_versioned_directories(self):
445
713
        create, root = self.get_transform()
446
714
        kansas = create.new_directory('kansas', root, 'kansas-id')
479
747
        rename.set_executability(True, myfile)
480
748
        rename.apply()
481
749
 
482
 
    def test_find_interesting(self):
483
 
        create, root = self.get_transform()
484
 
        wt = create._tree
485
 
        create.new_file('vfile', root, 'myfile-text', 'myfile-id')
486
 
        create.new_file('uvfile', root, 'othertext')
487
 
        create.apply()
488
 
        self.assertEqual(find_interesting(wt, wt, ['vfile']),
489
 
                         set(['myfile-id']))
490
 
        self.assertRaises(NotVersionedError, find_interesting, wt, wt,
491
 
                          ['uvfile'])
 
750
    def test_set_executability_order(self):
 
751
        """Ensure that executability behaves the same, no matter what order.
 
752
        
 
753
        - create file and set executability simultaneously
 
754
        - create file and set executability afterward
 
755
        - unsetting the executability of a file whose executability has not been
 
756
        declared should throw an exception (this may happen when a
 
757
        merge attempts to create a file with a duplicate ID)
 
758
        """
 
759
        transform, root = self.get_transform()
 
760
        wt = transform._tree
 
761
        wt.lock_read()
 
762
        self.addCleanup(wt.unlock)
 
763
        transform.new_file('set_on_creation', root, 'Set on creation', 'soc',
 
764
                           True)
 
765
        sac = transform.new_file('set_after_creation', root,
 
766
                                 'Set after creation', 'sac')
 
767
        transform.set_executability(True, sac)
 
768
        uws = transform.new_file('unset_without_set', root, 'Unset badly',
 
769
                                 'uws')
 
770
        self.assertRaises(KeyError, transform.set_executability, None, uws)
 
771
        transform.apply()
 
772
        self.assertTrue(wt.is_executable('soc'))
 
773
        self.assertTrue(wt.is_executable('sac'))
 
774
 
 
775
    def test_preserve_mode(self):
 
776
        """File mode is preserved when replacing content"""
 
777
        if sys.platform == 'win32':
 
778
            raise TestSkipped('chmod has no effect on win32')
 
779
        transform, root = self.get_transform()
 
780
        transform.new_file('file1', root, 'contents', 'file1-id', True)
 
781
        transform.apply()
 
782
        self.wt.lock_write()
 
783
        self.addCleanup(self.wt.unlock)
 
784
        self.assertTrue(self.wt.is_executable('file1-id'))
 
785
        transform, root = self.get_transform()
 
786
        file1_id = transform.trans_id_tree_file_id('file1-id')
 
787
        transform.delete_contents(file1_id)
 
788
        transform.create_file('contents2', file1_id)
 
789
        transform.apply()
 
790
        self.assertTrue(self.wt.is_executable('file1-id'))
 
791
 
 
792
    def test__set_mode_stats_correctly(self):
 
793
        """_set_mode stats to determine file mode."""
 
794
        if sys.platform == 'win32':
 
795
            raise TestSkipped('chmod has no effect on win32')
 
796
 
 
797
        stat_paths = []
 
798
        real_stat = os.stat
 
799
        def instrumented_stat(path):
 
800
            stat_paths.append(path)
 
801
            return real_stat(path)
 
802
 
 
803
        transform, root = self.get_transform()
 
804
 
 
805
        bar1_id = transform.new_file('bar', root, 'bar contents 1\n',
 
806
                                     file_id='bar-id-1', executable=False)
 
807
        transform.apply()
 
808
 
 
809
        transform, root = self.get_transform()
 
810
        bar1_id = transform.trans_id_tree_path('bar')
 
811
        bar2_id = transform.trans_id_tree_path('bar2')
 
812
        try:
 
813
            os.stat = instrumented_stat
 
814
            transform.create_file('bar2 contents\n', bar2_id, mode_id=bar1_id)
 
815
        finally:
 
816
            os.stat = real_stat
 
817
            transform.finalize()
 
818
 
 
819
        bar1_abspath = self.wt.abspath('bar')
 
820
        self.assertEqual([bar1_abspath], stat_paths)
 
821
 
 
822
    def test_iter_changes(self):
 
823
        self.wt.set_root_id('eert_toor')
 
824
        transform, root = self.get_transform()
 
825
        transform.new_file('old', root, 'blah', 'id-1', True)
 
826
        transform.apply()
 
827
        transform, root = self.get_transform()
 
828
        try:
 
829
            self.assertEqual([], list(transform.iter_changes()))
 
830
            old = transform.trans_id_tree_file_id('id-1')
 
831
            transform.unversion_file(old)
 
832
            self.assertEqual([('id-1', ('old', None), False, (True, False),
 
833
                ('eert_toor', 'eert_toor'), ('old', 'old'), ('file', 'file'),
 
834
                (True, True))], list(transform.iter_changes()))
 
835
            transform.new_directory('new', root, 'id-1')
 
836
            self.assertEqual([('id-1', ('old', 'new'), True, (True, True),
 
837
                ('eert_toor', 'eert_toor'), ('old', 'new'),
 
838
                ('file', 'directory'),
 
839
                (True, False))], list(transform.iter_changes()))
 
840
        finally:
 
841
            transform.finalize()
 
842
 
 
843
    def test_iter_changes_new(self):
 
844
        self.wt.set_root_id('eert_toor')
 
845
        transform, root = self.get_transform()
 
846
        transform.new_file('old', root, 'blah')
 
847
        transform.apply()
 
848
        transform, root = self.get_transform()
 
849
        try:
 
850
            old = transform.trans_id_tree_path('old')
 
851
            transform.version_file('id-1', old)
 
852
            self.assertEqual([('id-1', (None, 'old'), False, (False, True),
 
853
                ('eert_toor', 'eert_toor'), ('old', 'old'), ('file', 'file'),
 
854
                (False, False))], list(transform.iter_changes()))
 
855
        finally:
 
856
            transform.finalize()
 
857
 
 
858
    def test_iter_changes_modifications(self):
 
859
        self.wt.set_root_id('eert_toor')
 
860
        transform, root = self.get_transform()
 
861
        transform.new_file('old', root, 'blah', 'id-1')
 
862
        transform.new_file('new', root, 'blah')
 
863
        transform.new_directory('subdir', root, 'subdir-id')
 
864
        transform.apply()
 
865
        transform, root = self.get_transform()
 
866
        try:
 
867
            old = transform.trans_id_tree_path('old')
 
868
            subdir = transform.trans_id_tree_file_id('subdir-id')
 
869
            new = transform.trans_id_tree_path('new')
 
870
            self.assertEqual([], list(transform.iter_changes()))
 
871
 
 
872
            #content deletion
 
873
            transform.delete_contents(old)
 
874
            self.assertEqual([('id-1', ('old', 'old'), True, (True, True),
 
875
                ('eert_toor', 'eert_toor'), ('old', 'old'), ('file', None),
 
876
                (False, False))], list(transform.iter_changes()))
 
877
 
 
878
            #content change
 
879
            transform.create_file('blah', old)
 
880
            self.assertEqual([('id-1', ('old', 'old'), True, (True, True),
 
881
                ('eert_toor', 'eert_toor'), ('old', 'old'), ('file', 'file'),
 
882
                (False, False))], list(transform.iter_changes()))
 
883
            transform.cancel_deletion(old)
 
884
            self.assertEqual([('id-1', ('old', 'old'), True, (True, True),
 
885
                ('eert_toor', 'eert_toor'), ('old', 'old'), ('file', 'file'),
 
886
                (False, False))], list(transform.iter_changes()))
 
887
            transform.cancel_creation(old)
 
888
 
 
889
            # move file_id to a different file
 
890
            self.assertEqual([], list(transform.iter_changes()))
 
891
            transform.unversion_file(old)
 
892
            transform.version_file('id-1', new)
 
893
            transform.adjust_path('old', root, new)
 
894
            self.assertEqual([('id-1', ('old', 'old'), True, (True, True),
 
895
                ('eert_toor', 'eert_toor'), ('old', 'old'), ('file', 'file'),
 
896
                (False, False))], list(transform.iter_changes()))
 
897
            transform.cancel_versioning(new)
 
898
            transform._removed_id = set()
 
899
 
 
900
            #execute bit
 
901
            self.assertEqual([], list(transform.iter_changes()))
 
902
            transform.set_executability(True, old)
 
903
            self.assertEqual([('id-1', ('old', 'old'), False, (True, True),
 
904
                ('eert_toor', 'eert_toor'), ('old', 'old'), ('file', 'file'),
 
905
                (False, True))], list(transform.iter_changes()))
 
906
            transform.set_executability(None, old)
 
907
 
 
908
            # filename
 
909
            self.assertEqual([], list(transform.iter_changes()))
 
910
            transform.adjust_path('new', root, old)
 
911
            transform._new_parent = {}
 
912
            self.assertEqual([('id-1', ('old', 'new'), False, (True, True),
 
913
                ('eert_toor', 'eert_toor'), ('old', 'new'), ('file', 'file'),
 
914
                (False, False))], list(transform.iter_changes()))
 
915
            transform._new_name = {}
 
916
 
 
917
            # parent directory
 
918
            self.assertEqual([], list(transform.iter_changes()))
 
919
            transform.adjust_path('new', subdir, old)
 
920
            transform._new_name = {}
 
921
            self.assertEqual([('id-1', ('old', 'subdir/old'), False,
 
922
                (True, True), ('eert_toor', 'subdir-id'), ('old', 'old'),
 
923
                ('file', 'file'), (False, False))],
 
924
                list(transform.iter_changes()))
 
925
            transform._new_path = {}
 
926
 
 
927
        finally:
 
928
            transform.finalize()
 
929
 
 
930
    def test_iter_changes_modified_bleed(self):
 
931
        self.wt.set_root_id('eert_toor')
 
932
        """Modified flag should not bleed from one change to another"""
 
933
        # unfortunately, we have no guarantee that file1 (which is modified)
 
934
        # will be applied before file2.  And if it's applied after file2, it
 
935
        # obviously can't bleed into file2's change output.  But for now, it
 
936
        # works.
 
937
        transform, root = self.get_transform()
 
938
        transform.new_file('file1', root, 'blah', 'id-1')
 
939
        transform.new_file('file2', root, 'blah', 'id-2')
 
940
        transform.apply()
 
941
        transform, root = self.get_transform()
 
942
        try:
 
943
            transform.delete_contents(transform.trans_id_file_id('id-1'))
 
944
            transform.set_executability(True,
 
945
            transform.trans_id_file_id('id-2'))
 
946
            self.assertEqual([('id-1', (u'file1', u'file1'), True, (True, True),
 
947
                ('eert_toor', 'eert_toor'), ('file1', u'file1'),
 
948
                ('file', None), (False, False)),
 
949
                ('id-2', (u'file2', u'file2'), False, (True, True),
 
950
                ('eert_toor', 'eert_toor'), ('file2', u'file2'),
 
951
                ('file', 'file'), (False, True))],
 
952
                list(transform.iter_changes()))
 
953
        finally:
 
954
            transform.finalize()
 
955
 
 
956
    def test_iter_changes_move_missing(self):
 
957
        """Test moving ids with no files around"""
 
958
        self.wt.set_root_id('toor_eert')
 
959
        # Need two steps because versioning a non-existant file is a conflict.
 
960
        transform, root = self.get_transform()
 
961
        transform.new_directory('floater', root, 'floater-id')
 
962
        transform.apply()
 
963
        transform, root = self.get_transform()
 
964
        transform.delete_contents(transform.trans_id_tree_path('floater'))
 
965
        transform.apply()
 
966
        transform, root = self.get_transform()
 
967
        floater = transform.trans_id_tree_path('floater')
 
968
        try:
 
969
            transform.adjust_path('flitter', root, floater)
 
970
            self.assertEqual([('floater-id', ('floater', 'flitter'), False,
 
971
            (True, True), ('toor_eert', 'toor_eert'), ('floater', 'flitter'),
 
972
            (None, None), (False, False))], list(transform.iter_changes()))
 
973
        finally:
 
974
            transform.finalize()
 
975
 
 
976
    def test_iter_changes_pointless(self):
 
977
        """Ensure that no-ops are not treated as modifications"""
 
978
        self.wt.set_root_id('eert_toor')
 
979
        transform, root = self.get_transform()
 
980
        transform.new_file('old', root, 'blah', 'id-1')
 
981
        transform.new_directory('subdir', root, 'subdir-id')
 
982
        transform.apply()
 
983
        transform, root = self.get_transform()
 
984
        try:
 
985
            old = transform.trans_id_tree_path('old')
 
986
            subdir = transform.trans_id_tree_file_id('subdir-id')
 
987
            self.assertEqual([], list(transform.iter_changes()))
 
988
            transform.delete_contents(subdir)
 
989
            transform.create_directory(subdir)
 
990
            transform.set_executability(False, old)
 
991
            transform.unversion_file(old)
 
992
            transform.version_file('id-1', old)
 
993
            transform.adjust_path('old', root, old)
 
994
            self.assertEqual([], list(transform.iter_changes()))
 
995
        finally:
 
996
            transform.finalize()
 
997
 
 
998
    def test_rename_count(self):
 
999
        transform, root = self.get_transform()
 
1000
        transform.new_file('name1', root, 'contents')
 
1001
        self.assertEqual(transform.rename_count, 0)
 
1002
        transform.apply()
 
1003
        self.assertEqual(transform.rename_count, 1)
 
1004
        transform2, root = self.get_transform()
 
1005
        transform2.adjust_path('name2', root,
 
1006
                               transform2.trans_id_tree_path('name1'))
 
1007
        self.assertEqual(transform2.rename_count, 0)
 
1008
        transform2.apply()
 
1009
        self.assertEqual(transform2.rename_count, 2)
 
1010
 
 
1011
    def test_change_parent(self):
 
1012
        """Ensure that after we change a parent, the results are still right.
 
1013
 
 
1014
        Renames and parent changes on pending transforms can happen as part
 
1015
        of conflict resolution, and are explicitly permitted by the
 
1016
        TreeTransform API.
 
1017
 
 
1018
        This test ensures they work correctly with the rename-avoidance
 
1019
        optimization.
 
1020
        """
 
1021
        transform, root = self.get_transform()
 
1022
        parent1 = transform.new_directory('parent1', root)
 
1023
        child1 = transform.new_file('child1', parent1, 'contents')
 
1024
        parent2 = transform.new_directory('parent2', root)
 
1025
        transform.adjust_path('child1', parent2, child1)
 
1026
        transform.apply()
 
1027
        self.failIfExists(self.wt.abspath('parent1/child1'))
 
1028
        self.failUnlessExists(self.wt.abspath('parent2/child1'))
 
1029
        # rename limbo/new-1 => parent1, rename limbo/new-3 => parent2
 
1030
        # no rename for child1 (counting only renames during apply)
 
1031
        self.failUnlessEqual(2, transform.rename_count)
 
1032
 
 
1033
    def test_cancel_parent(self):
 
1034
        """Cancelling a parent doesn't cause deletion of a non-empty directory
 
1035
 
 
1036
        This is like the test_change_parent, except that we cancel the parent
 
1037
        before adjusting the path.  The transform must detect that the
 
1038
        directory is non-empty, and move children to safe locations.
 
1039
        """
 
1040
        transform, root = self.get_transform()
 
1041
        parent1 = transform.new_directory('parent1', root)
 
1042
        child1 = transform.new_file('child1', parent1, 'contents')
 
1043
        child2 = transform.new_file('child2', parent1, 'contents')
 
1044
        try:
 
1045
            transform.cancel_creation(parent1)
 
1046
        except OSError:
 
1047
            self.fail('Failed to move child1 before deleting parent1')
 
1048
        transform.cancel_creation(child2)
 
1049
        transform.create_directory(parent1)
 
1050
        try:
 
1051
            transform.cancel_creation(parent1)
 
1052
        # If the transform incorrectly believes that child2 is still in
 
1053
        # parent1's limbo directory, it will try to rename it and fail
 
1054
        # because was already moved by the first cancel_creation.
 
1055
        except OSError:
 
1056
            self.fail('Transform still thinks child2 is a child of parent1')
 
1057
        parent2 = transform.new_directory('parent2', root)
 
1058
        transform.adjust_path('child1', parent2, child1)
 
1059
        transform.apply()
 
1060
        self.failIfExists(self.wt.abspath('parent1'))
 
1061
        self.failUnlessExists(self.wt.abspath('parent2/child1'))
 
1062
        # rename limbo/new-3 => parent2, rename limbo/new-2 => child1
 
1063
        self.failUnlessEqual(2, transform.rename_count)
 
1064
 
 
1065
    def test_adjust_and_cancel(self):
 
1066
        """Make sure adjust_path keeps track of limbo children properly"""
 
1067
        transform, root = self.get_transform()
 
1068
        parent1 = transform.new_directory('parent1', root)
 
1069
        child1 = transform.new_file('child1', parent1, 'contents')
 
1070
        parent2 = transform.new_directory('parent2', root)
 
1071
        transform.adjust_path('child1', parent2, child1)
 
1072
        transform.cancel_creation(child1)
 
1073
        try:
 
1074
            transform.cancel_creation(parent1)
 
1075
        # if the transform thinks child1 is still in parent1's limbo
 
1076
        # directory, it will attempt to move it and fail.
 
1077
        except OSError:
 
1078
            self.fail('Transform still thinks child1 is a child of parent1')
 
1079
        transform.finalize()
 
1080
 
 
1081
    def test_noname_contents(self):
 
1082
        """TreeTransform should permit deferring naming files."""
 
1083
        transform, root = self.get_transform()
 
1084
        parent = transform.trans_id_file_id('parent-id')
 
1085
        try:
 
1086
            transform.create_directory(parent)
 
1087
        except KeyError:
 
1088
            self.fail("Can't handle contents with no name")
 
1089
        transform.finalize()
 
1090
 
 
1091
    def test_noname_contents_nested(self):
 
1092
        """TreeTransform should permit deferring naming files."""
 
1093
        transform, root = self.get_transform()
 
1094
        parent = transform.trans_id_file_id('parent-id')
 
1095
        try:
 
1096
            transform.create_directory(parent)
 
1097
        except KeyError:
 
1098
            self.fail("Can't handle contents with no name")
 
1099
        child = transform.new_directory('child', parent)
 
1100
        transform.adjust_path('parent', root, parent)
 
1101
        transform.apply()
 
1102
        self.failUnlessExists(self.wt.abspath('parent/child'))
 
1103
        self.assertEqual(1, transform.rename_count)
 
1104
 
 
1105
    def test_reuse_name(self):
 
1106
        """Avoid reusing the same limbo name for different files"""
 
1107
        transform, root = self.get_transform()
 
1108
        parent = transform.new_directory('parent', root)
 
1109
        child1 = transform.new_directory('child', parent)
 
1110
        try:
 
1111
            child2 = transform.new_directory('child', parent)
 
1112
        except OSError:
 
1113
            self.fail('Tranform tried to use the same limbo name twice')
 
1114
        transform.adjust_path('child2', parent, child2)
 
1115
        transform.apply()
 
1116
        # limbo/new-1 => parent, limbo/new-3 => parent/child2
 
1117
        # child2 is put into top-level limbo because child1 has already
 
1118
        # claimed the direct limbo path when child2 is created.  There is no
 
1119
        # advantage in renaming files once they're in top-level limbo, except
 
1120
        # as part of apply.
 
1121
        self.assertEqual(2, transform.rename_count)
 
1122
 
 
1123
    def test_reuse_when_first_moved(self):
 
1124
        """Don't avoid direct paths when it is safe to use them"""
 
1125
        transform, root = self.get_transform()
 
1126
        parent = transform.new_directory('parent', root)
 
1127
        child1 = transform.new_directory('child', parent)
 
1128
        transform.adjust_path('child1', parent, child1)
 
1129
        child2 = transform.new_directory('child', parent)
 
1130
        transform.apply()
 
1131
        # limbo/new-1 => parent
 
1132
        self.assertEqual(1, transform.rename_count)
 
1133
 
 
1134
    def test_reuse_after_cancel(self):
 
1135
        """Don't avoid direct paths when it is safe to use them"""
 
1136
        transform, root = self.get_transform()
 
1137
        parent2 = transform.new_directory('parent2', root)
 
1138
        child1 = transform.new_directory('child1', parent2)
 
1139
        transform.cancel_creation(parent2)
 
1140
        transform.create_directory(parent2)
 
1141
        child2 = transform.new_directory('child1', parent2)
 
1142
        transform.adjust_path('child2', parent2, child1)
 
1143
        transform.apply()
 
1144
        # limbo/new-1 => parent2, limbo/new-2 => parent2/child1
 
1145
        self.assertEqual(2, transform.rename_count)
 
1146
 
 
1147
    def test_finalize_order(self):
 
1148
        """Finalize must be done in child-to-parent order"""
 
1149
        transform, root = self.get_transform()
 
1150
        parent = transform.new_directory('parent', root)
 
1151
        child = transform.new_directory('child', parent)
 
1152
        try:
 
1153
            transform.finalize()
 
1154
        except OSError:
 
1155
            self.fail('Tried to remove parent before child1')
 
1156
 
 
1157
    def test_cancel_with_cancelled_child_should_succeed(self):
 
1158
        transform, root = self.get_transform()
 
1159
        parent = transform.new_directory('parent', root)
 
1160
        child = transform.new_directory('child', parent)
 
1161
        transform.cancel_creation(child)
 
1162
        transform.cancel_creation(parent)
 
1163
        transform.finalize()
 
1164
 
 
1165
    def test_case_insensitive_clash(self):
 
1166
        self.requireFeature(CaseInsensitiveFilesystemFeature)
 
1167
        def tt_helper():
 
1168
            wt = self.make_branch_and_tree('.')
 
1169
            tt = TreeTransform(wt)  # TreeTransform obtains write lock
 
1170
            try:
 
1171
                tt.new_file('foo', tt.root, 'bar')
 
1172
                tt.new_file('Foo', tt.root, 'spam')
 
1173
                # Lie to tt that we've already resolved all conflicts.
 
1174
                tt.apply(no_conflicts=True)
 
1175
            except:
 
1176
                wt.unlock()
 
1177
                raise
 
1178
        err = self.assertRaises(errors.FileExists, tt_helper)
 
1179
        self.assertContainsRe(str(err),
 
1180
            "^File exists: .+/foo")
 
1181
 
 
1182
    def test_two_directories_clash(self):
 
1183
        def tt_helper():
 
1184
            wt = self.make_branch_and_tree('.')
 
1185
            tt = TreeTransform(wt)  # TreeTransform obtains write lock
 
1186
            try:
 
1187
                foo_1 = tt.new_directory('foo', tt.root)
 
1188
                tt.new_directory('bar', foo_1)
 
1189
                foo_2 = tt.new_directory('foo', tt.root)
 
1190
                tt.new_directory('baz', foo_2)
 
1191
                # Lie to tt that we've already resolved all conflicts.
 
1192
                tt.apply(no_conflicts=True)
 
1193
            except:
 
1194
                wt.unlock()
 
1195
                raise
 
1196
        err = self.assertRaises(errors.FileExists, tt_helper)
 
1197
        self.assertContainsRe(str(err),
 
1198
            "^File exists: .+/foo")
 
1199
 
 
1200
    def test_two_directories_clash_finalize(self):
 
1201
        def tt_helper():
 
1202
            wt = self.make_branch_and_tree('.')
 
1203
            tt = TreeTransform(wt)  # TreeTransform obtains write lock
 
1204
            try:
 
1205
                foo_1 = tt.new_directory('foo', tt.root)
 
1206
                tt.new_directory('bar', foo_1)
 
1207
                foo_2 = tt.new_directory('foo', tt.root)
 
1208
                tt.new_directory('baz', foo_2)
 
1209
                # Lie to tt that we've already resolved all conflicts.
 
1210
                tt.apply(no_conflicts=True)
 
1211
            except:
 
1212
                tt.finalize()
 
1213
                raise
 
1214
        err = self.assertRaises(errors.FileExists, tt_helper)
 
1215
        self.assertContainsRe(str(err),
 
1216
            "^File exists: .+/foo")
492
1217
 
493
1218
 
494
1219
class TransformGroup(object):
495
 
    def __init__(self, dirname):
 
1220
 
 
1221
    def __init__(self, dirname, root_id):
496
1222
        self.name = dirname
497
1223
        os.mkdir(dirname)
498
1224
        self.wt = BzrDir.create_standalone_workingtree(dirname)
 
1225
        self.wt.set_root_id(root_id)
499
1226
        self.b = self.wt.branch
500
1227
        self.tt = TreeTransform(self.wt)
501
1228
        self.root = self.tt.trans_id_tree_file_id(self.wt.get_root_id())
502
1229
 
 
1230
 
503
1231
def conflict_text(tree, merge):
504
1232
    template = '%s TREE\n%s%s\n%s%s MERGE-SOURCE\n'
505
1233
    return template % ('<' * 7, tree, '=' * 7, merge, '>' * 7)
506
1234
 
507
1235
 
508
1236
class TestTransformMerge(TestCaseInTempDir):
 
1237
 
509
1238
    def test_text_merge(self):
510
 
        base = TransformGroup("base")
 
1239
        root_id = generate_ids.gen_root_id()
 
1240
        base = TransformGroup("base", root_id)
511
1241
        base.tt.new_file('a', base.root, 'a\nb\nc\nd\be\n', 'a')
512
1242
        base.tt.new_file('b', base.root, 'b1', 'b')
513
1243
        base.tt.new_file('c', base.root, 'c', 'c')
517
1247
        base.tt.new_directory('g', base.root, 'g')
518
1248
        base.tt.new_directory('h', base.root, 'h')
519
1249
        base.tt.apply()
520
 
        other = TransformGroup("other")
 
1250
        other = TransformGroup("other", root_id)
521
1251
        other.tt.new_file('a', other.root, 'y\nb\nc\nd\be\n', 'a')
522
1252
        other.tt.new_file('b', other.root, 'b2', 'b')
523
1253
        other.tt.new_file('c', other.root, 'c2', 'c')
528
1258
        other.tt.new_file('h', other.root, 'h\ni\nj\nk\n', 'h')
529
1259
        other.tt.new_file('i', other.root, 'h\ni\nj\nk\n', 'i')
530
1260
        other.tt.apply()
531
 
        this = TransformGroup("this")
 
1261
        this = TransformGroup("this", root_id)
532
1262
        this.tt.new_file('a', this.root, 'a\nb\nc\nd\bz\n', 'a')
533
1263
        this.tt.new_file('b', this.root, 'b', 'b')
534
1264
        this.tt.new_file('c', this.root, 'c', 'c')
540
1270
        this.tt.new_file('i', this.root, '1\n2\n3\n4\n', 'i')
541
1271
        this.tt.apply()
542
1272
        Merge3Merger(this.wt, this.wt, base.wt, other.wt)
 
1273
 
543
1274
        # textual merge
544
1275
        self.assertEqual(this.wt.get_file('a').read(), 'y\nb\nc\nd\bz\n')
545
1276
        # three-way text conflict
580
1311
        self.assertSubset(merge_modified, modified)
581
1312
        self.assertEqual(len(merge_modified), len(modified))
582
1313
        this.wt.remove('b')
583
 
        this.wt.revert([])
 
1314
        this.wt.revert()
584
1315
 
585
1316
    def test_file_merge(self):
586
 
        if not has_symlinks():
587
 
            raise TestSkipped('Symlinks are not supported on this platform')
588
 
        base = TransformGroup("BASE")
589
 
        this = TransformGroup("THIS")
590
 
        other = TransformGroup("OTHER")
 
1317
        self.requireFeature(SymlinkFeature)
 
1318
        root_id = generate_ids.gen_root_id()
 
1319
        base = TransformGroup("BASE", root_id)
 
1320
        this = TransformGroup("THIS", root_id)
 
1321
        other = TransformGroup("OTHER", root_id)
591
1322
        for tg in this, base, other:
592
1323
            tg.tt.new_directory('a', tg.root, 'a')
593
1324
            tg.tt.new_symlink('b', tg.root, 'b', 'b')
625
1356
        self.assertIs(os.path.lexists(this.wt.abspath('h.OTHER')), True)
626
1357
 
627
1358
    def test_filename_merge(self):
628
 
        base = TransformGroup("BASE")
629
 
        this = TransformGroup("THIS")
630
 
        other = TransformGroup("OTHER")
 
1359
        root_id = generate_ids.gen_root_id()
 
1360
        base = TransformGroup("BASE", root_id)
 
1361
        this = TransformGroup("THIS", root_id)
 
1362
        other = TransformGroup("OTHER", root_id)
631
1363
        base_a, this_a, other_a = [t.tt.new_directory('a', t.root, 'a') 
632
1364
                                   for t in [base, this, other]]
633
1365
        base_b, this_b, other_b = [t.tt.new_directory('b', t.root, 'b') 
657
1389
        self.assertEqual(this.wt.id2path('f'), pathjoin('b/f1'))
658
1390
 
659
1391
    def test_filename_merge_conflicts(self):
660
 
        base = TransformGroup("BASE")
661
 
        this = TransformGroup("THIS")
662
 
        other = TransformGroup("OTHER")
 
1392
        root_id = generate_ids.gen_root_id()
 
1393
        base = TransformGroup("BASE", root_id)
 
1394
        this = TransformGroup("THIS", root_id)
 
1395
        other = TransformGroup("OTHER", root_id)
663
1396
        base_a, this_a, other_a = [t.tt.new_directory('a', t.root, 'a') 
664
1397
                                   for t in [base, this, other]]
665
1398
        base_b, this_b, other_b = [t.tt.new_directory('b', t.root, 'b') 
686
1419
        self.assertIs(os.path.lexists(this.wt.abspath('b/h1.OTHER')), False)
687
1420
        self.assertEqual(this.wt.id2path('i'), pathjoin('b/i1.OTHER'))
688
1421
 
689
 
class TestBuildTree(TestCaseInTempDir):
690
 
    def test_build_tree(self):
691
 
        if not has_symlinks():
692
 
            raise TestSkipped('Test requires symlink support')
 
1422
 
 
1423
class TestBuildTree(tests.TestCaseWithTransport):
 
1424
 
 
1425
    def test_build_tree_with_symlinks(self):
 
1426
        self.requireFeature(SymlinkFeature)
693
1427
        os.mkdir('a')
694
1428
        a = BzrDir.create_standalone_workingtree('a')
695
1429
        os.mkdir('a/foo')
698
1432
        a.add(['foo', 'foo/bar', 'foo/baz'])
699
1433
        a.commit('initial commit')
700
1434
        b = BzrDir.create_standalone_workingtree('b')
701
 
        build_tree(a.basis_tree(), b)
 
1435
        basis = a.basis_tree()
 
1436
        basis.lock_read()
 
1437
        self.addCleanup(basis.unlock)
 
1438
        build_tree(basis, b)
702
1439
        self.assertIs(os.path.isdir('b/foo'), True)
703
1440
        self.assertEqual(file('b/foo/bar', 'rb').read(), "contents")
704
1441
        self.assertEqual(os.readlink('b/foo/baz'), 'a/foo/bar')
 
1442
 
 
1443
    def test_build_with_references(self):
 
1444
        tree = self.make_branch_and_tree('source',
 
1445
            format='dirstate-with-subtree')
 
1446
        subtree = self.make_branch_and_tree('source/subtree',
 
1447
            format='dirstate-with-subtree')
 
1448
        tree.add_reference(subtree)
 
1449
        tree.commit('a revision')
 
1450
        tree.branch.create_checkout('target')
 
1451
        self.failUnlessExists('target')
 
1452
        self.failUnlessExists('target/subtree')
 
1453
 
 
1454
    def test_file_conflict_handling(self):
 
1455
        """Ensure that when building trees, conflict handling is done"""
 
1456
        source = self.make_branch_and_tree('source')
 
1457
        target = self.make_branch_and_tree('target')
 
1458
        self.build_tree(['source/file', 'target/file'])
 
1459
        source.add('file', 'new-file')
 
1460
        source.commit('added file')
 
1461
        build_tree(source.basis_tree(), target)
 
1462
        self.assertEqual([DuplicateEntry('Moved existing file to',
 
1463
                          'file.moved', 'file', None, 'new-file')],
 
1464
                         target.conflicts())
 
1465
        target2 = self.make_branch_and_tree('target2')
 
1466
        target_file = file('target2/file', 'wb')
 
1467
        try:
 
1468
            source_file = file('source/file', 'rb')
 
1469
            try:
 
1470
                target_file.write(source_file.read())
 
1471
            finally:
 
1472
                source_file.close()
 
1473
        finally:
 
1474
            target_file.close()
 
1475
        build_tree(source.basis_tree(), target2)
 
1476
        self.assertEqual([], target2.conflicts())
 
1477
 
 
1478
    def test_symlink_conflict_handling(self):
 
1479
        """Ensure that when building trees, conflict handling is done"""
 
1480
        self.requireFeature(SymlinkFeature)
 
1481
        source = self.make_branch_and_tree('source')
 
1482
        os.symlink('foo', 'source/symlink')
 
1483
        source.add('symlink', 'new-symlink')
 
1484
        source.commit('added file')
 
1485
        target = self.make_branch_and_tree('target')
 
1486
        os.symlink('bar', 'target/symlink')
 
1487
        build_tree(source.basis_tree(), target)
 
1488
        self.assertEqual([DuplicateEntry('Moved existing file to',
 
1489
            'symlink.moved', 'symlink', None, 'new-symlink')],
 
1490
            target.conflicts())
 
1491
        target = self.make_branch_and_tree('target2')
 
1492
        os.symlink('foo', 'target2/symlink')
 
1493
        build_tree(source.basis_tree(), target)
 
1494
        self.assertEqual([], target.conflicts())
705
1495
        
 
1496
    def test_directory_conflict_handling(self):
 
1497
        """Ensure that when building trees, conflict handling is done"""
 
1498
        source = self.make_branch_and_tree('source')
 
1499
        target = self.make_branch_and_tree('target')
 
1500
        self.build_tree(['source/dir1/', 'source/dir1/file', 'target/dir1/'])
 
1501
        source.add(['dir1', 'dir1/file'], ['new-dir1', 'new-file'])
 
1502
        source.commit('added file')
 
1503
        build_tree(source.basis_tree(), target)
 
1504
        self.assertEqual([], target.conflicts())
 
1505
        self.failUnlessExists('target/dir1/file')
 
1506
 
 
1507
        # Ensure contents are merged
 
1508
        target = self.make_branch_and_tree('target2')
 
1509
        self.build_tree(['target2/dir1/', 'target2/dir1/file2'])
 
1510
        build_tree(source.basis_tree(), target)
 
1511
        self.assertEqual([], target.conflicts())
 
1512
        self.failUnlessExists('target2/dir1/file2')
 
1513
        self.failUnlessExists('target2/dir1/file')
 
1514
 
 
1515
        # Ensure new contents are suppressed for existing branches
 
1516
        target = self.make_branch_and_tree('target3')
 
1517
        self.make_branch('target3/dir1')
 
1518
        self.build_tree(['target3/dir1/file2'])
 
1519
        build_tree(source.basis_tree(), target)
 
1520
        self.failIfExists('target3/dir1/file')
 
1521
        self.failUnlessExists('target3/dir1/file2')
 
1522
        self.failUnlessExists('target3/dir1.diverted/file')
 
1523
        self.assertEqual([DuplicateEntry('Diverted to',
 
1524
            'dir1.diverted', 'dir1', 'new-dir1', None)],
 
1525
            target.conflicts())
 
1526
 
 
1527
        target = self.make_branch_and_tree('target4')
 
1528
        self.build_tree(['target4/dir1/'])
 
1529
        self.make_branch('target4/dir1/file')
 
1530
        build_tree(source.basis_tree(), target)
 
1531
        self.failUnlessExists('target4/dir1/file')
 
1532
        self.assertEqual('directory', file_kind('target4/dir1/file'))
 
1533
        self.failUnlessExists('target4/dir1/file.diverted')
 
1534
        self.assertEqual([DuplicateEntry('Diverted to',
 
1535
            'dir1/file.diverted', 'dir1/file', 'new-file', None)],
 
1536
            target.conflicts())
 
1537
 
 
1538
    def test_mixed_conflict_handling(self):
 
1539
        """Ensure that when building trees, conflict handling is done"""
 
1540
        source = self.make_branch_and_tree('source')
 
1541
        target = self.make_branch_and_tree('target')
 
1542
        self.build_tree(['source/name', 'target/name/'])
 
1543
        source.add('name', 'new-name')
 
1544
        source.commit('added file')
 
1545
        build_tree(source.basis_tree(), target)
 
1546
        self.assertEqual([DuplicateEntry('Moved existing file to',
 
1547
            'name.moved', 'name', None, 'new-name')], target.conflicts())
 
1548
 
 
1549
    def test_raises_in_populated(self):
 
1550
        source = self.make_branch_and_tree('source')
 
1551
        self.build_tree(['source/name'])
 
1552
        source.add('name')
 
1553
        source.commit('added name')
 
1554
        target = self.make_branch_and_tree('target')
 
1555
        self.build_tree(['target/name'])
 
1556
        target.add('name')
 
1557
        self.assertRaises(errors.WorkingTreeAlreadyPopulated, 
 
1558
            build_tree, source.basis_tree(), target)
 
1559
 
 
1560
    def test_build_tree_rename_count(self):
 
1561
        source = self.make_branch_and_tree('source')
 
1562
        self.build_tree(['source/file1', 'source/dir1/'])
 
1563
        source.add(['file1', 'dir1'])
 
1564
        source.commit('add1')
 
1565
        target1 = self.make_branch_and_tree('target1')
 
1566
        transform_result = build_tree(source.basis_tree(), target1)
 
1567
        self.assertEqual(2, transform_result.rename_count)
 
1568
 
 
1569
        self.build_tree(['source/dir1/file2'])
 
1570
        source.add(['dir1/file2'])
 
1571
        source.commit('add3')
 
1572
        target2 = self.make_branch_and_tree('target2')
 
1573
        transform_result = build_tree(source.basis_tree(), target2)
 
1574
        # children of non-root directories should not be renamed
 
1575
        self.assertEqual(2, transform_result.rename_count)
 
1576
 
 
1577
    def create_ab_tree(self):
 
1578
        """Create a committed test tree with two files"""
 
1579
        source = self.make_branch_and_tree('source')
 
1580
        self.build_tree_contents([('source/file1', 'A')])
 
1581
        self.build_tree_contents([('source/file2', 'B')])
 
1582
        source.add(['file1', 'file2'], ['file1-id', 'file2-id'])
 
1583
        source.commit('commit files')
 
1584
        source.lock_write()
 
1585
        self.addCleanup(source.unlock)
 
1586
        return source
 
1587
 
 
1588
    def test_build_tree_accelerator_tree(self):
 
1589
        source = self.create_ab_tree()
 
1590
        self.build_tree_contents([('source/file2', 'C')])
 
1591
        calls = []
 
1592
        real_source_get_file = source.get_file
 
1593
        def get_file(file_id, path=None):
 
1594
            calls.append(file_id)
 
1595
            return real_source_get_file(file_id, path)
 
1596
        source.get_file = get_file
 
1597
        target = self.make_branch_and_tree('target')
 
1598
        revision_tree = source.basis_tree()
 
1599
        revision_tree.lock_read()
 
1600
        self.addCleanup(revision_tree.unlock)
 
1601
        build_tree(revision_tree, target, source)
 
1602
        self.assertEqual(['file1-id'], calls)
 
1603
        target.lock_read()
 
1604
        self.addCleanup(target.unlock)
 
1605
        self.assertEqual([], list(target.iter_changes(revision_tree)))
 
1606
 
 
1607
    def test_build_tree_accelerator_tree_missing_file(self):
 
1608
        source = self.create_ab_tree()
 
1609
        os.unlink('source/file1')
 
1610
        source.remove(['file2'])
 
1611
        target = self.make_branch_and_tree('target')
 
1612
        revision_tree = source.basis_tree()
 
1613
        revision_tree.lock_read()
 
1614
        self.addCleanup(revision_tree.unlock)
 
1615
        build_tree(revision_tree, target, source)
 
1616
        target.lock_read()
 
1617
        self.addCleanup(target.unlock)
 
1618
        self.assertEqual([], list(target.iter_changes(revision_tree)))
 
1619
 
 
1620
    def test_build_tree_accelerator_wrong_kind(self):
 
1621
        self.requireFeature(SymlinkFeature)
 
1622
        source = self.make_branch_and_tree('source')
 
1623
        self.build_tree_contents([('source/file1', '')])
 
1624
        self.build_tree_contents([('source/file2', '')])
 
1625
        source.add(['file1', 'file2'], ['file1-id', 'file2-id'])
 
1626
        source.commit('commit files')
 
1627
        os.unlink('source/file2')
 
1628
        self.build_tree_contents([('source/file2/', 'C')])
 
1629
        os.unlink('source/file1')
 
1630
        os.symlink('file2', 'source/file1')
 
1631
        calls = []
 
1632
        real_source_get_file = source.get_file
 
1633
        def get_file(file_id, path=None):
 
1634
            calls.append(file_id)
 
1635
            return real_source_get_file(file_id, path)
 
1636
        source.get_file = get_file
 
1637
        target = self.make_branch_and_tree('target')
 
1638
        revision_tree = source.basis_tree()
 
1639
        revision_tree.lock_read()
 
1640
        self.addCleanup(revision_tree.unlock)
 
1641
        build_tree(revision_tree, target, source)
 
1642
        self.assertEqual([], calls)
 
1643
        target.lock_read()
 
1644
        self.addCleanup(target.unlock)
 
1645
        self.assertEqual([], list(target.iter_changes(revision_tree)))
 
1646
 
 
1647
    def test_build_tree_hardlink(self):
 
1648
        self.requireFeature(HardlinkFeature)
 
1649
        source = self.create_ab_tree()
 
1650
        target = self.make_branch_and_tree('target')
 
1651
        revision_tree = source.basis_tree()
 
1652
        revision_tree.lock_read()
 
1653
        self.addCleanup(revision_tree.unlock)
 
1654
        build_tree(revision_tree, target, source, hardlink=True)
 
1655
        target.lock_read()
 
1656
        self.addCleanup(target.unlock)
 
1657
        self.assertEqual([], list(target.iter_changes(revision_tree)))
 
1658
        source_stat = os.stat('source/file1')
 
1659
        target_stat = os.stat('target/file1')
 
1660
        self.assertEqual(source_stat, target_stat)
 
1661
 
 
1662
        # Explicitly disallowing hardlinks should prevent them.
 
1663
        target2 = self.make_branch_and_tree('target2')
 
1664
        build_tree(revision_tree, target2, source, hardlink=False)
 
1665
        target2.lock_read()
 
1666
        self.addCleanup(target2.unlock)
 
1667
        self.assertEqual([], list(target2.iter_changes(revision_tree)))
 
1668
        source_stat = os.stat('source/file1')
 
1669
        target2_stat = os.stat('target2/file1')
 
1670
        self.assertNotEqual(source_stat, target2_stat)
 
1671
 
 
1672
    def test_build_tree_accelerator_tree_moved(self):
 
1673
        source = self.make_branch_and_tree('source')
 
1674
        self.build_tree_contents([('source/file1', 'A')])
 
1675
        source.add(['file1'], ['file1-id'])
 
1676
        source.commit('commit files')
 
1677
        source.rename_one('file1', 'file2')
 
1678
        source.lock_read()
 
1679
        self.addCleanup(source.unlock)
 
1680
        target = self.make_branch_and_tree('target')
 
1681
        revision_tree = source.basis_tree()
 
1682
        revision_tree.lock_read()
 
1683
        self.addCleanup(revision_tree.unlock)
 
1684
        build_tree(revision_tree, target, source)
 
1685
        target.lock_read()
 
1686
        self.addCleanup(target.unlock)
 
1687
        self.assertEqual([], list(target.iter_changes(revision_tree)))
 
1688
 
 
1689
    def test_build_tree_hardlinks_preserve_execute(self):
 
1690
        self.requireFeature(HardlinkFeature)
 
1691
        source = self.create_ab_tree()
 
1692
        tt = TreeTransform(source)
 
1693
        trans_id = tt.trans_id_tree_file_id('file1-id')
 
1694
        tt.set_executability(True, trans_id)
 
1695
        tt.apply()
 
1696
        self.assertTrue(source.is_executable('file1-id'))
 
1697
        target = self.make_branch_and_tree('target')
 
1698
        revision_tree = source.basis_tree()
 
1699
        revision_tree.lock_read()
 
1700
        self.addCleanup(revision_tree.unlock)
 
1701
        build_tree(revision_tree, target, source, hardlink=True)
 
1702
        target.lock_read()
 
1703
        self.addCleanup(target.unlock)
 
1704
        self.assertEqual([], list(target.iter_changes(revision_tree)))
 
1705
        self.assertTrue(source.is_executable('file1-id'))
 
1706
 
 
1707
 
706
1708
class MockTransform(object):
707
1709
 
708
1710
    def has_named_child(self, by_parent, parent_id, name):
714
1716
                return True
715
1717
        return False
716
1718
 
 
1719
 
717
1720
class MockEntry(object):
718
1721
    def __init__(self):
719
1722
        object.__init__(self)
720
1723
        self.name = "name"
721
1724
 
 
1725
 
722
1726
class TestGetBackupName(TestCase):
723
1727
    def test_get_backup_name(self):
724
1728
        tt = MockTransform()
732
1736
        self.assertEqual(name, 'name.~1~')
733
1737
        name = get_backup_name(MockEntry(), {'a':['1', '2', '3']}, 'a', tt)
734
1738
        self.assertEqual(name, 'name.~4~')
 
1739
 
 
1740
 
 
1741
class TestFileMover(tests.TestCaseWithTransport):
 
1742
 
 
1743
    def test_file_mover(self):
 
1744
        self.build_tree(['a/', 'a/b', 'c/', 'c/d'])
 
1745
        mover = _FileMover()
 
1746
        mover.rename('a', 'q')
 
1747
        self.failUnlessExists('q')
 
1748
        self.failIfExists('a')
 
1749
        self.failUnlessExists('q/b')
 
1750
        self.failUnlessExists('c')
 
1751
        self.failUnlessExists('c/d')
 
1752
 
 
1753
    def test_pre_delete_rollback(self):
 
1754
        self.build_tree(['a/'])
 
1755
        mover = _FileMover()
 
1756
        mover.pre_delete('a', 'q')
 
1757
        self.failUnlessExists('q')
 
1758
        self.failIfExists('a')
 
1759
        mover.rollback()
 
1760
        self.failIfExists('q')
 
1761
        self.failUnlessExists('a')
 
1762
 
 
1763
    def test_apply_deletions(self):
 
1764
        self.build_tree(['a/', 'b/'])
 
1765
        mover = _FileMover()
 
1766
        mover.pre_delete('a', 'q')
 
1767
        mover.pre_delete('b', 'r')
 
1768
        self.failUnlessExists('q')
 
1769
        self.failUnlessExists('r')
 
1770
        self.failIfExists('a')
 
1771
        self.failIfExists('b')
 
1772
        mover.apply_deletions()
 
1773
        self.failIfExists('q')
 
1774
        self.failIfExists('r')
 
1775
        self.failIfExists('a')
 
1776
        self.failIfExists('b')
 
1777
 
 
1778
    def test_file_mover_rollback(self):
 
1779
        self.build_tree(['a/', 'a/b', 'c/', 'c/d/', 'c/e/'])
 
1780
        mover = _FileMover()
 
1781
        mover.rename('c/d', 'c/f')
 
1782
        mover.rename('c/e', 'c/d')
 
1783
        try:
 
1784
            mover.rename('a', 'c')
 
1785
        except errors.FileExists, e:
 
1786
            mover.rollback()
 
1787
        self.failUnlessExists('a')
 
1788
        self.failUnlessExists('c/d')
 
1789
 
 
1790
 
 
1791
class Bogus(Exception):
 
1792
    pass
 
1793
 
 
1794
 
 
1795
class TestTransformRollback(tests.TestCaseWithTransport):
 
1796
 
 
1797
    class ExceptionFileMover(_FileMover):
 
1798
 
 
1799
        def __init__(self, bad_source=None, bad_target=None):
 
1800
            _FileMover.__init__(self)
 
1801
            self.bad_source = bad_source
 
1802
            self.bad_target = bad_target
 
1803
 
 
1804
        def rename(self, source, target):
 
1805
            if (self.bad_source is not None and
 
1806
                source.endswith(self.bad_source)):
 
1807
                raise Bogus
 
1808
            elif (self.bad_target is not None and
 
1809
                target.endswith(self.bad_target)):
 
1810
                raise Bogus
 
1811
            else:
 
1812
                _FileMover.rename(self, source, target)
 
1813
 
 
1814
    def test_rollback_rename(self):
 
1815
        tree = self.make_branch_and_tree('.')
 
1816
        self.build_tree(['a/', 'a/b'])
 
1817
        tt = TreeTransform(tree)
 
1818
        self.addCleanup(tt.finalize)
 
1819
        a_id = tt.trans_id_tree_path('a')
 
1820
        tt.adjust_path('c', tt.root, a_id)
 
1821
        tt.adjust_path('d', a_id, tt.trans_id_tree_path('a/b'))
 
1822
        self.assertRaises(Bogus, tt.apply,
 
1823
                          _mover=self.ExceptionFileMover(bad_source='a'))
 
1824
        self.failUnlessExists('a')
 
1825
        self.failUnlessExists('a/b')
 
1826
        tt.apply()
 
1827
        self.failUnlessExists('c')
 
1828
        self.failUnlessExists('c/d')
 
1829
 
 
1830
    def test_rollback_rename_into_place(self):
 
1831
        tree = self.make_branch_and_tree('.')
 
1832
        self.build_tree(['a/', 'a/b'])
 
1833
        tt = TreeTransform(tree)
 
1834
        self.addCleanup(tt.finalize)
 
1835
        a_id = tt.trans_id_tree_path('a')
 
1836
        tt.adjust_path('c', tt.root, a_id)
 
1837
        tt.adjust_path('d', a_id, tt.trans_id_tree_path('a/b'))
 
1838
        self.assertRaises(Bogus, tt.apply,
 
1839
                          _mover=self.ExceptionFileMover(bad_target='c/d'))
 
1840
        self.failUnlessExists('a')
 
1841
        self.failUnlessExists('a/b')
 
1842
        tt.apply()
 
1843
        self.failUnlessExists('c')
 
1844
        self.failUnlessExists('c/d')
 
1845
 
 
1846
    def test_rollback_deletion(self):
 
1847
        tree = self.make_branch_and_tree('.')
 
1848
        self.build_tree(['a/', 'a/b'])
 
1849
        tt = TreeTransform(tree)
 
1850
        self.addCleanup(tt.finalize)
 
1851
        a_id = tt.trans_id_tree_path('a')
 
1852
        tt.delete_contents(a_id)
 
1853
        tt.adjust_path('d', tt.root, tt.trans_id_tree_path('a/b'))
 
1854
        self.assertRaises(Bogus, tt.apply,
 
1855
                          _mover=self.ExceptionFileMover(bad_target='d'))
 
1856
        self.failUnlessExists('a')
 
1857
        self.failUnlessExists('a/b')
 
1858
 
 
1859
    def test_resolve_no_parent(self):
 
1860
        wt = self.make_branch_and_tree('.')
 
1861
        tt = TreeTransform(wt)
 
1862
        self.addCleanup(tt.finalize)
 
1863
        parent = tt.trans_id_file_id('parent-id')
 
1864
        tt.new_file('file', parent, 'Contents')
 
1865
        resolve_conflicts(tt)
 
1866
 
 
1867
 
 
1868
class TestTransformPreview(tests.TestCaseWithTransport):
 
1869
 
 
1870
    def create_tree(self):
 
1871
        tree = self.make_branch_and_tree('.')
 
1872
        self.build_tree_contents([('a', 'content 1')])
 
1873
        tree.add('a', 'a-id')
 
1874
        tree.commit('rev1', rev_id='rev1')
 
1875
        return tree.branch.repository.revision_tree('rev1')
 
1876
 
 
1877
    def get_empty_preview(self):
 
1878
        repository = self.make_repository('repo')
 
1879
        tree = repository.revision_tree(_mod_revision.NULL_REVISION)
 
1880
        preview = TransformPreview(tree)
 
1881
        self.addCleanup(preview.finalize)
 
1882
        return preview
 
1883
 
 
1884
    def test_transform_preview(self):
 
1885
        revision_tree = self.create_tree()
 
1886
        preview = TransformPreview(revision_tree)
 
1887
        self.addCleanup(preview.finalize)
 
1888
 
 
1889
    def test_transform_preview_tree(self):
 
1890
        revision_tree = self.create_tree()
 
1891
        preview = TransformPreview(revision_tree)
 
1892
        self.addCleanup(preview.finalize)
 
1893
        preview.get_preview_tree()
 
1894
 
 
1895
    def test_transform_new_file(self):
 
1896
        revision_tree = self.create_tree()
 
1897
        preview = TransformPreview(revision_tree)
 
1898
        self.addCleanup(preview.finalize)
 
1899
        preview.new_file('file2', preview.root, 'content B\n', 'file2-id')
 
1900
        preview_tree = preview.get_preview_tree()
 
1901
        self.assertEqual(preview_tree.kind('file2-id'), 'file')
 
1902
        self.assertEqual(
 
1903
            preview_tree.get_file('file2-id').read(), 'content B\n')
 
1904
 
 
1905
    def test_diff_preview_tree(self):
 
1906
        revision_tree = self.create_tree()
 
1907
        preview = TransformPreview(revision_tree)
 
1908
        self.addCleanup(preview.finalize)
 
1909
        preview.new_file('file2', preview.root, 'content B\n', 'file2-id')
 
1910
        preview_tree = preview.get_preview_tree()
 
1911
        out = StringIO()
 
1912
        show_diff_trees(revision_tree, preview_tree, out)
 
1913
        lines = out.getvalue().splitlines()
 
1914
        self.assertEqual(lines[0], "=== added file 'file2'")
 
1915
        # 3 lines of diff administrivia
 
1916
        self.assertEqual(lines[4], "+content B")
 
1917
 
 
1918
    def test_transform_conflicts(self):
 
1919
        revision_tree = self.create_tree()
 
1920
        preview = TransformPreview(revision_tree)
 
1921
        self.addCleanup(preview.finalize)
 
1922
        preview.new_file('a', preview.root, 'content 2')
 
1923
        resolve_conflicts(preview)
 
1924
        trans_id = preview.trans_id_file_id('a-id')
 
1925
        self.assertEqual('a.moved', preview.final_name(trans_id))
 
1926
 
 
1927
    def get_tree_and_preview_tree(self):
 
1928
        revision_tree = self.create_tree()
 
1929
        preview = TransformPreview(revision_tree)
 
1930
        self.addCleanup(preview.finalize)
 
1931
        a_trans_id = preview.trans_id_file_id('a-id')
 
1932
        preview.delete_contents(a_trans_id)
 
1933
        preview.create_file('b content', a_trans_id)
 
1934
        preview_tree = preview.get_preview_tree()
 
1935
        return revision_tree, preview_tree
 
1936
 
 
1937
    def test_iter_changes(self):
 
1938
        revision_tree, preview_tree = self.get_tree_and_preview_tree()
 
1939
        root = revision_tree.inventory.root.file_id
 
1940
        self.assertEqual([('a-id', ('a', 'a'), True, (True, True),
 
1941
                          (root, root), ('a', 'a'), ('file', 'file'),
 
1942
                          (False, False))],
 
1943
                          list(preview_tree.iter_changes(revision_tree)))
 
1944
 
 
1945
    def test_wrong_tree_value_error(self):
 
1946
        revision_tree, preview_tree = self.get_tree_and_preview_tree()
 
1947
        e = self.assertRaises(ValueError, preview_tree.iter_changes,
 
1948
                              preview_tree)
 
1949
        self.assertEqual('from_tree must be transform source tree.', str(e))
 
1950
 
 
1951
    def test_include_unchanged_value_error(self):
 
1952
        revision_tree, preview_tree = self.get_tree_and_preview_tree()
 
1953
        e = self.assertRaises(ValueError, preview_tree.iter_changes,
 
1954
                              revision_tree, include_unchanged=True)
 
1955
        self.assertEqual('include_unchanged is not supported', str(e))
 
1956
 
 
1957
    def test_specific_files(self):
 
1958
        revision_tree, preview_tree = self.get_tree_and_preview_tree()
 
1959
        e = self.assertRaises(ValueError, preview_tree.iter_changes,
 
1960
                              revision_tree, specific_files=['pete'])
 
1961
        self.assertEqual('specific_files is not supported', str(e))
 
1962
 
 
1963
    def test_want_unversioned_value_error(self):
 
1964
        revision_tree, preview_tree = self.get_tree_and_preview_tree()
 
1965
        e = self.assertRaises(ValueError, preview_tree.iter_changes,
 
1966
                              revision_tree, want_unversioned=True)
 
1967
        self.assertEqual('want_unversioned is not supported', str(e))
 
1968
 
 
1969
    def test_ignore_extra_trees_no_specific_files(self):
 
1970
        # extra_trees is harmless without specific_files, so we'll silently
 
1971
        # accept it, even though we won't use it.
 
1972
        revision_tree, preview_tree = self.get_tree_and_preview_tree()
 
1973
        preview_tree.iter_changes(revision_tree, extra_trees=[preview_tree])
 
1974
 
 
1975
    def test_ignore_require_versioned_no_specific_files(self):
 
1976
        # require_versioned is meaningless without specific_files.
 
1977
        revision_tree, preview_tree = self.get_tree_and_preview_tree()
 
1978
        preview_tree.iter_changes(revision_tree, require_versioned=False)
 
1979
 
 
1980
    def test_ignore_pb(self):
 
1981
        # pb could be supported, but TT.iter_changes doesn't support it.
 
1982
        revision_tree, preview_tree = self.get_tree_and_preview_tree()
 
1983
        preview_tree.iter_changes(revision_tree, pb=progress.DummyProgress())
 
1984
 
 
1985
    def test_kind(self):
 
1986
        revision_tree = self.create_tree()
 
1987
        preview = TransformPreview(revision_tree)
 
1988
        self.addCleanup(preview.finalize)
 
1989
        preview.new_file('file', preview.root, 'contents', 'file-id')
 
1990
        preview.new_directory('directory', preview.root, 'dir-id')
 
1991
        preview_tree = preview.get_preview_tree()
 
1992
        self.assertEqual('file', preview_tree.kind('file-id'))
 
1993
        self.assertEqual('directory', preview_tree.kind('dir-id'))
 
1994
 
 
1995
    def test_get_file_mtime(self):
 
1996
        preview = self.get_empty_preview()
 
1997
        file_trans_id = preview.new_file('file', preview.root, 'contents',
 
1998
                                         'file-id')
 
1999
        limbo_path = preview._limbo_name(file_trans_id)
 
2000
        preview_tree = preview.get_preview_tree()
 
2001
        self.assertEqual(os.stat(limbo_path).st_mtime,
 
2002
                         preview_tree.get_file_mtime('file-id'))
 
2003
 
 
2004
    def test_get_file(self):
 
2005
        preview = self.get_empty_preview()
 
2006
        preview.new_file('file', preview.root, 'contents', 'file-id')
 
2007
        preview_tree = preview.get_preview_tree()
 
2008
        tree_file = preview_tree.get_file('file-id')
 
2009
        try:
 
2010
            self.assertEqual('contents', tree_file.read())
 
2011
        finally:
 
2012
            tree_file.close()
 
2013
 
 
2014
    def test_get_symlink_target(self):
 
2015
        self.requireFeature(SymlinkFeature)
 
2016
        preview = self.get_empty_preview()
 
2017
        preview.new_symlink('symlink', preview.root, 'target', 'symlink-id')
 
2018
        preview_tree = preview.get_preview_tree()
 
2019
        self.assertEqual('target',
 
2020
                         preview_tree.get_symlink_target('symlink-id'))