37
55
class TestDefaultFormat(TestCase):
39
57
def test_get_set_default_format(self):
40
old_format = bzrlib.branch.BranchFormat.get_default_format()
58
old_format = BranchFormat.get_default_format()
42
self.assertTrue(isinstance(old_format, bzrlib.branch.BzrBranchFormat5))
43
bzrlib.branch.BranchFormat.set_default_format(SampleBranchFormat())
60
self.assertTrue(isinstance(old_format, BzrBranchFormat5))
61
BranchFormat.set_default_format(SampleBranchFormat())
45
63
# the default branch format is used by the meta dir format
46
64
# which is not the default bzrdir format at this point
47
dir = bzrdir.BzrDirMetaFormat1().initialize('memory:/')
65
dir = BzrDirMetaFormat1().initialize('memory:///')
48
66
result = dir.create_branch()
49
67
self.assertEqual(result, 'A branch')
51
bzrlib.branch.BranchFormat.set_default_format(old_format)
52
self.assertEqual(old_format, bzrlib.branch.BranchFormat.get_default_format())
55
class SampleBranchFormat(bzrlib.branch.BranchFormat):
69
BranchFormat.set_default_format(old_format)
70
self.assertEqual(old_format, BranchFormat.get_default_format())
73
class TestBranchFormat5(TestCaseWithTransport):
74
"""Tests specific to branch format 5"""
76
def test_branch_format_5_uses_lockdir(self):
78
bzrdir = BzrDirMetaFormat1().initialize(url)
79
bzrdir.create_repository()
80
branch = bzrdir.create_branch()
81
t = self.get_transport()
82
self.log("branch instance is %r" % branch)
83
self.assert_(isinstance(branch, BzrBranch5))
84
self.assertIsDirectory('.', t)
85
self.assertIsDirectory('.bzr/branch', t)
86
self.assertIsDirectory('.bzr/branch/lock', t)
89
self.assertIsDirectory('.bzr/branch/lock/held', t)
93
def test_set_push_location(self):
94
from bzrlib.config import (locations_config_filename,
95
ensure_config_dir_exists)
96
ensure_config_dir_exists()
97
fn = locations_config_filename()
98
branch = self.make_branch('.', format='knit')
99
branch.set_push_location('foo')
100
local_path = urlutils.local_path_from_url(branch.base[:-1])
101
self.assertFileEqual("[%s]\n"
102
"push_location = foo\n"
103
"push_location:policy = norecurse" % local_path,
106
# TODO RBC 20051029 test getting a push location from a branch in a
107
# recursive section - that is, it appends the branch name.
110
class SampleBranchFormat(BranchFormat):
56
111
"""A sample format
58
113
this format is initializable, unsupported to aid in testing the
88
143
dir = format._matchingbzrdir.initialize(url)
89
144
dir.create_repository()
90
145
format.initialize(dir)
91
found_format = bzrlib.branch.BranchFormat.find_format(dir)
146
found_format = BranchFormat.find_format(dir)
92
147
self.failUnless(isinstance(found_format, format.__class__))
93
check_format(bzrlib.branch.BzrBranchFormat5(), "bar")
148
check_format(BzrBranchFormat5(), "bar")
95
150
def test_find_format_not_branch(self):
96
151
dir = bzrdir.BzrDirMetaFormat1().initialize(self.get_url())
97
152
self.assertRaises(NotBranchError,
98
bzrlib.branch.BranchFormat.find_format,
153
BranchFormat.find_format,
101
156
def test_find_format_unknown_format(self):
102
157
dir = bzrdir.BzrDirMetaFormat1().initialize(self.get_url())
103
158
SampleBranchFormat().initialize(dir)
104
159
self.assertRaises(UnknownFormatError,
105
bzrlib.branch.BranchFormat.find_format,
160
BranchFormat.find_format,
108
163
def test_register_unregister_format(self):
113
168
format.initialize(dir)
114
169
# register a format for it.
115
bzrlib.branch.BranchFormat.register_format(format)
170
BranchFormat.register_format(format)
116
171
# which branch.Open will refuse (not supported)
117
self.assertRaises(UnsupportedFormatError, bzrlib.branch.Branch.open, self.get_url())
172
self.assertRaises(UnsupportedFormatError, Branch.open, self.get_url())
173
self.make_branch_and_tree('foo')
118
174
# but open_downlevel will work
119
175
self.assertEqual(format.open(dir), bzrdir.BzrDir.open(self.get_url()).open_branch(unsupported=True))
120
176
# unregister the format
121
bzrlib.branch.BranchFormat.unregister_format(format)
177
BranchFormat.unregister_format(format)
178
self.make_branch_and_tree('bar')
181
class TestBranch6(TestCaseWithTransport):
183
def test_creation(self):
184
format = BzrDirMetaFormat1()
185
format.set_branch_format(_mod_branch.BzrBranchFormat6())
186
branch = self.make_branch('a', format=format)
187
self.assertIsInstance(branch, _mod_branch.BzrBranch6)
188
branch = self.make_branch('b', format='dirstate-tags')
189
self.assertIsInstance(branch, _mod_branch.BzrBranch6)
190
branch = _mod_branch.Branch.open('a')
191
self.assertIsInstance(branch, _mod_branch.BzrBranch6)
193
def test_layout(self):
194
branch = self.make_branch('a', format='dirstate-tags')
195
self.failUnlessExists('a/.bzr/branch/last-revision')
196
self.failIfExists('a/.bzr/branch/revision-history')
198
def test_config(self):
199
"""Ensure that all configuration data is stored in the branch"""
200
branch = self.make_branch('a', format='dirstate-tags')
201
branch.set_parent('http://bazaar-vcs.org')
202
self.failIfExists('a/.bzr/branch/parent')
203
self.assertEqual('http://bazaar-vcs.org', branch.get_parent())
204
branch.set_push_location('sftp://bazaar-vcs.org')
205
config = branch.get_config()._get_branch_data_config()
206
self.assertEqual('sftp://bazaar-vcs.org',
207
config.get_user_option('push_location'))
208
branch.set_bound_location('ftp://bazaar-vcs.org')
209
self.failIfExists('a/.bzr/branch/bound')
210
self.assertEqual('ftp://bazaar-vcs.org', branch.get_bound_location())
212
def test_set_revision_history(self):
213
tree = self.make_branch_and_memory_tree('.',
214
format='dirstate-tags')
218
tree.commit('foo', rev_id='foo')
219
tree.commit('bar', rev_id='bar')
220
tree.branch.set_revision_history(['foo', 'bar'])
221
tree.branch.set_revision_history(['foo'])
222
self.assertRaises(errors.NotLefthandHistory,
223
tree.branch.set_revision_history, ['bar'])
227
def test_append_revision(self):
228
tree = self.make_branch_and_tree('branch1',
229
format='dirstate-tags')
232
tree.commit('foo', rev_id='foo')
233
tree.commit('bar', rev_id='bar')
234
tree.commit('baz', rev_id='baz')
235
tree.set_last_revision('bar')
236
tree.branch.set_last_revision_info(2, 'bar')
237
tree.commit('qux', rev_id='qux')
238
tree.add_parent_tree_id('baz')
239
tree.commit('qux', rev_id='quxx')
240
tree.branch.set_last_revision_info(0, 'null:')
241
self.assertRaises(errors.NotLeftParentDescendant,
242
tree.branch.append_revision, 'bar')
243
tree.branch.append_revision('foo')
244
self.assertRaises(errors.NotLeftParentDescendant,
245
tree.branch.append_revision, 'baz')
246
tree.branch.append_revision('bar')
247
tree.branch.append_revision('baz')
248
self.assertRaises(errors.NotLeftParentDescendant,
249
tree.branch.append_revision, 'quxx')
253
def do_checkout_test(self, lightweight=False):
254
tree = self.make_branch_and_tree('source', format='dirstate-with-subtree')
255
subtree = self.make_branch_and_tree('source/subtree',
256
format='dirstate-with-subtree')
257
subsubtree = self.make_branch_and_tree('source/subtree/subsubtree',
258
format='dirstate-with-subtree')
259
self.build_tree(['source/subtree/file',
260
'source/subtree/subsubtree/file'])
261
subsubtree.add('file')
263
subtree.add_reference(subsubtree)
264
tree.add_reference(subtree)
265
tree.commit('a revision')
266
subtree.commit('a subtree file')
267
subsubtree.commit('a subsubtree file')
268
tree.branch.create_checkout('target', lightweight=lightweight)
269
self.failUnlessExists('target')
270
self.failUnlessExists('target/subtree')
271
self.failUnlessExists('target/subtree/file')
272
self.failUnlessExists('target/subtree/subsubtree/file')
273
subbranch = _mod_branch.Branch.open('target/subtree/subsubtree')
275
self.assertEndsWith(subbranch.base, 'source/subtree/subsubtree/')
277
self.assertEndsWith(subbranch.base, 'target/subtree/subsubtree/')
280
def test_checkout_with_references(self):
281
self.do_checkout_test()
283
def test_light_checkout_with_references(self):
284
self.do_checkout_test(lightweight=True)
286
def test_set_push(self):
287
branch = self.make_branch('source', format='dirstate-tags')
288
branch.get_config().set_user_option('push_location', 'old',
289
store=config.STORE_LOCATION)
292
warnings.append(args[0] % args[1:])
293
_warning = trace.warning
294
trace.warning = warning
296
branch.set_push_location('new')
298
trace.warning = _warning
299
self.assertEqual(warnings[0], 'Value "new" is masked by "old" from '
124
302
class TestBranchReference(TestCaseWithTransport):
125
303
"""Tests for the branch reference facility."""
133
311
target_branch = dir.create_branch()
134
312
t.mkdir('branch')
135
313
branch_dir = bzrdirformat.initialize(self.get_url('branch'))
136
made_branch = bzrlib.branch.BranchReferenceFormat().initialize(branch_dir, target_branch)
314
made_branch = BranchReferenceFormat().initialize(branch_dir, target_branch)
137
315
self.assertEqual(made_branch.base, target_branch.base)
138
316
opened_branch = branch_dir.open_branch()
139
317
self.assertEqual(opened_branch.base, target_branch.base)
319
def test_get_reference(self):
320
"""For a BranchReference, get_reference should reutrn the location."""
321
branch = self.make_branch('target')
322
checkout = branch.create_checkout('checkout', lightweight=True)
323
reference_url = branch.bzrdir.root_transport.abspath('') + '/'
324
# if the api for create_checkout changes to return different checkout types
325
# then this file read will fail.
326
self.assertFileEqual(reference_url, 'checkout/.bzr/branch/location')
327
self.assertEqual(reference_url,
328
_mod_branch.BranchReferenceFormat().get_reference(checkout.bzrdir))
331
class TestHooks(TestCase):
333
def test_constructor(self):
334
"""Check that creating a BranchHooks instance has the right defaults."""
335
hooks = BranchHooks()
336
self.assertTrue("set_rh" in hooks, "set_rh not in %s" % hooks)
337
self.assertTrue("post_push" in hooks, "post_push not in %s" % hooks)
338
self.assertTrue("post_commit" in hooks, "post_commit not in %s" % hooks)
339
self.assertTrue("post_pull" in hooks, "post_pull not in %s" % hooks)
340
self.assertTrue("post_uncommit" in hooks, "post_uncommit not in %s" % hooks)
342
def test_installed_hooks_are_BranchHooks(self):
343
"""The installed hooks object should be a BranchHooks."""
344
# the installed hooks are saved in self._preserved_hooks.
345
self.assertIsInstance(self._preserved_hooks[_mod_branch.Branch], BranchHooks)
348
class TestPullResult(TestCase):
350
def test_pull_result_to_int(self):
351
# to support old code, the pull result can be used as an int
355
# this usage of results is not recommended for new code (because it
356
# doesn't describe very well what happened), but for api stability
357
# it's still supported
358
a = "%d revisions pulled" % r
359
self.assertEqual(a, "10 revisions pulled")