1
# Copyright (C) 2005 by Canonical Ltd
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.
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.
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
20
from bzrlib.selftest import TestCaseInTempDir
21
from bzrlib.branch import Branch
22
from bzrlib.commit import Commit
23
from bzrlib.errors import PointlessCommit
26
class TestCommit(TestCaseInTempDir):
27
def test_simple_commit(self):
28
"""Commit and check two versions of a single file."""
29
b = Branch('.', init=True)
30
file('hello', 'w').write('hello world')
32
b.commit(message='add hello')
33
file_id = b.working_tree().path2id('hello')
35
file('hello', 'w').write('version 2')
36
b.commit(message='commit 2')
38
eq = self.assertEquals
40
rh = b.revision_history()
41
rev = b.get_revision(rh[0])
42
eq(rev.message, 'add hello')
44
tree1 = b.revision_tree(rh[0])
45
text = tree1.get_file_text(file_id)
46
eq(text, 'hello world')
48
tree2 = b.revision_tree(rh[1])
49
eq(tree2.get_file_text(file_id), 'version 2')
52
def test_delete_commit(self):
53
"""Test a commit with a deleted file"""
54
b = Branch('.', init=True)
55
file('hello', 'w').write('hello world')
56
b.add(['hello'], ['hello-id'])
57
b.commit(message='add hello')
60
b.commit('removed hello', rev_id='rev2')
62
tree = b.revision_tree('rev2')
63
self.assertFalse(tree.has_id('hello-id'))
66
def test_pointless_commit(self):
67
"""Commit refuses unless there are changes or it's forced."""
68
b = Branch('.', init=True)
69
file('hello', 'w').write('hello')
71
b.commit(message='add hello')
72
self.assertEquals(b.revno(), 1)
73
self.assertRaises(PointlessCommit,
76
allow_pointless=False)
77
self.assertEquals(b.revno(), 1)
81
def test_commit_empty(self):
82
"""Commiting an empty tree works."""
83
b = Branch('.', init=True)
84
b.commit(message='empty tree', allow_pointless=True)
85
self.assertRaises(PointlessCommit,
88
allow_pointless=False)
89
b.commit(message='empty tree', allow_pointless=True)
90
self.assertEquals(b.revno(), 2)
93
def test_selective_delete(self):
94
"""Selective commit in tree with deletions"""
95
b = Branch('.', init=True)
96
file('hello', 'w').write('hello')
97
file('buongia', 'w').write('buongia')
98
b.add(['hello', 'buongia'])
99
b.commit(message='add files')
102
file('buongia', 'w').write('new text')
103
b.commit(message='update text',
104
specific_files=['buongia'],
105
allow_pointless=False)
107
b.commit(message='remove hello',
108
specific_files=['hello'],
109
allow_pointless=False)
111
eq = self.assertEquals
116
def test_removed_commit(self):
117
"""Test a commit with a removed file"""
118
b = Branch('.', init=True)
119
file('hello', 'w').write('hello world')
120
b.add(['hello'], ['hello-id'])
121
b.commit(message='add hello')
124
b.commit('removed hello', rev_id='rev2')
126
tree = b.revision_tree('rev2')
127
self.assertFalse(tree.has_id('hello-id'))
130
if __name__ == '__main__':