~bzr-pqm/bzr/bzr.dev

« back to all changes in this revision

Viewing changes to bzrlib/tests/test_bugtracker.py

  • Committer: Martin Pool
  • Date: 2005-08-24 08:59:32 UTC
  • Revision ID: mbp@sourcefrog.net-20050824085932-c61f1f1f1c930e13
- Add a simple UIFactory 

  The idea of this is to let a client of bzrlib set some 
  policy about how output is displayed.

  In this revision all that's done is that progress bars
  are constructed by a policy established by the application
  rather than being randomly constructed in the library 
  or passed down the calls.  This avoids progress bars
  popping up while running the test suite and cleans up
  some code.

Show diffs side-by-side

added added

removed removed

Lines of Context:
1
 
# Copyright (C) 2007 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 bzrlib import bugtracker, errors, urlutils
19
 
from bzrlib.tests import TestCase, TestCaseWithMemoryTransport
20
 
 
21
 
 
22
 
class TestGetBugURL(TestCaseWithMemoryTransport):
23
 
    """Tests for bugtracker.get_bug_url"""
24
 
 
25
 
    class TransientTracker(object):
26
 
        """An transient tracker used for testing."""
27
 
 
28
 
        @classmethod
29
 
        def get(klass, abbreviation, branch):
30
 
            klass.log.append(('get', abbreviation, branch))
31
 
            if abbreviation != 'transient':
32
 
                return None
33
 
            return klass()
34
 
 
35
 
        def get_bug_url(self, bug_id):
36
 
            self.log.append(('get_bug_url', bug_id))
37
 
            return "http://bugs.com/%s" % bug_id
38
 
 
39
 
    def setUp(self):
40
 
        TestCaseWithMemoryTransport.setUp(self)
41
 
        self.tracker_type = TestGetBugURL.TransientTracker
42
 
        self.tracker_type.log = []
43
 
        bugtracker.tracker_registry.register('transient', self.tracker_type)
44
 
        self.addCleanup(bugtracker.tracker_registry.remove, 'transient')
45
 
 
46
 
    def test_get_bug_url_for_transient_tracker(self):
47
 
        branch = self.make_branch('some_branch')
48
 
        self.assertEqual('http://bugs.com/1234',
49
 
                         bugtracker.get_bug_url('transient', branch, '1234'))
50
 
        self.assertEqual(
51
 
            [('get', 'transient', branch), ('get_bug_url', '1234')],
52
 
            self.tracker_type.log)
53
 
 
54
 
    def test_unrecognized_abbreviation_raises_error(self):
55
 
        """If the abbreviation is unrecognized, then raise an error."""
56
 
        branch = self.make_branch('some_branch')
57
 
        self.assertRaises(errors.UnknownBugTrackerAbbreviation,
58
 
                          bugtracker.get_bug_url, 'xxx', branch, '1234')
59
 
        self.assertEqual([('get', 'xxx', branch)], self.tracker_type.log)
60
 
 
61
 
 
62
 
class TestBuiltinTrackers(TestCaseWithMemoryTransport):
63
 
    """Test that the builtin trackers are registered and return sane URLs."""
64
 
 
65
 
    def test_launchpad_registered(self):
66
 
        """The Launchpad bug tracker should be registered by default and
67
 
        generate Launchpad bug page URLs.
68
 
        """
69
 
        branch = self.make_branch('some_branch')
70
 
        tracker = bugtracker.tracker_registry.get_tracker('lp', branch)
71
 
        self.assertEqual('https://launchpad.net/bugs/1234',
72
 
                         tracker.get_bug_url('1234'))
73
 
 
74
 
    def test_debian_registered(self):
75
 
        """The Debian bug tracker should be registered by default and generate
76
 
        bugs.debian.org bug page URLs.
77
 
        """
78
 
        branch = self.make_branch('some_branch')
79
 
        tracker = bugtracker.tracker_registry.get_tracker('deb', branch)
80
 
        self.assertEqual('http://bugs.debian.org/1234',
81
 
                         tracker.get_bug_url('1234'))
82
 
 
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'))
88
 
 
89
 
    def test_trac_registered(self):
90
 
        """The Trac bug tracker should be registered by default and generate
91
 
        Trac bug page URLs when the appropriate configuration is present.
92
 
        """
93
 
        branch = self.make_branch('some_branch')
94
 
        config = branch.get_config()
95
 
        config.set_user_option('trac_foo_url', 'http://bugs.com/trac')
96
 
        tracker = bugtracker.tracker_registry.get_tracker('foo', branch)
97
 
        self.assertEqual('http://bugs.com/trac/ticket/1234',
98
 
                         tracker.get_bug_url('1234'))
99
 
 
100
 
    def test_bugzilla_registered(self):
101
 
        """The Bugzilla bug tracker should be registered by default and
102
 
        generate Bugzilla bug page URLs when the appropriate configuration is
103
 
        present.
104
 
        """
105
 
        branch = self.make_branch('some_branch')
106
 
        config = branch.get_config()
107
 
        config.set_user_option('bugzilla_foo_url', 'http://bugs.com')
108
 
        tracker = bugtracker.tracker_registry.get_tracker('foo', branch)
109
 
        self.assertEqual('http://bugs.com/show_bug.cgi?id=1234',
110
 
                         tracker.get_bug_url('1234'))
111
 
 
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'))
119
 
 
120
 
    def test_generic_incorrect_url(self):
121
 
        branch = self.make_branch('some_branch')
122
 
        config = branch.get_config()
123
 
        config.set_user_option('bugtracker_foo_url', 'http://bugs.com/view.html')
124
 
        tracker = bugtracker.tracker_registry.get_tracker('foo', branch)
125
 
        self.assertRaises(errors.InvalidBugTrackerURL, tracker.get_bug_url, '1234')
126
 
 
127
 
 
128
 
class TestUniqueIntegerBugTracker(TestCaseWithMemoryTransport):
129
 
 
130
 
    def test_appends_id_to_base_url(self):
131
 
        """The URL of a bug is the base URL joined to the identifier."""
132
 
        tracker = bugtracker.UniqueIntegerBugTracker('xxx',
133
 
                'http://bugs.com/foo')
134
 
        self.assertEqual('http://bugs.com/foo1234', tracker.get_bug_url('1234'))
135
 
 
136
 
    def test_returns_tracker_if_abbreviation_matches(self):
137
 
        """The get() method should return an instance of the tracker if the
138
 
        given abbreviation matches the tracker's abbreviated name.
139
 
        """
140
 
        tracker = bugtracker.UniqueIntegerBugTracker('xxx',
141
 
                'http://bugs.com/')
142
 
        branch = self.make_branch('some_branch')
143
 
        self.assertIs(tracker, tracker.get('xxx', branch))
144
 
 
145
 
    def test_returns_none_if_abbreviation_doesnt_match(self):
146
 
        """The get() method should return None if the given abbreviated name
147
 
        doesn't match the tracker's abbreviation.
148
 
        """
149
 
        tracker = bugtracker.UniqueIntegerBugTracker('xxx',
150
 
                'http://bugs.com/')
151
 
        branch = self.make_branch('some_branch')
152
 
        self.assertIs(None, tracker.get('yyy', branch))
153
 
 
154
 
    def test_doesnt_consult_branch(self):
155
 
        """A UniqueIntegerBugTracker shouldn't consult the branch for tracker
156
 
        information.
157
 
        """
158
 
        tracker = bugtracker.UniqueIntegerBugTracker('xxx',
159
 
                'http://bugs.com/')
160
 
        self.assertIs(tracker, tracker.get('xxx', None))
161
 
        self.assertIs(None, tracker.get('yyy', None))
162
 
 
163
 
    def test_check_bug_id_only_accepts_integers(self):
164
 
        """A UniqueIntegerBugTracker accepts integers as bug IDs."""
165
 
        tracker = bugtracker.UniqueIntegerBugTracker('xxx',
166
 
                'http://bugs.com/')
167
 
        tracker.check_bug_id('1234')
168
 
 
169
 
    def test_check_bug_id_doesnt_accept_non_integers(self):
170
 
        """A UniqueIntegerBugTracker rejects non-integers as bug IDs."""
171
 
        tracker = bugtracker.UniqueIntegerBugTracker('xxx',
172
 
                'http://bugs.com/')
173
 
        self.assertRaises(
174
 
            errors.MalformedBugIdentifier, tracker.check_bug_id, 'red')
175
 
 
176
 
 
177
 
class TestURLParametrizedIntegerBugTracker(TestCaseWithMemoryTransport):
178
 
    """Tests for TracTracker."""
179
 
 
180
 
    def setUp(self):
181
 
        TestCaseWithMemoryTransport.setUp(self)
182
 
        self.url = 'http://twistedmatrix.com/trac'
183
 
        self.tracker = bugtracker.URLParametrizedIntegerBugTracker('some',
184
 
                                                                   'ticket/')
185
 
 
186
 
    def test_get_with_unsupported_tag(self):
187
 
        """If asked for an unrecognized or unconfigured tag, return None."""
188
 
        branch = self.make_branch('some_branch')
189
 
        self.assertEqual(None, self.tracker.get('lp', branch))
190
 
        self.assertEqual(None, self.tracker.get('twisted', branch))
191
 
 
192
 
    def test_get_with_supported_tag(self):
193
 
        """If asked for a valid tag, return a tracker instance that can map bug
194
 
        IDs to <base_url>/<bug_area> + <bug_id>."""
195
 
        bugtracker.tracker_registry.register('some', self.tracker)
196
 
        self.addCleanup(bugtracker.tracker_registry.remove, 'some')
197
 
 
198
 
        branch = self.make_branch('some_branch')
199
 
        config = branch.get_config()
200
 
        config.set_user_option('some_twisted_url', self.url)
201
 
        tracker = self.tracker.get('twisted', branch)
202
 
        self.assertEqual(
203
 
            urlutils.join(self.url, 'ticket/') + '1234',
204
 
            tracker.get_bug_url('1234'))
205
 
 
206
 
    def test_get_bug_url_for_bad_bug(self):
207
 
        """When given a bug identifier that is invalid for Trac, get_bug_url
208
 
        should raise an error.
209
 
        """
210
 
        self.assertRaises(
211
 
            errors.MalformedBugIdentifier, self.tracker.get_bug_url, 'bad')
212
 
 
213
 
 
214
 
class TestPropertyEncoding(TestCase):
215
 
    """Tests for how the bug URLs are encoded as revision properties."""
216
 
 
217
 
    def test_encoding_one(self):
218
 
        self.assertEqual(
219
 
            'http://example.com/bugs/1 fixed',
220
 
            bugtracker.encode_fixes_bug_urls(['http://example.com/bugs/1']))
221
 
 
222
 
    def test_encoding_zero(self):
223
 
        self.assertEqual('', bugtracker.encode_fixes_bug_urls([]))
224
 
 
225
 
    def test_encoding_two(self):
226
 
        self.assertEqual(
227
 
            'http://example.com/bugs/1 fixed\n'
228
 
            'http://example.com/bugs/2 fixed',
229
 
            bugtracker.encode_fixes_bug_urls(
230
 
                ['http://example.com/bugs/1', 'http://example.com/bugs/2']))