52
48
class TestTransport(tests.TestCase):
53
49
"""Test the non transport-concrete class functionality."""
51
# FIXME: These tests should use addCleanup() and/or overrideAttr() instead
52
# of try/finally -- vila 20100205
55
54
def test__get_set_protocol_handlers(self):
56
55
handlers = transport._get_protocol_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())
56
self.assertNotEqual([], handlers.keys( ))
58
transport._clear_protocol_handlers()
59
self.assertEqual([], transport._get_protocol_handlers().keys())
61
transport._set_protocol_handlers(handlers)
62
63
def test_get_transport_modules(self):
63
64
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()
68
67
class SampleHandler(object):
69
68
"""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())
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)
84
86
def test_transport_dependency(self):
85
87
"""Transport with missing dependency causes no error"""
86
88
saved_handlers = transport._get_protocol_handlers()
87
self.addCleanup(transport._set_protocol_handlers, saved_handlers)
88
89
# don't pollute the current handlers
89
90
transport._clear_protocol_handlers()
90
transport.register_transport_proto('foo')
91
transport.register_lazy_transport(
92
'foo', 'bzrlib.tests.test_transport', 'BadTransportHandler')
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')
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)
104
109
def test_transport_fallback(self):
105
110
"""Transport with missing dependency causes no error"""
106
111
saved_handlers = transport._get_protocol_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))
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)
117
124
def test_ssh_hints(self):
118
125
"""Transport ssh:// should raise an error pointing out bzr+ssh://"""
120
transport.get_transport_from_url('ssh://fooserver/foo')
127
transport.get_transport('ssh://fooserver/foo')
121
128
except errors.UnsupportedProtocol, e:
123
130
self.assertEquals('Unsupported protocol'
212
219
def test_coalesce_fudge(self):
213
220
self.check([(10, 30, [(0, 10), (20, 10)]),
214
(100, 10, [(0, 10)]),
221
(100, 10, [(0, 10),]),
215
222
], [(10, 10), (30, 10), (100, 10)],
218
225
def test_coalesce_max_size(self):
219
226
self.check([(10, 20, [(0, 10), (10, 10)]),
220
227
(30, 50, [(0, 50)]),
221
228
# If one range is above max_size, it gets its own coalesced
223
(100, 80, [(0, 80)]),],
230
(100, 80, [(0, 80),]),],
224
231
[(10, 10), (20, 10), (30, 50), (100, 80)],
227
235
def test_coalesce_no_max_size(self):
228
self.check([(10, 170, [(0, 10), (10, 10), (20, 50), (70, 100)])],
236
self.check([(10, 170, [(0, 10), (10, 10), (20, 50), (70, 100)]),],
229
237
[(10, 10), (20, 10), (30, 50), (80, 100)],
232
240
def test_coalesce_default_limit(self):
233
241
# By default we use a 100MB max size.
234
ten_mb = 10 * 1024 * 1024
235
self.check([(0, 10 * ten_mb, [(i * ten_mb, ten_mb) for i in range(10)]),
242
ten_mb = 10*1024*1024
243
self.check([(0, 10*ten_mb, [(i*ten_mb, ten_mb) for i in range(10)]),
236
244
(10*ten_mb, ten_mb, [(0, ten_mb)])],
237
245
[(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)],
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)],
240
248
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)
716
698
class TestLocalTransports(tests.TestCase):
718
700
def test_get_transport_from_abspath(self):
869
827
def test_reuse_same_transport(self):
870
828
possible_transports = []
871
t1 = transport.get_transport_from_url('http://foo/',
829
t1 = transport.get_transport('http://foo/',
872
830
possible_transports=possible_transports)
873
831
self.assertEqual([t1], possible_transports)
874
t2 = transport.get_transport_from_url('http://foo/',
832
t2 = transport.get_transport('http://foo/',
875
833
possible_transports=[t1])
876
834
self.assertIs(t1, t2)
878
836
# Also check that final '/' are handled correctly
879
t3 = transport.get_transport_from_url('http://foo/path/')
880
t4 = transport.get_transport_from_url('http://foo/path',
837
t3 = transport.get_transport('http://foo/path/')
838
t4 = transport.get_transport('http://foo/path',
881
839
possible_transports=[t3])
882
840
self.assertIs(t3, t4)
884
t5 = transport.get_transport_from_url('http://foo/path')
885
t6 = transport.get_transport_from_url('http://foo/path/',
842
t5 = transport.get_transport('http://foo/path')
843
t6 = transport.get_transport('http://foo/path/',
886
844
possible_transports=[t5])
887
845
self.assertIs(t5, t6)
889
847
def test_don_t_reuse_different_transport(self):
890
t1 = transport.get_transport_from_url('http://foo/path')
891
t2 = transport.get_transport_from_url('http://bar/path',
848
t1 = transport.get_transport('http://foo/path')
849
t2 = transport.get_transport('http://bar/path',
892
850
possible_transports=[t1])
893
851
self.assertIsNot(t1, t2)
896
854
class TestTransportTrace(tests.TestCase):
898
def test_decorator(self):
899
t = transport.get_transport_from_url('trace+memory://')
900
self.assertIsInstance(
901
t, bzrlib.transport.trace.TransportTraceDecorator)
857
t = transport.get_transport('trace+memory://')
858
self.assertIsInstance(t, bzrlib.transport.trace.TransportTraceDecorator)
903
860
def test_clone_preserves_activity(self):
904
t = transport.get_transport_from_url('trace+memory://')
861
t = transport.get_transport('trace+memory://')
905
862
t2 = t.clone('.')
906
863
self.assertTrue(t is not t2)
907
864
self.assertTrue(t._activity is t2._activity)
1048
1004
result = http.unhtml_roughly(fake_html)
1049
1005
self.assertEquals(len(result), 1000)
1050
1006
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"))