~bzr-pqm/bzr/bzr.dev

« back to all changes in this revision

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

  • Committer: Canonical.com Patch Queue Manager
  • Date: 2010-02-08 10:14:23 UTC
  • mfrom: (5013.1.1 integration)
  • Revision ID: pqm@pqm.ubuntu.com-20100208101423-q81doa9rua7c3x6t
(vila) Fix a bunch of test imports

Show diffs side-by-side

added added

removed removed

Lines of Context:
16
16
 
17
17
"""Tests for the contract of commit on branches."""
18
18
 
19
 
from bzrlib.branch import Branch
20
 
from bzrlib import errors
21
 
from bzrlib.tests.per_branch.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):
 
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):
28
30
 
29
31
    def test_commit_nicks(self):
30
32
        """Nicknames are committed to the revision"""
31
 
        get_transport(self.get_url()).mkdir('bzr.dev')
 
33
        transport.get_transport(self.get_url()).mkdir('bzr.dev')
32
34
        wt = self.make_branch_and_tree('bzr.dev')
33
35
        branch = wt.branch
34
36
        branch.nick = "My happy branch"
38
40
                         "My happy branch")
39
41
 
40
42
 
41
 
class TestCommitHook(TestCaseWithBranch):
 
43
class TestCommitHook(per_branch.TestCaseWithBranch):
42
44
 
43
45
    def setUp(self):
44
46
        self.hook_calls = []
45
 
        TestCaseWithBranch.setUp(self)
 
47
        super(TestCommitHook, self).setUp()
46
48
 
47
49
    def capture_post_commit_hook(self, local, master, old_revno,
48
50
        old_revid, new_revno, new_revid):
68
70
 
69
71
    def test_post_commit_to_origin(self):
70
72
        tree = self.make_branch_and_memory_tree('branch')
71
 
        Branch.hooks.install_named_hook('post_commit',
72
 
            self.capture_post_commit_hook, None)
 
73
        branch.Branch.hooks.install_named_hook(
 
74
            'post_commit', self.capture_post_commit_hook, None)
73
75
        tree.lock_write()
74
76
        tree.add('')
75
77
        revid = tree.commit('a revision')
76
78
        # should have had one notification, from origin, and
77
79
        # have the branch locked at notification time.
78
80
        self.assertEqual([
79
 
            ('post_commit', None, tree.branch.base, 0, NULL_REVISION, 1, revid,
80
 
             None, True)
 
81
            ('post_commit', None, tree.branch.base, 0, revision.NULL_REVISION,
 
82
             1, revid, None, True)
81
83
            ],
82
84
            self.hook_calls)
83
85
        tree.unlock()
90
92
        except errors.UpgradeRequired:
91
93
            # cant bind this format, the test is irrelevant.
92
94
            return
93
 
        Branch.hooks.install_named_hook('post_commit',
94
 
            self.capture_post_commit_hook, None)
 
95
        branch.Branch.hooks.install_named_hook(
 
96
            'post_commit', self.capture_post_commit_hook, None)
95
97
        tree.lock_write()
96
98
        tree.add('')
97
99
        revid = tree.commit('a revision')
98
100
        # with a bound branch, local is set.
99
101
        self.assertEqual([
100
 
            ('post_commit', tree.branch.base, master.base, 0, NULL_REVISION,
101
 
             1, revid, True, True)
 
102
            ('post_commit', tree.branch.base, master.base, 0,
 
103
             revision.NULL_REVISION, 1, revid, True, True)
102
104
            ],
103
105
            self.hook_calls)
104
106
        tree.unlock()
108
110
        tree.lock_write()
109
111
        tree.add('')
110
112
        revid = tree.commit('first revision')
111
 
        Branch.hooks.install_named_hook('post_commit',
112
 
            self.capture_post_commit_hook, None)
 
113
        branch.Branch.hooks.install_named_hook(
 
114
            'post_commit', self.capture_post_commit_hook, None)
113
115
        revid2 = tree.commit('second revision')
114
116
        # having committed from up the branch, we should get the
115
117
        # before and after revnos and revids correctly.
121
123
        tree.unlock()
122
124
 
123
125
    def test_pre_commit_passes(self):
124
 
        empty_delta = TreeDelta()
125
 
        root_delta = TreeDelta()
 
126
        empty_delta = delta.TreeDelta()
 
127
        root_delta = delta.TreeDelta()
126
128
        tree = self.make_branch_and_memory_tree('branch')
127
129
        tree.lock_write()
128
130
        tree.add('')
129
131
        root_delta.added = [('', tree.path2id(''), 'directory')]
130
 
        Branch.hooks.install_named_hook("pre_commit",
131
 
            self.capture_pre_commit_hook, None)
 
132
        branch.Branch.hooks.install_named_hook(
 
133
            "pre_commit", self.capture_pre_commit_hook, None)
132
134
        revid1 = tree.commit('first revision')
133
135
        revid2 = tree.commit('second revision')
134
136
        self.assertEqual([
135
 
            ('pre_commit', 0, NULL_REVISION, 1, revid1, root_delta),
 
137
            ('pre_commit', 0, revision.NULL_REVISION, 1, revid1, root_delta),
136
138
            ('pre_commit', 1, revid1, 2, revid2, empty_delta)
137
139
            ],
138
140
            self.hook_calls)
139
141
        tree.unlock()
140
142
 
141
143
    def test_pre_commit_fails(self):
142
 
        empty_delta = TreeDelta()
143
 
        root_delta = TreeDelta()
 
144
        empty_delta = delta.TreeDelta()
 
145
        root_delta = delta.TreeDelta()
144
146
        tree = self.make_branch_and_memory_tree('branch')
145
147
        tree.lock_write()
146
148
        tree.add('')
150
152
                      old_revno, old_revid, new_revno, new_revid,
151
153
                      tree_delta, future_tree):
152
154
            raise PreCommitException(new_revid)
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)
 
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)
156
158
        revids = [None, None, None]
157
159
        # this commit will raise an exception
158
160
        # so the commit is rolled back and revno unchanged
160
162
        # we have to record the revid to use in assertEqual later
161
163
        revids[0] = str(err)
162
164
        # unregister all pre_commit hooks
163
 
        Branch.hooks["pre_commit"] = []
 
165
        branch.Branch.hooks["pre_commit"] = []
164
166
        # and re-register the capture hook
165
 
        Branch.hooks.install_named_hook("pre_commit",
166
 
            self.capture_pre_commit_hook, None)
 
167
        branch.Branch.hooks.install_named_hook(
 
168
            "pre_commit", self.capture_pre_commit_hook, None)
167
169
        # now these commits should go through
168
170
        for i in range(1, 3):
169
171
            revids[i] = tree.commit('message')
170
172
        self.assertEqual([
171
 
            ('pre_commit', 0, NULL_REVISION, 1, revids[0], root_delta),
172
 
            ('pre_commit', 0, NULL_REVISION, 1, revids[1], root_delta),
 
173
            ('pre_commit', 0, revision.NULL_REVISION, 1, revids[0], root_delta),
 
174
            ('pre_commit', 0, revision.NULL_REVISION, 1, revids[1], root_delta),
173
175
            ('pre_commit', 1, revids[1], 2, revids[2], empty_delta)
174
176
            ],
175
177
            self.hook_calls)
202
204
            tree.unversion(['to_be_unversioned_id'])
203
205
            tree.mkdir('added_dir', 'added_dir_id')
204
206
            # start to capture pre_commit delta
205
 
            Branch.hooks.install_named_hook("pre_commit",
206
 
                self.capture_pre_commit_hook, None)
 
207
            branch.Branch.hooks.install_named_hook(
 
208
                "pre_commit", self.capture_pre_commit_hook, None)
207
209
            revid2 = tree.commit('second revision')
208
210
        finally:
209
211
            tree.unlock()
210
212
 
211
 
        expected_delta = TreeDelta()
 
213
        expected_delta = delta.TreeDelta()
212
214
        expected_delta.added = [('added_dir', 'added_dir_id', 'directory')]
213
215
        expected_delta.removed = [('to_be_unversioned',
214
216
                                   'to_be_unversioned_id', 'directory')]