/brz/remove-bazaar

To get this branch, use:
bzr branch http://gegoxaren.bato24.eu/bzr/brz/remove-bazaar
6614.1.3 by Vincent Ladeuil
Fix assertEquals being deprecated by using assertEqual.
1
# Copyright (C) 2008-2012, 2016 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
6624 by Jelmer Vernooij
Merge Python3 porting work ('py3 pokes')
19
from .. import (
5273.1.7 by Vincent Ladeuil
No more use of the get_transport imported *symbol*, all uses are through
20
    transport,
4789.13.1 by John Arbash Meinel
Change the DirectoryService tests a little bit.
21
    urlutils,
22
    )
6624 by Jelmer Vernooij
Merge Python3 porting work ('py3 pokes')
23
from ..directory_service import (
6319.2.1 by Jelmer Vernooij
Allow registering custom location aliases.
24
    AliasDirectory,
25
    DirectoryServiceRegistry,
6734.1.15 by Jelmer Vernooij
Move errors for breezy.directory_service.
26
    InvalidLocationAlias,
27
    UnsetLocationAlias,
6319.2.1 by Jelmer Vernooij
Allow registering custom location aliases.
28
    directories,
29
    )
6624 by Jelmer Vernooij
Merge Python3 porting work ('py3 pokes')
30
from . import TestCase, TestCaseWithTransport
3625.1.2 by Michael Hudson
import urlutils and write urlutils.join rather than join
31
3251.3.1 by Aaron Bentley
Add support for directory services
32
33
class FooService(object):
34
    """A directory service that maps the name to a FILE url"""
35
5278.1.2 by Martin Pool
Don't say 'Linux' except when specifically talking about the kernel
36
    # 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.
37
    base = urlutils.local_path_to_url('/foo')
38
7268.11.2 by Jelmer Vernooij
Add purpose argument.
39
    def look_up(self, name, url, purpose=None):
4789.13.1 by John Arbash Meinel
Change the DirectoryService tests a little bit.
40
        return self.base + name
3251.3.1 by Aaron Bentley
Add support for directory services
41
42
43
class TestDirectoryLookup(TestCase):
44
45
    def setUp(self):
6552.1.4 by Vincent Ladeuil
Remaining tests matching setup(self) that can be rewritten with super().
46
        super(TestDirectoryLookup, self).setUp()
3251.3.1 by Aaron Bentley
Add support for directory services
47
        self.registry = DirectoryServiceRegistry()
48
        self.registry.register('foo:', FooService, 'Map foo URLs to http urls')
49
50
    def test_get_directory_service(self):
51
        directory, suffix = self.registry.get_prefix('foo:bar')
52
        self.assertIs(FooService, directory)
53
        self.assertEqual('bar', suffix)
54
55
    def test_dereference(self):
4789.13.1 by John Arbash Meinel
Change the DirectoryService tests a little bit.
56
        self.assertEqual(FooService.base + 'bar',
3251.3.1 by Aaron Bentley
Add support for directory services
57
                         self.registry.dereference('foo:bar'))
7268.11.2 by Jelmer Vernooij
Add purpose argument.
58
        self.assertEqual(FooService.base + 'bar',
7268.11.5 by Jelmer Vernooij
s/push/write.
59
                         self.registry.dereference('foo:bar', purpose='write'))
3251.3.1 by Aaron Bentley
Add support for directory services
60
        self.assertEqual('baz:qux', self.registry.dereference('baz:qux'))
7268.11.2 by Jelmer Vernooij
Add purpose argument.
61
        self.assertEqual(
62
            'baz:qux',
7268.11.5 by Jelmer Vernooij
s/push/write.
63
            self.registry.dereference('baz:qux', purpose='write'))
3251.3.1 by Aaron Bentley
Add support for directory services
64
65
    def test_get_transport(self):
66
        directories.register('foo:', FooService, 'Map foo URLs to http urls')
4985.2.1 by Vincent Ladeuil
Deploy addAttrCleanup on the whole test suite.
67
        self.addCleanup(directories.remove, 'foo:')
4789.13.1 by John Arbash Meinel
Change the DirectoryService tests a little bit.
68
        self.assertEqual(FooService.base + 'bar/',
5273.1.7 by Vincent Ladeuil
No more use of the get_transport imported *symbol*, all uses are through
69
                         transport.get_transport('foo:bar').base)
3512.2.1 by Aaron Bentley
Add support for branch-associated locations
70
71
7268.11.3 by Jelmer Vernooij
Add backwards compatibility for older users of the directory API.
72
class OldService(object):
73
    """A directory service that maps the name to a FILE url"""
74
75
    # eg 'file:///foo' on Unix, or 'file:///C:/foo' on Windows
76
    base = urlutils.local_path_to_url('/foo')
77
78
    def look_up(self, name, url):
79
        return self.base + name
80
81
82
class TestOldDirectoryLookup(TestCase):
83
    """Test compatibility with older implementations of Directory
84
    that don't support the purpose argument."""
85
86
    def setUp(self):
87
        super(TestOldDirectoryLookup, self).setUp()
88
        self.registry = DirectoryServiceRegistry()
89
        self.registry.register('old:', OldService, 'Map foo URLs to http urls')
90
91
    def test_dereference(self):
92
        self.assertEqual(OldService.base + 'bar',
93
                         self.registry.dereference('old:bar'))
94
        self.assertEqual(OldService.base + 'bar',
7268.11.5 by Jelmer Vernooij
s/push/write.
95
                         self.registry.dereference('old:bar', purpose='write'))
7268.11.3 by Jelmer Vernooij
Add backwards compatibility for older users of the directory API.
96
        self.assertEqual('baz:qux', self.registry.dereference('baz:qux'))
97
        self.assertEqual(
98
            'baz:qux',
7268.11.5 by Jelmer Vernooij
s/push/write.
99
            self.registry.dereference('baz:qux', purpose='write'))
7268.11.3 by Jelmer Vernooij
Add backwards compatibility for older users of the directory API.
100
101
3512.2.1 by Aaron Bentley
Add support for branch-associated locations
102
class TestAliasDirectory(TestCaseWithTransport):
103
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.
104
    def setUp(self):
105
        super(TestAliasDirectory, self).setUp()
106
        self.branch = self.make_branch('.')
107
108
    def assertAliasFromBranch(self, setter, value, alias):
6404.6.7 by Vincent Ladeuil
Change set/remove to require a lock for the branch config files.
109
        setter(value)
6614.1.3 by Vincent Ladeuil
Fix assertEquals being deprecated by using assertEqual.
110
        self.assertEqual(value, directories.dereference(alias))
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.
111
3512.2.1 by Aaron Bentley
Add support for branch-associated locations
112
    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.
113
        self.assertAliasFromBranch(self.branch.set_parent, 'http://a',
7143.15.2 by Jelmer Vernooij
Run autopep8.
114
                                   ':parent')
3512.2.1 by Aaron Bentley
Add support for branch-associated locations
115
116
    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.
117
        self.assertAliasFromBranch(self.branch.set_submit_branch, 'http://b',
118
                                   ':submit')
3512.2.1 by Aaron Bentley
Add support for branch-associated locations
119
120
    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.
121
        self.assertAliasFromBranch(self.branch.set_public_branch, 'http://c',
122
                                   ':public')
3512.2.1 by Aaron Bentley
Add support for branch-associated locations
123
124
    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.
125
        self.assertAliasFromBranch(self.branch.set_bound_location, 'http://d',
126
                                   ':bound')
3512.2.1 by Aaron Bentley
Add support for branch-associated locations
127
3512.2.2 by Aaron Bentley
Add :push and :this
128
    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.
129
        self.assertAliasFromBranch(self.branch.set_push_location, 'http://e',
130
                                   ':push')
3512.2.2 by Aaron Bentley
Add :push and :this
131
132
    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.
133
        self.assertEqual(self.branch.base, directories.dereference(':this'))
3512.2.2 by Aaron Bentley
Add :push and :this
134
3625.1.1 by Michael Hudson
Allow appending path segments to the :<name> style aliases.
135
    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.
136
        self.assertEqual(urlutils.join(self.branch.base, 'arg'),
3625.1.2 by Michael Hudson
import urlutils and write urlutils.join rather than join
137
                         directories.dereference(':this/arg'))
3625.1.1 by Michael Hudson
Allow appending path segments to the :<name> style aliases.
138
3512.2.1 by Aaron Bentley
Add support for branch-associated locations
139
    def test_lookup_badname(self):
6734.1.15 by Jelmer Vernooij
Move errors for breezy.directory_service.
140
        e = self.assertRaises(InvalidLocationAlias,
3512.2.1 by Aaron Bentley
Add support for branch-associated locations
141
                              directories.dereference, ':booga')
142
        self.assertEqual('":booga" is not a valid location alias.',
143
                         str(e))
144
145
    def test_lookup_badvalue(self):
6734.1.15 by Jelmer Vernooij
Move errors for breezy.directory_service.
146
        e = self.assertRaises(UnsetLocationAlias,
3512.2.1 by Aaron Bentley
Add support for branch-associated locations
147
                              directories.dereference, ':parent')
148
        self.assertEqual('No parent location assigned.', str(e))
6319.2.1 by Jelmer Vernooij
Allow registering custom location aliases.
149
150
    def test_register_location_alias(self):
151
        self.addCleanup(AliasDirectory.branch_aliases.remove, "booga")
152
        AliasDirectory.branch_aliases.register("booga",
7143.15.2 by Jelmer Vernooij
Run autopep8.
153
                                               lambda b: "UHH?", help="Nobody knows")
6614.1.3 by Vincent Ladeuil
Fix assertEquals being deprecated by using assertEqual.
154
        self.assertEqual("UHH?", directories.dereference(":booga"))
6511.3.2 by Jelmer Vernooij
Tests.
155
156
157
class TestColocatedDirectory(TestCaseWithTransport):
158
159
    def test_lookup_non_default(self):
160
        default = self.make_branch('.')
6653.6.1 by Jelmer Vernooij
Rename a number of attributes from bzrdir to controldir.
161
        non_default = default.controldir.create_branch(name='nondefault')
7143.15.2 by Jelmer Vernooij
Run autopep8.
162
        self.assertEqual(non_default.base,
163
                         directories.dereference('co:nondefault'))
6511.3.2 by Jelmer Vernooij
Tests.
164
165
    def test_lookup_default(self):
166
        default = self.make_branch('.')
6653.6.1 by Jelmer Vernooij
Rename a number of attributes from bzrdir to controldir.
167
        non_default = default.controldir.create_branch(name='nondefault')
168
        self.assertEqual(urlutils.join_segment_parameters(default.controldir.user_url,
7143.15.2 by Jelmer Vernooij
Run autopep8.
169
                                                          {"branch": ""}), directories.dereference('co:'))
6511.3.2 by Jelmer Vernooij
Tests.
170
171
    def test_no_such_branch(self):
172
        # No error is raised in this case, that is up to the code that actually
173
        # opens the branch.
174
        default = self.make_branch('.')
6653.6.1 by Jelmer Vernooij
Rename a number of attributes from bzrdir to controldir.
175
        self.assertEqual(urlutils.join_segment_parameters(default.controldir.user_url,
7143.15.2 by Jelmer Vernooij
Run autopep8.
176
                                                          {"branch": "foo"}), directories.dereference('co:foo'))