1
# Copyright (C) 2006, 2007, 2009, 2010 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., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
17
"""Black-box tests for bzr revert."""
22
from bzrlib.tests import TestCaseWithTransport
23
from bzrlib.trace import mutter
24
from bzrlib.workingtree import WorkingTree
27
class TestRevert(TestCaseWithTransport):
29
def _prepare_tree(self):
31
self.run_bzr('mkdir dir')
33
f = file('dir/file', 'wb')
36
self.run_bzr('add dir/file')
38
self.run_bzr('commit -m1')
41
f = file('dir/file', 'wb')
46
self.assertEquals('modified:\n dir/file\n', self.run_bzr('status')[0])
48
def _prepare_rename_mod_tree(self):
49
self.build_tree(['a/', 'a/b', 'a/c', 'a/d/', 'a/d/e', 'f/', 'f/g',
53
self.run_bzr('commit -m 1')
54
wt = WorkingTree.open('.')
55
wt.rename_one('a/b', 'f/b')
56
wt.rename_one('a/d/e', 'f/e')
57
wt.rename_one('a/d', 'f/d')
58
wt.rename_one('f/g', 'a/g')
59
wt.rename_one('f/h', 'h')
60
wt.rename_one('f', 'j')
62
def helper(self, param=''):
65
# revert to default revision for file in subdir does work
69
self.assertEquals('1\n', self.run_bzr('revno')[0])
70
self.run_bzr('revert %s file' % param)
71
self.assertEquals('spam', open('file', 'rb').read())
73
def test_revert_in_subdir(self):
76
def test_revert_to_revision_in_subdir(self):
77
# test case for bug #29424:
78
# revert to specific revision for file in subdir does not work
81
def test_revert_in_checkout(self):
85
self.run_bzr('checkout --lightweight . ../sprach')
86
self.run_bzr('commit -m more')
88
self.assertEqual('', self.run_bzr('status')[0])
89
self.run_bzr('revert')
90
self.assertEqual('', self.run_bzr('status')[0])
92
def test_revert_dirname(self):
93
"""Test that revert DIRECTORY does what's expected"""
94
self._prepare_rename_mod_tree()
95
self.run_bzr('revert a')
96
self.assertPathExists('a/b')
97
self.assertPathExists('a/d')
98
self.assertPathDoesNotExist('a/g')
100
"j is in the delta revert applies because j was renamed too",
101
self.assertPathExists, 'j')
102
self.assertPathExists('h')
103
self.run_bzr('revert f')
104
self.assertPathDoesNotExist('j')
105
self.assertPathDoesNotExist('h')
106
self.assertPathExists('a/d/e')
108
def test_revert_chatter(self):
109
self._prepare_rename_mod_tree()
110
chatter = self.run_bzr('revert')[1]
111
self.assertEqualDiff(
120
def test_revert(self):
123
with file('hello', 'wt') as f: f.write('foo')
124
self.run_bzr('add hello')
125
self.run_bzr('commit -m setup hello')
127
with file('goodbye', 'wt') as f: f.write('baz')
128
self.run_bzr('add goodbye')
129
self.run_bzr('commit -m setup goodbye')
131
with file('hello', 'wt') as f: f.write('bar')
132
with file('goodbye', 'wt') as f: f.write('qux')
133
self.run_bzr('revert hello')
134
self.check_file_contents('hello', 'foo')
135
self.check_file_contents('goodbye', 'qux')
136
self.run_bzr('revert')
137
self.check_file_contents('goodbye', 'baz')
139
os.mkdir('revertdir')
140
self.run_bzr('add revertdir')
141
self.run_bzr('commit -m f')
142
os.rmdir('revertdir')
143
self.run_bzr('revert')
145
if bzrlib.osutils.has_symlinks():
146
os.symlink('/unlikely/to/exist', 'symlink')
147
self.run_bzr('add symlink')
148
self.run_bzr('commit -m f')
150
self.run_bzr('revert')
151
self.assertPathExists('symlink')
153
os.symlink('a-different-path', 'symlink')
154
self.run_bzr('revert')
155
self.assertEqual('/unlikely/to/exist',
156
os.readlink('symlink'))
158
self.log("skipping revert symlink tests")
160
with file('hello', 'wt') as f: f.write('xyz')
161
self.run_bzr('commit -m xyz hello')
162
self.run_bzr('revert -r 1 hello')
163
self.check_file_contents('hello', 'foo')
164
self.run_bzr('revert hello')
165
self.check_file_contents('hello', 'xyz')
166
os.chdir('revertdir')
167
self.run_bzr('revert')
170
def test_revert_newly_added(self):
171
# this tests the UI reports reverting a newly added file
172
# correct (such files are not deleted)
173
tree = self.make_branch_and_tree('.')
174
self.build_tree(['file'])
176
out, err = self.run_bzr('revert')
177
self.assertEqual('', out)
178
self.assertEqual('- file\n', err)
180
def test_revert_removing_file(self):
181
# this tests the UI reports reverting a file which has been committed
182
# to a revision that did not have it, reports it as being deleted.
183
tree = self.make_branch_and_tree('.')
184
tree.commit('empty commit')
185
self.build_tree(['file'])
187
tree.commit('add file')
188
out, err = self.run_bzr('revert -r -2')
189
self.assertEqual('', out)
190
self.assertEqual('-D file\n', err)
192
def test_revert_forget_merges(self):
193
# revert --forget-merges removes any pending merges into the tree, but
194
# leaves the files unchanged
195
tree = self.make_branch_and_tree('.')
196
# forget-merges before first commit, though pointless, does not fail
197
self.run_bzr(['revert', '--forget-merges'])
198
self.build_tree(['file'])
199
first_rev_id = tree.commit('initial commit')
200
self.build_tree_contents([('file', 'new content')])
201
existing_parents = tree.get_parent_ids()
202
self.assertEquals([first_rev_id], existing_parents)
203
merged_parents = existing_parents + ['merged-in-rev']
204
tree.set_parent_ids(merged_parents)
205
self.assertEquals(merged_parents, tree.get_parent_ids())
206
self.run_bzr(['revert', '--forget-merges'])
207
self.assertEquals([first_rev_id], tree.get_parent_ids())
208
# changed files are not reverted
209
self.assertFileEqual('new content', 'file')
210
# you can give it the path of a tree
211
self.run_bzr(['revert', '--forget-merges', tree.abspath('.')])