1
# Copyright (C) 2005, 2006 by Canonical Ltd
1
# Copyright (C) 2005 by Canonical Ltd
3
3
# This program is free software; you can redistribute it and/or modify
4
4
# it under the terms of the GNU General Public License as published by
19
19
from bzrlib.branch import Branch
20
20
from bzrlib.errors import BzrBadParameterNotString, NoSuchFile, ReadOnlyError
21
from bzrlib.lockable_files import LockableFiles, TransportLock
22
from bzrlib.lockdir import LockDir
21
from bzrlib.lockable_files import LockableFiles
23
22
from bzrlib.tests import TestCaseInTempDir
24
from bzrlib.tests.test_transactions import DummyWeave
25
from bzrlib.transactions import (PassThroughTransaction,
23
from bzrlib.transactions import PassThroughTransaction, ReadOnlyTransaction
29
24
from bzrlib.transport import get_transport
26
class TestLockableFiles(TestCaseInTempDir):
32
# these tests are applied in each parameterized suite for LockableFiles
33
class _TestLockableFiles_mixin(object):
29
super(TestLockableFiles, self).setUp()
30
transport = get_transport('.')
31
transport.mkdir('.bzr')
32
transport.put('.bzr/my-lock', StringIO(''))
33
self.lockable = LockableFiles(transport.clone('.bzr'), 'my-lock')
35
35
def test_read_write(self):
36
36
self.assertRaises(NoSuchFile, self.lockable.get, 'foo')
82
82
PassThroughTransaction)
83
83
self.lockable.lock_write()
84
84
self.assertIs(self.lockable.get_transaction().__class__,
86
# check that finish is called:
88
self.lockable.get_transaction().register_dirty(vf)
85
PassThroughTransaction)
89
86
self.lockable.unlock()
90
self.assertTrue(vf.finished)
92
88
def test__escape(self):
93
89
self.assertEqual('%25', self.lockable._escape('%'))
95
91
def test__escape_empty(self):
96
92
self.assertEqual('', self.lockable._escape(''))
99
# This method of adapting tests to parameters is different to
100
# the TestProviderAdapters used elsewhere, but seems simpler for this
102
class TestLockableFiles_TransportLock(TestCaseInTempDir,
103
_TestLockableFiles_mixin):
106
super(TestLockableFiles_TransportLock, self).setUp()
107
transport = get_transport('.')
108
transport.mkdir('.bzr')
109
sub_transport = transport.clone('.bzr')
110
self.lockable = LockableFiles(sub_transport, 'my-lock', TransportLock)
111
self.lockable.create_lock()
114
class TestLockableFiles_LockDir(TestCaseInTempDir,
115
_TestLockableFiles_mixin):
116
"""LockableFile tests run with LockDir underneath"""
119
super(TestLockableFiles_LockDir, self).setUp()
120
self.transport = get_transport('.')
121
self.lockable = LockableFiles(self.transport, 'my-lock', LockDir)
122
self.lockable.create_lock()
124
def test_lock_is_lockdir(self):
125
"""Created instance should use a LockDir.
127
This primarily tests the mixin adapter works properly.
129
## self.assertIsInstance(self.lockable, LockableFiles)
130
## self.assertIsInstance(self.lockable._lock_strategy,
133
def test_lock_created(self):
134
self.assertTrue(self.transport.has('my-lock'))
135
self.lockable.lock_write()
136
self.assertTrue(self.transport.has('my-lock/held/info'))
137
self.lockable.unlock()
138
self.assertFalse(self.transport.has('my-lock/held/info'))
139
self.assertTrue(self.transport.has('my-lock'))
142
# TODO: Test the lockdir inherits the right file and directory permissions
143
# from the LockableFiles.