~bzr-pqm/bzr/bzr.dev

« back to all changes in this revision

Viewing changes to bzrlib/selftest/test_commit.py

  • Committer: Robert Collins
  • Date: 2005-10-17 00:00:33 UTC
  • mfrom: (1458)
  • mto: This revision was merged to the branch mainline in revision 1459.
  • Revision ID: robertc@lifelesslap.robertcollins.net-20051017000033-b7385b1772d31605
mergeĀ inĀ integration

Show diffs side-by-side

added added

removed removed

Lines of Context:
 
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
 
23
from bzrlib.errors import PointlessCommit, BzrError
 
24
 
 
25
 
 
26
# TODO: Test commit with some added, and added-but-missing files
 
27
 
 
28
class TestCommit(TestCaseInTempDir):
 
29
 
 
30
    def test_simple_commit(self):
 
31
        """Commit and check two versions of a single file."""
 
32
        b = Branch.initialize('.')
 
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
    def test_delete_commit(self):
 
55
        """Test a commit with a deleted file"""
 
56
        b = Branch.initialize('.')
 
57
        file('hello', 'w').write('hello world')
 
58
        b.add(['hello'], ['hello-id'])
 
59
        b.commit(message='add hello')
 
60
 
 
61
        os.remove('hello')
 
62
        b.commit('removed hello', rev_id='rev2')
 
63
 
 
64
        tree = b.revision_tree('rev2')
 
65
        self.assertFalse(tree.has_id('hello-id'))
 
66
 
 
67
 
 
68
    def test_pointless_commit(self):
 
69
        """Commit refuses unless there are changes or it's forced."""
 
70
        b = Branch.initialize('.')
 
71
        file('hello', 'w').write('hello')
 
72
        b.add(['hello'])
 
73
        b.commit(message='add hello')
 
74
        self.assertEquals(b.revno(), 1)
 
75
        self.assertRaises(PointlessCommit,
 
76
                          b.commit,
 
77
                          message='fails',
 
78
                          allow_pointless=False)
 
79
        self.assertEquals(b.revno(), 1)
 
80
        
 
81
 
 
82
 
 
83
    def test_commit_empty(self):
 
84
        """Commiting an empty tree works."""
 
85
        b = Branch.initialize('.')
 
86
        b.commit(message='empty tree', allow_pointless=True)
 
87
        self.assertRaises(PointlessCommit,
 
88
                          b.commit,
 
89
                          message='empty tree',
 
90
                          allow_pointless=False)
 
91
        b.commit(message='empty tree', allow_pointless=True)
 
92
        self.assertEquals(b.revno(), 2)
 
93
 
 
94
 
 
95
    def test_selective_delete(self):
 
96
        """Selective commit in tree with deletions"""
 
97
        b = Branch.initialize('.')
 
98
        file('hello', 'w').write('hello')
 
99
        file('buongia', 'w').write('buongia')
 
100
        b.add(['hello', 'buongia'],
 
101
              ['hello-id', 'buongia-id'])
 
102
        b.commit(message='add files',
 
103
                 rev_id='test@rev-1')
 
104
        
 
105
        os.remove('hello')
 
106
        file('buongia', 'w').write('new text')
 
107
        b.commit(message='update text',
 
108
                 specific_files=['buongia'],
 
109
                 allow_pointless=False,
 
110
                 rev_id='test@rev-2')
 
111
 
 
112
        b.commit(message='remove hello',
 
113
                 specific_files=['hello'],
 
114
                 allow_pointless=False,
 
115
                 rev_id='test@rev-3')
 
116
 
 
117
        eq = self.assertEquals
 
118
        eq(b.revno(), 3)
 
119
 
 
120
        tree2 = b.revision_tree('test@rev-2')
 
121
        self.assertTrue(tree2.has_filename('hello'))
 
122
        self.assertEquals(tree2.get_file_text('hello-id'), 'hello')
 
123
        self.assertEquals(tree2.get_file_text('buongia-id'), 'new text')
 
124
        
 
125
        tree3 = b.revision_tree('test@rev-3')
 
126
        self.assertFalse(tree3.has_filename('hello'))
 
127
        self.assertEquals(tree3.get_file_text('buongia-id'), 'new text')
 
128
 
 
129
 
 
130
    def test_commit_rename(self):
 
131
        """Test commit of a revision where a file is renamed."""
 
132
        b = Branch.initialize('.')
 
133
        self.build_tree(['hello'])
 
134
        b.add(['hello'], ['hello-id'])
 
135
        b.commit(message='one', rev_id='test@rev-1', allow_pointless=False)
 
136
 
 
137
        b.rename_one('hello', 'fruity')
 
138
        b.commit(message='renamed', rev_id='test@rev-2', allow_pointless=False)
 
139
 
 
140
        eq = self.assertEquals
 
141
        tree1 = b.revision_tree('test@rev-1')
 
142
        eq(tree1.id2path('hello-id'), 'hello')
 
143
        eq(tree1.get_file_text('hello-id'), 'contents of hello\n')
 
144
        self.assertFalse(tree1.has_filename('fruity'))
 
145
        self.check_inventory_shape(tree1.inventory, ['hello'])
 
146
        ie = tree1.inventory['hello-id']
 
147
        eq(ie.revision, 'test@rev-1')
 
148
 
 
149
        tree2 = b.revision_tree('test@rev-2')
 
150
        eq(tree2.id2path('hello-id'), 'fruity')
 
151
        eq(tree2.get_file_text('hello-id'), 'contents of hello\n')
 
152
        self.check_inventory_shape(tree2.inventory, ['fruity'])
 
153
        ie = tree2.inventory['hello-id']
 
154
        eq(ie.revision, 'test@rev-2')
 
155
 
 
156
 
 
157
    def test_reused_rev_id(self):
 
158
        """Test that a revision id cannot be reused in a branch"""
 
159
        b = Branch.initialize('.')
 
160
        b.commit('initial', rev_id='test@rev-1', allow_pointless=True)
 
161
        self.assertRaises(Exception,
 
162
                          b.commit,
 
163
                          message='reused id',
 
164
                          rev_id='test@rev-1',
 
165
                          allow_pointless=True)
 
166
                          
 
167
 
 
168
 
 
169
    def test_commit_move(self):
 
170
        """Test commit of revisions with moved files and directories"""
 
171
        eq = self.assertEquals
 
172
        b = Branch.initialize('.')
 
173
        r1 = 'test@rev-1'
 
174
        self.build_tree(['hello', 'a/', 'b/'])
 
175
        b.add(['hello', 'a', 'b'], ['hello-id', 'a-id', 'b-id'])
 
176
        b.commit('initial', rev_id=r1, allow_pointless=False)
 
177
 
 
178
        b.move(['hello'], 'a')
 
179
        r2 = 'test@rev-2'
 
180
        b.commit('two', rev_id=r2, allow_pointless=False)
 
181
        self.check_inventory_shape(b.inventory,
 
182
                                   ['a', 'a/hello', 'b'])
 
183
 
 
184
        b.move(['b'], 'a')
 
185
        r3 = 'test@rev-3'
 
186
        b.commit('three', rev_id=r3, allow_pointless=False)
 
187
        self.check_inventory_shape(b.inventory,
 
188
                                   ['a', 'a/hello', 'a/b'])
 
189
        self.check_inventory_shape(b.get_revision_inventory(r3),
 
190
                                   ['a', 'a/hello', 'a/b'])
 
191
 
 
192
        b.move([os.sep.join(['a', 'hello'])],
 
193
               os.sep.join(['a', 'b']))
 
194
        r4 = 'test@rev-4'
 
195
        b.commit('four', rev_id=r4, allow_pointless=False)
 
196
        self.check_inventory_shape(b.inventory,
 
197
                                   ['a', 'a/b/hello', 'a/b'])
 
198
 
 
199
        inv = b.get_revision_inventory(r4)
 
200
        eq(inv['hello-id'].revision, r4)
 
201
        eq(inv['a-id'].revision, r1)
 
202
        eq(inv['b-id'].revision, r3)
 
203
 
 
204
        
 
205
    def test_removed_commit(self):
 
206
        """Test a commit with a removed file"""
 
207
        b = Branch.initialize('.')
 
208
        file('hello', 'w').write('hello world')
 
209
        b.add(['hello'], ['hello-id'])
 
210
        b.commit(message='add hello')
 
211
 
 
212
        b.remove('hello')
 
213
        b.commit('removed hello', rev_id='rev2')
 
214
 
 
215
        tree = b.revision_tree('rev2')
 
216
        self.assertFalse(tree.has_id('hello-id'))
 
217
 
 
218
 
 
219
    def test_committed_ancestry(self):
 
220
        """Test commit appends revisions to ancestry."""
 
221
        b = Branch.initialize('.')
 
222
        rev_ids = []
 
223
        for i in range(4):
 
224
            file('hello', 'w').write((str(i) * 4) + '\n')
 
225
            if i == 0:
 
226
                b.add(['hello'], ['hello-id'])
 
227
            rev_id = 'test@rev-%d' % (i+1)
 
228
            rev_ids.append(rev_id)
 
229
            b.commit(message='rev %d' % (i+1),
 
230
                     rev_id=rev_id)
 
231
        eq = self.assertEquals
 
232
        eq(b.revision_history(), rev_ids)
 
233
        for i in range(4):
 
234
            anc = b.get_ancestry(rev_ids[i])
 
235
            eq(anc, [None] + rev_ids[:i+1])
 
236
 
 
237
    def test_commit_new_subdir_child_selective(self):
 
238
        b = Branch.initialize('.')
 
239
        self.build_tree(['dir/', 'dir/file1', 'dir/file2'])
 
240
        b.add(['dir', 'dir/file1', 'dir/file2'],
 
241
              ['dirid', 'file1id', 'file2id'])
 
242
        b.commit('dir/file1', specific_files=['dir/file1'], rev_id='1')
 
243
        inv = b.get_inventory('1')
 
244
        self.assertEqual('1', inv['dirid'].revision)
 
245
        self.assertEqual('1', inv['file1id'].revision)
 
246
        # FIXME: This should raise a KeyError I think, rbc20051006
 
247
        self.assertRaises(BzrError, inv.__getitem__, 'file2id')