20
21
from bzrlib.selftest import TestCaseInTempDir
21
22
from bzrlib.branch import Branch
23
from bzrlib.workingtree import WorkingTree
22
24
from bzrlib.commit import Commit
23
from bzrlib.errors import PointlessCommit, BzrError
25
from bzrlib.config import BranchConfig
26
from bzrlib.errors import PointlessCommit, BzrError, SigningFailed
26
29
# TODO: Test commit with some added, and added-but-missing files
31
class MustSignConfig(BranchConfig):
33
def signature_needed(self):
36
def gpg_signing_command(self):
40
class BranchWithHooks(BranchConfig):
42
def post_commit(self):
43
return "bzrlib.ahook bzrlib.ahook"
28
46
class TestCommit(TestCaseInTempDir):
30
48
def test_simple_commit(self):
246
265
self.assertEqual('1', inv['file1id'].revision)
247
266
# FIXME: This should raise a KeyError I think, rbc20051006
248
267
self.assertRaises(BzrError, inv.__getitem__, 'file2id')
269
def test_strict_commit(self):
270
"""Try and commit with unknown files and strict = True, should fail."""
271
from bzrlib.errors import StrictCommitFailed
272
b = Branch.initialize('.')
273
file('hello', 'w').write('hello world')
275
file('goodbye', 'w').write('goodbye cruel world!')
276
self.assertRaises(StrictCommitFailed, b.commit,
277
message='add hello but not goodbye', strict=True)
279
def test_strict_commit_without_unknowns(self):
280
"""Try and commit with no unknown files and strict = True,
282
from bzrlib.errors import StrictCommitFailed
283
b = Branch.initialize('.')
284
file('hello', 'w').write('hello world')
286
b.commit(message='add hello', strict=True)
288
def test_nonstrict_commit(self):
289
"""Try and commit with unknown files and strict = False, should work."""
290
b = Branch.initialize('.')
291
file('hello', 'w').write('hello world')
293
file('goodbye', 'w').write('goodbye cruel world!')
294
b.commit(message='add hello but not goodbye', strict=False)
296
def test_nonstrict_commit_without_unknowns(self):
297
"""Try and commit with no unknown files and strict = False,
299
b = Branch.initialize('.')
300
file('hello', 'w').write('hello world')
302
b.commit(message='add hello', strict=False)
304
def test_signed_commit(self):
306
import bzrlib.commit as commit
307
oldstrategy = bzrlib.gpg.GPGStrategy
308
branch = Branch.initialize('.')
309
branch.commit("base", allow_pointless=True, rev_id='A')
310
self.failIf(branch.revision_store.has_id('A', 'sig'))
312
from bzrlib.testament import Testament
313
# monkey patch gpg signing mechanism
314
bzrlib.gpg.GPGStrategy = bzrlib.gpg.LoopbackGPGStrategy
315
commit.Commit(config=MustSignConfig(branch)).commit(branch, "base",
316
allow_pointless=True,
318
self.assertEqual(Testament.from_revision(branch,'B').as_short_text(),
319
branch.revision_store.get('B', 'sig').read())
321
bzrlib.gpg.GPGStrategy = oldstrategy
323
def test_commit_failed_signature(self):
325
import bzrlib.commit as commit
326
oldstrategy = bzrlib.gpg.GPGStrategy
327
branch = Branch.initialize('.')
328
branch.commit("base", allow_pointless=True, rev_id='A')
329
self.failIf(branch.revision_store.has_id('A', 'sig'))
331
from bzrlib.testament import Testament
332
# monkey patch gpg signing mechanism
333
bzrlib.gpg.GPGStrategy = bzrlib.gpg.DisabledGPGStrategy
334
config = MustSignConfig(branch)
335
self.assertRaises(SigningFailed,
336
commit.Commit(config=config).commit,
338
allow_pointless=True,
340
branch = Branch.open('.')
341
self.assertEqual(branch.revision_history(), ['A'])
342
self.failIf(branch.revision_store.has_id('B'))
344
bzrlib.gpg.GPGStrategy = oldstrategy
346
def test_commit_invokes_hooks(self):
347
import bzrlib.commit as commit
348
branch = Branch.initialize('.')
350
def called(branch, rev_id):
351
calls.append('called')
352
bzrlib.ahook = called
354
config = BranchWithHooks(branch)
355
commit.Commit(config=config).commit(
357
allow_pointless=True,
359
self.assertEqual(['called', 'called'], calls)