1
# Copyright (C) 2005, 2006 Canonical Ltd
1
# Copyright (C) 2005, 2006, 2007 Canonical Ltd
3
3
# This program is free software; you can redistribute it and/or modify
4
4
# it under the terms of the GNU General Public License as published by
34
from bzrlib.branch import (BzrBranch5,
33
from bzrlib.branch import (
37
BranchReferenceFormat,
36
42
from bzrlib.bzrdir import (BzrDirMetaFormat1, BzrDirMeta1,
37
43
BzrDir, BzrDirFormat)
38
44
from bzrlib.errors import (NotBranchError,
47
53
class TestDefaultFormat(TestCase):
49
55
def test_get_set_default_format(self):
50
old_format = bzrlib.branch.BranchFormat.get_default_format()
56
old_format = BranchFormat.get_default_format()
52
self.assertTrue(isinstance(old_format, bzrlib.branch.BzrBranchFormat5))
53
bzrlib.branch.BranchFormat.set_default_format(SampleBranchFormat())
58
self.assertTrue(isinstance(old_format, BzrBranchFormat5))
59
BranchFormat.set_default_format(SampleBranchFormat())
55
61
# the default branch format is used by the meta dir format
56
62
# which is not the default bzrdir format at this point
58
64
result = dir.create_branch()
59
65
self.assertEqual(result, 'A branch')
61
bzrlib.branch.BranchFormat.set_default_format(old_format)
62
self.assertEqual(old_format, bzrlib.branch.BranchFormat.get_default_format())
67
BranchFormat.set_default_format(old_format)
68
self.assertEqual(old_format, BranchFormat.get_default_format())
65
71
class TestBranchFormat5(TestCaseWithTransport):
99
105
# recursive section - that is, it appends the branch name.
102
class SampleBranchFormat(bzrlib.branch.BranchFormat):
108
class SampleBranchFormat(BranchFormat):
103
109
"""A sample format
105
111
this format is initializable, unsupported to aid in testing the
135
141
dir = format._matchingbzrdir.initialize(url)
136
142
dir.create_repository()
137
143
format.initialize(dir)
138
found_format = bzrlib.branch.BranchFormat.find_format(dir)
144
found_format = BranchFormat.find_format(dir)
139
145
self.failUnless(isinstance(found_format, format.__class__))
140
check_format(bzrlib.branch.BzrBranchFormat5(), "bar")
146
check_format(BzrBranchFormat5(), "bar")
142
148
def test_find_format_not_branch(self):
143
149
dir = bzrdir.BzrDirMetaFormat1().initialize(self.get_url())
144
150
self.assertRaises(NotBranchError,
145
bzrlib.branch.BranchFormat.find_format,
151
BranchFormat.find_format,
148
154
def test_find_format_unknown_format(self):
149
155
dir = bzrdir.BzrDirMetaFormat1().initialize(self.get_url())
150
156
SampleBranchFormat().initialize(dir)
151
157
self.assertRaises(UnknownFormatError,
152
bzrlib.branch.BranchFormat.find_format,
158
BranchFormat.find_format,
155
161
def test_register_unregister_format(self):
160
166
format.initialize(dir)
161
167
# register a format for it.
162
bzrlib.branch.BranchFormat.register_format(format)
168
BranchFormat.register_format(format)
163
169
# which branch.Open will refuse (not supported)
164
self.assertRaises(UnsupportedFormatError, bzrlib.branch.Branch.open, self.get_url())
170
self.assertRaises(UnsupportedFormatError, Branch.open, self.get_url())
165
171
self.make_branch_and_tree('foo')
166
172
# but open_downlevel will work
167
173
self.assertEqual(format.open(dir), bzrdir.BzrDir.open(self.get_url()).open_branch(unsupported=True))
168
174
# unregister the format
169
bzrlib.branch.BranchFormat.unregister_format(format)
175
BranchFormat.unregister_format(format)
170
176
self.make_branch_and_tree('bar')
172
178
def test_checkout_format(self):
263
269
target_branch = dir.create_branch()
264
270
t.mkdir('branch')
265
271
branch_dir = bzrdirformat.initialize(self.get_url('branch'))
266
made_branch = bzrlib.branch.BranchReferenceFormat().initialize(branch_dir, target_branch)
272
made_branch = BranchReferenceFormat().initialize(branch_dir, target_branch)
267
273
self.assertEqual(made_branch.base, target_branch.base)
268
274
opened_branch = branch_dir.open_branch()
269
275
self.assertEqual(opened_branch.base, target_branch.base)
274
280
def test_constructor(self):
275
281
"""Check that creating a BranchHooks instance has the right defaults."""
276
hooks = bzrlib.branch.BranchHooks()
282
hooks = BranchHooks()
277
283
self.assertTrue("set_rh" in hooks, "set_rh not in %s" % hooks)
278
284
self.assertTrue("post_push" in hooks, "post_push not in %s" % hooks)
279
285
self.assertTrue("post_commit" in hooks, "post_commit not in %s" % hooks)
283
289
def test_installed_hooks_are_BranchHooks(self):
284
290
"""The installed hooks object should be a BranchHooks."""
285
291
# the installed hooks are saved in self._preserved_hooks.
286
self.assertIsInstance(self._preserved_hooks, bzrlib.branch.BranchHooks)
292
self.assertIsInstance(self._preserved_hooks, BranchHooks)
288
294
def test_install_hook_raises_unknown_hook(self):
289
295
"""install_hook should raise UnknownHook if a hook is unknown."""
290
hooks = bzrlib.branch.BranchHooks()
296
hooks = BranchHooks()
291
297
self.assertRaises(UnknownHook, hooks.install_hook, 'silly', None)
293
299
def test_install_hook_appends_known_hook(self):
294
300
"""install_hook should append the callable for known hooks."""
295
hooks = bzrlib.branch.BranchHooks()
301
hooks = BranchHooks()
296
302
hooks.install_hook('set_rh', None)
297
303
self.assertEqual(hooks['set_rh'], [None])
306
class TestPullResult(TestCase):
308
def test_pull_result_to_int(self):
309
# to support old code, the pull result can be used as an int
313
# this usage of results is not recommended for new code (because it
314
# doesn't describe very well what happened), but for api stability
315
# it's still supported
316
a = "%d revisions pulled" % r
317
self.assertEqual(a, "10 revisions pulled")