1
# Copyright (C) 2007-2010 Canonical Ltd
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.
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.
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
18
from .. import bugtracker, urlutils
19
from . import TestCase, TestCaseWithMemoryTransport
22
class ErrorsTest(TestCaseWithMemoryTransport):
24
def test_unknown_bug_tracker_abbreviation(self):
25
"""Test the formatting of UnknownBugTrackerAbbreviation."""
26
branch = self.make_branch('some_branch')
27
error = bugtracker.UnknownBugTrackerAbbreviation('xxx', branch)
29
"Cannot find registered bug tracker called xxx on %s" % branch,
32
def test_malformed_bug_identifier(self):
33
"""Test the formatting of MalformedBugIdentifier."""
34
error = bugtracker.MalformedBugIdentifier(
35
'bogus', 'reason for bogosity')
37
'Did not understand bug identifier bogus: reason for bogosity. '
38
'See "brz help bugs" for more information on this feature.',
41
def test_incorrect_url(self):
42
err = bugtracker.InvalidBugTrackerURL('foo', 'http://bug.example.com/')
44
("The URL for bug tracker \"foo\" doesn't contain {id}: "
45
"http://bug.example.com/"),
49
class TestGetBugURL(TestCaseWithMemoryTransport):
50
"""Tests for bugtracker.get_bug_url"""
52
class TransientTracker(object):
53
"""An transient tracker used for testing."""
56
def get(klass, abbreviation, branch):
57
klass.log.append(('get', abbreviation, branch))
58
if abbreviation != 'transient':
62
def get_bug_url(self, bug_id):
63
self.log.append(('get_bug_url', bug_id))
64
return "http://bugs.example.com/%s" % bug_id
67
super(TestGetBugURL, self).setUp()
68
self.tracker_type = TestGetBugURL.TransientTracker
69
self.tracker_type.log = []
70
bugtracker.tracker_registry.register('transient', self.tracker_type)
71
self.addCleanup(bugtracker.tracker_registry.remove, 'transient')
73
def test_get_bug_url_for_transient_tracker(self):
74
branch = self.make_branch('some_branch')
75
self.assertEqual('http://bugs.example.com/1234',
76
bugtracker.get_bug_url('transient', branch, '1234'))
78
[('get', 'transient', branch), ('get_bug_url', '1234')],
79
self.tracker_type.log)
81
def test_unrecognized_abbreviation_raises_error(self):
82
"""If the abbreviation is unrecognized, then raise an error."""
83
branch = self.make_branch('some_branch')
84
self.assertRaises(bugtracker.UnknownBugTrackerAbbreviation,
85
bugtracker.get_bug_url, 'xxx', branch, '1234')
86
self.assertEqual([('get', 'xxx', branch)], self.tracker_type.log)
89
class TestBuiltinTrackers(TestCaseWithMemoryTransport):
90
"""Test that the builtin trackers are registered and return sane URLs."""
92
def test_launchpad_registered(self):
93
"""The Launchpad bug tracker should be registered by default and
94
generate Launchpad bug page URLs.
96
branch = self.make_branch('some_branch')
97
tracker = bugtracker.tracker_registry.get_tracker('lp', branch)
98
self.assertEqual('https://launchpad.net/bugs/1234',
99
tracker.get_bug_url('1234'))
101
def test_debian_registered(self):
102
"""The Debian bug tracker should be registered by default and generate
103
bugs.debian.org bug page URLs.
105
branch = self.make_branch('some_branch')
106
tracker = bugtracker.tracker_registry.get_tracker('deb', branch)
107
self.assertEqual('http://bugs.debian.org/1234',
108
tracker.get_bug_url('1234'))
110
def test_gnome_registered(self):
111
branch = self.make_branch('some_branch')
112
tracker = bugtracker.tracker_registry.get_tracker('gnome', branch)
113
self.assertEqual('http://bugzilla.gnome.org/show_bug.cgi?id=1234',
114
tracker.get_bug_url('1234'))
116
def test_trac_registered(self):
117
"""The Trac bug tracker should be registered by default and generate
118
Trac bug page URLs when the appropriate configuration is present.
120
branch = self.make_branch('some_branch')
121
config = branch.get_config()
122
config.set_user_option('trac_foo_url', 'http://bugs.example.com/trac')
123
tracker = bugtracker.tracker_registry.get_tracker('foo', branch)
124
self.assertEqual('http://bugs.example.com/trac/ticket/1234',
125
tracker.get_bug_url('1234'))
127
def test_bugzilla_registered(self):
128
"""The Bugzilla bug tracker should be registered by default and
129
generate Bugzilla bug page URLs when the appropriate configuration is
132
branch = self.make_branch('some_branch')
133
config = branch.get_config()
134
config.set_user_option('bugzilla_foo_url', 'http://bugs.example.com')
135
tracker = bugtracker.tracker_registry.get_tracker('foo', branch)
136
self.assertEqual('http://bugs.example.com/show_bug.cgi?id=1234',
137
tracker.get_bug_url('1234'))
139
def test_github(self):
140
branch = self.make_branch('some_branch')
141
tracker = bugtracker.tracker_registry.get_tracker('github', branch)
142
self.assertEqual('https://github.com/breezy-team/breezy/issues/1234',
143
tracker.get_bug_url('breezy-team/breezy/1234'))
145
def test_generic_registered(self):
146
branch = self.make_branch('some_branch')
147
config = branch.get_config()
148
config.set_user_option('bugtracker_foo_url',
149
'http://bugs.example.com/{id}/view.html')
150
tracker = bugtracker.tracker_registry.get_tracker('foo', branch)
151
self.assertEqual('http://bugs.example.com/1234/view.html',
152
tracker.get_bug_url('1234'))
154
def test_generic_registered_non_integer(self):
155
branch = self.make_branch('some_branch')
156
config = branch.get_config()
157
config.set_user_option('bugtracker_foo_url',
158
'http://bugs.example.com/{id}/view.html')
159
tracker = bugtracker.tracker_registry.get_tracker('foo', branch)
160
self.assertEqual('http://bugs.example.com/ABC-1234/view.html',
161
tracker.get_bug_url('ABC-1234'))
163
def test_generic_incorrect_url(self):
164
branch = self.make_branch('some_branch')
165
config = branch.get_config()
166
config.set_user_option('bugtracker_foo_url',
167
'http://bugs.example.com/view.html')
168
tracker = bugtracker.tracker_registry.get_tracker('foo', branch)
169
self.assertRaises(bugtracker.InvalidBugTrackerURL, tracker.get_bug_url,
173
class TestUniqueIntegerBugTracker(TestCaseWithMemoryTransport):
175
def test_appends_id_to_base_url(self):
176
"""The URL of a bug is the base URL joined to the identifier."""
177
tracker = bugtracker.UniqueIntegerBugTracker('xxx',
178
'http://bugs.example.com/foo')
179
self.assertEqual('http://bugs.example.com/foo1234',
180
tracker.get_bug_url('1234'))
182
def test_returns_tracker_if_abbreviation_matches(self):
183
"""The get() method should return an instance of the tracker if the
184
given abbreviation matches the tracker's abbreviated name.
186
tracker = bugtracker.UniqueIntegerBugTracker('xxx',
187
'http://bugs.example.com/')
188
branch = self.make_branch('some_branch')
189
self.assertIs(tracker, tracker.get('xxx', branch))
191
def test_returns_none_if_abbreviation_doesnt_match(self):
192
"""The get() method should return None if the given abbreviated name
193
doesn't match the tracker's abbreviation.
195
tracker = bugtracker.UniqueIntegerBugTracker('xxx',
196
'http://bugs.example.com/')
197
branch = self.make_branch('some_branch')
198
self.assertIs(None, tracker.get('yyy', branch))
200
def test_doesnt_consult_branch(self):
201
"""A UniqueIntegerBugTracker shouldn't consult the branch for tracker
204
tracker = bugtracker.UniqueIntegerBugTracker('xxx',
205
'http://bugs.example.com/')
206
self.assertIs(tracker, tracker.get('xxx', None))
207
self.assertIs(None, tracker.get('yyy', None))
209
def test_check_bug_id_only_accepts_integers(self):
210
"""A UniqueIntegerBugTracker accepts integers as bug IDs."""
211
tracker = bugtracker.UniqueIntegerBugTracker('xxx',
212
'http://bugs.example.com/')
213
tracker.check_bug_id('1234')
215
def test_check_bug_id_doesnt_accept_non_integers(self):
216
"""A UniqueIntegerBugTracker rejects non-integers as bug IDs."""
217
tracker = bugtracker.UniqueIntegerBugTracker('xxx',
218
'http://bugs.example.com/')
220
bugtracker.MalformedBugIdentifier, tracker.check_bug_id, 'red')
223
class TestProjectIntegerBugTracker(TestCaseWithMemoryTransport):
225
def test_appends_id_to_base_url(self):
226
"""The URL of a bug is the base URL joined to the identifier."""
227
tracker = bugtracker.ProjectIntegerBugTracker('xxx',
228
'http://bugs.example.com/{project}/{id}')
229
self.assertEqual('http://bugs.example.com/foo/1234',
230
tracker.get_bug_url('foo/1234'))
232
def test_returns_tracker_if_abbreviation_matches(self):
233
"""The get() method should return an instance of the tracker if the
234
given abbreviation matches the tracker's abbreviated name.
236
tracker = bugtracker.ProjectIntegerBugTracker('xxx',
237
'http://bugs.example.com/{project}/{id}')
238
branch = self.make_branch('some_branch')
239
self.assertIs(tracker, tracker.get('xxx', branch))
241
def test_returns_none_if_abbreviation_doesnt_match(self):
242
"""The get() method should return None if the given abbreviated name
243
doesn't match the tracker's abbreviation.
245
tracker = bugtracker.ProjectIntegerBugTracker('xxx',
246
'http://bugs.example.com/{project}/{id}')
247
branch = self.make_branch('some_branch')
248
self.assertIs(None, tracker.get('yyy', branch))
250
def test_doesnt_consult_branch(self):
251
"""Shouldn't consult the branch for tracker information.
253
tracker = bugtracker.ProjectIntegerBugTracker('xxx',
254
'http://bugs.example.com/{project}/{id}')
255
self.assertIs(tracker, tracker.get('xxx', None))
256
self.assertIs(None, tracker.get('yyy', None))
258
def test_check_bug_id_only_accepts_project_integers(self):
259
"""Accepts integers as bug IDs."""
260
tracker = bugtracker.ProjectIntegerBugTracker('xxx',
261
'http://bugs.example.com/{project}/{id}')
262
tracker.check_bug_id('project/1234')
264
def test_check_bug_id_doesnt_accept_non_project_integers(self):
265
"""Rejects non-integers as bug IDs."""
266
tracker = bugtracker.ProjectIntegerBugTracker('xxx',
267
'http://bugs.example.com/{project}/{id}')
269
bugtracker.MalformedBugIdentifier, tracker.check_bug_id, 'red')
271
bugtracker.MalformedBugIdentifier, tracker.check_bug_id, '1234')
274
class TestURLParametrizedBugTracker(TestCaseWithMemoryTransport):
275
"""Tests for URLParametrizedBugTracker."""
278
super(TestURLParametrizedBugTracker, self).setUp()
279
self.url = 'http://twistedmatrix.com/trac'
280
self.tracker = bugtracker.URLParametrizedBugTracker('some', 'ticket/')
282
def test_get_with_unsupported_tag(self):
283
"""If asked for an unrecognized or unconfigured tag, return None."""
284
branch = self.make_branch('some_branch')
285
self.assertEqual(None, self.tracker.get('lp', branch))
286
self.assertEqual(None, self.tracker.get('twisted', branch))
288
def test_get_with_supported_tag(self):
289
"""If asked for a valid tag, return a tracker instance that can map bug
290
IDs to <base_url>/<bug_area> + <bug_id>."""
291
bugtracker.tracker_registry.register('some', self.tracker)
292
self.addCleanup(bugtracker.tracker_registry.remove, 'some')
294
branch = self.make_branch('some_branch')
295
config = branch.get_config()
296
config.set_user_option('some_twisted_url', self.url)
297
tracker = self.tracker.get('twisted', branch)
299
urlutils.join(self.url, 'ticket/') + '1234',
300
tracker.get_bug_url('1234'))
302
def test_get_bug_url_for_integer_id(self):
303
self.tracker.check_bug_id('1234')
305
def test_get_bug_url_for_non_integer_id(self):
306
self.tracker.check_bug_id('ABC-1234')
309
class TestURLParametrizedIntegerBugTracker(TestCaseWithMemoryTransport):
310
"""Tests for URLParametrizedIntegerBugTracker."""
313
super(TestURLParametrizedIntegerBugTracker, self).setUp()
314
self.url = 'http://twistedmatrix.com/trac'
315
self.tracker = bugtracker.URLParametrizedIntegerBugTracker('some',
318
def test_get_bug_url_for_bad_bug(self):
319
"""When given a bug identifier that is invalid for Trac, get_bug_url
320
should raise an error.
323
bugtracker.MalformedBugIdentifier, self.tracker.get_bug_url, 'bad')
326
class TestPropertyEncoding(TestCase):
327
"""Tests for how the bug URLs are encoded as revision properties."""
329
def test_encoding_one(self):
331
'http://example.com/bugs/1 fixed',
332
bugtracker.encode_fixes_bug_urls(
333
[('http://example.com/bugs/1', 'fixed')]))
335
def test_encoding_zero(self):
336
self.assertEqual('', bugtracker.encode_fixes_bug_urls([]))
338
def test_encoding_two(self):
340
'http://example.com/bugs/1 fixed\n'
341
'http://example.com/bugs/2 related',
342
bugtracker.encode_fixes_bug_urls(
343
[('http://example.com/bugs/1', 'fixed'),
344
('http://example.com/bugs/2', 'related')]))
346
def test_encoding_with_space(self):
348
bugtracker.InvalidBugUrl,
349
bugtracker.encode_fixes_bug_urls,
350
[('http://example.com/bugs/ 1', 'fixed')])
353
class TestPropertyDecoding(TestCase):
354
"""Tests for parsing bug revision properties."""
356
def test_decoding_one(self):
358
[('http://example.com/bugs/1', 'fixed')],
359
list(bugtracker.decode_bug_urls(
360
'http://example.com/bugs/1 fixed')))
362
def test_decoding_zero(self):
363
self.assertEqual([], list(bugtracker.decode_bug_urls('')))
365
def test_decoding_two(self):
367
[('http://example.com/bugs/1', 'fixed'),
368
('http://example.com/bugs/2', 'related')],
369
list(bugtracker.decode_bug_urls(
370
'http://example.com/bugs/1 fixed\n'
371
'http://example.com/bugs/2 related')))
373
def test_decoding_invalid(self):
375
bugtracker.InvalidLineInBugsProperty,
377
bugtracker.decode_bug_urls('http://example.com/bugs/ 1 fixed\n'))