~bzr-pqm/bzr/bzr.dev

« back to all changes in this revision

Viewing changes to bzrlib/tests/test_branch.py

  • Committer: Martin Pool
  • Date: 2006-03-06 11:20:10 UTC
  • mfrom: (1593 +trunk)
  • mto: This revision was merged to the branch mainline in revision 1611.
  • Revision ID: mbp@sourcefrog.net-20060306112010-17c0170dde5d1eea
[merge] large merge to sync with bzr.dev

Show diffs side-by-side

added added

removed removed

Lines of Context:
16
16
 
17
17
"""Tests for the Branch facility that are not interface  tests.
18
18
 
19
 
For interface tests see test_branch_implementations.py.
 
19
For interface tests see tests/branch_implementations/*.py.
20
20
 
21
21
For concrete class tests see this file, and for meta-branch tests
22
22
also see this file.
24
24
 
25
25
from StringIO import StringIO
26
26
 
27
 
import bzrlib.branch as branch
 
27
import bzrlib.branch
 
28
import bzrlib.bzrdir as bzrdir
28
29
from bzrlib.errors import (NotBranchError,
29
30
                           UnknownFormatError,
30
31
                           UnsupportedFormatError,
31
32
                           )
32
33
 
33
 
from bzrlib.tests import TestCase, TestCaseInTempDir
 
34
from bzrlib.tests import TestCase, TestCaseWithTransport
34
35
from bzrlib.transport import get_transport
35
36
 
36
37
class TestDefaultFormat(TestCase):
37
38
 
38
 
    def test_get_set_default_initializer(self):
39
 
        old_initializer = branch.Branch.get_default_initializer()
40
 
        # default is BzrBranch._initialize
41
 
        self.assertEqual(branch.BzrBranch._initialize, old_initializer)
42
 
        def recorder(url):
43
 
            return "a branch %s" % url
44
 
        branch.Branch.set_default_initializer(recorder)
 
39
    def test_get_set_default_format(self):
 
40
        old_format = bzrlib.branch.BranchFormat.get_default_format()
 
41
        # default is 5
 
42
        self.assertTrue(isinstance(old_format, bzrlib.branch.BzrBranchFormat5))
 
43
        bzrlib.branch.BranchFormat.set_default_format(SampleBranchFormat())
45
44
        try:
46
 
            b = branch.Branch.create("memory:/")
47
 
            self.assertEqual("a branch memory:/", b)
 
45
            # the default branch format is used by the meta dir format
 
46
            # which is not the default bzrdir format at this point
 
47
            dir = bzrdir.BzrDirMetaFormat1().initialize('memory:/')
 
48
            result = dir.create_branch()
 
49
            self.assertEqual(result, 'A branch')
48
50
        finally:
49
 
            branch.Branch.set_default_initializer(old_initializer)
50
 
        self.assertEqual(old_initializer, branch.Branch.get_default_initializer())
51
 
 
52
 
 
53
 
class SampleBranchFormat(branch.BzrBranchFormat):
 
51
            bzrlib.branch.BranchFormat.set_default_format(old_format)
 
52
        self.assertEqual(old_format, bzrlib.branch.BranchFormat.get_default_format())
 
53
 
 
54
 
 
55
class SampleBranchFormat(bzrlib.branch.BranchFormat):
54
56
    """A sample format
55
57
 
56
58
    this format is initializable, unsupported to aid in testing the 
61
63
        """See BzrBranchFormat.get_format_string()."""
62
64
        return "Sample branch format."
63
65
 
64
 
    def initialize(self, url):
 
66
    def initialize(self, a_bzrdir):
65
67
        """Format 4 branches cannot be created."""
66
 
        t = get_transport(url)
67
 
        t.mkdir('.bzr')
68
 
        t.put('.bzr/branch-format', StringIO(self.get_format_string()))
 
68
        t = a_bzrdir.get_branch_transport(self)
 
69
        t.put('format', StringIO(self.get_format_string()))
69
70
        return 'A branch'
70
71
 
71
72
    def is_supported(self):
72
73
        return False
73
74
 
74
 
    def open(self, transport):
 
75
    def open(self, transport, _found=False):
75
76
        return "opened branch."
76
77
 
77
78
 
78
 
class TestBzrBranchFormat(TestCaseInTempDir):
 
79
class TestBzrBranchFormat(TestCaseWithTransport):
79
80
    """Tests for the BzrBranchFormat facility."""
80
81
 
81
82
    def test_find_format(self):
84
85
        # this is not quite the same as 
85
86
        self.build_tree(["foo/", "bar/"])
86
87
        def check_format(format, url):
87
 
            format.initialize(url)
88
 
            t = get_transport(url)
89
 
            found_format = branch.BzrBranchFormat.find_format(t)
 
88
            dir = format._matchingbzrdir.initialize(url)
 
89
            dir.create_repository()
 
90
            format.initialize(dir)
 
91
            found_format = bzrlib.branch.BranchFormat.find_format(dir)
90
92
            self.failUnless(isinstance(found_format, format.__class__))
91
 
        check_format(branch.BzrBranchFormat5(), "foo")
92
 
        check_format(branch.BzrBranchFormat6(), "bar")
 
93
        check_format(bzrlib.branch.BzrBranchFormat5(), "bar")
93
94
        
94
95
    def test_find_format_not_branch(self):
 
96
        dir = bzrdir.BzrDirMetaFormat1().initialize(self.get_url())
95
97
        self.assertRaises(NotBranchError,
96
 
                          branch.BzrBranchFormat.find_format,
97
 
                          get_transport('.'))
 
98
                          bzrlib.branch.BranchFormat.find_format,
 
99
                          dir)
98
100
 
99
101
    def test_find_format_unknown_format(self):
100
 
        t = get_transport('.')
101
 
        t.mkdir('.bzr')
102
 
        t.put('.bzr/branch-format', StringIO())
 
102
        dir = bzrdir.BzrDirMetaFormat1().initialize(self.get_url())
 
103
        SampleBranchFormat().initialize(dir)
103
104
        self.assertRaises(UnknownFormatError,
104
 
                          branch.BzrBranchFormat.find_format,
105
 
                          get_transport('.'))
 
105
                          bzrlib.branch.BranchFormat.find_format,
 
106
                          dir)
106
107
 
107
108
    def test_register_unregister_format(self):
108
109
        format = SampleBranchFormat()
 
110
        # make a control dir
 
111
        dir = bzrdir.BzrDirMetaFormat1().initialize(self.get_url())
109
112
        # make a branch
110
 
        format.initialize('.')
 
113
        format.initialize(dir)
111
114
        # register a format for it.
112
 
        branch.BzrBranchFormat.register_format(format)
 
115
        bzrlib.branch.BranchFormat.register_format(format)
113
116
        # which branch.Open will refuse (not supported)
114
 
        self.assertRaises(UnsupportedFormatError, branch.Branch.open, '.')
 
117
        self.assertRaises(UnsupportedFormatError, bzrlib.branch.Branch.open, self.get_url())
115
118
        # but open_downlevel will work
116
 
        t = get_transport('.')
117
 
        self.assertEqual(format.open(t), branch.Branch.open_downlevel('.'))
 
119
        self.assertEqual(format.open(dir), bzrdir.BzrDir.open(self.get_url()).open_branch(unsupported=True))
118
120
        # unregister the format
119
 
        branch.BzrBranchFormat.unregister_format(format)
120
 
        # now open_downlevel should fail too.
121
 
        self.assertRaises(UnknownFormatError, branch.Branch.open_downlevel, '.')
 
121
        bzrlib.branch.BranchFormat.unregister_format(format)
 
122
 
 
123
 
 
124
class TestBranchReference(TestCaseWithTransport):
 
125
    """Tests for the branch reference facility."""
 
126
 
 
127
    def test_create_open_reference(self):
 
128
        bzrdirformat = bzrdir.BzrDirMetaFormat1()
 
129
        t = get_transport(self.get_url('.'))
 
130
        t.mkdir('repo')
 
131
        dir = bzrdirformat.initialize(self.get_url('repo'))
 
132
        dir.create_repository()
 
133
        target_branch = dir.create_branch()
 
134
        t.mkdir('branch')
 
135
        branch_dir = bzrdirformat.initialize(self.get_url('branch'))
 
136
        made_branch = bzrlib.branch.BranchReferenceFormat().initialize(branch_dir, target_branch)
 
137
        self.assertEqual(made_branch.base, target_branch.base)
 
138
        opened_branch = branch_dir.open_branch()
 
139
        self.assertEqual(opened_branch.base, target_branch.base)