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
201
def test_coalesce_fudge(self):
221
202
self.check([(10, 30, [(0, 10), (20, 10)]),
222
(100, 10, [(0, 10),]),
203
(100, 10, [(0, 10)]),
223
204
], [(10, 10), (30, 10), (100, 10)],
226
207
def test_coalesce_max_size(self):
227
208
self.check([(10, 20, [(0, 10), (10, 10)]),
228
209
(30, 50, [(0, 50)]),
229
210
# If one range is above max_size, it gets its own coalesced
231
(100, 80, [(0, 80),]),],
212
(100, 80, [(0, 80)]),],
232
213
[(10, 10), (20, 10), (30, 50), (100, 80)],
236
216
def test_coalesce_no_max_size(self):
237
self.check([(10, 170, [(0, 10), (10, 10), (20, 50), (70, 100)]),],
217
self.check([(10, 170, [(0, 10), (10, 10), (20, 50), (70, 100)])],
238
218
[(10, 10), (20, 10), (30, 50), (80, 100)],
241
221
def test_coalesce_default_limit(self):
242
222
# 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)]),
223
ten_mb = 10 * 1024 * 1024
224
self.check([(0, 10 * ten_mb, [(i * ten_mb, ten_mb) for i in range(10)]),
245
225
(10*ten_mb, ten_mb, [(0, ten_mb)])],
246
226
[(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)],
227
self.check([(0, 11 * ten_mb, [(i * ten_mb, ten_mb) for i in range(11)])],
228
[(i * ten_mb, ten_mb) for i in range(11)],
249
229
max_size=1*1024*1024*1024)
677
class TestTransportFromPath(tests.TestCaseInTempDir):
679
def test_with_path(self):
680
t = transport.get_transport_from_path(self.test_dir)
681
self.assertIsInstance(t, local.LocalTransport)
682
self.assertEquals(t.base.rstrip("/"),
683
urlutils.local_path_to_url(self.test_dir))
685
def test_with_url(self):
686
t = transport.get_transport_from_path("file:")
687
self.assertIsInstance(t, local.LocalTransport)
688
self.assertEquals(t.base.rstrip("/"),
689
urlutils.local_path_to_url(os.path.join(self.test_dir, "file:")))
692
class TestTransportFromUrl(tests.TestCaseInTempDir):
694
def test_with_path(self):
695
self.assertRaises(errors.InvalidURL, transport.get_transport_from_url,
698
def test_with_url(self):
699
url = urlutils.local_path_to_url(self.test_dir)
700
t = transport.get_transport_from_url(url)
701
self.assertIsInstance(t, local.LocalTransport)
702
self.assertEquals(t.base.rstrip("/"), url)
704
def test_with_url_and_segment_parameters(self):
705
url = urlutils.local_path_to_url(self.test_dir)+",branch=foo"
706
t = transport.get_transport_from_url(url)
707
self.assertIsInstance(t, local.LocalTransport)
708
self.assertEquals(t.base.rstrip("/"), url)
709
with open(os.path.join(self.test_dir, "afile"), 'w') as f:
711
self.assertTrue(t.has("afile"))
699
714
class TestLocalTransports(tests.TestCase):
701
716
def test_get_transport_from_abspath(self):
763
786
def test_parse_url(self):
764
787
t = transport.ConnectedTransport(
765
788
'http://simple.example.com/home/source')
766
self.assertEquals(t._host, 'simple.example.com')
767
self.assertEquals(t._port, None)
768
self.assertEquals(t._path, '/home/source/')
769
self.assertTrue(t._user is None)
770
self.assertTrue(t._password is None)
789
self.assertEquals(t._parsed_url.host, 'simple.example.com')
790
self.assertEquals(t._parsed_url.port, None)
791
self.assertEquals(t._parsed_url.path, '/home/source/')
792
self.assertTrue(t._parsed_url.user is None)
793
self.assertTrue(t._parsed_url.password is None)
772
795
self.assertEquals(t.base, 'http://simple.example.com/home/source/')
774
797
def test_parse_url_with_at_in_user(self):
776
799
t = transport.ConnectedTransport('ftp://user@host.com@www.host.com/')
777
self.assertEquals(t._user, 'user@host.com')
800
self.assertEquals(t._parsed_url.user, 'user@host.com')
779
802
def test_parse_quoted_url(self):
780
803
t = transport.ConnectedTransport(
781
804
'http://ro%62ey:h%40t@ex%41mple.com:2222/path')
782
self.assertEquals(t._host, 'exAmple.com')
783
self.assertEquals(t._port, 2222)
784
self.assertEquals(t._user, 'robey')
785
self.assertEquals(t._password, 'h@t')
786
self.assertEquals(t._path, '/path/')
805
self.assertEquals(t._parsed_url.host, 'exAmple.com')
806
self.assertEquals(t._parsed_url.port, 2222)
807
self.assertEquals(t._parsed_url.user, 'robey')
808
self.assertEquals(t._parsed_url.password, 'h@t')
809
self.assertEquals(t._parsed_url.path, '/path/')
788
811
# Base should not keep track of the password
789
self.assertEquals(t.base, 'http://robey@exAmple.com:2222/path/')
812
self.assertEquals(t.base, 'http://ro%62ey@ex%41mple.com:2222/path/')
791
814
def test_parse_invalid_url(self):
792
815
self.assertRaises(errors.InvalidURL,
847
871
def test_reuse_same_transport(self):
848
872
possible_transports = []
849
t1 = transport.get_transport('http://foo/',
873
t1 = transport.get_transport_from_url('http://foo/',
850
874
possible_transports=possible_transports)
851
875
self.assertEqual([t1], possible_transports)
852
t2 = transport.get_transport('http://foo/',
876
t2 = transport.get_transport_from_url('http://foo/',
853
877
possible_transports=[t1])
854
878
self.assertIs(t1, t2)
856
880
# Also check that final '/' are handled correctly
857
t3 = transport.get_transport('http://foo/path/')
858
t4 = transport.get_transport('http://foo/path',
881
t3 = transport.get_transport_from_url('http://foo/path/')
882
t4 = transport.get_transport_from_url('http://foo/path',
859
883
possible_transports=[t3])
860
884
self.assertIs(t3, t4)
862
t5 = transport.get_transport('http://foo/path')
863
t6 = transport.get_transport('http://foo/path/',
886
t5 = transport.get_transport_from_url('http://foo/path')
887
t6 = transport.get_transport_from_url('http://foo/path/',
864
888
possible_transports=[t5])
865
889
self.assertIs(t5, t6)
867
891
def test_don_t_reuse_different_transport(self):
868
t1 = transport.get_transport('http://foo/path')
869
t2 = transport.get_transport('http://bar/path',
892
t1 = transport.get_transport_from_url('http://foo/path')
893
t2 = transport.get_transport_from_url('http://bar/path',
870
894
possible_transports=[t1])
871
895
self.assertIsNot(t1, t2)
874
898
class TestTransportTrace(tests.TestCase):
877
t = transport.get_transport('trace+memory://')
878
self.assertIsInstance(t, bzrlib.transport.trace.TransportTraceDecorator)
900
def test_decorator(self):
901
t = transport.get_transport_from_url('trace+memory://')
902
self.assertIsInstance(
903
t, bzrlib.transport.trace.TransportTraceDecorator)
880
905
def test_clone_preserves_activity(self):
881
t = transport.get_transport('trace+memory://')
906
t = transport.get_transport_from_url('trace+memory://')
882
907
t2 = t.clone('.')
883
908
self.assertTrue(t is not t2)
884
909
self.assertTrue(t._activity is t2._activity)
1024
1050
result = http.unhtml_roughly(fake_html)
1025
1051
self.assertEquals(len(result), 1000)
1026
1052
self.assertStartsWith(result, " something!")
1055
class SomeDirectory(object):
1057
def look_up(self, name, url):
1061
class TestLocationToUrl(tests.TestCase):
1063
def get_base_location(self):
1064
path = osutils.abspath('/foo/bar')
1065
if path.startswith('/'):
1066
url = 'file://%s' % (path,)
1068
# On Windows, abspaths start with the drive letter, so we have to
1069
# add in the extra '/'
1070
url = 'file:///%s' % (path,)
1073
def test_regular_url(self):
1074
self.assertEquals("file://foo", location_to_url("file://foo"))
1076
def test_directory(self):
1077
directories.register("bar:", SomeDirectory, "Dummy directory")
1078
self.addCleanup(directories.remove, "bar:")
1079
self.assertEquals("http://bar", location_to_url("bar:"))
1081
def test_unicode_url(self):
1082
self.assertRaises(errors.InvalidURL, location_to_url,
1083
"http://fo/\xc3\xaf".decode("utf-8"))
1085
def test_unicode_path(self):
1086
path, url = self.get_base_location()
1087
location = path + "\xc3\xaf".decode("utf-8")
1089
self.assertEquals(url, location_to_url(location))
1091
def test_path(self):
1092
path, url = self.get_base_location()
1093
self.assertEquals(url, location_to_url(path))
1095
def test_relative_file_url(self):
1096
self.assertEquals(urlutils.local_path_to_url(".") + "/bar",
1097
location_to_url("file:bar"))
1099
def test_absolute_file_url(self):
1100
self.assertEquals("file:///bar", location_to_url("file:/bar"))