86
87
self.assertIsDirectory('.bzr/branch/lock/held', t)
88
89
def test_set_push_location(self):
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')
90
conf = config.LocationConfig.from_string('# comment\n', '.', save=True)
102
92
branch = self.make_branch('.', format='knit')
103
93
branch.set_push_location('foo')
123
113
"""See BzrBranchFormat.get_format_string()."""
124
114
return "Sample branch format."
126
def initialize(self, a_bzrdir):
116
def initialize(self, a_bzrdir, name=None):
127
117
"""Format 4 branches cannot be created."""
128
t = a_bzrdir.get_branch_transport(self)
118
t = a_bzrdir.get_branch_transport(self, name=name)
129
119
t.put_bytes('format', self.get_format_string())
130
120
return 'A branch'
132
122
def is_supported(self):
135
def open(self, transport, _found=False, ignore_fallbacks=False):
125
def open(self, transport, name=None, _found=False, ignore_fallbacks=False):
136
126
return "opened branch."
129
# Demonstrating how lazy loading is often implemented:
130
# A constant string is created.
131
SampleSupportedBranchFormatString = "Sample supported branch format."
133
# And the format class can then reference the constant to avoid skew.
134
class SampleSupportedBranchFormat(_mod_branch.BranchFormat):
135
"""A sample supported format."""
137
def get_format_string(self):
138
"""See BzrBranchFormat.get_format_string()."""
139
return SampleSupportedBranchFormatString
141
def initialize(self, a_bzrdir, name=None):
142
t = a_bzrdir.get_branch_transport(self, name=name)
143
t.put_bytes('format', self.get_format_string())
146
def open(self, transport, name=None, _found=False, ignore_fallbacks=False):
147
return "opened supported branch."
139
150
class TestBzrBranchFormat(tests.TestCaseWithTransport):
140
151
"""Tests for the BzrBranchFormat facility."""
152
163
self.failUnless(isinstance(found_format, format.__class__))
153
164
check_format(_mod_branch.BzrBranchFormat5(), "bar")
166
def test_find_format_factory(self):
167
dir = bzrdir.BzrDirMetaFormat1().initialize(self.get_url())
168
SampleSupportedBranchFormat().initialize(dir)
169
factory = _mod_branch.MetaDirBranchFormatFactory(
170
SampleSupportedBranchFormatString,
171
"bzrlib.tests.test_branch", "SampleSupportedBranchFormat")
172
_mod_branch.BranchFormat.register_format(factory)
173
self.addCleanup(_mod_branch.BranchFormat.unregister_format, factory)
174
b = _mod_branch.Branch.open(self.get_url())
175
self.assertEqual(b, "opened supported branch.")
155
177
def test_find_format_not_branch(self):
156
178
dir = bzrdir.BzrDirMetaFormat1().initialize(self.get_url())
157
179
self.assertRaises(errors.NotBranchError,
186
208
self.make_branch_and_tree('bar')
211
#Used by TestMetaDirBranchFormatFactory
212
FakeLazyFormat = None
215
class TestMetaDirBranchFormatFactory(tests.TestCase):
217
def test_get_format_string_does_not_load(self):
218
"""Formats have a static format string."""
219
factory = _mod_branch.MetaDirBranchFormatFactory("yo", None, None)
220
self.assertEqual("yo", factory.get_format_string())
222
def test_call_loads(self):
223
# __call__ is used by the network_format_registry interface to get a
225
global FakeLazyFormat
227
factory = _mod_branch.MetaDirBranchFormatFactory(None,
228
"bzrlib.tests.test_branch", "FakeLazyFormat")
229
self.assertRaises(AttributeError, factory)
231
def test_call_returns_call_of_referenced_object(self):
232
global FakeLazyFormat
233
FakeLazyFormat = lambda:'called'
234
factory = _mod_branch.MetaDirBranchFormatFactory(None,
235
"bzrlib.tests.test_branch", "FakeLazyFormat")
236
self.assertEqual('called', factory())
189
239
class TestBranch67(object):
190
240
"""Common tests for both branch 6 and 7 which are mostly the same."""
476
530
self.assertIsInstance(self._preserved_hooks[_mod_branch.Branch][1],
477
531
_mod_branch.BranchHooks)
533
def test_post_branch_init_hook(self):
535
_mod_branch.Branch.hooks.install_named_hook('post_branch_init',
537
self.assertLength(0, calls)
538
branch = self.make_branch('a')
539
self.assertLength(1, calls)
541
self.assertIsInstance(params, _mod_branch.BranchInitHookParams)
542
self.assertTrue(hasattr(params, 'bzrdir'))
543
self.assertTrue(hasattr(params, 'branch'))
545
def test_post_branch_init_hook_repr(self):
547
_mod_branch.Branch.hooks.install_named_hook('post_branch_init',
548
lambda params: param_reprs.append(repr(params)), None)
549
branch = self.make_branch('a')
550
self.assertLength(1, param_reprs)
551
param_repr = param_reprs[0]
552
self.assertStartsWith(param_repr, '<BranchInitHookParams of ')
554
def test_post_switch_hook(self):
555
from bzrlib import switch
557
_mod_branch.Branch.hooks.install_named_hook('post_switch',
559
tree = self.make_branch_and_tree('branch-1')
560
self.build_tree(['branch-1/file-1'])
563
to_branch = tree.bzrdir.sprout('branch-2').open_branch()
564
self.build_tree(['branch-1/file-2'])
566
tree.remove('file-1')
568
checkout = tree.branch.create_checkout('checkout')
569
self.assertLength(0, calls)
570
switch.switch(checkout.bzrdir, to_branch)
571
self.assertLength(1, calls)
573
self.assertIsInstance(params, _mod_branch.SwitchHookParams)
574
self.assertTrue(hasattr(params, 'to_branch'))
575
self.assertTrue(hasattr(params, 'revision_id'))
578
class TestBranchOptions(tests.TestCaseWithTransport):
581
super(TestBranchOptions, self).setUp()
582
self.branch = self.make_branch('.')
583
self.config = self.branch.get_config()
585
def check_append_revisions_only(self, expected_value, value=None):
586
"""Set append_revisions_only in config and check its interpretation."""
587
if value is not None:
588
self.config.set_user_option('append_revisions_only', value)
589
self.assertEqual(expected_value,
590
self.branch._get_append_revisions_only())
592
def test_valid_append_revisions_only(self):
593
self.assertEquals(None,
594
self.config.get_user_option('append_revisions_only'))
595
self.check_append_revisions_only(None)
596
self.check_append_revisions_only(False, 'False')
597
self.check_append_revisions_only(True, 'True')
598
# The following values will cause compatibility problems on projects
599
# using older bzr versions (<2.2) but are accepted
600
self.check_append_revisions_only(False, 'false')
601
self.check_append_revisions_only(True, 'true')
603
def test_invalid_append_revisions_only(self):
604
"""Ensure warning is noted on invalid settings"""
607
self.warnings.append(args[0] % args[1:])
608
self.overrideAttr(trace, 'warning', warning)
609
self.check_append_revisions_only(None, 'not-a-bool')
610
self.assertLength(1, self.warnings)
612
'Value "not-a-bool" is not a boolean for "append_revisions_only"',
480
616
class TestPullResult(tests.TestCase):