~launchpad-pqm/launchpad/devel

« back to all changes in this revision

Viewing changes to lib/lp/codehosting/tests/test_bzrutils.py

  • Committer: Launchpad Patch Queue Manager
  • Date: 2009-07-15 07:27:09 UTC
  • mfrom: (7675.240.21 lpserve-oops-bug-274578)
  • Revision ID: launchpad@pqm.canonical.com-20090715072709-1ebxcicjb3qcjp5l
[r=mwhudson][ui=none][bug=274578] Log exceptions that occur in bzr
        subprocesses as OOPSes.

Show diffs side-by-side

added added

removed removed

Lines of Context:
5
5
__metaclass__ = type
6
6
 
7
7
import gc
 
8
import sys
8
9
 
9
10
from bzrlib import errors
10
11
from bzrlib.branch import Branch
11
12
from bzrlib.bzrdir import format_registry
 
13
from bzrlib import trace
12
14
from bzrlib.tests import (
13
 
    multiply_tests, TestCaseWithTransport, TestLoader, TestNotApplicable)
 
15
    multiply_tests, TestCase, TestCaseWithTransport, TestLoader,
 
16
    TestNotApplicable)
14
17
try:
15
18
    from bzrlib.tests.per_branch import TestCaseWithBzrDir, branch_scenarios
16
19
except ImportError:
17
20
    from bzrlib.tests.branch_implementations import (
18
21
        TestCaseWithBzrDir, branch_scenarios)
19
22
from lp.codehosting.bzrutils import (
20
 
    DenyingServer, get_branch_stacked_on_url, is_branch_stackable)
 
23
    add_exception_logging_hook, DenyingServer, get_branch_stacked_on_url,
 
24
    is_branch_stackable, remove_exception_logging_hook)
21
25
from lp.codehosting.tests.helpers import TestResultWrapper
22
26
 
23
27
 
44
48
 
45
49
    def testGetBranchStackedOnUrl(self):
46
50
        # get_branch_stacked_on_url returns the URL of the stacked-on branch.
47
 
        stacked_on_branch = self.make_branch('stacked-on')
 
51
        self.make_branch('stacked-on')
48
52
        stacked_branch = self.make_branch('stacked')
49
53
        try:
50
54
            stacked_branch.set_stacked_on_url('../stacked-on')
130
134
        Branch.open(branch.base)
131
135
 
132
136
 
 
137
class TestExceptionLoggingHooks(TestCase):
 
138
 
 
139
    def logException(self, exception):
 
140
        """Log exception with Bazaar's exception logger."""
 
141
        try:
 
142
            raise exception
 
143
        except exception.__class__:
 
144
            trace.log_exception_quietly()
 
145
 
 
146
    def test_calls_hook_when_added(self):
 
147
        # add_exception_logging_hook adds a hook function that's called
 
148
        # whenever Bazaar logs an exception.
 
149
        exceptions = []
 
150
        def hook():
 
151
            exceptions.append(sys.exc_info()[:2])
 
152
        add_exception_logging_hook(hook)
 
153
        self.addCleanup(remove_exception_logging_hook, hook)
 
154
        exception = RuntimeError('foo')
 
155
        self.logException(exception)
 
156
        self.assertEqual([(RuntimeError, exception)], exceptions)
 
157
 
 
158
    def test_doesnt_call_hook_when_removed(self):
 
159
        # remove_exception_logging_hook removes the hook function, ensuring
 
160
        # it's not called when Bazaar logs an exception.
 
161
        exceptions = []
 
162
        def hook():
 
163
            exceptions.append(sys.exc_info()[:2])
 
164
        add_exception_logging_hook(hook)
 
165
        remove_exception_logging_hook(hook)
 
166
        self.logException(RuntimeError('foo'))
 
167
        self.assertEqual([], exceptions)
 
168
 
 
169
 
133
170
def load_tests(basic_tests, module, loader):
134
171
    """Parametrize the tests of get_branch_stacked_on_url by branch format."""
135
172
    result = loader.suiteClass()
142
179
 
143
180
    result.addTests(loader.loadTestsFromTestCase(TestIsBranchStackable))
144
181
    result.addTests(loader.loadTestsFromTestCase(TestDenyingServer))
 
182
    result.addTests(loader.loadTestsFromTestCase(TestExceptionLoggingHooks))
145
183
    return result
146
184
 
147
185