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., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
15
# Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
18
18
from bzrlib import bugtracker, errors, urlutils
19
from bzrlib.tests import TestCaseWithMemoryTransport
19
from bzrlib.tests import TestCase, TestCaseWithMemoryTransport
22
22
class TestGetBugURL(TestCaseWithMemoryTransport):
37
37
return "http://bugs.com/%s" % bug_id
40
TestCaseWithMemoryTransport.setUp(self)
40
super(TestGetBugURL, self).setUp()
41
41
self.tracker_type = TestGetBugURL.TransientTracker
42
42
self.tracker_type.log = []
43
43
bugtracker.tracker_registry.register('transient', self.tracker_type)
44
self.addCleanup(lambda:
45
bugtracker.tracker_registry.remove('transient'))
44
self.addCleanup(bugtracker.tracker_registry.remove, 'transient')
47
46
def test_get_bug_url_for_transient_tracker(self):
48
47
branch = self.make_branch('some_branch')
81
80
self.assertEqual('http://bugs.debian.org/1234',
82
81
tracker.get_bug_url('1234'))
83
def test_gnome_registered(self):
84
branch = self.make_branch('some_branch')
85
tracker = bugtracker.tracker_registry.get_tracker('gnome', branch)
86
self.assertEqual('http://bugzilla.gnome.org/show_bug.cgi?id=1234',
87
tracker.get_bug_url('1234'))
84
89
def test_trac_registered(self):
85
90
"""The Trac bug tracker should be registered by default and generate
86
91
Trac bug page URLs when the appropriate configuration is present.
104
109
self.assertEqual('http://bugs.com/show_bug.cgi?id=1234',
105
110
tracker.get_bug_url('1234'))
112
def test_generic_registered(self):
113
branch = self.make_branch('some_branch')
114
config = branch.get_config()
115
config.set_user_option('bugtracker_foo_url', 'http://bugs.com/{id}/view.html')
116
tracker = bugtracker.tracker_registry.get_tracker('foo', branch)
117
self.assertEqual('http://bugs.com/1234/view.html',
118
tracker.get_bug_url('1234'))
120
def test_generic_registered_non_integer(self):
121
branch = self.make_branch('some_branch')
122
config = branch.get_config()
123
config.set_user_option('bugtracker_foo_url', 'http://bugs.com/{id}/view.html')
124
tracker = bugtracker.tracker_registry.get_tracker('foo', branch)
125
self.assertEqual('http://bugs.com/ABC-1234/view.html',
126
tracker.get_bug_url('ABC-1234'))
128
def test_generic_incorrect_url(self):
129
branch = self.make_branch('some_branch')
130
config = branch.get_config()
131
config.set_user_option('bugtracker_foo_url', 'http://bugs.com/view.html')
132
tracker = bugtracker.tracker_registry.get_tracker('foo', branch)
133
self.assertRaises(errors.InvalidBugTrackerURL, tracker.get_bug_url, '1234')
108
136
class TestUniqueIntegerBugTracker(TestCaseWithMemoryTransport):
110
def test_joins_id_to_base_url(self):
138
def test_appends_id_to_base_url(self):
111
139
"""The URL of a bug is the base URL joined to the identifier."""
112
tracker = bugtracker.UniqueIntegerBugTracker('xxx', 'http://bugs.com')
113
self.assertEqual('http://bugs.com/1234', tracker.get_bug_url('1234'))
140
tracker = bugtracker.UniqueIntegerBugTracker('xxx',
141
'http://bugs.com/foo')
142
self.assertEqual('http://bugs.com/foo1234', tracker.get_bug_url('1234'))
115
144
def test_returns_tracker_if_abbreviation_matches(self):
116
145
"""The get() method should return an instance of the tracker if the
117
146
given abbreviation matches the tracker's abbreviated name.
119
tracker = bugtracker.UniqueIntegerBugTracker('xxx', 'http://bugs.com')
148
tracker = bugtracker.UniqueIntegerBugTracker('xxx',
120
150
branch = self.make_branch('some_branch')
121
151
self.assertIs(tracker, tracker.get('xxx', branch))
124
154
"""The get() method should return None if the given abbreviated name
125
155
doesn't match the tracker's abbreviation.
127
tracker = bugtracker.UniqueIntegerBugTracker('xxx', 'http://bugs.com')
157
tracker = bugtracker.UniqueIntegerBugTracker('xxx',
128
159
branch = self.make_branch('some_branch')
129
160
self.assertIs(None, tracker.get('yyy', branch))
132
163
"""A UniqueIntegerBugTracker shouldn't consult the branch for tracker
135
tracker = bugtracker.UniqueIntegerBugTracker('xxx', 'http://bugs.com')
166
tracker = bugtracker.UniqueIntegerBugTracker('xxx',
136
168
self.assertIs(tracker, tracker.get('xxx', None))
137
169
self.assertIs(None, tracker.get('yyy', None))
139
171
def test_check_bug_id_only_accepts_integers(self):
140
172
"""A UniqueIntegerBugTracker accepts integers as bug IDs."""
141
tracker = bugtracker.UniqueIntegerBugTracker('xxx', 'http://bugs.com')
173
tracker = bugtracker.UniqueIntegerBugTracker('xxx',
142
175
tracker.check_bug_id('1234')
144
177
def test_check_bug_id_doesnt_accept_non_integers(self):
145
178
"""A UniqueIntegerBugTracker rejects non-integers as bug IDs."""
146
tracker = bugtracker.UniqueIntegerBugTracker('xxx', 'http://bugs.com')
179
tracker = bugtracker.UniqueIntegerBugTracker('xxx',
147
181
self.assertRaises(
148
182
errors.MalformedBugIdentifier, tracker.check_bug_id, 'red')
151
class TestURLParametrizedIntegerBugTracker(TestCaseWithMemoryTransport):
152
"""Tests for TracTracker."""
184
class TestURLParametrizedBugTracker(TestCaseWithMemoryTransport):
185
"""Tests for URLParametrizedBugTracker."""
155
TestCaseWithMemoryTransport.setUp(self)
188
super(TestURLParametrizedBugTracker, self).setUp()
156
189
self.url = 'http://twistedmatrix.com/trac'
157
self.tracker = bugtracker.URLParametrizedIntegerBugTracker('some',
190
self.tracker = bugtracker.URLParametrizedBugTracker('some', 'ticket/')
160
192
def test_get_with_unsupported_tag(self):
161
193
"""If asked for an unrecognized or unconfigured tag, return None."""
167
199
"""If asked for a valid tag, return a tracker instance that can map bug
168
200
IDs to <base_url>/<bug_area> + <bug_id>."""
169
201
bugtracker.tracker_registry.register('some', self.tracker)
170
self.addCleanup(lambda: bugtracker.tracker_registry.remove('some'))
202
self.addCleanup(bugtracker.tracker_registry.remove, 'some')
172
204
branch = self.make_branch('some_branch')
173
205
config = branch.get_config()
177
209
urlutils.join(self.url, 'ticket/') + '1234',
178
210
tracker.get_bug_url('1234'))
212
def test_get_bug_url_for_integer_id(self):
213
self.tracker.check_bug_id('1234')
215
def test_get_bug_url_for_non_integer_id(self):
216
self.tracker.check_bug_id('ABC-1234')
219
class TestURLParametrizedIntegerBugTracker(TestCaseWithMemoryTransport):
220
"""Tests for URLParametrizedIntegerBugTracker."""
223
super(TestURLParametrizedIntegerBugTracker, self).setUp()
224
self.url = 'http://twistedmatrix.com/trac'
225
self.tracker = bugtracker.URLParametrizedIntegerBugTracker('some',
180
228
def test_get_bug_url_for_bad_bug(self):
181
229
"""When given a bug identifier that is invalid for Trac, get_bug_url
182
230
should raise an error.
184
232
self.assertRaises(
185
233
errors.MalformedBugIdentifier, self.tracker.get_bug_url, 'bad')
236
class TestPropertyEncoding(TestCase):
237
"""Tests for how the bug URLs are encoded as revision properties."""
239
def test_encoding_one(self):
241
'http://example.com/bugs/1 fixed',
242
bugtracker.encode_fixes_bug_urls(['http://example.com/bugs/1']))
244
def test_encoding_zero(self):
245
self.assertEqual('', bugtracker.encode_fixes_bug_urls([]))
247
def test_encoding_two(self):
249
'http://example.com/bugs/1 fixed\n'
250
'http://example.com/bugs/2 fixed',
251
bugtracker.encode_fixes_bug_urls(
252
['http://example.com/bugs/1', 'http://example.com/bugs/2']))