~bzr-pqm/bzr/bzr.dev

2872.5.1 by Martin Pool
Avoid internal error tracebacks on failure to lock on readonly transport (#129701).
1
# Copyright (C) 2005, 2006, 2007 Canonical Ltd
1887.1.1 by Adeodato Simó
Do not separate paragraphs in the copyright statement with blank lines,
2
#
1508.1.22 by Robert Collins
implement out of date working tree checks in commit.
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.
1887.1.1 by Adeodato Simó
Do not separate paragraphs in the copyright statement with blank lines,
7
#
1508.1.22 by Robert Collins
implement out of date working tree checks in commit.
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.
1887.1.1 by Adeodato Simó
Do not separate paragraphs in the copyright statement with blank lines,
12
#
1508.1.22 by Robert Collins
implement out of date working tree checks in commit.
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
"""Tests for the commit CLI of bzr."""
19
20
import os
21
1551.9.5 by Aaron Bentley
Revert broken save-commit-message code
22
from bzrlib import (
2846.2.1 by Alexander Belchenko
merge approved chunks
23
    osutils,
1551.9.5 by Aaron Bentley
Revert broken save-commit-message code
24
    ignores,
25
    )
1669.2.1 by Martin Pool
verbose commit now specifically identifies modified/renamed/reparented files
26
from bzrlib.bzrdir import BzrDir
1508.1.22 by Robert Collins
implement out of date working tree checks in commit.
27
from bzrlib.tests.blackbox import ExternalBase
28
29
30
class TestCommit(ExternalBase):
31
1616.1.3 by Martin Pool
Clean up cut&pasted test for verbose commit
32
    def test_05_empty_commit(self):
33
        """Commit of tree with no versioned files should fail"""
34
        # If forced, it should succeed, but this is not tested here.
2664.13.2 by Daniel Watkins
tests.blackbox.test_commit now uses internals where appropriate.
35
        self.make_branch_and_tree('.')
1508.1.22 by Robert Collins
implement out of date working tree checks in commit.
36
        self.build_tree(['hello.txt'])
2552.2.3 by Vincent Ladeuil
Deprecate the varargs syntax and fix the tests.
37
        out,err = self.run_bzr('commit -m empty', retcode=3)
2089.1.1 by wang
If a commit fails, the commit message is stored in a file at the root of
38
        self.assertEqual('', out)
2747.6.7 by Daniel Watkins
Modify tests to reflect change in commit output.
39
        self.assertContainsRe(err, 'bzr: ERROR: no changes to commit\.'
2089.1.1 by wang
If a commit fails, the commit message is stored in a file at the root of
40
                                  ' use --unchanged to commit anyhow\n')
41
42
    def test_commit_success(self):
43
        """Successful commit should not leave behind a bzr-commit-* file"""
2664.13.2 by Daniel Watkins
tests.blackbox.test_commit now uses internals where appropriate.
44
        self.make_branch_and_tree('.')
2552.2.3 by Vincent Ladeuil
Deprecate the varargs syntax and fix the tests.
45
        self.run_bzr('commit --unchanged -m message')
2552.2.2 by Vincent Ladeuil
Enforce run_bzr(string) where possible.
46
        self.assertEqual('', self.run_bzr('unknowns')[0])
2089.1.1 by wang
If a commit fails, the commit message is stored in a file at the root of
47
48
        # same for unicode messages
2552.2.3 by Vincent Ladeuil
Deprecate the varargs syntax and fix the tests.
49
        self.run_bzr(["commit", "--unchanged", "-m", u'foo\xb5'])
2552.2.2 by Vincent Ladeuil
Enforce run_bzr(string) where possible.
50
        self.assertEqual('', self.run_bzr('unknowns')[0])
1616.1.3 by Martin Pool
Clean up cut&pasted test for verbose commit
51
1704.2.11 by Martin Pool
Handle 'bzr commit DIR' when dir contains pending merges.
52
    def test_commit_with_path(self):
53
        """Commit tree with path of root specified"""
2664.13.2 by Daniel Watkins
tests.blackbox.test_commit now uses internals where appropriate.
54
        a_tree = self.make_branch_and_tree('a')
1704.2.11 by Martin Pool
Handle 'bzr commit DIR' when dir contains pending merges.
55
        self.build_tree(['a/a_file'])
2664.13.2 by Daniel Watkins
tests.blackbox.test_commit now uses internals where appropriate.
56
        a_tree.add('a_file')
2552.2.5 by Vincent Ladeuil
Revert the intrusive run_bzr('commit') rewritings.
57
        self.run_bzr(['commit', '-m', 'first commit', 'a'])
1704.2.11 by Martin Pool
Handle 'bzr commit DIR' when dir contains pending merges.
58
2664.13.2 by Daniel Watkins
tests.blackbox.test_commit now uses internals where appropriate.
59
        b_tree = a_tree.bzrdir.sprout('b').open_workingtree()
1704.2.11 by Martin Pool
Handle 'bzr commit DIR' when dir contains pending merges.
60
        self.build_tree_contents([('b/a_file', 'changes in b')])
2552.2.5 by Vincent Ladeuil
Revert the intrusive run_bzr('commit') rewritings.
61
        self.run_bzr(['commit', '-m', 'first commit in b', 'b'])
1704.2.11 by Martin Pool
Handle 'bzr commit DIR' when dir contains pending merges.
62
63
        self.build_tree_contents([('a/a_file', 'new contents')])
2552.2.5 by Vincent Ladeuil
Revert the intrusive run_bzr('commit') rewritings.
64
        self.run_bzr(['commit', '-m', 'change in a', 'a'])
1704.2.11 by Martin Pool
Handle 'bzr commit DIR' when dir contains pending merges.
65
2664.13.2 by Daniel Watkins
tests.blackbox.test_commit now uses internals where appropriate.
66
        b_tree.merge_from_branch(a_tree.branch)
2738.4.2 by Daniel Watkins
Now test for conflicts where appropriate.
67
        self.assertEqual(len(b_tree.conflicts()), 1)
2552.2.3 by Vincent Ladeuil
Deprecate the varargs syntax and fix the tests.
68
        self.run_bzr('resolved b/a_file')
2552.2.5 by Vincent Ladeuil
Revert the intrusive run_bzr('commit') rewritings.
69
        self.run_bzr(['commit', '-m', 'merge into b', 'b'])
1704.2.11 by Martin Pool
Handle 'bzr commit DIR' when dir contains pending merges.
70
71
1616.1.3 by Martin Pool
Clean up cut&pasted test for verbose commit
72
    def test_10_verbose_commit(self):
73
        """Add one file and examine verbose commit output"""
2664.13.2 by Daniel Watkins
tests.blackbox.test_commit now uses internals where appropriate.
74
        tree = self.make_branch_and_tree('.')
1616.1.3 by Martin Pool
Clean up cut&pasted test for verbose commit
75
        self.build_tree(['hello.txt'])
2664.13.2 by Daniel Watkins
tests.blackbox.test_commit now uses internals where appropriate.
76
        tree.add("hello.txt")
2789.2.1 by Ian Clatworthy
Make commit less verbose by default
77
        out,err = self.run_bzr('commit -m added')
78
        self.assertEqual('', out)
2747.6.7 by Daniel Watkins
Modify tests to reflect change in commit output.
79
        self.assertContainsRe(err, '^Committing revision 1 to ".*"\.\n'
2789.2.11 by Ian Clatworthy
remove more reporting stuff
80
                              'added hello.txt\n'
81
                              'Committed revision 1.\n$',)
2789.2.1 by Ian Clatworthy
Make commit less verbose by default
82
1669.2.1 by Martin Pool
verbose commit now specifically identifies modified/renamed/reparented files
83
    def prepare_simple_history(self):
84
        """Prepare and return a working tree with one commit of one file"""
85
        # Commit with modified file should say so
86
        wt = BzrDir.create_standalone_workingtree('.')
87
        self.build_tree(['hello.txt', 'extra.txt'])
88
        wt.add(['hello.txt'])
89
        wt.commit(message='added')
90
        return wt
91
92
    def test_verbose_commit_modified(self):
93
        # Verbose commit of modified file should say so
94
        wt = self.prepare_simple_history()
95
        self.build_tree_contents([('hello.txt', 'new contents')])
2789.2.11 by Ian Clatworthy
remove more reporting stuff
96
        out, err = self.run_bzr('commit -m modified')
1669.2.1 by Martin Pool
verbose commit now specifically identifies modified/renamed/reparented files
97
        self.assertEqual('', out)
2747.6.7 by Daniel Watkins
Modify tests to reflect change in commit output.
98
        self.assertContainsRe(err, '^Committing revision 2 to ".*"\.\n'
99
                              'modified hello\.txt\n'
2789.2.11 by Ian Clatworthy
remove more reporting stuff
100
                              'Committed revision 2\.\n$')
2789.2.1 by Ian Clatworthy
Make commit less verbose by default
101
1669.2.1 by Martin Pool
verbose commit now specifically identifies modified/renamed/reparented files
102
    def test_verbose_commit_renamed(self):
103
        # Verbose commit of renamed file should say so
104
        wt = self.prepare_simple_history()
105
        wt.rename_one('hello.txt', 'gutentag.txt')
2789.2.11 by Ian Clatworthy
remove more reporting stuff
106
        out, err = self.run_bzr('commit -m renamed')
1669.2.1 by Martin Pool
verbose commit now specifically identifies modified/renamed/reparented files
107
        self.assertEqual('', out)
2747.6.7 by Daniel Watkins
Modify tests to reflect change in commit output.
108
        self.assertContainsRe(err, '^Committing revision 2 to ".*"\.\n'
109
                              'renamed hello\.txt => gutentag\.txt\n'
2789.2.11 by Ian Clatworthy
remove more reporting stuff
110
                              'Committed revision 2\.$\n')
2789.2.1 by Ian Clatworthy
Make commit less verbose by default
111
1669.2.1 by Martin Pool
verbose commit now specifically identifies modified/renamed/reparented files
112
    def test_verbose_commit_moved(self):
113
        # Verbose commit of file moved to new directory should say so
114
        wt = self.prepare_simple_history()
115
        os.mkdir('subdir')
116
        wt.add(['subdir'])
117
        wt.rename_one('hello.txt', 'subdir/hello.txt')
2789.2.11 by Ian Clatworthy
remove more reporting stuff
118
        out, err = self.run_bzr('commit -m renamed')
1669.2.1 by Martin Pool
verbose commit now specifically identifies modified/renamed/reparented files
119
        self.assertEqual('', out)
2747.6.7 by Daniel Watkins
Modify tests to reflect change in commit output.
120
        self.assertContainsRe(err, '^Committing revision 2 to ".*"\.\n'
121
                              'added subdir\n'
122
                              'renamed hello\.txt => subdir/hello\.txt\n'
2789.2.11 by Ian Clatworthy
remove more reporting stuff
123
                              'Committed revision 2\.\n$')
2789.2.1 by Ian Clatworthy
Make commit less verbose by default
124
1669.2.1 by Martin Pool
verbose commit now specifically identifies modified/renamed/reparented files
125
    def test_verbose_commit_with_unknown(self):
1616.1.3 by Martin Pool
Clean up cut&pasted test for verbose commit
126
        """Unknown files should not be listed by default in verbose output"""
127
        # Is that really the best policy?
1669.2.1 by Martin Pool
verbose commit now specifically identifies modified/renamed/reparented files
128
        wt = BzrDir.create_standalone_workingtree('.')
1616.1.3 by Martin Pool
Clean up cut&pasted test for verbose commit
129
        self.build_tree(['hello.txt', 'extra.txt'])
1669.2.1 by Martin Pool
verbose commit now specifically identifies modified/renamed/reparented files
130
        wt.add(['hello.txt'])
2789.2.11 by Ian Clatworthy
remove more reporting stuff
131
        out,err = self.run_bzr('commit -m added')
1616.1.3 by Martin Pool
Clean up cut&pasted test for verbose commit
132
        self.assertEqual('', out)
2789.2.7 by Ian Clatworthy
merge bzr.dev including updates to test_commit
133
        self.assertContainsRe(err, '^Committing revision 1 to ".*"\.\n'
2747.6.7 by Daniel Watkins
Modify tests to reflect change in commit output.
134
                              'added hello\.txt\n'
2789.2.11 by Ian Clatworthy
remove more reporting stuff
135
                              'Committed revision 1\.\n$')
2789.2.1 by Ian Clatworthy
Make commit less verbose by default
136
1668.1.5 by Martin Pool
[broken] fix up display of files changed by a commit
137
    def test_verbose_commit_with_unchanged(self):
1616.1.4 by Martin Pool
Verbose commit shouldn't talk about every unchanged file.
138
        """Unchanged files should not be listed by default in verbose output"""
2664.13.2 by Daniel Watkins
tests.blackbox.test_commit now uses internals where appropriate.
139
        tree = self.make_branch_and_tree('.')
1616.1.4 by Martin Pool
Verbose commit shouldn't talk about every unchanged file.
140
        self.build_tree(['hello.txt', 'unchanged.txt'])
2664.13.2 by Daniel Watkins
tests.blackbox.test_commit now uses internals where appropriate.
141
        tree.add('unchanged.txt')
2530.3.1 by Martin Pool
Cleanup old variations on run_bzr in the test suite
142
        self.run_bzr('commit -m unchanged unchanged.txt')
2664.13.2 by Daniel Watkins
tests.blackbox.test_commit now uses internals where appropriate.
143
        tree.add("hello.txt")
2789.2.11 by Ian Clatworthy
remove more reporting stuff
144
        out,err = self.run_bzr('commit -m added')
1616.1.4 by Martin Pool
Verbose commit shouldn't talk about every unchanged file.
145
        self.assertEqual('', out)
2747.6.7 by Daniel Watkins
Modify tests to reflect change in commit output.
146
        self.assertContainsRe(err, '^Committing revision 2 to ".*"\.\n'
147
                              'added hello\.txt\n'
2789.2.11 by Ian Clatworthy
remove more reporting stuff
148
                              'Committed revision 2\.$\n')
1508.1.22 by Robert Collins
implement out of date working tree checks in commit.
149
2747.6.13 by Daniel Watkins
Renamed test to reflect what it is actually doing.
150
    def test_verbose_commit_includes_master_location(self):
2747.6.4 by Daniel Watkins
Modified test as suggested on-list.
151
        """Location of master is displayed when committing to bound branch"""
2747.6.2 by Daniel Watkins
Added test for behaviour.
152
        a_tree = self.make_branch_and_tree('a')
153
        self.build_tree(['a/b'])
154
        a_tree.add('b')
155
        a_tree.commit(message='Initial message')
156
157
        b_tree = a_tree.branch.create_checkout('b')
2839.2.1 by Alexander Belchenko
using functions from osutils instead of os.path.*
158
        expected = "%s/" % (osutils.abspath('a'), )
2747.6.4 by Daniel Watkins
Modified test as suggested on-list.
159
        out, err = self.run_bzr('commit -m blah --unchanged', working_dir='b')
2747.6.7 by Daniel Watkins
Modify tests to reflect change in commit output.
160
        self.assertEqual(err, 'Committing revision 2 to "%s".\n'
161
                         'Committed revision 2.\n' % expected)
2747.6.2 by Daniel Watkins
Added test for behaviour.
162
1668.1.5 by Martin Pool
[broken] fix up display of files changed by a commit
163
    def test_commit_merge_reports_all_modified_files(self):
164
        # the commit command should show all the files that are shown by
165
        # bzr diff or bzr status when committing, even when they were not
166
        # changed by the user but rather through doing a merge.
167
        this_tree = self.make_branch_and_tree('this')
168
        # we need a bunch of files and dirs, to perform one action on each.
169
        self.build_tree([
170
            'this/dirtorename/',
171
            'this/dirtoreparent/',
172
            'this/dirtoleave/',
173
            'this/dirtoremove/',
174
            'this/filetoreparent',
175
            'this/filetorename',
176
            'this/filetomodify',
177
            'this/filetoremove',
178
            'this/filetoleave']
179
            )
180
        this_tree.add([
181
            'dirtorename',
182
            'dirtoreparent',
183
            'dirtoleave',
184
            'dirtoremove',
185
            'filetoreparent',
186
            'filetorename',
187
            'filetomodify',
188
            'filetoremove',
189
            'filetoleave']
190
            )
191
        this_tree.commit('create_files')
192
        other_dir = this_tree.bzrdir.sprout('other')
193
        other_tree = other_dir.open_workingtree()
194
        other_tree.lock_write()
195
        # perform the needed actions on the files and dirs.
196
        try:
197
            other_tree.rename_one('dirtorename', 'renameddir')
198
            other_tree.rename_one('dirtoreparent', 'renameddir/reparenteddir')
199
            other_tree.rename_one('filetorename', 'renamedfile')
2738.4.6 by Daniel Watkins
Rewrapped lines longer than 79 characters.
200
            other_tree.rename_one('filetoreparent',
201
                                  'renameddir/reparentedfile')
1668.1.5 by Martin Pool
[broken] fix up display of files changed by a commit
202
            other_tree.remove(['dirtoremove', 'filetoremove'])
203
            self.build_tree_contents([
2738.4.5 by Daniel Watkins
Fixed whitespace issues.
204
                ('other/newdir/',),
1668.1.5 by Martin Pool
[broken] fix up display of files changed by a commit
205
                ('other/filetomodify', 'new content'),
206
                ('other/newfile', 'new file content')])
207
            other_tree.add('newfile')
208
            other_tree.add('newdir/')
209
            other_tree.commit('modify all sample files and dirs.')
210
        finally:
211
            other_tree.unlock()
1979.2.1 by Robert Collins
(robertc) adds a convenience method "merge_from_branch" to WorkingTree.
212
        this_tree.merge_from_branch(other_tree.branch)
1668.1.5 by Martin Pool
[broken] fix up display of files changed by a commit
213
        os.chdir('this')
2552.2.3 by Vincent Ladeuil
Deprecate the varargs syntax and fix the tests.
214
        out,err = self.run_bzr('commit -m added')
1668.1.5 by Martin Pool
[broken] fix up display of files changed by a commit
215
        self.assertEqual('', out)
2839.2.1 by Alexander Belchenko
using functions from osutils instead of os.path.*
216
        expected = '%s/' % (osutils.getcwd(), )
1668.1.5 by Martin Pool
[broken] fix up display of files changed by a commit
217
        self.assertEqualDiff(
2747.6.7 by Daniel Watkins
Modify tests to reflect change in commit output.
218
            'Committing revision 2 to "%s".\n'
1668.1.5 by Martin Pool
[broken] fix up display of files changed by a commit
219
            'modified filetomodify\n'
220
            'added newdir\n'
221
            'added newfile\n'
222
            'renamed dirtorename => renameddir\n'
2829.1.1 by Ian Clatworthy
re-apply Aaron's fix for #94975 (Ian Clatworthy)
223
            'renamed filetorename => renamedfile\n'
1668.1.5 by Martin Pool
[broken] fix up display of files changed by a commit
224
            'renamed dirtoreparent => renameddir/reparenteddir\n'
225
            'renamed filetoreparent => renameddir/reparentedfile\n'
226
            'deleted dirtoremove\n'
227
            'deleted filetoremove\n'
2747.6.12 by Daniel Watkins
Modified tests to match new output.
228
            'Committed revision 2.\n' % (expected, ),
1668.1.5 by Martin Pool
[broken] fix up display of files changed by a commit
229
            err)
230
1508.1.22 by Robert Collins
implement out of date working tree checks in commit.
231
    def test_empty_commit_message(self):
2664.13.2 by Daniel Watkins
tests.blackbox.test_commit now uses internals where appropriate.
232
        tree = self.make_branch_and_tree('.')
233
        self.build_tree_contents([('foo.c', 'int main() {}')])
234
        tree.add('foo.c')
2530.3.1 by Martin Pool
Cleanup old variations on run_bzr in the test suite
235
        self.run_bzr('commit -m ""', retcode=3)
1508.1.22 by Robert Collins
implement out of date working tree checks in commit.
236
2625.9.2 by Daniel Watkins
Added test to ensure correct error message is given if an unencodable commit message is given at the command line.
237
    def test_unsupported_encoding_commit_message(self):
238
        tree = self.make_branch_and_tree('.')
239
        self.build_tree_contents([('foo.c', 'int main() {}')])
240
        tree.add('foo.c')
241
        out,err = self.run_bzr_subprocess('commit -m "\xff"', retcode=1,
242
                                                    env_changes={'LANG': 'C'})
243
        self.assertContainsRe(err, r'bzrlib.errors.BzrError: Parameter.*is '
244
                                    'unsupported by the current encoding.')
245
1508.1.22 by Robert Collins
implement out of date working tree checks in commit.
246
    def test_other_branch_commit(self):
247
        # this branch is to ensure consistent behaviour, whether we're run
248
        # inside a branch, or not.
2664.13.2 by Daniel Watkins
tests.blackbox.test_commit now uses internals where appropriate.
249
        outer_tree = self.make_branch_and_tree('.')
250
        inner_tree = self.make_branch_and_tree('branch')
251
        self.build_tree_contents([
252
            ('branch/foo.c', 'int main() {}'),
253
            ('branch/bar.c', 'int main() {}')])
254
        inner_tree.add('foo.c')
255
        inner_tree.add('bar.c')
1508.1.22 by Robert Collins
implement out of date working tree checks in commit.
256
        # can't commit files in different trees; sane error
2530.3.1 by Martin Pool
Cleanup old variations on run_bzr in the test suite
257
        self.run_bzr('commit -m newstuff branch/foo.c .', retcode=3)
258
        self.run_bzr('commit -m newstuff branch/foo.c')
259
        self.run_bzr('commit -m newstuff branch')
260
        self.run_bzr('commit -m newstuff branch', retcode=3)
1508.1.22 by Robert Collins
implement out of date working tree checks in commit.
261
262
    def test_out_of_date_tree_commit(self):
263
        # check we get an error code and a clear message committing with an out
264
        # of date checkout
2664.13.2 by Daniel Watkins
tests.blackbox.test_commit now uses internals where appropriate.
265
        tree = self.make_branch_and_tree('branch')
1508.1.22 by Robert Collins
implement out of date working tree checks in commit.
266
        # make a checkout
2664.13.2 by Daniel Watkins
tests.blackbox.test_commit now uses internals where appropriate.
267
        checkout = tree.branch.create_checkout('checkout', lightweight=True)
1508.1.22 by Robert Collins
implement out of date working tree checks in commit.
268
        # commit to the original branch to make the checkout out of date
2664.13.2 by Daniel Watkins
tests.blackbox.test_commit now uses internals where appropriate.
269
        tree.commit('message branch', allow_pointless=True)
1508.1.22 by Robert Collins
implement out of date working tree checks in commit.
270
        # now commit to the checkout should emit
271
        # ERROR: Out of date with the branch, 'bzr update' is suggested
2530.3.1 by Martin Pool
Cleanup old variations on run_bzr in the test suite
272
        output = self.run_bzr('commit --unchanged -m checkout_message '
1508.1.22 by Robert Collins
implement out of date working tree checks in commit.
273
                             'checkout', retcode=3)
274
        self.assertEqual(output,
275
                         ('',
2738.4.6 by Daniel Watkins
Rewrapped lines longer than 79 characters.
276
                          "bzr: ERROR: Working tree is out of date, please "
277
                          "run 'bzr update'.\n"))
1587.1.8 by Robert Collins
Local commits on unbound branches fail.
278
279
    def test_local_commit_unbound(self):
280
        # a --local commit on an unbound branch is an error
281
        self.make_branch_and_tree('.')
2552.2.3 by Vincent Ladeuil
Deprecate the varargs syntax and fix the tests.
282
        out, err = self.run_bzr('commit --local', retcode=3)
1587.1.8 by Robert Collins
Local commits on unbound branches fail.
283
        self.assertEqualDiff('', out)
284
        self.assertEqualDiff('bzr: ERROR: Cannot perform local-only commits '
285
                             'on unbound branches.\n', err)
1668.1.3 by Martin Pool
[patch] use the correct transaction when committing snapshot (Malone: #43959)
286
287
    def test_commit_a_text_merge_in_a_checkout(self):
288
        # checkouts perform multiple actions in a transaction across bond
289
        # branches and their master, and have been observed to fail in the
290
        # past. This is a user story reported to fail in bug #43959 where 
291
        # a merge done in a checkout (using the update command) failed to
292
        # commit correctly.
2664.13.2 by Daniel Watkins
tests.blackbox.test_commit now uses internals where appropriate.
293
        trunk = self.make_branch_and_tree('trunk')
1668.1.3 by Martin Pool
[patch] use the correct transaction when committing snapshot (Malone: #43959)
294
2664.13.2 by Daniel Watkins
tests.blackbox.test_commit now uses internals where appropriate.
295
        u1 = trunk.branch.create_checkout('u1')
1668.1.3 by Martin Pool
[patch] use the correct transaction when committing snapshot (Malone: #43959)
296
        self.build_tree_contents([('u1/hosts', 'initial contents')])
2664.13.2 by Daniel Watkins
tests.blackbox.test_commit now uses internals where appropriate.
297
        u1.add('hosts')
2552.2.3 by Vincent Ladeuil
Deprecate the varargs syntax and fix the tests.
298
        self.run_bzr('commit -m add-hosts u1')
1668.1.3 by Martin Pool
[patch] use the correct transaction when committing snapshot (Malone: #43959)
299
2664.13.2 by Daniel Watkins
tests.blackbox.test_commit now uses internals where appropriate.
300
        u2 = trunk.branch.create_checkout('u2')
1668.1.3 by Martin Pool
[patch] use the correct transaction when committing snapshot (Malone: #43959)
301
        self.build_tree_contents([('u2/hosts', 'altered in u2')])
2552.2.3 by Vincent Ladeuil
Deprecate the varargs syntax and fix the tests.
302
        self.run_bzr('commit -m checkin-from-u2 u2')
1668.1.3 by Martin Pool
[patch] use the correct transaction when committing snapshot (Malone: #43959)
303
304
        # make an offline commits
305
        self.build_tree_contents([('u1/hosts', 'first offline change in u1')])
2552.2.3 by Vincent Ladeuil
Deprecate the varargs syntax and fix the tests.
306
        self.run_bzr('commit -m checkin-offline --local u1')
1668.1.3 by Martin Pool
[patch] use the correct transaction when committing snapshot (Malone: #43959)
307
308
        # now try to pull in online work from u2, and then commit our offline
309
        # work as a merge
310
        # retcode 1 as we expect a text conflict
2552.2.3 by Vincent Ladeuil
Deprecate the varargs syntax and fix the tests.
311
        self.run_bzr('update u1', retcode=1)
312
        self.run_bzr('resolved u1/hosts')
1668.1.3 by Martin Pool
[patch] use the correct transaction when committing snapshot (Malone: #43959)
313
        # add a text change here to represent resolving the merge conflicts in
314
        # favour of a new version of the file not identical to either the u1
315
        # version or the u2 version.
316
        self.build_tree_contents([('u1/hosts', 'merge resolution\n')])
2552.2.3 by Vincent Ladeuil
Deprecate the varargs syntax and fix the tests.
317
        self.run_bzr('commit -m checkin-merge-of-the-offline-work-from-u1 u1')
1551.7.24 by Aaron Bentley
Ensure commit respects file spec when committing removals
318
319
    def test_commit_respects_spec_for_removals(self):
320
        """Commit with a file spec should only commit removals that match"""
321
        t = self.make_branch_and_tree('.')
322
        self.build_tree(['file-a', 'dir-a/', 'dir-a/file-b'])
323
        t.add(['file-a', 'dir-a', 'dir-a/file-b'])
324
        t.commit('Create')
325
        t.remove(['file-a', 'dir-a/file-b'])
326
        os.chdir('dir-a')
2552.2.3 by Vincent Ladeuil
Deprecate the varargs syntax and fix the tests.
327
        result = self.run_bzr('commit . -m removed-file-b')[1]
1551.7.24 by Aaron Bentley
Ensure commit respects file spec when committing removals
328
        self.assertNotContainsRe(result, 'file-a')
329
        result = self.run_bzr('status')[0]
330
        self.assertContainsRe(result, 'removed:\n  file-a')
2116.2.1 by John Arbash Meinel
Add commit --strict tests, and add a default ignore so that commit --strict works again
331
332
    def test_strict_commit(self):
333
        """Commit with --strict works if everything is known"""
1551.9.5 by Aaron Bentley
Revert broken save-commit-message code
334
        ignores._set_user_ignores([])
2116.2.1 by John Arbash Meinel
Add commit --strict tests, and add a default ignore so that commit --strict works again
335
        tree = self.make_branch_and_tree('tree')
336
        self.build_tree(['tree/a'])
337
        tree.add('a')
338
        # A simple change should just work
2552.2.3 by Vincent Ladeuil
Deprecate the varargs syntax and fix the tests.
339
        self.run_bzr('commit --strict -m adding-a',
2116.2.1 by John Arbash Meinel
Add commit --strict tests, and add a default ignore so that commit --strict works again
340
                     working_dir='tree')
341
342
    def test_strict_commit_no_changes(self):
343
        """commit --strict gives "no changes" if there is nothing to commit"""
344
        tree = self.make_branch_and_tree('tree')
345
        self.build_tree(['tree/a'])
346
        tree.add('a')
347
        tree.commit('adding a')
348
349
        # With no changes, it should just be 'no changes'
350
        # Make sure that commit is failing because there is nothing to do
351
        self.run_bzr_error(['no changes to commit'],
2552.2.3 by Vincent Ladeuil
Deprecate the varargs syntax and fix the tests.
352
                           'commit --strict -m no-changes',
2116.2.1 by John Arbash Meinel
Add commit --strict tests, and add a default ignore so that commit --strict works again
353
                           working_dir='tree')
354
355
        # But --strict doesn't care if you supply --unchanged
2552.2.3 by Vincent Ladeuil
Deprecate the varargs syntax and fix the tests.
356
        self.run_bzr('commit --strict --unchanged -m no-changes',
2116.2.1 by John Arbash Meinel
Add commit --strict tests, and add a default ignore so that commit --strict works again
357
                     working_dir='tree')
358
359
    def test_strict_commit_unknown(self):
360
        """commit --strict fails if a file is unknown"""
361
        tree = self.make_branch_and_tree('tree')
362
        self.build_tree(['tree/a'])
363
        tree.add('a')
364
        tree.commit('adding a')
365
366
        # Add one file so there is a change, but forget the other
367
        self.build_tree(['tree/b', 'tree/c'])
368
        tree.add('b')
369
        self.run_bzr_error(['Commit refused because there are unknown files'],
2552.2.3 by Vincent Ladeuil
Deprecate the varargs syntax and fix the tests.
370
                           'commit --strict -m add-b',
2116.2.1 by John Arbash Meinel
Add commit --strict tests, and add a default ignore so that commit --strict works again
371
                           working_dir='tree')
372
373
        # --no-strict overrides --strict
2552.2.3 by Vincent Ladeuil
Deprecate the varargs syntax and fix the tests.
374
        self.run_bzr('commit --strict -m add-b --no-strict',
2116.2.1 by John Arbash Meinel
Add commit --strict tests, and add a default ignore so that commit --strict works again
375
                     working_dir='tree')
2376.4.1 by jml at canonical
Blackbox-driven --fixes option to commit.
376
377
    def test_fixes_bug_output(self):
378
        """commit --fixes=lp:23452 succeeds without output."""
2376.4.22 by Jonathan Lange
Variety of whitespace cleanups, tightening of tests and docstring changes in
379
        tree = self.make_branch_and_tree('tree')
380
        self.build_tree(['tree/hello.txt'])
381
        tree.add('hello.txt')
2376.4.12 by Jonathan Lange
Update NEWS file.
382
        output, err = self.run_bzr(
2789.2.11 by Ian Clatworthy
remove more reporting stuff
383
            'commit -m hello --fixes=lp:23452 tree/hello.txt')
2376.4.1 by jml at canonical
Blackbox-driven --fixes option to commit.
384
        self.assertEqual('', output)
2747.6.7 by Daniel Watkins
Modify tests to reflect change in commit output.
385
        self.assertContainsRe(err, 'Committing revision 1 to ".*"\.\n'
386
                              'added hello\.txt\n'
2789.2.11 by Ian Clatworthy
remove more reporting stuff
387
                              'Committed revision 1\.\n')
2376.4.1 by jml at canonical
Blackbox-driven --fixes option to commit.
388
2453.2.1 by Martin Pool
Don't set the bugs property unless bugs are actually set
389
    def test_no_bugs_no_properties(self):
390
        """If no bugs are fixed, the bugs property is not set.
391
392
        see https://beta.launchpad.net/bzr/+bug/109613
393
        """
394
        tree = self.make_branch_and_tree('tree')
395
        self.build_tree(['tree/hello.txt'])
396
        tree.add('hello.txt')
2552.2.3 by Vincent Ladeuil
Deprecate the varargs syntax and fix the tests.
397
        self.run_bzr( 'commit -m hello tree/hello.txt')
2453.2.1 by Martin Pool
Don't set the bugs property unless bugs are actually set
398
        # Get the revision properties, ignoring the branch-nick property, which
399
        # we don't care about for this test.
400
        last_rev = tree.branch.repository.get_revision(tree.last_revision())
401
        properties = dict(last_rev.properties)
402
        del properties['branch-nick']
403
        self.assertFalse('bugs' in properties)
404
2376.4.1 by jml at canonical
Blackbox-driven --fixes option to commit.
405
    def test_fixes_bug_sets_property(self):
2376.4.2 by jml at canonical
More sophisticated error handling for --fixes option
406
        """commit --fixes=lp:234 sets the lp:234 revprop to 'fixed'."""
2376.4.1 by jml at canonical
Blackbox-driven --fixes option to commit.
407
        tree = self.make_branch_and_tree('tree')
408
        self.build_tree(['tree/hello.txt'])
409
        tree.add('hello.txt')
2552.2.3 by Vincent Ladeuil
Deprecate the varargs syntax and fix the tests.
410
        self.run_bzr('commit -m hello --fixes=lp:234 tree/hello.txt')
2376.4.1 by jml at canonical
Blackbox-driven --fixes option to commit.
411
412
        # Get the revision properties, ignoring the branch-nick property, which
413
        # we don't care about for this test.
414
        last_rev = tree.branch.repository.get_revision(tree.last_revision())
415
        properties = dict(last_rev.properties)
416
        del properties['branch-nick']
417
2376.4.18 by Jonathan Lange
Store all bug fix URLs in a single property.
418
        self.assertEqual({'bugs': 'https://launchpad.net/bugs/234 fixed'},
2376.4.7 by jml at canonical
- Add docstrings to tests.
419
                         properties)
2376.4.1 by jml at canonical
Blackbox-driven --fixes option to commit.
420
421
    def test_fixes_multiple_bugs_sets_properties(self):
422
        """--fixes can be used more than once to show that bugs are fixed."""
423
        tree = self.make_branch_and_tree('tree')
424
        self.build_tree(['tree/hello.txt'])
425
        tree.add('hello.txt')
2552.2.3 by Vincent Ladeuil
Deprecate the varargs syntax and fix the tests.
426
        self.run_bzr('commit -m hello --fixes=lp:123 --fixes=lp:235'
427
                     ' tree/hello.txt')
2376.4.1 by jml at canonical
Blackbox-driven --fixes option to commit.
428
429
        # Get the revision properties, ignoring the branch-nick property, which
430
        # we don't care about for this test.
431
        last_rev = tree.branch.repository.get_revision(tree.last_revision())
432
        properties = dict(last_rev.properties)
433
        del properties['branch-nick']
434
2376.4.18 by Jonathan Lange
Store all bug fix URLs in a single property.
435
        self.assertEqual(
2376.4.21 by Jonathan Lange
Change the bugs separator to \n from ,
436
            {'bugs': 'https://launchpad.net/bugs/123 fixed\n'
2376.4.18 by Jonathan Lange
Store all bug fix URLs in a single property.
437
                     'https://launchpad.net/bugs/235 fixed'},
438
            properties)
2376.4.7 by jml at canonical
- Add docstrings to tests.
439
440
    def test_fixes_bug_with_alternate_trackers(self):
441
        """--fixes can be used on a properly configured branch to mark bug
442
        fixes on multiple trackers.
443
        """
444
        tree = self.make_branch_and_tree('tree')
445
        tree.branch.get_config().set_user_option(
446
            'trac_twisted_url', 'http://twistedmatrix.com/trac')
447
        self.build_tree(['tree/hello.txt'])
448
        tree.add('hello.txt')
2552.2.3 by Vincent Ladeuil
Deprecate the varargs syntax and fix the tests.
449
        self.run_bzr('commit -m hello --fixes=lp:123 --fixes=twisted:235 tree/')
2376.4.7 by jml at canonical
- Add docstrings to tests.
450
451
        # Get the revision properties, ignoring the branch-nick property, which
452
        # we don't care about for this test.
453
        last_rev = tree.branch.repository.get_revision(tree.last_revision())
454
        properties = dict(last_rev.properties)
455
        del properties['branch-nick']
456
2376.4.18 by Jonathan Lange
Store all bug fix URLs in a single property.
457
        self.assertEqual(
2376.4.21 by Jonathan Lange
Change the bugs separator to \n from ,
458
            {'bugs': 'https://launchpad.net/bugs/123 fixed\n'
2376.4.18 by Jonathan Lange
Store all bug fix URLs in a single property.
459
                     'http://twistedmatrix.com/trac/ticket/235 fixed'},
460
            properties)
2376.4.2 by jml at canonical
More sophisticated error handling for --fixes option
461
462
    def test_fixes_unknown_bug_prefix(self):
463
        tree = self.make_branch_and_tree('tree')
464
        self.build_tree(['tree/hello.txt'])
465
        tree.add('hello.txt')
466
        self.run_bzr_error(
467
            ["Unrecognized bug %s. Commit refused." % 'xxx:123'],
2552.2.3 by Vincent Ladeuil
Deprecate the varargs syntax and fix the tests.
468
            'commit -m add-b --fixes=xxx:123',
2376.4.2 by jml at canonical
More sophisticated error handling for --fixes option
469
            working_dir='tree')
470
471
    def test_fixes_invalid_bug_number(self):
472
        tree = self.make_branch_and_tree('tree')
473
        self.build_tree(['tree/hello.txt'])
474
        tree.add('hello.txt')
475
        self.run_bzr_error(
2376.4.7 by jml at canonical
- Add docstrings to tests.
476
            ["Invalid bug identifier for %s. Commit refused." % 'lp:orange'],
2552.2.3 by Vincent Ladeuil
Deprecate the varargs syntax and fix the tests.
477
            'commit -m add-b --fixes=lp:orange',
2376.4.2 by jml at canonical
More sophisticated error handling for --fixes option
478
            working_dir='tree')
2376.4.7 by jml at canonical
- Add docstrings to tests.
479
480
    def test_fixes_invalid_argument(self):
481
        """Raise an appropriate error when the fixes argument isn't tag:id."""
482
        tree = self.make_branch_and_tree('tree')
483
        self.build_tree(['tree/hello.txt'])
484
        tree.add('hello.txt')
485
        self.run_bzr_error(
2376.4.13 by Jonathan Lange
Some stylistic cleanups
486
            [r"Invalid bug orange. Must be in the form of 'tag:id'\. "
487
             r"Commit refused\."],
2552.2.3 by Vincent Ladeuil
Deprecate the varargs syntax and fix the tests.
488
            'commit -m add-b --fixes=orange',
2376.4.7 by jml at canonical
- Add docstrings to tests.
489
            working_dir='tree')
2671.2.1 by Lukáš Lalinský
Add --author option to 'bzr commit' to record the author's name, if it's different from the committer.
490
491
    def test_no_author(self):
492
        """If the author is not specified, the author property is not set."""
493
        tree = self.make_branch_and_tree('tree')
494
        self.build_tree(['tree/hello.txt'])
495
        tree.add('hello.txt')
496
        self.run_bzr( 'commit -m hello tree/hello.txt')
497
        last_rev = tree.branch.repository.get_revision(tree.last_revision())
2671.2.2 by Lukáš Lalinský
Move setting of the author revision property to MutableTree.commit. Don't use try/except KeyError in LongLogFormatter to display authors and branch-nicks. Removed warning about missing e-mail in the authors name.
498
        properties = last_rev.properties
2671.2.1 by Lukáš Lalinský
Add --author option to 'bzr commit' to record the author's name, if it's different from the committer.
499
        self.assertFalse('author' in properties)
500
501
    def test_author_sets_property(self):
2671.2.2 by Lukáš Lalinský
Move setting of the author revision property to MutableTree.commit. Don't use try/except KeyError in LongLogFormatter to display authors and branch-nicks. Removed warning about missing e-mail in the authors name.
502
        """commit --author='John Doe <jdoe@example.com>' sets the author
503
           revprop.
504
        """
2671.2.1 by Lukáš Lalinský
Add --author option to 'bzr commit' to record the author's name, if it's different from the committer.
505
        tree = self.make_branch_and_tree('tree')
506
        self.build_tree(['tree/hello.txt'])
507
        tree.add('hello.txt')
2671.2.4 by Lukáš Lalinský
Fixed broken test_author_* blackbox tests.
508
        self.run_bzr("commit -m hello --author='John Doe <jdoe@example.com>' "
2671.2.2 by Lukáš Lalinský
Move setting of the author revision property to MutableTree.commit. Don't use try/except KeyError in LongLogFormatter to display authors and branch-nicks. Removed warning about missing e-mail in the authors name.
509
                     "tree/hello.txt")
2671.2.1 by Lukáš Lalinský
Add --author option to 'bzr commit' to record the author's name, if it's different from the committer.
510
        last_rev = tree.branch.repository.get_revision(tree.last_revision())
2671.2.2 by Lukáš Lalinský
Move setting of the author revision property to MutableTree.commit. Don't use try/except KeyError in LongLogFormatter to display authors and branch-nicks. Removed warning about missing e-mail in the authors name.
511
        properties = last_rev.properties
512
        self.assertEqual('John Doe <jdoe@example.com>', properties['author'])
2671.2.1 by Lukáš Lalinský
Add --author option to 'bzr commit' to record the author's name, if it's different from the committer.
513
514
    def test_author_no_email(self):
2671.2.2 by Lukáš Lalinský
Move setting of the author revision property to MutableTree.commit. Don't use try/except KeyError in LongLogFormatter to display authors and branch-nicks. Removed warning about missing e-mail in the authors name.
515
        """Author's name without an email address is allowed, too."""
2671.2.1 by Lukáš Lalinský
Add --author option to 'bzr commit' to record the author's name, if it's different from the committer.
516
        tree = self.make_branch_and_tree('tree')
517
        self.build_tree(['tree/hello.txt'])
518
        tree.add('hello.txt')
2671.2.4 by Lukáš Lalinský
Fixed broken test_author_* blackbox tests.
519
        out, err = self.run_bzr("commit -m hello --author='John Doe' "
2671.2.2 by Lukáš Lalinský
Move setting of the author revision property to MutableTree.commit. Don't use try/except KeyError in LongLogFormatter to display authors and branch-nicks. Removed warning about missing e-mail in the authors name.
520
                                "tree/hello.txt")
2671.2.1 by Lukáš Lalinský
Add --author option to 'bzr commit' to record the author's name, if it's different from the committer.
521
        last_rev = tree.branch.repository.get_revision(tree.last_revision())
2671.2.2 by Lukáš Lalinský
Move setting of the author revision property to MutableTree.commit. Don't use try/except KeyError in LongLogFormatter to display authors and branch-nicks. Removed warning about missing e-mail in the authors name.
522
        properties = last_rev.properties
2671.2.1 by Lukáš Lalinský
Add --author option to 'bzr commit' to record the author's name, if it's different from the committer.
523
        self.assertEqual('John Doe', properties['author'])
2833.2.1 by Alexander Belchenko
XFAIL test for bug #140419
524
525
    def test_partial_commit_with_renames_in_tree(self):
2833.2.2 by Alexander Belchenko
Bug #140419 fixed by Robert Collins
526
        # this test illustrates bug #140419
2833.2.1 by Alexander Belchenko
XFAIL test for bug #140419
527
        t = self.make_branch_and_tree('.')
528
        self.build_tree(['dir/', 'dir/a', 'test'])
529
        t.add(['dir/', 'dir/a', 'test'])
530
        t.commit('initial commit')
531
        # important part: file dir/a should change parent
532
        # and should appear before old parent
533
        # then during partial commit we have error
534
        # parent_id {dir-XXX} not in inventory
535
        t.rename_one('dir/a', 'a')
536
        self.build_tree_contents([('test', 'changes in test')])
2833.2.2 by Alexander Belchenko
Bug #140419 fixed by Robert Collins
537
        # partial commit
538
        out, err = self.run_bzr('commit test -m "partial commit"')
539
        self.assertEquals('', out)
540
        self.assertContainsRe(err, r'modified test\nCommitted revision 2.')
2872.5.1 by Martin Pool
Avoid internal error tracebacks on failure to lock on readonly transport (#129701).
541
542
    def test_commit_readonly_checkout(self):
543
        # https://bugs.edge.launchpad.net/bzr/+bug/129701
544
        # "UnlockableTransport error trying to commit in checkout of readonly
545
        # branch"
546
        self.make_branch('master')
547
        master = BzrDir.open_from_transport(
548
            self.get_readonly_transport('master')).open_branch()
549
        master.create_checkout('checkout')
550
        out, err = self.run_bzr(['commit', '--unchanged', '-mfoo', 'checkout'],
551
            retcode=3)
552
        self.assertContainsRe(err,
553
            r'^bzr: ERROR: Cannot lock.*readonly transport\n$')