/brz/remove-bazaar

To get this branch, use:
bzr branch http://gegoxaren.bato24.eu/bzr/brz/remove-bazaar

« back to all changes in this revision

Viewing changes to breezy/tests/test_bugtracker.py

  • Committer: Jelmer Vernooij
  • Date: 2017-08-07 11:49:46 UTC
  • mto: (6747.3.4 avoid-set-revid-3)
  • mto: This revision was merged to the branch mainline in revision 6750.
  • Revision ID: jelmer@jelmer.uk-20170807114946-luclmxuawyzhpiot
Avoid setting revision_ids.

Show diffs side-by-side

added added

removed removed

Lines of Context:
 
1
# Copyright (C) 2007-2010 Canonical Ltd
 
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
 
 
18
from .. import bugtracker, urlutils
 
19
from . import TestCase, TestCaseWithMemoryTransport
 
20
 
 
21
 
 
22
class ErrorsTest(TestCaseWithMemoryTransport):
 
23
 
 
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)
 
28
        self.assertEqual(
 
29
            "Cannot find registered bug tracker called xxx on %s" % branch,
 
30
            str(error))
 
31
 
 
32
    def test_malformed_bug_identifier(self):
 
33
        """Test the formatting of MalformedBugIdentifier."""
 
34
        error = bugtracker.MalformedBugIdentifier(
 
35
            'bogus', 'reason for bogosity')
 
36
        self.assertEqual(
 
37
            'Did not understand bug identifier bogus: reason for bogosity. '
 
38
            'See "brz help bugs" for more information on this feature.',
 
39
            str(error))
 
40
 
 
41
    def test_incorrect_url(self):
 
42
        err = bugtracker.InvalidBugTrackerURL('foo', 'http://bug.com/')
 
43
        self.assertEqual(
 
44
            ("The URL for bug tracker \"foo\" doesn't contain {id}: "
 
45
             "http://bug.com/"),
 
46
            str(err))
 
47
 
 
48
 
 
49
class TestGetBugURL(TestCaseWithMemoryTransport):
 
50
    """Tests for bugtracker.get_bug_url"""
 
51
 
 
52
    class TransientTracker(object):
 
53
        """An transient tracker used for testing."""
 
54
 
 
55
        @classmethod
 
56
        def get(klass, abbreviation, branch):
 
57
            klass.log.append(('get', abbreviation, branch))
 
58
            if abbreviation != 'transient':
 
59
                return None
 
60
            return klass()
 
61
 
 
62
        def get_bug_url(self, bug_id):
 
63
            self.log.append(('get_bug_url', bug_id))
 
64
            return "http://bugs.com/%s" % bug_id
 
65
 
 
66
    def setUp(self):
 
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')
 
72
 
 
73
    def test_get_bug_url_for_transient_tracker(self):
 
74
        branch = self.make_branch('some_branch')
 
75
        self.assertEqual('http://bugs.com/1234',
 
76
                         bugtracker.get_bug_url('transient', branch, '1234'))
 
77
        self.assertEqual(
 
78
            [('get', 'transient', branch), ('get_bug_url', '1234')],
 
79
            self.tracker_type.log)
 
80
 
 
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)
 
87
 
 
88
 
 
89
class TestBuiltinTrackers(TestCaseWithMemoryTransport):
 
90
    """Test that the builtin trackers are registered and return sane URLs."""
 
91
 
 
92
    def test_launchpad_registered(self):
 
93
        """The Launchpad bug tracker should be registered by default and
 
94
        generate Launchpad bug page URLs.
 
95
        """
 
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'))
 
100
 
 
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.
 
104
        """
 
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'))
 
109
 
 
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'))
 
115
 
 
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.
 
119
        """
 
120
        branch = self.make_branch('some_branch')
 
121
        config = branch.get_config()
 
122
        config.set_user_option('trac_foo_url', 'http://bugs.com/trac')
 
123
        tracker = bugtracker.tracker_registry.get_tracker('foo', branch)
 
124
        self.assertEqual('http://bugs.com/trac/ticket/1234',
 
125
                         tracker.get_bug_url('1234'))
 
126
 
 
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
 
130
        present.
 
131
        """
 
132
        branch = self.make_branch('some_branch')
 
133
        config = branch.get_config()
 
134
        config.set_user_option('bugzilla_foo_url', 'http://bugs.com')
 
135
        tracker = bugtracker.tracker_registry.get_tracker('foo', branch)
 
136
        self.assertEqual('http://bugs.com/show_bug.cgi?id=1234',
 
137
                         tracker.get_bug_url('1234'))
 
138
 
 
139
    def test_generic_registered(self):
 
140
        branch = self.make_branch('some_branch')
 
141
        config = branch.get_config()
 
142
        config.set_user_option('bugtracker_foo_url', 'http://bugs.com/{id}/view.html')
 
143
        tracker = bugtracker.tracker_registry.get_tracker('foo', branch)
 
144
        self.assertEqual('http://bugs.com/1234/view.html',
 
145
                         tracker.get_bug_url('1234'))
 
146
 
 
147
    def test_generic_registered_non_integer(self):
 
148
        branch = self.make_branch('some_branch')
 
149
        config = branch.get_config()
 
150
        config.set_user_option('bugtracker_foo_url', 'http://bugs.com/{id}/view.html')
 
151
        tracker = bugtracker.tracker_registry.get_tracker('foo', branch)
 
152
        self.assertEqual('http://bugs.com/ABC-1234/view.html',
 
153
                         tracker.get_bug_url('ABC-1234'))
 
154
 
 
155
    def test_generic_incorrect_url(self):
 
156
        branch = self.make_branch('some_branch')
 
157
        config = branch.get_config()
 
158
        config.set_user_option('bugtracker_foo_url', 'http://bugs.com/view.html')
 
159
        tracker = bugtracker.tracker_registry.get_tracker('foo', branch)
 
160
        self.assertRaises(bugtracker.InvalidBugTrackerURL, tracker.get_bug_url,
 
161
                '1234')
 
162
 
 
163
 
 
164
class TestUniqueIntegerBugTracker(TestCaseWithMemoryTransport):
 
165
 
 
166
    def test_appends_id_to_base_url(self):
 
167
        """The URL of a bug is the base URL joined to the identifier."""
 
168
        tracker = bugtracker.UniqueIntegerBugTracker('xxx',
 
169
                'http://bugs.com/foo')
 
170
        self.assertEqual('http://bugs.com/foo1234', tracker.get_bug_url('1234'))
 
171
 
 
172
    def test_returns_tracker_if_abbreviation_matches(self):
 
173
        """The get() method should return an instance of the tracker if the
 
174
        given abbreviation matches the tracker's abbreviated name.
 
175
        """
 
176
        tracker = bugtracker.UniqueIntegerBugTracker('xxx',
 
177
                'http://bugs.com/')
 
178
        branch = self.make_branch('some_branch')
 
179
        self.assertIs(tracker, tracker.get('xxx', branch))
 
180
 
 
181
    def test_returns_none_if_abbreviation_doesnt_match(self):
 
182
        """The get() method should return None if the given abbreviated name
 
183
        doesn't match the tracker's abbreviation.
 
184
        """
 
185
        tracker = bugtracker.UniqueIntegerBugTracker('xxx',
 
186
                'http://bugs.com/')
 
187
        branch = self.make_branch('some_branch')
 
188
        self.assertIs(None, tracker.get('yyy', branch))
 
189
 
 
190
    def test_doesnt_consult_branch(self):
 
191
        """A UniqueIntegerBugTracker shouldn't consult the branch for tracker
 
192
        information.
 
193
        """
 
194
        tracker = bugtracker.UniqueIntegerBugTracker('xxx',
 
195
                'http://bugs.com/')
 
196
        self.assertIs(tracker, tracker.get('xxx', None))
 
197
        self.assertIs(None, tracker.get('yyy', None))
 
198
 
 
199
    def test_check_bug_id_only_accepts_integers(self):
 
200
        """A UniqueIntegerBugTracker accepts integers as bug IDs."""
 
201
        tracker = bugtracker.UniqueIntegerBugTracker('xxx',
 
202
                'http://bugs.com/')
 
203
        tracker.check_bug_id('1234')
 
204
 
 
205
    def test_check_bug_id_doesnt_accept_non_integers(self):
 
206
        """A UniqueIntegerBugTracker rejects non-integers as bug IDs."""
 
207
        tracker = bugtracker.UniqueIntegerBugTracker('xxx',
 
208
                'http://bugs.com/')
 
209
        self.assertRaises(
 
210
            bugtracker.MalformedBugIdentifier, tracker.check_bug_id, 'red')
 
211
 
 
212
class TestURLParametrizedBugTracker(TestCaseWithMemoryTransport):
 
213
    """Tests for URLParametrizedBugTracker."""
 
214
 
 
215
    def setUp(self):
 
216
        super(TestURLParametrizedBugTracker, self).setUp()
 
217
        self.url = 'http://twistedmatrix.com/trac'
 
218
        self.tracker = bugtracker.URLParametrizedBugTracker('some', 'ticket/')
 
219
 
 
220
    def test_get_with_unsupported_tag(self):
 
221
        """If asked for an unrecognized or unconfigured tag, return None."""
 
222
        branch = self.make_branch('some_branch')
 
223
        self.assertEqual(None, self.tracker.get('lp', branch))
 
224
        self.assertEqual(None, self.tracker.get('twisted', branch))
 
225
 
 
226
    def test_get_with_supported_tag(self):
 
227
        """If asked for a valid tag, return a tracker instance that can map bug
 
228
        IDs to <base_url>/<bug_area> + <bug_id>."""
 
229
        bugtracker.tracker_registry.register('some', self.tracker)
 
230
        self.addCleanup(bugtracker.tracker_registry.remove, 'some')
 
231
 
 
232
        branch = self.make_branch('some_branch')
 
233
        config = branch.get_config()
 
234
        config.set_user_option('some_twisted_url', self.url)
 
235
        tracker = self.tracker.get('twisted', branch)
 
236
        self.assertEqual(
 
237
            urlutils.join(self.url, 'ticket/') + '1234',
 
238
            tracker.get_bug_url('1234'))
 
239
 
 
240
    def test_get_bug_url_for_integer_id(self):
 
241
        self.tracker.check_bug_id('1234')
 
242
 
 
243
    def test_get_bug_url_for_non_integer_id(self):
 
244
        self.tracker.check_bug_id('ABC-1234')
 
245
 
 
246
 
 
247
class TestURLParametrizedIntegerBugTracker(TestCaseWithMemoryTransport):
 
248
    """Tests for URLParametrizedIntegerBugTracker."""
 
249
 
 
250
    def setUp(self):
 
251
        super(TestURLParametrizedIntegerBugTracker, self).setUp()
 
252
        self.url = 'http://twistedmatrix.com/trac'
 
253
        self.tracker = bugtracker.URLParametrizedIntegerBugTracker('some',
 
254
                                                                   'ticket/')
 
255
 
 
256
    def test_get_bug_url_for_bad_bug(self):
 
257
        """When given a bug identifier that is invalid for Trac, get_bug_url
 
258
        should raise an error.
 
259
        """
 
260
        self.assertRaises(
 
261
            bugtracker.MalformedBugIdentifier, self.tracker.get_bug_url, 'bad')
 
262
 
 
263
 
 
264
class TestPropertyEncoding(TestCase):
 
265
    """Tests for how the bug URLs are encoded as revision properties."""
 
266
 
 
267
    def test_encoding_one(self):
 
268
        self.assertEqual(
 
269
            'http://example.com/bugs/1 fixed',
 
270
            bugtracker.encode_fixes_bug_urls(['http://example.com/bugs/1']))
 
271
 
 
272
    def test_encoding_zero(self):
 
273
        self.assertEqual('', bugtracker.encode_fixes_bug_urls([]))
 
274
 
 
275
    def test_encoding_two(self):
 
276
        self.assertEqual(
 
277
            'http://example.com/bugs/1 fixed\n'
 
278
            'http://example.com/bugs/2 fixed',
 
279
            bugtracker.encode_fixes_bug_urls(
 
280
                ['http://example.com/bugs/1', 'http://example.com/bugs/2']))