86
86
self.assertIsDirectory('.bzr/branch/lock/held', t)
88
88
def test_set_push_location(self):
89
conf = config.LocationConfig.from_string('# comment\n', '.', save=True)
89
from bzrlib.config import (locations_config_filename,
90
ensure_config_dir_exists)
91
ensure_config_dir_exists()
92
fn = locations_config_filename()
93
# write correct newlines to locations.conf
94
# by default ConfigObj uses native line-endings for new files
95
# but uses already existing line-endings if file is not empty
98
f.write('# comment\n')
91
102
branch = self.make_branch('.', format='knit')
92
103
branch.set_push_location('foo')
125
136
return "opened branch."
128
# Demonstrating how lazy loading is often implemented:
129
# A constant string is created.
130
SampleSupportedBranchFormatString = "Sample supported branch format."
132
# And the format class can then reference the constant to avoid skew.
133
class SampleSupportedBranchFormat(_mod_branch.BranchFormat):
134
"""A sample supported format."""
136
def get_format_string(self):
137
"""See BzrBranchFormat.get_format_string()."""
138
return SampleSupportedBranchFormatString
140
def initialize(self, a_bzrdir, name=None):
141
t = a_bzrdir.get_branch_transport(self, name=name)
142
t.put_bytes('format', self.get_format_string())
145
def open(self, transport, name=None, _found=False, ignore_fallbacks=False):
146
return "opened supported branch."
149
139
class TestBzrBranchFormat(tests.TestCaseWithTransport):
150
140
"""Tests for the BzrBranchFormat facility."""
162
152
self.failUnless(isinstance(found_format, format.__class__))
163
153
check_format(_mod_branch.BzrBranchFormat5(), "bar")
165
def test_find_format_factory(self):
166
dir = bzrdir.BzrDirMetaFormat1().initialize(self.get_url())
167
SampleSupportedBranchFormat().initialize(dir)
168
factory = _mod_branch.MetaDirBranchFormatFactory(
169
SampleSupportedBranchFormatString,
170
"bzrlib.tests.test_branch", "SampleSupportedBranchFormat")
171
_mod_branch.BranchFormat.register_format(factory)
172
self.addCleanup(_mod_branch.BranchFormat.unregister_format, factory)
173
b = _mod_branch.Branch.open(self.get_url())
174
self.assertEqual(b, "opened supported branch.")
176
155
def test_find_format_not_branch(self):
177
156
dir = bzrdir.BzrDirMetaFormat1().initialize(self.get_url())
178
157
self.assertRaises(errors.NotBranchError,
207
186
self.make_branch_and_tree('bar')
210
#Used by TestMetaDirBranchFormatFactory
211
FakeLazyFormat = None
214
class TestMetaDirBranchFormatFactory(tests.TestCase):
216
def test_get_format_string_does_not_load(self):
217
"""Formats have a static format string."""
218
factory = _mod_branch.MetaDirBranchFormatFactory("yo", None, None)
219
self.assertEqual("yo", factory.get_format_string())
221
def test_call_loads(self):
222
# __call__ is used by the network_format_registry interface to get a
224
global FakeLazyFormat
226
factory = _mod_branch.MetaDirBranchFormatFactory(None,
227
"bzrlib.tests.test_branch", "FakeLazyFormat")
228
self.assertRaises(AttributeError, factory)
230
def test_call_returns_call_of_referenced_object(self):
231
global FakeLazyFormat
232
FakeLazyFormat = lambda:'called'
233
factory = _mod_branch.MetaDirBranchFormatFactory(None,
234
"bzrlib.tests.test_branch", "FakeLazyFormat")
235
self.assertEqual('called', factory())
238
189
class TestBranch67(object):
239
190
"""Common tests for both branch 6 and 7 which are mostly the same."""
529
476
self.assertIsInstance(self._preserved_hooks[_mod_branch.Branch][1],
530
477
_mod_branch.BranchHooks)
532
def test_post_branch_init_hook(self):
534
_mod_branch.Branch.hooks.install_named_hook('post_branch_init',
536
self.assertLength(0, calls)
537
branch = self.make_branch('a')
538
self.assertLength(1, calls)
540
self.assertIsInstance(params, _mod_branch.BranchInitHookParams)
541
self.assertTrue(hasattr(params, 'bzrdir'))
542
self.assertTrue(hasattr(params, 'branch'))
544
def test_post_branch_init_hook_repr(self):
546
_mod_branch.Branch.hooks.install_named_hook('post_branch_init',
547
lambda params: param_reprs.append(repr(params)), None)
548
branch = self.make_branch('a')
549
self.assertLength(1, param_reprs)
550
param_repr = param_reprs[0]
551
self.assertStartsWith(param_repr, '<BranchInitHookParams of ')
553
def test_post_switch_hook(self):
554
from bzrlib import switch
556
_mod_branch.Branch.hooks.install_named_hook('post_switch',
558
tree = self.make_branch_and_tree('branch-1')
559
self.build_tree(['branch-1/file-1'])
562
to_branch = tree.bzrdir.sprout('branch-2').open_branch()
563
self.build_tree(['branch-1/file-2'])
565
tree.remove('file-1')
567
checkout = tree.branch.create_checkout('checkout')
568
self.assertLength(0, calls)
569
switch.switch(checkout.bzrdir, to_branch)
570
self.assertLength(1, calls)
572
self.assertIsInstance(params, _mod_branch.SwitchHookParams)
573
self.assertTrue(hasattr(params, 'to_branch'))
574
self.assertTrue(hasattr(params, 'revision_id'))
577
class TestBranchOptions(tests.TestCaseWithTransport):
580
super(TestBranchOptions, self).setUp()
581
self.branch = self.make_branch('.')
582
self.config = self.branch.get_config()
584
def check_append_revisions_only(self, expected_value, value=None):
585
"""Set append_revisions_only in config and check its interpretation."""
586
if value is not None:
587
self.config.set_user_option('append_revisions_only', value)
588
self.assertEqual(expected_value,
589
self.branch._get_append_revisions_only())
591
def test_valid_append_revisions_only(self):
592
self.assertEquals(None,
593
self.config.get_user_option('append_revisions_only'))
594
self.check_append_revisions_only(None)
595
self.check_append_revisions_only(False, 'False')
596
self.check_append_revisions_only(True, 'True')
597
# The following values will cause compatibility problems on projects
598
# using older bzr versions (<2.2) but are accepted
599
self.check_append_revisions_only(False, 'false')
600
self.check_append_revisions_only(True, 'true')
602
def test_invalid_append_revisions_only(self):
603
"""Ensure warning is noted on invalid settings"""
606
self.warnings.append(args[0] % args[1:])
607
self.overrideAttr(trace, 'warning', warning)
608
self.check_append_revisions_only(None, 'not-a-bool')
609
self.assertLength(1, self.warnings)
611
'Value "not-a-bool" is not a boolean for "append_revisions_only"',
615
480
class TestPullResult(tests.TestCase):