49
52
class TestTransport(tests.TestCase):
50
53
"""Test the non transport-concrete class functionality."""
52
# FIXME: These tests should use addCleanup() and/or overrideAttr() instead
53
# of try/finally -- vila 20100205
55
55
def test__get_set_protocol_handlers(self):
56
56
handlers = transport._get_protocol_handlers()
57
self.assertNotEqual([], handlers.keys( ))
59
transport._clear_protocol_handlers()
60
self.assertEqual([], transport._get_protocol_handlers().keys())
62
transport._set_protocol_handlers(handlers)
57
self.assertNotEqual([], handlers.keys())
58
transport._clear_protocol_handlers()
59
self.addCleanup(transport._set_protocol_handlers, handlers)
60
self.assertEqual([], transport._get_protocol_handlers().keys())
64
62
def test_get_transport_modules(self):
65
63
handlers = transport._get_protocol_handlers()
64
self.addCleanup(transport._set_protocol_handlers, handlers)
66
65
# don't pollute the current handlers
67
66
transport._clear_protocol_handlers()
68
68
class SampleHandler(object):
69
69
"""I exist, isnt that enough?"""
71
transport._clear_protocol_handlers()
72
transport.register_transport_proto('foo')
73
transport.register_lazy_transport('foo',
74
'bzrlib.tests.test_transport',
75
'TestTransport.SampleHandler')
76
transport.register_transport_proto('bar')
77
transport.register_lazy_transport('bar',
78
'bzrlib.tests.test_transport',
79
'TestTransport.SampleHandler')
80
self.assertEqual([SampleHandler.__module__,
81
'bzrlib.transport.chroot',
82
'bzrlib.transport.pathfilter'],
83
transport._get_transport_modules())
85
transport._set_protocol_handlers(handlers)
70
transport._clear_protocol_handlers()
71
transport.register_transport_proto('foo')
72
transport.register_lazy_transport('foo',
73
'bzrlib.tests.test_transport',
74
'TestTransport.SampleHandler')
75
transport.register_transport_proto('bar')
76
transport.register_lazy_transport('bar',
77
'bzrlib.tests.test_transport',
78
'TestTransport.SampleHandler')
79
self.assertEqual([SampleHandler.__module__,
80
'bzrlib.transport.chroot',
81
'bzrlib.transport.pathfilter'],
82
transport._get_transport_modules())
87
84
def test_transport_dependency(self):
88
85
"""Transport with missing dependency causes no error"""
89
86
saved_handlers = transport._get_protocol_handlers()
87
self.addCleanup(transport._set_protocol_handlers, saved_handlers)
90
88
# don't pollute the current handlers
91
89
transport._clear_protocol_handlers()
90
transport.register_transport_proto('foo')
91
transport.register_lazy_transport(
92
'foo', 'bzrlib.tests.test_transport', 'BadTransportHandler')
93
transport.register_transport_proto('foo')
94
transport.register_lazy_transport(
95
'foo', 'bzrlib.tests.test_transport', 'BadTransportHandler')
97
transport.get_transport('foo://fooserver/foo')
98
except errors.UnsupportedProtocol, e:
100
self.assertEquals('Unsupported protocol'
101
' for url "foo://fooserver/foo":'
102
' Unable to import library "some_lib":'
103
' testing missing dependency', str(e))
105
self.fail('Did not raise UnsupportedProtocol')
107
# restore original values
108
transport._set_protocol_handlers(saved_handlers)
94
transport.get_transport_from_url('foo://fooserver/foo')
95
except errors.UnsupportedProtocol, e:
97
self.assertEquals('Unsupported protocol'
98
' for url "foo://fooserver/foo":'
99
' Unable to import library "some_lib":'
100
' testing missing dependency', str(e))
102
self.fail('Did not raise UnsupportedProtocol')
110
104
def test_transport_fallback(self):
111
105
"""Transport with missing dependency causes no error"""
112
106
saved_handlers = transport._get_protocol_handlers()
114
transport._clear_protocol_handlers()
115
transport.register_transport_proto('foo')
116
transport.register_lazy_transport(
117
'foo', 'bzrlib.tests.test_transport', 'BackupTransportHandler')
118
transport.register_lazy_transport(
119
'foo', 'bzrlib.tests.test_transport', 'BadTransportHandler')
120
t = transport.get_transport('foo://fooserver/foo')
121
self.assertTrue(isinstance(t, BackupTransportHandler))
123
transport._set_protocol_handlers(saved_handlers)
107
self.addCleanup(transport._set_protocol_handlers, saved_handlers)
108
transport._clear_protocol_handlers()
109
transport.register_transport_proto('foo')
110
transport.register_lazy_transport(
111
'foo', 'bzrlib.tests.test_transport', 'BackupTransportHandler')
112
transport.register_lazy_transport(
113
'foo', 'bzrlib.tests.test_transport', 'BadTransportHandler')
114
t = transport.get_transport_from_url('foo://fooserver/foo')
115
self.assertTrue(isinstance(t, BackupTransportHandler))
125
117
def test_ssh_hints(self):
126
118
"""Transport ssh:// should raise an error pointing out bzr+ssh://"""
128
transport.get_transport('ssh://fooserver/foo')
120
transport.get_transport_from_url('ssh://fooserver/foo')
129
121
except errors.UnsupportedProtocol, e:
131
123
self.assertEquals('Unsupported protocol'
220
212
def test_coalesce_fudge(self):
221
213
self.check([(10, 30, [(0, 10), (20, 10)]),
222
(100, 10, [(0, 10),]),
214
(100, 10, [(0, 10)]),
223
215
], [(10, 10), (30, 10), (100, 10)],
226
218
def test_coalesce_max_size(self):
227
219
self.check([(10, 20, [(0, 10), (10, 10)]),
228
220
(30, 50, [(0, 50)]),
229
221
# If one range is above max_size, it gets its own coalesced
231
(100, 80, [(0, 80),]),],
223
(100, 80, [(0, 80)]),],
232
224
[(10, 10), (20, 10), (30, 50), (100, 80)],
236
227
def test_coalesce_no_max_size(self):
237
self.check([(10, 170, [(0, 10), (10, 10), (20, 50), (70, 100)]),],
228
self.check([(10, 170, [(0, 10), (10, 10), (20, 50), (70, 100)])],
238
229
[(10, 10), (20, 10), (30, 50), (80, 100)],
241
232
def test_coalesce_default_limit(self):
242
233
# By default we use a 100MB max size.
243
ten_mb = 10*1024*1024
244
self.check([(0, 10*ten_mb, [(i*ten_mb, ten_mb) for i in range(10)]),
234
ten_mb = 10 * 1024 * 1024
235
self.check([(0, 10 * ten_mb, [(i * ten_mb, ten_mb) for i in range(10)]),
245
236
(10*ten_mb, ten_mb, [(0, ten_mb)])],
246
237
[(i*ten_mb, ten_mb) for i in range(11)])
247
self.check([(0, 11*ten_mb, [(i*ten_mb, ten_mb) for i in range(11)]),],
248
[(i*ten_mb, ten_mb) for i in range(11)],
238
self.check([(0, 11 * ten_mb, [(i * ten_mb, ten_mb) for i in range(11)])],
239
[(i * ten_mb, ten_mb) for i in range(11)],
249
240
max_size=1*1024*1024*1024)
688
class TestTransportFromPath(tests.TestCaseInTempDir):
690
def test_with_path(self):
691
t = transport.get_transport_from_path(self.test_dir)
692
self.assertIsInstance(t, local.LocalTransport)
693
self.assertEquals(t.base.rstrip("/"),
694
urlutils.local_path_to_url(self.test_dir))
696
def test_with_url(self):
697
t = transport.get_transport_from_path("file:")
698
self.assertIsInstance(t, local.LocalTransport)
699
self.assertEquals(t.base.rstrip("/"),
700
urlutils.local_path_to_url(os.path.join(self.test_dir, "file:")))
703
class TestTransportFromUrl(tests.TestCaseInTempDir):
705
def test_with_path(self):
706
self.assertRaises(errors.InvalidURL, transport.get_transport_from_url,
709
def test_with_url(self):
710
url = urlutils.local_path_to_url(self.test_dir)
711
t = transport.get_transport_from_url(url)
712
self.assertIsInstance(t, local.LocalTransport)
713
self.assertEquals(t.base.rstrip("/"), url)
699
716
class TestLocalTransports(tests.TestCase):
701
718
def test_get_transport_from_abspath(self):
847
869
def test_reuse_same_transport(self):
848
870
possible_transports = []
849
t1 = transport.get_transport('http://foo/',
871
t1 = transport.get_transport_from_url('http://foo/',
850
872
possible_transports=possible_transports)
851
873
self.assertEqual([t1], possible_transports)
852
t2 = transport.get_transport('http://foo/',
874
t2 = transport.get_transport_from_url('http://foo/',
853
875
possible_transports=[t1])
854
876
self.assertIs(t1, t2)
856
878
# Also check that final '/' are handled correctly
857
t3 = transport.get_transport('http://foo/path/')
858
t4 = transport.get_transport('http://foo/path',
879
t3 = transport.get_transport_from_url('http://foo/path/')
880
t4 = transport.get_transport_from_url('http://foo/path',
859
881
possible_transports=[t3])
860
882
self.assertIs(t3, t4)
862
t5 = transport.get_transport('http://foo/path')
863
t6 = transport.get_transport('http://foo/path/',
884
t5 = transport.get_transport_from_url('http://foo/path')
885
t6 = transport.get_transport_from_url('http://foo/path/',
864
886
possible_transports=[t5])
865
887
self.assertIs(t5, t6)
867
889
def test_don_t_reuse_different_transport(self):
868
t1 = transport.get_transport('http://foo/path')
869
t2 = transport.get_transport('http://bar/path',
890
t1 = transport.get_transport_from_url('http://foo/path')
891
t2 = transport.get_transport_from_url('http://bar/path',
870
892
possible_transports=[t1])
871
893
self.assertIsNot(t1, t2)
874
896
class TestTransportTrace(tests.TestCase):
877
t = transport.get_transport('trace+memory://')
878
self.assertIsInstance(t, bzrlib.transport.trace.TransportTraceDecorator)
898
def test_decorator(self):
899
t = transport.get_transport_from_url('trace+memory://')
900
self.assertIsInstance(
901
t, bzrlib.transport.trace.TransportTraceDecorator)
880
903
def test_clone_preserves_activity(self):
881
t = transport.get_transport('trace+memory://')
904
t = transport.get_transport_from_url('trace+memory://')
882
905
t2 = t.clone('.')
883
906
self.assertTrue(t is not t2)
884
907
self.assertTrue(t._activity is t2._activity)
1024
1048
result = http.unhtml_roughly(fake_html)
1025
1049
self.assertEquals(len(result), 1000)
1026
1050
self.assertStartsWith(result, " something!")
1053
class SomeDirectory(object):
1055
def look_up(self, name, url):
1059
class TestLocationToUrl(tests.TestCase):
1061
def test_regular_url(self):
1062
self.assertEquals("file://foo", location_to_url("file://foo"))
1064
def test_directory(self):
1065
directories.register("bar:", SomeDirectory, "Dummy directory")
1066
self.addCleanup(directories.remove, "bar:")
1067
self.assertEquals("http://bar", location_to_url("bar:"))
1069
def test_unicode_url(self):
1070
self.assertRaises(errors.InvalidURL, location_to_url,
1071
"http://fo/\xc3\xaf".decode("utf-8"))
1073
def test_unicode_path(self):
1074
self.assertEquals("file:///foo/bar%C3%AF",
1075
location_to_url("/foo/bar\xc3\xaf".decode("utf-8")))
1077
def test_path(self):
1078
self.assertEquals("file:///foo/bar", location_to_url("/foo/bar"))
1080
def test_relative_file_url(self):
1081
self.assertEquals(urlutils.local_path_to_url(".") + "/bar",
1082
location_to_url("file:bar"))
1084
def test_absolute_file_url(self):
1085
self.assertEquals("file:///bar", location_to_url("file:/bar"))