1
# Copyright (C) 2006-2011 Canonical Ltd
1
# Copyright (C) 2006-2010 Canonical Ltd
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
18
18
"""Tests for the commit CLI of bzr."""
25
from testtools.matchers import DocTestMatches
27
24
from bzrlib import (
34
31
from bzrlib.bzrdir import BzrDir
35
32
from bzrlib.tests import (
36
33
probe_bad_non_ascii,
39
UnicodeFilenameFeature,
41
36
from bzrlib.tests import TestCaseWithTransport
50
45
self.build_tree(['hello.txt'])
51
46
out,err = self.run_bzr('commit -m empty', retcode=3)
52
47
self.assertEqual('', out)
54
# 1) We really don't want 'aborting commit write group' anymore.
55
# 2) bzr: ERROR: is a really long line, so we wrap it with '\'
60
bzr: ERROR: No changes to commit.\
61
Please 'bzr add' the files you want to commit,\
62
or use --unchanged to force an empty commit.
63
""", flags=doctest.ELLIPSIS|doctest.REPORT_UDIFF))
48
self.assertContainsRe(err, 'bzr: ERROR: No changes to commit\.'
49
' Use --unchanged to commit anyhow.\n')
65
51
def test_commit_success(self):
66
52
"""Successful commit should not leave behind a bzr-commit-* file"""
72
58
self.run_bzr(["commit", "--unchanged", "-m", u'foo\xb5'])
73
59
self.assertEqual('', self.run_bzr('unknowns')[0])
75
def test_commit_lossy_native(self):
76
"""A --lossy option to commit is supported."""
77
self.make_branch_and_tree('.')
78
self.run_bzr('commit --lossy --unchanged -m message')
79
self.assertEqual('', self.run_bzr('unknowns')[0])
81
def test_commit_lossy_foreign(self):
82
test_foreign.register_dummy_foreign_for_test(self)
83
self.make_branch_and_tree('.',
84
format=test_foreign.DummyForeignVcsDirFormat())
85
self.run_bzr('commit --lossy --unchanged -m message')
86
output = self.run_bzr('revision-info')[0]
87
self.assertTrue(output.startswith('1 dummy-'))
89
61
def test_commit_with_path(self):
90
62
"""Commit tree with path of root specified"""
91
63
a_tree = self.make_branch_and_tree('a')
105
77
self.run_bzr('resolved b/a_file')
106
78
self.run_bzr(['commit', '-m', 'merge into b', 'b'])
108
81
def test_10_verbose_commit(self):
109
82
"""Add one file and examine verbose commit output"""
110
83
tree = self.make_branch_and_tree('.')
138
111
def test_unicode_commit_message_is_filename(self):
139
112
"""Unicode commit message same as a filename (Bug #563646).
141
self.requireFeature(UnicodeFilenameFeature)
142
114
file_name = u'\N{euro sign}'
143
115
self.run_bzr(['init'])
144
116
open(file_name, 'w').write('hello world')
334
306
tree.add('foo.c')
335
307
self.run_bzr('commit -m ""', retcode=3)
309
def test_unsupported_encoding_commit_message(self):
310
if sys.platform == 'win32':
311
raise tests.TestNotApplicable('Win32 parses arguments directly'
312
' as Unicode, so we can\'t pass invalid non-ascii')
313
tree = self.make_branch_and_tree('.')
314
self.build_tree_contents([('foo.c', 'int main() {}')])
316
# LANG env variable has no effect on Windows
317
# but some characters anyway cannot be represented
318
# in default user encoding
319
char = probe_bad_non_ascii(osutils.get_user_encoding())
321
raise TestSkipped('Cannot find suitable non-ascii character'
322
'for user_encoding (%s)' % osutils.get_user_encoding())
323
out,err = self.run_bzr_subprocess('commit -m "%s"' % char,
325
env_changes={'LANG': 'C'})
326
self.assertContainsRe(err, r'bzrlib.errors.BzrError: Parameter.*is '
327
'unsupported by the current encoding.')
337
329
def test_other_branch_commit(self):
338
330
# this branch is to ensure consistent behaviour, whether we're run
339
331
# inside a branch, or not.
724
716
f = file('fed.bat', 'w')
725
717
f.write('@rem dummy fed')
727
self.overrideEnv('BZR_EDITOR', "fed.bat")
719
osutils.set_or_unset_env('BZR_EDITOR', "fed.bat")
729
721
f = file('fed.sh', 'wb')
730
722
f.write('#!/bin/sh\n')
732
724
os.chmod('fed.sh', 0755)
733
self.overrideEnv('BZR_EDITOR', "./fed.sh")
725
osutils.set_or_unset_env('BZR_EDITOR', "./fed.sh")
735
727
def setup_commit_with_template(self):
736
728
self.setup_editor()
754
746
"commit tree/hello.txt", stdin="n\n")
755
747
self.assertEqual(expected, tree.last_revision())
757
def test_set_commit_message(self):
758
msgeditor.hooks.install_named_hook("set_commit_message",
759
lambda commit_obj, msg: "save me some typing\n", None)
760
tree = self.make_branch_and_tree('tree')
761
self.build_tree(['tree/hello.txt'])
762
tree.add('hello.txt')
763
out, err = self.run_bzr("commit tree/hello.txt")
764
last_rev = tree.branch.repository.get_revision(tree.last_revision())
765
self.assertEqual('save me some typing\n', last_rev.message)
767
749
def test_commit_without_username(self):
768
750
"""Ensure commit error if username is not set.
772
754
open('foo.txt', 'w').write('hello')
773
755
self.run_bzr(['add'])
774
self.overrideEnv('EMAIL', None)
775
self.overrideEnv('BZR_EMAIL', None)
776
# Also, make sure that it's not inferred from mailname.
777
self.overrideAttr(config, '_auto_user_id',
778
lambda: (None, None))
756
osutils.set_or_unset_env('EMAIL', None)
757
osutils.set_or_unset_env('BZR_EMAIL', None)
779
758
out, err = self.run_bzr(['commit', '-m', 'initial'], 3)
780
759
self.assertContainsRe(err, 'Unable to determine your name')
782
def test_commit_recursive_checkout(self):
783
"""Ensure that a commit to a recursive checkout fails cleanly.
785
self.run_bzr(['init', 'test_branch'])
786
self.run_bzr(['checkout', 'test_branch', 'test_checkout'])
787
os.chdir('test_checkout')
788
self.run_bzr(['bind', '.']) # bind to self
789
open('foo.txt', 'w').write('hello')
790
self.run_bzr(['add'])
791
out, err = self.run_bzr(['commit', '-m', 'addedfoo'], 3)
792
self.assertEqual(out, '')
793
self.assertContainsRe(err,
794
'Branch.*test_checkout.*appears to be bound to itself')