~bzr-pqm/bzr/bzr.dev

1246 by Martin Pool
- add very simple commit tests
1
# Copyright (C) 2005 by Canonical Ltd
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
15
# Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307  USA
16
17
18
import os
19
1472 by Robert Collins
post commit hook, first pass implementation
20
import bzrlib
1534.4.26 by Robert Collins
Move working tree initialisation out from Branch.initialize, deprecated Branch.initialize to Branch.create.
21
from bzrlib.tests import TestCaseWithTransport
1246 by Martin Pool
- add very simple commit tests
22
from bzrlib.branch import Branch
1185.16.72 by Martin Pool
[merge] from robert and fix up tests
23
from bzrlib.workingtree import WorkingTree
1246 by Martin Pool
- add very simple commit tests
24
from bzrlib.commit import Commit
1442.1.60 by Robert Collins
gpg sign commits if the policy says we need to
25
from bzrlib.config import BranchConfig
1442.1.62 by Robert Collins
Allow creation of testaments from uncommitted data, and use that to get signatures before committing revisions.
26
from bzrlib.errors import PointlessCommit, BzrError, SigningFailed
1246 by Martin Pool
- add very simple commit tests
27
28
1257 by Martin Pool
doc
29
# TODO: Test commit with some added, and added-but-missing files
30
1442.1.60 by Robert Collins
gpg sign commits if the policy says we need to
31
class MustSignConfig(BranchConfig):
32
33
    def signature_needed(self):
34
        return True
35
36
    def gpg_signing_command(self):
37
        return ['cat', '-']
38
39
1472 by Robert Collins
post commit hook, first pass implementation
40
class BranchWithHooks(BranchConfig):
41
42
    def post_commit(self):
43
        return "bzrlib.ahook bzrlib.ahook"
44
45
1534.4.26 by Robert Collins
Move working tree initialisation out from Branch.initialize, deprecated Branch.initialize to Branch.create.
46
class TestCommit(TestCaseWithTransport):
1390 by Robert Collins
pair programming worx... merge integration and weave
47
1246 by Martin Pool
- add very simple commit tests
48
    def test_simple_commit(self):
49
        """Commit and check two versions of a single file."""
1534.4.26 by Robert Collins
Move working tree initialisation out from Branch.initialize, deprecated Branch.initialize to Branch.create.
50
        wt = self.make_branch_and_tree('.')
51
        b = wt.branch
1246 by Martin Pool
- add very simple commit tests
52
        file('hello', 'w').write('hello world')
1534.4.26 by Robert Collins
Move working tree initialisation out from Branch.initialize, deprecated Branch.initialize to Branch.create.
53
        wt.add('hello')
54
        wt.commit(message='add hello')
55
        file_id = wt.path2id('hello')
1246 by Martin Pool
- add very simple commit tests
56
57
        file('hello', 'w').write('version 2')
1534.4.26 by Robert Collins
Move working tree initialisation out from Branch.initialize, deprecated Branch.initialize to Branch.create.
58
        wt.commit(message='commit 2')
1246 by Martin Pool
- add very simple commit tests
59
60
        eq = self.assertEquals
61
        eq(b.revno(), 2)
62
        rh = b.revision_history()
1185.67.2 by Aaron Bentley
Renamed Branch.storage to Branch.repository
63
        rev = b.repository.get_revision(rh[0])
1246 by Martin Pool
- add very simple commit tests
64
        eq(rev.message, 'add hello')
65
1185.67.2 by Aaron Bentley
Renamed Branch.storage to Branch.repository
66
        tree1 = b.repository.revision_tree(rh[0])
1246 by Martin Pool
- add very simple commit tests
67
        text = tree1.get_file_text(file_id)
68
        eq(text, 'hello world')
69
1185.67.2 by Aaron Bentley
Renamed Branch.storage to Branch.repository
70
        tree2 = b.repository.revision_tree(rh[1])
1246 by Martin Pool
- add very simple commit tests
71
        eq(tree2.get_file_text(file_id), 'version 2')
72
73
    def test_delete_commit(self):
74
        """Test a commit with a deleted file"""
1534.4.26 by Robert Collins
Move working tree initialisation out from Branch.initialize, deprecated Branch.initialize to Branch.create.
75
        wt = self.make_branch_and_tree('.')
76
        b = wt.branch
1247 by Martin Pool
- tests for deletion and removal of files in commits
77
        file('hello', 'w').write('hello world')
1534.4.26 by Robert Collins
Move working tree initialisation out from Branch.initialize, deprecated Branch.initialize to Branch.create.
78
        wt.add(['hello'], ['hello-id'])
79
        wt.commit(message='add hello')
1247 by Martin Pool
- tests for deletion and removal of files in commits
80
81
        os.remove('hello')
1534.4.26 by Robert Collins
Move working tree initialisation out from Branch.initialize, deprecated Branch.initialize to Branch.create.
82
        wt.commit('removed hello', rev_id='rev2')
1247 by Martin Pool
- tests for deletion and removal of files in commits
83
1185.67.2 by Aaron Bentley
Renamed Branch.storage to Branch.repository
84
        tree = b.repository.revision_tree('rev2')
1247 by Martin Pool
- tests for deletion and removal of files in commits
85
        self.assertFalse(tree.has_id('hello-id'))
86
1253 by Martin Pool
- test that pointless commits are trapped
87
    def test_pointless_commit(self):
88
        """Commit refuses unless there are changes or it's forced."""
1534.4.26 by Robert Collins
Move working tree initialisation out from Branch.initialize, deprecated Branch.initialize to Branch.create.
89
        wt = self.make_branch_and_tree('.')
90
        b = wt.branch
1253 by Martin Pool
- test that pointless commits are trapped
91
        file('hello', 'w').write('hello')
1534.4.26 by Robert Collins
Move working tree initialisation out from Branch.initialize, deprecated Branch.initialize to Branch.create.
92
        wt.add(['hello'])
93
        wt.commit(message='add hello')
1253 by Martin Pool
- test that pointless commits are trapped
94
        self.assertEquals(b.revno(), 1)
95
        self.assertRaises(PointlessCommit,
1534.4.26 by Robert Collins
Move working tree initialisation out from Branch.initialize, deprecated Branch.initialize to Branch.create.
96
                          wt.commit,
1253 by Martin Pool
- test that pointless commits are trapped
97
                          message='fails',
98
                          allow_pointless=False)
99
        self.assertEquals(b.revno(), 1)
100
        
1252 by Martin Pool
- add test for commit of an empty tree
101
    def test_commit_empty(self):
1253 by Martin Pool
- test that pointless commits are trapped
102
        """Commiting an empty tree works."""
1534.4.26 by Robert Collins
Move working tree initialisation out from Branch.initialize, deprecated Branch.initialize to Branch.create.
103
        wt = self.make_branch_and_tree('.')
104
        b = wt.branch
105
        wt.commit(message='empty tree', allow_pointless=True)
1253 by Martin Pool
- test that pointless commits are trapped
106
        self.assertRaises(PointlessCommit,
1534.4.26 by Robert Collins
Move working tree initialisation out from Branch.initialize, deprecated Branch.initialize to Branch.create.
107
                          wt.commit,
1253 by Martin Pool
- test that pointless commits are trapped
108
                          message='empty tree',
109
                          allow_pointless=False)
1534.4.26 by Robert Collins
Move working tree initialisation out from Branch.initialize, deprecated Branch.initialize to Branch.create.
110
        wt.commit(message='empty tree', allow_pointless=True)
1252 by Martin Pool
- add test for commit of an empty tree
111
        self.assertEquals(b.revno(), 2)
112
113
    def test_selective_delete(self):
114
        """Selective commit in tree with deletions"""
1534.4.26 by Robert Collins
Move working tree initialisation out from Branch.initialize, deprecated Branch.initialize to Branch.create.
115
        wt = self.make_branch_and_tree('.')
116
        b = wt.branch
1254 by Martin Pool
- fix handling of selective commit with deleted files
117
        file('hello', 'w').write('hello')
118
        file('buongia', 'w').write('buongia')
1534.4.26 by Robert Collins
Move working tree initialisation out from Branch.initialize, deprecated Branch.initialize to Branch.create.
119
        wt.add(['hello', 'buongia'],
1255 by Martin Pool
- more tests for selective commit of deletion
120
              ['hello-id', 'buongia-id'])
1534.4.26 by Robert Collins
Move working tree initialisation out from Branch.initialize, deprecated Branch.initialize to Branch.create.
121
        wt.commit(message='add files',
1255 by Martin Pool
- more tests for selective commit of deletion
122
                 rev_id='test@rev-1')
1254 by Martin Pool
- fix handling of selective commit with deleted files
123
        
124
        os.remove('hello')
125
        file('buongia', 'w').write('new text')
1534.4.26 by Robert Collins
Move working tree initialisation out from Branch.initialize, deprecated Branch.initialize to Branch.create.
126
        wt.commit(message='update text',
1254 by Martin Pool
- fix handling of selective commit with deleted files
127
                 specific_files=['buongia'],
1255 by Martin Pool
- more tests for selective commit of deletion
128
                 allow_pointless=False,
129
                 rev_id='test@rev-2')
1254 by Martin Pool
- fix handling of selective commit with deleted files
130
1534.4.26 by Robert Collins
Move working tree initialisation out from Branch.initialize, deprecated Branch.initialize to Branch.create.
131
        wt.commit(message='remove hello',
1254 by Martin Pool
- fix handling of selective commit with deleted files
132
                 specific_files=['hello'],
1255 by Martin Pool
- more tests for selective commit of deletion
133
                 allow_pointless=False,
134
                 rev_id='test@rev-3')
1254 by Martin Pool
- fix handling of selective commit with deleted files
135
136
        eq = self.assertEquals
137
        eq(b.revno(), 3)
1255 by Martin Pool
- more tests for selective commit of deletion
138
1185.67.2 by Aaron Bentley
Renamed Branch.storage to Branch.repository
139
        tree2 = b.repository.revision_tree('test@rev-2')
1255 by Martin Pool
- more tests for selective commit of deletion
140
        self.assertTrue(tree2.has_filename('hello'))
141
        self.assertEquals(tree2.get_file_text('hello-id'), 'hello')
142
        self.assertEquals(tree2.get_file_text('buongia-id'), 'new text')
143
        
1185.67.2 by Aaron Bentley
Renamed Branch.storage to Branch.repository
144
        tree3 = b.repository.revision_tree('test@rev-3')
1255 by Martin Pool
- more tests for selective commit of deletion
145
        self.assertFalse(tree3.has_filename('hello'))
146
        self.assertEquals(tree3.get_file_text('buongia-id'), 'new text')
1285 by Martin Pool
- fix bug in committing files that are renamed but not modified
147
148
    def test_commit_rename(self):
149
        """Test commit of a revision where a file is renamed."""
1534.4.26 by Robert Collins
Move working tree initialisation out from Branch.initialize, deprecated Branch.initialize to Branch.create.
150
        tree = self.make_branch_and_tree('.')
151
        b = tree.branch
1185.38.7 by John Arbash Meinel
Updated build_tree to use fixed line-endings for tests which read the file contents and compare
152
        self.build_tree(['hello'], line_endings='binary')
1508.1.7 by Robert Collins
Move rename_one from Branch to WorkingTree. (Robert Collins).
153
        tree.add(['hello'], ['hello-id'])
154
        tree.commit(message='one', rev_id='test@rev-1', allow_pointless=False)
1285 by Martin Pool
- fix bug in committing files that are renamed but not modified
155
1508.1.7 by Robert Collins
Move rename_one from Branch to WorkingTree. (Robert Collins).
156
        tree.rename_one('hello', 'fruity')
157
        tree.commit(message='renamed', rev_id='test@rev-2', allow_pointless=False)
1285 by Martin Pool
- fix bug in committing files that are renamed but not modified
158
1303 by Martin Pool
- commit updates entry_version
159
        eq = self.assertEquals
1185.67.2 by Aaron Bentley
Renamed Branch.storage to Branch.repository
160
        tree1 = b.repository.revision_tree('test@rev-1')
1303 by Martin Pool
- commit updates entry_version
161
        eq(tree1.id2path('hello-id'), 'hello')
162
        eq(tree1.get_file_text('hello-id'), 'contents of hello\n')
1285 by Martin Pool
- fix bug in committing files that are renamed but not modified
163
        self.assertFalse(tree1.has_filename('fruity'))
1291 by Martin Pool
- add test for moving files between directories
164
        self.check_inventory_shape(tree1.inventory, ['hello'])
1303 by Martin Pool
- commit updates entry_version
165
        ie = tree1.inventory['hello-id']
1092.2.21 by Robert Collins
convert name_version to revision in inventory entries
166
        eq(ie.revision, 'test@rev-1')
1285 by Martin Pool
- fix bug in committing files that are renamed but not modified
167
1185.67.2 by Aaron Bentley
Renamed Branch.storage to Branch.repository
168
        tree2 = b.repository.revision_tree('test@rev-2')
1303 by Martin Pool
- commit updates entry_version
169
        eq(tree2.id2path('hello-id'), 'fruity')
170
        eq(tree2.get_file_text('hello-id'), 'contents of hello\n')
1291 by Martin Pool
- add test for moving files between directories
171
        self.check_inventory_shape(tree2.inventory, ['fruity'])
1303 by Martin Pool
- commit updates entry_version
172
        ie = tree2.inventory['hello-id']
1092.2.21 by Robert Collins
convert name_version to revision in inventory entries
173
        eq(ie.revision, 'test@rev-2')
1291 by Martin Pool
- add test for moving files between directories
174
175
    def test_reused_rev_id(self):
1292 by Martin Pool
- add check that revision ids cannot be committed twice
176
        """Test that a revision id cannot be reused in a branch"""
1534.4.26 by Robert Collins
Move working tree initialisation out from Branch.initialize, deprecated Branch.initialize to Branch.create.
177
        wt = self.make_branch_and_tree('.')
178
        b = wt.branch
179
        wt.commit('initial', rev_id='test@rev-1', allow_pointless=True)
1292 by Martin Pool
- add check that revision ids cannot be committed twice
180
        self.assertRaises(Exception,
1534.4.35 by Robert Collins
Give branch its own basis tree and last_revision methods; deprecated branch.working_tree()
181
                          wt.commit,
1292 by Martin Pool
- add check that revision ids cannot be committed twice
182
                          message='reused id',
183
                          rev_id='test@rev-1',
184
                          allow_pointless=True)
1291 by Martin Pool
- add test for moving files between directories
185
186
    def test_commit_move(self):
187
        """Test commit of revisions with moved files and directories"""
1306 by Martin Pool
- tests that name_version is updated properly in renames/moves
188
        eq = self.assertEquals
1534.4.26 by Robert Collins
Move working tree initialisation out from Branch.initialize, deprecated Branch.initialize to Branch.create.
189
        wt = self.make_branch_and_tree('.')
190
        b = wt.branch
1306 by Martin Pool
- tests that name_version is updated properly in renames/moves
191
        r1 = 'test@rev-1'
1291 by Martin Pool
- add test for moving files between directories
192
        self.build_tree(['hello', 'a/', 'b/'])
1534.4.26 by Robert Collins
Move working tree initialisation out from Branch.initialize, deprecated Branch.initialize to Branch.create.
193
        wt.add(['hello', 'a', 'b'], ['hello-id', 'a-id', 'b-id'])
194
        wt.commit('initial', rev_id=r1, allow_pointless=False)
195
        wt.move(['hello'], 'a')
1306 by Martin Pool
- tests that name_version is updated properly in renames/moves
196
        r2 = 'test@rev-2'
1534.4.26 by Robert Collins
Move working tree initialisation out from Branch.initialize, deprecated Branch.initialize to Branch.create.
197
        wt.commit('two', rev_id=r2, allow_pointless=False)
1534.4.35 by Robert Collins
Give branch its own basis tree and last_revision methods; deprecated branch.working_tree()
198
        self.check_inventory_shape(wt.read_working_inventory(),
1291 by Martin Pool
- add test for moving files between directories
199
                                   ['a', 'a/hello', 'b'])
200
1534.4.26 by Robert Collins
Move working tree initialisation out from Branch.initialize, deprecated Branch.initialize to Branch.create.
201
        wt.move(['b'], 'a')
1306 by Martin Pool
- tests that name_version is updated properly in renames/moves
202
        r3 = 'test@rev-3'
1534.4.26 by Robert Collins
Move working tree initialisation out from Branch.initialize, deprecated Branch.initialize to Branch.create.
203
        wt.commit('three', rev_id=r3, allow_pointless=False)
204
        self.check_inventory_shape(wt.read_working_inventory(),
1291 by Martin Pool
- add test for moving files between directories
205
                                   ['a', 'a/hello', 'a/b'])
1185.67.2 by Aaron Bentley
Renamed Branch.storage to Branch.repository
206
        self.check_inventory_shape(b.repository.get_revision_inventory(r3),
1291 by Martin Pool
- add test for moving files between directories
207
                                   ['a', 'a/hello', 'a/b'])
208
1534.4.26 by Robert Collins
Move working tree initialisation out from Branch.initialize, deprecated Branch.initialize to Branch.create.
209
        wt.move(['a/hello'], 'a/b')
1306 by Martin Pool
- tests that name_version is updated properly in renames/moves
210
        r4 = 'test@rev-4'
1534.4.26 by Robert Collins
Move working tree initialisation out from Branch.initialize, deprecated Branch.initialize to Branch.create.
211
        wt.commit('four', rev_id=r4, allow_pointless=False)
212
        self.check_inventory_shape(wt.read_working_inventory(),
1291 by Martin Pool
- add test for moving files between directories
213
                                   ['a', 'a/b/hello', 'a/b'])
1306 by Martin Pool
- tests that name_version is updated properly in renames/moves
214
1185.67.2 by Aaron Bentley
Renamed Branch.storage to Branch.repository
215
        inv = b.repository.get_revision_inventory(r4)
1092.2.21 by Robert Collins
convert name_version to revision in inventory entries
216
        eq(inv['hello-id'].revision, r4)
217
        eq(inv['a-id'].revision, r1)
218
        eq(inv['b-id'].revision, r3)
1306 by Martin Pool
- tests that name_version is updated properly in renames/moves
219
        
1246 by Martin Pool
- add very simple commit tests
220
    def test_removed_commit(self):
1185.16.72 by Martin Pool
[merge] from robert and fix up tests
221
        """Commit with a removed file"""
1534.4.26 by Robert Collins
Move working tree initialisation out from Branch.initialize, deprecated Branch.initialize to Branch.create.
222
        wt = self.make_branch_and_tree('.')
223
        b = wt.branch
1247 by Martin Pool
- tests for deletion and removal of files in commits
224
        file('hello', 'w').write('hello world')
1534.4.26 by Robert Collins
Move working tree initialisation out from Branch.initialize, deprecated Branch.initialize to Branch.create.
225
        wt.add(['hello'], ['hello-id'])
226
        wt.commit(message='add hello')
1185.16.72 by Martin Pool
[merge] from robert and fix up tests
227
        wt.remove('hello')
1534.4.26 by Robert Collins
Move working tree initialisation out from Branch.initialize, deprecated Branch.initialize to Branch.create.
228
        wt.commit('removed hello', rev_id='rev2')
1247 by Martin Pool
- tests for deletion and removal of files in commits
229
1185.67.2 by Aaron Bentley
Renamed Branch.storage to Branch.repository
230
        tree = b.repository.revision_tree('rev2')
1247 by Martin Pool
- tests for deletion and removal of files in commits
231
        self.assertFalse(tree.has_id('hello-id'))
1246 by Martin Pool
- add very simple commit tests
232
1256 by Martin Pool
- test that commits append to the tree's ancestry
233
    def test_committed_ancestry(self):
234
        """Test commit appends revisions to ancestry."""
1534.4.26 by Robert Collins
Move working tree initialisation out from Branch.initialize, deprecated Branch.initialize to Branch.create.
235
        wt = self.make_branch_and_tree('.')
236
        b = wt.branch
1256 by Martin Pool
- test that commits append to the tree's ancestry
237
        rev_ids = []
238
        for i in range(4):
239
            file('hello', 'w').write((str(i) * 4) + '\n')
240
            if i == 0:
1534.4.26 by Robert Collins
Move working tree initialisation out from Branch.initialize, deprecated Branch.initialize to Branch.create.
241
                wt.add(['hello'], ['hello-id'])
1256 by Martin Pool
- test that commits append to the tree's ancestry
242
            rev_id = 'test@rev-%d' % (i+1)
243
            rev_ids.append(rev_id)
1534.4.26 by Robert Collins
Move working tree initialisation out from Branch.initialize, deprecated Branch.initialize to Branch.create.
244
            wt.commit(message='rev %d' % (i+1),
1256 by Martin Pool
- test that commits append to the tree's ancestry
245
                     rev_id=rev_id)
246
        eq = self.assertEquals
247
        eq(b.revision_history(), rev_ids)
248
        for i in range(4):
1185.67.2 by Aaron Bentley
Renamed Branch.storage to Branch.repository
249
            anc = b.repository.get_ancestry(rev_ids[i])
1390 by Robert Collins
pair programming worx... merge integration and weave
250
            eq(anc, [None] + rev_ids[:i+1])
1416 by Robert Collins
when committing a specific file, include all its parents
251
252
    def test_commit_new_subdir_child_selective(self):
1534.4.26 by Robert Collins
Move working tree initialisation out from Branch.initialize, deprecated Branch.initialize to Branch.create.
253
        wt = self.make_branch_and_tree('.')
254
        b = wt.branch
1416 by Robert Collins
when committing a specific file, include all its parents
255
        self.build_tree(['dir/', 'dir/file1', 'dir/file2'])
1534.4.26 by Robert Collins
Move working tree initialisation out from Branch.initialize, deprecated Branch.initialize to Branch.create.
256
        wt.add(['dir', 'dir/file1', 'dir/file2'],
1416 by Robert Collins
when committing a specific file, include all its parents
257
              ['dirid', 'file1id', 'file2id'])
1534.4.26 by Robert Collins
Move working tree initialisation out from Branch.initialize, deprecated Branch.initialize to Branch.create.
258
        wt.commit('dir/file1', specific_files=['dir/file1'], rev_id='1')
1185.67.2 by Aaron Bentley
Renamed Branch.storage to Branch.repository
259
        inv = b.repository.get_inventory('1')
1416 by Robert Collins
when committing a specific file, include all its parents
260
        self.assertEqual('1', inv['dirid'].revision)
261
        self.assertEqual('1', inv['file1id'].revision)
262
        # FIXME: This should raise a KeyError I think, rbc20051006
263
        self.assertRaises(BzrError, inv.__getitem__, 'file2id')
1185.16.65 by mbp at sourcefrog
- new commit --strict option
264
265
    def test_strict_commit(self):
266
        """Try and commit with unknown files and strict = True, should fail."""
267
        from bzrlib.errors import StrictCommitFailed
1534.4.26 by Robert Collins
Move working tree initialisation out from Branch.initialize, deprecated Branch.initialize to Branch.create.
268
        wt = self.make_branch_and_tree('.')
269
        b = wt.branch
1185.16.65 by mbp at sourcefrog
- new commit --strict option
270
        file('hello', 'w').write('hello world')
1534.4.26 by Robert Collins
Move working tree initialisation out from Branch.initialize, deprecated Branch.initialize to Branch.create.
271
        wt.add('hello')
1185.16.65 by mbp at sourcefrog
- new commit --strict option
272
        file('goodbye', 'w').write('goodbye cruel world!')
1534.4.35 by Robert Collins
Give branch its own basis tree and last_revision methods; deprecated branch.working_tree()
273
        self.assertRaises(StrictCommitFailed, wt.commit,
1185.16.65 by mbp at sourcefrog
- new commit --strict option
274
            message='add hello but not goodbye', strict=True)
275
1185.22.4 by Michael Ellerman
Strict commit was a little .. ah .. too strict, oops :}
276
    def test_strict_commit_without_unknowns(self):
277
        """Try and commit with no unknown files and strict = True,
278
        should work."""
279
        from bzrlib.errors import StrictCommitFailed
1534.4.26 by Robert Collins
Move working tree initialisation out from Branch.initialize, deprecated Branch.initialize to Branch.create.
280
        wt = self.make_branch_and_tree('.')
281
        b = wt.branch
1185.22.4 by Michael Ellerman
Strict commit was a little .. ah .. too strict, oops :}
282
        file('hello', 'w').write('hello world')
1534.4.26 by Robert Collins
Move working tree initialisation out from Branch.initialize, deprecated Branch.initialize to Branch.create.
283
        wt.add('hello')
284
        wt.commit(message='add hello', strict=True)
1185.22.4 by Michael Ellerman
Strict commit was a little .. ah .. too strict, oops :}
285
1185.16.65 by mbp at sourcefrog
- new commit --strict option
286
    def test_nonstrict_commit(self):
287
        """Try and commit with unknown files and strict = False, should work."""
1534.4.26 by Robert Collins
Move working tree initialisation out from Branch.initialize, deprecated Branch.initialize to Branch.create.
288
        wt = self.make_branch_and_tree('.')
289
        b = wt.branch
1185.16.65 by mbp at sourcefrog
- new commit --strict option
290
        file('hello', 'w').write('hello world')
1534.4.26 by Robert Collins
Move working tree initialisation out from Branch.initialize, deprecated Branch.initialize to Branch.create.
291
        wt.add('hello')
1185.16.65 by mbp at sourcefrog
- new commit --strict option
292
        file('goodbye', 'w').write('goodbye cruel world!')
1534.4.26 by Robert Collins
Move working tree initialisation out from Branch.initialize, deprecated Branch.initialize to Branch.create.
293
        wt.commit(message='add hello but not goodbye', strict=False)
1185.16.72 by Martin Pool
[merge] from robert and fix up tests
294
1185.22.4 by Michael Ellerman
Strict commit was a little .. ah .. too strict, oops :}
295
    def test_nonstrict_commit_without_unknowns(self):
296
        """Try and commit with no unknown files and strict = False,
297
        should work."""
1534.4.26 by Robert Collins
Move working tree initialisation out from Branch.initialize, deprecated Branch.initialize to Branch.create.
298
        wt = self.make_branch_and_tree('.')
299
        b = wt.branch
1185.22.4 by Michael Ellerman
Strict commit was a little .. ah .. too strict, oops :}
300
        file('hello', 'w').write('hello world')
1534.4.26 by Robert Collins
Move working tree initialisation out from Branch.initialize, deprecated Branch.initialize to Branch.create.
301
        wt.add('hello')
302
        wt.commit(message='add hello', strict=False)
1185.22.4 by Michael Ellerman
Strict commit was a little .. ah .. too strict, oops :}
303
1442.1.60 by Robert Collins
gpg sign commits if the policy says we need to
304
    def test_signed_commit(self):
305
        import bzrlib.gpg
306
        import bzrlib.commit as commit
307
        oldstrategy = bzrlib.gpg.GPGStrategy
1534.4.26 by Robert Collins
Move working tree initialisation out from Branch.initialize, deprecated Branch.initialize to Branch.create.
308
        wt = self.make_branch_and_tree('.')
309
        branch = wt.branch
310
        wt.commit("base", allow_pointless=True, rev_id='A')
1185.67.2 by Aaron Bentley
Renamed Branch.storage to Branch.repository
311
        self.failIf(branch.repository.revision_store.has_id('A', 'sig'))
1442.1.60 by Robert Collins
gpg sign commits if the policy says we need to
312
        try:
313
            from bzrlib.testament import Testament
314
            # monkey patch gpg signing mechanism
315
            bzrlib.gpg.GPGStrategy = bzrlib.gpg.LoopbackGPGStrategy
1534.4.26 by Robert Collins
Move working tree initialisation out from Branch.initialize, deprecated Branch.initialize to Branch.create.
316
            commit.Commit(config=MustSignConfig(branch)).commit(message="base",
1442.1.60 by Robert Collins
gpg sign commits if the policy says we need to
317
                                                      allow_pointless=True,
1534.4.26 by Robert Collins
Move working tree initialisation out from Branch.initialize, deprecated Branch.initialize to Branch.create.
318
                                                      rev_id='B',
319
                                                      working_tree=wt)
1185.67.2 by Aaron Bentley
Renamed Branch.storage to Branch.repository
320
            self.assertEqual(Testament.from_revision(branch.repository,
1185.65.1 by Aaron Bentley
Refactored out ControlFiles and RevisionStore from _Branch
321
                             'B').as_short_text(),
1185.67.2 by Aaron Bentley
Renamed Branch.storage to Branch.repository
322
                             branch.repository.revision_store.get('B', 
1185.65.1 by Aaron Bentley
Refactored out ControlFiles and RevisionStore from _Branch
323
                                                               'sig').read())
1442.1.60 by Robert Collins
gpg sign commits if the policy says we need to
324
        finally:
325
            bzrlib.gpg.GPGStrategy = oldstrategy
1442.1.62 by Robert Collins
Allow creation of testaments from uncommitted data, and use that to get signatures before committing revisions.
326
327
    def test_commit_failed_signature(self):
328
        import bzrlib.gpg
329
        import bzrlib.commit as commit
330
        oldstrategy = bzrlib.gpg.GPGStrategy
1534.4.26 by Robert Collins
Move working tree initialisation out from Branch.initialize, deprecated Branch.initialize to Branch.create.
331
        wt = self.make_branch_and_tree('.')
332
        branch = wt.branch
333
        wt.commit("base", allow_pointless=True, rev_id='A')
1185.67.2 by Aaron Bentley
Renamed Branch.storage to Branch.repository
334
        self.failIf(branch.repository.revision_store.has_id('A', 'sig'))
1442.1.62 by Robert Collins
Allow creation of testaments from uncommitted data, and use that to get signatures before committing revisions.
335
        try:
336
            from bzrlib.testament import Testament
337
            # monkey patch gpg signing mechanism
338
            bzrlib.gpg.GPGStrategy = bzrlib.gpg.DisabledGPGStrategy
339
            config = MustSignConfig(branch)
340
            self.assertRaises(SigningFailed,
341
                              commit.Commit(config=config).commit,
1534.4.34 by Robert Collins
Fix remaining uses of deprecated apis within bzrlib.
342
                              message="base",
1442.1.62 by Robert Collins
Allow creation of testaments from uncommitted data, and use that to get signatures before committing revisions.
343
                              allow_pointless=True,
1534.4.34 by Robert Collins
Fix remaining uses of deprecated apis within bzrlib.
344
                              rev_id='B',
345
                              working_tree=wt)
1534.4.26 by Robert Collins
Move working tree initialisation out from Branch.initialize, deprecated Branch.initialize to Branch.create.
346
            branch = Branch.open(self.get_url('.'))
1442.1.62 by Robert Collins
Allow creation of testaments from uncommitted data, and use that to get signatures before committing revisions.
347
            self.assertEqual(branch.revision_history(), ['A'])
1185.67.2 by Aaron Bentley
Renamed Branch.storage to Branch.repository
348
            self.failIf(branch.repository.revision_store.has_id('B'))
1442.1.62 by Robert Collins
Allow creation of testaments from uncommitted data, and use that to get signatures before committing revisions.
349
        finally:
350
            bzrlib.gpg.GPGStrategy = oldstrategy
1472 by Robert Collins
post commit hook, first pass implementation
351
352
    def test_commit_invokes_hooks(self):
353
        import bzrlib.commit as commit
1534.4.26 by Robert Collins
Move working tree initialisation out from Branch.initialize, deprecated Branch.initialize to Branch.create.
354
        wt = self.make_branch_and_tree('.')
355
        branch = wt.branch
1472 by Robert Collins
post commit hook, first pass implementation
356
        calls = []
357
        def called(branch, rev_id):
358
            calls.append('called')
359
        bzrlib.ahook = called
360
        try:
361
            config = BranchWithHooks(branch)
362
            commit.Commit(config=config).commit(
1534.4.26 by Robert Collins
Move working tree initialisation out from Branch.initialize, deprecated Branch.initialize to Branch.create.
363
                            message = "base",
1472 by Robert Collins
post commit hook, first pass implementation
364
                            allow_pointless=True,
1534.4.26 by Robert Collins
Move working tree initialisation out from Branch.initialize, deprecated Branch.initialize to Branch.create.
365
                            rev_id='A', working_tree = wt)
1472 by Robert Collins
post commit hook, first pass implementation
366
            self.assertEqual(['called', 'called'], calls)
367
        finally:
368
            del bzrlib.ahook