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
23
27
from bzrlib import (
30
34
from bzrlib.bzrdir import BzrDir
31
35
from bzrlib.tests import (
32
36
probe_bad_non_ascii,
38
UnicodeFilenameFeature,
35
from bzrlib.tests.blackbox import ExternalBase
38
class TestCommit(ExternalBase):
40
from bzrlib.tests import TestCaseWithTransport
43
class TestCommit(TestCaseWithTransport):
40
45
def test_05_empty_commit(self):
41
46
"""Commit of tree with no versioned files should fail"""
44
49
self.build_tree(['hello.txt'])
45
50
out,err = self.run_bzr('commit -m empty', retcode=3)
46
51
self.assertEqual('', out)
47
self.assertContainsRe(err, 'bzr: ERROR: No changes to commit\.'
48
' Use --unchanged to commit anyhow.\n')
53
# 1) We really don't want 'aborting commit write group' anymore.
54
# 2) bzr: ERROR: is a really long line, so we wrap it with '\'
59
aborting commit write group: PointlessCommit(No changes to commit)
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))
50
65
def test_commit_success(self):
51
66
"""Successful commit should not leave behind a bzr-commit-* file"""
107
122
'modified hello\.txt\n'
108
123
'Committed revision 2\.\n$')
125
def test_unicode_commit_message_is_filename(self):
126
"""Unicode commit message same as a filename (Bug #563646).
128
self.requireFeature(UnicodeFilenameFeature)
129
file_name = u'\N{euro sign}'
130
self.run_bzr(['init'])
131
open(file_name, 'w').write('hello world')
132
self.run_bzr(['add'])
133
out, err = self.run_bzr(['commit', '-m', file_name])
134
reflags = re.MULTILINE|re.DOTALL|re.UNICODE
135
te = osutils.get_terminal_encoding()
136
self.assertContainsRe(err.decode(te),
137
u'The commit message is a file name:',
140
# Run same test with a filename that causes encode
141
# error for the terminal encoding. We do this
142
# by forcing terminal encoding of ascii for
143
# osutils.get_terminal_encoding which is used
144
# by ui.text.show_warning
145
default_get_terminal_enc = osutils.get_terminal_encoding
147
osutils.get_terminal_encoding = lambda trace=None: 'ascii'
148
file_name = u'foo\u1234'
149
open(file_name, 'w').write('hello world')
150
self.run_bzr(['add'])
151
out, err = self.run_bzr(['commit', '-m', file_name])
152
reflags = re.MULTILINE|re.DOTALL|re.UNICODE
153
te = osutils.get_terminal_encoding()
154
self.assertContainsRe(err.decode(te, 'replace'),
155
u'The commit message is a file name:',
158
osutils.get_terminal_encoding = default_get_terminal_enc
110
160
def test_warn_about_forgotten_commit_message(self):
111
161
"""Test that the lack of -m parameter is caught"""
112
162
wt = self.make_branch_and_tree('.')
663
713
self.assertContainsRe(err, r'modified test\nCommitted revision 2.')
665
715
def test_commit_readonly_checkout(self):
666
# https://bugs.edge.launchpad.net/bzr/+bug/129701
716
# https://bugs.launchpad.net/bzr/+bug/129701
667
717
# "UnlockableTransport error trying to commit in checkout of readonly
669
719
self.make_branch('master')
681
731
f = file('fed.bat', 'w')
682
732
f.write('@rem dummy fed')
684
osutils.set_or_unset_env('BZR_EDITOR', "fed.bat")
734
self.overrideEnv('BZR_EDITOR', "fed.bat")
686
736
f = file('fed.sh', 'wb')
687
737
f.write('#!/bin/sh\n')
689
739
os.chmod('fed.sh', 0755)
690
osutils.set_or_unset_env('BZR_EDITOR', "./fed.sh")
740
self.overrideEnv('BZR_EDITOR', "./fed.sh")
692
742
def setup_commit_with_template(self):
693
743
self.setup_editor()
710
760
out, err = self.run_bzr_error(["empty commit message"],
711
761
"commit tree/hello.txt", stdin="n\n")
712
762
self.assertEqual(expected, tree.last_revision())
764
def test_commit_without_username(self):
765
"""Ensure commit error if username is not set.
767
self.run_bzr(['init', 'foo'])
769
open('foo.txt', 'w').write('hello')
770
self.run_bzr(['add'])
771
self.overrideEnv('EMAIL', None)
772
self.overrideEnv('BZR_EMAIL', None)
773
# Also, make sure that it's not inferred from mailname.
774
self.overrideAttr(config, '_auto_user_id',
775
lambda: (None, None))
776
out, err = self.run_bzr(['commit', '-m', 'initial'], 3)
777
self.assertContainsRe(err, 'Unable to determine your name')
779
def test_commit_recursive_checkout(self):
780
"""Ensure that a commit to a recursive checkout fails cleanly.
782
self.run_bzr(['init', 'test_branch'])
783
self.run_bzr(['checkout', 'test_branch', 'test_checkout'])
784
os.chdir('test_checkout')
785
self.run_bzr(['bind', '.']) # bind to self
786
open('foo.txt', 'w').write('hello')
787
self.run_bzr(['add'])
788
out, err = self.run_bzr(['commit', '-m', 'addedfoo'], 3)
789
self.assertEqual(out, '')
790
self.assertContainsRe(err,
791
'Branch.*test_checkout.*appears to be bound to itself')