~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
1252 by Martin Pool
- add test for commit of an empty tree
23
from bzrlib.errors import PointlessCommit
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):
29
    def test_simple_commit(self):
30
        """Commit and check two versions of a single file."""
31
        b = Branch('.', init=True)
32
        file('hello', 'w').write('hello world')
33
        b.add('hello')
34
        b.commit(message='add hello')
35
        file_id = b.working_tree().path2id('hello')
36
37
        file('hello', 'w').write('version 2')
38
        b.commit(message='commit 2')
39
40
        eq = self.assertEquals
41
        eq(b.revno(), 2)
42
        rh = b.revision_history()
43
        rev = b.get_revision(rh[0])
44
        eq(rev.message, 'add hello')
45
46
        tree1 = b.revision_tree(rh[0])
47
        text = tree1.get_file_text(file_id)
48
        eq(text, 'hello world')
49
50
        tree2 = b.revision_tree(rh[1])
51
        eq(tree2.get_file_text(file_id), 'version 2')
52
53
54
    def test_delete_commit(self):
55
        """Test a commit with a deleted file"""
1247 by Martin Pool
- tests for deletion and removal of files in commits
56
        b = Branch('.', init=True)
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
1246 by Martin Pool
- add very simple commit tests
67
1253 by Martin Pool
- test that pointless commits are trapped
68
    def test_pointless_commit(self):
69
        """Commit refuses unless there are changes or it's forced."""
70
        b = Branch('.', init=True)
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
1252 by Martin Pool
- add test for commit of an empty tree
83
    def test_commit_empty(self):
1253 by Martin Pool
- test that pointless commits are trapped
84
        """Commiting an empty tree works."""
1252 by Martin Pool
- add test for commit of an empty tree
85
        b = Branch('.', init=True)
86
        b.commit(message='empty tree', allow_pointless=True)
1253 by Martin Pool
- test that pointless commits are trapped
87
        self.assertRaises(PointlessCommit,
88
                          b.commit,
89
                          message='empty tree',
90
                          allow_pointless=False)
1252 by Martin Pool
- add test for commit of an empty tree
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"""
1254 by Martin Pool
- fix handling of selective commit with deleted files
97
        b = Branch('.', init=True)
98
        file('hello', 'w').write('hello')
99
        file('buongia', 'w').write('buongia')
1255 by Martin Pool
- more tests for selective commit of deletion
100
        b.add(['hello', 'buongia'],
101
              ['hello-id', 'buongia-id'])
102
        b.commit(message='add files',
103
                 rev_id='test@rev-1')
1254 by Martin Pool
- fix handling of selective commit with deleted files
104
        
105
        os.remove('hello')
106
        file('buongia', 'w').write('new text')
107
        b.commit(message='update text',
108
                 specific_files=['buongia'],
1255 by Martin Pool
- more tests for selective commit of deletion
109
                 allow_pointless=False,
110
                 rev_id='test@rev-2')
1254 by Martin Pool
- fix handling of selective commit with deleted files
111
112
        b.commit(message='remove hello',
113
                 specific_files=['hello'],
1255 by Martin Pool
- more tests for selective commit of deletion
114
                 allow_pointless=False,
115
                 rev_id='test@rev-3')
1254 by Martin Pool
- fix handling of selective commit with deleted files
116
117
        eq = self.assertEquals
118
        eq(b.revno(), 3)
1255 by Martin Pool
- more tests for selective commit of deletion
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')
1285 by Martin Pool
- fix bug in committing files that are renamed but not modified
128
129
130
    def test_commit_rename(self):
131
        """Test commit of a revision where a file is renamed."""
132
        b = Branch('.', init=True)
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
        tree1 = b.revision_tree('test@rev-1')
141
        self.assertEquals(tree1.id2path('hello-id'), 'hello')
142
        self.assertEquals(tree1.get_file_text('hello-id'), 'contents of hello\n')
143
        self.assertFalse(tree1.has_filename('fruity'))
144
145
        tree2 = b.revision_tree('test@rev-2')
146
        self.assertEquals(tree2.id2path('hello-id'), 'fruity')
147
        self.assertEquals(tree2.get_file_text('hello-id'), 'contents of hello\n')
148
        self.assertFalse(tree2.has_filename('hello'))
1254 by Martin Pool
- fix handling of selective commit with deleted files
149
        
1252 by Martin Pool
- add test for commit of an empty tree
150
1246 by Martin Pool
- add very simple commit tests
151
    def test_removed_commit(self):
152
        """Test a commit with a removed file"""
1247 by Martin Pool
- tests for deletion and removal of files in commits
153
        b = Branch('.', init=True)
154
        file('hello', 'w').write('hello world')
155
        b.add(['hello'], ['hello-id'])
156
        b.commit(message='add hello')
157
158
        b.remove('hello')
159
        b.commit('removed hello', rev_id='rev2')
160
161
        tree = b.revision_tree('rev2')
162
        self.assertFalse(tree.has_id('hello-id'))
1246 by Martin Pool
- add very simple commit tests
163
164
1256 by Martin Pool
- test that commits append to the tree's ancestry
165
    def test_committed_ancestry(self):
166
        """Test commit appends revisions to ancestry."""
167
        b = Branch('.', init=True)
168
        rev_ids = []
169
        for i in range(4):
170
            file('hello', 'w').write((str(i) * 4) + '\n')
171
            if i == 0:
172
                b.add(['hello'], ['hello-id'])
173
            rev_id = 'test@rev-%d' % (i+1)
174
            rev_ids.append(rev_id)
175
            b.commit(message='rev %d' % (i+1),
176
                     rev_id=rev_id)
177
        eq = self.assertEquals
178
        eq(b.revision_history(), rev_ids)
179
        for i in range(4):
180
            anc = b.get_ancestry(rev_ids[i])
181
            eq(anc, rev_ids[:i+1])
182
            
183
        
184
185
1246 by Martin Pool
- add very simple commit tests
186
if __name__ == '__main__':
187
    import unittest
188
    unittest.main()
189