1
# Copyright (C) 2006-2010 Canonical Ltd
1
# Copyright (C) 2006-2011 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
24
27
from bzrlib import (
31
33
from bzrlib.bzrdir import BzrDir
32
34
from bzrlib.tests import (
36
from bzrlib.tests.blackbox import ExternalBase
39
class TestCommit(ExternalBase):
38
from bzrlib.tests import TestCaseWithTransport
41
class TestCommit(TestCaseWithTransport):
41
43
def test_05_empty_commit(self):
42
44
"""Commit of tree with no versioned files should fail"""
45
47
self.build_tree(['hello.txt'])
46
48
out,err = self.run_bzr('commit -m empty', retcode=3)
47
49
self.assertEqual('', out)
48
self.assertContainsRe(err, 'bzr: ERROR: No changes to commit\.'
49
' Use --unchanged to commit anyhow.\n')
51
# 1) We really don't want 'aborting commit write group' anymore.
52
# 2) bzr: ERROR: is a really long line, so we wrap it with '\'
57
bzr: ERROR: No changes to commit.\
58
Please 'bzr add' the files you want to commit,\
59
or use --unchanged to force an empty commit.
60
""", flags=doctest.ELLIPSIS|doctest.REPORT_UDIFF))
51
62
def test_commit_success(self):
52
63
"""Successful commit should not leave behind a bzr-commit-* file"""
58
69
self.run_bzr(["commit", "--unchanged", "-m", u'foo\xb5'])
59
70
self.assertEqual('', self.run_bzr('unknowns')[0])
72
def test_commit_lossy_native(self):
73
"""A --lossy option to commit is supported."""
74
self.make_branch_and_tree('.')
75
self.run_bzr('commit --lossy --unchanged -m message')
76
self.assertEqual('', self.run_bzr('unknowns')[0])
78
def test_commit_lossy_foreign(self):
79
test_foreign.register_dummy_foreign_for_test(self)
80
self.make_branch_and_tree('.',
81
format=test_foreign.DummyForeignVcsDirFormat())
82
self.run_bzr('commit --lossy --unchanged -m message')
83
output = self.run_bzr('revision-info')[0]
84
self.assertTrue(output.startswith('1 dummy-'))
61
86
def test_commit_with_path(self):
62
87
"""Commit tree with path of root specified"""
63
88
a_tree = self.make_branch_and_tree('a')
77
102
self.run_bzr('resolved b/a_file')
78
103
self.run_bzr(['commit', '-m', 'merge into b', 'b'])
81
105
def test_10_verbose_commit(self):
82
106
"""Add one file and examine verbose commit output"""
83
107
tree = self.make_branch_and_tree('.')
111
135
def test_unicode_commit_message_is_filename(self):
112
136
"""Unicode commit message same as a filename (Bug #563646).
138
self.requireFeature(features.UnicodeFilenameFeature)
114
139
file_name = u'\N{euro sign}'
115
140
self.run_bzr(['init'])
116
141
open(file_name, 'w').write('hello world')
129
154
# by ui.text.show_warning
130
155
default_get_terminal_enc = osutils.get_terminal_encoding
132
osutils.get_terminal_encoding = lambda: 'ascii'
157
osutils.get_terminal_encoding = lambda trace=None: 'ascii'
133
158
file_name = u'foo\u1234'
134
159
open(file_name, 'w').write('hello world')
135
160
self.run_bzr(['add'])
304
329
tree = self.make_branch_and_tree('.')
305
330
self.build_tree_contents([('foo.c', 'int main() {}')])
306
331
tree.add('foo.c')
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.')
332
self.run_bzr('commit -m ""')
329
334
def test_other_branch_commit(self):
330
335
# this branch is to ensure consistent behaviour, whether we're run
591
596
'commit -m add-b --fixes=xxx:123',
592
597
working_dir='tree')
599
def test_fixes_bug_with_default_tracker(self):
600
"""commit --fixes=234 uses the default bug tracker."""
601
tree = self.make_branch_and_tree('tree')
602
self.build_tree(['tree/hello.txt'])
603
tree.add('hello.txt')
605
["bzr: ERROR: No tracker specified for bug 123. Use the form "
606
"'tracker:id' or specify a default bug tracker using the "
607
"`bugtracker` option.\n"
608
"See \"bzr help bugs\" for more information on this feature. "
610
'commit -m add-b --fixes=123',
612
tree.branch.get_config().set_user_option("bugtracker", "lp")
613
self.run_bzr('commit -m hello --fixes=234 tree/hello.txt')
614
last_rev = tree.branch.repository.get_revision(tree.last_revision())
615
self.assertEqual('https://launchpad.net/bugs/234 fixed',
616
last_rev.properties['bugs'])
594
618
def test_fixes_invalid_bug_number(self):
595
619
tree = self.make_branch_and_tree('tree')
596
620
self.build_tree(['tree/hello.txt'])
608
632
self.build_tree(['tree/hello.txt'])
609
633
tree.add('hello.txt')
610
634
self.run_bzr_error(
611
[r"Invalid bug orange. Must be in the form of 'tracker:id'\. "
612
r"See \"bzr help bugs\" for more information on this feature.\n"
613
r"Commit refused\."],
614
'commit -m add-b --fixes=orange',
635
[r"Invalid bug orange:apples:bananas. Must be in the form of "
636
r"'tracker:id'\. See \"bzr help bugs\" for more information on "
637
r"this feature.\nCommit refused\."],
638
'commit -m add-b --fixes=orange:apples:bananas',
615
639
working_dir='tree')
617
641
def test_no_author(self):
698
722
self.assertContainsRe(err, r'modified test\nCommitted revision 2.')
700
724
def test_commit_readonly_checkout(self):
701
# https://bugs.edge.launchpad.net/bzr/+bug/129701
725
# https://bugs.launchpad.net/bzr/+bug/129701
702
726
# "UnlockableTransport error trying to commit in checkout of readonly
704
728
self.make_branch('master')
716
740
f = file('fed.bat', 'w')
717
741
f.write('@rem dummy fed')
719
osutils.set_or_unset_env('BZR_EDITOR', "fed.bat")
743
self.overrideEnv('BZR_EDITOR', "fed.bat")
721
745
f = file('fed.sh', 'wb')
722
746
f.write('#!/bin/sh\n')
724
748
os.chmod('fed.sh', 0755)
725
osutils.set_or_unset_env('BZR_EDITOR', "./fed.sh")
749
self.overrideEnv('BZR_EDITOR', "./fed.sh")
727
751
def setup_commit_with_template(self):
728
752
self.setup_editor()
733
757
tree.add('hello.txt')
760
def test_edit_empty_message(self):
761
tree = self.make_branch_and_tree('tree')
763
self.build_tree(['tree/hello.txt'])
764
tree.add('hello.txt')
765
out, err = self.run_bzr("commit tree/hello.txt", retcode=3,
767
self.assertContainsRe(err,
768
"bzr: ERROR: Empty commit message specified")
736
770
def test_commit_hook_template_accepted(self):
737
771
tree = self.setup_commit_with_template()
738
772
out, err = self.run_bzr("commit tree/hello.txt", stdin="y\n")
742
776
def test_commit_hook_template_rejected(self):
743
777
tree = self.setup_commit_with_template()
744
778
expected = tree.last_revision()
745
out, err = self.run_bzr_error(["empty commit message"],
779
out, err = self.run_bzr_error(["Empty commit message specified."
780
" Please specify a commit message with either"
781
" --message or --file or leave a blank message"
782
" with --message \"\"."],
746
783
"commit tree/hello.txt", stdin="n\n")
747
784
self.assertEqual(expected, tree.last_revision())
786
def test_set_commit_message(self):
787
msgeditor.hooks.install_named_hook("set_commit_message",
788
lambda commit_obj, msg: "save me some typing\n", None)
789
tree = self.make_branch_and_tree('tree')
790
self.build_tree(['tree/hello.txt'])
791
tree.add('hello.txt')
792
out, err = self.run_bzr("commit tree/hello.txt")
793
last_rev = tree.branch.repository.get_revision(tree.last_revision())
794
self.assertEqual('save me some typing\n', last_rev.message)
749
796
def test_commit_without_username(self):
750
797
"""Ensure commit error if username is not set.
754
801
open('foo.txt', 'w').write('hello')
755
802
self.run_bzr(['add'])
756
osutils.set_or_unset_env('EMAIL', None)
757
osutils.set_or_unset_env('BZR_EMAIL', None)
803
self.overrideEnv('EMAIL', None)
804
self.overrideEnv('BZR_EMAIL', None)
805
# Also, make sure that it's not inferred from mailname.
806
self.overrideAttr(config, '_auto_user_id',
807
lambda: (None, None))
758
808
out, err = self.run_bzr(['commit', '-m', 'initial'], 3)
759
809
self.assertContainsRe(err, 'Unable to determine your name')
811
def test_commit_recursive_checkout(self):
812
"""Ensure that a commit to a recursive checkout fails cleanly.
814
self.run_bzr(['init', 'test_branch'])
815
self.run_bzr(['checkout', 'test_branch', 'test_checkout'])
816
os.chdir('test_checkout')
817
self.run_bzr(['bind', '.']) # bind to self
818
open('foo.txt', 'w').write('hello')
819
self.run_bzr(['add'])
820
out, err = self.run_bzr(['commit', '-m', 'addedfoo'], 3)
821
self.assertEqual(out, '')
822
self.assertContainsRe(err,
823
'Branch.*test_checkout.*appears to be bound to itself')