~bzr-pqm/bzr/bzr.dev

« back to all changes in this revision

Viewing changes to bzrlib/tests/test_bundle.py

  • Committer: Canonical.com Patch Queue Manager
  • Date: 2006-07-03 13:56:48 UTC
  • mfrom: (1711.4.41 win32-accepted)
  • Revision ID: pqm@pqm.ubuntu.com-20060703135648-b927e5efb9f5b907
(jam) cleanup for win32, close open file handles, tests pass

Show diffs side-by-side

added added

removed removed

Lines of Context:
15
15
# Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307  USA
16
16
 
17
17
from cStringIO import StringIO
 
18
import os
 
19
import sys
 
20
import tempfile
18
21
 
19
22
from bzrlib.builtins import merge
20
23
from bzrlib.bzrdir import BzrDir
21
24
from bzrlib.bundle.apply_bundle import install_bundle, merge_bundle
22
25
from bzrlib.bundle.bundle_data import BundleTree
23
26
from bzrlib.bundle.serializer import write_bundle, read_bundle
 
27
from bzrlib.branch import Branch
24
28
from bzrlib.diff import internal_diff
 
29
from bzrlib.delta import compare_trees
25
30
from bzrlib.errors import BzrError, TestamentMismatch, NotABundle, BadBundle
26
31
from bzrlib.merge import Merge3Merger
27
32
from bzrlib.osutils import has_symlinks, sha_file
52
57
            return self.make_entry(file_id, self.paths[file_id])
53
58
 
54
59
    def parent_id(self, file_id):
55
 
        from os.path import dirname
56
 
        parent_dir = dirname(self.paths[file_id])
 
60
        parent_dir = os.path.dirname(self.paths[file_id])
57
61
        if parent_dir == "":
58
62
            return None
59
63
        return self.ids[parent_dir]
70
74
        return kind
71
75
 
72
76
    def make_entry(self, file_id, path):
73
 
        from os.path import basename
74
77
        from bzrlib.inventory import (InventoryEntry, InventoryFile
75
78
                                    , InventoryDirectory, InventoryLink)
76
 
        name = basename(path)
 
79
        name = os.path.basename(path)
77
80
        kind = self.get_file_kind(file_id)
78
81
        parent_id = self.parent_id(file_id)
79
82
        text_sha_1, text_size = self.contents_stats(file_id)
295
298
        self.assertEqual(self.sorted_ids(btree), ['a', 'b', 'd', 'e'])
296
299
 
297
300
 
298
 
class BundleTester(TestCaseInTempDir):
 
301
class BundleTester(TestCaseWithTransport):
299
302
 
300
303
    def create_bundle_text(self, base_rev_id, rev_id):
301
304
        bundle_txt = StringIO()
379
382
    def get_checkout(self, rev_id, checkout_dir=None):
380
383
        """Get a new tree, with the specified revision in it.
381
384
        """
382
 
        from bzrlib.branch import Branch
383
 
        import tempfile
384
385
 
385
386
        if checkout_dir is None:
386
387
            checkout_dir = tempfile.mkdtemp(prefix='test-branch-', dir='.')
387
388
        else:
388
 
            import os
389
389
            if not os.path.exists(checkout_dir):
390
390
                os.mkdir(checkout_dir)
391
391
        tree = BzrDir.create_standalone_workingtree(checkout_dir)
398
398
        for ancestor in ancestors:
399
399
            old = self.b1.repository.revision_tree(ancestor)
400
400
            new = tree.branch.repository.revision_tree(ancestor)
 
401
 
 
402
            # Check that there aren't any inventory level changes
 
403
            delta = compare_trees(old, new)
 
404
            self.assertFalse(delta.has_changed(),
 
405
                             'Revision %s not copied correctly.'
 
406
                             % (ancestor,))
 
407
 
 
408
            # Now check that the file contents are all correct
401
409
            for inventory_id in old:
402
410
                try:
403
411
                    old_file = old.get_file(inventory_id)
411
419
            rh = self.b1.revision_history()
412
420
            tree.branch.set_revision_history(rh[:rh.index(rev_id)+1])
413
421
            tree.update()
 
422
            delta = compare_trees(self.b1.repository.revision_tree(rev_id),
 
423
                                  tree)
 
424
            self.assertFalse(delta.has_changed(),
 
425
                             'Working tree has modifications')
414
426
        return tree
415
427
 
416
428
    def valid_apply_bundle(self, base_rev_id, info, checkout_dir=None):
462
474
            #         to_tree.get_file(fileid).read())
463
475
 
464
476
    def test_bundle(self):
465
 
        self.tree1 = BzrDir.create_standalone_workingtree('b1')
 
477
        self.tree1 = self.make_branch_and_tree('b1')
466
478
        self.b1 = self.tree1.branch
467
479
 
468
480
        open('b1/one', 'wb').write('one\n')
484
496
                , 'b1/dir/'
485
497
                , 'b1/dir/filein subdir.c'
486
498
                , 'b1/dir/WithCaps.txt'
487
 
                , 'b1/dir/trailing space '
 
499
                , 'b1/dir/ pre space'
488
500
                , 'b1/sub/'
489
501
                , 'b1/sub/sub/'
490
502
                , 'b1/sub/sub/nonempty.txt'
491
 
                # Tabs are not valid in filenames on windows
492
 
                #'b1/with\ttab.txt'
493
503
                ])
494
504
        open('b1/sub/sub/emptyfile.txt', 'wb').close()
495
505
        open('b1/dir/nolastnewline.txt', 'wb').write('bloop')
501
511
                , 'dir'
502
512
                , 'dir/filein subdir.c'
503
513
                , 'dir/WithCaps.txt'
504
 
                , 'dir/trailing space '
 
514
                , 'dir/ pre space'
505
515
                , 'dir/nolastnewline.txt'
506
516
                , 'sub'
507
517
                , 'sub/sub'
533
543
        # Check a rollup bundle 
534
544
        bundle = self.get_valid_bundle(None, 'a@cset-0-3')
535
545
 
536
 
 
537
546
        # Now move the directory
538
547
        self.tree1.rename_one('dir', 'sub/dir')
539
548
        self.tree1.commit('rename dir', rev_id='a@cset-0-4')
544
553
 
545
554
        # Modified files
546
555
        open('b1/sub/dir/WithCaps.txt', 'ab').write('\nAdding some text\n')
547
 
        open('b1/sub/dir/trailing space ', 'ab').write('\nAdding some\nDOS format lines\n')
 
556
        open('b1/sub/dir/ pre space', 'ab').write('\r\nAdding some\r\nDOS format lines\r\n')
548
557
        open('b1/sub/dir/nolastnewline.txt', 'ab').write('\n')
549
 
        self.tree1.rename_one('sub/dir/trailing space ', 
550
 
                              'sub/ start and end space ')
 
558
        self.tree1.rename_one('sub/dir/ pre space', 
 
559
                              'sub/ start space')
551
560
        self.tree1.commit('Modified files', rev_id='a@cset-0-5')
552
561
        bundle = self.get_valid_bundle('a@cset-0-4', 'a@cset-0-5')
553
562
 
554
 
        # Handle international characters
555
 
        try:
556
 
            f = open(u'b1/with Dod\xe9', 'wb')
557
 
        except UnicodeEncodeError:
558
 
            raise TestSkipped("Filesystem doesn't support unicode")
559
 
        f.write((u'A file\n'
560
 
            u'With international man of mystery\n'
561
 
            u'William Dod\xe9\n').encode('utf-8'))
562
 
        self.tree1.add([u'with Dod\xe9'])
563
 
        # BUG: (sort of) You must set verbose=False, so that python doesn't try
564
 
        #       and print the name of William Dode as part of the commit
565
 
        self.tree1.commit(u'i18n commit from William Dod\xe9', 
566
 
                          rev_id='a@cset-0-6', committer=u'William Dod\xe9',
567
 
                          verbose=False)
568
 
        bundle = self.get_valid_bundle('a@cset-0-5', 'a@cset-0-6')
569
563
        self.tree1.rename_one('sub/dir/WithCaps.txt', 'temp')
570
564
        self.tree1.rename_one('with space.txt', 'WithCaps.txt')
571
565
        self.tree1.rename_one('temp', 'with space.txt')
572
 
        self.tree1.commit(u'swap filenames', rev_id='a@cset-0-7',
 
566
        self.tree1.commit(u'swap filenames', rev_id='a@cset-0-6',
 
567
                          verbose=False)
 
568
        bundle = self.get_valid_bundle('a@cset-0-5', 'a@cset-0-6')
 
569
        other = self.get_checkout('a@cset-0-5')
 
570
        other.rename_one('sub/dir/nolastnewline.txt', 'sub/nolastnewline.txt')
 
571
        other.commit('rename file', rev_id='a@cset-0-6b')
 
572
        merge([other.basedir, -1], [None, None], this_dir=self.tree1.basedir)
 
573
        self.tree1.commit(u'Merge', rev_id='a@cset-0-7',
573
574
                          verbose=False)
574
575
        bundle = self.get_valid_bundle('a@cset-0-6', 'a@cset-0-7')
575
 
        other = self.get_checkout('a@cset-0-6')
576
 
        other.rename_one('sub/dir/nolastnewline.txt', 'sub/nolastnewline.txt')
577
 
        other.commit('rename file', rev_id='a@cset-0-7b')
578
 
        merge([other.basedir, -1], [None, None], this_dir=self.tree1.basedir)
579
 
        self.tree1.commit(u'Merge', rev_id='a@cset-0-8',
580
 
                          verbose=False)
581
 
        bundle = self.get_valid_bundle('a@cset-0-7', 'a@cset-0-8')
582
576
 
583
577
    def test_symlink_bundle(self):
584
578
        if not has_symlinks():
689
683
        self.assertContainsRe(bundle_file.getvalue(), 'one')
690
684
        self.assertContainsRe(bundle_file.getvalue(), 'three')
691
685
 
 
686
    def test_unicode_bundle(self):
 
687
        # Handle international characters
 
688
        os.mkdir('b1')
 
689
        try:
 
690
            f = open(u'b1/with Dod\xe9', 'wb')
 
691
        except UnicodeEncodeError:
 
692
            raise TestSkipped("Filesystem doesn't support unicode")
 
693
 
 
694
        self.tree1 = self.make_branch_and_tree('b1')
 
695
        self.b1 = self.tree1.branch
 
696
 
 
697
        f.write((u'A file\n'
 
698
            u'With international man of mystery\n'
 
699
            u'William Dod\xe9\n').encode('utf-8'))
 
700
        f.close()
 
701
 
 
702
        self.tree1.add([u'with Dod\xe9'])
 
703
        self.tree1.commit(u'i18n commit from William Dod\xe9', 
 
704
                          rev_id='i18n-1', committer=u'William Dod\xe9')
 
705
 
 
706
        # Add
 
707
        bundle = self.get_valid_bundle(None, 'i18n-1')
 
708
 
 
709
        # Modified
 
710
        f = open(u'b1/with Dod\xe9', 'wb')
 
711
        f.write(u'Modified \xb5\n'.encode('utf8'))
 
712
        f.close()
 
713
        self.tree1.commit(u'modified', rev_id='i18n-2')
 
714
 
 
715
        bundle = self.get_valid_bundle('i18n-1', 'i18n-2')
 
716
        
 
717
        # Renamed
 
718
        self.tree1.rename_one(u'with Dod\xe9', u'B\xe5gfors')
 
719
        self.tree1.commit(u'renamed, the new i18n man', rev_id='i18n-3',
 
720
                          committer=u'Erik B\xe5gfors')
 
721
 
 
722
        bundle = self.get_valid_bundle('i18n-2', 'i18n-3')
 
723
 
 
724
        # Removed
 
725
        self.tree1.remove([u'B\xe5gfors'])
 
726
        self.tree1.commit(u'removed', rev_id='i18n-4')
 
727
 
 
728
        bundle = self.get_valid_bundle('i18n-3', 'i18n-4')
 
729
 
 
730
        # Rollup
 
731
        bundle = self.get_valid_bundle(None, 'i18n-4')
 
732
 
 
733
 
 
734
    def test_whitespace_bundle(self):
 
735
        if sys.platform in ('win32', 'cygwin'):
 
736
            raise TestSkipped('Windows doesn\'t support filenames'
 
737
                              ' with tabs or trailing spaces')
 
738
        self.tree1 = self.make_branch_and_tree('b1')
 
739
        self.b1 = self.tree1.branch
 
740
 
 
741
        self.build_tree(['b1/trailing space '])
 
742
        self.tree1.add(['trailing space '])
 
743
        # TODO: jam 20060701 Check for handling files with '\t' characters
 
744
        #       once we actually support them
 
745
 
 
746
        # Added
 
747
        self.tree1.commit('funky whitespace', rev_id='white-1')
 
748
 
 
749
        bundle = self.get_valid_bundle(None, 'white-1')
 
750
 
 
751
        # Modified
 
752
        open('b1/trailing space ', 'ab').write('add some text\n')
 
753
        self.tree1.commit('add text', rev_id='white-2')
 
754
 
 
755
        bundle = self.get_valid_bundle('white-1', 'white-2')
 
756
 
 
757
        # Renamed
 
758
        self.tree1.rename_one('trailing space ', ' start and end space ')
 
759
        self.tree1.commit('rename', rev_id='white-3')
 
760
 
 
761
        bundle = self.get_valid_bundle('white-2', 'white-3')
 
762
 
 
763
        # Removed
 
764
        self.tree1.remove([' start and end space '])
 
765
        self.tree1.commit('removed', rev_id='white-4')
 
766
 
 
767
        bundle = self.get_valid_bundle('white-3', 'white-4')
 
768
        
 
769
        # Now test a complet roll-up
 
770
        bundle = self.get_valid_bundle(None, 'white-4')
 
771
 
692
772
 
693
773
class MungedBundleTester(TestCaseWithTransport):
694
774