13
13
# You should have received a copy of the GNU General Public License
14
14
# along with this program; if not, write to the Free Software
15
# Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
15
# Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
17
17
"""Test directory service implementation"""
24
from bzrlib.directory_service import (
26
DirectoryServiceRegistry,
19
from bzrlib import errors
20
from bzrlib.directory_service import DirectoryServiceRegistry, directories
29
21
from bzrlib.tests import TestCase, TestCaseWithTransport
22
from bzrlib.transport import get_transport
32
25
class FooService(object):
33
26
"""A directory service that maps the name to a FILE url"""
35
# eg 'file:///foo' on Unix, or 'file:///C:/foo' on Windows
36
base = urlutils.local_path_to_url('/foo')
38
28
def look_up(self, name, url):
39
return self.base + name
29
return 'file:///foo' + name
42
32
class TestDirectoryLookup(TestCase):
45
super(TestDirectoryLookup, self).setUp()
46
36
self.registry = DirectoryServiceRegistry()
47
37
self.registry.register('foo:', FooService, 'Map foo URLs to http urls')
52
42
self.assertEqual('bar', suffix)
54
44
def test_dereference(self):
55
self.assertEqual(FooService.base + 'bar',
45
self.assertEqual('file:///foobar',
56
46
self.registry.dereference('foo:bar'))
57
47
self.assertEqual('baz:qux', self.registry.dereference('baz:qux'))
59
49
def test_get_transport(self):
60
50
directories.register('foo:', FooService, 'Map foo URLs to http urls')
61
self.addCleanup(directories.remove, 'foo:')
62
self.assertEqual(FooService.base + 'bar/',
63
transport.get_transport('foo:bar').base)
51
self.addCleanup(lambda: directories.remove('foo:'))
52
self.assertEqual('file:///foobar/', get_transport('foo:bar').base)
66
55
class TestAliasDirectory(TestCaseWithTransport):
69
super(TestAliasDirectory, self).setUp()
70
self.branch = self.make_branch('.')
72
def assertAliasFromBranch(self, setter, value, alias):
74
self.assertEqual(value, directories.dereference(alias))
76
57
def test_lookup_parent(self):
77
self.assertAliasFromBranch(self.branch.set_parent, 'http://a',
58
branch = self.make_branch('.')
59
branch.set_parent('http://a')
60
self.assertEqual('http://a', directories.dereference(':parent'))
80
62
def test_lookup_submit(self):
81
self.assertAliasFromBranch(self.branch.set_submit_branch, 'http://b',
63
branch = self.make_branch('.')
64
branch.set_submit_branch('http://b')
65
self.assertEqual('http://b', directories.dereference(':submit'))
84
67
def test_lookup_public(self):
85
self.assertAliasFromBranch(self.branch.set_public_branch, 'http://c',
68
branch = self.make_branch('.')
69
branch.set_public_branch('http://c')
70
self.assertEqual('http://c', directories.dereference(':public'))
88
72
def test_lookup_bound(self):
89
self.assertAliasFromBranch(self.branch.set_bound_location, 'http://d',
73
branch = self.make_branch('.')
74
branch.set_bound_location('http://d')
75
self.assertEqual('http://d', directories.dereference(':bound'))
92
77
def test_lookup_push(self):
93
self.assertAliasFromBranch(self.branch.set_push_location, 'http://e',
78
branch = self.make_branch('.')
79
branch.set_push_location('http://e')
80
self.assertEqual('http://e', directories.dereference(':push'))
96
82
def test_lookup_this(self):
97
self.assertEqual(self.branch.base, directories.dereference(':this'))
99
def test_extra_path(self):
100
self.assertEqual(urlutils.join(self.branch.base, 'arg'),
101
directories.dereference(':this/arg'))
83
branch = self.make_branch('.')
84
self.assertEqual(branch.base, directories.dereference(':this'))
103
86
def test_lookup_badname(self):
87
branch = self.make_branch('.')
104
88
e = self.assertRaises(errors.InvalidLocationAlias,
105
89
directories.dereference, ':booga')
106
90
self.assertEqual('":booga" is not a valid location alias.',
109
93
def test_lookup_badvalue(self):
94
branch = self.make_branch('.')
110
95
e = self.assertRaises(errors.UnsetLocationAlias,
111
96
directories.dereference, ':parent')
112
97
self.assertEqual('No parent location assigned.', str(e))
114
def test_register_location_alias(self):
115
self.addCleanup(AliasDirectory.branch_aliases.remove, "booga")
116
AliasDirectory.branch_aliases.register("booga",
117
lambda b: "UHH?", help="Nobody knows")
118
self.assertEqual("UHH?", directories.dereference(":booga"))
121
class TestColocatedDirectory(TestCaseWithTransport):
123
def test_lookup_non_default(self):
124
default = self.make_branch('.')
125
non_default = default.bzrdir.create_branch(name='nondefault')
126
self.assertEqual(non_default.base, directories.dereference('co:nondefault'))
128
def test_lookup_default(self):
129
default = self.make_branch('.')
130
non_default = default.bzrdir.create_branch(name='nondefault')
131
self.assertEqual(urlutils.join_segment_parameters(default.bzrdir.user_url,
132
{"branch": ""}), directories.dereference('co:'))
134
def test_no_such_branch(self):
135
# No error is raised in this case, that is up to the code that actually
137
default = self.make_branch('.')
138
self.assertEqual(urlutils.join_segment_parameters(default.bzrdir.user_url,
139
{"branch": "foo"}), directories.dereference('co:foo'))