49
53
class TestTransport(tests.TestCase):
50
54
"""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
56
def test__get_set_protocol_handlers(self):
56
57
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)
58
self.assertNotEqual([], handlers.keys())
59
transport._clear_protocol_handlers()
60
self.addCleanup(transport._set_protocol_handlers, handlers)
61
self.assertEqual([], transport._get_protocol_handlers().keys())
64
63
def test_get_transport_modules(self):
65
64
handlers = transport._get_protocol_handlers()
65
self.addCleanup(transport._set_protocol_handlers, handlers)
66
66
# don't pollute the current handlers
67
67
transport._clear_protocol_handlers()
68
69
class SampleHandler(object):
69
70
"""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)
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())
87
85
def test_transport_dependency(self):
88
86
"""Transport with missing dependency causes no error"""
89
87
saved_handlers = transport._get_protocol_handlers()
88
self.addCleanup(transport._set_protocol_handlers, saved_handlers)
90
89
# don't pollute the current handlers
91
90
transport._clear_protocol_handlers()
91
transport.register_transport_proto('foo')
92
transport.register_lazy_transport(
93
'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)
95
transport.get_transport_from_url('foo://fooserver/foo')
96
except errors.UnsupportedProtocol, e:
98
self.assertEquals('Unsupported protocol'
99
' for url "foo://fooserver/foo":'
100
' Unable to import library "some_lib":'
101
' testing missing dependency', str(e))
103
self.fail('Did not raise UnsupportedProtocol')
110
105
def test_transport_fallback(self):
111
106
"""Transport with missing dependency causes no error"""
112
107
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)
108
self.addCleanup(transport._set_protocol_handlers, saved_handlers)
109
transport._clear_protocol_handlers()
110
transport.register_transport_proto('foo')
111
transport.register_lazy_transport(
112
'foo', 'bzrlib.tests.test_transport', 'BackupTransportHandler')
113
transport.register_lazy_transport(
114
'foo', 'bzrlib.tests.test_transport', 'BadTransportHandler')
115
t = transport.get_transport_from_url('foo://fooserver/foo')
116
self.assertTrue(isinstance(t, BackupTransportHandler))
125
118
def test_ssh_hints(self):
126
119
"""Transport ssh:// should raise an error pointing out bzr+ssh://"""
128
transport.get_transport('ssh://fooserver/foo')
121
transport.get_transport_from_url('ssh://fooserver/foo')
129
122
except errors.UnsupportedProtocol, e:
131
124
self.assertEquals('Unsupported protocol'
220
202
def test_coalesce_fudge(self):
221
203
self.check([(10, 30, [(0, 10), (20, 10)]),
222
(100, 10, [(0, 10),]),
204
(100, 10, [(0, 10)]),
223
205
], [(10, 10), (30, 10), (100, 10)],
226
208
def test_coalesce_max_size(self):
227
209
self.check([(10, 20, [(0, 10), (10, 10)]),
228
210
(30, 50, [(0, 50)]),
229
211
# If one range is above max_size, it gets its own coalesced
231
(100, 80, [(0, 80),]),],
213
(100, 80, [(0, 80)]),],
232
214
[(10, 10), (20, 10), (30, 50), (100, 80)],
236
217
def test_coalesce_no_max_size(self):
237
self.check([(10, 170, [(0, 10), (10, 10), (20, 50), (70, 100)]),],
218
self.check([(10, 170, [(0, 10), (10, 10), (20, 50), (70, 100)])],
238
219
[(10, 10), (20, 10), (30, 50), (80, 100)],
241
222
def test_coalesce_default_limit(self):
242
223
# 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)]),
224
ten_mb = 10 * 1024 * 1024
225
self.check([(0, 10 * ten_mb, [(i * ten_mb, ten_mb) for i in range(10)]),
245
226
(10*ten_mb, ten_mb, [(0, ten_mb)])],
246
227
[(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)],
228
self.check([(0, 11 * ten_mb, [(i * ten_mb, ten_mb) for i in range(11)])],
229
[(i * ten_mb, ten_mb) for i in range(11)],
249
230
max_size=1*1024*1024*1024)
678
class TestTransportFromPath(tests.TestCaseInTempDir):
680
def test_with_path(self):
681
t = transport.get_transport_from_path(self.test_dir)
682
self.assertIsInstance(t, local.LocalTransport)
683
self.assertEquals(t.base.rstrip("/"),
684
urlutils.local_path_to_url(self.test_dir))
686
def test_with_url(self):
687
t = transport.get_transport_from_path("file:")
688
self.assertIsInstance(t, local.LocalTransport)
689
self.assertEquals(t.base.rstrip("/"),
690
urlutils.local_path_to_url(os.path.join(self.test_dir, "file:")))
693
class TestTransportFromUrl(tests.TestCaseInTempDir):
695
def test_with_path(self):
696
self.assertRaises(errors.InvalidURL, transport.get_transport_from_url,
699
def test_with_url(self):
700
url = urlutils.local_path_to_url(self.test_dir)
701
t = transport.get_transport_from_url(url)
702
self.assertIsInstance(t, local.LocalTransport)
703
self.assertEquals(t.base.rstrip("/"), url)
705
def test_with_url_and_segment_parameters(self):
706
url = urlutils.local_path_to_url(self.test_dir)+",branch=foo"
707
t = transport.get_transport_from_url(url)
708
self.assertIsInstance(t, local.LocalTransport)
709
self.assertEquals(t.base.rstrip("/"), url)
710
with open(os.path.join(self.test_dir, "afile"), 'w') as f:
712
self.assertTrue(t.has("afile"))
699
715
class TestLocalTransports(tests.TestCase):
701
717
def test_get_transport_from_abspath(self):
723
739
self.assertEquals(t.local_abspath(''), here)
742
class TestLocalTransportMutation(tests.TestCaseInTempDir):
744
def test_local_transport_mkdir(self):
745
here = osutils.abspath('.')
746
t = transport.get_transport(here)
748
self.assertTrue(os.path.exists('test'))
750
def test_local_transport_mkdir_permission_denied(self):
751
# See https://bugs.launchpad.net/bzr/+bug/606537
752
here = osutils.abspath('.')
753
t = transport.get_transport(here)
754
def fake_chmod(path, mode):
755
e = OSError('permission denied')
756
e.errno = errno.EPERM
758
self.overrideAttr(os, 'chmod', fake_chmod)
760
t.mkdir('test2', mode=0707)
761
self.assertTrue(os.path.exists('test'))
762
self.assertTrue(os.path.exists('test2'))
765
class TestLocalTransportWriteStream(tests.TestCaseWithTransport):
767
def test_local_fdatasync_calls_fdatasync(self):
768
"""Check fdatasync on a stream tries to flush the data to the OS.
770
We can't easily observe the external effect but we can at least see
774
fdatasync = getattr(os, 'fdatasync', sentinel)
775
if fdatasync is sentinel:
776
raise tests.TestNotApplicable('fdatasync not supported')
777
t = self.get_transport('.')
778
calls = self.recordCalls(os, 'fdatasync')
779
w = t.open_write_stream('out')
782
with open('out', 'rb') as f:
783
# Should have been flushed.
784
self.assertEquals(f.read(), 'foo')
785
self.assertEquals(len(calls), 1, calls)
787
def test_missing_directory(self):
788
t = self.get_transport('.')
789
self.assertRaises(errors.NoSuchFile, t.open_write_stream, 'dir/foo')
726
792
class TestWin32LocalTransport(tests.TestCase):
728
794
def test_unc_clone_to_root(self):
744
810
def test_parse_url(self):
745
811
t = transport.ConnectedTransport(
746
812
'http://simple.example.com/home/source')
747
self.assertEquals(t._host, 'simple.example.com')
748
self.assertEquals(t._port, None)
749
self.assertEquals(t._path, '/home/source/')
750
self.failUnless(t._user is None)
751
self.failUnless(t._password is None)
813
self.assertEquals(t._parsed_url.host, 'simple.example.com')
814
self.assertEquals(t._parsed_url.port, None)
815
self.assertEquals(t._parsed_url.path, '/home/source/')
816
self.assertTrue(t._parsed_url.user is None)
817
self.assertTrue(t._parsed_url.password is None)
753
819
self.assertEquals(t.base, 'http://simple.example.com/home/source/')
755
821
def test_parse_url_with_at_in_user(self):
757
823
t = transport.ConnectedTransport('ftp://user@host.com@www.host.com/')
758
self.assertEquals(t._user, 'user@host.com')
824
self.assertEquals(t._parsed_url.user, 'user@host.com')
760
826
def test_parse_quoted_url(self):
761
827
t = transport.ConnectedTransport(
762
828
'http://ro%62ey:h%40t@ex%41mple.com:2222/path')
763
self.assertEquals(t._host, 'exAmple.com')
764
self.assertEquals(t._port, 2222)
765
self.assertEquals(t._user, 'robey')
766
self.assertEquals(t._password, 'h@t')
767
self.assertEquals(t._path, '/path/')
829
self.assertEquals(t._parsed_url.host, 'exAmple.com')
830
self.assertEquals(t._parsed_url.port, 2222)
831
self.assertEquals(t._parsed_url.user, 'robey')
832
self.assertEquals(t._parsed_url.password, 'h@t')
833
self.assertEquals(t._parsed_url.path, '/path/')
769
835
# Base should not keep track of the password
770
self.assertEquals(t.base, 'http://robey@exAmple.com:2222/path/')
836
self.assertEquals(t.base, 'http://ro%62ey@ex%41mple.com:2222/path/')
772
838
def test_parse_invalid_url(self):
773
839
self.assertRaises(errors.InvalidURL,
828
895
def test_reuse_same_transport(self):
829
896
possible_transports = []
830
t1 = transport.get_transport('http://foo/',
897
t1 = transport.get_transport_from_url('http://foo/',
831
898
possible_transports=possible_transports)
832
899
self.assertEqual([t1], possible_transports)
833
t2 = transport.get_transport('http://foo/',
900
t2 = transport.get_transport_from_url('http://foo/',
834
901
possible_transports=[t1])
835
902
self.assertIs(t1, t2)
837
904
# Also check that final '/' are handled correctly
838
t3 = transport.get_transport('http://foo/path/')
839
t4 = transport.get_transport('http://foo/path',
905
t3 = transport.get_transport_from_url('http://foo/path/')
906
t4 = transport.get_transport_from_url('http://foo/path',
840
907
possible_transports=[t3])
841
908
self.assertIs(t3, t4)
843
t5 = transport.get_transport('http://foo/path')
844
t6 = transport.get_transport('http://foo/path/',
910
t5 = transport.get_transport_from_url('http://foo/path')
911
t6 = transport.get_transport_from_url('http://foo/path/',
845
912
possible_transports=[t5])
846
913
self.assertIs(t5, t6)
848
915
def test_don_t_reuse_different_transport(self):
849
t1 = transport.get_transport('http://foo/path')
850
t2 = transport.get_transport('http://bar/path',
916
t1 = transport.get_transport_from_url('http://foo/path')
917
t2 = transport.get_transport_from_url('http://bar/path',
851
918
possible_transports=[t1])
852
919
self.assertIsNot(t1, t2)
855
922
class TestTransportTrace(tests.TestCase):
858
t = transport.get_transport('trace+memory://')
859
self.assertIsInstance(t, bzrlib.transport.trace.TransportTraceDecorator)
924
def test_decorator(self):
925
t = transport.get_transport_from_url('trace+memory://')
926
self.assertIsInstance(
927
t, bzrlib.transport.trace.TransportTraceDecorator)
861
929
def test_clone_preserves_activity(self):
862
t = transport.get_transport('trace+memory://')
930
t = transport.get_transport_from_url('trace+memory://')
863
931
t2 = t.clone('.')
864
932
self.assertTrue(t is not t2)
865
933
self.assertTrue(t._activity is t2._activity)
1005
1074
result = http.unhtml_roughly(fake_html)
1006
1075
self.assertEquals(len(result), 1000)
1007
1076
self.assertStartsWith(result, " something!")
1079
class SomeDirectory(object):
1081
def look_up(self, name, url):
1085
class TestLocationToUrl(tests.TestCase):
1087
def get_base_location(self):
1088
path = osutils.abspath('/foo/bar')
1089
if path.startswith('/'):
1090
url = 'file://%s' % (path,)
1092
# On Windows, abspaths start with the drive letter, so we have to
1093
# add in the extra '/'
1094
url = 'file:///%s' % (path,)
1097
def test_regular_url(self):
1098
self.assertEquals("file://foo", location_to_url("file://foo"))
1100
def test_directory(self):
1101
directories.register("bar:", SomeDirectory, "Dummy directory")
1102
self.addCleanup(directories.remove, "bar:")
1103
self.assertEquals("http://bar", location_to_url("bar:"))
1105
def test_unicode_url(self):
1106
self.assertRaises(errors.InvalidURL, location_to_url,
1107
"http://fo/\xc3\xaf".decode("utf-8"))
1109
def test_unicode_path(self):
1110
path, url = self.get_base_location()
1111
location = path + "\xc3\xaf".decode("utf-8")
1113
self.assertEquals(url, location_to_url(location))
1115
def test_path(self):
1116
path, url = self.get_base_location()
1117
self.assertEquals(url, location_to_url(path))
1119
def test_relative_file_url(self):
1120
self.assertEquals(urlutils.local_path_to_url(".") + "/bar",
1121
location_to_url("file:bar"))
1123
def test_absolute_file_url(self):
1124
self.assertEquals("file:///bar", location_to_url("file:/bar"))