/brz/remove-bazaar

To get this branch, use:
bzr branch http://gegoxaren.bato24.eu/bzr/brz/remove-bazaar

« back to all changes in this revision

Viewing changes to bzrlib/tests/test_lockable_files.py

  • Committer: Michael Ellerman
  • Date: 2006-03-09 00:24:48 UTC
  • mto: (1610.1.8 bzr.mbp.integration)
  • mto: This revision was merged to the branch mainline in revision 1616.
  • Revision ID: michael@ellerman.id.au-20060309002448-70cce15e3d605130
Make the "ignore line" in the commit message editor the "right" width, so
that if you make your message that wide it won't wrap in bzr log output.
Just as a visual aid.

Show diffs side-by-side

added added

removed removed

Lines of Context:
 
1
# Copyright (C) 2005, 2006 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
 
 
17
from StringIO import StringIO
 
18
 
 
19
from bzrlib.branch import Branch
 
20
from bzrlib.errors import BzrBadParameterNotString, NoSuchFile, ReadOnlyError
 
21
from bzrlib.lockable_files import LockableFiles, TransportLock
 
22
from bzrlib.lockdir import LockDir
 
23
from bzrlib.tests import TestCaseInTempDir
 
24
from bzrlib.transactions import PassThroughTransaction, ReadOnlyTransaction
 
25
from bzrlib.transport import get_transport
 
26
 
 
27
 
 
28
# these tests are applied in each parameterized suite for LockableFiles
 
29
class _TestLockableFiles_mixin(object):
 
30
 
 
31
    def test_read_write(self):
 
32
        self.assertRaises(NoSuchFile, self.lockable.get, 'foo')
 
33
        self.assertRaises(NoSuchFile, self.lockable.get_utf8, 'foo')
 
34
        self.lockable.lock_write()
 
35
        try:
 
36
            unicode_string = u'bar\u1234'
 
37
            self.assertEqual(4, len(unicode_string))
 
38
            byte_string = unicode_string.encode('utf-8')
 
39
            self.assertEqual(6, len(byte_string))
 
40
            self.assertRaises(UnicodeEncodeError, self.lockable.put, 'foo', 
 
41
                              StringIO(unicode_string))
 
42
            self.lockable.put('foo', StringIO(byte_string))
 
43
            self.assertEqual(byte_string,
 
44
                             self.lockable.get('foo').read())
 
45
            self.assertEqual(unicode_string,
 
46
                             self.lockable.get_utf8('foo').read())
 
47
            self.assertRaises(BzrBadParameterNotString,
 
48
                              self.lockable.put_utf8,
 
49
                              'bar',
 
50
                              StringIO(unicode_string)
 
51
                              )
 
52
            self.lockable.put_utf8('bar', unicode_string)
 
53
            self.assertEqual(unicode_string, 
 
54
                             self.lockable.get_utf8('bar').read())
 
55
            self.assertEqual(byte_string, 
 
56
                             self.lockable.get('bar').read())
 
57
        finally:
 
58
            self.lockable.unlock()
 
59
 
 
60
    def test_locks(self):
 
61
        self.lockable.lock_read()
 
62
        try:
 
63
            self.assertRaises(ReadOnlyError, self.lockable.put, 'foo', 
 
64
                              StringIO('bar\u1234'))
 
65
        finally:
 
66
            self.lockable.unlock()
 
67
 
 
68
    def test_transactions(self):
 
69
        self.assertIs(self.lockable.get_transaction().__class__,
 
70
                      PassThroughTransaction)
 
71
        self.lockable.lock_read()
 
72
        try:
 
73
            self.assertIs(self.lockable.get_transaction().__class__,
 
74
                          ReadOnlyTransaction)
 
75
        finally:
 
76
            self.lockable.unlock()
 
77
        self.assertIs(self.lockable.get_transaction().__class__,
 
78
                      PassThroughTransaction)
 
79
        self.lockable.lock_write()
 
80
        self.assertIs(self.lockable.get_transaction().__class__,
 
81
                      PassThroughTransaction)
 
82
        self.lockable.unlock()
 
83
 
 
84
    def test__escape(self):
 
85
        self.assertEqual('%25', self.lockable._escape('%'))
 
86
        
 
87
    def test__escape_empty(self):
 
88
        self.assertEqual('', self.lockable._escape(''))
 
89
 
 
90
 
 
91
# This method of adapting tests to parameters is different to 
 
92
# the TestProviderAdapters used elsewhere, but seems simpler for this 
 
93
# case.  
 
94
class TestLockableFiles_TransportLock(TestCaseInTempDir,
 
95
                                      _TestLockableFiles_mixin):
 
96
 
 
97
    def setUp(self):
 
98
        super(TestLockableFiles_TransportLock, self).setUp()
 
99
        transport = get_transport('.')
 
100
        transport.mkdir('.bzr')
 
101
        sub_transport = transport.clone('.bzr')
 
102
        self.lockable = LockableFiles(sub_transport, 'my-lock', TransportLock)
 
103
        self.lockable.create_lock()
 
104
        
 
105
 
 
106
class TestLockableFiles_LockDir(TestCaseInTempDir,
 
107
                              _TestLockableFiles_mixin):
 
108
    """LockableFile tests run with LockDir underneath"""
 
109
 
 
110
    def setUp(self):
 
111
        super(TestLockableFiles_LockDir, self).setUp()
 
112
        self.transport = get_transport('.')
 
113
        self.lockable = LockableFiles(self.transport, 'my-lock', LockDir)
 
114
        self.lockable.create_lock()
 
115
 
 
116
    def test_lock_is_lockdir(self):
 
117
        """Created instance should use a LockDir.
 
118
        
 
119
        This primarily tests the mixin adapter works properly.
 
120
        """
 
121
        ## self.assertIsInstance(self.lockable, LockableFiles)
 
122
        ## self.assertIsInstance(self.lockable._lock_strategy,
 
123
                              ## LockDirStrategy)
 
124
 
 
125
    def test_lock_created(self):
 
126
        self.assertTrue(self.transport.has('my-lock'))
 
127
        self.lockable.lock_write()
 
128
        self.assertTrue(self.transport.has('my-lock/held/info'))
 
129
        self.lockable.unlock()
 
130
        self.assertFalse(self.transport.has('my-lock/held/info'))
 
131
        self.assertTrue(self.transport.has('my-lock'))
 
132
 
 
133
 
 
134
    # TODO: Test the lockdir inherits the right file and directory permissions
 
135
    # from the LockableFiles.