~launchpad-pqm/launchpad/devel

14291.1.2 by Jeroen Vermeulen
Lint.
1
# Copyright 2009-2011 Canonical Ltd.  This software is licensed under the
8687.15.17 by Karl Fogel
Add the copyright header block to the rest of the files under lib/lp/.
2
# GNU Affero General Public License version 3 (see the file LICENSE).
6876.11.6 by Jonathan Lange
Add a test_bzrutils module that's parametrized by BzrDir.
3
4
"""Tests for bzrutils."""
5
6
__metaclass__ = type
7
7033.1.1 by Michael Hudson
fix testGetBranchStackedOnUrl with remote branches
8
import gc
7675.240.15 by Jonathan Lange
Add our own APIs for hooking into Bazaar's exception logger.
9
import sys
7033.1.1 by Michael Hudson
fix testGetBranchStackedOnUrl with remote branches
10
11403.1.4 by Henning Eggers
Reformatted imports using format-imports script r32.
11
from bzrlib import (
12
    errors,
13
    trace,
14
    )
14291.1.2 by Jeroen Vermeulen
Lint.
15
from bzrlib.branch import Branch
16
from bzrlib.bzrdir import format_registry
9478.1.4 by Tim Penhey
Move identical_formats and get_vfs_format_classes into bzrutils.
17
from bzrlib.remote import RemoteBranch
6876.11.10 by Jonathan Lange
OK, now the tests pass, because we have the result wrapper.
18
from bzrlib.tests import (
11403.1.4 by Henning Eggers
Reformatted imports using format-imports script r32.
19
    multiply_tests,
20
    test_server,
21
    TestCase,
22
    TestCaseWithTransport,
13677.3.5 by Steve Kowalik
Revert three more failing test files.
23
    TestLoader,
11403.1.4 by Henning Eggers
Reformatted imports using format-imports script r32.
24
    TestNotApplicable,
25
    )
26
from bzrlib.tests.per_branch import (
27
    branch_scenarios,
13081.2.3 by Jelmer Vernooij
Update imports for newer version of bzr.
28
    TestCaseWithControlDir,
11403.1.4 by Henning Eggers
Reformatted imports using format-imports script r32.
29
    )
9478.1.4 by Tim Penhey
Move identical_formats and get_vfs_format_classes into bzrutils.
30
8426.6.1 by Michael Hudson
bzr ls --versioned --recursive --kind=file | xargs sed -i -e 's,from canonical.codehosting,from lp.codehosting,'
31
from lp.codehosting.bzrutils import (
11403.1.4 by Henning Eggers
Reformatted imports using format-imports script r32.
32
    add_exception_logging_hook,
33
    DenyingServer,
34
    get_branch_stacked_on_url,
35
    get_vfs_format_classes,
36
    is_branch_stackable,
37
    remove_exception_logging_hook,
38
    )
8426.6.1 by Michael Hudson
bzr ls --versioned --recursive --kind=file | xargs sed -i -e 's,from canonical.codehosting,from lp.codehosting,'
39
from lp.codehosting.tests.helpers import TestResultWrapper
6876.11.7 by Jonathan Lange
Add get_branch_stacked_on_url and tests for same. The tests currently
40
6876.11.6 by Jonathan Lange
Add a test_bzrutils module that's parametrized by BzrDir.
41
13081.2.3 by Jelmer Vernooij
Update imports for newer version of bzr.
42
class TestGetBranchStackedOnURL(TestCaseWithControlDir):
6876.11.7 by Jonathan Lange
Add get_branch_stacked_on_url and tests for same. The tests currently
43
    """Tests for get_branch_stacked_on_url()."""
6876.11.6 by Jonathan Lange
Add a test_bzrutils module that's parametrized by BzrDir.
44
45
    def __str__(self):
46
        """Return the test id so that Zope test output shows the format."""
47
        return self.id()
48
7033.1.1 by Michael Hudson
fix testGetBranchStackedOnUrl with remote branches
49
    def tearDown(self):
50
        # This makes sure the connections held by the branches opened in the
51
        # test are dropped, so the daemon threads serving those branches can
52
        # exit.
53
        gc.collect()
13081.2.3 by Jelmer Vernooij
Update imports for newer version of bzr.
54
        TestCaseWithControlDir.tearDown(self)
7033.1.1 by Michael Hudson
fix testGetBranchStackedOnUrl with remote branches
55
6876.11.10 by Jonathan Lange
OK, now the tests pass, because we have the result wrapper.
56
    def run(self, result=None):
57
        """Run the test, with the result wrapped so that it knows about skips.
58
        """
59
        if result is None:
60
            result = self.defaultTestResult()
61
        super(TestGetBranchStackedOnURL, self).run(TestResultWrapper(result))
62
6876.11.7 by Jonathan Lange
Add get_branch_stacked_on_url and tests for same. The tests currently
63
    def testGetBranchStackedOnUrl(self):
64
        # get_branch_stacked_on_url returns the URL of the stacked-on branch.
7675.240.15 by Jonathan Lange
Add our own APIs for hooking into Bazaar's exception logger.
65
        self.make_branch('stacked-on')
6876.11.7 by Jonathan Lange
Add get_branch_stacked_on_url and tests for same. The tests currently
66
        stacked_branch = self.make_branch('stacked')
67
        try:
68
            stacked_branch.set_stacked_on_url('../stacked-on')
69
        except errors.UnstackableBranchFormat:
6876.11.10 by Jonathan Lange
OK, now the tests pass, because we have the result wrapper.
70
            raise TestNotApplicable('This format does not support stacking.')
6876.11.13 by Jonathan Lange
Explain why we delete the branch.
71
        # Deleting the stacked-on branch ensures that Bazaar will raise an
72
        # error if it tries to open the stacked-on branch.
6876.11.7 by Jonathan Lange
Add get_branch_stacked_on_url and tests for same. The tests currently
73
        self.get_transport('.').delete_tree('stacked-on')
74
        self.assertEqual(
75
            '../stacked-on',
76
            get_branch_stacked_on_url(stacked_branch.bzrdir))
77
78
    def testGetBranchStackedOnUrlUnstackable(self):
79
        # get_branch_stacked_on_url raises UnstackableBranchFormat if it's
80
        # called on the bzrdir of a branch that cannot be stacked.
81
        branch = self.make_branch('source')
82
        try:
83
            branch.get_stacked_on_url()
84
        except errors.NotStacked:
6876.11.10 by Jonathan Lange
OK, now the tests pass, because we have the result wrapper.
85
            raise TestNotApplicable('This format supports stacked branches.')
6876.11.7 by Jonathan Lange
Add get_branch_stacked_on_url and tests for same. The tests currently
86
        except errors.UnstackableBranchFormat:
87
            pass
88
        self.assertRaises(
89
            errors.UnstackableBranchFormat,
90
            get_branch_stacked_on_url, branch.bzrdir)
91
92
    def testGetBranchStackedOnUrlNotStacked(self):
93
        # get_branch_stacked_on_url raises NotStacked if it's called on the
94
        # bzrdir of a non-stacked branch.
95
        branch = self.make_branch('source')
96
        try:
97
            branch.get_stacked_on_url()
98
        except errors.NotStacked:
99
            pass
100
        except errors.UnstackableBranchFormat:
6876.11.10 by Jonathan Lange
OK, now the tests pass, because we have the result wrapper.
101
            raise TestNotApplicable(
102
                'This format does not support stacked branches')
6876.11.7 by Jonathan Lange
Add get_branch_stacked_on_url and tests for same. The tests currently
103
        self.assertRaises(
104
            errors.NotStacked, get_branch_stacked_on_url, branch.bzrdir)
105
106
    def testGetBranchStackedOnUrlNoBranch(self):
107
        # get_branch_stacked_on_url raises a NotBranchError if it's called on
108
        # a bzrdir that's not got a branch.
109
        a_bzrdir = self.make_bzrdir('source')
110
        if a_bzrdir.has_branch():
6876.11.10 by Jonathan Lange
OK, now the tests pass, because we have the result wrapper.
111
            raise TestNotApplicable(
112
                'This format does not support branchless bzrdirs.')
6876.11.7 by Jonathan Lange
Add get_branch_stacked_on_url and tests for same. The tests currently
113
        self.assertRaises(
114
            errors.NotBranchError, get_branch_stacked_on_url, a_bzrdir)
6876.11.6 by Jonathan Lange
Add a test_bzrutils module that's parametrized by BzrDir.
115
116
7675.122.7 by Tim Penhey
Move is_branch_stackable to codehosting/bzrutils.
117
class TestIsBranchStackable(TestCaseWithTransport):
118
    """Tests for is_branch_stackable."""
119
120
    def test_packs_unstackable(self):
121
        # The original packs are unstackable.
122
        branch = self.make_branch(
123
            'branch', format=format_registry.get("pack-0.92")())
124
        self.assertFalse(is_branch_stackable(branch))
125
126
    def test_1_9_stackable(self):
127
        # The original packs are unstackable.
128
        branch = self.make_branch(
129
            'branch', format=format_registry.get("1.9")())
130
        self.assertTrue(is_branch_stackable(branch))
131
132
7404.1.17 by Michael Hudson
move DenyingServer into bzrutils, add tests for it.
133
class TestDenyingServer(TestCaseWithTransport):
134
    """Tests for `DenyingServer`."""
135
136
    def test_denyingServer(self):
137
        # DenyingServer prevents creations of transports for the given URL
138
        # schemes between setUp() and tearDown().
139
        branch = self.make_branch('branch')
140
        self.assertTrue(
141
            branch.base.startswith('file://'),
142
            "make_branch() didn't make branch with file:// URL")
143
        file_denier = DenyingServer(['file://'])
10197.5.15 by Michael Hudson
it never ends
144
        file_denier.start_server()
7404.1.17 by Michael Hudson
move DenyingServer into bzrutils, add tests for it.
145
        self.assertRaises(AssertionError, Branch.open, branch.base)
10197.5.15 by Michael Hudson
it never ends
146
        file_denier.stop_server()
7404.1.17 by Michael Hudson
move DenyingServer into bzrutils, add tests for it.
147
        # This is just "assertNotRaises":
148
        Branch.open(branch.base)
149
150
7675.240.15 by Jonathan Lange
Add our own APIs for hooking into Bazaar's exception logger.
151
class TestExceptionLoggingHooks(TestCase):
152
153
    def logException(self, exception):
154
        """Log exception with Bazaar's exception logger."""
155
        try:
156
            raise exception
157
        except exception.__class__:
158
            trace.log_exception_quietly()
159
160
    def test_calls_hook_when_added(self):
161
        # add_exception_logging_hook adds a hook function that's called
162
        # whenever Bazaar logs an exception.
163
        exceptions = []
14291.1.2 by Jeroen Vermeulen
Lint.
164
7675.240.15 by Jonathan Lange
Add our own APIs for hooking into Bazaar's exception logger.
165
        def hook():
166
            exceptions.append(sys.exc_info()[:2])
14291.1.2 by Jeroen Vermeulen
Lint.
167
7675.240.15 by Jonathan Lange
Add our own APIs for hooking into Bazaar's exception logger.
168
        add_exception_logging_hook(hook)
169
        self.addCleanup(remove_exception_logging_hook, hook)
170
        exception = RuntimeError('foo')
171
        self.logException(exception)
172
        self.assertEqual([(RuntimeError, exception)], exceptions)
173
174
    def test_doesnt_call_hook_when_removed(self):
175
        # remove_exception_logging_hook removes the hook function, ensuring
176
        # it's not called when Bazaar logs an exception.
177
        exceptions = []
14291.1.2 by Jeroen Vermeulen
Lint.
178
7675.240.15 by Jonathan Lange
Add our own APIs for hooking into Bazaar's exception logger.
179
        def hook():
180
            exceptions.append(sys.exc_info()[:2])
14291.1.2 by Jeroen Vermeulen
Lint.
181
7675.240.15 by Jonathan Lange
Add our own APIs for hooking into Bazaar's exception logger.
182
        add_exception_logging_hook(hook)
183
        remove_exception_logging_hook(hook)
184
        self.logException(RuntimeError('foo'))
185
        self.assertEqual([], exceptions)
186
187
9478.1.4 by Tim Penhey
Move identical_formats and get_vfs_format_classes into bzrutils.
188
class TestGetVfsFormatClasses(TestCaseWithTransport):
189
    """Tests for `lp.codehosting.bzrutils.get_vfs_format_classes`.
190
    """
191
9920.1.4 by Michael Hudson
more of the same
192
    def setUp(self):
14104.6.47 by Robert Collins
Fix some cases of manual diamond inheritance - use super().
193
        super(TestGetVfsFormatClasses, self).setUp()
9920.1.4 by Michael Hudson
more of the same
194
        self.disable_directory_isolation()
9478.1.4 by Tim Penhey
Move identical_formats and get_vfs_format_classes into bzrutils.
195
        # This makes sure the connections held by the branches opened in the
196
        # test are dropped, so the daemon threads serving those branches can
197
        # exit.
14104.6.50 by Robert Collins
Hate typos.
198
        self.addCleanup(gc.collect)
9478.1.4 by Tim Penhey
Move identical_formats and get_vfs_format_classes into bzrutils.
199
200
    def test_get_vfs_format_classes(self):
201
        # get_vfs_format_classes for a returns the underlying format classes
202
        # of the branch, repo and bzrdir, even if the branch is a
203
        # RemoteBranch.
204
        vfs_branch = self.make_branch('.')
11040.1.3 by Aaron Bentley
Updates for bzr 2.2
205
        smart_server = test_server.SmartTCPServer_for_testing()
10197.5.15 by Michael Hudson
it never ends
206
        smart_server.start_server(self.get_vfs_only_server())
207
        self.addCleanup(smart_server.stop_server)
9478.1.4 by Tim Penhey
Move identical_formats and get_vfs_format_classes into bzrutils.
208
        remote_branch = Branch.open(smart_server.get_url())
209
        # Check that our set up worked: remote_branch is Remote and
210
        # source_branch is not.
211
        self.assertIsInstance(remote_branch, RemoteBranch)
212
        self.failIf(isinstance(vfs_branch, RemoteBranch))
213
        # Now, get_vfs_format_classes on both branches returns the same format
214
        # information.
215
        self.assertEqual(
216
            get_vfs_format_classes(vfs_branch),
217
            get_vfs_format_classes(remote_branch))
218
219
6876.11.6 by Jonathan Lange
Add a test_bzrutils module that's parametrized by BzrDir.
220
def load_tests(basic_tests, module, loader):
7925.2.7 by Michael Hudson
minor tweaks
221
    """Parametrize the tests of get_branch_stacked_on_url by branch format."""
6876.11.6 by Jonathan Lange
Add a test_bzrutils module that's parametrized by BzrDir.
222
    result = loader.suiteClass()
223
7404.1.17 by Michael Hudson
move DenyingServer into bzrutils, add tests for it.
224
    get_branch_stacked_on_url_tests = loader.loadTestsFromTestCase(
225
        TestGetBranchStackedOnURL)
7675.122.7 by Tim Penhey
Move is_branch_stackable to codehosting/bzrutils.
226
    scenarios = [scenario for scenario in branch_scenarios()
14517.1.3 by Jelmer Vernooij
Re-merge always loading of foreign plugins.
227
                 if scenario[0] not in (
228
                     'BranchReferenceFormat', 'GitBranchFormat',
229
                     'HgBranchFormat', 'SvnBranchFormat')]
7675.122.7 by Tim Penhey
Move is_branch_stackable to codehosting/bzrutils.
230
    multiply_tests(get_branch_stacked_on_url_tests, scenarios, result)
231
232
    result.addTests(loader.loadTestsFromTestCase(TestIsBranchStackable))
7404.1.17 by Michael Hudson
move DenyingServer into bzrutils, add tests for it.
233
    result.addTests(loader.loadTestsFromTestCase(TestDenyingServer))
7675.240.15 by Jonathan Lange
Add our own APIs for hooking into Bazaar's exception logger.
234
    result.addTests(loader.loadTestsFromTestCase(TestExceptionLoggingHooks))
9478.1.4 by Tim Penhey
Move identical_formats and get_vfs_format_classes into bzrutils.
235
    result.addTests(loader.loadTestsFromTestCase(TestGetVfsFormatClasses))
6876.11.6 by Jonathan Lange
Add a test_bzrutils module that's parametrized by BzrDir.
236
    return result
13677.3.5 by Steve Kowalik
Revert three more failing test files.
237
238
239
def test_suite():
240
    loader = TestLoader()
241
    return loader.loadTestsFromName(__name__)