~bzr-pqm/bzr/bzr.dev

« back to all changes in this revision

Viewing changes to bzrlib/tests/blackbox/test_branch.py

  • Committer: Canonical.com Patch Queue Manager
  • Date: 2010-02-11 04:02:41 UTC
  • mfrom: (5017.2.2 tariff)
  • Revision ID: pqm@pqm.ubuntu.com-20100211040241-w6n021dz0uus341n
(mbp) add import-tariff tests

Show diffs side-by-side

added added

removed removed

Lines of Context:
19
19
 
20
20
import os
21
21
 
22
 
from bzrlib import (branch, bzrdir, errors, repository)
 
22
from bzrlib import (
 
23
    branch,
 
24
    bzrdir,
 
25
    errors,
 
26
    repository,
 
27
    revision as _mod_revision,
 
28
    )
23
29
from bzrlib.repofmt.knitrepo import RepositoryFormatKnit1
24
30
from bzrlib.tests.blackbox import ExternalBase
25
 
from bzrlib.tests import HardlinkFeature
 
31
from bzrlib.tests import (
 
32
    KnownFailure,
 
33
    HardlinkFeature,
 
34
    )
26
35
from bzrlib.tests.test_sftp_transport import TestCaseWithSFTPServer
27
36
from bzrlib.urlutils import local_path_to_url, strip_trailing_slash
28
37
from bzrlib.workingtree import WorkingTree
49
58
        self.assertFalse(b._transport.has('branch-name'))
50
59
        b.bzrdir.open_workingtree().commit(message='foo', allow_pointless=True)
51
60
 
 
61
    def test_branch_switch_no_branch(self):
 
62
        # No branch in the current directory:
 
63
        #  => new branch will be created, but switch fails
 
64
        self.example_branch('a')
 
65
        self.make_repository('current')
 
66
        self.run_bzr_error(['No WorkingTree exists for'],
 
67
            'branch --switch ../a ../b', working_dir='current')
 
68
        a = branch.Branch.open('a')
 
69
        b = branch.Branch.open('b')
 
70
        self.assertEqual(a.last_revision(), b.last_revision())
 
71
 
 
72
    def test_branch_switch_no_wt(self):
 
73
        # No working tree in the current directory:
 
74
        #  => new branch will be created, but switch fails and the current
 
75
        #     branch is unmodified
 
76
        self.example_branch('a')
 
77
        self.make_branch('current')
 
78
        self.run_bzr_error(['No WorkingTree exists for'],
 
79
            'branch --switch ../a ../b', working_dir='current')
 
80
        a = branch.Branch.open('a')
 
81
        b = branch.Branch.open('b')
 
82
        self.assertEqual(a.last_revision(), b.last_revision())
 
83
        work = branch.Branch.open('current')
 
84
        self.assertEqual(work.last_revision(), _mod_revision.NULL_REVISION)
 
85
 
 
86
    def test_branch_switch_no_checkout(self):
 
87
        # Standalone branch in the current directory:
 
88
        #  => new branch will be created, but switch fails and the current
 
89
        #     branch is unmodified
 
90
        self.example_branch('a')
 
91
        self.make_branch_and_tree('current')
 
92
        self.run_bzr_error(['Cannot switch a branch, only a checkout'],
 
93
            'branch --switch ../a ../b', working_dir='current')
 
94
        a = branch.Branch.open('a')
 
95
        b = branch.Branch.open('b')
 
96
        self.assertEqual(a.last_revision(), b.last_revision())
 
97
        work = branch.Branch.open('current')
 
98
        self.assertEqual(work.last_revision(), _mod_revision.NULL_REVISION)
 
99
 
 
100
    def test_branch_switch_checkout(self):
 
101
        # Checkout in the current directory:
 
102
        #  => new branch will be created and checkout bound to the new branch
 
103
        self.example_branch('a')
 
104
        self.run_bzr('checkout a current')
 
105
        out, err = self.run_bzr('branch --switch ../a ../b', working_dir='current')
 
106
        a = branch.Branch.open('a')
 
107
        b = branch.Branch.open('b')
 
108
        self.assertEqual(a.last_revision(), b.last_revision())
 
109
        work = WorkingTree.open('current')
 
110
        self.assertEndsWith(work.branch.get_bound_location(), '/b/')
 
111
        self.assertContainsRe(err, "Switched to branch: .*/b/")
 
112
 
 
113
    def test_branch_switch_lightweight_checkout(self):
 
114
        # Lightweight checkout in the current directory:
 
115
        #  => new branch will be created and lightweight checkout pointed to
 
116
        #     the new branch
 
117
        self.example_branch('a')
 
118
        self.run_bzr('checkout --lightweight a current')
 
119
        out, err = self.run_bzr('branch --switch ../a ../b', working_dir='current')
 
120
        a = branch.Branch.open('a')
 
121
        b = branch.Branch.open('b')
 
122
        self.assertEqual(a.last_revision(), b.last_revision())
 
123
        work = WorkingTree.open('current')
 
124
        self.assertEndsWith(work.branch.base, '/b/')
 
125
        self.assertContainsRe(err, "Switched to branch: .*/b/")
 
126
 
52
127
    def test_branch_only_copies_history(self):
53
128
        # Knit branches should only push the history for the current revision.
54
129
        format = bzrdir.BzrDirMetaFormat1()
93
168
        self.build_tree(['source/file1'])
94
169
        source.add('file1')
95
170
        source.commit('added file')
96
 
        self.run_bzr(['branch', 'source', 'target', '--hardlink'])
 
171
        out, err = self.run_bzr(['branch', 'source', 'target', '--hardlink'])
97
172
        source_stat = os.stat('source/file1')
98
173
        target_stat = os.stat('target/file1')
99
174
        self.assertEqual(source_stat, target_stat)
134
209
        self.assertEqual('', out)
135
210
        self.assertEqual('bzr: ERROR: Already a branch: "b".\n', err)
136
211
 
 
212
    def test_branch_bind(self):
 
213
        self.example_branch('a')
 
214
        out, err = self.run_bzr('branch a b --bind')
 
215
        self.assertEndsWith(err, "New branch bound to a\n")
 
216
        b = branch.Branch.open('b')
 
217
        self.assertEndsWith(b.get_bound_location(), '/a/')
 
218
 
137
219
 
138
220
class TestBranchStacked(ExternalBase):
139
221
    """Tests for branch --stacked"""
140
222
 
141
 
    def check_shallow_branch(self, branch_revid, stacked_on):
142
 
        """Assert that the branch 'newbranch' has been published correctly.
143
 
 
144
 
        :param stacked_on: url of a branch this one is stacked upon.
145
 
        :param branch_revid: a revision id that should be the only
146
 
            revision present in the stacked branch, and it should not be in
147
 
            the reference branch.
148
 
        """
149
 
        new_branch = branch.Branch.open('newbranch')
150
 
        # The branch refers to the mainline
151
 
        self.assertEqual(stacked_on, new_branch.get_stacked_on_url())
152
 
        # and the branch's work was pushed
153
 
        self.assertTrue(new_branch.repository.has_revision(branch_revid))
154
 
        # The newly committed revision shoud be present in the stacked branch,
155
 
        # but not in the stacked-on branch.  Because stacking is set up by the
156
 
        # branch object, if we open the stacked branch's repository directly,
157
 
        # bypassing the branch, we see only what's in the stacked repository.
158
 
        stacked_repo = bzrdir.BzrDir.open('newbranch').open_repository()
159
 
        stacked_repo_revisions = set(stacked_repo.all_revision_ids())
160
 
        if len(stacked_repo_revisions) != 1:
161
 
            self.fail("wrong revisions in stacked repository: %r"
162
 
                % (stacked_repo_revisions,))
163
 
 
164
223
    def assertRevisionInRepository(self, repo_path, revid):
165
224
        """Check that a revision is in a repository, disregarding stacking."""
166
225
        repo = bzrdir.BzrDir.open(repo_path).open_repository()
187
246
            format='1.9')
188
247
        branch_tree.branch.set_stacked_on_url(trunk_tree.branch.base)
189
248
        # with some work on it
190
 
        branch_tree.commit('moar work plz')
 
249
        work_tree = trunk_tree.branch.bzrdir.sprout('local').open_workingtree()
 
250
        work_tree.commit('moar work plz')
 
251
        work_tree.branch.push(branch_tree.branch)
191
252
        # branching our local branch gives us a new stacked branch pointing at
192
253
        # mainline.
193
254
        out, err = self.run_bzr(['branch', 'branch', 'newbranch'])
194
255
        self.assertEqual('', out)
195
 
        self.assertEqual('Branched 1 revision(s).\n',
 
256
        self.assertEqual('Branched 2 revision(s).\n',
196
257
            err)
197
258
        # it should have preserved the branch format, and so it should be
198
259
        # capable of supporting stacking, but not actually have a stacked_on
211
272
            format='1.9')
212
273
        branch_tree.branch.set_stacked_on_url(trunk_tree.branch.base)
213
274
        # with some work on it
214
 
        branch_revid = branch_tree.commit('moar work plz')
 
275
        work_tree = trunk_tree.branch.bzrdir.sprout('local').open_workingtree()
 
276
        branch_revid = work_tree.commit('moar work plz')
 
277
        work_tree.branch.push(branch_tree.branch)
215
278
        # you can chain branches on from there
216
279
        out, err = self.run_bzr(['branch', 'branch', '--stacked', 'branch2'])
217
280
        self.assertEqual('', out)
220
283
        self.assertEqual(branch_tree.branch.base,
221
284
            branch.Branch.open('branch2').get_stacked_on_url())
222
285
        branch2_tree = WorkingTree.open('branch2')
223
 
        branch2_revid = branch2_tree.commit('work on second stacked branch')
 
286
        branch2_revid = work_tree.commit('work on second stacked branch')
 
287
        work_tree.branch.push(branch2_tree.branch)
224
288
        self.assertRevisionInRepository('branch2', branch2_revid)
225
289
        self.assertRevisionsInBranchRepository(
226
290
            [trunk_revid, branch_revid, branch2_revid],
239
303
        self.assertEqual('Created new stacked branch referring to %s.\n' %
240
304
            trunk_tree.branch.base, err)
241
305
        self.assertRevisionNotInRepository('newbranch', original_revid)
242
 
        new_tree = WorkingTree.open('newbranch')
243
 
        new_revid = new_tree.commit('new work')
244
 
        self.check_shallow_branch(new_revid, trunk_tree.branch.base)
 
306
        new_branch = branch.Branch.open('newbranch')
 
307
        self.assertEqual(trunk_tree.branch.base, new_branch.get_stacked_on_url())
245
308
 
246
309
    def test_branch_stacked_from_smart_server(self):
247
310
        # We can branch stacking on a smart server
318
381
            t.commit(message='commit %d' % count)
319
382
        tree2 = t.branch.bzrdir.sprout('feature', stacked=True
320
383
            ).open_workingtree()
321
 
        tree2.commit('feature change')
 
384
        local_tree = t.branch.bzrdir.sprout('local-working').open_workingtree()
 
385
        local_tree.commit('feature change')
 
386
        local_tree.branch.push(tree2.branch)
322
387
        self.reset_smart_call_log()
323
388
        out, err = self.run_bzr(['branch', self.get_url('feature'),
324
389
            'local-target'])