~bzr-pqm/bzr/bzr.dev

« back to all changes in this revision

Viewing changes to bzrlib/tests/test_repository.py

Split out repository into .bzr/repository

Show diffs side-by-side

added added

removed removed

Lines of Context:
22
22
also see this file.
23
23
"""
24
24
 
 
25
from stat import *
25
26
from StringIO import StringIO
26
27
 
27
28
import bzrlib.bzrdir as bzrdir
 
29
import bzrlib.errors as errors
28
30
from bzrlib.errors import (NotBranchError,
29
31
                           NoSuchFile,
30
32
                           UnknownFormatError,
42
44
    def test_get_set_default_format(self):
43
45
        old_format = repository.RepositoryFormat.get_default_format()
44
46
        # default is None - we cannot create a Repository independently yet
45
 
        self.assertEqual(old_format, None)
 
47
        self.assertTrue(isinstance(old_format, repository.RepositoryFormat7))
46
48
        repository.RepositoryFormat.set_default_format(SampleRepositoryFormat())
47
49
        # creating a repository should now create an instrumented dir.
48
50
        try:
49
 
            # directly
50
 
            control = bzrdir.BzrDir.create('memory:/')
51
 
            result = repository.Repository.create(control)
 
51
            # the default branch format is used by the meta dir format
 
52
            # which is not the default bzrdir format at this point
 
53
            dir = bzrdir.BzrDirMetaFormat1().initialize('memory:/')
 
54
            result = dir.create_repository()
52
55
            self.assertEqual(result, 'A bzr repository dir')
53
56
        finally:
54
57
            repository.RepositoryFormat.set_default_format(old_format)
59
62
    """A sample format
60
63
 
61
64
    this format is initializable, unsupported to aid in testing the 
62
 
    open and open_downlevel routines.
 
65
    open and open(unsupported=True) routines.
63
66
    """
64
67
 
65
68
    def get_format_string(self):
68
71
 
69
72
    def initialize(self, a_bzrdir):
70
73
        """Initialize a repository in a BzrDir"""
71
 
        t = a_bzrdir.transport
72
 
        t.mkdir('repository')
73
 
        t.put('repository/format', StringIO(self.get_format_string()))
 
74
        t = a_bzrdir.get_repository_transport(self)
 
75
        t.put('format', StringIO(self.get_format_string()))
74
76
        return 'A bzr repository dir'
75
77
 
76
78
    def is_supported(self):
77
79
        return False
78
80
 
79
 
    def open(self, a_bzrdir):
 
81
    def open(self, a_bzrdir, _found=False):
80
82
        return "opened repository."
81
83
 
82
84
 
 
85
class TestRepositoryFormat(TestCaseWithTransport):
 
86
    """Tests for the Repository format detection used by the bzr meta dir facility.BzrBranchFormat facility."""
 
87
 
 
88
    def test_find_format(self):
 
89
        # is the right format object found for a repository?
 
90
        # create a branch with a few known format objects.
 
91
        # this is not quite the same as 
 
92
        self.build_tree(["foo/", "bar/"])
 
93
        def check_format(format, url):
 
94
            dir = format._matchingbzrdir.initialize(url)
 
95
            format.initialize(dir)
 
96
            t = get_transport(url)
 
97
            found_format = repository.RepositoryFormat.find_format(dir)
 
98
            self.failUnless(isinstance(found_format, format.__class__))
 
99
        check_format(repository.RepositoryFormat7(), "bar")
 
100
        
 
101
    def test_find_format_no_repository(self):
 
102
        dir = bzrdir.BzrDirMetaFormat1().initialize(self.get_url())
 
103
        self.assertRaises(errors.NoRepositoryPresent,
 
104
                          repository.RepositoryFormat.find_format,
 
105
                          dir)
 
106
 
 
107
    def test_find_format_unknown_format(self):
 
108
        dir = bzrdir.BzrDirMetaFormat1().initialize(self.get_url())
 
109
        SampleRepositoryFormat().initialize(dir)
 
110
        self.assertRaises(UnknownFormatError,
 
111
                          repository.RepositoryFormat.find_format,
 
112
                          dir)
 
113
 
 
114
    def test_register_unregister_format(self):
 
115
        format = SampleRepositoryFormat()
 
116
        # make a control dir
 
117
        dir = bzrdir.BzrDirMetaFormat1().initialize(self.get_url())
 
118
        # make a repo
 
119
        format.initialize(dir)
 
120
        # register a format for it.
 
121
        repository.RepositoryFormat.register_format(format)
 
122
        # which repository.Open will refuse (not supported)
 
123
        self.assertRaises(UnsupportedFormatError, repository.Repository.open, self.get_url())
 
124
        # but open(unsupported) will work
 
125
        self.assertEqual(format.open(dir), "opened repository.")
 
126
        # unregister the format
 
127
        repository.RepositoryFormat.unregister_format(format)
 
128
 
 
129
 
83
130
class TestFormat6(TestCaseWithTransport):
84
131
 
85
132
    def test_no_ancestry_weave(self):
91
138
                          control.transport.get,
92
139
                          'ancestry.weave')
93
140
 
 
141
 
 
142
class TestFormat7(TestCaseWithTransport):
 
143
    
 
144
    def test_disk_layout(self):
 
145
        control = bzrdir.BzrDirMetaFormat1().initialize(self.get_url())
 
146
        repo = repository.RepositoryFormat7().initialize(control)
 
147
        # we want:
 
148
        # format 'Bazaar-NG Repository format 7'
 
149
        # lock ''
 
150
        # inventory.weave == empty_weave
 
151
        # empty revision-store directory
 
152
        # empty weaves directory
 
153
        t = control.get_repository_transport(None)
 
154
        self.assertEqualDiff('Bazaar-NG Repository format 7',
 
155
                             t.get('format').read())
 
156
        self.assertEqualDiff('', t.get('lock').read())
 
157
        self.assertTrue(S_ISDIR(t.stat('revision-store').st_mode))
 
158
        self.assertTrue(S_ISDIR(t.stat('weaves').st_mode))
 
159
        self.assertEqualDiff('# bzr weave file v5\n'
 
160
                             'w\n'
 
161
                             'W\n',
 
162
                             t.get('inventory.weave').read())