1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
|
# Copyright (C) 2005-2011, 2015, 2016 Canonical Ltd
#
# This program is free software; you can redistribute it and/or modify
# it under the terms of the GNU General Public License as published by
# the Free Software Foundation; either version 2 of the License, or
# (at your option) any later version.
#
# This program is distributed in the hope that it will be useful,
# but WITHOUT ANY WARRANTY; without even the implied warranty of
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
# GNU General Public License for more details.
#
# You should have received a copy of the GNU General Public License
# along with this program; if not, write to the Free Software
# Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
"""Tests for breezy.location."""
from .. import (
osutils,
tests,
urlutils,
)
from ..directory_service import directories
from ..location import (
hooks as location_hooks,
location_to_url,
rcp_location_to_url,
)
class SomeDirectory(object):
def look_up(self, name, url, purpose=None):
return "http://bar"
class TestLocationToUrl(tests.TestCase):
def get_base_location(self):
path = osutils.abspath('/foo/bar')
if path.startswith('/'):
url = 'file://%s' % (path,)
else:
# On Windows, abspaths start with the drive letter, so we have to
# add in the extra '/'
url = 'file:///%s' % (path,)
return path, url
def test_regular_url(self):
self.assertEqual("file://foo", location_to_url("file://foo"))
def test_directory(self):
directories.register("bar:", SomeDirectory, "Dummy directory")
self.addCleanup(directories.remove, "bar:")
self.assertEqual("http://bar", location_to_url("bar:"))
def test_unicode_url(self):
self.assertRaises(urlutils.InvalidURL, location_to_url,
b"http://fo/\xc3\xaf".decode("utf-8"))
def test_unicode_path(self):
path, url = self.get_base_location()
location = path + b"\xc3\xaf".decode("utf-8")
url += '%C3%AF'
self.assertEqual(url, location_to_url(location))
def test_path(self):
path, url = self.get_base_location()
self.assertEqual(url, location_to_url(path))
def test_relative_file_url(self):
self.assertEqual(urlutils.local_path_to_url(".") + "/bar",
location_to_url("file:bar"))
def test_absolute_file_url(self):
self.assertEqual("file:///bar", location_to_url("file:/bar"))
def test_rcp_url(self):
self.assertEqual(
"ssh://example.com/srv/git/bar",
location_to_url("example.com:/srv/git/bar"))
def test_rewrite_hook(self):
self.assertEqual(
'http://foo.example.com/blah', location_to_url('http://foo.example.com/blah'))
def rewrite_url(url, purpose=None):
return url.replace('foo', 'bar')
self.addCleanup(location_hooks.uninstall_named_hook, 'rewrite_url', 'test')
location_hooks.install_named_hook('rewrite_url', rewrite_url, 'test')
self.assertEqual(
'http://bar.example.com/bar', location_to_url('http://foo.example.com/foo'))
class RCPLocationTests(tests.TestCase):
def test_without_user(self):
self.assertEqual(
"git+ssh://example.com/srv/git/bar",
rcp_location_to_url("example.com:/srv/git/bar", scheme='git+ssh'))
self.assertEqual(
"ssh://example.com/srv/git/bar",
rcp_location_to_url("example.com:/srv/git/bar"))
def test_with_user(self):
self.assertEqual(
"git+ssh://foo@example.com/srv/git/bar",
rcp_location_to_url("foo@example.com:/srv/git/bar", scheme='git+ssh'))
def test_invalid(self):
self.assertRaises(ValueError, rcp_location_to_url, "http://srv/git/bar")
self.assertRaises(ValueError, rcp_location_to_url, "git/bar")
|