/brz/remove-bazaar

To get this branch, use:
bzr branch http://gegoxaren.bato24.eu/bzr/brz/remove-bazaar
2052.3.2 by John Arbash Meinel
Change Copyright .. by Canonical to Copyright ... Canonical
1
# Copyright (C) 2005, 2006 Canonical Ltd
1887.1.1 by Adeodato Simó
Do not separate paragraphs in the copyright statement with blank lines,
2
#
1185.67.4 by Aaron Bentley
Throw if we try to write to a LockableFiles with no write lock
3
# This program is free software; you can redistribute it and/or modify
4
# it under the terms of the GNU General Public License as published by
5
# the Free Software Foundation; either version 2 of the License, or
6
# (at your option) any later version.
1887.1.1 by Adeodato Simó
Do not separate paragraphs in the copyright statement with blank lines,
7
#
1185.67.4 by Aaron Bentley
Throw if we try to write to a LockableFiles with no write lock
8
# This program is distributed in the hope that it will be useful,
9
# but WITHOUT ANY WARRANTY; without even the implied warranty of
10
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
11
# GNU General Public License for more details.
1887.1.1 by Adeodato Simó
Do not separate paragraphs in the copyright statement with blank lines,
12
#
1185.67.4 by Aaron Bentley
Throw if we try to write to a LockableFiles with no write lock
13
# You should have received a copy of the GNU General Public License
14
# along with this program; if not, write to the Free Software
15
# Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307  USA
16
1185.70.2 by Martin Pool
Fix funny mistake
17
from StringIO import StringIO
18
1687.1.6 by Robert Collins
Extend LockableFiles to support break_lock() calls.
19
import bzrlib
1185.70.2 by Martin Pool
Fix funny mistake
20
from bzrlib.branch import Branch
1687.1.6 by Robert Collins
Extend LockableFiles to support break_lock() calls.
21
import bzrlib.errors as errors
1185.65.29 by Robert Collins
Implement final review suggestions.
22
from bzrlib.errors import BzrBadParameterNotString, NoSuchFile, ReadOnlyError
1553.5.63 by Martin Pool
Lock type is now mandatory for LockableFiles constructor
23
from bzrlib.lockable_files import LockableFiles, TransportLock
1553.5.44 by Martin Pool
LockableFiles can now call LockDir directly
24
from bzrlib.lockdir import LockDir
1185.67.4 by Aaron Bentley
Throw if we try to write to a LockableFiles with no write lock
25
from bzrlib.tests import TestCaseInTempDir
2018.5.59 by Robert Collins
Get BranchConfig working somewhat on RemoteBranches (Robert Collins, Vincent Ladeuil).
26
from bzrlib.tests.test_smart import TestCaseWithSmartMedium
1594.2.22 by Robert Collins
Ensure that lockable files calls finish() on transactions.:
27
from bzrlib.tests.test_transactions import DummyWeave
1563.2.34 by Robert Collins
Remove the commit and rollback transaction methods as misleading, and implement a WriteTransaction
28
from bzrlib.transactions import (PassThroughTransaction,
29
                                 ReadOnlyTransaction,
30
                                 WriteTransaction,
31
                                 )
1185.67.4 by Aaron Bentley
Throw if we try to write to a LockableFiles with no write lock
32
from bzrlib.transport import get_transport
1185.65.23 by Robert Collins
update tests somewhat
33
1553.5.42 by Martin Pool
Start to parameterize LockableFiles test cases.
34
35
# these tests are applied in each parameterized suite for LockableFiles
36
class _TestLockableFiles_mixin(object):
1185.67.4 by Aaron Bentley
Throw if we try to write to a LockableFiles with no write lock
37
1185.67.6 by Aaron Bentley
Added tests and fixes for LockableFiles.put_utf8(); imported IterableFile
38
    def test_read_write(self):
1185.65.29 by Robert Collins
Implement final review suggestions.
39
        self.assertRaises(NoSuchFile, self.lockable.get, 'foo')
40
        self.assertRaises(NoSuchFile, self.lockable.get_utf8, 'foo')
1185.67.6 by Aaron Bentley
Added tests and fixes for LockableFiles.put_utf8(); imported IterableFile
41
        self.lockable.lock_write()
42
        try:
43
            unicode_string = u'bar\u1234'
44
            self.assertEqual(4, len(unicode_string))
45
            byte_string = unicode_string.encode('utf-8')
46
            self.assertEqual(6, len(byte_string))
47
            self.assertRaises(UnicodeEncodeError, self.lockable.put, 'foo', 
48
                              StringIO(unicode_string))
49
            self.lockable.put('foo', StringIO(byte_string))
50
            self.assertEqual(byte_string,
1185.65.29 by Robert Collins
Implement final review suggestions.
51
                             self.lockable.get('foo').read())
1185.67.6 by Aaron Bentley
Added tests and fixes for LockableFiles.put_utf8(); imported IterableFile
52
            self.assertEqual(unicode_string,
1185.65.29 by Robert Collins
Implement final review suggestions.
53
                             self.lockable.get_utf8('foo').read())
54
            self.assertRaises(BzrBadParameterNotString,
55
                              self.lockable.put_utf8,
56
                              'bar',
57
                              StringIO(unicode_string)
58
                              )
59
            self.lockable.put_utf8('bar', unicode_string)
1185.67.6 by Aaron Bentley
Added tests and fixes for LockableFiles.put_utf8(); imported IterableFile
60
            self.assertEqual(unicode_string, 
1185.65.29 by Robert Collins
Implement final review suggestions.
61
                             self.lockable.get_utf8('bar').read())
1185.67.6 by Aaron Bentley
Added tests and fixes for LockableFiles.put_utf8(); imported IterableFile
62
            self.assertEqual(byte_string, 
1185.65.29 by Robert Collins
Implement final review suggestions.
63
                             self.lockable.get('bar').read())
1185.67.6 by Aaron Bentley
Added tests and fixes for LockableFiles.put_utf8(); imported IterableFile
64
        finally:
65
            self.lockable.unlock()
66
1185.67.4 by Aaron Bentley
Throw if we try to write to a LockableFiles with no write lock
67
    def test_locks(self):
1185.67.8 by Aaron Bentley
Test and fix read locks
68
        self.lockable.lock_read()
1185.65.27 by Robert Collins
Tweak storage towards mergability.
69
        try:
70
            self.assertRaises(ReadOnlyError, self.lockable.put, 'foo', 
71
                              StringIO('bar\u1234'))
72
        finally:
73
            self.lockable.unlock()
1185.68.1 by Aaron Bentley
test transactions
74
75
    def test_transactions(self):
76
        self.assertIs(self.lockable.get_transaction().__class__,
77
                      PassThroughTransaction)
78
        self.lockable.lock_read()
79
        try:
80
            self.assertIs(self.lockable.get_transaction().__class__,
81
                          ReadOnlyTransaction)
82
        finally:
83
            self.lockable.unlock()
84
        self.assertIs(self.lockable.get_transaction().__class__,
85
                      PassThroughTransaction)
86
        self.lockable.lock_write()
87
        self.assertIs(self.lockable.get_transaction().__class__,
1563.2.34 by Robert Collins
Remove the commit and rollback transaction methods as misleading, and implement a WriteTransaction
88
                      WriteTransaction)
1594.2.22 by Robert Collins
Ensure that lockable files calls finish() on transactions.:
89
        # check that finish is called:
90
        vf = DummyWeave('a')
91
        self.lockable.get_transaction().register_dirty(vf)
1185.68.1 by Aaron Bentley
test transactions
92
        self.lockable.unlock()
1594.2.22 by Robert Collins
Ensure that lockable files calls finish() on transactions.:
93
        self.assertTrue(vf.finished)
1185.65.23 by Robert Collins
update tests somewhat
94
95
    def test__escape(self):
96
        self.assertEqual('%25', self.lockable._escape('%'))
97
        
98
    def test__escape_empty(self):
99
        self.assertEqual('', self.lockable._escape(''))
100
1687.1.6 by Robert Collins
Extend LockableFiles to support break_lock() calls.
101
    def test_break_lock(self):
102
        # some locks are not breakable
103
        self.lockable.lock_write()
104
        try:
105
            self.assertRaises(AssertionError, self.lockable.break_lock)
106
        except NotImplementedError:
107
            # this lock cannot be broken
108
            self.lockable.unlock()
109
            return
110
        l2 = self.get_lockable()
111
        orig_factory = bzrlib.ui.ui_factory
112
        # silent ui - no need for stdout
113
        bzrlib.ui.ui_factory = bzrlib.ui.SilentUIFactory()
114
        bzrlib.ui.ui_factory.stdin = StringIO("y\n")
115
        try:
116
            l2.break_lock()
117
        finally:
118
            bzrlib.ui.ui_factory = orig_factory
119
        try:
120
            l2.lock_write()
121
            l2.unlock()
122
        finally:
123
            self.assertRaises(errors.LockBroken, self.lockable.unlock)
124
            self.assertFalse(self.lockable.is_locked())
125
1553.5.42 by Martin Pool
Start to parameterize LockableFiles test cases.
126
127
# This method of adapting tests to parameters is different to 
128
# the TestProviderAdapters used elsewhere, but seems simpler for this 
129
# case.  
1553.5.45 by Martin Pool
Clean up Transport-based locks for old branches
130
class TestLockableFiles_TransportLock(TestCaseInTempDir,
131
                                      _TestLockableFiles_mixin):
1553.5.42 by Martin Pool
Start to parameterize LockableFiles test cases.
132
133
    def setUp(self):
1553.5.45 by Martin Pool
Clean up Transport-based locks for old branches
134
        super(TestLockableFiles_TransportLock, self).setUp()
1553.5.42 by Martin Pool
Start to parameterize LockableFiles test cases.
135
        transport = get_transport('.')
136
        transport.mkdir('.bzr')
1687.1.6 by Robert Collins
Extend LockableFiles to support break_lock() calls.
137
        self.sub_transport = transport.clone('.bzr')
138
        self.lockable = self.get_lockable()
1553.5.61 by Martin Pool
Locks protecting LockableFiles must now be explicitly created before use.
139
        self.lockable.create_lock()
1687.1.6 by Robert Collins
Extend LockableFiles to support break_lock() calls.
140
141
    def tearDown(self):
142
        super(TestLockableFiles_TransportLock, self).tearDown()
1687.1.15 by Robert Collins
Review comments.
143
        # free the subtransport so that we do not get a 5 second
144
        # timeout due to the SFTP connection cache.
1687.1.6 by Robert Collins
Extend LockableFiles to support break_lock() calls.
145
        del self.sub_transport
146
147
    def get_lockable(self):
148
        return LockableFiles(self.sub_transport, 'my-lock', TransportLock)
1553.5.43 by Martin Pool
Get LockableFiles tests running against LockDir
149
        
150
151
class TestLockableFiles_LockDir(TestCaseInTempDir,
152
                              _TestLockableFiles_mixin):
153
    """LockableFile tests run with LockDir underneath"""
154
155
    def setUp(self):
156
        super(TestLockableFiles_LockDir, self).setUp()
1553.5.61 by Martin Pool
Locks protecting LockableFiles must now be explicitly created before use.
157
        self.transport = get_transport('.')
1687.1.6 by Robert Collins
Extend LockableFiles to support break_lock() calls.
158
        self.lockable = self.get_lockable()
1666.1.4 by Robert Collins
* 'Metadir' is now the default disk format. This improves behaviour in
159
        # the lock creation here sets mode - test_permissions on branch 
160
        # tests that implicitly, but it might be a good idea to factor 
161
        # out the mode checking logic and have it applied to loackable files
162
        # directly. RBC 20060418
1553.5.61 by Martin Pool
Locks protecting LockableFiles must now be explicitly created before use.
163
        self.lockable.create_lock()
1553.5.43 by Martin Pool
Get LockableFiles tests running against LockDir
164
1687.1.6 by Robert Collins
Extend LockableFiles to support break_lock() calls.
165
    def get_lockable(self):
166
        return LockableFiles(self.transport, 'my-lock', LockDir)
1553.5.60 by Martin Pool
New LockableFiles.create_lock() method
167
168
    def test_lock_created(self):
1553.5.61 by Martin Pool
Locks protecting LockableFiles must now be explicitly created before use.
169
        self.assertTrue(self.transport.has('my-lock'))
170
        self.lockable.lock_write()
171
        self.assertTrue(self.transport.has('my-lock/held/info'))
172
        self.lockable.unlock()
173
        self.assertFalse(self.transport.has('my-lock/held/info'))
174
        self.assertTrue(self.transport.has('my-lock'))
175
176
177
    # TODO: Test the lockdir inherits the right file and directory permissions
178
    # from the LockableFiles.
2018.5.59 by Robert Collins
Get BranchConfig working somewhat on RemoteBranches (Robert Collins, Vincent Ladeuil).
179
        
180
181
class TestLockableFiles_RemoteLockDir(TestCaseWithSmartMedium,
182
                              _TestLockableFiles_mixin):
183
    """LockableFile tests run with RemoteLockDir on a branch."""
184
185
    def setUp(self):
186
        super(TestLockableFiles_RemoteLockDir, self).setUp()
187
        # can only get a RemoteLockDir with some RemoteObject...
188
        # use a branch as thats what we want. These mixin tests test the end
189
        # to end behaviour, so stubbing out the backend and simulating would
190
        # defeat the purpose. We test the protocol implementation separately
191
        # in test_remote and test_smart as usual.
192
        self.make_branch('foo')
193
        self.transport = get_transport('.')
194
        self.lockable = self.get_lockable()
195
196
    def get_lockable(self):
197
        # getting a new lockable involves opening a new instance of the branch
198
        branch = bzrlib.branch.Branch.open(self.get_url('foo'))
199
        return branch.control_files