~bzr-pqm/bzr/bzr.dev

1246 by Martin Pool
- add very simple commit tests
1
# Copyright (C) 2005 by Canonical Ltd
2
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.
7
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.
12
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
import os
19
20
from bzrlib.selftest import TestCaseInTempDir
21
from bzrlib.branch import Branch
22
from bzrlib.commit import Commit
1292 by Martin Pool
- add check that revision ids cannot be committed twice
23
from bzrlib.errors import PointlessCommit, BzrError
1246 by Martin Pool
- add very simple commit tests
24
25
1257 by Martin Pool
doc
26
# TODO: Test commit with some added, and added-but-missing files
27
1246 by Martin Pool
- add very simple commit tests
28
class TestCommit(TestCaseInTempDir):
1390 by Robert Collins
pair programming worx... merge integration and weave
29
1246 by Martin Pool
- add very simple commit tests
30
    def test_simple_commit(self):
31
        """Commit and check two versions of a single file."""
1390 by Robert Collins
pair programming worx... merge integration and weave
32
        b = Branch.initialize('.')
1246 by Martin Pool
- add very simple commit tests
33
        file('hello', 'w').write('hello world')
34
        b.add('hello')
35
        b.commit(message='add hello')
36
        file_id = b.working_tree().path2id('hello')
37
38
        file('hello', 'w').write('version 2')
39
        b.commit(message='commit 2')
40
41
        eq = self.assertEquals
42
        eq(b.revno(), 2)
43
        rh = b.revision_history()
44
        rev = b.get_revision(rh[0])
45
        eq(rev.message, 'add hello')
46
47
        tree1 = b.revision_tree(rh[0])
48
        text = tree1.get_file_text(file_id)
49
        eq(text, 'hello world')
50
51
        tree2 = b.revision_tree(rh[1])
52
        eq(tree2.get_file_text(file_id), 'version 2')
53
54
55
    def test_delete_commit(self):
56
        """Test a commit with a deleted file"""
1390 by Robert Collins
pair programming worx... merge integration and weave
57
        b = Branch.initialize('.')
1247 by Martin Pool
- tests for deletion and removal of files in commits
58
        file('hello', 'w').write('hello world')
59
        b.add(['hello'], ['hello-id'])
60
        b.commit(message='add hello')
61
62
        os.remove('hello')
63
        b.commit('removed hello', rev_id='rev2')
64
65
        tree = b.revision_tree('rev2')
66
        self.assertFalse(tree.has_id('hello-id'))
67
1246 by Martin Pool
- add very simple commit tests
68
1253 by Martin Pool
- test that pointless commits are trapped
69
    def test_pointless_commit(self):
70
        """Commit refuses unless there are changes or it's forced."""
1390 by Robert Collins
pair programming worx... merge integration and weave
71
        b = Branch.initialize('.')
1253 by Martin Pool
- test that pointless commits are trapped
72
        file('hello', 'w').write('hello')
73
        b.add(['hello'])
74
        b.commit(message='add hello')
75
        self.assertEquals(b.revno(), 1)
76
        self.assertRaises(PointlessCommit,
77
                          b.commit,
78
                          message='fails',
79
                          allow_pointless=False)
80
        self.assertEquals(b.revno(), 1)
81
        
82
83
1252 by Martin Pool
- add test for commit of an empty tree
84
    def test_commit_empty(self):
1253 by Martin Pool
- test that pointless commits are trapped
85
        """Commiting an empty tree works."""
1390 by Robert Collins
pair programming worx... merge integration and weave
86
        b = Branch.initialize('.')
1252 by Martin Pool
- add test for commit of an empty tree
87
        b.commit(message='empty tree', allow_pointless=True)
1253 by Martin Pool
- test that pointless commits are trapped
88
        self.assertRaises(PointlessCommit,
89
                          b.commit,
90
                          message='empty tree',
91
                          allow_pointless=False)
1252 by Martin Pool
- add test for commit of an empty tree
92
        b.commit(message='empty tree', allow_pointless=True)
93
        self.assertEquals(b.revno(), 2)
94
95
96
    def test_selective_delete(self):
97
        """Selective commit in tree with deletions"""
1390 by Robert Collins
pair programming worx... merge integration and weave
98
        b = Branch.initialize('.')
1254 by Martin Pool
- fix handling of selective commit with deleted files
99
        file('hello', 'w').write('hello')
100
        file('buongia', 'w').write('buongia')
1255 by Martin Pool
- more tests for selective commit of deletion
101
        b.add(['hello', 'buongia'],
102
              ['hello-id', 'buongia-id'])
103
        b.commit(message='add files',
104
                 rev_id='test@rev-1')
1254 by Martin Pool
- fix handling of selective commit with deleted files
105
        
106
        os.remove('hello')
107
        file('buongia', 'w').write('new text')
108
        b.commit(message='update text',
109
                 specific_files=['buongia'],
1255 by Martin Pool
- more tests for selective commit of deletion
110
                 allow_pointless=False,
111
                 rev_id='test@rev-2')
1254 by Martin Pool
- fix handling of selective commit with deleted files
112
113
        b.commit(message='remove hello',
114
                 specific_files=['hello'],
1255 by Martin Pool
- more tests for selective commit of deletion
115
                 allow_pointless=False,
116
                 rev_id='test@rev-3')
1254 by Martin Pool
- fix handling of selective commit with deleted files
117
118
        eq = self.assertEquals
119
        eq(b.revno(), 3)
1255 by Martin Pool
- more tests for selective commit of deletion
120
121
        tree2 = b.revision_tree('test@rev-2')
122
        self.assertTrue(tree2.has_filename('hello'))
123
        self.assertEquals(tree2.get_file_text('hello-id'), 'hello')
124
        self.assertEquals(tree2.get_file_text('buongia-id'), 'new text')
125
        
126
        tree3 = b.revision_tree('test@rev-3')
127
        self.assertFalse(tree3.has_filename('hello'))
128
        self.assertEquals(tree3.get_file_text('buongia-id'), 'new text')
1285 by Martin Pool
- fix bug in committing files that are renamed but not modified
129
130
131
    def test_commit_rename(self):
132
        """Test commit of a revision where a file is renamed."""
1390 by Robert Collins
pair programming worx... merge integration and weave
133
        b = Branch.initialize('.')
1285 by Martin Pool
- fix bug in committing files that are renamed but not modified
134
        self.build_tree(['hello'])
135
        b.add(['hello'], ['hello-id'])
136
        b.commit(message='one', rev_id='test@rev-1', allow_pointless=False)
137
138
        b.rename_one('hello', 'fruity')
139
        b.commit(message='renamed', rev_id='test@rev-2', allow_pointless=False)
140
1303 by Martin Pool
- commit updates entry_version
141
        eq = self.assertEquals
1285 by Martin Pool
- fix bug in committing files that are renamed but not modified
142
        tree1 = b.revision_tree('test@rev-1')
1303 by Martin Pool
- commit updates entry_version
143
        eq(tree1.id2path('hello-id'), 'hello')
144
        eq(tree1.get_file_text('hello-id'), 'contents of hello\n')
1285 by Martin Pool
- fix bug in committing files that are renamed but not modified
145
        self.assertFalse(tree1.has_filename('fruity'))
1291 by Martin Pool
- add test for moving files between directories
146
        self.check_inventory_shape(tree1.inventory, ['hello'])
1303 by Martin Pool
- commit updates entry_version
147
        ie = tree1.inventory['hello-id']
1092.2.21 by Robert Collins
convert name_version to revision in inventory entries
148
        eq(ie.revision, 'test@rev-1')
1285 by Martin Pool
- fix bug in committing files that are renamed but not modified
149
150
        tree2 = b.revision_tree('test@rev-2')
1303 by Martin Pool
- commit updates entry_version
151
        eq(tree2.id2path('hello-id'), 'fruity')
152
        eq(tree2.get_file_text('hello-id'), 'contents of hello\n')
1291 by Martin Pool
- add test for moving files between directories
153
        self.check_inventory_shape(tree2.inventory, ['fruity'])
1303 by Martin Pool
- commit updates entry_version
154
        ie = tree2.inventory['hello-id']
1092.2.21 by Robert Collins
convert name_version to revision in inventory entries
155
        eq(ie.revision, 'test@rev-2')
1291 by Martin Pool
- add test for moving files between directories
156
157
158
    def test_reused_rev_id(self):
1292 by Martin Pool
- add check that revision ids cannot be committed twice
159
        """Test that a revision id cannot be reused in a branch"""
1390 by Robert Collins
pair programming worx... merge integration and weave
160
        b = Branch.initialize('.')
1292 by Martin Pool
- add check that revision ids cannot be committed twice
161
        b.commit('initial', rev_id='test@rev-1', allow_pointless=True)
162
        self.assertRaises(Exception,
163
                          b.commit,
164
                          message='reused id',
165
                          rev_id='test@rev-1',
166
                          allow_pointless=True)
167
                          
1291 by Martin Pool
- add test for moving files between directories
168
169
170
    def test_commit_move(self):
171
        """Test commit of revisions with moved files and directories"""
1306 by Martin Pool
- tests that name_version is updated properly in renames/moves
172
        eq = self.assertEquals
1390 by Robert Collins
pair programming worx... merge integration and weave
173
        b = Branch.initialize('.')
1306 by Martin Pool
- tests that name_version is updated properly in renames/moves
174
        r1 = 'test@rev-1'
1291 by Martin Pool
- add test for moving files between directories
175
        self.build_tree(['hello', 'a/', 'b/'])
176
        b.add(['hello', 'a', 'b'], ['hello-id', 'a-id', 'b-id'])
1306 by Martin Pool
- tests that name_version is updated properly in renames/moves
177
        b.commit('initial', rev_id=r1, allow_pointless=False)
1291 by Martin Pool
- add test for moving files between directories
178
179
        b.move(['hello'], 'a')
1306 by Martin Pool
- tests that name_version is updated properly in renames/moves
180
        r2 = 'test@rev-2'
181
        b.commit('two', rev_id=r2, allow_pointless=False)
1291 by Martin Pool
- add test for moving files between directories
182
        self.check_inventory_shape(b.inventory,
183
                                   ['a', 'a/hello', 'b'])
184
185
        b.move(['b'], 'a')
1306 by Martin Pool
- tests that name_version is updated properly in renames/moves
186
        r3 = 'test@rev-3'
187
        b.commit('three', rev_id=r3, allow_pointless=False)
1291 by Martin Pool
- add test for moving files between directories
188
        self.check_inventory_shape(b.inventory,
189
                                   ['a', 'a/hello', 'a/b'])
1306 by Martin Pool
- tests that name_version is updated properly in renames/moves
190
        self.check_inventory_shape(b.get_revision_inventory(r3),
1291 by Martin Pool
- add test for moving files between directories
191
                                   ['a', 'a/hello', 'a/b'])
192
193
        b.move([os.sep.join(['a', 'hello'])],
194
               os.sep.join(['a', 'b']))
1306 by Martin Pool
- tests that name_version is updated properly in renames/moves
195
        r4 = 'test@rev-4'
196
        b.commit('four', rev_id=r4, allow_pointless=False)
1291 by Martin Pool
- add test for moving files between directories
197
        self.check_inventory_shape(b.inventory,
198
                                   ['a', 'a/b/hello', 'a/b'])
1306 by Martin Pool
- tests that name_version is updated properly in renames/moves
199
200
        inv = b.get_revision_inventory(r4)
1092.2.21 by Robert Collins
convert name_version to revision in inventory entries
201
        eq(inv['hello-id'].revision, r4)
202
        eq(inv['a-id'].revision, r1)
203
        eq(inv['b-id'].revision, r3)
1306 by Martin Pool
- tests that name_version is updated properly in renames/moves
204
205
        
1246 by Martin Pool
- add very simple commit tests
206
    def test_removed_commit(self):
207
        """Test a commit with a removed file"""
1390 by Robert Collins
pair programming worx... merge integration and weave
208
        b = Branch.initialize('.')
1247 by Martin Pool
- tests for deletion and removal of files in commits
209
        file('hello', 'w').write('hello world')
210
        b.add(['hello'], ['hello-id'])
211
        b.commit(message='add hello')
212
213
        b.remove('hello')
214
        b.commit('removed hello', rev_id='rev2')
215
216
        tree = b.revision_tree('rev2')
217
        self.assertFalse(tree.has_id('hello-id'))
1246 by Martin Pool
- add very simple commit tests
218
219
1256 by Martin Pool
- test that commits append to the tree's ancestry
220
    def test_committed_ancestry(self):
221
        """Test commit appends revisions to ancestry."""
1390 by Robert Collins
pair programming worx... merge integration and weave
222
        b = Branch.initialize('.')
1256 by Martin Pool
- test that commits append to the tree's ancestry
223
        rev_ids = []
224
        for i in range(4):
225
            file('hello', 'w').write((str(i) * 4) + '\n')
226
            if i == 0:
227
                b.add(['hello'], ['hello-id'])
228
            rev_id = 'test@rev-%d' % (i+1)
229
            rev_ids.append(rev_id)
230
            b.commit(message='rev %d' % (i+1),
231
                     rev_id=rev_id)
232
        eq = self.assertEquals
233
        eq(b.revision_history(), rev_ids)
234
        for i in range(4):
235
            anc = b.get_ancestry(rev_ids[i])
1390 by Robert Collins
pair programming worx... merge integration and weave
236
            eq(anc, [None] + rev_ids[:i+1])
1416 by Robert Collins
when committing a specific file, include all its parents
237
238
    def test_commit_new_subdir_child_selective(self):
239
        b = Branch.initialize('.')
240
        self.build_tree(['dir/', 'dir/file1', 'dir/file2'])
241
        b.add(['dir', 'dir/file1', 'dir/file2'],
242
              ['dirid', 'file1id', 'file2id'])
243
        b.commit('dir/file1', specific_files=['dir/file1'], rev_id='1')
244
        inv = b.get_inventory('1')
245
        self.assertEqual('1', inv['dirid'].revision)
246
        self.assertEqual('1', inv['file1id'].revision)
247
        # FIXME: This should raise a KeyError I think, rbc20051006
248
        self.assertRaises(BzrError, inv.__getitem__, 'file2id')