1
# Copyright (C) 2005-2011 Canonical Ltd
1
# Copyright (C) 2005-2011, 2015, 2016 Canonical Ltd
3
3
# This program is free software; you can redistribute it and/or modify
4
4
# it under the terms of the GNU General Public License as published by
53
53
from bzrlib.tests.test_transport import TestTransportImplementation
54
54
from bzrlib.transport import (
55
55
ConnectedTransport,
56
57
_get_transport_modules,
58
59
from bzrlib.transport.memory import MemoryTransport
60
from bzrlib.transport.remote import RemoteTransport
61
63
def get_transport_test_permutations(module):
518
520
self.assertTransportMode(t, 'dir777', 0777)
520
522
def test_put_bytes_unicode(self):
521
# Expect put_bytes to raise AssertionError or UnicodeEncodeError if
522
# given unicode "bytes". UnicodeEncodeError doesn't really make sense
523
# (we don't want to encode unicode here at all, callers should be
524
# strictly passing bytes to put_bytes), but we allow it for backwards
525
# compatibility. At some point we should use a specific exception.
526
# See https://bugs.launchpad.net/bzr/+bug/106898.
527
523
t = self.get_transport()
528
524
if t.is_readonly():
530
526
unicode_string = u'\u1234'
532
(AssertionError, UnicodeEncodeError),
533
t.put_bytes, 'foo', unicode_string)
535
def test_put_file_unicode(self):
536
# Like put_bytes, except with a StringIO.StringIO of a unicode string.
537
# This situation can happen (and has) if code is careless about the type
538
# of "string" they initialise/write to a StringIO with. We cannot use
539
# cStringIO, because it never returns unicode from read.
540
# Like put_bytes, UnicodeEncodeError isn't quite the right exception to
541
# raise, but we raise it for hysterical raisins.
542
t = self.get_transport()
545
unicode_file = pyStringIO(u'\u1234')
546
self.assertRaises(UnicodeEncodeError, t.put_file, 'foo', unicode_file)
527
self.assertRaises(TypeError, t.put_bytes, 'foo', unicode_string)
548
529
def test_mkdir(self):
549
530
t = self.get_transport()
628
609
def test_opening_a_file_stream_can_set_mode(self):
629
610
t = self.get_transport()
630
611
if t.is_readonly():
612
self.assertRaises((TransportNotPossible, NotImplementedError),
613
t.open_write_stream, 'foo')
632
615
if not t._can_roundtrip_unix_modebits():
633
616
# Can't roundtrip, so no need to run this test
635
619
def check_mode(name, mode, expected):
636
620
handle = t.open_write_stream(name, mode=mode)
1000
988
# perhaps all of this could be done in a subdirectory
1002
990
t.put_bytes('a', 'a first file\n')
1003
self.assertEquals([True, False], list(t.has_multi(['a', 'b'])))
991
self.assertEqual([True, False], list(t.has_multi(['a', 'b'])))
1005
993
t.move('a', 'b')
1006
994
self.assertTrue(t.has('b'))
1007
995
self.assertFalse(t.has('a'))
1009
997
self.check_transport_contents('a first file\n', t, 'b')
1010
self.assertEquals([False, True], list(t.has_multi(['a', 'b'])))
998
self.assertEqual([False, True], list(t.has_multi(['a', 'b'])))
1012
1000
# Overwrite a file
1013
1001
t.put_bytes('c', 'c this file\n')
1206
1194
raise TestSkipped("not a connected transport")
1208
1196
t2 = t1.clone('subdir')
1209
self.assertEquals(t1._parsed_url.scheme, t2._parsed_url.scheme)
1210
self.assertEquals(t1._parsed_url.user, t2._parsed_url.user)
1211
self.assertEquals(t1._parsed_url.password, t2._parsed_url.password)
1212
self.assertEquals(t1._parsed_url.host, t2._parsed_url.host)
1213
self.assertEquals(t1._parsed_url.port, t2._parsed_url.port)
1197
self.assertEqual(t1._parsed_url.scheme, t2._parsed_url.scheme)
1198
self.assertEqual(t1._parsed_url.user, t2._parsed_url.user)
1199
self.assertEqual(t1._parsed_url.password, t2._parsed_url.password)
1200
self.assertEqual(t1._parsed_url.host, t2._parsed_url.host)
1201
self.assertEqual(t1._parsed_url.port, t2._parsed_url.port)
1215
1203
def test__reuse_for(self):
1216
1204
t = self.get_transport()
1780
1768
"""Segment parameters should be stripped and stored in
1781
1769
transport.segment_parameters."""
1782
1770
transport = self.get_transport("foo")
1783
self.assertEquals({}, transport.get_segment_parameters())
1771
self.assertEqual({}, transport.get_segment_parameters())
1785
1773
def test_segment_parameters(self):
1786
1774
"""Segment parameters should be stripped and stored in
1789
1777
parameters = {"key1": "val1", "key2": "val2"}
1790
1778
url = urlutils.join_segment_parameters(base_url, parameters)
1791
1779
transport = _mod_transport.get_transport_from_url(url)
1792
self.assertEquals(parameters, transport.get_segment_parameters())
1780
self.assertEqual(parameters, transport.get_segment_parameters())
1794
1782
def test_set_segment_parameters(self):
1795
1783
"""Segment parameters can be set and show up in base."""
1796
1784
transport = self.get_transport("foo")
1797
1785
orig_base = transport.base
1798
1786
transport.set_segment_parameter("arm", "board")
1799
self.assertEquals("%s,arm=board" % orig_base, transport.base)
1800
self.assertEquals({"arm": "board"}, transport.get_segment_parameters())
1787
self.assertEqual("%s,arm=board" % orig_base, transport.base)
1788
self.assertEqual({"arm": "board"}, transport.get_segment_parameters())
1801
1789
transport.set_segment_parameter("arm", None)
1802
1790
transport.set_segment_parameter("nonexistant", None)
1803
self.assertEquals({}, transport.get_segment_parameters())
1804
self.assertEquals(orig_base, transport.base)
1791
self.assertEqual({}, transport.get_segment_parameters())
1792
self.assertEqual(orig_base, transport.base)
1806
1794
def test_stat_symlink(self):
1807
1795
# if a transport points directly to a symlink (and supports symlinks
1836
1824
self.build_tree([needlessly_escaped_dir], transport=t1)
1837
1825
t2 = t1.clone(needlessly_escaped_dir)
1838
1826
self.assertEqual(t1.base + "-.09AZ_az~/", t2.base)
1828
def test_hook_post_connection_one(self):
1829
"""Fire post_connect hook after a ConnectedTransport is first used"""
1831
Transport.hooks.install_named_hook("post_connect", log.append, None)
1832
t = self.get_transport()
1833
self.assertEqual([], log)
1834
t.has("non-existant")
1835
if isinstance(t, RemoteTransport):
1836
self.assertEqual([t.get_smart_medium()], log)
1837
elif isinstance(t, ConnectedTransport):
1838
self.assertEqual([t], log)
1840
self.assertEqual([], log)
1842
def test_hook_post_connection_multi(self):
1843
"""Fire post_connect hook once per unshared underlying connection"""
1845
Transport.hooks.install_named_hook("post_connect", log.append, None)
1846
t1 = self.get_transport()
1848
t3 = self.get_transport()
1849
self.assertEqual([], log)
1853
if isinstance(t1, RemoteTransport):
1854
self.assertEqual([t.get_smart_medium() for t in [t1, t3]], log)
1855
elif isinstance(t1, ConnectedTransport):
1856
self.assertEqual([t1, t3], log)
1858
self.assertEqual([], log)