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'
201
219
def test_coalesce_fudge(self):
202
220
self.check([(10, 30, [(0, 10), (20, 10)]),
203
(100, 10, [(0, 10)]),
221
(100, 10, [(0, 10),]),
204
222
], [(10, 10), (30, 10), (100, 10)],
207
225
def test_coalesce_max_size(self):
208
226
self.check([(10, 20, [(0, 10), (10, 10)]),
209
227
(30, 50, [(0, 50)]),
210
228
# If one range is above max_size, it gets its own coalesced
212
(100, 80, [(0, 80)]),],
230
(100, 80, [(0, 80),]),],
213
231
[(10, 10), (20, 10), (30, 50), (100, 80)],
216
235
def test_coalesce_no_max_size(self):
217
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)]),],
218
237
[(10, 10), (20, 10), (30, 50), (80, 100)],
221
240
def test_coalesce_default_limit(self):
222
241
# By default we use a 100MB max size.
223
ten_mb = 10 * 1024 * 1024
224
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)]),
225
244
(10*ten_mb, ten_mb, [(0, ten_mb)])],
226
245
[(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)],
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)],
229
248
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"))
714
698
class TestLocalTransports(tests.TestCase):
716
700
def test_get_transport_from_abspath(self):
738
722
self.assertEquals(t.local_abspath(''), here)
741
class TestLocalTransportWriteStream(tests.TestCaseWithTransport):
743
def test_local_fdatasync_calls_fdatasync(self):
744
"""Check fdatasync on a stream tries to flush the data to the OS.
746
We can't easily observe the external effect but we can at least see
750
fdatasync = getattr(os, 'fdatasync', sentinel)
751
if fdatasync is sentinel:
752
raise tests.TestNotApplicable('fdatasync not supported')
753
t = self.get_transport('.')
754
calls = self.recordCalls(os, 'fdatasync')
755
w = t.open_write_stream('out')
758
with open('out', 'rb') as f:
759
# Should have been flushed.
760
self.assertEquals(f.read(), 'foo')
761
self.assertEquals(len(calls), 1, calls)
763
def test_missing_directory(self):
764
t = self.get_transport('.')
765
self.assertRaises(errors.NoSuchFile, t.open_write_stream, 'dir/foo')
768
725
class TestWin32LocalTransport(tests.TestCase):
770
727
def test_unc_clone_to_root(self):
786
743
def test_parse_url(self):
787
744
t = transport.ConnectedTransport(
788
745
'http://simple.example.com/home/source')
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)
746
self.assertEquals(t._host, 'simple.example.com')
747
self.assertEquals(t._port, None)
748
self.assertEquals(t._path, '/home/source/')
749
self.failUnless(t._user is None)
750
self.failUnless(t._password is None)
795
752
self.assertEquals(t.base, 'http://simple.example.com/home/source/')
797
754
def test_parse_url_with_at_in_user(self):
799
756
t = transport.ConnectedTransport('ftp://user@host.com@www.host.com/')
800
self.assertEquals(t._parsed_url.user, 'user@host.com')
757
self.assertEquals(t._user, 'user@host.com')
802
759
def test_parse_quoted_url(self):
803
760
t = transport.ConnectedTransport(
804
761
'http://ro%62ey:h%40t@ex%41mple.com:2222/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/')
762
self.assertEquals(t._host, 'exAmple.com')
763
self.assertEquals(t._port, 2222)
764
self.assertEquals(t._user, 'robey')
765
self.assertEquals(t._password, 'h@t')
766
self.assertEquals(t._path, '/path/')
811
768
# Base should not keep track of the password
812
self.assertEquals(t.base, 'http://ro%62ey@ex%41mple.com:2222/path/')
769
self.assertEquals(t.base, 'http://robey@exAmple.com:2222/path/')
814
771
def test_parse_invalid_url(self):
815
772
self.assertRaises(errors.InvalidURL,
871
827
def test_reuse_same_transport(self):
872
828
possible_transports = []
873
t1 = transport.get_transport_from_url('http://foo/',
829
t1 = transport.get_transport('http://foo/',
874
830
possible_transports=possible_transports)
875
831
self.assertEqual([t1], possible_transports)
876
t2 = transport.get_transport_from_url('http://foo/',
832
t2 = transport.get_transport('http://foo/',
877
833
possible_transports=[t1])
878
834
self.assertIs(t1, t2)
880
836
# Also check that final '/' are handled correctly
881
t3 = transport.get_transport_from_url('http://foo/path/')
882
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',
883
839
possible_transports=[t3])
884
840
self.assertIs(t3, t4)
886
t5 = transport.get_transport_from_url('http://foo/path')
887
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/',
888
844
possible_transports=[t5])
889
845
self.assertIs(t5, t6)
891
847
def test_don_t_reuse_different_transport(self):
892
t1 = transport.get_transport_from_url('http://foo/path')
893
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',
894
850
possible_transports=[t1])
895
851
self.assertIsNot(t1, t2)
898
854
class TestTransportTrace(tests.TestCase):
900
def test_decorator(self):
901
t = transport.get_transport_from_url('trace+memory://')
902
self.assertIsInstance(
903
t, bzrlib.transport.trace.TransportTraceDecorator)
857
t = transport.get_transport('trace+memory://')
858
self.assertIsInstance(t, bzrlib.transport.trace.TransportTraceDecorator)
905
860
def test_clone_preserves_activity(self):
906
t = transport.get_transport_from_url('trace+memory://')
861
t = transport.get_transport('trace+memory://')
907
862
t2 = t.clone('.')
908
863
self.assertTrue(t is not t2)
909
864
self.assertTrue(t._activity is t2._activity)
1050
1004
result = http.unhtml_roughly(fake_html)
1051
1005
self.assertEquals(len(result), 1000)
1052
1006
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"))