~bzr-pqm/bzr/bzr.dev

« back to all changes in this revision

Viewing changes to bzrlib/tests/test_branch.py

  • Committer: Canonical.com Patch Queue Manager
  • Date: 2006-08-17 07:52:09 UTC
  • mfrom: (1910.3.4 trivial)
  • Revision ID: pqm@pqm.ubuntu.com-20060817075209-e85a1f9e05ff8b87
(andrew) Trivial fixes to NotImplemented errors.

Show diffs side-by-side

added added

removed removed

Lines of Context:
1
 
# Copyright (C) 2005, 2006, 2007 Canonical Ltd
 
1
# Copyright (C) 2005, 2006 Canonical Ltd
2
2
#
3
3
# This program is free software; you can redistribute it and/or modify
4
4
# it under the terms of the GNU General Public License as published by
24
24
 
25
25
from StringIO import StringIO
26
26
 
27
 
from bzrlib import (
28
 
    branch as _mod_branch,
29
 
    bzrdir,
30
 
    config,
31
 
    errors,
32
 
    trace,
33
 
    urlutils,
34
 
    )
35
 
from bzrlib.branch import (
36
 
    Branch,
37
 
    BranchHooks,
38
 
    BranchFormat,
39
 
    BranchReferenceFormat,
40
 
    BzrBranch5,
41
 
    BzrBranchFormat5,
42
 
    BzrBranchFormat6,
43
 
    PullResult,
44
 
    )
 
27
import bzrlib.branch
 
28
from bzrlib.branch import (BzrBranch5, 
 
29
                           BzrBranchFormat5)
 
30
import bzrlib.bzrdir as bzrdir
45
31
from bzrlib.bzrdir import (BzrDirMetaFormat1, BzrDirMeta1, 
46
32
                           BzrDir, BzrDirFormat)
47
33
from bzrlib.errors import (NotBranchError,
48
34
                           UnknownFormatError,
49
 
                           UnknownHook,
50
35
                           UnsupportedFormatError,
51
36
                           )
52
37
 
55
40
 
56
41
class TestDefaultFormat(TestCase):
57
42
 
58
 
    def test_default_format(self):
59
 
        # update this if you change the default branch format
60
 
        self.assertIsInstance(BranchFormat.get_default_format(),
61
 
                BzrBranchFormat6)
62
 
 
63
 
    def test_default_format_is_same_as_bzrdir_default(self):
64
 
        # XXX: it might be nice if there was only one place the default was
65
 
        # set, but at the moment that's not true -- mbp 20070814 -- 
66
 
        # https://bugs.launchpad.net/bzr/+bug/132376
67
 
        self.assertEqual(BranchFormat.get_default_format(),
68
 
                BzrDirFormat.get_default_format().get_branch_format())
69
 
 
70
43
    def test_get_set_default_format(self):
71
 
        # set the format and then set it back again
72
 
        old_format = BranchFormat.get_default_format()
73
 
        BranchFormat.set_default_format(SampleBranchFormat())
 
44
        old_format = bzrlib.branch.BranchFormat.get_default_format()
 
45
        # default is 5
 
46
        self.assertTrue(isinstance(old_format, bzrlib.branch.BzrBranchFormat5))
 
47
        bzrlib.branch.BranchFormat.set_default_format(SampleBranchFormat())
74
48
        try:
75
49
            # the default branch format is used by the meta dir format
76
50
            # which is not the default bzrdir format at this point
78
52
            result = dir.create_branch()
79
53
            self.assertEqual(result, 'A branch')
80
54
        finally:
81
 
            BranchFormat.set_default_format(old_format)
82
 
        self.assertEqual(old_format, BranchFormat.get_default_format())
 
55
            bzrlib.branch.BranchFormat.set_default_format(old_format)
 
56
        self.assertEqual(old_format, bzrlib.branch.BranchFormat.get_default_format())
83
57
 
84
58
 
85
59
class TestBranchFormat5(TestCaseWithTransport):
102
76
        finally:
103
77
            branch.unlock()
104
78
 
105
 
    def test_set_push_location(self):
106
 
        from bzrlib.config import (locations_config_filename,
107
 
                                   ensure_config_dir_exists)
108
 
        ensure_config_dir_exists()
109
 
        fn = locations_config_filename()
110
 
        # write correct newlines to locations.conf
111
 
        # by default ConfigObj uses native line-endings for new files
112
 
        # but uses already existing line-endings if file is not empty
113
 
        f = open(fn, 'wb')
114
 
        try:
115
 
            f.write('# comment\n')
116
 
        finally:
117
 
            f.close()
118
 
 
119
 
        branch = self.make_branch('.', format='knit')
120
 
        branch.set_push_location('foo')
121
 
        local_path = urlutils.local_path_from_url(branch.base[:-1])
122
 
        self.assertFileEqual("# comment\n"
123
 
                             "[%s]\n"
124
 
                             "push_location = foo\n"
125
 
                             "push_location:policy = norecurse" % local_path,
126
 
                             fn)
127
 
 
128
 
    # TODO RBC 20051029 test getting a push location from a branch in a
129
 
    # recursive section - that is, it appends the branch name.
130
 
 
131
 
 
132
 
class SampleBranchFormat(BranchFormat):
 
79
 
 
80
class TestBranchEscaping(TestCaseWithTransport):
 
81
    """Test a branch can be correctly stored and used on a vfat-like transport
 
82
    
 
83
    Makes sure we have proper escaping of invalid characters, etc.
 
84
 
 
85
    It'd be better to test all operations on the FakeVFATTransportDecorator,
 
86
    but working trees go straight to the os not through the Transport layer.
 
87
    Therefore we build some history first in the regular way and then 
 
88
    check it's safe to access for vfat.
 
89
    """
 
90
 
 
91
    FOO_ID = 'foo<:>ID'
 
92
    REV_ID = 'revid-1'
 
93
 
 
94
    def setUp(self):
 
95
        super(TestBranchEscaping, self).setUp()
 
96
        from bzrlib.repository import RepositoryFormatKnit1
 
97
        bzrdir = BzrDirMetaFormat1().initialize(self.get_url())
 
98
        repo = RepositoryFormatKnit1().initialize(bzrdir)
 
99
        branch = bzrdir.create_branch()
 
100
        wt = bzrdir.create_workingtree()
 
101
        self.build_tree_contents([("foo", "contents of foo")])
 
102
        # add file with id containing wierd characters
 
103
        wt.add(['foo'], [self.FOO_ID])
 
104
        wt.commit('this is my new commit', rev_id=self.REV_ID)
 
105
 
 
106
    def test_branch_on_vfat(self):
 
107
        from bzrlib.transport.fakevfat import FakeVFATTransportDecorator
 
108
        # now access over vfat; should be safe
 
109
        transport = FakeVFATTransportDecorator('vfat+' + self.get_url())
 
110
        bzrdir, junk = BzrDir.open_containing_from_transport(transport)
 
111
        branch = bzrdir.open_branch()
 
112
        revtree = branch.repository.revision_tree(self.REV_ID)
 
113
        contents = revtree.get_file_text(self.FOO_ID)
 
114
        self.assertEqual(contents, 'contents of foo')
 
115
 
 
116
 
 
117
class SampleBranchFormat(bzrlib.branch.BranchFormat):
133
118
    """A sample format
134
119
 
135
120
    this format is initializable, unsupported to aid in testing the 
143
128
    def initialize(self, a_bzrdir):
144
129
        """Format 4 branches cannot be created."""
145
130
        t = a_bzrdir.get_branch_transport(self)
146
 
        t.put_bytes('format', self.get_format_string())
 
131
        t.put('format', StringIO(self.get_format_string()))
147
132
        return 'A branch'
148
133
 
149
134
    def is_supported(self):
165
150
            dir = format._matchingbzrdir.initialize(url)
166
151
            dir.create_repository()
167
152
            format.initialize(dir)
168
 
            found_format = BranchFormat.find_format(dir)
 
153
            found_format = bzrlib.branch.BranchFormat.find_format(dir)
169
154
            self.failUnless(isinstance(found_format, format.__class__))
170
 
        check_format(BzrBranchFormat5(), "bar")
 
155
        check_format(bzrlib.branch.BzrBranchFormat5(), "bar")
171
156
        
172
157
    def test_find_format_not_branch(self):
173
158
        dir = bzrdir.BzrDirMetaFormat1().initialize(self.get_url())
174
159
        self.assertRaises(NotBranchError,
175
 
                          BranchFormat.find_format,
 
160
                          bzrlib.branch.BranchFormat.find_format,
176
161
                          dir)
177
162
 
178
163
    def test_find_format_unknown_format(self):
179
164
        dir = bzrdir.BzrDirMetaFormat1().initialize(self.get_url())
180
165
        SampleBranchFormat().initialize(dir)
181
166
        self.assertRaises(UnknownFormatError,
182
 
                          BranchFormat.find_format,
 
167
                          bzrlib.branch.BranchFormat.find_format,
183
168
                          dir)
184
169
 
185
170
    def test_register_unregister_format(self):
189
174
        # make a branch
190
175
        format.initialize(dir)
191
176
        # register a format for it.
192
 
        BranchFormat.register_format(format)
 
177
        bzrlib.branch.BranchFormat.register_format(format)
193
178
        # which branch.Open will refuse (not supported)
194
 
        self.assertRaises(UnsupportedFormatError, Branch.open, self.get_url())
195
 
        self.make_branch_and_tree('foo')
 
179
        self.assertRaises(UnsupportedFormatError, bzrlib.branch.Branch.open, self.get_url())
196
180
        # but open_downlevel will work
197
181
        self.assertEqual(format.open(dir), bzrdir.BzrDir.open(self.get_url()).open_branch(unsupported=True))
198
182
        # unregister the format
199
 
        BranchFormat.unregister_format(format)
200
 
        self.make_branch_and_tree('bar')
201
 
 
202
 
 
203
 
class TestBranch6(TestCaseWithTransport):
204
 
 
205
 
    def test_creation(self):
206
 
        format = BzrDirMetaFormat1()
207
 
        format.set_branch_format(_mod_branch.BzrBranchFormat6())
208
 
        branch = self.make_branch('a', format=format)
209
 
        self.assertIsInstance(branch, _mod_branch.BzrBranch6)
210
 
        branch = self.make_branch('b', format='dirstate-tags')
211
 
        self.assertIsInstance(branch, _mod_branch.BzrBranch6)
212
 
        branch = _mod_branch.Branch.open('a')
213
 
        self.assertIsInstance(branch, _mod_branch.BzrBranch6)
214
 
 
215
 
    def test_layout(self):
216
 
        branch = self.make_branch('a', format='dirstate-tags')
217
 
        self.failUnlessExists('a/.bzr/branch/last-revision')
218
 
        self.failIfExists('a/.bzr/branch/revision-history')
219
 
 
220
 
    def test_config(self):
221
 
        """Ensure that all configuration data is stored in the branch"""
222
 
        branch = self.make_branch('a', format='dirstate-tags')
223
 
        branch.set_parent('http://bazaar-vcs.org')
224
 
        self.failIfExists('a/.bzr/branch/parent')
225
 
        self.assertEqual('http://bazaar-vcs.org', branch.get_parent())
226
 
        branch.set_push_location('sftp://bazaar-vcs.org')
227
 
        config = branch.get_config()._get_branch_data_config()
228
 
        self.assertEqual('sftp://bazaar-vcs.org',
229
 
                         config.get_user_option('push_location'))
230
 
        branch.set_bound_location('ftp://bazaar-vcs.org')
231
 
        self.failIfExists('a/.bzr/branch/bound')
232
 
        self.assertEqual('ftp://bazaar-vcs.org', branch.get_bound_location())
233
 
 
234
 
    def test_set_revision_history(self):
235
 
        tree = self.make_branch_and_memory_tree('.',
236
 
            format='dirstate-tags')
237
 
        tree.lock_write()
238
 
        try:
239
 
            tree.add('.')
240
 
            tree.commit('foo', rev_id='foo')
241
 
            tree.commit('bar', rev_id='bar')
242
 
            tree.branch.set_revision_history(['foo', 'bar'])
243
 
            tree.branch.set_revision_history(['foo'])
244
 
            self.assertRaises(errors.NotLefthandHistory,
245
 
                              tree.branch.set_revision_history, ['bar'])
246
 
        finally:
247
 
            tree.unlock()
248
 
 
249
 
    def do_checkout_test(self, lightweight=False):
250
 
        tree = self.make_branch_and_tree('source', format='dirstate-with-subtree')
251
 
        subtree = self.make_branch_and_tree('source/subtree',
252
 
            format='dirstate-with-subtree')
253
 
        subsubtree = self.make_branch_and_tree('source/subtree/subsubtree',
254
 
            format='dirstate-with-subtree')
255
 
        self.build_tree(['source/subtree/file',
256
 
                         'source/subtree/subsubtree/file'])
257
 
        subsubtree.add('file')
258
 
        subtree.add('file')
259
 
        subtree.add_reference(subsubtree)
260
 
        tree.add_reference(subtree)
261
 
        tree.commit('a revision')
262
 
        subtree.commit('a subtree file')
263
 
        subsubtree.commit('a subsubtree file')
264
 
        tree.branch.create_checkout('target', lightweight=lightweight)
265
 
        self.failUnlessExists('target')
266
 
        self.failUnlessExists('target/subtree')
267
 
        self.failUnlessExists('target/subtree/file')
268
 
        self.failUnlessExists('target/subtree/subsubtree/file')
269
 
        subbranch = _mod_branch.Branch.open('target/subtree/subsubtree')
270
 
        if lightweight:
271
 
            self.assertEndsWith(subbranch.base, 'source/subtree/subsubtree/')
272
 
        else:
273
 
            self.assertEndsWith(subbranch.base, 'target/subtree/subsubtree/')
274
 
 
275
 
    def test_checkout_with_references(self):
276
 
        self.do_checkout_test()
277
 
 
278
 
    def test_light_checkout_with_references(self):
279
 
        self.do_checkout_test(lightweight=True)
280
 
 
281
 
    def test_set_push(self):
282
 
        branch = self.make_branch('source', format='dirstate-tags')
283
 
        branch.get_config().set_user_option('push_location', 'old',
284
 
            store=config.STORE_LOCATION)
285
 
        warnings = []
286
 
        def warning(*args):
287
 
            warnings.append(args[0] % args[1:])
288
 
        _warning = trace.warning
289
 
        trace.warning = warning
290
 
        try:
291
 
            branch.set_push_location('new')
292
 
        finally:
293
 
            trace.warning = _warning
294
 
        self.assertEqual(warnings[0], 'Value "new" is masked by "old" from '
295
 
                         'locations.conf')
 
183
        bzrlib.branch.BranchFormat.unregister_format(format)
 
184
 
296
185
 
297
186
class TestBranchReference(TestCaseWithTransport):
298
187
    """Tests for the branch reference facility."""
306
195
        target_branch = dir.create_branch()
307
196
        t.mkdir('branch')
308
197
        branch_dir = bzrdirformat.initialize(self.get_url('branch'))
309
 
        made_branch = BranchReferenceFormat().initialize(branch_dir, target_branch)
 
198
        made_branch = bzrlib.branch.BranchReferenceFormat().initialize(branch_dir, target_branch)
310
199
        self.assertEqual(made_branch.base, target_branch.base)
311
200
        opened_branch = branch_dir.open_branch()
312
201
        self.assertEqual(opened_branch.base, target_branch.base)
313
 
 
314
 
    def test_get_reference(self):
315
 
        """For a BranchReference, get_reference should reutrn the location."""
316
 
        branch = self.make_branch('target')
317
 
        checkout = branch.create_checkout('checkout', lightweight=True)
318
 
        reference_url = branch.bzrdir.root_transport.abspath('') + '/'
319
 
        # if the api for create_checkout changes to return different checkout types
320
 
        # then this file read will fail.
321
 
        self.assertFileEqual(reference_url, 'checkout/.bzr/branch/location')
322
 
        self.assertEqual(reference_url,
323
 
            _mod_branch.BranchReferenceFormat().get_reference(checkout.bzrdir))
324
 
 
325
 
 
326
 
class TestHooks(TestCase):
327
 
 
328
 
    def test_constructor(self):
329
 
        """Check that creating a BranchHooks instance has the right defaults."""
330
 
        hooks = BranchHooks()
331
 
        self.assertTrue("set_rh" in hooks, "set_rh not in %s" % hooks)
332
 
        self.assertTrue("post_push" in hooks, "post_push not in %s" % hooks)
333
 
        self.assertTrue("post_commit" in hooks, "post_commit not in %s" % hooks)
334
 
        self.assertTrue("pre_commit" in hooks, "pre_commit not in %s" % hooks)
335
 
        self.assertTrue("post_pull" in hooks, "post_pull not in %s" % hooks)
336
 
        self.assertTrue("post_uncommit" in hooks, "post_uncommit not in %s" % hooks)
337
 
 
338
 
    def test_installed_hooks_are_BranchHooks(self):
339
 
        """The installed hooks object should be a BranchHooks."""
340
 
        # the installed hooks are saved in self._preserved_hooks.
341
 
        self.assertIsInstance(self._preserved_hooks[_mod_branch.Branch], BranchHooks)
342
 
 
343
 
 
344
 
class TestPullResult(TestCase):
345
 
 
346
 
    def test_pull_result_to_int(self):
347
 
        # to support old code, the pull result can be used as an int
348
 
        r = PullResult()
349
 
        r.old_revno = 10
350
 
        r.new_revno = 20
351
 
        # this usage of results is not recommended for new code (because it
352
 
        # doesn't describe very well what happened), but for api stability
353
 
        # it's still supported
354
 
        a = "%d revisions pulled" % r
355
 
        self.assertEqual(a, "10 revisions pulled")