~bzr-pqm/bzr/bzr.dev

« back to all changes in this revision

Viewing changes to bzrlib/tests/test_branch.py

  • Committer: Martin Pool
  • Date: 2008-10-20 23:58:12 UTC
  • mto: This revision was merged to the branch mainline in revision 3787.
  • Revision ID: mbp@sourcefrog.net-20081020235812-itg90mk0u4dez92z
lp-upload-release now handles names like bzr-1.8.tar.gz

Show diffs side-by-side

added added

removed removed

Lines of Context:
12
12
#
13
13
# You should have received a copy of the GNU General Public License
14
14
# along with this program; if not, write to the Free Software
15
 
# Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
 
15
# Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307  USA
16
16
 
17
17
"""Tests for the Branch facility that are not interface  tests.
18
18
 
19
 
For interface tests see tests/per_branch/*.py.
 
19
For interface tests see tests/branch_implementations/*.py.
20
20
 
21
21
For concrete class tests see this file, and for meta-branch tests
22
22
also see this file.
40
40
    BzrBranch5,
41
41
    BzrBranchFormat5,
42
42
    BzrBranchFormat6,
43
 
    BzrBranchFormat7,
44
43
    PullResult,
45
 
    _run_with_write_locked_target,
46
44
    )
47
 
from bzrlib.bzrdir import (BzrDirMetaFormat1, BzrDirMeta1,
 
45
from bzrlib.bzrdir import (BzrDirMetaFormat1, BzrDirMeta1, 
48
46
                           BzrDir, BzrDirFormat)
49
47
from bzrlib.errors import (NotBranchError,
50
48
                           UnknownFormatError,
61
59
    def test_default_format(self):
62
60
        # update this if you change the default branch format
63
61
        self.assertIsInstance(BranchFormat.get_default_format(),
64
 
                BzrBranchFormat7)
 
62
                BzrBranchFormat6)
65
63
 
66
64
    def test_default_format_is_same_as_bzrdir_default(self):
67
65
        # XXX: it might be nice if there was only one place the default was
68
 
        # set, but at the moment that's not true -- mbp 20070814 --
 
66
        # set, but at the moment that's not true -- mbp 20070814 -- 
69
67
        # https://bugs.launchpad.net/bzr/+bug/132376
70
68
        self.assertEqual(BranchFormat.get_default_format(),
71
69
                BzrDirFormat.get_default_format().get_branch_format())
135
133
class SampleBranchFormat(BranchFormat):
136
134
    """A sample format
137
135
 
138
 
    this format is initializable, unsupported to aid in testing the
 
136
    this format is initializable, unsupported to aid in testing the 
139
137
    open and open_downlevel routines.
140
138
    """
141
139
 
152
150
    def is_supported(self):
153
151
        return False
154
152
 
155
 
    def open(self, transport, _found=False, ignore_fallbacks=False):
 
153
    def open(self, transport, _found=False):
156
154
        return "opened branch."
157
155
 
158
156
 
162
160
    def test_find_format(self):
163
161
        # is the right format object found for a branch?
164
162
        # create a branch with a few known format objects.
165
 
        # this is not quite the same as
 
163
        # this is not quite the same as 
166
164
        self.build_tree(["foo/", "bar/"])
167
165
        def check_format(format, url):
168
166
            dir = format._matchingbzrdir.initialize(url)
171
169
            found_format = BranchFormat.find_format(dir)
172
170
            self.failUnless(isinstance(found_format, format.__class__))
173
171
        check_format(BzrBranchFormat5(), "bar")
174
 
 
 
172
        
175
173
    def test_find_format_not_branch(self):
176
174
        dir = bzrdir.BzrDirMetaFormat1().initialize(self.get_url())
177
175
        self.assertRaises(NotBranchError,
229
227
        branch = self.make_branch('a', format=self.get_format_name())
230
228
        self.failUnlessExists('a/.bzr/branch/last-revision')
231
229
        self.failIfExists('a/.bzr/branch/revision-history')
232
 
        self.failIfExists('a/.bzr/branch/references')
233
230
 
234
231
    def test_config(self):
235
232
        """Ensure that all configuration data is stored in the branch"""
336
333
        return _mod_branch.BzrBranch7
337
334
 
338
335
    def get_format_name(self):
339
 
        return "1.9"
 
336
        return "development"
340
337
 
341
338
    def get_format_name_subtree(self):
342
339
        return "development-subtree"
380
377
        self.assertTrue(branch.repository.has_revision(revid))
381
378
 
382
379
 
383
 
class BzrBranch8(TestCaseWithTransport):
384
 
 
385
 
    def make_branch(self, location, format=None):
386
 
        if format is None:
387
 
            format = bzrdir.format_registry.make_bzrdir('1.9')
388
 
            format.set_branch_format(_mod_branch.BzrBranchFormat8())
389
 
        return TestCaseWithTransport.make_branch(self, location, format=format)
390
 
 
391
 
    def create_branch_with_reference(self):
392
 
        branch = self.make_branch('branch')
393
 
        branch._set_all_reference_info({'file-id': ('path', 'location')})
394
 
        return branch
395
 
 
396
 
    @staticmethod
397
 
    def instrument_branch(branch, gets):
398
 
        old_get = branch._transport.get
399
 
        def get(*args, **kwargs):
400
 
            gets.append((args, kwargs))
401
 
            return old_get(*args, **kwargs)
402
 
        branch._transport.get = get
403
 
 
404
 
    def test_reference_info_caching_read_locked(self):
405
 
        gets = []
406
 
        branch = self.create_branch_with_reference()
407
 
        branch.lock_read()
408
 
        self.addCleanup(branch.unlock)
409
 
        self.instrument_branch(branch, gets)
410
 
        branch.get_reference_info('file-id')
411
 
        branch.get_reference_info('file-id')
412
 
        self.assertEqual(1, len(gets))
413
 
 
414
 
    def test_reference_info_caching_read_unlocked(self):
415
 
        gets = []
416
 
        branch = self.create_branch_with_reference()
417
 
        self.instrument_branch(branch, gets)
418
 
        branch.get_reference_info('file-id')
419
 
        branch.get_reference_info('file-id')
420
 
        self.assertEqual(2, len(gets))
421
 
 
422
 
    def test_reference_info_caching_write_locked(self):
423
 
        gets = []
424
 
        branch = self.make_branch('branch')
425
 
        branch.lock_write()
426
 
        self.instrument_branch(branch, gets)
427
 
        self.addCleanup(branch.unlock)
428
 
        branch._set_all_reference_info({'file-id': ('path2', 'location2')})
429
 
        path, location = branch.get_reference_info('file-id')
430
 
        self.assertEqual(0, len(gets))
431
 
        self.assertEqual('path2', path)
432
 
        self.assertEqual('location2', location)
433
 
 
434
 
    def test_reference_info_caches_cleared(self):
435
 
        branch = self.make_branch('branch')
436
 
        branch.lock_write()
437
 
        branch.set_reference_info('file-id', 'path2', 'location2')
438
 
        branch.unlock()
439
 
        doppelganger = Branch.open('branch')
440
 
        doppelganger.set_reference_info('file-id', 'path3', 'location3')
441
 
        self.assertEqual(('path3', 'location3'),
442
 
                         branch.get_reference_info('file-id'))
443
 
 
444
380
class TestBranchReference(TestCaseWithTransport):
445
381
    """Tests for the branch reference facility."""
446
382
 
487
423
    def test_installed_hooks_are_BranchHooks(self):
488
424
        """The installed hooks object should be a BranchHooks."""
489
425
        # the installed hooks are saved in self._preserved_hooks.
490
 
        self.assertIsInstance(self._preserved_hooks[_mod_branch.Branch][1],
491
 
            BranchHooks)
 
426
        self.assertIsInstance(self._preserved_hooks[_mod_branch.Branch], BranchHooks)
492
427
 
493
428
 
494
429
class TestPullResult(TestCase):
503
438
        # it's still supported
504
439
        a = "%d revisions pulled" % r
505
440
        self.assertEqual(a, "10 revisions pulled")
506
 
 
507
 
    def test_report_changed(self):
508
 
        r = PullResult()
509
 
        r.old_revid = "old-revid"
510
 
        r.old_revno = 10
511
 
        r.new_revid = "new-revid"
512
 
        r.new_revno = 20
513
 
        f = StringIO()
514
 
        r.report(f)
515
 
        self.assertEqual("Now on revision 20.\n", f.getvalue())
516
 
 
517
 
    def test_report_unchanged(self):
518
 
        r = PullResult()
519
 
        r.old_revid = "same-revid"
520
 
        r.new_revid = "same-revid"
521
 
        f = StringIO()
522
 
        r.report(f)
523
 
        self.assertEqual("No revisions to pull.\n", f.getvalue())
524
 
 
525
 
 
526
 
class _StubLockable(object):
527
 
    """Helper for TestRunWithWriteLockedTarget."""
528
 
 
529
 
    def __init__(self, calls, unlock_exc=None):
530
 
        self.calls = calls
531
 
        self.unlock_exc = unlock_exc
532
 
 
533
 
    def lock_write(self):
534
 
        self.calls.append('lock_write')
535
 
 
536
 
    def unlock(self):
537
 
        self.calls.append('unlock')
538
 
        if self.unlock_exc is not None:
539
 
            raise self.unlock_exc
540
 
 
541
 
 
542
 
class _ErrorFromCallable(Exception):
543
 
    """Helper for TestRunWithWriteLockedTarget."""
544
 
 
545
 
 
546
 
class _ErrorFromUnlock(Exception):
547
 
    """Helper for TestRunWithWriteLockedTarget."""
548
 
 
549
 
 
550
 
class TestRunWithWriteLockedTarget(TestCase):
551
 
    """Tests for _run_with_write_locked_target."""
552
 
 
553
 
    def setUp(self):
554
 
        TestCase.setUp(self)
555
 
        self._calls = []
556
 
 
557
 
    def func_that_returns_ok(self):
558
 
        self._calls.append('func called')
559
 
        return 'ok'
560
 
 
561
 
    def func_that_raises(self):
562
 
        self._calls.append('func called')
563
 
        raise _ErrorFromCallable()
564
 
 
565
 
    def test_success_unlocks(self):
566
 
        lockable = _StubLockable(self._calls)
567
 
        result = _run_with_write_locked_target(
568
 
            lockable, self.func_that_returns_ok)
569
 
        self.assertEqual('ok', result)
570
 
        self.assertEqual(['lock_write', 'func called', 'unlock'], self._calls)
571
 
 
572
 
    def test_exception_unlocks_and_propagates(self):
573
 
        lockable = _StubLockable(self._calls)
574
 
        self.assertRaises(_ErrorFromCallable,
575
 
            _run_with_write_locked_target, lockable, self.func_that_raises)
576
 
        self.assertEqual(['lock_write', 'func called', 'unlock'], self._calls)
577
 
 
578
 
    def test_callable_succeeds_but_error_during_unlock(self):
579
 
        lockable = _StubLockable(self._calls, unlock_exc=_ErrorFromUnlock())
580
 
        self.assertRaises(_ErrorFromUnlock,
581
 
            _run_with_write_locked_target, lockable, self.func_that_returns_ok)
582
 
        self.assertEqual(['lock_write', 'func called', 'unlock'], self._calls)
583
 
 
584
 
    def test_error_during_unlock_does_not_mask_original_error(self):
585
 
        lockable = _StubLockable(self._calls, unlock_exc=_ErrorFromUnlock())
586
 
        self.assertRaises(_ErrorFromCallable,
587
 
            _run_with_write_locked_target, lockable, self.func_that_raises)
588
 
        self.assertEqual(['lock_write', 'func called', 'unlock'], self._calls)
589
 
 
590