bzr branch
http://gegoxaren.bato24.eu/bzr/brz/remove-bazaar
|
7408.3.2
by Jelmer Vernooij
Add some tests. |
1 |
# Copyright (C) 2019 Breezy Developers
|
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
|
|
15 |
# Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
|
|
16 |
||
17 |
import os |
|
18 |
||
19 |
from ..propose import ( |
|
20 |
get_proposal_by_url, |
|
21 |
get_hoster, |
|
22 |
Hoster, |
|
23 |
MergeProposal, |
|
24 |
UnsupportedHoster, |
|
25 |
)
|
|
26 |
from .. import ( |
|
27 |
propose as _mod_propose, |
|
28 |
registry, |
|
29 |
urlutils, |
|
30 |
)
|
|
31 |
||
32 |
from .. import ( |
|
33 |
tests, |
|
34 |
)
|
|
35 |
||
36 |
||
37 |
class SampleMergeProposal(MergeProposal): |
|
38 |
"""Sample merge proposal.""" |
|
39 |
||
40 |
||
41 |
class SampleHoster(Hoster): |
|
42 |
||
43 |
_locations = [] |
|
44 |
||
45 |
@classmethod
|
|
46 |
def _add_location(cls, url): |
|
47 |
cls._locations.append(url) |
|
48 |
||
49 |
@classmethod
|
|
50 |
def probe_from_url(cls, url, possible_transports=None): |
|
51 |
for b in cls._locations: |
|
52 |
if url.startswith(b): |
|
53 |
return cls() |
|
54 |
raise UnsupportedHoster(url) |
|
55 |
||
56 |
def hosts(self, branch): |
|
57 |
for b in self._locations: |
|
58 |
if branch.user_url.startswith(b): |
|
59 |
return True |
|
60 |
return False |
|
61 |
||
62 |
@classmethod
|
|
63 |
def iter_instances(cls): |
|
64 |
return iter([cls()]) |
|
65 |
||
66 |
def get_proposal_by_url(self, url): |
|
67 |
for b in self._locations: |
|
68 |
if url.startswith(b): |
|
69 |
return MergeProposal() |
|
70 |
raise UnsupportedHoster(url) |
|
71 |
||
72 |
||
73 |
class SampleHosterTestCase(tests.TestCaseWithTransport): |
|
74 |
||
75 |
def setUp(self): |
|
76 |
super(SampleHosterTestCase, self).setUp() |
|
77 |
self._old_hosters = _mod_propose.hosters |
|
78 |
_mod_propose.hosters = registry.Registry() |
|
79 |
self.hoster = SampleHoster() |
|
80 |
os.mkdir('hosted') |
|
81 |
SampleHoster._add_location( |
|
82 |
urlutils.local_path_to_url(os.path.join(self.test_dir, 'hosted'))) |
|
83 |
_mod_propose.hosters.register('sample', self.hoster) |
|
84 |
||
85 |
def tearDown(self): |
|
86 |
super(SampleHosterTestCase, self).tearDown() |
|
87 |
_mod_propose.hosters = self._old_hosters |
|
88 |
SampleHoster._locations = [] |
|
89 |
||
90 |
||
91 |
class TestGetHosterTests(SampleHosterTestCase): |
|
92 |
||
93 |
def test_get_hoster(self): |
|
94 |
tree = self.make_branch_and_tree('hosted/branch') |
|
95 |
self.assertIs(self.hoster, get_hoster(tree.branch, [self.hoster])) |
|
96 |
self.assertIsInstance(get_hoster(tree.branch), SampleHoster) |
|
97 |
||
98 |
tree = self.make_branch_and_tree('blah') |
|
99 |
self.assertRaises(UnsupportedHoster, get_hoster, tree.branch) |
|
100 |
||
101 |
||
102 |
class TestGetProposal(SampleHosterTestCase): |
|
103 |
||
104 |
def test_get_proposal_by_url(self): |
|
105 |
self.assertRaises(UnsupportedHoster, get_proposal_by_url, 'blah') |
|
106 |
||
107 |
url = urlutils.local_path_to_url(os.path.join(self.test_dir, 'hosted', 'proposal')) |
|
108 |
self.assertIsInstance(get_proposal_by_url(url), MergeProposal) |