~bzr-pqm/bzr/bzr.dev

« back to all changes in this revision

Viewing changes to bzrlib/tests/test_bundle.py

  • Committer: Jelmer Vernooij
  • Date: 2011-11-29 11:48:02 UTC
  • mto: This revision was merged to the branch mainline in revision 6325.
  • Revision ID: jelmer@samba.org-20111129114802-vtm3rm4e5brvt87f
Fix capitalization.

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 SocketServer
 
19
import socket
20
20
import sys
 
21
import threading
21
22
 
22
23
from bzrlib import (
23
24
    bzrdir,
40
41
from bzrlib.bundle.serializer.v4 import BundleSerializerV4
41
42
from bzrlib.repofmt import knitrepo
42
43
from bzrlib.tests import (
43
 
    features,
 
44
    test_read_bundle,
44
45
    test_commit,
45
 
    test_read_bundle,
46
 
    test_server,
47
46
    )
48
47
from bzrlib.transform import TreeTransform
 
48
from bzrlib.tests import (
 
49
    features,
 
50
    )
49
51
 
50
52
 
51
53
def get_text(vf, key):
75
77
        self.root = InventoryDirectory(ROOT_ID, '', None)
76
78
 
77
79
    inventory = property(lambda x:x)
78
 
    root_inventory = property(lambda x:x)
79
 
 
80
 
    def get_root_id(self):
81
 
        return self.root.file_id
82
80
 
83
81
    def all_file_ids(self):
84
82
        return set(self.paths.keys())
85
83
 
86
 
    def is_executable(self, file_id):
87
 
        # Not all the files are executable.
88
 
        return False
89
 
 
90
84
    def __getitem__(self, file_id):
91
85
        if file_id == self.root.file_id:
92
86
            return self.root
103
97
        for path, file_id in self.ids.iteritems():
104
98
            yield path, self[file_id]
105
99
 
106
 
    def kind(self, file_id):
 
100
    def get_file_kind(self, file_id):
107
101
        if file_id in self.contents:
108
102
            kind = 'file'
109
103
        else:
111
105
        return kind
112
106
 
113
107
    def make_entry(self, file_id, path):
114
 
        from bzrlib.inventory import (InventoryFile , InventoryDirectory,
115
 
            InventoryLink)
 
108
        from bzrlib.inventory import (InventoryEntry, InventoryFile
 
109
                                    , InventoryDirectory, InventoryLink)
116
110
        name = os.path.basename(path)
117
 
        kind = self.kind(file_id)
 
111
        kind = self.get_file_kind(file_id)
118
112
        parent_id = self.parent_id(file_id)
119
113
        text_sha_1, text_size = self.contents_stats(file_id)
120
114
        if kind == 'directory':
155
149
    def get_file_revision(self, file_id):
156
150
        return self.inventory[file_id].revision
157
151
 
158
 
    def get_file_size(self, file_id):
159
 
        return self.inventory[file_id].text_size
160
 
 
161
 
    def get_file_sha1(self, file_id):
162
 
        return self.inventory[file_id].text_sha1
163
 
 
164
152
    def contents_stats(self, file_id):
165
153
        if file_id not in self.contents:
166
154
            return None, None
329
317
        self.assertTrue(btree.path2id("grandparent/parent/file") is None)
330
318
 
331
319
    def sorted_ids(self, tree):
332
 
        ids = list(tree.all_file_ids())
 
320
        ids = list(tree)
333
321
        ids.sort()
334
322
        return ids
335
323
 
663
651
        bundle = self.get_valid_bundle('null:', 'a@cset-0-4')
664
652
 
665
653
        # Modified files
666
 
        with open('b1/sub/dir/WithCaps.txt', 'ab') as f: f.write('\nAdding some text\n')
667
 
        with open('b1/sub/dir/ pre space', 'ab') as f: f.write(
 
654
        open('b1/sub/dir/WithCaps.txt', 'ab').write('\nAdding some text\n')
 
655
        open('b1/sub/dir/ pre space', 'ab').write(
668
656
             '\r\nAdding some\r\nDOS format lines\r\n')
669
 
        with open('b1/sub/dir/nolastnewline.txt', 'ab') as f: f.write('\n')
 
657
        open('b1/sub/dir/nolastnewline.txt', 'ab').write('\n')
670
658
        self.tree1.rename_one('sub/dir/ pre space',
671
659
                              'sub/ start space')
672
660
        self.tree1.commit('Modified files', rev_id='a@cset-0-5')
821
809
        self.tree1 = self.make_branch_and_tree('b1')
822
810
        self.b1 = self.tree1.branch
823
811
 
824
 
        with open('b1/one', 'wb') as f: f.write('one\n')
 
812
        open('b1/one', 'wb').write('one\n')
825
813
        self.tree1.add('one')
826
814
        self.tree1.commit('add file', rev_id='a@cset-0-1')
827
 
        with open('b1/one', 'wb') as f: f.write('two\n')
 
815
        open('b1/one', 'wb').write('two\n')
828
816
        self.tree1.commit('modify', rev_id='a@cset-0-2')
829
 
        with open('b1/one', 'wb') as f: f.write('three\n')
 
817
        open('b1/one', 'wb').write('three\n')
830
818
        self.tree1.commit('modify', rev_id='a@cset-0-3')
831
819
        bundle_file = StringIO()
832
820
        rev_ids = write_bundle(self.tree1.branch.repository, 'a@cset-0-3',
911
899
        bundle = self.get_valid_bundle('null:', 'white-1')
912
900
 
913
901
        # Modified
914
 
        with open('b1/trailing space ', 'ab') as f: f.write('add some text\n')
 
902
        open('b1/trailing space ', 'ab').write('add some text\n')
915
903
        self.tree1.commit('add text', rev_id='white-2')
916
904
 
917
905
        bundle = self.get_valid_bundle('white-1', 'white-2')
959
947
        self.tree1.commit('message', rev_id='revid1')
960
948
        bundle = self.get_valid_bundle('null:', 'revid1')
961
949
        tree = self.get_bundle_tree(bundle, 'revid1')
962
 
        root_revision = tree.get_file_revision(tree.get_root_id())
963
 
        self.assertEqual('revid1', root_revision)
 
950
        self.assertEqual('revid1', tree.inventory.root.revision)
964
951
 
965
952
    def test_install_revisions(self):
966
953
        self.tree1 = self.make_branch_and_tree('b1')
1433
1420
            from bzrlib.testament import Testament
1434
1421
            # monkey patch gpg signing mechanism
1435
1422
            bzrlib.gpg.GPGStrategy = bzrlib.gpg.LoopbackGPGStrategy
1436
 
            new_config = test_commit.MustSignConfig()
1437
 
            commit.Commit(config_stack=new_config).commit(message="base",
 
1423
            new_config = test_commit.MustSignConfig(branch)
 
1424
            commit.Commit(config=new_config).commit(message="base",
1438
1425
                                                    allow_pointless=True,
1439
1426
                                                    rev_id='B',
1440
1427
                                                    working_tree=tree_a)
1849
1836
        bundle, then the ConnectionReset error should be propagated.
1850
1837
        """
1851
1838
        # Instantiate a server that will provoke a ConnectionReset
1852
 
        sock_server = DisconnectingServer()
 
1839
        sock_server = _DisconnectingTCPServer()
1853
1840
        self.start_server(sock_server)
1854
1841
        # We don't really care what the url is since the server will close the
1855
1842
        # connection without interpreting it
1857
1844
        self.assertRaises(errors.ConnectionReset, read_mergeable_from_url, url)
1858
1845
 
1859
1846
 
1860
 
class DisconnectingHandler(SocketServer.BaseRequestHandler):
1861
 
    """A request handler that immediately closes any connection made to it."""
1862
 
 
1863
 
    def handle(self):
1864
 
        self.request.close()
1865
 
 
1866
 
 
1867
 
class DisconnectingServer(test_server.TestingTCPServerInAThread):
1868
 
 
1869
 
    def __init__(self):
1870
 
        super(DisconnectingServer, self).__init__(
1871
 
            ('127.0.0.1', 0),
1872
 
            test_server.TestingTCPServer,
1873
 
            DisconnectingHandler)
 
1847
class _DisconnectingTCPServer(object):
 
1848
    """A TCP server that immediately closes any connection made to it."""
 
1849
 
 
1850
    def start_server(self):
 
1851
        self.sock = socket.socket(socket.AF_INET, socket.SOCK_STREAM)
 
1852
        self.sock.bind(('127.0.0.1', 0))
 
1853
        self.sock.listen(1)
 
1854
        self.port = self.sock.getsockname()[1]
 
1855
        self.thread = threading.Thread(
 
1856
            name='%s (port %d)' % (self.__class__.__name__, self.port),
 
1857
            target=self.accept_and_close)
 
1858
        self.thread.start()
 
1859
 
 
1860
    def accept_and_close(self):
 
1861
        conn, addr = self.sock.accept()
 
1862
        conn.shutdown(socket.SHUT_RDWR)
 
1863
        conn.close()
1874
1864
 
1875
1865
    def get_url(self):
1876
 
        """Return the url of the server"""
1877
 
        return "bzr://%s:%d/" % self.server.server_address
 
1866
        return 'bzr://127.0.0.1:%d/' % (self.port,)
 
1867
 
 
1868
    def stop_server(self):
 
1869
        try:
 
1870
            # make sure the thread dies by connecting to the listening socket,
 
1871
            # just in case the test failed to do so.
 
1872
            conn = socket.socket(socket.AF_INET, socket.SOCK_STREAM)
 
1873
            conn.connect(self.sock.getsockname())
 
1874
            conn.close()
 
1875
        except socket.error:
 
1876
            pass
 
1877
        self.sock.close()
 
1878
        self.thread.join()