~bzr-pqm/bzr/bzr.dev

« back to all changes in this revision

Viewing changes to bzrlib/tests/test_bugtracker.py

  • Committer: NamNguyen
  • Date: 2007-08-01 06:14:14 UTC
  • mto: (2789.1.1 ianc-integration)
  • mto: This revision was merged to the branch mainline in revision 2790.
  • Revision ID: namnguyen-20070801061414-u0tzrfgcz6z604lz
``Branch.hooks`` now supports ``pre_commit`` hook.

The hook's signature is

::

  hook(local, master, old_revno, old_revid, new_revno, new_revid,
       deleted_paths, added_paths, future_revision_tree)

``deleted_paths`` and ``added_paths`` are lists of paths. Renamed paths are
recorded in both ``deleted_paths`` and ``added_paths`` (i.e. deleted then
added).

``future_revision_tree`` is obtained from ``CommitBuilder.revision_tree``
to save hooks from getting it from the branch again.

For example, hooks can get a file:

::

  for path in added_paths:
      id = future_revision_tree.path2id(path)
      if future_revision_tree.kind(id) == 'file':
          file = future_revision_tree.get_file(id)
          ...

or export a tree and do ``make check`` or similar

::

  import bzrlib.export
  bzrlib.export.export(future_revision_tree, 'tmp_space')


If the commit is to be rejected, hooks should raise an ``Exception``.

Show diffs side-by-side

added added

removed removed

Lines of Context:
12
12
#
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., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
 
15
# Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307  USA
16
16
 
17
17
 
18
18
from bzrlib import bugtracker, errors, urlutils
19
 
from bzrlib.tests import TestCase, TestCaseWithMemoryTransport
 
19
from bzrlib.tests import TestCaseWithMemoryTransport
20
20
 
21
21
 
22
22
class TestGetBugURL(TestCaseWithMemoryTransport):
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(bugtracker.tracker_registry.remove, 'transient')
 
44
        self.addCleanup(lambda:
 
45
                        bugtracker.tracker_registry.remove('transient'))
45
46
 
46
47
    def test_get_bug_url_for_transient_tracker(self):
47
48
        branch = self.make_branch('some_branch')
80
81
        self.assertEqual('http://bugs.debian.org/1234',
81
82
                         tracker.get_bug_url('1234'))
82
83
 
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
84
    def test_trac_registered(self):
90
85
        """The Trac bug tracker should be registered by default and generate
91
86
        Trac bug page URLs when the appropriate configuration is present.
109
104
        self.assertEqual('http://bugs.com/show_bug.cgi?id=1234',
110
105
                         tracker.get_bug_url('1234'))
111
106
 
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
107
 
128
108
class TestUniqueIntegerBugTracker(TestCaseWithMemoryTransport):
129
109
 
130
 
    def test_appends_id_to_base_url(self):
 
110
    def test_joins_id_to_base_url(self):
131
111
        """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'))
 
112
        tracker = bugtracker.UniqueIntegerBugTracker('xxx', 'http://bugs.com')
 
113
        self.assertEqual('http://bugs.com/1234', tracker.get_bug_url('1234'))
135
114
 
136
115
    def test_returns_tracker_if_abbreviation_matches(self):
137
116
        """The get() method should return an instance of the tracker if the
138
117
        given abbreviation matches the tracker's abbreviated name.
139
118
        """
140
 
        tracker = bugtracker.UniqueIntegerBugTracker('xxx',
141
 
                'http://bugs.com/')
 
119
        tracker = bugtracker.UniqueIntegerBugTracker('xxx', 'http://bugs.com')
142
120
        branch = self.make_branch('some_branch')
143
121
        self.assertIs(tracker, tracker.get('xxx', branch))
144
122
 
146
124
        """The get() method should return None if the given abbreviated name
147
125
        doesn't match the tracker's abbreviation.
148
126
        """
149
 
        tracker = bugtracker.UniqueIntegerBugTracker('xxx',
150
 
                'http://bugs.com/')
 
127
        tracker = bugtracker.UniqueIntegerBugTracker('xxx', 'http://bugs.com')
151
128
        branch = self.make_branch('some_branch')
152
129
        self.assertIs(None, tracker.get('yyy', branch))
153
130
 
155
132
        """A UniqueIntegerBugTracker shouldn't consult the branch for tracker
156
133
        information.
157
134
        """
158
 
        tracker = bugtracker.UniqueIntegerBugTracker('xxx',
159
 
                'http://bugs.com/')
 
135
        tracker = bugtracker.UniqueIntegerBugTracker('xxx', 'http://bugs.com')
160
136
        self.assertIs(tracker, tracker.get('xxx', None))
161
137
        self.assertIs(None, tracker.get('yyy', None))
162
138
 
163
139
    def test_check_bug_id_only_accepts_integers(self):
164
140
        """A UniqueIntegerBugTracker accepts integers as bug IDs."""
165
 
        tracker = bugtracker.UniqueIntegerBugTracker('xxx',
166
 
                'http://bugs.com/')
 
141
        tracker = bugtracker.UniqueIntegerBugTracker('xxx', 'http://bugs.com')
167
142
        tracker.check_bug_id('1234')
168
143
 
169
144
    def test_check_bug_id_doesnt_accept_non_integers(self):
170
145
        """A UniqueIntegerBugTracker rejects non-integers as bug IDs."""
171
 
        tracker = bugtracker.UniqueIntegerBugTracker('xxx',
172
 
                'http://bugs.com/')
 
146
        tracker = bugtracker.UniqueIntegerBugTracker('xxx', 'http://bugs.com')
173
147
        self.assertRaises(
174
148
            errors.MalformedBugIdentifier, tracker.check_bug_id, 'red')
175
149
 
193
167
        """If asked for a valid tag, return a tracker instance that can map bug
194
168
        IDs to <base_url>/<bug_area> + <bug_id>."""
195
169
        bugtracker.tracker_registry.register('some', self.tracker)
196
 
        self.addCleanup(bugtracker.tracker_registry.remove, 'some')
 
170
        self.addCleanup(lambda: bugtracker.tracker_registry.remove('some'))
197
171
 
198
172
        branch = self.make_branch('some_branch')
199
173
        config = branch.get_config()
209
183
        """
210
184
        self.assertRaises(
211
185
            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']))