~bzr-pqm/bzr/bzr.dev

1185.67.4 by Aaron Bentley
Throw if we try to write to a LockableFiles with no write lock
1
# Copyright (C) 2005 by Canonical Ltd
2
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.
7
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.
12
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
19
from bzrlib.branch import Branch
1185.65.29 by Robert Collins
Implement final review suggestions.
20
from bzrlib.errors import BzrBadParameterNotString, NoSuchFile, ReadOnlyError
1185.70.2 by Martin Pool
Fix funny mistake
21
from bzrlib.lockable_files import LockableFiles
1185.67.4 by Aaron Bentley
Throw if we try to write to a LockableFiles with no write lock
22
from bzrlib.tests import TestCaseInTempDir
1185.70.2 by Martin Pool
Fix funny mistake
23
from bzrlib.transactions import PassThroughTransaction, ReadOnlyTransaction
1185.67.4 by Aaron Bentley
Throw if we try to write to a LockableFiles with no write lock
24
from bzrlib.transport import get_transport
1185.65.23 by Robert Collins
update tests somewhat
25
1185.67.4 by Aaron Bentley
Throw if we try to write to a LockableFiles with no write lock
26
class TestLockableFiles(TestCaseInTempDir):
1185.65.23 by Robert Collins
update tests somewhat
27
1185.67.4 by Aaron Bentley
Throw if we try to write to a LockableFiles with no write lock
28
    def setUp(self):
1185.65.23 by Robert Collins
update tests somewhat
29
        super(TestLockableFiles, self).setUp()
1185.67.4 by Aaron Bentley
Throw if we try to write to a LockableFiles with no write lock
30
        transport = get_transport('.')
31
        transport.mkdir('.bzr')
1185.67.8 by Aaron Bentley
Test and fix read locks
32
        transport.put('.bzr/my-lock', StringIO(''))
1185.69.2 by John Arbash Meinel
Changed LockableFiles to take the root directory directly. Moved mode information into LockableFiles instead of Branch
33
        self.lockable = LockableFiles(transport.clone('.bzr'), 'my-lock')
1185.67.4 by Aaron Bentley
Throw if we try to write to a LockableFiles with no write lock
34
1185.67.6 by Aaron Bentley
Added tests and fixes for LockableFiles.put_utf8(); imported IterableFile
35
    def test_read_write(self):
1185.65.29 by Robert Collins
Implement final review suggestions.
36
        self.assertRaises(NoSuchFile, self.lockable.get, 'foo')
37
        self.assertRaises(NoSuchFile, self.lockable.get_utf8, 'foo')
1185.67.6 by Aaron Bentley
Added tests and fixes for LockableFiles.put_utf8(); imported IterableFile
38
        self.lockable.lock_write()
39
        try:
40
            unicode_string = u'bar\u1234'
41
            self.assertEqual(4, len(unicode_string))
42
            byte_string = unicode_string.encode('utf-8')
43
            self.assertEqual(6, len(byte_string))
44
            self.assertRaises(UnicodeEncodeError, self.lockable.put, 'foo', 
45
                              StringIO(unicode_string))
46
            self.lockable.put('foo', StringIO(byte_string))
47
            self.assertEqual(byte_string,
1185.65.29 by Robert Collins
Implement final review suggestions.
48
                             self.lockable.get('foo').read())
1185.67.6 by Aaron Bentley
Added tests and fixes for LockableFiles.put_utf8(); imported IterableFile
49
            self.assertEqual(unicode_string,
1185.65.29 by Robert Collins
Implement final review suggestions.
50
                             self.lockable.get_utf8('foo').read())
51
            self.assertRaises(BzrBadParameterNotString,
52
                              self.lockable.put_utf8,
53
                              'bar',
54
                              StringIO(unicode_string)
55
                              )
56
            self.lockable.put_utf8('bar', unicode_string)
1185.67.6 by Aaron Bentley
Added tests and fixes for LockableFiles.put_utf8(); imported IterableFile
57
            self.assertEqual(unicode_string, 
1185.65.29 by Robert Collins
Implement final review suggestions.
58
                             self.lockable.get_utf8('bar').read())
1185.67.6 by Aaron Bentley
Added tests and fixes for LockableFiles.put_utf8(); imported IterableFile
59
            self.assertEqual(byte_string, 
1185.65.29 by Robert Collins
Implement final review suggestions.
60
                             self.lockable.get('bar').read())
1185.67.6 by Aaron Bentley
Added tests and fixes for LockableFiles.put_utf8(); imported IterableFile
61
        finally:
62
            self.lockable.unlock()
63
1185.67.4 by Aaron Bentley
Throw if we try to write to a LockableFiles with no write lock
64
    def test_locks(self):
1185.67.8 by Aaron Bentley
Test and fix read locks
65
        self.lockable.lock_read()
1185.65.27 by Robert Collins
Tweak storage towards mergability.
66
        try:
67
            self.assertRaises(ReadOnlyError, self.lockable.put, 'foo', 
68
                              StringIO('bar\u1234'))
69
        finally:
70
            self.lockable.unlock()
1185.68.1 by Aaron Bentley
test transactions
71
72
    def test_transactions(self):
73
        self.assertIs(self.lockable.get_transaction().__class__,
74
                      PassThroughTransaction)
75
        self.lockable.lock_read()
76
        try:
77
            self.assertIs(self.lockable.get_transaction().__class__,
78
                          ReadOnlyTransaction)
79
        finally:
80
            self.lockable.unlock()
81
        self.assertIs(self.lockable.get_transaction().__class__,
82
                      PassThroughTransaction)
83
        self.lockable.lock_write()
84
        self.assertIs(self.lockable.get_transaction().__class__,
85
                      PassThroughTransaction)
86
        self.lockable.unlock()
1185.65.23 by Robert Collins
update tests somewhat
87
88
    def test__escape(self):
89
        self.assertEqual('%25', self.lockable._escape('%'))
90
        
91
    def test__escape_empty(self):
92
        self.assertEqual('', self.lockable._escape(''))
93