~bzr-pqm/bzr/bzr.dev

« back to all changes in this revision

Viewing changes to bzrlib/tests/test_transport.py

Merge bzr.dev

Show diffs side-by-side

added added

removed removed

Lines of Context:
1
 
# Copyright (C) 2004, 2005 by Canonical Ltd
 
1
# Copyright (C) 2004, 2005, 2006 by 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
21
21
from cStringIO import StringIO
22
22
 
23
23
from bzrlib.errors import (NoSuchFile, FileExists,
24
 
                           TransportNotPossible, ConnectionError)
 
24
                           TransportNotPossible,
 
25
                           ConnectionError,
 
26
                           DependencyNotPresent,
 
27
                           )
25
28
from bzrlib.tests import TestCase
26
29
from bzrlib.transport import (_get_protocol_handlers,
27
30
                              _get_transport_modules,
29
32
                              register_lazy_transport,
30
33
                              _set_protocol_handlers,
31
34
                              urlescape,
 
35
                              Transport,
32
36
                              )
 
37
from bzrlib.transport.memory import MemoryTransport
 
38
from bzrlib.transport.local import LocalTransport
33
39
 
34
40
 
35
41
class TestTransport(TestCase):
60
66
                             _get_transport_modules())
61
67
        finally:
62
68
            _set_protocol_handlers(handlers)
63
 
            
64
 
 
65
 
class MemoryTransportTest(TestCase):
66
 
    """Memory transport specific tests."""
 
69
 
 
70
    def test_transport_dependency(self):
 
71
        """Transport with missing dependency causes no error"""
 
72
        saved_handlers = _get_protocol_handlers()
 
73
        try:
 
74
            register_lazy_transport('foo', 'bzrlib.tests.test_transport',
 
75
                    'BadTransportHandler')
 
76
            t = get_transport('foo://fooserver/foo')
 
77
            # because we failed to load the transport
 
78
            self.assertTrue(isinstance(t, LocalTransport))
 
79
        finally:
 
80
            # restore original values
 
81
            _set_protocol_handlers(saved_handlers)
 
82
            
 
83
    def test_transport_fallback(self):
 
84
        """Transport with missing dependency causes no error"""
 
85
        saved_handlers = _get_protocol_handlers()
 
86
        try:
 
87
            _set_protocol_handlers({})
 
88
            register_lazy_transport('foo', 'bzrlib.tests.test_transport',
 
89
                    'BackupTransportHandler')
 
90
            register_lazy_transport('foo', 'bzrlib.tests.test_transport',
 
91
                    'BadTransportHandler')
 
92
            t = get_transport('foo://fooserver/foo')
 
93
            self.assertTrue(isinstance(t, BackupTransportHandler))
 
94
        finally:
 
95
            _set_protocol_handlers(saved_handlers)
 
96
            
 
97
 
 
98
class TestMemoryTransport(TestCase):
 
99
 
 
100
    def test_get_transport(self):
 
101
        MemoryTransport()
 
102
 
 
103
    def test_clone(self):
 
104
        transport = MemoryTransport()
 
105
        self.assertTrue(isinstance(transport, MemoryTransport))
 
106
 
 
107
    def test_abspath(self):
 
108
        transport = MemoryTransport()
 
109
        self.assertEqual("memory:/relpath", transport.abspath('relpath'))
 
110
 
 
111
    def test_relpath(self):
 
112
        transport = MemoryTransport()
 
113
 
 
114
    def test_append_and_get(self):
 
115
        transport = MemoryTransport()
 
116
        transport.append('path', StringIO('content'))
 
117
        self.assertEqual(transport.get('path').read(), 'content')
 
118
        transport.append('path', StringIO('content'))
 
119
        self.assertEqual(transport.get('path').read(), 'contentcontent')
 
120
 
 
121
    def test_put_and_get(self):
 
122
        transport = MemoryTransport()
 
123
        transport.put('path', StringIO('content'))
 
124
        self.assertEqual(transport.get('path').read(), 'content')
 
125
        transport.put('path', StringIO('content'))
 
126
        self.assertEqual(transport.get('path').read(), 'content')
 
127
 
 
128
    def test_append_without_dir_fails(self):
 
129
        transport = MemoryTransport()
 
130
        self.assertRaises(NoSuchFile,
 
131
                          transport.append, 'dir/path', StringIO('content'))
 
132
 
 
133
    def test_put_without_dir_fails(self):
 
134
        transport = MemoryTransport()
 
135
        self.assertRaises(NoSuchFile,
 
136
                          transport.put, 'dir/path', StringIO('content'))
 
137
 
 
138
    def test_get_missing(self):
 
139
        transport = MemoryTransport()
 
140
        self.assertRaises(NoSuchFile, transport.get, 'foo')
 
141
 
 
142
    def test_has_missing(self):
 
143
        transport = MemoryTransport()
 
144
        self.assertEquals(False, transport.has('foo'))
 
145
 
 
146
    def test_has_present(self):
 
147
        transport = MemoryTransport()
 
148
        transport.append('foo', StringIO('content'))
 
149
        self.assertEquals(True, transport.has('foo'))
 
150
 
 
151
    def test_mkdir(self):
 
152
        transport = MemoryTransport()
 
153
        transport.mkdir('dir')
 
154
        transport.append('dir/path', StringIO('content'))
 
155
        self.assertEqual(transport.get('dir/path').read(), 'content')
 
156
 
 
157
    def test_mkdir_missing_parent(self):
 
158
        transport = MemoryTransport()
 
159
        self.assertRaises(NoSuchFile,
 
160
                          transport.mkdir, 'dir/dir')
 
161
 
 
162
    def test_mkdir_twice(self):
 
163
        transport = MemoryTransport()
 
164
        transport.mkdir('dir')
 
165
        self.assertRaises(FileExists, transport.mkdir, 'dir')
67
166
 
68
167
    def test_parameters(self):
69
 
        import bzrlib.transport.memory as memory
70
 
        transport = memory.MemoryTransport()
 
168
        transport = MemoryTransport()
71
169
        self.assertEqual(True, transport.listable())
72
170
        self.assertEqual(False, transport.should_cache())
73
171
        self.assertEqual(False, transport.is_readonly())
74
 
        
 
172
 
 
173
    def test_iter_files_recursive(self):
 
174
        transport = MemoryTransport()
 
175
        transport.mkdir('dir')
 
176
        transport.put('dir/foo', StringIO('content'))
 
177
        transport.put('dir/bar', StringIO('content'))
 
178
        transport.put('bar', StringIO('content'))
 
179
        paths = set(transport.iter_files_recursive())
 
180
        self.assertEqual(set(['dir/foo', 'dir/bar', 'bar']), paths)
 
181
 
 
182
    def test_stat(self):
 
183
        transport = MemoryTransport()
 
184
        transport.put('foo', StringIO('content'))
 
185
        transport.put('bar', StringIO('phowar'))
 
186
        self.assertEqual(7, transport.stat('foo').st_size)
 
187
        self.assertEqual(6, transport.stat('bar').st_size)
 
188
 
75
189
        
76
190
class ReadonlyDecoratorTransportTest(TestCase):
77
191
    """Readonly decoration specific tests."""
99
213
            self.assertEqual(True, transport.is_readonly())
100
214
        finally:
101
215
            server.tearDown()
 
216
 
 
217
 
 
218
class BadTransportHandler(Transport):
 
219
    def __init__(self, base_url):
 
220
        raise DependencyNotPresent('some_lib', 'testing missing dependency')
 
221
 
 
222
 
 
223
class BackupTransportHandler(Transport):
 
224
    """Test transport that works as a backup for the BadTransportHandler"""
 
225
    pass