/brz/remove-bazaar

To get this branch, use:
bzr branch http://gegoxaren.bato24.eu/bzr/brz/remove-bazaar

« back to all changes in this revision

Viewing changes to bzrlib/tests/test_transform.py

  • Committer: Aaron Bentley
  • Date: 2007-01-17 15:39:21 UTC
  • mfrom: (1551.9.35 Aaron's mergeable stuff)
  • mto: This revision was merged to the branch mainline in revision 2239.
  • Revision ID: abentley@panoramicfeedback.com-20070117153921-6pp9ssa2r8n5izoo
Merge bzr.ab, to avoid conflicts submitting

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
import sys
18
20
 
 
21
from bzrlib import (
 
22
    errors,
 
23
    generate_ids,
 
24
    tests,
 
25
    urlutils,
 
26
    )
19
27
from bzrlib.bzrdir import BzrDir
20
28
from bzrlib.conflicts import (DuplicateEntry, DuplicateID, MissingParent,
21
 
                              UnversionedParent, ParentLoop)
 
29
                              UnversionedParent, ParentLoop, DeletingParent,)
22
30
from bzrlib.errors import (DuplicateKey, MalformedTransform, NoSuchFile,
23
 
                           ReusingTransform, CantMoveRoot, NotVersionedError,
24
 
                           ExistingLimbo, ImmortalLimbo, LockError)
 
31
                           ReusingTransform, CantMoveRoot, 
 
32
                           PathsNotVersionedError, ExistingLimbo,
 
33
                           ImmortalLimbo, LockError)
25
34
from bzrlib.osutils import file_kind, has_symlinks, pathjoin
26
35
from bzrlib.merge import Merge3Merger
27
36
from bzrlib.tests import TestCaseInTempDir, TestSkipped, TestCase
29
38
                              resolve_conflicts, cook_conflicts, 
30
39
                              find_interesting, build_tree, get_backup_name)
31
40
 
 
41
 
32
42
class TestTreeTransform(TestCaseInTempDir):
 
43
 
33
44
    def setUp(self):
34
45
        super(TestTreeTransform, self).setUp()
35
46
        self.wt = BzrDir.create_standalone_workingtree('.')
38
49
    def get_transform(self):
39
50
        transform = TreeTransform(self.wt)
40
51
        #self.addCleanup(transform.finalize)
41
 
        return transform, transform.trans_id_tree_file_id(self.wt.get_root_id())
 
52
        return transform, transform.root
42
53
 
43
54
    def test_existing_limbo(self):
44
 
        limbo_name = self.wt._control_files.controlfilename('limbo')
 
55
        limbo_name = urlutils.local_path_from_url(
 
56
            self.wt._control_files.controlfilename('limbo'))
45
57
        transform, root = self.get_transform()
46
58
        os.mkdir(pathjoin(limbo_name, 'hehe'))
47
59
        self.assertRaises(ImmortalLimbo, transform.apply)
57
69
        transform, root = self.get_transform() 
58
70
        self.assertIs(transform.get_tree_parent(root), ROOT_PARENT)
59
71
        imaginary_id = transform.trans_id_tree_path('imaginary')
 
72
        imaginary_id2 = transform.trans_id_tree_path('imaginary/')
 
73
        self.assertEqual(imaginary_id, imaginary_id2)
60
74
        self.assertEqual(transform.get_tree_parent(imaginary_id), root)
61
75
        self.assertEqual(transform.final_kind(root), 'directory')
62
76
        self.assertEqual(transform.final_file_id(root), self.wt.get_root_id())
185
199
        transform3.delete_contents(oz_id)
186
200
        self.assertEqual(transform3.find_conflicts(), 
187
201
                         [('missing parent', oz_id)])
188
 
        root_id = transform3.trans_id_tree_file_id('TREE_ROOT')
 
202
        root_id = transform3.root
189
203
        tip_id = transform3.trans_id_tree_file_id('tip-id')
190
204
        transform3.adjust_path('tip', root_id, tip_id)
191
205
        transform3.apply()
217
231
    def test_name_invariants(self):
218
232
        create_tree, root = self.get_transform()
219
233
        # prepare tree
220
 
        root = create_tree.trans_id_tree_file_id('TREE_ROOT')
 
234
        root = create_tree.root
221
235
        create_tree.new_file('name1', root, 'hello1', 'name1')
222
236
        create_tree.new_file('name2', root, 'hello2', 'name2')
223
237
        ddir = create_tree.new_directory('dying_directory', root, 'ddir')
227
241
        create_tree.apply()
228
242
 
229
243
        mangle_tree,root = self.get_transform()
230
 
        root = mangle_tree.trans_id_tree_file_id('TREE_ROOT')
 
244
        root = mangle_tree.root
231
245
        #swap names
232
246
        name1 = mangle_tree.trans_id_tree_file_id('name1')
233
247
        name2 = mangle_tree.trans_id_tree_file_id('name2')
312
326
    def test_move_dangling_ie(self):
313
327
        create_tree, root = self.get_transform()
314
328
        # prepare tree
315
 
        root = create_tree.trans_id_tree_file_id('TREE_ROOT')
 
329
        root = create_tree.root
316
330
        create_tree.new_file('name1', root, 'hello1', 'name1')
317
331
        create_tree.apply()
318
332
        delete_contents, root = self.get_transform()
328
342
    def test_replace_dangling_ie(self):
329
343
        create_tree, root = self.get_transform()
330
344
        # prepare tree
331
 
        root = create_tree.trans_id_tree_file_id('TREE_ROOT')
 
345
        root = create_tree.root
332
346
        create_tree.new_file('name1', root, 'hello1', 'name1')
333
347
        create_tree.apply()
334
348
        delete_contents = TreeTransform(self.wt)
381
395
                                         'dorothy-id')
382
396
        old_dorothy = conflicts.trans_id_tree_file_id('dorothy-id')
383
397
        oz = conflicts.trans_id_tree_file_id('oz-id')
384
 
        # set up missing, unversioned parent
 
398
        # set up DeletedParent parent conflict
385
399
        conflicts.delete_versioned(oz)
386
400
        emerald = conflicts.trans_id_tree_file_id('emerald-id')
 
401
        # set up MissingParent conflict
 
402
        munchkincity = conflicts.trans_id_file_id('munchkincity-id')
 
403
        conflicts.adjust_path('munchkincity', root, munchkincity)
 
404
        conflicts.new_directory('auntem', munchkincity, 'auntem-id')
387
405
        # set up parent loop
388
406
        conflicts.adjust_path('emeraldcity', emerald, emerald)
389
407
        return conflicts, emerald, oz, old_dorothy, new_dorothy
410
428
                                   'dorothy.moved', 'dorothy', None,
411
429
                                   'dorothy-id')
412
430
        self.assertEqual(cooked_conflicts[1], duplicate_id)
413
 
        missing_parent = MissingParent('Not deleting', 'oz', 'oz-id')
 
431
        missing_parent = MissingParent('Created directory', 'munchkincity',
 
432
                                       'munchkincity-id')
 
433
        deleted_parent = DeletingParent('Not deleting', 'oz', 'oz-id')
414
434
        self.assertEqual(cooked_conflicts[2], missing_parent)
415
 
        unversioned_parent = UnversionedParent('Versioned directory', 'oz',
 
435
        unversioned_parent = UnversionedParent('Versioned directory',
 
436
                                               'munchkincity',
 
437
                                               'munchkincity-id')
 
438
        unversioned_parent2 = UnversionedParent('Versioned directory', 'oz',
416
439
                                               'oz-id')
417
440
        self.assertEqual(cooked_conflicts[3], unversioned_parent)
418
441
        parent_loop = ParentLoop('Cancelled move', 'oz/emeraldcity', 
419
442
                                 'oz/emeraldcity', 'emerald-id', 'emerald-id')
420
 
        self.assertEqual(cooked_conflicts[4], parent_loop)
421
 
        self.assertEqual(len(cooked_conflicts), 5)
 
443
        self.assertEqual(cooked_conflicts[4], deleted_parent)
 
444
        self.assertEqual(cooked_conflicts[5], unversioned_parent2)
 
445
        self.assertEqual(cooked_conflicts[6], parent_loop)
 
446
        self.assertEqual(len(cooked_conflicts), 7)
422
447
        tt.finalize()
423
448
 
424
449
    def test_string_conflicts(self):
434
459
        self.assertEqual(conflicts_s[1], 'Conflict adding id to dorothy.  '
435
460
                                         'Unversioned existing file '
436
461
                                         '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'
 
462
        self.assertEqual(conflicts_s[2], 'Conflict adding files to'
 
463
                                         ' munchkincity.  Created directory.')
 
464
        self.assertEqual(conflicts_s[3], 'Conflict because munchkincity is not'
 
465
                                         ' versioned, but has versioned'
 
466
                                         ' children.  Versioned directory.')
 
467
        self.assertEqualDiff(conflicts_s[4], "Conflict: can't delete oz because it"
 
468
                                         " is not empty.  Not deleting.")
 
469
        self.assertEqual(conflicts_s[5], 'Conflict because oz is not'
 
470
                                         ' versioned, but has versioned'
 
471
                                         ' children.  Versioned directory.')
 
472
        self.assertEqual(conflicts_s[6], 'Conflict moving oz/emeraldcity into'
442
473
                                         ' oz/emeraldcity.  Cancelled move.')
443
474
 
444
475
    def test_moving_versioned_directories(self):
487
518
        create.apply()
488
519
        self.assertEqual(find_interesting(wt, wt, ['vfile']),
489
520
                         set(['myfile-id']))
490
 
        self.assertRaises(NotVersionedError, find_interesting, wt, wt,
 
521
        self.assertRaises(PathsNotVersionedError, find_interesting, wt, wt,
491
522
                          ['uvfile'])
492
523
 
 
524
    def test_set_executability_order(self):
 
525
        """Ensure that executability behaves the same, no matter what order.
 
526
        
 
527
        - create file and set executability simultaneously
 
528
        - create file and set executability afterward
 
529
        - unsetting the executability of a file whose executability has not been
 
530
        declared should throw an exception (this may happen when a
 
531
        merge attempts to create a file with a duplicate ID)
 
532
        """
 
533
        transform, root = self.get_transform()
 
534
        wt = transform._tree
 
535
        transform.new_file('set_on_creation', root, 'Set on creation', 'soc',
 
536
                           True)
 
537
        sac = transform.new_file('set_after_creation', root, 'Set after creation', 'sac')
 
538
        transform.set_executability(True, sac)
 
539
        uws = transform.new_file('unset_without_set', root, 'Unset badly', 'uws')
 
540
        self.assertRaises(KeyError, transform.set_executability, None, uws)
 
541
        transform.apply()
 
542
        self.assertTrue(wt.is_executable('soc'))
 
543
        self.assertTrue(wt.is_executable('sac'))
 
544
 
 
545
    def test_preserve_mode(self):
 
546
        """File mode is preserved when replacing content"""
 
547
        if sys.platform == 'win32':
 
548
            raise TestSkipped('chmod has no effect on win32')
 
549
        transform, root = self.get_transform()
 
550
        transform.new_file('file1', root, 'contents', 'file1-id', True)
 
551
        transform.apply()
 
552
        self.assertTrue(self.wt.is_executable('file1-id'))
 
553
        transform, root = self.get_transform()
 
554
        file1_id = transform.trans_id_tree_file_id('file1-id')
 
555
        transform.delete_contents(file1_id)
 
556
        transform.create_file('contents2', file1_id)
 
557
        transform.apply()
 
558
        self.assertTrue(self.wt.is_executable('file1-id'))
 
559
 
 
560
    def test__set_mode_stats_correctly(self):
 
561
        """_set_mode stats to determine file mode."""
 
562
        if sys.platform == 'win32':
 
563
            raise TestSkipped('chmod has no effect on win32')
 
564
 
 
565
        stat_paths = []
 
566
        real_stat = os.stat
 
567
        def instrumented_stat(path):
 
568
            stat_paths.append(path)
 
569
            return real_stat(path)
 
570
 
 
571
        transform, root = self.get_transform()
 
572
 
 
573
        bar1_id = transform.new_file('bar', root, 'bar contents 1\n',
 
574
                                     file_id='bar-id-1', executable=False)
 
575
        transform.apply()
 
576
 
 
577
        transform, root = self.get_transform()
 
578
        bar1_id = transform.trans_id_tree_path('bar')
 
579
        bar2_id = transform.trans_id_tree_path('bar2')
 
580
        try:
 
581
            os.stat = instrumented_stat
 
582
            transform.create_file('bar2 contents\n', bar2_id, mode_id=bar1_id)
 
583
        finally:
 
584
            os.stat = real_stat
 
585
            transform.finalize()
 
586
 
 
587
        bar1_abspath = self.wt.abspath('bar')
 
588
        self.assertEqual([bar1_abspath], stat_paths)
 
589
 
493
590
 
494
591
class TransformGroup(object):
495
 
    def __init__(self, dirname):
 
592
    def __init__(self, dirname, root_id):
496
593
        self.name = dirname
497
594
        os.mkdir(dirname)
498
595
        self.wt = BzrDir.create_standalone_workingtree(dirname)
 
596
        self.wt.set_root_id(root_id)
499
597
        self.b = self.wt.branch
500
598
        self.tt = TreeTransform(self.wt)
501
599
        self.root = self.tt.trans_id_tree_file_id(self.wt.get_root_id())
507
605
 
508
606
class TestTransformMerge(TestCaseInTempDir):
509
607
    def test_text_merge(self):
510
 
        base = TransformGroup("base")
 
608
        root_id = generate_ids.gen_root_id()
 
609
        base = TransformGroup("base", root_id)
511
610
        base.tt.new_file('a', base.root, 'a\nb\nc\nd\be\n', 'a')
512
611
        base.tt.new_file('b', base.root, 'b1', 'b')
513
612
        base.tt.new_file('c', base.root, 'c', 'c')
517
616
        base.tt.new_directory('g', base.root, 'g')
518
617
        base.tt.new_directory('h', base.root, 'h')
519
618
        base.tt.apply()
520
 
        other = TransformGroup("other")
 
619
        other = TransformGroup("other", root_id)
521
620
        other.tt.new_file('a', other.root, 'y\nb\nc\nd\be\n', 'a')
522
621
        other.tt.new_file('b', other.root, 'b2', 'b')
523
622
        other.tt.new_file('c', other.root, 'c2', 'c')
528
627
        other.tt.new_file('h', other.root, 'h\ni\nj\nk\n', 'h')
529
628
        other.tt.new_file('i', other.root, 'h\ni\nj\nk\n', 'i')
530
629
        other.tt.apply()
531
 
        this = TransformGroup("this")
 
630
        this = TransformGroup("this", root_id)
532
631
        this.tt.new_file('a', this.root, 'a\nb\nc\nd\bz\n', 'a')
533
632
        this.tt.new_file('b', this.root, 'b', 'b')
534
633
        this.tt.new_file('c', this.root, 'c', 'c')
585
684
    def test_file_merge(self):
586
685
        if not has_symlinks():
587
686
            raise TestSkipped('Symlinks are not supported on this platform')
588
 
        base = TransformGroup("BASE")
589
 
        this = TransformGroup("THIS")
590
 
        other = TransformGroup("OTHER")
 
687
        root_id = generate_ids.gen_root_id()
 
688
        base = TransformGroup("BASE", root_id)
 
689
        this = TransformGroup("THIS", root_id)
 
690
        other = TransformGroup("OTHER", root_id)
591
691
        for tg in this, base, other:
592
692
            tg.tt.new_directory('a', tg.root, 'a')
593
693
            tg.tt.new_symlink('b', tg.root, 'b', 'b')
625
725
        self.assertIs(os.path.lexists(this.wt.abspath('h.OTHER')), True)
626
726
 
627
727
    def test_filename_merge(self):
628
 
        base = TransformGroup("BASE")
629
 
        this = TransformGroup("THIS")
630
 
        other = TransformGroup("OTHER")
 
728
        root_id = generate_ids.gen_root_id()
 
729
        base = TransformGroup("BASE", root_id)
 
730
        this = TransformGroup("THIS", root_id)
 
731
        other = TransformGroup("OTHER", root_id)
631
732
        base_a, this_a, other_a = [t.tt.new_directory('a', t.root, 'a') 
632
733
                                   for t in [base, this, other]]
633
734
        base_b, this_b, other_b = [t.tt.new_directory('b', t.root, 'b') 
657
758
        self.assertEqual(this.wt.id2path('f'), pathjoin('b/f1'))
658
759
 
659
760
    def test_filename_merge_conflicts(self):
660
 
        base = TransformGroup("BASE")
661
 
        this = TransformGroup("THIS")
662
 
        other = TransformGroup("OTHER")
 
761
        root_id = generate_ids.gen_root_id()
 
762
        base = TransformGroup("BASE", root_id)
 
763
        this = TransformGroup("THIS", root_id)
 
764
        other = TransformGroup("OTHER", root_id)
663
765
        base_a, this_a, other_a = [t.tt.new_directory('a', t.root, 'a') 
664
766
                                   for t in [base, this, other]]
665
767
        base_b, this_b, other_b = [t.tt.new_directory('b', t.root, 'b') 
686
788
        self.assertIs(os.path.lexists(this.wt.abspath('b/h1.OTHER')), False)
687
789
        self.assertEqual(this.wt.id2path('i'), pathjoin('b/i1.OTHER'))
688
790
 
689
 
class TestBuildTree(TestCaseInTempDir):
 
791
 
 
792
class TestBuildTree(tests.TestCaseWithTransport):
 
793
 
690
794
    def test_build_tree(self):
691
795
        if not has_symlinks():
692
796
            raise TestSkipped('Test requires symlink support')
702
806
        self.assertIs(os.path.isdir('b/foo'), True)
703
807
        self.assertEqual(file('b/foo/bar', 'rb').read(), "contents")
704
808
        self.assertEqual(os.readlink('b/foo/baz'), 'a/foo/bar')
 
809
 
 
810
    def test_file_conflict_handling(self):
 
811
        """Ensure that when building trees, conflict handling is done"""
 
812
        source = self.make_branch_and_tree('source')
 
813
        target = self.make_branch_and_tree('target')
 
814
        self.build_tree(['source/file', 'target/file'])
 
815
        source.add('file', 'new-file')
 
816
        source.commit('added file')
 
817
        build_tree(source.basis_tree(), target)
 
818
        self.assertEqual([DuplicateEntry('Moved existing file to',
 
819
                          'file.moved', 'file', None, 'new-file')],
 
820
                         target.conflicts())
 
821
        target2 = self.make_branch_and_tree('target2')
 
822
        target_file = file('target2/file', 'wb')
 
823
        try:
 
824
            source_file = file('source/file', 'rb')
 
825
            try:
 
826
                target_file.write(source_file.read())
 
827
            finally:
 
828
                source_file.close()
 
829
        finally:
 
830
            target_file.close()
 
831
        build_tree(source.basis_tree(), target2)
 
832
        self.assertEqual([], target2.conflicts())
 
833
 
 
834
    def test_symlink_conflict_handling(self):
 
835
        """Ensure that when building trees, conflict handling is done"""
 
836
        if not has_symlinks():
 
837
            raise TestSkipped('Test requires symlink support')
 
838
        source = self.make_branch_and_tree('source')
 
839
        os.symlink('foo', 'source/symlink')
 
840
        source.add('symlink', 'new-symlink')
 
841
        source.commit('added file')
 
842
        target = self.make_branch_and_tree('target')
 
843
        os.symlink('bar', 'target/symlink')
 
844
        build_tree(source.basis_tree(), target)
 
845
        self.assertEqual([DuplicateEntry('Moved existing file to',
 
846
            'symlink.moved', 'symlink', None, 'new-symlink')],
 
847
            target.conflicts())
 
848
        target = self.make_branch_and_tree('target2')
 
849
        os.symlink('foo', 'target2/symlink')
 
850
        build_tree(source.basis_tree(), target)
 
851
        self.assertEqual([], target.conflicts())
705
852
        
 
853
    def test_directory_conflict_handling(self):
 
854
        """Ensure that when building trees, conflict handling is done"""
 
855
        source = self.make_branch_and_tree('source')
 
856
        target = self.make_branch_and_tree('target')
 
857
        self.build_tree(['source/dir1/', 'source/dir1/file', 'target/dir1/'])
 
858
        source.add(['dir1', 'dir1/file'], ['new-dir1', 'new-file'])
 
859
        source.commit('added file')
 
860
        build_tree(source.basis_tree(), target)
 
861
        self.assertEqual([], target.conflicts())
 
862
        self.failUnlessExists('target/dir1/file')
 
863
 
 
864
        # Ensure contents are merged
 
865
        target = self.make_branch_and_tree('target2')
 
866
        self.build_tree(['target2/dir1/', 'target2/dir1/file2'])
 
867
        build_tree(source.basis_tree(), target)
 
868
        self.assertEqual([], target.conflicts())
 
869
        self.failUnlessExists('target2/dir1/file2')
 
870
        self.failUnlessExists('target2/dir1/file')
 
871
 
 
872
        # Ensure new contents are suppressed for existing branches
 
873
        target = self.make_branch_and_tree('target3')
 
874
        self.make_branch('target3/dir1')
 
875
        self.build_tree(['target3/dir1/file2'])
 
876
        build_tree(source.basis_tree(), target)
 
877
        self.failIfExists('target3/dir1/file')
 
878
        self.failUnlessExists('target3/dir1/file2')
 
879
        self.failUnlessExists('target3/dir1.diverted/file')
 
880
        self.assertEqual([DuplicateEntry('Diverted to',
 
881
            'dir1.diverted', 'dir1', 'new-dir1', None)],
 
882
            target.conflicts())
 
883
 
 
884
        target = self.make_branch_and_tree('target4')
 
885
        self.build_tree(['target4/dir1/'])
 
886
        self.make_branch('target4/dir1/file')
 
887
        build_tree(source.basis_tree(), target)
 
888
        self.failUnlessExists('target4/dir1/file')
 
889
        self.assertEqual('directory', file_kind('target4/dir1/file'))
 
890
        self.failUnlessExists('target4/dir1/file.diverted')
 
891
        self.assertEqual([DuplicateEntry('Diverted to',
 
892
            'dir1/file.diverted', 'dir1/file', 'new-file', None)],
 
893
            target.conflicts())
 
894
 
 
895
    def test_mixed_conflict_handling(self):
 
896
        """Ensure that when building trees, conflict handling is done"""
 
897
        source = self.make_branch_and_tree('source')
 
898
        target = self.make_branch_and_tree('target')
 
899
        self.build_tree(['source/name', 'target/name/'])
 
900
        source.add('name', 'new-name')
 
901
        source.commit('added file')
 
902
        build_tree(source.basis_tree(), target)
 
903
        self.assertEqual([DuplicateEntry('Moved existing file to',
 
904
            'name.moved', 'name', None, 'new-name')], target.conflicts())
 
905
 
 
906
    def test_raises_in_populated(self):
 
907
        source = self.make_branch_and_tree('source')
 
908
        self.build_tree(['source/name'])
 
909
        source.add('name')
 
910
        source.commit('added name')
 
911
        target = self.make_branch_and_tree('target')
 
912
        self.build_tree(['target/name'])
 
913
        target.add('name')
 
914
        self.assertRaises(errors.WorkingTreeAlreadyPopulated, 
 
915
            build_tree, source.basis_tree(), target)
 
916
 
 
917
 
706
918
class MockTransform(object):
707
919
 
708
920
    def has_named_child(self, by_parent, parent_id, name):