~bzr-pqm/bzr/bzr.dev

« back to all changes in this revision

Viewing changes to bzrlib/tests/test_branch.py

Initial commit for russian version of documents.

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.
41
41
    BzrBranchFormat5,
42
42
    BzrBranchFormat6,
43
43
    PullResult,
44
 
    _run_with_write_locked_target,
45
44
    )
46
 
from bzrlib.bzrdir import (BzrDirMetaFormat1, BzrDirMeta1,
 
45
from bzrlib.bzrdir import (BzrDirMetaFormat1, BzrDirMeta1, 
47
46
                           BzrDir, BzrDirFormat)
48
47
from bzrlib.errors import (NotBranchError,
49
48
                           UnknownFormatError,
64
63
 
65
64
    def test_default_format_is_same_as_bzrdir_default(self):
66
65
        # 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 --
 
66
        # set, but at the moment that's not true -- mbp 20070814 -- 
68
67
        # https://bugs.launchpad.net/bzr/+bug/132376
69
68
        self.assertEqual(BranchFormat.get_default_format(),
70
69
                BzrDirFormat.get_default_format().get_branch_format())
134
133
class SampleBranchFormat(BranchFormat):
135
134
    """A sample format
136
135
 
137
 
    this format is initializable, unsupported to aid in testing the
 
136
    this format is initializable, unsupported to aid in testing the 
138
137
    open and open_downlevel routines.
139
138
    """
140
139
 
151
150
    def is_supported(self):
152
151
        return False
153
152
 
154
 
    def open(self, transport, _found=False, ignore_fallbacks=False):
 
153
    def open(self, transport, _found=False):
155
154
        return "opened branch."
156
155
 
157
156
 
161
160
    def test_find_format(self):
162
161
        # is the right format object found for a branch?
163
162
        # create a branch with a few known format objects.
164
 
        # this is not quite the same as
 
163
        # this is not quite the same as 
165
164
        self.build_tree(["foo/", "bar/"])
166
165
        def check_format(format, url):
167
166
            dir = format._matchingbzrdir.initialize(url)
170
169
            found_format = BranchFormat.find_format(dir)
171
170
            self.failUnless(isinstance(found_format, format.__class__))
172
171
        check_format(BzrBranchFormat5(), "bar")
173
 
 
 
172
        
174
173
    def test_find_format_not_branch(self):
175
174
        dir = bzrdir.BzrDirMetaFormat1().initialize(self.get_url())
176
175
        self.assertRaises(NotBranchError,
228
227
        branch = self.make_branch('a', format=self.get_format_name())
229
228
        self.failUnlessExists('a/.bzr/branch/last-revision')
230
229
        self.failIfExists('a/.bzr/branch/revision-history')
231
 
        self.failIfExists('a/.bzr/branch/references')
232
230
 
233
231
    def test_config(self):
234
232
        """Ensure that all configuration data is stored in the branch"""
335
333
        return _mod_branch.BzrBranch7
336
334
 
337
335
    def get_format_name(self):
338
 
        return "1.9"
 
336
        return "development"
339
337
 
340
338
    def get_format_name_subtree(self):
341
339
        return "development-subtree"
379
377
        self.assertTrue(branch.repository.has_revision(revid))
380
378
 
381
379
 
382
 
class BzrBranch8(TestCaseWithTransport):
383
 
 
384
 
    def make_branch(self, location, format=None):
385
 
        if format is None:
386
 
            format = bzrdir.format_registry.make_bzrdir('1.9')
387
 
            format.set_branch_format(_mod_branch.BzrBranchFormat8())
388
 
        return TestCaseWithTransport.make_branch(self, location, format=format)
389
 
 
390
 
    def create_branch_with_reference(self):
391
 
        branch = self.make_branch('branch')
392
 
        branch._set_all_reference_info({'file-id': ('path', 'location')})
393
 
        return branch
394
 
 
395
 
    @staticmethod
396
 
    def instrument_branch(branch, gets):
397
 
        old_get = branch._transport.get
398
 
        def get(*args, **kwargs):
399
 
            gets.append((args, kwargs))
400
 
            return old_get(*args, **kwargs)
401
 
        branch._transport.get = get
402
 
 
403
 
    def test_reference_info_caching_read_locked(self):
404
 
        gets = []
405
 
        branch = self.create_branch_with_reference()
406
 
        branch.lock_read()
407
 
        self.addCleanup(branch.unlock)
408
 
        self.instrument_branch(branch, gets)
409
 
        branch.get_reference_info('file-id')
410
 
        branch.get_reference_info('file-id')
411
 
        self.assertEqual(1, len(gets))
412
 
 
413
 
    def test_reference_info_caching_read_unlocked(self):
414
 
        gets = []
415
 
        branch = self.create_branch_with_reference()
416
 
        self.instrument_branch(branch, gets)
417
 
        branch.get_reference_info('file-id')
418
 
        branch.get_reference_info('file-id')
419
 
        self.assertEqual(2, len(gets))
420
 
 
421
 
    def test_reference_info_caching_write_locked(self):
422
 
        gets = []
423
 
        branch = self.make_branch('branch')
424
 
        branch.lock_write()
425
 
        self.instrument_branch(branch, gets)
426
 
        self.addCleanup(branch.unlock)
427
 
        branch._set_all_reference_info({'file-id': ('path2', 'location2')})
428
 
        path, location = branch.get_reference_info('file-id')
429
 
        self.assertEqual(0, len(gets))
430
 
        self.assertEqual('path2', path)
431
 
        self.assertEqual('location2', location)
432
 
 
433
 
    def test_reference_info_caches_cleared(self):
434
 
        branch = self.make_branch('branch')
435
 
        branch.lock_write()
436
 
        branch.set_reference_info('file-id', 'path2', 'location2')
437
 
        branch.unlock()
438
 
        doppelganger = Branch.open('branch')
439
 
        doppelganger.set_reference_info('file-id', 'path3', 'location3')
440
 
        self.assertEqual(('path3', 'location3'),
441
 
                         branch.get_reference_info('file-id'))
442
 
 
443
380
class TestBranchReference(TestCaseWithTransport):
444
381
    """Tests for the branch reference facility."""
445
382
 
486
423
    def test_installed_hooks_are_BranchHooks(self):
487
424
        """The installed hooks object should be a BranchHooks."""
488
425
        # the installed hooks are saved in self._preserved_hooks.
489
 
        self.assertIsInstance(self._preserved_hooks[_mod_branch.Branch][1],
490
 
            BranchHooks)
 
426
        self.assertIsInstance(self._preserved_hooks[_mod_branch.Branch], BranchHooks)
491
427
 
492
428
 
493
429
class TestPullResult(TestCase):
502
438
        # it's still supported
503
439
        a = "%d revisions pulled" % r
504
440
        self.assertEqual(a, "10 revisions pulled")
505
 
 
506
 
 
507
 
 
508
 
class _StubLockable(object):
509
 
    """Helper for TestRunWithWriteLockedTarget."""
510
 
 
511
 
    def __init__(self, calls, unlock_exc=None):
512
 
        self.calls = calls
513
 
        self.unlock_exc = unlock_exc
514
 
 
515
 
    def lock_write(self):
516
 
        self.calls.append('lock_write')
517
 
 
518
 
    def unlock(self):
519
 
        self.calls.append('unlock')
520
 
        if self.unlock_exc is not None:
521
 
            raise self.unlock_exc
522
 
 
523
 
 
524
 
class _ErrorFromCallable(Exception):
525
 
    """Helper for TestRunWithWriteLockedTarget."""
526
 
 
527
 
 
528
 
class _ErrorFromUnlock(Exception):
529
 
    """Helper for TestRunWithWriteLockedTarget."""
530
 
 
531
 
 
532
 
class TestRunWithWriteLockedTarget(TestCase):
533
 
    """Tests for _run_with_write_locked_target."""
534
 
 
535
 
    def setUp(self):
536
 
        TestCase.setUp(self)
537
 
        self._calls = []
538
 
 
539
 
    def func_that_returns_ok(self):
540
 
        self._calls.append('func called')
541
 
        return 'ok'
542
 
 
543
 
    def func_that_raises(self):
544
 
        self._calls.append('func called')
545
 
        raise _ErrorFromCallable()
546
 
 
547
 
    def test_success_unlocks(self):
548
 
        lockable = _StubLockable(self._calls)
549
 
        result = _run_with_write_locked_target(
550
 
            lockable, self.func_that_returns_ok)
551
 
        self.assertEqual('ok', result)
552
 
        self.assertEqual(['lock_write', 'func called', 'unlock'], self._calls)
553
 
 
554
 
    def test_exception_unlocks_and_propagates(self):
555
 
        lockable = _StubLockable(self._calls)
556
 
        self.assertRaises(_ErrorFromCallable,
557
 
            _run_with_write_locked_target, lockable, self.func_that_raises)
558
 
        self.assertEqual(['lock_write', 'func called', 'unlock'], self._calls)
559
 
 
560
 
    def test_callable_succeeds_but_error_during_unlock(self):
561
 
        lockable = _StubLockable(self._calls, unlock_exc=_ErrorFromUnlock())
562
 
        self.assertRaises(_ErrorFromUnlock,
563
 
            _run_with_write_locked_target, lockable, self.func_that_returns_ok)
564
 
        self.assertEqual(['lock_write', 'func called', 'unlock'], self._calls)
565
 
 
566
 
    def test_error_during_unlock_does_not_mask_original_error(self):
567
 
        lockable = _StubLockable(self._calls, unlock_exc=_ErrorFromUnlock())
568
 
        self.assertRaises(_ErrorFromCallable,
569
 
            _run_with_write_locked_target, lockable, self.func_that_raises)
570
 
        self.assertEqual(['lock_write', 'func called', 'unlock'], self._calls)
571
 
 
572