1
# Copyright (C) 2005, 2006, 2007 Canonical Ltd
3
# This program is free software; you can redistribute it and/or modify
4
# it under the terms of the GNU General Public License as published by
5
# the Free Software Foundation; either version 2 of the License, or
6
# (at your option) any later version.
8
# This program is distributed in the hope that it will be useful,
9
# but WITHOUT ANY WARRANTY; without even the implied warranty of
10
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
11
# GNU General Public License for more details.
13
# You should have received a copy of the GNU General Public License
14
# along with this program; if not, write to the Free Software
15
# Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
17
"""Tests for the Branch facility that are not interface tests.
19
For interface tests see tests/branch_implementations/*.py.
21
For concrete class tests see this file, and for meta-branch tests
25
from StringIO import StringIO
28
branch as _mod_branch,
35
from bzrlib.branch import (
39
BranchReferenceFormat,
45
from bzrlib.bzrdir import (BzrDirMetaFormat1, BzrDirMeta1,
47
from bzrlib.errors import (NotBranchError,
50
UnsupportedFormatError,
53
from bzrlib.symbol_versioning import deprecated_in
54
from bzrlib.tests import TestCase, TestCaseWithTransport
55
from bzrlib.transport import get_transport
58
class TestDefaultFormat(TestCase):
60
def test_default_format(self):
61
# update this if you change the default branch format
62
self.assertIsInstance(BranchFormat.get_default_format(),
65
def test_default_format_is_same_as_bzrdir_default(self):
66
# XXX: it might be nice if there was only one place the default was
67
# set, but at the moment that's not true -- mbp 20070814 --
68
# https://bugs.launchpad.net/bzr/+bug/132376
69
self.assertEqual(BranchFormat.get_default_format(),
70
BzrDirFormat.get_default_format().get_branch_format())
72
def test_get_set_default_format(self):
73
# set the format and then set it back again
74
old_format = BranchFormat.get_default_format()
75
BranchFormat.set_default_format(SampleBranchFormat())
77
# the default branch format is used by the meta dir format
78
# which is not the default bzrdir format at this point
79
dir = BzrDirMetaFormat1().initialize('memory:///')
80
result = dir.create_branch()
81
self.assertEqual(result, 'A branch')
83
BranchFormat.set_default_format(old_format)
84
self.assertEqual(old_format, BranchFormat.get_default_format())
87
class TestBranchFormat5(TestCaseWithTransport):
88
"""Tests specific to branch format 5"""
90
def test_branch_format_5_uses_lockdir(self):
92
bzrdir = BzrDirMetaFormat1().initialize(url)
93
bzrdir.create_repository()
94
branch = bzrdir.create_branch()
95
t = self.get_transport()
96
self.log("branch instance is %r" % branch)
97
self.assert_(isinstance(branch, BzrBranch5))
98
self.assertIsDirectory('.', t)
99
self.assertIsDirectory('.bzr/branch', t)
100
self.assertIsDirectory('.bzr/branch/lock', t)
103
self.assertIsDirectory('.bzr/branch/lock/held', t)
107
def test_set_push_location(self):
108
from bzrlib.config import (locations_config_filename,
109
ensure_config_dir_exists)
110
ensure_config_dir_exists()
111
fn = locations_config_filename()
112
# write correct newlines to locations.conf
113
# by default ConfigObj uses native line-endings for new files
114
# but uses already existing line-endings if file is not empty
117
f.write('# comment\n')
121
branch = self.make_branch('.', format='knit')
122
branch.set_push_location('foo')
123
local_path = urlutils.local_path_from_url(branch.base[:-1])
124
self.assertFileEqual("# comment\n"
126
"push_location = foo\n"
127
"push_location:policy = norecurse\n" % local_path,
130
# TODO RBC 20051029 test getting a push location from a branch in a
131
# recursive section - that is, it appends the branch name.
133
def test_missing_revisions(self):
134
t1 = self.make_branch_and_tree('b1', format='knit')
135
rev1 = t1.commit('one')
136
t2 = t1.bzrdir.sprout('b2').open_workingtree()
137
rev2 = t1.commit('two')
138
rev3 = t1.commit('three')
140
self.assertEqual([rev2, rev3],
141
self.applyDeprecated(deprecated_in((1, 6, 0)),
142
t2.branch.missing_revisions, t1.branch))
145
self.applyDeprecated(deprecated_in((1, 6, 0)),
146
t2.branch.missing_revisions, t1.branch, stop_revision=1))
147
self.assertEqual([rev2],
148
self.applyDeprecated(deprecated_in((1, 6, 0)),
149
t2.branch.missing_revisions, t1.branch, stop_revision=2))
150
self.assertEqual([rev2, rev3],
151
self.applyDeprecated(deprecated_in((1, 6, 0)),
152
t2.branch.missing_revisions, t1.branch, stop_revision=3))
154
self.assertRaises(errors.NoSuchRevision,
155
self.applyDeprecated, deprecated_in((1, 6, 0)),
156
t2.branch.missing_revisions, t1.branch, stop_revision=4)
158
rev4 = t2.commit('four')
159
self.assertRaises(errors.DivergedBranches,
160
self.applyDeprecated, deprecated_in((1, 6, 0)),
161
t2.branch.missing_revisions, t1.branch)
166
class SampleBranchFormat(BranchFormat):
169
this format is initializable, unsupported to aid in testing the
170
open and open_downlevel routines.
173
def get_format_string(self):
174
"""See BzrBranchFormat.get_format_string()."""
175
return "Sample branch format."
177
def initialize(self, a_bzrdir):
178
"""Format 4 branches cannot be created."""
179
t = a_bzrdir.get_branch_transport(self)
180
t.put_bytes('format', self.get_format_string())
183
def is_supported(self):
186
def open(self, transport, _found=False):
187
return "opened branch."
190
class TestBzrBranchFormat(TestCaseWithTransport):
191
"""Tests for the BzrBranchFormat facility."""
193
def test_find_format(self):
194
# is the right format object found for a branch?
195
# create a branch with a few known format objects.
196
# this is not quite the same as
197
self.build_tree(["foo/", "bar/"])
198
def check_format(format, url):
199
dir = format._matchingbzrdir.initialize(url)
200
dir.create_repository()
201
format.initialize(dir)
202
found_format = BranchFormat.find_format(dir)
203
self.failUnless(isinstance(found_format, format.__class__))
204
check_format(BzrBranchFormat5(), "bar")
206
def test_find_format_not_branch(self):
207
dir = bzrdir.BzrDirMetaFormat1().initialize(self.get_url())
208
self.assertRaises(NotBranchError,
209
BranchFormat.find_format,
212
def test_find_format_unknown_format(self):
213
dir = bzrdir.BzrDirMetaFormat1().initialize(self.get_url())
214
SampleBranchFormat().initialize(dir)
215
self.assertRaises(UnknownFormatError,
216
BranchFormat.find_format,
219
def test_register_unregister_format(self):
220
format = SampleBranchFormat()
222
dir = bzrdir.BzrDirMetaFormat1().initialize(self.get_url())
224
format.initialize(dir)
225
# register a format for it.
226
BranchFormat.register_format(format)
227
# which branch.Open will refuse (not supported)
228
self.assertRaises(UnsupportedFormatError, Branch.open, self.get_url())
229
self.make_branch_and_tree('foo')
230
# but open_downlevel will work
231
self.assertEqual(format.open(dir), bzrdir.BzrDir.open(self.get_url()).open_branch(unsupported=True))
232
# unregister the format
233
BranchFormat.unregister_format(format)
234
self.make_branch_and_tree('bar')
237
class TestBranch6(TestCaseWithTransport):
239
def test_creation(self):
240
format = BzrDirMetaFormat1()
241
format.set_branch_format(_mod_branch.BzrBranchFormat6())
242
branch = self.make_branch('a', format=format)
243
self.assertIsInstance(branch, _mod_branch.BzrBranch6)
244
branch = self.make_branch('b', format='dirstate-tags')
245
self.assertIsInstance(branch, _mod_branch.BzrBranch6)
246
branch = _mod_branch.Branch.open('a')
247
self.assertIsInstance(branch, _mod_branch.BzrBranch6)
249
def test_layout(self):
250
branch = self.make_branch('a', format='dirstate-tags')
251
self.failUnlessExists('a/.bzr/branch/last-revision')
252
self.failIfExists('a/.bzr/branch/revision-history')
254
def test_config(self):
255
"""Ensure that all configuration data is stored in the branch"""
256
branch = self.make_branch('a', format='dirstate-tags')
257
branch.set_parent('http://bazaar-vcs.org')
258
self.failIfExists('a/.bzr/branch/parent')
259
self.assertEqual('http://bazaar-vcs.org', branch.get_parent())
260
branch.set_push_location('sftp://bazaar-vcs.org')
261
config = branch.get_config()._get_branch_data_config()
262
self.assertEqual('sftp://bazaar-vcs.org',
263
config.get_user_option('push_location'))
264
branch.set_bound_location('ftp://bazaar-vcs.org')
265
self.failIfExists('a/.bzr/branch/bound')
266
self.assertEqual('ftp://bazaar-vcs.org', branch.get_bound_location())
268
def test_set_revision_history(self):
269
tree = self.make_branch_and_memory_tree('.',
270
format='dirstate-tags')
274
tree.commit('foo', rev_id='foo')
275
tree.commit('bar', rev_id='bar')
276
tree.branch.set_revision_history(['foo', 'bar'])
277
tree.branch.set_revision_history(['foo'])
278
self.assertRaises(errors.NotLefthandHistory,
279
tree.branch.set_revision_history, ['bar'])
283
def do_checkout_test(self, lightweight=False):
284
tree = self.make_branch_and_tree('source', format='dirstate-with-subtree')
285
subtree = self.make_branch_and_tree('source/subtree',
286
format='dirstate-with-subtree')
287
subsubtree = self.make_branch_and_tree('source/subtree/subsubtree',
288
format='dirstate-with-subtree')
289
self.build_tree(['source/subtree/file',
290
'source/subtree/subsubtree/file'])
291
subsubtree.add('file')
293
subtree.add_reference(subsubtree)
294
tree.add_reference(subtree)
295
tree.commit('a revision')
296
subtree.commit('a subtree file')
297
subsubtree.commit('a subsubtree file')
298
tree.branch.create_checkout('target', lightweight=lightweight)
299
self.failUnlessExists('target')
300
self.failUnlessExists('target/subtree')
301
self.failUnlessExists('target/subtree/file')
302
self.failUnlessExists('target/subtree/subsubtree/file')
303
subbranch = _mod_branch.Branch.open('target/subtree/subsubtree')
305
self.assertEndsWith(subbranch.base, 'source/subtree/subsubtree/')
307
self.assertEndsWith(subbranch.base, 'target/subtree/subsubtree/')
309
def test_checkout_with_references(self):
310
self.do_checkout_test()
312
def test_light_checkout_with_references(self):
313
self.do_checkout_test(lightweight=True)
315
def test_set_push(self):
316
branch = self.make_branch('source', format='dirstate-tags')
317
branch.get_config().set_user_option('push_location', 'old',
318
store=config.STORE_LOCATION)
321
warnings.append(args[0] % args[1:])
322
_warning = trace.warning
323
trace.warning = warning
325
branch.set_push_location('new')
327
trace.warning = _warning
328
self.assertEqual(warnings[0], 'Value "new" is masked by "old" from '
332
class TestBranchReference(TestCaseWithTransport):
333
"""Tests for the branch reference facility."""
335
def test_create_open_reference(self):
336
bzrdirformat = bzrdir.BzrDirMetaFormat1()
337
t = get_transport(self.get_url('.'))
339
dir = bzrdirformat.initialize(self.get_url('repo'))
340
dir.create_repository()
341
target_branch = dir.create_branch()
343
branch_dir = bzrdirformat.initialize(self.get_url('branch'))
344
made_branch = BranchReferenceFormat().initialize(branch_dir, target_branch)
345
self.assertEqual(made_branch.base, target_branch.base)
346
opened_branch = branch_dir.open_branch()
347
self.assertEqual(opened_branch.base, target_branch.base)
349
def test_get_reference(self):
350
"""For a BranchReference, get_reference should reutrn the location."""
351
branch = self.make_branch('target')
352
checkout = branch.create_checkout('checkout', lightweight=True)
353
reference_url = branch.bzrdir.root_transport.abspath('') + '/'
354
# if the api for create_checkout changes to return different checkout types
355
# then this file read will fail.
356
self.assertFileEqual(reference_url, 'checkout/.bzr/branch/location')
357
self.assertEqual(reference_url,
358
_mod_branch.BranchReferenceFormat().get_reference(checkout.bzrdir))
361
class TestHooks(TestCase):
363
def test_constructor(self):
364
"""Check that creating a BranchHooks instance has the right defaults."""
365
hooks = BranchHooks()
366
self.assertTrue("set_rh" in hooks, "set_rh not in %s" % hooks)
367
self.assertTrue("post_push" in hooks, "post_push not in %s" % hooks)
368
self.assertTrue("post_commit" in hooks, "post_commit not in %s" % hooks)
369
self.assertTrue("pre_commit" in hooks, "pre_commit not in %s" % hooks)
370
self.assertTrue("post_pull" in hooks, "post_pull not in %s" % hooks)
371
self.assertTrue("post_uncommit" in hooks, "post_uncommit not in %s" % hooks)
372
self.assertTrue("post_change_branch_tip" in hooks,
373
"post_change_branch_tip not in %s" % hooks)
375
def test_installed_hooks_are_BranchHooks(self):
376
"""The installed hooks object should be a BranchHooks."""
377
# the installed hooks are saved in self._preserved_hooks.
378
self.assertIsInstance(self._preserved_hooks[_mod_branch.Branch], BranchHooks)
381
class TestPullResult(TestCase):
383
def test_pull_result_to_int(self):
384
# to support old code, the pull result can be used as an int
388
# this usage of results is not recommended for new code (because it
389
# doesn't describe very well what happened), but for api stability
390
# it's still supported
391
a = "%d revisions pulled" % r
392
self.assertEqual(a, "10 revisions pulled")