~bzr-pqm/bzr/bzr.dev

« back to all changes in this revision

Viewing changes to bzrlib/tests/test_branch.py

  • Committer: Andrew Bennetts
  • Date: 2007-03-26 06:24:01 UTC
  • mto: This revision was merged to the branch mainline in revision 2376.
  • Revision ID: andrew.bennetts@canonical.com-20070326062401-k3nbefzje5332jaf
Deal with review comments from Robert:

  * Add my name to the NEWS file
  * Move the test case to a new module in branch_implementations
  * Remove revision_history cruft from identitymap and test_identitymap
  * Improve some docstrings

Also, this fixes a bug where revision_history was not returning a copy of the
cached data, allowing the cache to be corrupted.

Show diffs side-by-side

added added

removed removed

Lines of Context:
27
27
from bzrlib import (
28
28
    branch as _mod_branch,
29
29
    bzrdir,
30
 
    config,
31
30
    errors,
32
 
    trace,
33
31
    urlutils,
34
32
    )
35
33
from bzrlib.branch import (
39
37
    BranchReferenceFormat,
40
38
    BzrBranch5,
41
39
    BzrBranchFormat5,
42
 
    BzrBranchFormat6,
43
40
    PullResult,
44
41
    )
45
42
from bzrlib.bzrdir import (BzrDirMetaFormat1, BzrDirMeta1, 
53
50
from bzrlib.tests import TestCase, TestCaseWithTransport
54
51
from bzrlib.transport import get_transport
55
52
 
56
 
 
57
53
class TestDefaultFormat(TestCase):
58
54
 
59
 
    def test_default_format(self):
60
 
        # update this if you change the default branch format
61
 
        self.assertIsInstance(BranchFormat.get_default_format(),
62
 
                BzrBranchFormat6)
63
 
 
64
 
    def test_default_format_is_same_as_bzrdir_default(self):
65
 
        # XXX: it might be nice if there was only one place the default was
66
 
        # set, but at the moment that's not true -- mbp 20070814 -- 
67
 
        # https://bugs.launchpad.net/bzr/+bug/132376
68
 
        self.assertEqual(BranchFormat.get_default_format(),
69
 
                BzrDirFormat.get_default_format().get_branch_format())
70
 
 
71
55
    def test_get_set_default_format(self):
72
 
        # set the format and then set it back again
73
56
        old_format = BranchFormat.get_default_format()
 
57
        # default is 5
 
58
        self.assertTrue(isinstance(old_format, BzrBranchFormat5))
74
59
        BranchFormat.set_default_format(SampleBranchFormat())
75
60
        try:
76
61
            # the default branch format is used by the meta dir format
108
93
                                   ensure_config_dir_exists)
109
94
        ensure_config_dir_exists()
110
95
        fn = locations_config_filename()
111
 
        # write correct newlines to locations.conf
112
 
        # by default ConfigObj uses native line-endings for new files
113
 
        # but uses already existing line-endings if file is not empty
114
 
        f = open(fn, 'wb')
115
 
        try:
116
 
            f.write('# comment\n')
117
 
        finally:
118
 
            f.close()
119
 
 
120
96
        branch = self.make_branch('.', format='knit')
121
97
        branch.set_push_location('foo')
122
98
        local_path = urlutils.local_path_from_url(branch.base[:-1])
123
 
        self.assertFileEqual("# comment\n"
124
 
                             "[%s]\n"
 
99
        self.assertFileEqual("[%s]\n"
125
100
                             "push_location = foo\n"
126
 
                             "push_location:policy = norecurse\n" % local_path,
 
101
                             "push_location:policy = norecurse" % local_path,
127
102
                             fn)
128
103
 
129
104
    # TODO RBC 20051029 test getting a push location from a branch in a
201
176
        self.make_branch_and_tree('bar')
202
177
 
203
178
 
204
 
class TestBranch67(object):
205
 
    """Common tests for both branch 6 and 7 which are mostly the same."""
206
 
 
207
 
    def get_format_name(self):
208
 
        raise NotImplementedError(self.get_format_name)
209
 
 
210
 
    def get_format_name_subtree(self):
211
 
        raise NotImplementedError(self.get_format_name)
212
 
 
213
 
    def get_class(self):
214
 
        raise NotImplementedError(self.get_class)
 
179
class TestBranch6(TestCaseWithTransport):
215
180
 
216
181
    def test_creation(self):
217
182
        format = BzrDirMetaFormat1()
218
183
        format.set_branch_format(_mod_branch.BzrBranchFormat6())
219
184
        branch = self.make_branch('a', format=format)
220
 
        self.assertIsInstance(branch, self.get_class())
221
 
        branch = self.make_branch('b', format=self.get_format_name())
222
 
        self.assertIsInstance(branch, self.get_class())
 
185
        self.assertIsInstance(branch, _mod_branch.BzrBranch6)
 
186
        branch = self.make_branch('b', format='dirstate-tags')
 
187
        self.assertIsInstance(branch, _mod_branch.BzrBranch6)
223
188
        branch = _mod_branch.Branch.open('a')
224
 
        self.assertIsInstance(branch, self.get_class())
 
189
        self.assertIsInstance(branch, _mod_branch.BzrBranch6)
225
190
 
226
191
    def test_layout(self):
227
 
        branch = self.make_branch('a', format=self.get_format_name())
 
192
        branch = self.make_branch('a', format='dirstate-tags')
228
193
        self.failUnlessExists('a/.bzr/branch/last-revision')
229
194
        self.failIfExists('a/.bzr/branch/revision-history')
230
195
 
231
196
    def test_config(self):
232
197
        """Ensure that all configuration data is stored in the branch"""
233
 
        branch = self.make_branch('a', format=self.get_format_name())
 
198
        branch = self.make_branch('a', format='dirstate-tags')
234
199
        branch.set_parent('http://bazaar-vcs.org')
235
200
        self.failIfExists('a/.bzr/branch/parent')
236
201
        self.assertEqual('http://bazaar-vcs.org', branch.get_parent())
243
208
        self.assertEqual('ftp://bazaar-vcs.org', branch.get_bound_location())
244
209
 
245
210
    def test_set_revision_history(self):
246
 
        builder = self.make_branch_builder('.', format=self.get_format_name())
247
 
        builder.build_snapshot('foo', None,
248
 
            [('add', ('', None, 'directory', None))],
249
 
            message='foo')
250
 
        builder.build_snapshot('bar', None, [], message='bar')
251
 
        branch = builder.get_branch()
252
 
        branch.lock_write()
253
 
        self.addCleanup(branch.unlock)
254
 
        branch.set_revision_history(['foo', 'bar'])
255
 
        branch.set_revision_history(['foo'])
256
 
        self.assertRaises(errors.NotLefthandHistory,
257
 
                          branch.set_revision_history, ['bar'])
 
211
        tree = self.make_branch_and_memory_tree('.',
 
212
            format='dirstate-tags')
 
213
        tree.lock_write()
 
214
        try:
 
215
            tree.add('.')
 
216
            tree.commit('foo', rev_id='foo')
 
217
            tree.commit('bar', rev_id='bar')
 
218
            tree.branch.set_revision_history(['foo', 'bar'])
 
219
            tree.branch.set_revision_history(['foo'])
 
220
            self.assertRaises(errors.NotLefthandHistory,
 
221
                              tree.branch.set_revision_history, ['bar'])
 
222
        finally:
 
223
            tree.unlock()
 
224
 
 
225
    def test_append_revision(self):
 
226
        tree = self.make_branch_and_tree('branch1',
 
227
            format='dirstate-tags')
 
228
        tree.lock_write()
 
229
        try:
 
230
            tree.commit('foo', rev_id='foo')
 
231
            tree.commit('bar', rev_id='bar')
 
232
            tree.commit('baz', rev_id='baz')
 
233
            tree.set_last_revision('bar')
 
234
            tree.branch.set_last_revision_info(2, 'bar')
 
235
            tree.commit('qux', rev_id='qux')
 
236
            tree.add_parent_tree_id('baz')
 
237
            tree.commit('qux', rev_id='quxx')
 
238
            tree.branch.set_last_revision_info(0, 'null:')
 
239
            self.assertRaises(errors.NotLeftParentDescendant,
 
240
                              tree.branch.append_revision, 'bar')
 
241
            tree.branch.append_revision('foo')
 
242
            self.assertRaises(errors.NotLeftParentDescendant,
 
243
                              tree.branch.append_revision, 'baz')
 
244
            tree.branch.append_revision('bar')
 
245
            tree.branch.append_revision('baz')
 
246
            self.assertRaises(errors.NotLeftParentDescendant,
 
247
                              tree.branch.append_revision, 'quxx')
 
248
        finally:
 
249
            tree.unlock()
258
250
 
259
251
    def do_checkout_test(self, lightweight=False):
260
 
        tree = self.make_branch_and_tree('source',
261
 
            format=self.get_format_name_subtree())
 
252
        tree = self.make_branch_and_tree('source', format='dirstate-with-subtree')
262
253
        subtree = self.make_branch_and_tree('source/subtree',
263
 
            format=self.get_format_name_subtree())
 
254
            format='dirstate-with-subtree')
264
255
        subsubtree = self.make_branch_and_tree('source/subtree/subsubtree',
265
 
            format=self.get_format_name_subtree())
 
256
            format='dirstate-with-subtree')
266
257
        self.build_tree(['source/subtree/file',
267
258
                         'source/subtree/subsubtree/file'])
268
259
        subsubtree.add('file')
283
274
        else:
284
275
            self.assertEndsWith(subbranch.base, 'target/subtree/subsubtree/')
285
276
 
 
277
 
286
278
    def test_checkout_with_references(self):
287
279
        self.do_checkout_test()
288
280
 
289
281
    def test_light_checkout_with_references(self):
290
282
        self.do_checkout_test(lightweight=True)
291
283
 
292
 
    def test_set_push(self):
293
 
        branch = self.make_branch('source', format=self.get_format_name())
294
 
        branch.get_config().set_user_option('push_location', 'old',
295
 
            store=config.STORE_LOCATION)
296
 
        warnings = []
297
 
        def warning(*args):
298
 
            warnings.append(args[0] % args[1:])
299
 
        _warning = trace.warning
300
 
        trace.warning = warning
301
 
        try:
302
 
            branch.set_push_location('new')
303
 
        finally:
304
 
            trace.warning = _warning
305
 
        self.assertEqual(warnings[0], 'Value "new" is masked by "old" from '
306
 
                         'locations.conf')
307
 
 
308
 
 
309
 
class TestBranch6(TestBranch67, TestCaseWithTransport):
310
 
 
311
 
    def get_class(self):
312
 
        return _mod_branch.BzrBranch6
313
 
 
314
 
    def get_format_name(self):
315
 
        return "dirstate-tags"
316
 
 
317
 
    def get_format_name_subtree(self):
318
 
        return "dirstate-with-subtree"
319
 
 
320
 
    def test_set_stacked_on_url_errors(self):
321
 
        branch = self.make_branch('a', format=self.get_format_name())
322
 
        self.assertRaises(errors.UnstackableBranchFormat,
323
 
            branch.set_stacked_on_url, None)
324
 
 
325
 
    def test_default_stacked_location(self):
326
 
        branch = self.make_branch('a', format=self.get_format_name())
327
 
        self.assertRaises(errors.UnstackableBranchFormat, branch.get_stacked_on_url)
328
 
 
329
 
 
330
 
class TestBranch7(TestBranch67, TestCaseWithTransport):
331
 
 
332
 
    def get_class(self):
333
 
        return _mod_branch.BzrBranch7
334
 
 
335
 
    def get_format_name(self):
336
 
        return "development"
337
 
 
338
 
    def get_format_name_subtree(self):
339
 
        return "development-subtree"
340
 
 
341
 
    def test_set_stacked_on_url_unstackable_repo(self):
342
 
        repo = self.make_repository('a', format='dirstate-tags')
343
 
        control = repo.bzrdir
344
 
        branch = _mod_branch.BzrBranchFormat7().initialize(control)
345
 
        target = self.make_branch('b')
346
 
        self.assertRaises(errors.UnstackableRepositoryFormat,
347
 
            branch.set_stacked_on_url, target.base)
348
 
 
349
 
    def test_clone_stacked_on_unstackable_repo(self):
350
 
        repo = self.make_repository('a', format='dirstate-tags')
351
 
        control = repo.bzrdir
352
 
        branch = _mod_branch.BzrBranchFormat7().initialize(control)
353
 
        # Calling clone should not raise UnstackableRepositoryFormat.
354
 
        cloned_bzrdir = control.clone('cloned')
355
 
 
356
 
    def _test_default_stacked_location(self):
357
 
        branch = self.make_branch('a', format=self.get_format_name())
358
 
        self.assertRaises(errors.NotStacked, branch.get_stacked_on_url)
359
 
 
360
 
    def test_stack_and_unstack(self):
361
 
        branch = self.make_branch('a', format=self.get_format_name())
362
 
        target = self.make_branch_and_tree('b', format=self.get_format_name())
363
 
        branch.set_stacked_on_url(target.branch.base)
364
 
        self.assertEqual(target.branch.base, branch.get_stacked_on_url())
365
 
        revid = target.commit('foo')
366
 
        self.assertTrue(branch.repository.has_revision(revid))
367
 
        branch.set_stacked_on_url(None)
368
 
        self.assertRaises(errors.NotStacked, branch.get_stacked_on_url)
369
 
        self.assertFalse(branch.repository.has_revision(revid))
370
 
 
371
 
    def test_open_opens_stacked_reference(self):
372
 
        branch = self.make_branch('a', format=self.get_format_name())
373
 
        target = self.make_branch_and_tree('b', format=self.get_format_name())
374
 
        branch.set_stacked_on_url(target.branch.base)
375
 
        branch = branch.bzrdir.open_branch()
376
 
        revid = target.commit('foo')
377
 
        self.assertTrue(branch.repository.has_revision(revid))
378
 
 
379
 
 
380
284
class TestBranchReference(TestCaseWithTransport):
381
285
    """Tests for the branch reference facility."""
382
286
 
394
298
        opened_branch = branch_dir.open_branch()
395
299
        self.assertEqual(opened_branch.base, target_branch.base)
396
300
 
397
 
    def test_get_reference(self):
398
 
        """For a BranchReference, get_reference should reutrn the location."""
399
 
        branch = self.make_branch('target')
400
 
        checkout = branch.create_checkout('checkout', lightweight=True)
401
 
        reference_url = branch.bzrdir.root_transport.abspath('') + '/'
402
 
        # if the api for create_checkout changes to return different checkout types
403
 
        # then this file read will fail.
404
 
        self.assertFileEqual(reference_url, 'checkout/.bzr/branch/location')
405
 
        self.assertEqual(reference_url,
406
 
            _mod_branch.BranchReferenceFormat().get_reference(checkout.bzrdir))
407
 
 
408
301
 
409
302
class TestHooks(TestCase):
410
303
 
414
307
        self.assertTrue("set_rh" in hooks, "set_rh not in %s" % hooks)
415
308
        self.assertTrue("post_push" in hooks, "post_push not in %s" % hooks)
416
309
        self.assertTrue("post_commit" in hooks, "post_commit not in %s" % hooks)
417
 
        self.assertTrue("pre_commit" in hooks, "pre_commit not in %s" % hooks)
418
310
        self.assertTrue("post_pull" in hooks, "post_pull not in %s" % hooks)
419
311
        self.assertTrue("post_uncommit" in hooks, "post_uncommit not in %s" % hooks)
420
 
        self.assertTrue("post_change_branch_tip" in hooks,
421
 
                        "post_change_branch_tip not in %s" % hooks)
422
312
 
423
313
    def test_installed_hooks_are_BranchHooks(self):
424
314
        """The installed hooks object should be a BranchHooks."""
425
315
        # the installed hooks are saved in self._preserved_hooks.
426
 
        self.assertIsInstance(self._preserved_hooks[_mod_branch.Branch], BranchHooks)
 
316
        self.assertIsInstance(self._preserved_hooks, BranchHooks)
 
317
 
 
318
    def test_install_hook_raises_unknown_hook(self):
 
319
        """install_hook should raise UnknownHook if a hook is unknown."""
 
320
        hooks = BranchHooks()
 
321
        self.assertRaises(UnknownHook, hooks.install_hook, 'silly', None)
 
322
 
 
323
    def test_install_hook_appends_known_hook(self):
 
324
        """install_hook should append the callable for known hooks."""
 
325
        hooks = BranchHooks()
 
326
        hooks.install_hook('set_rh', None)
 
327
        self.assertEqual(hooks['set_rh'], [None])
427
328
 
428
329
 
429
330
class TestPullResult(TestCase):