48
52
class TestTransport(tests.TestCase):
49
53
"""Test the non transport-concrete class functionality."""
51
# FIXME: These tests should use addCleanup() and/or overrideAttr() instead
52
# of try/finally -- vila 20100205
54
55
def test__get_set_protocol_handlers(self):
55
56
handlers = transport._get_protocol_handlers()
56
self.assertNotEqual([], handlers.keys( ))
58
transport._clear_protocol_handlers()
59
self.assertEqual([], transport._get_protocol_handlers().keys())
61
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())
63
62
def test_get_transport_modules(self):
64
63
handlers = transport._get_protocol_handlers()
64
self.addCleanup(transport._set_protocol_handlers, handlers)
65
65
# don't pollute the current handlers
66
66
transport._clear_protocol_handlers()
67
68
class SampleHandler(object):
68
69
"""I exist, isnt that enough?"""
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())
84
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())
86
84
def test_transport_dependency(self):
87
85
"""Transport with missing dependency causes no error"""
88
86
saved_handlers = transport._get_protocol_handlers()
87
self.addCleanup(transport._set_protocol_handlers, saved_handlers)
89
88
# don't pollute the current handlers
90
89
transport._clear_protocol_handlers()
90
transport.register_transport_proto('foo')
91
transport.register_lazy_transport(
92
'foo', 'bzrlib.tests.test_transport', 'BadTransportHandler')
92
transport.register_transport_proto('foo')
93
transport.register_lazy_transport(
94
'foo', 'bzrlib.tests.test_transport', 'BadTransportHandler')
96
transport.get_transport('foo://fooserver/foo')
97
except errors.UnsupportedProtocol, e:
99
self.assertEquals('Unsupported protocol'
100
' for url "foo://fooserver/foo":'
101
' Unable to import library "some_lib":'
102
' testing missing dependency', str(e))
104
self.fail('Did not raise UnsupportedProtocol')
106
# restore original values
107
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')
109
104
def test_transport_fallback(self):
110
105
"""Transport with missing dependency causes no error"""
111
106
saved_handlers = transport._get_protocol_handlers()
113
transport._clear_protocol_handlers()
114
transport.register_transport_proto('foo')
115
transport.register_lazy_transport(
116
'foo', 'bzrlib.tests.test_transport', 'BackupTransportHandler')
117
transport.register_lazy_transport(
118
'foo', 'bzrlib.tests.test_transport', 'BadTransportHandler')
119
t = transport.get_transport('foo://fooserver/foo')
120
self.assertTrue(isinstance(t, BackupTransportHandler))
122
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))
124
117
def test_ssh_hints(self):
125
118
"""Transport ssh:// should raise an error pointing out bzr+ssh://"""
127
transport.get_transport('ssh://fooserver/foo')
120
transport.get_transport_from_url('ssh://fooserver/foo')
128
121
except errors.UnsupportedProtocol, e:
130
123
self.assertEquals('Unsupported protocol'
219
212
def test_coalesce_fudge(self):
220
213
self.check([(10, 30, [(0, 10), (20, 10)]),
221
(100, 10, [(0, 10),]),
214
(100, 10, [(0, 10)]),
222
215
], [(10, 10), (30, 10), (100, 10)],
225
218
def test_coalesce_max_size(self):
226
219
self.check([(10, 20, [(0, 10), (10, 10)]),
227
220
(30, 50, [(0, 50)]),
228
221
# If one range is above max_size, it gets its own coalesced
230
(100, 80, [(0, 80),]),],
223
(100, 80, [(0, 80)]),],
231
224
[(10, 10), (20, 10), (30, 50), (100, 80)],
235
227
def test_coalesce_no_max_size(self):
236
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)])],
237
229
[(10, 10), (20, 10), (30, 50), (80, 100)],
240
232
def test_coalesce_default_limit(self):
241
233
# By default we use a 100MB max size.
242
ten_mb = 10*1024*1024
243
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)]),
244
236
(10*ten_mb, ten_mb, [(0, ten_mb)])],
245
237
[(i*ten_mb, ten_mb) for i in range(11)])
246
self.check([(0, 11*ten_mb, [(i*ten_mb, ten_mb) for i in range(11)]),],
247
[(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)],
248
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)
698
716
class TestLocalTransports(tests.TestCase):
700
718
def test_get_transport_from_abspath(self):
827
865
def test_reuse_same_transport(self):
828
866
possible_transports = []
829
t1 = transport.get_transport('http://foo/',
867
t1 = transport.get_transport_from_url('http://foo/',
830
868
possible_transports=possible_transports)
831
869
self.assertEqual([t1], possible_transports)
832
t2 = transport.get_transport('http://foo/',
870
t2 = transport.get_transport_from_url('http://foo/',
833
871
possible_transports=[t1])
834
872
self.assertIs(t1, t2)
836
874
# Also check that final '/' are handled correctly
837
t3 = transport.get_transport('http://foo/path/')
838
t4 = transport.get_transport('http://foo/path',
875
t3 = transport.get_transport_from_url('http://foo/path/')
876
t4 = transport.get_transport_from_url('http://foo/path',
839
877
possible_transports=[t3])
840
878
self.assertIs(t3, t4)
842
t5 = transport.get_transport('http://foo/path')
843
t6 = transport.get_transport('http://foo/path/',
880
t5 = transport.get_transport_from_url('http://foo/path')
881
t6 = transport.get_transport_from_url('http://foo/path/',
844
882
possible_transports=[t5])
845
883
self.assertIs(t5, t6)
847
885
def test_don_t_reuse_different_transport(self):
848
t1 = transport.get_transport('http://foo/path')
849
t2 = transport.get_transport('http://bar/path',
886
t1 = transport.get_transport_from_url('http://foo/path')
887
t2 = transport.get_transport_from_url('http://bar/path',
850
888
possible_transports=[t1])
851
889
self.assertIsNot(t1, t2)
854
892
class TestTransportTrace(tests.TestCase):
857
t = transport.get_transport('trace+memory://')
858
self.assertIsInstance(t, bzrlib.transport.trace.TransportTraceDecorator)
894
def test_decorator(self):
895
t = transport.get_transport_from_url('trace+memory://')
896
self.assertIsInstance(
897
t, bzrlib.transport.trace.TransportTraceDecorator)
860
899
def test_clone_preserves_activity(self):
861
t = transport.get_transport('trace+memory://')
900
t = transport.get_transport_from_url('trace+memory://')
862
901
t2 = t.clone('.')
863
902
self.assertTrue(t is not t2)
864
903
self.assertTrue(t._activity is t2._activity)
989
1033
# And the rest are threads
990
1034
for t in started[1:]:
1038
class TestUnhtml(tests.TestCase):
1040
"""Tests for unhtml_roughly"""
1042
def test_truncation(self):
1043
fake_html = "<p>something!\n" * 1000
1044
result = http.unhtml_roughly(fake_html)
1045
self.assertEquals(len(result), 1000)
1046
self.assertStartsWith(result, " something!")
1049
class SomeDirectory(object):
1051
def look_up(self, name, url):
1055
class TestLocationToUrl(tests.TestCase):
1057
def test_regular_url(self):
1058
self.assertEquals("file://foo", location_to_url("file://foo"))
1060
def test_directory(self):
1061
directories.register("bar:", SomeDirectory, "Dummy directory")
1062
self.addCleanup(directories.remove, "bar:")
1063
self.assertEquals("http://bar", location_to_url("bar:"))
1065
def test_unicode_url(self):
1066
self.assertRaises(errors.InvalidURL, location_to_url,
1067
"http://fo/\xc3\xaf".decode("utf-8"))
1069
def test_unicode_path(self):
1070
self.assertEquals("file:///foo/bar%C3%AF",
1071
location_to_url("/foo/bar\xc3\xaf".decode("utf-8")))
1073
def test_path(self):
1074
self.assertEquals("file:///foo/bar", location_to_url("/foo/bar"))
1076
def test_relative_file_url(self):
1077
self.assertEquals(urlutils.local_path_to_url(".") + "/bar",
1078
location_to_url("file:bar"))
1080
def test_absolute_file_url(self):
1081
self.assertEquals("file:///bar", location_to_url("file:/bar"))