~bzr-pqm/bzr/bzr.dev

« back to all changes in this revision

Viewing changes to bzrlib/tests/branch_implementations/test_commit.py

  • Committer: John Arbash Meinel
  • Author(s): Mark Hammond
  • Date: 2008-09-09 17:02:21 UTC
  • mto: This revision was merged to the branch mainline in revision 3697.
  • Revision ID: john@arbash-meinel.com-20080909170221-svim3jw2mrz0amp3
An updated transparent icon for bzr.

Show diffs side-by-side

added added

removed removed

Lines of Context:
1
 
# Copyright (C) 2007-2010 Canonical Ltd
 
1
# Copyright (C) 2007 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
12
12
#
13
13
# You should have received a copy of the GNU General Public License
14
14
# along with this program; if not, write to the Free Software
15
 
# Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
 
15
# Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307  USA
16
16
 
17
17
"""Tests for the contract of commit on branches."""
18
18
 
19
 
from bzrlib import (
20
 
    branch,
21
 
    delta,
22
 
    errors,
23
 
    revision,
24
 
    transport,
25
 
    )
26
 
from bzrlib.tests import per_branch
27
 
 
28
 
 
29
 
class TestCommit(per_branch.TestCaseWithBranch):
 
19
from bzrlib.branch import Branch
 
20
from bzrlib import errors
 
21
from bzrlib.tests.branch_implementations.test_branch import TestCaseWithBranch
 
22
from bzrlib.revision import NULL_REVISION
 
23
from bzrlib.transport import get_transport
 
24
from bzrlib.delta import TreeDelta
 
25
 
 
26
 
 
27
class TestCommit(TestCaseWithBranch):
30
28
 
31
29
    def test_commit_nicks(self):
32
30
        """Nicknames are committed to the revision"""
33
 
        self.get_transport().mkdir('bzr.dev')
 
31
        get_transport(self.get_url()).mkdir('bzr.dev')
34
32
        wt = self.make_branch_and_tree('bzr.dev')
35
33
        branch = wt.branch
36
34
        branch.nick = "My happy branch"
37
35
        wt.commit('My commit respect da nick.')
38
36
        committed = branch.repository.get_revision(branch.last_revision())
39
 
        self.assertEqual(committed.properties["branch-nick"],
 
37
        self.assertEqual(committed.properties["branch-nick"], 
40
38
                         "My happy branch")
41
39
 
42
40
 
43
 
class TestCommitHook(per_branch.TestCaseWithBranch):
 
41
class TestCommitHook(TestCaseWithBranch):
44
42
 
45
43
    def setUp(self):
46
44
        self.hook_calls = []
47
 
        super(TestCommitHook, self).setUp()
 
45
        TestCaseWithBranch.setUp(self)
48
46
 
49
47
    def capture_post_commit_hook(self, local, master, old_revno,
50
48
        old_revid, new_revno, new_revid):
51
49
        """Capture post commit hook calls to self.hook_calls.
52
 
 
 
50
        
53
51
        The call is logged, as is some state of the two branches.
54
52
        """
55
53
        if local:
70
68
 
71
69
    def test_post_commit_to_origin(self):
72
70
        tree = self.make_branch_and_memory_tree('branch')
73
 
        branch.Branch.hooks.install_named_hook(
74
 
            'post_commit', self.capture_post_commit_hook, None)
 
71
        Branch.hooks.install_named_hook('post_commit',
 
72
            self.capture_post_commit_hook, None)
75
73
        tree.lock_write()
76
74
        tree.add('')
77
75
        revid = tree.commit('a revision')
78
76
        # should have had one notification, from origin, and
79
77
        # have the branch locked at notification time.
80
78
        self.assertEqual([
81
 
            ('post_commit', None, tree.branch.base, 0, revision.NULL_REVISION,
82
 
             1, revid, None, True)
 
79
            ('post_commit', None, tree.branch.base, 0, NULL_REVISION, 1, revid,
 
80
             None, True)
83
81
            ],
84
82
            self.hook_calls)
85
83
        tree.unlock()
92
90
        except errors.UpgradeRequired:
93
91
            # cant bind this format, the test is irrelevant.
94
92
            return
95
 
        branch.Branch.hooks.install_named_hook(
96
 
            'post_commit', self.capture_post_commit_hook, None)
 
93
        Branch.hooks.install_named_hook('post_commit',
 
94
            self.capture_post_commit_hook, None)
97
95
        tree.lock_write()
98
96
        tree.add('')
99
97
        revid = tree.commit('a revision')
100
98
        # with a bound branch, local is set.
101
99
        self.assertEqual([
102
 
            ('post_commit', tree.branch.base, master.base, 0,
103
 
             revision.NULL_REVISION, 1, revid, True, True)
 
100
            ('post_commit', tree.branch.base, master.base, 0, NULL_REVISION,
 
101
             1, revid, True, True)
104
102
            ],
105
103
            self.hook_calls)
106
104
        tree.unlock()
110
108
        tree.lock_write()
111
109
        tree.add('')
112
110
        revid = tree.commit('first revision')
113
 
        branch.Branch.hooks.install_named_hook(
114
 
            'post_commit', self.capture_post_commit_hook, None)
 
111
        Branch.hooks.install_named_hook('post_commit',
 
112
            self.capture_post_commit_hook, None)
115
113
        revid2 = tree.commit('second revision')
116
114
        # having committed from up the branch, we should get the
117
115
        # before and after revnos and revids correctly.
121
119
            ],
122
120
            self.hook_calls)
123
121
        tree.unlock()
124
 
 
 
122
    
125
123
    def test_pre_commit_passes(self):
126
 
        empty_delta = delta.TreeDelta()
127
 
        root_delta = delta.TreeDelta()
 
124
        empty_delta = TreeDelta()
 
125
        root_delta = TreeDelta()
128
126
        tree = self.make_branch_and_memory_tree('branch')
129
127
        tree.lock_write()
130
128
        tree.add('')
131
129
        root_delta.added = [('', tree.path2id(''), 'directory')]
132
 
        branch.Branch.hooks.install_named_hook(
133
 
            "pre_commit", self.capture_pre_commit_hook, None)
 
130
        Branch.hooks.install_named_hook("pre_commit",
 
131
            self.capture_pre_commit_hook, None)
134
132
        revid1 = tree.commit('first revision')
135
133
        revid2 = tree.commit('second revision')
136
134
        self.assertEqual([
137
 
            ('pre_commit', 0, revision.NULL_REVISION, 1, revid1, root_delta),
 
135
            ('pre_commit', 0, NULL_REVISION, 1, revid1, root_delta),
138
136
            ('pre_commit', 1, revid1, 2, revid2, empty_delta)
139
137
            ],
140
138
            self.hook_calls)
141
139
        tree.unlock()
142
140
 
143
141
    def test_pre_commit_fails(self):
144
 
        empty_delta = delta.TreeDelta()
145
 
        root_delta = delta.TreeDelta()
 
142
        empty_delta = TreeDelta()
 
143
        root_delta = TreeDelta()
146
144
        tree = self.make_branch_and_memory_tree('branch')
147
145
        tree.lock_write()
148
146
        tree.add('')
152
150
                      old_revno, old_revid, new_revno, new_revid,
153
151
                      tree_delta, future_tree):
154
152
            raise PreCommitException(new_revid)
155
 
        branch.Branch.hooks.install_named_hook(
156
 
            "pre_commit", self.capture_pre_commit_hook, None)
157
 
        branch.Branch.hooks.install_named_hook("pre_commit", hook_func, None)
 
153
        Branch.hooks.install_named_hook("pre_commit",
 
154
            self.capture_pre_commit_hook, None)
 
155
        Branch.hooks.install_named_hook("pre_commit", hook_func, None)
158
156
        revids = [None, None, None]
159
157
        # this commit will raise an exception
160
158
        # so the commit is rolled back and revno unchanged
162
160
        # we have to record the revid to use in assertEqual later
163
161
        revids[0] = str(err)
164
162
        # unregister all pre_commit hooks
165
 
        branch.Branch.hooks["pre_commit"] = []
 
163
        Branch.hooks["pre_commit"] = []
166
164
        # and re-register the capture hook
167
 
        branch.Branch.hooks.install_named_hook(
168
 
            "pre_commit", self.capture_pre_commit_hook, None)
 
165
        Branch.hooks.install_named_hook("pre_commit",
 
166
            self.capture_pre_commit_hook, None)
169
167
        # now these commits should go through
170
168
        for i in range(1, 3):
171
169
            revids[i] = tree.commit('message')
172
170
        self.assertEqual([
173
 
            ('pre_commit', 0, revision.NULL_REVISION, 1, revids[0], root_delta),
174
 
            ('pre_commit', 0, revision.NULL_REVISION, 1, revids[1], root_delta),
 
171
            ('pre_commit', 0, NULL_REVISION, 1, revids[0], root_delta),
 
172
            ('pre_commit', 0, NULL_REVISION, 1, revids[1], root_delta),
175
173
            ('pre_commit', 1, revids[1], 2, revids[2], empty_delta)
176
174
            ],
177
175
            self.hook_calls)
195
193
            revid1 = tree.commit('first revision')
196
194
        finally:
197
195
            tree.unlock()
198
 
 
 
196
        
199
197
        tree.lock_write()
200
198
        try:
201
199
            # making changes
204
202
            tree.unversion(['to_be_unversioned_id'])
205
203
            tree.mkdir('added_dir', 'added_dir_id')
206
204
            # start to capture pre_commit delta
207
 
            branch.Branch.hooks.install_named_hook(
208
 
                "pre_commit", self.capture_pre_commit_hook, None)
 
205
            Branch.hooks.install_named_hook("pre_commit",
 
206
                self.capture_pre_commit_hook, None)
209
207
            revid2 = tree.commit('second revision')
210
208
        finally:
211
209
            tree.unlock()
212
 
 
213
 
        expected_delta = delta.TreeDelta()
 
210
        
 
211
        expected_delta = TreeDelta()
214
212
        expected_delta.added = [('added_dir', 'added_dir_id', 'directory')]
215
213
        expected_delta.removed = [('to_be_unversioned',
216
214
                                   'to_be_unversioned_id', 'directory')]