~bzr-pqm/bzr/bzr.dev

« back to all changes in this revision

Viewing changes to bzrlib/tests/test_lockable_files.py

  • Committer: Canonical.com Patch Queue Manager
  • Date: 2009-06-22 17:11:20 UTC
  • mfrom: (4398.8.10 1.16-commit-fulltext)
  • Revision ID: pqm@pqm.ubuntu.com-20090622171120-fuxez9ylfqpxynqn
(jam) Add VF._add_text and reduce memory overhead during commit (see
        bug #109114)

Show diffs side-by-side

added added

removed removed

Lines of Context:
1
 
# Copyright (C) 2005-2011 Canonical Ltd
 
1
# Copyright (C) 2005, 2006, 2008 Canonical Ltd
2
2
#
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
14
14
# along with this program; if not, write to the Free Software
15
15
# Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
16
16
 
 
17
from StringIO import StringIO
 
18
 
17
19
import bzrlib
18
20
from bzrlib import (
19
21
    errors,
20
22
    lockdir,
21
23
    osutils,
22
 
    transport,
23
24
    )
 
25
from bzrlib.errors import BzrBadParameterNotString, NoSuchFile, ReadOnlyError
24
26
from bzrlib.lockable_files import LockableFiles, TransportLock
 
27
from bzrlib.symbol_versioning import (
 
28
    deprecated_in,
 
29
    )
25
30
from bzrlib.tests import (
26
31
    TestCaseInTempDir,
27
32
    TestNotApplicable,
32
37
                                 ReadOnlyTransaction,
33
38
                                 WriteTransaction,
34
39
                                 )
 
40
from bzrlib.transport import get_transport
35
41
 
36
42
 
37
43
# these tests are applied in each parameterized suite for LockableFiles
40
46
# so won't modernize them now. - mbp 20080430
41
47
class _TestLockableFiles_mixin(object):
42
48
 
 
49
    def test_read_write(self):
 
50
        self.assertRaises(NoSuchFile,
 
51
            self.applyDeprecated,
 
52
            deprecated_in((1, 5, 0)),
 
53
            self.lockable.get, 'foo')
 
54
        self.assertRaises(NoSuchFile,
 
55
            self.applyDeprecated,
 
56
            deprecated_in((1, 5, 0)),
 
57
            self.lockable.get_utf8, 'foo')
 
58
        self.lockable.lock_write()
 
59
        self.addCleanup(self.lockable.unlock)
 
60
        unicode_string = u'bar\u1234'
 
61
        self.assertEqual(4, len(unicode_string))
 
62
        byte_string = unicode_string.encode('utf-8')
 
63
        self.assertEqual(6, len(byte_string))
 
64
        self.assertRaises(UnicodeEncodeError,
 
65
            self.applyDeprecated,
 
66
            deprecated_in((1, 6, 0)),
 
67
            self.lockable.put, 'foo',
 
68
            StringIO(unicode_string))
 
69
        self.applyDeprecated(
 
70
            deprecated_in((1, 6, 0)),
 
71
            self.lockable.put,
 
72
            'foo', StringIO(byte_string))
 
73
        byte_stream = self.applyDeprecated(
 
74
            deprecated_in((1, 5, 0)),
 
75
            self.lockable.get,
 
76
            'foo')
 
77
        self.assertEqual(byte_string, byte_stream.read())
 
78
        unicode_stream = self.applyDeprecated(
 
79
            deprecated_in((1, 5, 0)),
 
80
            self.lockable.get_utf8,
 
81
            'foo')
 
82
        self.assertEqual(unicode_string,
 
83
            unicode_stream.read())
 
84
        self.assertRaises(BzrBadParameterNotString,
 
85
            self.applyDeprecated,
 
86
            deprecated_in((1, 6, 0)),
 
87
            self.lockable.put_utf8,
 
88
            'bar',
 
89
            StringIO(unicode_string))
 
90
        self.applyDeprecated(
 
91
            deprecated_in((1, 6, 0)),
 
92
            self.lockable.put_utf8,
 
93
            'bar',
 
94
            unicode_string)
 
95
        unicode_stream = self.applyDeprecated(
 
96
            deprecated_in((1, 5, 0)),
 
97
            self.lockable.get_utf8,
 
98
            'bar')
 
99
        self.assertEqual(unicode_string,
 
100
            unicode_stream.read())
 
101
        byte_stream = self.applyDeprecated(
 
102
            deprecated_in((1, 5, 0)),
 
103
            self.lockable.get,
 
104
            'bar')
 
105
        self.assertEqual(byte_string, byte_stream.read())
 
106
        self.applyDeprecated(
 
107
            deprecated_in((1, 6, 0)),
 
108
            self.lockable.put_bytes,
 
109
            'raw', 'raw\xffbytes')
 
110
        byte_stream = self.applyDeprecated(
 
111
            deprecated_in((1, 5, 0)),
 
112
            self.lockable.get,
 
113
            'raw')
 
114
        self.assertEqual('raw\xffbytes', byte_stream.read())
 
115
 
 
116
    def test_locks(self):
 
117
        self.lockable.lock_read()
 
118
        self.addCleanup(self.lockable.unlock)
 
119
        self.assertRaises(ReadOnlyError, self.lockable.put, 'foo',
 
120
                          StringIO('bar\u1234'))
 
121
 
43
122
    def test_transactions(self):
44
123
        self.assertIs(self.lockable.get_transaction().__class__,
45
124
                      PassThroughTransaction)
78
157
        l2 = self.get_lockable()
79
158
        orig_factory = bzrlib.ui.ui_factory
80
159
        # silent ui - no need for stdout
81
 
        bzrlib.ui.ui_factory = bzrlib.ui.CannedInputUIFactory([True])
 
160
        bzrlib.ui.ui_factory = bzrlib.ui.SilentUIFactory()
 
161
        bzrlib.ui.ui_factory.stdin = StringIO("y\n")
82
162
        try:
83
163
            l2.break_lock()
84
164
        finally:
273
353
 
274
354
    def setUp(self):
275
355
        TestCaseInTempDir.setUp(self)
276
 
        t = transport.get_transport('.')
277
 
        t.mkdir('.bzr')
278
 
        self.sub_transport = t.clone('.bzr')
 
356
        transport = get_transport('.')
 
357
        transport.mkdir('.bzr')
 
358
        self.sub_transport = transport.clone('.bzr')
279
359
        self.lockable = self.get_lockable()
280
360
        self.lockable.create_lock()
281
361
 
282
 
    def stop_server(self):
283
 
        super(TestLockableFiles_TransportLock, self).stop_server()
 
362
    def tearDown(self):
 
363
        super(TestLockableFiles_TransportLock, self).tearDown()
284
364
        # free the subtransport so that we do not get a 5 second
285
365
        # timeout due to the SFTP connection cache.
286
366
        try:
298
378
 
299
379
    def setUp(self):
300
380
        TestCaseInTempDir.setUp(self)
301
 
        self.transport = transport.get_transport('.')
 
381
        self.transport = get_transport('.')
302
382
        self.lockable = self.get_lockable()
303
383
        # the lock creation here sets mode - test_permissions on branch
304
384
        # tests that implicitly, but it might be a good idea to factor
341
421
        # in test_remote and test_smart as usual.
342
422
        b = self.make_branch('foo')
343
423
        self.addCleanup(b.bzrdir.transport.disconnect)
344
 
        self.transport = transport.get_transport('.')
 
424
        self.transport = get_transport('.')
345
425
        self.lockable = self.get_lockable()
346
426
 
347
427
    def get_lockable(self):