bzr branch
http://gegoxaren.bato24.eu/bzr/brz/remove-bazaar
4988.10.5
by John Arbash Meinel
Merge bzr.dev 5021 to resolve NEWS |
1 |
# Copyright (C) 2007-2010 Canonical Ltd
|
2246.1.3
by Robert Collins
New branch hooks: post_push, post_pull, post_commit, post_uncommit. These |
2 |
#
|
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.
|
|
7 |
#
|
|
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.
|
|
12 |
#
|
|
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
|
|
4183.7.1
by Sabin Iacob
update FSF mailing address |
15 |
# Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
|
2246.1.3
by Robert Collins
New branch hooks: post_push, post_pull, post_commit, post_uncommit. These |
16 |
|
17 |
"""Tests for the contract of commit on branches."""
|
|
18 |
||
6622.1.34
by Jelmer Vernooij
Rename brzlib => breezy. |
19 |
from breezy import ( |
5010.2.12
by Vincent Ladeuil
Fix imports in per_branch/test_commit.py. |
20 |
branch, |
21 |
delta, |
|
22 |
errors, |
|
23 |
revision, |
|
24 |
transport, |
|
25 |
)
|
|
6622.1.34
by Jelmer Vernooij
Rename brzlib => breezy. |
26 |
from breezy.tests import per_branch |
5010.2.12
by Vincent Ladeuil
Fix imports in per_branch/test_commit.py. |
27 |
|
28 |
||
29 |
class TestCommit(per_branch.TestCaseWithBranch): |
|
2246.1.3
by Robert Collins
New branch hooks: post_push, post_pull, post_commit, post_uncommit. These |
30 |
|
31 |
def test_commit_nicks(self): |
|
32 |
"""Nicknames are committed to the revision""" |
|
5609.9.4
by Vincent Ladeuil
Use self.get_transport instead of transport.get_transport where possible. |
33 |
self.get_transport().mkdir('bzr.dev') |
2246.1.3
by Robert Collins
New branch hooks: post_push, post_pull, post_commit, post_uncommit. These |
34 |
wt = self.make_branch_and_tree('bzr.dev') |
35 |
branch = wt.branch |
|
36 |
branch.nick = "My happy branch" |
|
37 |
wt.commit('My commit respect da nick.') |
|
38 |
committed = branch.repository.get_revision(branch.last_revision()) |
|
6820.1.1
by Jelmer Vernooij
add RepositoryFormat.supports_storing_branch_nick. |
39 |
if branch.repository._format.supports_storing_branch_nick: |
40 |
self.assertEqual(committed.properties["branch-nick"], |
|
41 |
"My happy branch") |
|
42 |
else: |
|
43 |
self.assertNotIn("branch-nick", committed.properties) |
|
2246.1.3
by Robert Collins
New branch hooks: post_push, post_pull, post_commit, post_uncommit. These |
44 |
|
45 |
||
5010.2.12
by Vincent Ladeuil
Fix imports in per_branch/test_commit.py. |
46 |
class TestCommitHook(per_branch.TestCaseWithBranch): |
2246.1.3
by Robert Collins
New branch hooks: post_push, post_pull, post_commit, post_uncommit. These |
47 |
|
48 |
def setUp(self): |
|
49 |
self.hook_calls = [] |
|
5010.2.12
by Vincent Ladeuil
Fix imports in per_branch/test_commit.py. |
50 |
super(TestCommitHook, self).setUp() |
2246.1.3
by Robert Collins
New branch hooks: post_push, post_pull, post_commit, post_uncommit. These |
51 |
|
52 |
def capture_post_commit_hook(self, local, master, old_revno, |
|
7143.15.2
by Jelmer Vernooij
Run autopep8. |
53 |
old_revid, new_revno, new_revid): |
2246.1.3
by Robert Collins
New branch hooks: post_push, post_pull, post_commit, post_uncommit. These |
54 |
"""Capture post commit hook calls to self.hook_calls. |
3943.8.1
by Marius Kruger
remove all trailing whitespace from bzr source |
55 |
|
2246.1.3
by Robert Collins
New branch hooks: post_push, post_pull, post_commit, post_uncommit. These |
56 |
The call is logged, as is some state of the two branches.
|
57 |
"""
|
|
58 |
if local: |
|
59 |
local_locked = local.is_locked() |
|
60 |
local_base = local.base |
|
61 |
else: |
|
62 |
local_locked = None |
|
63 |
local_base = None |
|
64 |
self.hook_calls.append( |
|
65 |
('post_commit', local_base, master.base, old_revno, old_revid, |
|
66 |
new_revno, new_revid, local_locked, master.is_locked())) |
|
67 |
||
2659.3.1
by NamNguyen
``Branch.hooks`` now supports ``pre_commit`` hook. |
68 |
def capture_pre_commit_hook(self, local, master, old_revno, old_revid, |
2659.3.9
by NamNguyen
branch.py: |
69 |
new_revno, new_revid, |
70 |
tree_delta, future_tree): |
|
2659.3.1
by NamNguyen
``Branch.hooks`` now supports ``pre_commit`` hook. |
71 |
self.hook_calls.append(('pre_commit', old_revno, old_revid, |
2659.3.9
by NamNguyen
branch.py: |
72 |
new_revno, new_revid, tree_delta)) |
2659.3.1
by NamNguyen
``Branch.hooks`` now supports ``pre_commit`` hook. |
73 |
|
2246.1.3
by Robert Collins
New branch hooks: post_push, post_pull, post_commit, post_uncommit. These |
74 |
def test_post_commit_to_origin(self): |
75 |
tree = self.make_branch_and_memory_tree('branch') |
|
5010.2.12
by Vincent Ladeuil
Fix imports in per_branch/test_commit.py. |
76 |
branch.Branch.hooks.install_named_hook( |
77 |
'post_commit', self.capture_post_commit_hook, None) |
|
2246.1.3
by Robert Collins
New branch hooks: post_push, post_pull, post_commit, post_uncommit. These |
78 |
tree.lock_write() |
79 |
tree.add('') |
|
80 |
revid = tree.commit('a revision') |
|
81 |
# should have had one notification, from origin, and
|
|
82 |
# have the branch locked at notification time.
|
|
83 |
self.assertEqual([ |
|
5010.2.12
by Vincent Ladeuil
Fix imports in per_branch/test_commit.py. |
84 |
('post_commit', None, tree.branch.base, 0, revision.NULL_REVISION, |
85 |
1, revid, None, True) |
|
2246.1.3
by Robert Collins
New branch hooks: post_push, post_pull, post_commit, post_uncommit. These |
86 |
],
|
87 |
self.hook_calls) |
|
88 |
tree.unlock() |
|
89 |
||
90 |
def test_post_commit_bound(self): |
|
91 |
master = self.make_branch('master') |
|
92 |
tree = self.make_branch_and_memory_tree('local') |
|
93 |
try: |
|
94 |
tree.branch.bind(master) |
|
95 |
except errors.UpgradeRequired: |
|
96 |
# cant bind this format, the test is irrelevant.
|
|
97 |
return
|
|
5010.2.12
by Vincent Ladeuil
Fix imports in per_branch/test_commit.py. |
98 |
branch.Branch.hooks.install_named_hook( |
99 |
'post_commit', self.capture_post_commit_hook, None) |
|
2246.1.3
by Robert Collins
New branch hooks: post_push, post_pull, post_commit, post_uncommit. These |
100 |
tree.lock_write() |
101 |
tree.add('') |
|
102 |
revid = tree.commit('a revision') |
|
103 |
# with a bound branch, local is set.
|
|
104 |
self.assertEqual([ |
|
5010.2.12
by Vincent Ladeuil
Fix imports in per_branch/test_commit.py. |
105 |
('post_commit', tree.branch.base, master.base, 0, |
106 |
revision.NULL_REVISION, 1, revid, True, True) |
|
2246.1.3
by Robert Collins
New branch hooks: post_push, post_pull, post_commit, post_uncommit. These |
107 |
],
|
108 |
self.hook_calls) |
|
109 |
tree.unlock() |
|
110 |
||
111 |
def test_post_commit_not_to_origin(self): |
|
112 |
tree = self.make_branch_and_memory_tree('branch') |
|
7358.11.3
by Jelmer Vernooij
TreeDelta holds TreeChange objects rather than tuples of various sizes. |
113 |
with tree.lock_write(): |
114 |
tree.add('') |
|
115 |
revid = tree.commit('first revision') |
|
116 |
branch.Branch.hooks.install_named_hook( |
|
117 |
'post_commit', self.capture_post_commit_hook, None) |
|
118 |
revid2 = tree.commit('second revision') |
|
119 |
# having committed from up the branch, we should get the
|
|
120 |
# before and after revnos and revids correctly.
|
|
121 |
self.assertEqual([ |
|
122 |
('post_commit', None, tree.branch.base, 1, revid, 2, revid2, |
|
123 |
None, True) |
|
124 |
],
|
|
125 |
self.hook_calls) |
|
126 |
||
127 |
def get_rootfull_delta(self, repository, revid): |
|
128 |
tree = repository.revision_tree(revid) |
|
129 |
with repository.lock_read(): |
|
130 |
parent_revid = repository.get_parent_map([revid])[revid][0] |
|
131 |
basis_tree = repository.revision_tree(parent_revid) |
|
132 |
tree = repository.revision_tree(revid) |
|
133 |
return tree.changes_from(basis_tree, include_root=True) |
|
3943.8.1
by Marius Kruger
remove all trailing whitespace from bzr source |
134 |
|
2659.3.1
by NamNguyen
``Branch.hooks`` now supports ``pre_commit`` hook. |
135 |
def test_pre_commit_passes(self): |
136 |
tree = self.make_branch_and_memory_tree('branch') |
|
7358.11.3
by Jelmer Vernooij
TreeDelta holds TreeChange objects rather than tuples of various sizes. |
137 |
with tree.lock_write(): |
138 |
tree.add('') |
|
139 |
branch.Branch.hooks.install_named_hook( |
|
140 |
"pre_commit", self.capture_pre_commit_hook, None) |
|
141 |
revid1 = tree.commit('first revision') |
|
142 |
revid2 = tree.commit('second revision') |
|
143 |
root_delta = self.get_rootfull_delta(tree.branch.repository, revid1) |
|
144 |
empty_delta = tree.branch.repository.get_revision_delta(revid2) |
|
145 |
self.assertEqual([ |
|
146 |
('pre_commit', 0, revision.NULL_REVISION, 1, revid1, root_delta), |
|
147 |
('pre_commit', 1, revid1, 2, revid2, empty_delta) |
|
148 |
],
|
|
149 |
self.hook_calls) |
|
2659.3.1
by NamNguyen
``Branch.hooks`` now supports ``pre_commit`` hook. |
150 |
|
151 |
def test_pre_commit_fails(self): |
|
152 |
tree = self.make_branch_and_memory_tree('branch') |
|
7358.11.3
by Jelmer Vernooij
TreeDelta holds TreeChange objects rather than tuples of various sizes. |
153 |
with tree.lock_write(): |
154 |
tree.add('') |
|
155 |
||
156 |
class PreCommitException(Exception): |
|
157 |
||
158 |
def __init__(self, revid): |
|
159 |
self.revid = revid |
|
160 |
||
161 |
def hook_func(local, master, |
|
162 |
old_revno, old_revid, new_revno, new_revid, |
|
163 |
tree_delta, future_tree): |
|
164 |
raise PreCommitException(new_revid) |
|
165 |
branch.Branch.hooks.install_named_hook( |
|
166 |
"pre_commit", self.capture_pre_commit_hook, None) |
|
167 |
branch.Branch.hooks.install_named_hook("pre_commit", hook_func, None) |
|
168 |
revids = [None, None, None] |
|
169 |
# this commit will raise an exception
|
|
170 |
# so the commit is rolled back and revno unchanged
|
|
171 |
err = self.assertRaises(PreCommitException, tree.commit, 'message') |
|
172 |
# we have to record the revid to use in assertEqual later
|
|
173 |
revids[0] = err.revid |
|
174 |
# unregister all pre_commit hooks
|
|
175 |
branch.Branch.hooks["pre_commit"] = [] |
|
176 |
# and re-register the capture hook
|
|
177 |
branch.Branch.hooks.install_named_hook( |
|
178 |
"pre_commit", self.capture_pre_commit_hook, None) |
|
179 |
# now these commits should go through
|
|
180 |
for i in range(1, 3): |
|
181 |
revids[i] = tree.commit('message') |
|
182 |
self.assertEqual([ |
|
183 |
('pre_commit', 0, revision.NULL_REVISION, |
|
184 |
1, revids[0], self.get_rootfull_delta(tree.branch.repository, revids[0])), |
|
185 |
('pre_commit', 0, revision.NULL_REVISION, |
|
186 |
1, revids[1], self.get_rootfull_delta(tree.branch.repository, revids[1])), |
|
187 |
('pre_commit', 1, revids[1], 2, revids[2], |
|
188 |
self.get_rootfull_delta(tree.branch.repository, revids[2])) |
|
189 |
],
|
|
190 |
self.hook_calls) |
|
2659.3.9
by NamNguyen
branch.py: |
191 |
|
192 |
def test_pre_commit_delta(self): |
|
193 |
# This tests the TreeDelta object passed to pre_commit hook.
|
|
194 |
# This does not try to validate data correctness in the delta.
|
|
2659.3.6
by NamNguyen
branch_implementations/test_commit.py: |
195 |
self.build_tree(['rootfile', 'dir/', 'dir/subfile']) |
196 |
tree = self.make_branch_and_tree('.') |
|
6844.1.1
by Jelmer Vernooij
Many more foreign branch fixes. |
197 |
with tree.lock_write(): |
2659.3.9
by NamNguyen
branch.py: |
198 |
# setting up a playground
|
6793.4.1
by Jelmer Vernooij
Improve set_root_id handling. |
199 |
tree.add('rootfile') |
200 |
rootfile_id = tree.path2id('rootfile') |
|
6973.6.2
by Jelmer Vernooij
Fix more tests. |
201 |
tree.put_file_bytes_non_atomic('rootfile', b'abc') |
6793.4.1
by Jelmer Vernooij
Improve set_root_id handling. |
202 |
tree.add('dir') |
203 |
dir_id = tree.path2id('dir') |
|
204 |
tree.add('dir/subfile') |
|
205 |
dir_subfile_id = tree.path2id('dir/subfile') |
|
6973.6.2
by Jelmer Vernooij
Fix more tests. |
206 |
tree.put_file_bytes_non_atomic('to_be_unversioned', b'blah') |
6883.5.20
by Jelmer Vernooij
Fix another test for trees without rename tracking. |
207 |
tree.add(['to_be_unversioned']) |
6793.4.1
by Jelmer Vernooij
Improve set_root_id handling. |
208 |
to_be_unversioned_id = tree.path2id('to_be_unversioned') |
6973.6.2
by Jelmer Vernooij
Fix more tests. |
209 |
tree.put_file_bytes_non_atomic('dir/subfile', b'def') |
2659.3.9
by NamNguyen
branch.py: |
210 |
revid1 = tree.commit('first revision') |
3943.8.1
by Marius Kruger
remove all trailing whitespace from bzr source |
211 |
|
6844.1.1
by Jelmer Vernooij
Many more foreign branch fixes. |
212 |
with tree.lock_write(): |
2659.3.9
by NamNguyen
branch.py: |
213 |
# making changes
|
6973.6.2
by Jelmer Vernooij
Fix more tests. |
214 |
tree.put_file_bytes_non_atomic('rootfile', b'jkl') |
2659.3.9
by NamNguyen
branch.py: |
215 |
tree.rename_one('dir/subfile', 'dir/subfile_renamed') |
6809.4.25
by Jelmer Vernooij
Add paths argument to .unversion. |
216 |
tree.unversion(['to_be_unversioned']) |
6793.4.1
by Jelmer Vernooij
Improve set_root_id handling. |
217 |
tree.mkdir('added_dir') |
218 |
added_dir_id = tree.path2id('added_dir') |
|
2659.3.9
by NamNguyen
branch.py: |
219 |
# start to capture pre_commit delta
|
5010.2.12
by Vincent Ladeuil
Fix imports in per_branch/test_commit.py. |
220 |
branch.Branch.hooks.install_named_hook( |
221 |
"pre_commit", self.capture_pre_commit_hook, None) |
|
2659.3.9
by NamNguyen
branch.py: |
222 |
revid2 = tree.commit('second revision') |
3943.8.1
by Marius Kruger
remove all trailing whitespace from bzr source |
223 |
|
2659.3.9
by NamNguyen
branch.py: |
224 |
self.assertEqual([('pre_commit', 1, revid1, 2, revid2, |
7358.11.3
by Jelmer Vernooij
TreeDelta holds TreeChange objects rather than tuples of various sizes. |
225 |
self.get_rootfull_delta(tree.branch.repository, revid2))], self.hook_calls) |