~bzr-pqm/bzr/bzr.dev

« back to all changes in this revision

Viewing changes to bzrlib/tests/test_bundle.py

  • Committer: Robert Collins
  • Date: 2008-09-23 02:16:55 UTC
  • mfrom: (3723 +trunk)
  • mto: This revision was merged to the branch mainline in revision 3725.
  • Revision ID: robertc@robertcollins.net-20080923021655-w6fiwcz42r9ju9xs
Adjust for trunk.

Show diffs side-by-side

added added

removed removed

Lines of Context:
16
16
 
17
17
from cStringIO import StringIO
18
18
import os
 
19
import socket
19
20
import sys
 
21
import threading
20
22
 
21
23
from bzrlib import (
22
24
    bzrdir,
38
40
from bzrlib.bundle.serializer.v4 import BundleSerializerV4
39
41
from bzrlib.branch import Branch
40
42
from bzrlib.diff import internal_diff
41
 
from bzrlib.errors import (BzrError, TestamentMismatch, NotABundle, BadBundle, 
42
 
                           NoSuchFile,)
43
43
from bzrlib.merge import Merge3Merger
44
44
from bzrlib.repofmt import knitrepo
45
45
from bzrlib.osutils import sha_file, sha_string
106
106
        elif kind == 'symlink':
107
107
            ie = InventoryLink(file_id, name, parent_id)
108
108
        else:
109
 
            raise BzrError('unknown kind %r' % kind)
 
109
            raise errors.BzrError('unknown kind %r' % kind)
110
110
        ie.text_sha1 = text_sha_1
111
111
        ie.text_size = text_size
112
112
        return ie
435
435
        return bundle 
436
436
 
437
437
    def test_non_bundle(self):
438
 
        self.assertRaises(NotABundle, read_bundle, StringIO('#!/bin/sh\n'))
 
438
        self.assertRaises(errors.NotABundle,
 
439
                          read_bundle, StringIO('#!/bin/sh\n'))
439
440
 
440
441
    def test_malformed(self):
441
 
        self.assertRaises(BadBundle, read_bundle, 
 
442
        self.assertRaises(errors.BadBundle, read_bundle,
442
443
                          StringIO('# Bazaar revision bundle v'))
443
444
 
444
445
    def test_crlf_bundle(self):
445
446
        try:
446
447
            read_bundle(StringIO('# Bazaar revision bundle v0.8\r\n'))
447
 
        except BadBundle:
 
448
        except errors.BadBundle:
448
449
            # It is currently permitted for bundles with crlf line endings to
449
450
            # make read_bundle raise a BadBundle, but this should be fixed.
450
451
            # Anything else, especially NotABundle, is an error.
482
483
                for inventory_id in old:
483
484
                    try:
484
485
                        old_file = old.get_file(inventory_id)
485
 
                    except NoSuchFile:
 
486
                    except errors.NoSuchFile:
486
487
                        continue
487
488
                    if old_file is None:
488
489
                        continue
618
619
        self.tree1.commit('removed', rev_id='a@cset-0-3')
619
620
        
620
621
        bundle = self.get_valid_bundle('a@cset-0-2', 'a@cset-0-3')
621
 
        self.assertRaises((TestamentMismatch,
 
622
        self.assertRaises((errors.TestamentMismatch,
622
623
            errors.VersionedFileInvalidChecksum), self.get_invalid_bundle,
623
624
            'a@cset-0-2', 'a@cset-0-3')
624
625
        # Check a rollup bundle 
796
797
        # Handle international characters
797
798
        os.mkdir('b1')
798
799
        try:
799
 
            f = open(u'b1/with Dod\xe9', 'wb')
 
800
            f = open(u'b1/with Dod\N{Euro Sign}', 'wb')
800
801
        except UnicodeEncodeError:
801
802
            raise TestSkipped("Filesystem doesn't support unicode")
802
803
 
808
809
            u'William Dod\xe9\n').encode('utf-8'))
809
810
        f.close()
810
811
 
811
 
        self.tree1.add([u'with Dod\xe9'], ['withdod-id'])
 
812
        self.tree1.add([u'with Dod\N{Euro Sign}'], ['withdod-id'])
812
813
        self.tree1.commit(u'i18n commit from William Dod\xe9',
813
814
                          rev_id='i18n-1', committer=u'William Dod\xe9')
814
815
 
815
 
        if sys.platform == 'darwin':
816
 
            from bzrlib.workingtree import WorkingTree3
817
 
            if type(self.tree1) is WorkingTree3:
818
 
                self.knownFailure("Bug #141438: fails for WorkingTree3 on OSX")
819
 
 
820
 
            # On Mac the '\xe9' gets changed to 'e\u0301'
821
 
            self.assertEqual([u'.bzr', u'with Dode\u0301'],
822
 
                             sorted(os.listdir(u'b1')))
823
 
            delta = self.tree1.changes_from(self.tree1.basis_tree())
824
 
            self.assertEqual([(u'with Dod\xe9', 'withdod-id', 'file')],
825
 
                             delta.removed)
826
 
            self.knownFailure("Mac OSX doesn't preserve unicode"
827
 
                              " combining characters.")
828
 
 
829
816
        # Add
830
817
        bundle = self.get_valid_bundle('null:', 'i18n-1')
831
818
 
832
819
        # Modified
833
 
        f = open(u'b1/with Dod\xe9', 'wb')
 
820
        f = open(u'b1/with Dod\N{Euro Sign}', 'wb')
834
821
        f.write(u'Modified \xb5\n'.encode('utf8'))
835
822
        f.close()
836
823
        self.tree1.commit(u'modified', rev_id='i18n-2')
837
824
 
838
825
        bundle = self.get_valid_bundle('i18n-1', 'i18n-2')
839
 
        
 
826
 
840
827
        # Renamed
841
 
        self.tree1.rename_one(u'with Dod\xe9', u'B\xe5gfors')
 
828
        self.tree1.rename_one(u'with Dod\N{Euro Sign}', u'B\N{Euro Sign}gfors')
842
829
        self.tree1.commit(u'renamed, the new i18n man', rev_id='i18n-3',
843
830
                          committer=u'Erik B\xe5gfors')
844
831
 
845
832
        bundle = self.get_valid_bundle('i18n-2', 'i18n-3')
846
833
 
847
834
        # Removed
848
 
        self.tree1.remove([u'B\xe5gfors'])
 
835
        self.tree1.remove([u'B\N{Euro Sign}gfors'])
849
836
        self.tree1.commit(u'removed', rev_id='i18n-4')
850
837
 
851
838
        bundle = self.get_valid_bundle('i18n-3', 'i18n-4')
1583
1570
        record = record_iter.next()
1584
1571
        self.assertEqual((None, {'foo': 'bar', 'storage_kind': 'header'},
1585
1572
            'info', None, None), record)
1586
 
        self.assertRaises(BadBundle, record_iter.next)
 
1573
        self.assertRaises(errors.BadBundle, record_iter.next)
1587
1574
 
1588
1575
 
1589
1576
class TestReadMergeableFromUrl(TestCaseWithTransport):
1602
1589
        self.build_tree_contents([('./foo:bar', out.getvalue())])
1603
1590
        self.assertRaises(errors.NotABundle, read_mergeable_from_url,
1604
1591
                          'foo:bar')
 
1592
 
 
1593
    def test_smart_server_connection_reset(self):
 
1594
        """If a smart server connection fails during the attempt to read a
 
1595
        bundle, then the ConnectionReset error should be propagated.
 
1596
        """
 
1597
        # Instantiate a server that will provoke a ConnectionReset
 
1598
        sock_server = _DisconnectingTCPServer()
 
1599
        sock_server.setUp()
 
1600
        self.addCleanup(sock_server.tearDown)
 
1601
        url = sock_server.get_url()
 
1602
        self.assertRaises(errors.ConnectionReset, read_mergeable_from_url, url)
 
1603
 
 
1604
 
 
1605
class _DisconnectingTCPServer(object):
 
1606
    """A TCP server that immediately closes any connection made to it."""
 
1607
 
 
1608
    def setUp(self):
 
1609
        self.sock = socket.socket(socket.AF_INET, socket.SOCK_STREAM)
 
1610
        self.sock.bind(('127.0.0.1', 0))
 
1611
        self.sock.listen(1)
 
1612
        self.port = self.sock.getsockname()[1]
 
1613
        self.thread = threading.Thread(
 
1614
            name='%s (port %d)' % (self.__class__.__name__, self.port),
 
1615
            target=self.accept_and_close)
 
1616
        self.thread.start()
 
1617
 
 
1618
    def accept_and_close(self):
 
1619
        conn, addr = self.sock.accept()
 
1620
        conn.shutdown(socket.SHUT_RDWR)
 
1621
        conn.close()
 
1622
 
 
1623
    def get_url(self):
 
1624
        return 'bzr://127.0.0.1:%d/' % (self.port,)
 
1625
 
 
1626
    def tearDown(self):
 
1627
        try:
 
1628
            # make sure the thread dies by connecting to the listening socket,
 
1629
            # just in case the test failed to do so.
 
1630
            conn = socket.socket(socket.AF_INET, socket.SOCK_STREAM)
 
1631
            conn.connect(self.sock.getsockname())
 
1632
            conn.close()
 
1633
        except socket.error:
 
1634
            pass
 
1635
        self.sock.close()
 
1636
        self.thread.join()
 
1637