/brz/remove-bazaar

To get this branch, use:
bzr branch http://gegoxaren.bato24.eu/bzr/brz/remove-bazaar
2297.1.2 by Martin Pool
Cleanup imports
1
# Copyright (C) 2005, 2006, 2007 Canonical Ltd
1553.5.71 by Martin Pool
Change branch format 5 to use LockDirs, not transport locks
2
#
1534.4.3 by Robert Collins
Implement BranchTestProviderAdapter, so tests now run across all branch formats.
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.
1553.5.71 by Martin Pool
Change branch format 5 to use LockDirs, not transport locks
7
#
1534.4.3 by Robert Collins
Implement BranchTestProviderAdapter, so tests now run across all branch formats.
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.
1553.5.71 by Martin Pool
Change branch format 5 to use LockDirs, not transport locks
12
#
1534.4.3 by Robert Collins
Implement BranchTestProviderAdapter, so tests now run across all branch formats.
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., 59 Temple Place, Suite 330, Boston, MA  02111-1307  USA
16
17
"""Tests for the Branch facility that are not interface  tests.
18
1534.4.39 by Robert Collins
Basic BzrDir support.
19
For interface tests see tests/branch_implementations/*.py.
1534.4.3 by Robert Collins
Implement BranchTestProviderAdapter, so tests now run across all branch formats.
20
21
For concrete class tests see this file, and for meta-branch tests
22
also see this file.
23
"""
24
1534.4.7 by Robert Collins
Move downlevel check up to the Branch.open logic, removing it from the Branch constructor and deprecating relax_version_check to the same.
25
from StringIO import StringIO
1534.4.3 by Robert Collins
Implement BranchTestProviderAdapter, so tests now run across all branch formats.
26
2230.3.3 by Aaron Bentley
Add more config testing
27
from bzrlib import (
28
    branch as _mod_branch,
2230.3.44 by Aaron Bentley
Change asserts to specific errors for left-hand history violations
29
    bzrdir,
1551.15.35 by Aaron Bentley
Warn when setting config values that will be masked (#122286)
30
    config,
2230.3.44 by Aaron Bentley
Change asserts to specific errors for left-hand history violations
31
    errors,
1551.15.35 by Aaron Bentley
Warn when setting config values that will be masked (#122286)
32
    trace,
2230.3.3 by Aaron Bentley
Add more config testing
33
    urlutils,
34
    )
2297.1.2 by Martin Pool
Cleanup imports
35
from bzrlib.branch import (
36
    Branch,
37
    BranchHooks,
38
    BranchFormat,
39
    BranchReferenceFormat,
40
    BzrBranch5,
41
    BzrBranchFormat5,
2696.3.1 by Martin Pool
(broken) start switching format to dirstate-tags
42
    BzrBranchFormat6,
2297.1.3 by Martin Pool
PullResult can pretend to be an int for api compatibility with old .pull()
43
    PullResult,
2297.1.2 by Martin Pool
Cleanup imports
44
    )
1553.5.71 by Martin Pool
Change branch format 5 to use LockDirs, not transport locks
45
from bzrlib.bzrdir import (BzrDirMetaFormat1, BzrDirMeta1, 
46
                           BzrDir, BzrDirFormat)
1534.4.7 by Robert Collins
Move downlevel check up to the Branch.open logic, removing it from the Branch constructor and deprecating relax_version_check to the same.
47
from bzrlib.errors import (NotBranchError,
48
                           UnknownFormatError,
2245.1.3 by Robert Collins
Add install_hook to the BranchHooks class as the official means for installing a hook.
49
                           UnknownHook,
1534.4.7 by Robert Collins
Move downlevel check up to the Branch.open logic, removing it from the Branch constructor and deprecating relax_version_check to the same.
50
                           UnsupportedFormatError,
51
                           )
52
1534.4.44 by Robert Collins
Make a new BzrDir format that uses a versioned branch format in a branch/ subdirectory.
53
from bzrlib.tests import TestCase, TestCaseWithTransport
1534.4.4 by Robert Collins
Make BzrBranchFormat.find_format take a transport not a url for efficiency.
54
from bzrlib.transport import get_transport
1534.4.3 by Robert Collins
Implement BranchTestProviderAdapter, so tests now run across all branch formats.
55
3445.2.1 by John Arbash Meinel
Add tests for Branch.missing_revisions and deprecate it.
56
1534.4.44 by Robert Collins
Make a new BzrDir format that uses a versioned branch format in a branch/ subdirectory.
57
class TestDefaultFormat(TestCase):
58
2696.3.1 by Martin Pool
(broken) start switching format to dirstate-tags
59
    def test_default_format(self):
60
        # update this if you change the default branch format
61
        self.assertIsInstance(BranchFormat.get_default_format(),
62
                BzrBranchFormat6)
63
2696.3.3 by Martin Pool
Start setting the default format to dirstate-tags
64
    def test_default_format_is_same_as_bzrdir_default(self):
65
        # XXX: it might be nice if there was only one place the default was
66
        # set, but at the moment that's not true -- mbp 20070814 -- 
2696.3.8 by Martin Pool
doc
67
        # https://bugs.launchpad.net/bzr/+bug/132376
2696.3.3 by Martin Pool
Start setting the default format to dirstate-tags
68
        self.assertEqual(BranchFormat.get_default_format(),
69
                BzrDirFormat.get_default_format().get_branch_format())
70
1534.4.44 by Robert Collins
Make a new BzrDir format that uses a versioned branch format in a branch/ subdirectory.
71
    def test_get_set_default_format(self):
2696.3.1 by Martin Pool
(broken) start switching format to dirstate-tags
72
        # set the format and then set it back again
2297.1.2 by Martin Pool
Cleanup imports
73
        old_format = BranchFormat.get_default_format()
74
        BranchFormat.set_default_format(SampleBranchFormat())
1534.4.44 by Robert Collins
Make a new BzrDir format that uses a versioned branch format in a branch/ subdirectory.
75
        try:
76
            # the default branch format is used by the meta dir format
77
            # which is not the default bzrdir format at this point
1685.1.42 by John Arbash Meinel
A couple more fixes to make sure memory:/// works correctly.
78
            dir = BzrDirMetaFormat1().initialize('memory:///')
1534.4.44 by Robert Collins
Make a new BzrDir format that uses a versioned branch format in a branch/ subdirectory.
79
            result = dir.create_branch()
80
            self.assertEqual(result, 'A branch')
81
        finally:
2297.1.2 by Martin Pool
Cleanup imports
82
            BranchFormat.set_default_format(old_format)
83
        self.assertEqual(old_format, BranchFormat.get_default_format())
1508.1.25 by Robert Collins
Update per review comments.
84
85
1553.5.71 by Martin Pool
Change branch format 5 to use LockDirs, not transport locks
86
class TestBranchFormat5(TestCaseWithTransport):
87
    """Tests specific to branch format 5"""
88
89
    def test_branch_format_5_uses_lockdir(self):
90
        url = self.get_url()
1553.5.72 by Martin Pool
Clean up test for Branch5 lockdirs
91
        bzrdir = BzrDirMetaFormat1().initialize(url)
92
        bzrdir.create_repository()
93
        branch = bzrdir.create_branch()
94
        t = self.get_transport()
95
        self.log("branch instance is %r" % branch)
96
        self.assert_(isinstance(branch, BzrBranch5))
97
        self.assertIsDirectory('.', t)
98
        self.assertIsDirectory('.bzr/branch', t)
99
        self.assertIsDirectory('.bzr/branch/lock', t)
1553.5.73 by Martin Pool
Additional test that Branch5 uses lockdir properly
100
        branch.lock_write()
1658.1.5 by Martin Pool
Release more locks taken during test suite
101
        try:
102
            self.assertIsDirectory('.bzr/branch/lock/held', t)
103
        finally:
104
            branch.unlock()
1553.5.71 by Martin Pool
Change branch format 5 to use LockDirs, not transport locks
105
2230.3.3 by Aaron Bentley
Add more config testing
106
    def test_set_push_location(self):
107
        from bzrlib.config import (locations_config_filename,
108
                                   ensure_config_dir_exists)
109
        ensure_config_dir_exists()
110
        fn = locations_config_filename()
2839.3.1 by Alexander Belchenko
provide non-empty locations.conf for test_branch.TestBranchFormat5.test_set_push_location
111
        # write correct newlines to locations.conf
112
        # by default ConfigObj uses native line-endings for new files
113
        # but uses already existing line-endings if file is not empty
114
        f = open(fn, 'wb')
115
        try:
116
            f.write('# comment\n')
117
        finally:
118
            f.close()
119
2230.3.3 by Aaron Bentley
Add more config testing
120
        branch = self.make_branch('.', format='knit')
121
        branch.set_push_location('foo')
122
        local_path = urlutils.local_path_from_url(branch.base[:-1])
2839.3.1 by Alexander Belchenko
provide non-empty locations.conf for test_branch.TestBranchFormat5.test_set_push_location
123
        self.assertFileEqual("# comment\n"
124
                             "[%s]\n"
2230.3.3 by Aaron Bentley
Add more config testing
125
                             "push_location = foo\n"
3221.7.1 by Matt Nordhoff
Upgrade ConfigObj to version 4.5.1.
126
                             "push_location:policy = norecurse\n" % local_path,
2230.3.3 by Aaron Bentley
Add more config testing
127
                             fn)
128
2230.3.12 by Aaron Bentley
Clean up trailing whitespace
129
    # TODO RBC 20051029 test getting a push location from a branch in a
2230.3.3 by Aaron Bentley
Add more config testing
130
    # recursive section - that is, it appends the branch name.
131
1553.5.71 by Martin Pool
Change branch format 5 to use LockDirs, not transport locks
132
2297.1.2 by Martin Pool
Cleanup imports
133
class SampleBranchFormat(BranchFormat):
1534.4.44 by Robert Collins
Make a new BzrDir format that uses a versioned branch format in a branch/ subdirectory.
134
    """A sample format
135
136
    this format is initializable, unsupported to aid in testing the 
137
    open and open_downlevel routines.
138
    """
139
140
    def get_format_string(self):
141
        """See BzrBranchFormat.get_format_string()."""
142
        return "Sample branch format."
143
144
    def initialize(self, a_bzrdir):
145
        """Format 4 branches cannot be created."""
146
        t = a_bzrdir.get_branch_transport(self)
1955.3.9 by John Arbash Meinel
Find more occurrances of put() and replace with put_file or put_bytes
147
        t.put_bytes('format', self.get_format_string())
1534.4.44 by Robert Collins
Make a new BzrDir format that uses a versioned branch format in a branch/ subdirectory.
148
        return 'A branch'
149
150
    def is_supported(self):
151
        return False
152
153
    def open(self, transport, _found=False):
154
        return "opened branch."
155
156
157
class TestBzrBranchFormat(TestCaseWithTransport):
158
    """Tests for the BzrBranchFormat facility."""
159
160
    def test_find_format(self):
161
        # is the right format object found for a branch?
162
        # create a branch with a few known format objects.
163
        # this is not quite the same as 
164
        self.build_tree(["foo/", "bar/"])
165
        def check_format(format, url):
166
            dir = format._matchingbzrdir.initialize(url)
1534.4.47 by Robert Collins
Split out repository into .bzr/repository
167
            dir.create_repository()
1534.4.44 by Robert Collins
Make a new BzrDir format that uses a versioned branch format in a branch/ subdirectory.
168
            format.initialize(dir)
2297.1.2 by Martin Pool
Cleanup imports
169
            found_format = BranchFormat.find_format(dir)
1534.4.44 by Robert Collins
Make a new BzrDir format that uses a versioned branch format in a branch/ subdirectory.
170
            self.failUnless(isinstance(found_format, format.__class__))
2297.1.2 by Martin Pool
Cleanup imports
171
        check_format(BzrBranchFormat5(), "bar")
1534.4.44 by Robert Collins
Make a new BzrDir format that uses a versioned branch format in a branch/ subdirectory.
172
        
173
    def test_find_format_not_branch(self):
174
        dir = bzrdir.BzrDirMetaFormat1().initialize(self.get_url())
175
        self.assertRaises(NotBranchError,
2297.1.2 by Martin Pool
Cleanup imports
176
                          BranchFormat.find_format,
1534.4.44 by Robert Collins
Make a new BzrDir format that uses a versioned branch format in a branch/ subdirectory.
177
                          dir)
178
179
    def test_find_format_unknown_format(self):
180
        dir = bzrdir.BzrDirMetaFormat1().initialize(self.get_url())
181
        SampleBranchFormat().initialize(dir)
182
        self.assertRaises(UnknownFormatError,
2297.1.2 by Martin Pool
Cleanup imports
183
                          BranchFormat.find_format,
1534.4.44 by Robert Collins
Make a new BzrDir format that uses a versioned branch format in a branch/ subdirectory.
184
                          dir)
185
186
    def test_register_unregister_format(self):
187
        format = SampleBranchFormat()
188
        # make a control dir
189
        dir = bzrdir.BzrDirMetaFormat1().initialize(self.get_url())
190
        # make a branch
191
        format.initialize(dir)
192
        # register a format for it.
2297.1.2 by Martin Pool
Cleanup imports
193
        BranchFormat.register_format(format)
1534.4.44 by Robert Collins
Make a new BzrDir format that uses a versioned branch format in a branch/ subdirectory.
194
        # which branch.Open will refuse (not supported)
2297.1.2 by Martin Pool
Cleanup imports
195
        self.assertRaises(UnsupportedFormatError, Branch.open, self.get_url())
2230.3.22 by Aaron Bentley
Make test suite use format registry default, not BzrDir default
196
        self.make_branch_and_tree('foo')
1534.4.44 by Robert Collins
Make a new BzrDir format that uses a versioned branch format in a branch/ subdirectory.
197
        # but open_downlevel will work
1534.4.50 by Robert Collins
Got the bzrdir api straightened out, plenty of refactoring to use it pending, but the api is up and running.
198
        self.assertEqual(format.open(dir), bzrdir.BzrDir.open(self.get_url()).open_branch(unsupported=True))
1534.4.44 by Robert Collins
Make a new BzrDir format that uses a versioned branch format in a branch/ subdirectory.
199
        # unregister the format
2297.1.2 by Martin Pool
Cleanup imports
200
        BranchFormat.unregister_format(format)
2230.3.22 by Aaron Bentley
Make test suite use format registry default, not BzrDir default
201
        self.make_branch_and_tree('bar')
1534.4.50 by Robert Collins
Got the bzrdir api straightened out, plenty of refactoring to use it pending, but the api is up and running.
202
203
3221.11.2 by Robert Collins
Create basic stackable branch facility.
204
class TestBranch67(object):
205
    """Common tests for both branch 6 and 7 which are mostly the same."""
206
207
    def get_format_name(self):
208
        raise NotImplementedError(self.get_format_name)
209
210
    def get_format_name_subtree(self):
211
        raise NotImplementedError(self.get_format_name)
212
213
    def get_class(self):
214
        raise NotImplementedError(self.get_class)
2230.3.1 by Aaron Bentley
Get branch6 creation working
215
216
    def test_creation(self):
217
        format = BzrDirMetaFormat1()
2230.3.55 by Aaron Bentley
Updates from review
218
        format.set_branch_format(_mod_branch.BzrBranchFormat6())
2230.3.1 by Aaron Bentley
Get branch6 creation working
219
        branch = self.make_branch('a', format=format)
3221.11.2 by Robert Collins
Create basic stackable branch facility.
220
        self.assertIsInstance(branch, self.get_class())
221
        branch = self.make_branch('b', format=self.get_format_name())
222
        self.assertIsInstance(branch, self.get_class())
2230.3.1 by Aaron Bentley
Get branch6 creation working
223
        branch = _mod_branch.Branch.open('a')
3221.11.2 by Robert Collins
Create basic stackable branch facility.
224
        self.assertIsInstance(branch, self.get_class())
2230.3.1 by Aaron Bentley
Get branch6 creation working
225
226
    def test_layout(self):
3221.11.2 by Robert Collins
Create basic stackable branch facility.
227
        branch = self.make_branch('a', format=self.get_format_name())
2230.3.1 by Aaron Bentley
Get branch6 creation working
228
        self.failUnlessExists('a/.bzr/branch/last-revision')
229
        self.failIfExists('a/.bzr/branch/revision-history')
230
2230.3.3 by Aaron Bentley
Add more config testing
231
    def test_config(self):
232
        """Ensure that all configuration data is stored in the branch"""
3221.11.2 by Robert Collins
Create basic stackable branch facility.
233
        branch = self.make_branch('a', format=self.get_format_name())
2230.3.3 by Aaron Bentley
Add more config testing
234
        branch.set_parent('http://bazaar-vcs.org')
235
        self.failIfExists('a/.bzr/branch/parent')
236
        self.assertEqual('http://bazaar-vcs.org', branch.get_parent())
237
        branch.set_push_location('sftp://bazaar-vcs.org')
238
        config = branch.get_config()._get_branch_data_config()
2230.3.12 by Aaron Bentley
Clean up trailing whitespace
239
        self.assertEqual('sftp://bazaar-vcs.org',
2230.3.3 by Aaron Bentley
Add more config testing
240
                         config.get_user_option('push_location'))
241
        branch.set_bound_location('ftp://bazaar-vcs.org')
242
        self.failIfExists('a/.bzr/branch/bound')
243
        self.assertEqual('ftp://bazaar-vcs.org', branch.get_bound_location())
244
2230.3.44 by Aaron Bentley
Change asserts to specific errors for left-hand history violations
245
    def test_set_revision_history(self):
246
        tree = self.make_branch_and_memory_tree('.',
3221.11.2 by Robert Collins
Create basic stackable branch facility.
247
            format=self.get_format_name())
2230.3.44 by Aaron Bentley
Change asserts to specific errors for left-hand history violations
248
        tree.lock_write()
249
        try:
250
            tree.add('.')
251
            tree.commit('foo', rev_id='foo')
252
            tree.commit('bar', rev_id='bar')
253
            tree.branch.set_revision_history(['foo', 'bar'])
254
            tree.branch.set_revision_history(['foo'])
255
            self.assertRaises(errors.NotLefthandHistory,
256
                              tree.branch.set_revision_history, ['bar'])
257
        finally:
258
            tree.unlock()
259
2100.3.26 by Aaron Bentley
checkout type is maintained for subtrees
260
    def do_checkout_test(self, lightweight=False):
3221.11.2 by Robert Collins
Create basic stackable branch facility.
261
        tree = self.make_branch_and_tree('source',
262
            format=self.get_format_name_subtree())
2255.2.194 by Robert Collins
[BROKEN] Many updates to stop using experimental formats in tests.
263
        subtree = self.make_branch_and_tree('source/subtree',
3221.11.2 by Robert Collins
Create basic stackable branch facility.
264
            format=self.get_format_name_subtree())
2100.3.25 by Aaron Bentley
add subsubtree to test
265
        subsubtree = self.make_branch_and_tree('source/subtree/subsubtree',
3221.11.2 by Robert Collins
Create basic stackable branch facility.
266
            format=self.get_format_name_subtree())
2100.3.25 by Aaron Bentley
add subsubtree to test
267
        self.build_tree(['source/subtree/file',
268
                         'source/subtree/subsubtree/file'])
269
        subsubtree.add('file')
2100.3.21 by Aaron Bentley
Work on checking out by-reference trees
270
        subtree.add('file')
2100.3.25 by Aaron Bentley
add subsubtree to test
271
        subtree.add_reference(subsubtree)
2100.3.21 by Aaron Bentley
Work on checking out by-reference trees
272
        tree.add_reference(subtree)
273
        tree.commit('a revision')
2100.3.23 by Aaron Bentley
Nested checkouts kinda work
274
        subtree.commit('a subtree file')
2100.3.25 by Aaron Bentley
add subsubtree to test
275
        subsubtree.commit('a subsubtree file')
2100.3.26 by Aaron Bentley
checkout type is maintained for subtrees
276
        tree.branch.create_checkout('target', lightweight=lightweight)
2100.3.21 by Aaron Bentley
Work on checking out by-reference trees
277
        self.failUnlessExists('target')
278
        self.failUnlessExists('target/subtree')
279
        self.failUnlessExists('target/subtree/file')
2100.3.25 by Aaron Bentley
add subsubtree to test
280
        self.failUnlessExists('target/subtree/subsubtree/file')
2100.3.31 by Aaron Bentley
Merged bzr.dev (17 tests failing)
281
        subbranch = _mod_branch.Branch.open('target/subtree/subsubtree')
2100.3.26 by Aaron Bentley
checkout type is maintained for subtrees
282
        if lightweight:
283
            self.assertEndsWith(subbranch.base, 'source/subtree/subsubtree/')
284
        else:
285
            self.assertEndsWith(subbranch.base, 'target/subtree/subsubtree/')
286
287
    def test_checkout_with_references(self):
288
        self.do_checkout_test()
289
290
    def test_light_checkout_with_references(self):
291
        self.do_checkout_test(lightweight=True)
2230.3.51 by Aaron Bentley
Store revno for Branch6, set_last_revision -> set_last_revision_info
292
1551.15.35 by Aaron Bentley
Warn when setting config values that will be masked (#122286)
293
    def test_set_push(self):
3221.11.2 by Robert Collins
Create basic stackable branch facility.
294
        branch = self.make_branch('source', format=self.get_format_name())
1551.15.35 by Aaron Bentley
Warn when setting config values that will be masked (#122286)
295
        branch.get_config().set_user_option('push_location', 'old',
296
            store=config.STORE_LOCATION)
297
        warnings = []
298
        def warning(*args):
299
            warnings.append(args[0] % args[1:])
300
        _warning = trace.warning
301
        trace.warning = warning
302
        try:
303
            branch.set_push_location('new')
304
        finally:
305
            trace.warning = _warning
306
        self.assertEqual(warnings[0], 'Value "new" is masked by "old" from '
307
                         'locations.conf')
308
3445.2.1 by John Arbash Meinel
Add tests for Branch.missing_revisions and deprecate it.
309
3221.11.2 by Robert Collins
Create basic stackable branch facility.
310
class TestBranch6(TestBranch67, TestCaseWithTransport):
311
312
    def get_class(self):
313
        return _mod_branch.BzrBranch6
314
315
    def get_format_name(self):
316
        return "dirstate-tags"
317
318
    def get_format_name_subtree(self):
319
        return "dirstate-with-subtree"
320
3537.3.3 by Martin Pool
Rename Branch.set_stacked_on to set_stacked_on_url
321
    def test_set_stacked_on_url_errors(self):
3221.11.2 by Robert Collins
Create basic stackable branch facility.
322
        branch = self.make_branch('a', format=self.get_format_name())
323
        self.assertRaises(errors.UnstackableBranchFormat,
3537.3.3 by Martin Pool
Rename Branch.set_stacked_on to set_stacked_on_url
324
            branch.set_stacked_on_url, None)
3221.11.2 by Robert Collins
Create basic stackable branch facility.
325
326
    def test_default_stacked_location(self):
327
        branch = self.make_branch('a', format=self.get_format_name())
3537.3.1 by Martin Pool
Rename branch.get_stacked_on to get_stacked_on_url
328
        self.assertRaises(errors.UnstackableBranchFormat, branch.get_stacked_on_url)
3221.11.2 by Robert Collins
Create basic stackable branch facility.
329
330
331
class TestBranch7(TestBranch67, TestCaseWithTransport):
332
333
    def get_class(self):
334
        return _mod_branch.BzrBranch7
335
336
    def get_format_name(self):
337
        return "development"
338
339
    def get_format_name_subtree(self):
340
        return "development-subtree"
341
3537.3.3 by Martin Pool
Rename Branch.set_stacked_on to set_stacked_on_url
342
    def test_set_stacked_on_url_unstackable_repo(self):
3221.11.6 by Robert Collins
Stackable branch fixes.
343
        repo = self.make_repository('a', format='dirstate-tags')
344
        control = repo.bzrdir
345
        branch = _mod_branch.BzrBranchFormat7().initialize(control)
3221.11.2 by Robert Collins
Create basic stackable branch facility.
346
        target = self.make_branch('b')
347
        self.assertRaises(errors.UnstackableRepositoryFormat,
3537.3.3 by Martin Pool
Rename Branch.set_stacked_on to set_stacked_on_url
348
            branch.set_stacked_on_url, target.base)
3221.11.2 by Robert Collins
Create basic stackable branch facility.
349
3242.3.21 by Jonathan Lange
Preserve stacking in clone
350
    def test_clone_stacked_on_unstackable_repo(self):
351
        repo = self.make_repository('a', format='dirstate-tags')
352
        control = repo.bzrdir
353
        branch = _mod_branch.BzrBranchFormat7().initialize(control)
354
        # Calling clone should not raise UnstackableRepositoryFormat.
355
        cloned_bzrdir = control.clone('cloned')
356
3221.11.2 by Robert Collins
Create basic stackable branch facility.
357
    def _test_default_stacked_location(self):
358
        branch = self.make_branch('a', format=self.get_format_name())
3537.3.1 by Martin Pool
Rename branch.get_stacked_on to get_stacked_on_url
359
        self.assertRaises(errors.NotStacked, branch.get_stacked_on_url)
3221.11.2 by Robert Collins
Create basic stackable branch facility.
360
3221.11.8 by Robert Collins
Minimally test stacking and unstacking a repository.
361
    def test_stack_and_unstack(self):
362
        branch = self.make_branch('a', format=self.get_format_name())
3221.11.10 by Robert Collins
Extend set_stacked_on to update the repository with the right external references.
363
        target = self.make_branch_and_tree('b', format=self.get_format_name())
3537.3.3 by Martin Pool
Rename Branch.set_stacked_on to set_stacked_on_url
364
        branch.set_stacked_on_url(target.branch.base)
3537.3.1 by Martin Pool
Rename branch.get_stacked_on to get_stacked_on_url
365
        self.assertEqual(target.branch.base, branch.get_stacked_on_url())
3221.11.10 by Robert Collins
Extend set_stacked_on to update the repository with the right external references.
366
        revid = target.commit('foo')
367
        self.assertTrue(branch.repository.has_revision(revid))
3537.3.3 by Martin Pool
Rename Branch.set_stacked_on to set_stacked_on_url
368
        branch.set_stacked_on_url(None)
3537.3.1 by Martin Pool
Rename branch.get_stacked_on to get_stacked_on_url
369
        self.assertRaises(errors.NotStacked, branch.get_stacked_on_url)
3221.11.10 by Robert Collins
Extend set_stacked_on to update the repository with the right external references.
370
        self.assertFalse(branch.repository.has_revision(revid))
3221.11.8 by Robert Collins
Minimally test stacking and unstacking a repository.
371
3221.11.11 by Robert Collins
Ensure opening a stacked branch gives a ready to use repository.
372
    def test_open_opens_stacked_reference(self):
373
        branch = self.make_branch('a', format=self.get_format_name())
374
        target = self.make_branch_and_tree('b', format=self.get_format_name())
3537.3.3 by Martin Pool
Rename Branch.set_stacked_on to set_stacked_on_url
375
        branch.set_stacked_on_url(target.branch.base)
3221.11.11 by Robert Collins
Ensure opening a stacked branch gives a ready to use repository.
376
        branch = branch.bzrdir.open_branch()
377
        revid = target.commit('foo')
378
        self.assertTrue(branch.repository.has_revision(revid))
379
3221.11.2 by Robert Collins
Create basic stackable branch facility.
380
1534.4.50 by Robert Collins
Got the bzrdir api straightened out, plenty of refactoring to use it pending, but the api is up and running.
381
class TestBranchReference(TestCaseWithTransport):
382
    """Tests for the branch reference facility."""
383
384
    def test_create_open_reference(self):
385
        bzrdirformat = bzrdir.BzrDirMetaFormat1()
386
        t = get_transport(self.get_url('.'))
387
        t.mkdir('repo')
388
        dir = bzrdirformat.initialize(self.get_url('repo'))
389
        dir.create_repository()
390
        target_branch = dir.create_branch()
391
        t.mkdir('branch')
392
        branch_dir = bzrdirformat.initialize(self.get_url('branch'))
2297.1.2 by Martin Pool
Cleanup imports
393
        made_branch = BranchReferenceFormat().initialize(branch_dir, target_branch)
1534.4.50 by Robert Collins
Got the bzrdir api straightened out, plenty of refactoring to use it pending, but the api is up and running.
394
        self.assertEqual(made_branch.base, target_branch.base)
395
        opened_branch = branch_dir.open_branch()
396
        self.assertEqual(opened_branch.base, target_branch.base)
2018.6.1 by Robert Collins
Implement a BzrDir.open_branch smart server method for opening a branch without VFS.
397
398
    def test_get_reference(self):
399
        """For a BranchReference, get_reference should reutrn the location."""
400
        branch = self.make_branch('target')
401
        checkout = branch.create_checkout('checkout', lightweight=True)
402
        reference_url = branch.bzrdir.root_transport.abspath('') + '/'
403
        # if the api for create_checkout changes to return different checkout types
404
        # then this file read will fail.
405
        self.assertFileEqual(reference_url, 'checkout/.bzr/branch/location')
406
        self.assertEqual(reference_url,
2018.5.97 by Andrew Bennetts
Fix more tests.
407
            _mod_branch.BranchReferenceFormat().get_reference(checkout.bzrdir))
2018.5.45 by Andrew Bennetts
Merge from bzr.dev
408
2245.1.1 by Robert Collins
New Branch hooks facility, with one initial hook 'set_rh' which triggers
409
410
class TestHooks(TestCase):
411
2245.1.2 by Robert Collins
Remove the static DefaultHooks method from Branch, replacing it with a derived dict BranchHooks object, which is easier to use and provides a place to put the policy-checking add method discussed on list.
412
    def test_constructor(self):
413
        """Check that creating a BranchHooks instance has the right defaults."""
2297.1.2 by Martin Pool
Cleanup imports
414
        hooks = BranchHooks()
2245.1.2 by Robert Collins
Remove the static DefaultHooks method from Branch, replacing it with a derived dict BranchHooks object, which is easier to use and provides a place to put the policy-checking add method discussed on list.
415
        self.assertTrue("set_rh" in hooks, "set_rh not in %s" % hooks)
2246.1.3 by Robert Collins
New branch hooks: post_push, post_pull, post_commit, post_uncommit. These
416
        self.assertTrue("post_push" in hooks, "post_push not in %s" % hooks)
417
        self.assertTrue("post_commit" in hooks, "post_commit not in %s" % hooks)
2659.3.1 by NamNguyen
``Branch.hooks`` now supports ``pre_commit`` hook.
418
        self.assertTrue("pre_commit" in hooks, "pre_commit not in %s" % hooks)
2246.1.3 by Robert Collins
New branch hooks: post_push, post_pull, post_commit, post_uncommit. These
419
        self.assertTrue("post_pull" in hooks, "post_pull not in %s" % hooks)
420
        self.assertTrue("post_uncommit" in hooks, "post_uncommit not in %s" % hooks)
3331.1.4 by James Henstridge
Adjust my tests to pass with Ian's API.
421
        self.assertTrue("post_change_branch_tip" in hooks,
422
                        "post_change_branch_tip not in %s" % hooks)
2245.1.1 by Robert Collins
New Branch hooks facility, with one initial hook 'set_rh' which triggers
423
2245.1.2 by Robert Collins
Remove the static DefaultHooks method from Branch, replacing it with a derived dict BranchHooks object, which is easier to use and provides a place to put the policy-checking add method discussed on list.
424
    def test_installed_hooks_are_BranchHooks(self):
425
        """The installed hooks object should be a BranchHooks."""
426
        # the installed hooks are saved in self._preserved_hooks.
2376.3.5 by Robert Collins
Fix Branch hook tests for the new eans of preserving hooks by the test suite.
427
        self.assertIsInstance(self._preserved_hooks[_mod_branch.Branch], BranchHooks)
2245.1.3 by Robert Collins
Add install_hook to the BranchHooks class as the official means for installing a hook.
428
2297.1.3 by Martin Pool
PullResult can pretend to be an int for api compatibility with old .pull()
429
430
class TestPullResult(TestCase):
431
432
    def test_pull_result_to_int(self):
433
        # to support old code, the pull result can be used as an int
434
        r = PullResult()
435
        r.old_revno = 10
436
        r.new_revno = 20
437
        # this usage of results is not recommended for new code (because it
438
        # doesn't describe very well what happened), but for api stability
439
        # it's still supported
440
        a = "%d revisions pulled" % r
441
        self.assertEqual(a, "10 revisions pulled")