69
71
transport._clear_protocol_handlers()
70
72
transport.register_transport_proto('foo')
71
73
transport.register_lazy_transport('foo',
72
'breezy.tests.test_transport',
73
'TestTransport.SampleHandler')
74
'brzlib.tests.test_transport',
75
'TestTransport.SampleHandler')
74
76
transport.register_transport_proto('bar')
75
77
transport.register_lazy_transport('bar',
76
'breezy.tests.test_transport',
77
'TestTransport.SampleHandler')
78
'brzlib.tests.test_transport',
79
'TestTransport.SampleHandler')
78
80
self.assertEqual([SampleHandler.__module__,
79
'breezy.transport.chroot',
80
'breezy.transport.pathfilter'],
81
transport._get_transport_modules())
81
'brzlib.transport.chroot',
82
'brzlib.transport.pathfilter'],
83
transport._get_transport_modules())
83
85
def test_transport_dependency(self):
84
86
"""Transport with missing dependency causes no error"""
88
90
transport._clear_protocol_handlers()
89
91
transport.register_transport_proto('foo')
90
92
transport.register_lazy_transport(
91
'foo', 'breezy.tests.test_transport', 'BadTransportHandler')
93
'foo', 'brzlib.tests.test_transport', 'BadTransportHandler')
93
95
transport.get_transport_from_url('foo://fooserver/foo')
94
except errors.UnsupportedProtocol as e:
96
except errors.UnsupportedProtocol, e:
95
98
self.assertEqual('Unsupported protocol'
96
' for url "foo://fooserver/foo":'
97
' Unable to import library "some_lib":'
98
' testing missing dependency', str(e))
99
' for url "foo://fooserver/foo":'
100
' Unable to import library "some_lib":'
101
' testing missing dependency', str(e))
100
103
self.fail('Did not raise UnsupportedProtocol')
106
109
transport._clear_protocol_handlers()
107
110
transport.register_transport_proto('foo')
108
111
transport.register_lazy_transport(
109
'foo', 'breezy.tests.test_transport', 'BackupTransportHandler')
112
'foo', 'brzlib.tests.test_transport', 'BackupTransportHandler')
110
113
transport.register_lazy_transport(
111
'foo', 'breezy.tests.test_transport', 'BadTransportHandler')
114
'foo', 'brzlib.tests.test_transport', 'BadTransportHandler')
112
115
t = transport.get_transport_from_url('foo://fooserver/foo')
113
116
self.assertTrue(isinstance(t, BackupTransportHandler))
116
119
"""Transport ssh:// should raise an error pointing out bzr+ssh://"""
118
121
transport.get_transport_from_url('ssh://fooserver/foo')
119
except errors.UnsupportedProtocol as e:
121
'Unsupported protocol'
122
' for url "ssh://fooserver/foo":'
123
' Use bzr+ssh for Bazaar operations over SSH, '
124
'e.g. "bzr+ssh://fooserver/foo". Use git+ssh '
125
'for Git operations over SSH, e.g. "git+ssh://fooserver/foo".',
122
except errors.UnsupportedProtocol, e:
124
self.assertEqual('Unsupported protocol'
125
' for url "ssh://fooserver/foo":'
126
' bzr supports bzr+ssh to operate over ssh,'
127
' use "bzr+ssh://fooserver/foo".',
128
130
self.fail('Did not raise UnsupportedProtocol')
176
178
def test_coalesce_overlapped(self):
177
179
self.assertRaises(ValueError,
178
self.check, [(0, 15, [(0, 10), (5, 10)])],
180
self.check, [(0, 15, [(0, 10), (5, 10)])],
181
183
def test_coalesce_limit(self):
182
184
self.check([(10, 50, [(0, 10), (10, 10), (20, 10),
183
185
(30, 10), (40, 10)]),
184
186
(60, 50, [(0, 10), (10, 10), (20, 10),
185
187
(30, 10), (40, 10)]),
186
], [(10, 10), (20, 10), (30, 10), (40, 10),
187
(50, 10), (60, 10), (70, 10), (80, 10),
188
(90, 10), (100, 10)],
188
], [(10, 10), (20, 10), (30, 10), (40, 10),
189
(50, 10), (60, 10), (70, 10), (80, 10),
190
(90, 10), (100, 10)],
191
193
def test_coalesce_no_limit(self):
192
194
self.check([(10, 100, [(0, 10), (10, 10), (20, 10),
193
195
(30, 10), (40, 10), (50, 10),
194
196
(60, 10), (70, 10), (80, 10),
196
], [(10, 10), (20, 10), (30, 10), (40, 10),
197
(50, 10), (60, 10), (70, 10), (80, 10),
198
(90, 10), (100, 10)])
198
], [(10, 10), (20, 10), (30, 10), (40, 10),
199
(50, 10), (60, 10), (70, 10), (80, 10),
200
(90, 10), (100, 10)])
200
202
def test_coalesce_fudge(self):
201
203
self.check([(10, 30, [(0, 10), (20, 10)]),
202
204
(100, 10, [(0, 10)]),
203
], [(10, 10), (30, 10), (100, 10)],
205
], [(10, 10), (30, 10), (100, 10)],
206
208
def test_coalesce_max_size(self):
208
210
(30, 50, [(0, 50)]),
209
211
# If one range is above max_size, it gets its own coalesced
211
(100, 80, [(0, 80)]), ],
213
(100, 80, [(0, 80)]),],
212
214
[(10, 10), (20, 10), (30, 50), (100, 80)],
215
217
def test_coalesce_no_max_size(self):
216
218
self.check([(10, 170, [(0, 10), (10, 10), (20, 50), (70, 100)])],
217
219
[(10, 10), (20, 10), (30, 50), (80, 100)],
220
222
def test_coalesce_default_limit(self):
221
223
# By default we use a 100MB max size.
222
224
ten_mb = 10 * 1024 * 1024
224
[(0, 10 * ten_mb, [(i * ten_mb, ten_mb) for i in range(10)]),
225
(10 * ten_mb, ten_mb, [(0, ten_mb)])],
226
[(i * ten_mb, ten_mb) for i in range(11)])
228
[(0, 11 * ten_mb, [(i * ten_mb, ten_mb) for i in range(11)])],
229
[(i * ten_mb, ten_mb) for i in range(11)],
230
max_size=1 * 1024 * 1024 * 1024)
225
self.check([(0, 10 * ten_mb, [(i * ten_mb, ten_mb) for i in range(10)]),
226
(10*ten_mb, ten_mb, [(0, ten_mb)])],
227
[(i*ten_mb, ten_mb) for i in range(11)])
228
self.check([(0, 11 * ten_mb, [(i * ten_mb, ten_mb) for i in range(11)])],
229
[(i * ten_mb, ten_mb) for i in range(11)],
230
max_size=1*1024*1024*1024)
233
233
class TestMemoryServer(tests.TestCase):
271
271
def test_append_and_get(self):
272
272
t = memory.MemoryTransport()
273
t.append_bytes('path', b'content')
274
self.assertEqual(t.get('path').read(), b'content')
275
t.append_file('path', BytesIO(b'content'))
276
with t.get('path') as f:
277
self.assertEqual(f.read(), b'contentcontent')
273
t.append_bytes('path', 'content')
274
self.assertEqual(t.get('path').read(), 'content')
275
t.append_file('path', StringIO('content'))
276
self.assertEqual(t.get('path').read(), 'contentcontent')
279
278
def test_put_and_get(self):
280
279
t = memory.MemoryTransport()
281
t.put_file('path', BytesIO(b'content'))
282
self.assertEqual(t.get('path').read(), b'content')
283
t.put_bytes('path', b'content')
284
self.assertEqual(t.get('path').read(), b'content')
280
t.put_file('path', StringIO('content'))
281
self.assertEqual(t.get('path').read(), 'content')
282
t.put_bytes('path', 'content')
283
self.assertEqual(t.get('path').read(), 'content')
286
285
def test_append_without_dir_fails(self):
287
286
t = memory.MemoryTransport()
288
287
self.assertRaises(errors.NoSuchFile,
289
t.append_bytes, 'dir/path', b'content')
288
t.append_bytes, 'dir/path', 'content')
291
290
def test_put_without_dir_fails(self):
292
291
t = memory.MemoryTransport()
293
292
self.assertRaises(errors.NoSuchFile,
294
t.put_file, 'dir/path', BytesIO(b'content'))
293
t.put_file, 'dir/path', StringIO('content'))
296
295
def test_get_missing(self):
297
296
transport = memory.MemoryTransport()
304
303
def test_has_present(self):
305
304
t = memory.MemoryTransport()
306
t.append_bytes('foo', b'content')
305
t.append_bytes('foo', 'content')
307
306
self.assertEqual(True, t.has('foo'))
309
308
def test_list_dir(self):
310
309
t = memory.MemoryTransport()
311
t.put_bytes('foo', b'content')
310
t.put_bytes('foo', 'content')
313
t.put_bytes('dir/subfoo', b'content')
314
t.put_bytes('dirlike', b'content')
312
t.put_bytes('dir/subfoo', 'content')
313
t.put_bytes('dirlike', 'content')
316
315
self.assertEqual(['dir', 'dirlike', 'foo'], sorted(t.list_dir('.')))
317
316
self.assertEqual(['subfoo'], sorted(t.list_dir('dir')))
340
338
def test_iter_files_recursive(self):
341
339
t = memory.MemoryTransport()
343
t.put_bytes('dir/foo', b'content')
344
t.put_bytes('dir/bar', b'content')
345
t.put_bytes('bar', b'content')
341
t.put_bytes('dir/foo', 'content')
342
t.put_bytes('dir/bar', 'content')
343
t.put_bytes('bar', 'content')
346
344
paths = set(t.iter_files_recursive())
347
self.assertEqual({'dir/foo', 'dir/bar', 'bar'}, paths)
345
self.assertEqual(set(['dir/foo', 'dir/bar', 'bar']), paths)
349
347
def test_stat(self):
350
348
t = memory.MemoryTransport()
351
t.put_bytes('foo', b'content')
352
t.put_bytes('bar', b'phowar')
349
t.put_bytes('foo', 'content')
350
t.put_bytes('bar', 'phowar')
353
351
self.assertEqual(7, t.stat('foo').st_size)
354
352
self.assertEqual(6, t.stat('bar').st_size)
455
453
"""Check all expected transport hook points are set up"""
456
454
hookpoint = transport.TransportHooks()
457
455
self.assertTrue("post_connect" in hookpoint,
458
"post_connect not in %s" % (hookpoint,))
456
"post_connect not in %s" % (hookpoint,))
460
458
def test_post_connect(self):
461
459
"""Ensure the post_connect hook is called when _set_transport is"""
463
461
transport.Transport.hooks.install_named_hook("post_connect",
465
463
t = self._get_connected_transport()
466
464
self.assertLength(0, calls)
467
465
t._set_connection("connection", "auth")
708
704
t = transport.get_transport_from_path(self.test_dir)
709
705
self.assertIsInstance(t, local.LocalTransport)
710
706
self.assertEqual(t.base.rstrip("/"),
711
urlutils.local_path_to_url(self.test_dir))
707
urlutils.local_path_to_url(self.test_dir))
713
709
def test_with_url(self):
714
710
t = transport.get_transport_from_path("file:")
715
711
self.assertIsInstance(t, local.LocalTransport)
712
self.assertEqual(t.base.rstrip("/"),
718
713
urlutils.local_path_to_url(os.path.join(self.test_dir, "file:")))
721
716
class TestTransportFromUrl(tests.TestCaseInTempDir):
723
718
def test_with_path(self):
724
self.assertRaises(urlutils.InvalidURL, transport.get_transport_from_url,
719
self.assertRaises(errors.InvalidURL, transport.get_transport_from_url,
727
722
def test_with_url(self):
728
723
url = urlutils.local_path_to_url(self.test_dir)
924
919
def test_reuse_same_transport(self):
925
920
possible_transports = []
926
t1 = transport.get_transport_from_url(
927
'http://foo/', possible_transports=possible_transports)
921
t1 = transport.get_transport_from_url('http://foo/',
922
possible_transports=possible_transports)
928
923
self.assertEqual([t1], possible_transports)
929
924
t2 = transport.get_transport_from_url('http://foo/',
930
possible_transports=[t1])
925
possible_transports=[t1])
931
926
self.assertIs(t1, t2)
933
928
# Also check that final '/' are handled correctly
934
929
t3 = transport.get_transport_from_url('http://foo/path/')
935
930
t4 = transport.get_transport_from_url('http://foo/path',
936
possible_transports=[t3])
931
possible_transports=[t3])
937
932
self.assertIs(t3, t4)
939
934
t5 = transport.get_transport_from_url('http://foo/path')
940
935
t6 = transport.get_transport_from_url('http://foo/path/',
941
possible_transports=[t5])
936
possible_transports=[t5])
942
937
self.assertIs(t5, t6)
944
939
def test_don_t_reuse_different_transport(self):
945
940
t1 = transport.get_transport_from_url('http://foo/path')
946
941
t2 = transport.get_transport_from_url('http://bar/path',
947
possible_transports=[t1])
942
possible_transports=[t1])
948
943
self.assertIsNot(t1, t2)
1105
1098
result = http.unhtml_roughly(fake_html)
1106
1099
self.assertEqual(len(result), 1000)
1107
1100
self.assertStartsWith(result, " something!")
1103
class SomeDirectory(object):
1105
def look_up(self, name, url):
1109
class TestLocationToUrl(tests.TestCase):
1111
def get_base_location(self):
1112
path = osutils.abspath('/foo/bar')
1113
if path.startswith('/'):
1114
url = 'file://%s' % (path,)
1116
# On Windows, abspaths start with the drive letter, so we have to
1117
# add in the extra '/'
1118
url = 'file:///%s' % (path,)
1121
def test_regular_url(self):
1122
self.assertEqual("file://foo", location_to_url("file://foo"))
1124
def test_directory(self):
1125
directories.register("bar:", SomeDirectory, "Dummy directory")
1126
self.addCleanup(directories.remove, "bar:")
1127
self.assertEqual("http://bar", location_to_url("bar:"))
1129
def test_unicode_url(self):
1130
self.assertRaises(errors.InvalidURL, location_to_url,
1131
"http://fo/\xc3\xaf".decode("utf-8"))
1133
def test_unicode_path(self):
1134
path, url = self.get_base_location()
1135
location = path + "\xc3\xaf".decode("utf-8")
1137
self.assertEqual(url, location_to_url(location))
1139
def test_path(self):
1140
path, url = self.get_base_location()
1141
self.assertEqual(url, location_to_url(path))
1143
def test_relative_file_url(self):
1144
self.assertEqual(urlutils.local_path_to_url(".") + "/bar",
1145
location_to_url("file:bar"))
1147
def test_absolute_file_url(self):
1148
self.assertEqual("file:///bar", location_to_url("file:/bar"))