~bzr-pqm/bzr/bzr.dev

6404.6.1 by Vincent Ladeuil
Tests passing for a first rough version of a cached branch config store. The changes here are too invasive and several parallel proposals have been made.
1
# Copyright (C) 2008-2012 Canonical Ltd
3251.3.1 by Aaron Bentley
Add support for directory services
2
#
3
# This program is free software; you can redistribute it and/or modify
4
# it under the terms of the GNU General Public License as published by
5
# the Free Software Foundation; either version 2 of the License, or
6
# (at your option) any later version.
7
#
8
# This program is distributed in the hope that it will be useful,
9
# but WITHOUT ANY WARRANTY; without even the implied warranty of
10
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
11
# GNU General Public License for more details.
12
#
13
# You should have received a copy of the GNU General Public License
14
# along with this program; if not, write to the Free Software
4183.7.1 by Sabin Iacob
update FSF mailing address
15
# Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
3251.3.1 by Aaron Bentley
Add support for directory services
16
17
"""Test directory service implementation"""
18
4789.13.1 by John Arbash Meinel
Change the DirectoryService tests a little bit.
19
from bzrlib import (
20
    errors,
5273.1.7 by Vincent Ladeuil
No more use of the get_transport imported *symbol*, all uses are through
21
    transport,
4789.13.1 by John Arbash Meinel
Change the DirectoryService tests a little bit.
22
    urlutils,
23
    )
6319.2.1 by Jelmer Vernooij
Allow registering custom location aliases.
24
from bzrlib.directory_service import (
25
    AliasDirectory,
26
    DirectoryServiceRegistry,
27
    directories,
28
    )
3512.2.1 by Aaron Bentley
Add support for branch-associated locations
29
from bzrlib.tests import TestCase, TestCaseWithTransport
3625.1.2 by Michael Hudson
import urlutils and write urlutils.join rather than join
30
3251.3.1 by Aaron Bentley
Add support for directory services
31
32
class FooService(object):
33
    """A directory service that maps the name to a FILE url"""
34
5278.1.2 by Martin Pool
Don't say 'Linux' except when specifically talking about the kernel
35
    # eg 'file:///foo' on Unix, or 'file:///C:/foo' on Windows
4789.13.1 by John Arbash Meinel
Change the DirectoryService tests a little bit.
36
    base = urlutils.local_path_to_url('/foo')
37
3251.3.1 by Aaron Bentley
Add support for directory services
38
    def look_up(self, name, url):
4789.13.1 by John Arbash Meinel
Change the DirectoryService tests a little bit.
39
        return self.base + name
3251.3.1 by Aaron Bentley
Add support for directory services
40
41
42
class TestDirectoryLookup(TestCase):
43
44
    def setUp(self):
45
        TestCase.setUp(self)
46
        self.registry = DirectoryServiceRegistry()
47
        self.registry.register('foo:', FooService, 'Map foo URLs to http urls')
48
49
    def test_get_directory_service(self):
50
        directory, suffix = self.registry.get_prefix('foo:bar')
51
        self.assertIs(FooService, directory)
52
        self.assertEqual('bar', suffix)
53
54
    def test_dereference(self):
4789.13.1 by John Arbash Meinel
Change the DirectoryService tests a little bit.
55
        self.assertEqual(FooService.base + 'bar',
3251.3.1 by Aaron Bentley
Add support for directory services
56
                         self.registry.dereference('foo:bar'))
57
        self.assertEqual('baz:qux', self.registry.dereference('baz:qux'))
58
59
    def test_get_transport(self):
60
        directories.register('foo:', FooService, 'Map foo URLs to http urls')
4985.2.1 by Vincent Ladeuil
Deploy addAttrCleanup on the whole test suite.
61
        self.addCleanup(directories.remove, 'foo:')
4789.13.1 by John Arbash Meinel
Change the DirectoryService tests a little bit.
62
        self.assertEqual(FooService.base + 'bar/',
5273.1.7 by Vincent Ladeuil
No more use of the get_transport imported *symbol*, all uses are through
63
                         transport.get_transport('foo:bar').base)
3512.2.1 by Aaron Bentley
Add support for branch-associated locations
64
65
66
class TestAliasDirectory(TestCaseWithTransport):
67
6404.6.1 by Vincent Ladeuil
Tests passing for a first rough version of a cached branch config store. The changes here are too invasive and several parallel proposals have been made.
68
    def setUp(self):
69
        super(TestAliasDirectory, self).setUp()
70
        self.branch = self.make_branch('.')
71
72
    def assertAliasFromBranch(self, setter, value, alias):
6404.6.7 by Vincent Ladeuil
Change set/remove to require a lock for the branch config files.
73
        setter(value)
6404.6.1 by Vincent Ladeuil
Tests passing for a first rough version of a cached branch config store. The changes here are too invasive and several parallel proposals have been made.
74
        self.assertEquals(value, directories.dereference(alias))
75
3512.2.1 by Aaron Bentley
Add support for branch-associated locations
76
    def test_lookup_parent(self):
6404.6.1 by Vincent Ladeuil
Tests passing for a first rough version of a cached branch config store. The changes here are too invasive and several parallel proposals have been made.
77
        self.assertAliasFromBranch(self.branch.set_parent, 'http://a',
78
                                  ':parent')
3512.2.1 by Aaron Bentley
Add support for branch-associated locations
79
80
    def test_lookup_submit(self):
6404.6.1 by Vincent Ladeuil
Tests passing for a first rough version of a cached branch config store. The changes here are too invasive and several parallel proposals have been made.
81
        self.assertAliasFromBranch(self.branch.set_submit_branch, 'http://b',
82
                                   ':submit')
3512.2.1 by Aaron Bentley
Add support for branch-associated locations
83
84
    def test_lookup_public(self):
6404.6.1 by Vincent Ladeuil
Tests passing for a first rough version of a cached branch config store. The changes here are too invasive and several parallel proposals have been made.
85
        self.assertAliasFromBranch(self.branch.set_public_branch, 'http://c',
86
                                   ':public')
3512.2.1 by Aaron Bentley
Add support for branch-associated locations
87
88
    def test_lookup_bound(self):
6404.6.1 by Vincent Ladeuil
Tests passing for a first rough version of a cached branch config store. The changes here are too invasive and several parallel proposals have been made.
89
        self.assertAliasFromBranch(self.branch.set_bound_location, 'http://d',
90
                                   ':bound')
3512.2.1 by Aaron Bentley
Add support for branch-associated locations
91
3512.2.2 by Aaron Bentley
Add :push and :this
92
    def test_lookup_push(self):
6404.6.1 by Vincent Ladeuil
Tests passing for a first rough version of a cached branch config store. The changes here are too invasive and several parallel proposals have been made.
93
        self.assertAliasFromBranch(self.branch.set_push_location, 'http://e',
94
                                   ':push')
3512.2.2 by Aaron Bentley
Add :push and :this
95
96
    def test_lookup_this(self):
6404.6.1 by Vincent Ladeuil
Tests passing for a first rough version of a cached branch config store. The changes here are too invasive and several parallel proposals have been made.
97
        self.assertEqual(self.branch.base, directories.dereference(':this'))
3512.2.2 by Aaron Bentley
Add :push and :this
98
3625.1.1 by Michael Hudson
Allow appending path segments to the :<name> style aliases.
99
    def test_extra_path(self):
6404.6.1 by Vincent Ladeuil
Tests passing for a first rough version of a cached branch config store. The changes here are too invasive and several parallel proposals have been made.
100
        self.assertEqual(urlutils.join(self.branch.base, 'arg'),
3625.1.2 by Michael Hudson
import urlutils and write urlutils.join rather than join
101
                         directories.dereference(':this/arg'))
3625.1.1 by Michael Hudson
Allow appending path segments to the :<name> style aliases.
102
3512.2.1 by Aaron Bentley
Add support for branch-associated locations
103
    def test_lookup_badname(self):
104
        e = self.assertRaises(errors.InvalidLocationAlias,
105
                              directories.dereference, ':booga')
106
        self.assertEqual('":booga" is not a valid location alias.',
107
                         str(e))
108
109
    def test_lookup_badvalue(self):
110
        e = self.assertRaises(errors.UnsetLocationAlias,
111
                              directories.dereference, ':parent')
112
        self.assertEqual('No parent location assigned.', str(e))
6319.2.1 by Jelmer Vernooij
Allow registering custom location aliases.
113
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.assertEquals("UHH?", directories.dereference(":booga"))