219
230
self.assertRaises(NoSuchFile, t.get_bytes, 'c')
222
t = self.get_transport()
227
self.applyDeprecated(zero_eleven, t.put, 'a', 'string\ncontents\n')
228
self.check_transport_contents('string\ncontents\n', t, 'a')
230
self.applyDeprecated(zero_eleven,
231
t.put, 'b', StringIO('file-like\ncontents\n'))
232
self.check_transport_contents('file-like\ncontents\n', t, 'b')
234
self.assertRaises(NoSuchFile,
235
self.applyDeprecated,
237
t.put, 'path/doesnt/exist/c', StringIO('contents'))
232
def test_get_with_open_write_stream_sees_all_content(self):
233
t = self.get_transport()
236
handle = t.open_write_stream('foo')
239
self.assertEqual('b', t.get('foo').read())
243
def test_get_bytes_with_open_write_stream_sees_all_content(self):
244
t = self.get_transport()
247
handle = t.open_write_stream('foo')
250
self.assertEqual('b', t.get_bytes('foo'))
251
self.assertEqual('b', t.get('foo').read())
239
255
def test_put_bytes(self):
240
256
t = self.get_transport()
424
440
# Yes, you can put a file such that it becomes readonly
425
441
t.put_file('mode400', StringIO('test text\n'), mode=0400)
426
442
self.assertTransportMode(t, 'mode400', 0400)
428
# XXX: put_multi is deprecated, so do we really care anymore?
429
self.applyDeprecated(zero_eleven, t.put_multi,
430
[('mmode644', StringIO('text\n'))], mode=0644)
431
self.assertTransportMode(t, 'mmode644', 0644)
433
443
# The default permissions should be based on the current umask
434
444
umask = osutils.get_umask()
435
445
t.put_file('nomode', StringIO('test text\n'), mode=None)
499
509
unicode_file = pyStringIO(u'\u1234')
500
510
self.assertRaises(UnicodeEncodeError, t.put_file, 'foo', unicode_file)
502
def test_put_multi(self):
503
t = self.get_transport()
507
self.assertEqual(2, self.applyDeprecated(zero_eleven,
508
t.put_multi, [('a', StringIO('new\ncontents for\na\n')),
509
('d', StringIO('contents\nfor d\n'))]
511
self.assertEqual(list(t.has_multi(['a', 'b', 'c', 'd'])),
512
[True, False, False, True])
513
self.check_transport_contents('new\ncontents for\na\n', t, 'a')
514
self.check_transport_contents('contents\nfor d\n', t, 'd')
516
self.assertEqual(2, self.applyDeprecated(zero_eleven,
517
t.put_multi, iter([('a', StringIO('diff\ncontents for\na\n')),
518
('d', StringIO('another contents\nfor d\n'))])
520
self.check_transport_contents('diff\ncontents for\na\n', t, 'a')
521
self.check_transport_contents('another contents\nfor d\n', t, 'd')
523
def test_put_permissions(self):
524
t = self.get_transport()
528
if not t._can_roundtrip_unix_modebits():
529
# Can't roundtrip, so no need to run this test
531
self.applyDeprecated(zero_eleven, t.put, 'mode644',
532
StringIO('test text\n'), mode=0644)
533
self.assertTransportMode(t, 'mode644', 0644)
534
self.applyDeprecated(zero_eleven, t.put, 'mode666',
535
StringIO('test text\n'), mode=0666)
536
self.assertTransportMode(t, 'mode666', 0666)
537
self.applyDeprecated(zero_eleven, t.put, 'mode600',
538
StringIO('test text\n'), mode=0600)
539
self.assertTransportMode(t, 'mode600', 0600)
540
# Yes, you can put a file such that it becomes readonly
541
self.applyDeprecated(zero_eleven, t.put, 'mode400',
542
StringIO('test text\n'), mode=0400)
543
self.assertTransportMode(t, 'mode400', 0400)
544
self.applyDeprecated(zero_eleven, t.put_multi,
545
[('mmode644', StringIO('text\n'))], mode=0644)
546
self.assertTransportMode(t, 'mmode644', 0644)
548
# The default permissions should be based on the current umask
549
umask = osutils.get_umask()
550
self.applyDeprecated(zero_eleven, t.put, 'nomode',
551
StringIO('test text\n'), mode=None)
552
self.assertTransportMode(t, 'nomode', 0666 & ~umask)
554
512
def test_mkdir(self):
555
513
t = self.get_transport()
621
579
t.mkdir('dnomode', mode=None)
622
580
self.assertTransportMode(t, 'dnomode', 0777 & ~umask)
582
def test_opening_a_file_stream_creates_file(self):
583
t = self.get_transport()
586
handle = t.open_write_stream('foo')
588
self.assertEqual('', t.get_bytes('foo'))
592
def test_opening_a_file_stream_can_set_mode(self):
593
t = self.get_transport()
596
if not t._can_roundtrip_unix_modebits():
597
# Can't roundtrip, so no need to run this test
599
def check_mode(name, mode, expected):
600
handle = t.open_write_stream(name, mode=mode)
602
self.assertTransportMode(t, name, expected)
603
check_mode('mode644', 0644, 0644)
604
check_mode('mode666', 0666, 0666)
605
check_mode('mode600', 0600, 0600)
606
# The default permissions should be based on the current umask
607
check_mode('nomode', None, 0666 & ~osutils.get_umask())
624
609
def test_copy_to(self):
625
610
# FIXME: test: same server to same server (partly done)
626
611
# same protocol two servers
627
612
# and different protocols (done for now except for MemoryTransport.
629
from bzrlib.transport.memory import MemoryTransport
631
615
def simple_copy_files(transport_from, transport_to):
632
616
files = ['a', 'b', 'c', 'd']
673
657
self.assertTransportMode(temp_transport, f, mode)
675
def test_append(self):
676
t = self.get_transport()
680
t.put_bytes('a', 'diff\ncontents for\na\n')
681
t.put_bytes('b', 'contents\nfor b\n')
683
self.assertEqual(20, self.applyDeprecated(zero_eleven,
684
t.append, 'a', StringIO('add\nsome\nmore\ncontents\n')))
686
self.check_transport_contents(
687
'diff\ncontents for\na\nadd\nsome\nmore\ncontents\n',
690
# And we can create new files, too
691
self.assertEqual(0, self.applyDeprecated(zero_eleven,
692
t.append, 'c', StringIO('some text\nfor a missing file\n')))
693
self.check_transport_contents('some text\nfor a missing file\n',
695
659
def test_append_file(self):
696
660
t = self.get_transport()
1022
991
except NotImplementedError:
1023
992
raise TestSkipped("Transport %s has no bogus URL support." %
1024
993
self._server.__class__)
1025
# This should be: but SSH still connects on construction. No COOKIE!
1026
# self.assertRaises((ConnectionError, NoSuchFile), t.get, '.bzr/branch')
1028
t = bzrlib.transport.get_transport(url)
1029
t.get('.bzr/branch')
1030
except (ConnectionError, NoSuchFile), e:
1032
except (Exception), e:
1033
self.fail('Wrong exception thrown (%s.%s): %s'
1034
% (e.__class__.__module__, e.__class__.__name__, e))
1036
self.fail('Did not get the expected ConnectionError or NoSuchFile.')
994
t = get_transport(url)
995
self.assertRaises((ConnectionError, NoSuchFile), t.get, '.bzr/branch')
1038
997
def test_stat(self):
1039
998
# TODO: Test stat, just try once, and if it throws, stop testing
1129
1088
self.assertEqual(['%25'], names)
1130
1089
self.assertIsInstance(names[0], str)
1091
def test_clone_preserve_info(self):
1092
t1 = self.get_transport()
1093
if not isinstance(t1, ConnectedTransport):
1094
raise TestSkipped("not a connected transport")
1096
t2 = t1.clone('subdir')
1097
self.assertEquals(t1._scheme, t2._scheme)
1098
self.assertEquals(t1._user, t2._user)
1099
self.assertEquals(t1._password, t2._password)
1100
self.assertEquals(t1._host, t2._host)
1101
self.assertEquals(t1._port, t2._port)
1103
def test__reuse_for(self):
1104
t = self.get_transport()
1105
if not isinstance(t, ConnectedTransport):
1106
raise TestSkipped("not a connected transport")
1108
def new_url(scheme=None, user=None, password=None,
1109
host=None, port=None, path=None):
1110
"""Build a new url from t.base chaging only parts of it.
1112
Only the parameters different from None will be changed.
1114
if scheme is None: scheme = t._scheme
1115
if user is None: user = t._user
1116
if password is None: password = t._password
1117
if user is None: user = t._user
1118
if host is None: host = t._host
1119
if port is None: port = t._port
1120
if path is None: path = t._path
1121
return t._unsplit_url(scheme, user, password, host, port, path)
1123
self.assertIsNot(t, t._reuse_for(new_url(scheme='foo')))
1128
self.assertIsNot(t, t._reuse_for(new_url(user=user)))
1129
# passwords are not taken into account because:
1130
# - it makes no sense to have two different valid passwords for the
1132
# - _password in ConnectedTransport is intended to collect what the
1133
# user specified from the command-line and there are cases where the
1134
# new url can contain no password (if the url was built from an
1135
# existing transport.base for example)
1136
# - password are considered part of the credentials provided at
1137
# connection creation time and as such may not be present in the url
1138
# (they may be typed by the user when prompted for example)
1139
self.assertIs(t, t._reuse_for(new_url(password='from space')))
1140
# We will not connect, we can use a invalid host
1141
self.assertIsNot(t, t._reuse_for(new_url(host=t._host + 'bar')))
1146
self.assertIsNot(t, t._reuse_for(new_url(port=port)))
1148
def test_connection_sharing(self):
1149
t = self.get_transport()
1150
if not isinstance(t, ConnectedTransport):
1151
raise TestSkipped("not a connected transport")
1153
c = t.clone('subdir')
1154
# Some transports will create the connection only when needed
1155
t.has('surely_not') # Force connection
1156
self.assertIs(t._get_connection(), c._get_connection())
1158
# Temporary failure, we need to create a new dummy connection
1159
new_connection = object()
1160
t._set_connection(new_connection)
1161
# Check that both transports use the same connection
1162
self.assertIs(new_connection, t._get_connection())
1163
self.assertIs(new_connection, c._get_connection())
1165
def test_reuse_connection_for_various_paths(self):
1166
t = self.get_transport()
1167
if not isinstance(t, ConnectedTransport):
1168
raise TestSkipped("not a connected transport")
1170
t.has('surely_not') # Force connection
1171
self.assertIsNot(None, t._get_connection())
1173
subdir = t._reuse_for(t.base + 'whatever/but/deep/down/the/path')
1174
self.assertIsNot(t, subdir)
1175
self.assertIs(t._get_connection(), subdir._get_connection())
1177
home = subdir._reuse_for(t.base + 'home')
1178
self.assertIs(t._get_connection(), home._get_connection())
1179
self.assertIs(subdir._get_connection(), home._get_connection())
1132
1181
def test_clone(self):
1133
1182
# TODO: Test that clone moves up and down the filesystem
1134
1183
t1 = self.get_transport()
1353
1402
self.check_transport_contents(contents, t, urlutils.escape(fname))
1355
1404
def test_connect_twice_is_same_content(self):
1356
# check that our server (whatever it is) is accessable reliably
1405
# check that our server (whatever it is) is accessible reliably
1357
1406
# via get_transport and multiple connections share content.
1358
1407
transport = self.get_transport()
1359
1408
if transport.is_readonly():
1361
1410
transport.put_bytes('foo', 'bar')
1362
transport2 = self.get_transport()
1363
self.check_transport_contents('bar', transport2, 'foo')
1411
transport3 = self.get_transport()
1412
self.check_transport_contents('bar', transport3, 'foo')
1364
1413
# its base should be usable.
1365
transport2 = bzrlib.transport.get_transport(transport.base)
1366
self.check_transport_contents('bar', transport2, 'foo')
1414
transport4 = get_transport(transport.base)
1415
self.check_transport_contents('bar', transport4, 'foo')
1368
1417
# now opening at a relative url should give use a sane result:
1369
1418
transport.mkdir('newdir')
1370
transport2 = bzrlib.transport.get_transport(transport.base + "newdir")
1371
transport2 = transport2.clone('..')
1372
self.check_transport_contents('bar', transport2, 'foo')
1419
transport5 = get_transport(transport.base + "newdir")
1420
transport6 = transport5.clone('..')
1421
self.check_transport_contents('bar', transport6, 'foo')
1374
1423
def test_lock_write(self):
1375
1424
"""Test transport-level write locks.