~bzr-pqm/bzr/bzr.dev

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
# Copyright (C) 2006, 2007, 2009, 2010 Canonical Ltd
#
# This program is free software; you can redistribute it and/or modify
# it under the terms of the GNU General Public License as published by
# the Free Software Foundation; either version 2 of the License, or
# (at your option) any later version.
#
# This program is distributed in the hope that it will be useful,
# but WITHOUT ANY WARRANTY; without even the implied warranty of
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
# GNU General Public License for more details.
#
# You should have received a copy of the GNU General Public License
# along with this program; if not, write to the Free Software
# Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA

"""Black-box tests for bzr revert."""

import os

import bzrlib.osutils
from bzrlib.tests import TestCaseWithTransport
from bzrlib.trace import mutter
from bzrlib.workingtree import WorkingTree


class TestRevert(TestCaseWithTransport):

    def _prepare_tree(self):
        self.run_bzr('init')
        self.run_bzr('mkdir dir')

        f = file('dir/file', 'wb')
        f.write('spam')
        f.close()
        self.run_bzr('add dir/file')

        self.run_bzr('commit -m1')

        # modify file
        f = file('dir/file', 'wb')
        f.write('eggs')
        f.close()

        # check status
        self.assertEquals('modified:\n  dir/file\n', self.run_bzr('status')[0])

    def _prepare_rename_mod_tree(self):
        self.build_tree(['a/', 'a/b', 'a/c', 'a/d/', 'a/d/e', 'f/', 'f/g',
                         'f/h', 'f/i'])
        self.run_bzr('init')
        self.run_bzr('add')
        self.run_bzr('commit -m 1')
        wt = WorkingTree.open('.')
        wt.rename_one('a/b', 'f/b')
        wt.rename_one('a/d/e', 'f/e')
        wt.rename_one('a/d', 'f/d')
        wt.rename_one('f/g', 'a/g')
        wt.rename_one('f/h', 'h')
        wt.rename_one('f', 'j')

    def helper(self, param=''):
        self._prepare_tree()
        # change dir
        # revert to default revision for file in subdir does work
        os.chdir('dir')
        mutter('cd dir\n')

        self.assertEquals('1\n', self.run_bzr('revno')[0])
        self.run_bzr('revert %s file' % param)
        self.assertEquals('spam', open('file', 'rb').read())

    def test_revert_in_subdir(self):
        self.helper()

    def test_revert_to_revision_in_subdir(self):
        # test case for bug #29424:
        # revert to specific revision for file in subdir does not work
        self.helper('-r 1')

    def test_revert_in_checkout(self):
        os.mkdir('brach')
        os.chdir('brach')
        self._prepare_tree()
        self.run_bzr('checkout --lightweight . ../sprach')
        self.run_bzr('commit -m more')
        os.chdir('../sprach')
        self.assertEqual('', self.run_bzr('status')[0])
        self.run_bzr('revert')
        self.assertEqual('', self.run_bzr('status')[0])

    def test_revert_dirname(self):
        """Test that revert DIRECTORY does what's expected"""
        self._prepare_rename_mod_tree()
        self.run_bzr('revert a')
        self.assertPathExists('a/b')
        self.assertPathExists('a/d')
        self.assertPathDoesNotExist('a/g')
        self.expectFailure(
            "j is in the delta revert applies because j was renamed too",
            self.assertPathExists, 'j')
        self.assertPathExists('h')
        self.run_bzr('revert f')
        self.assertPathDoesNotExist('j')
        self.assertPathDoesNotExist('h')
        self.assertPathExists('a/d/e')

    def test_revert_chatter(self):
        self._prepare_rename_mod_tree()
        chatter = self.run_bzr('revert')[1]
        self.assertEqualDiff(
            'R   a/g => f/g\n'
            'R   h => f/h\n'
            'R   j/ => f/\n'
            'R   j/b => a/b\n'
            'R   j/d/ => a/d/\n'
            'R   j/e => a/d/e\n',
            chatter)

    def test_revert(self):
        self.run_bzr('init')

        file('hello', 'wt').write('foo')
        self.run_bzr('add hello')
        self.run_bzr('commit -m setup hello')

        file('goodbye', 'wt').write('baz')
        self.run_bzr('add goodbye')
        self.run_bzr('commit -m setup goodbye')

        file('hello', 'wt').write('bar')
        file('goodbye', 'wt').write('qux')
        self.run_bzr('revert hello')
        self.check_file_contents('hello', 'foo')
        self.check_file_contents('goodbye', 'qux')
        self.run_bzr('revert')
        self.check_file_contents('goodbye', 'baz')

        os.mkdir('revertdir')
        self.run_bzr('add revertdir')
        self.run_bzr('commit -m f')
        os.rmdir('revertdir')
        self.run_bzr('revert')

        if bzrlib.osutils.has_symlinks():
            os.symlink('/unlikely/to/exist', 'symlink')
            self.run_bzr('add symlink')
            self.run_bzr('commit -m f')
            os.unlink('symlink')
            self.run_bzr('revert')
            self.assertPathExists('symlink')
            os.unlink('symlink')
            os.symlink('a-different-path', 'symlink')
            self.run_bzr('revert')
            self.assertEqual('/unlikely/to/exist',
                             os.readlink('symlink'))
        else:
            self.log("skipping revert symlink tests")

        file('hello', 'wt').write('xyz')
        self.run_bzr('commit -m xyz hello')
        self.run_bzr('revert -r 1 hello')
        self.check_file_contents('hello', 'foo')
        self.run_bzr('revert hello')
        self.check_file_contents('hello', 'xyz')
        os.chdir('revertdir')
        self.run_bzr('revert')
        os.chdir('..')

    def test_revert_newly_added(self):
        # this tests the UI reports reverting a newly added file
        # correct (such files are not deleted)
        tree = self.make_branch_and_tree('.')
        self.build_tree(['file'])
        tree.add(['file'])
        out, err = self.run_bzr('revert')
        self.assertEqual('', out)
        self.assertEqual('-   file\n', err)

    def test_revert_removing_file(self):
        # this tests the UI reports reverting a file which has been committed
        # to a revision that did not have it, reports it as being deleted.
        tree = self.make_branch_and_tree('.')
        tree.commit('empty commit')
        self.build_tree(['file'])
        tree.add(['file'])
        tree.commit('add file')
        out, err = self.run_bzr('revert -r -2')
        self.assertEqual('', out)
        self.assertEqual('-D  file\n', err)

    def test_revert_forget_merges(self):
        # revert --forget-merges removes any pending merges into the tree, but
        # leaves the files unchanged
        tree = self.make_branch_and_tree('.')
        # forget-merges before first commit, though pointless, does not fail
        self.run_bzr(['revert', '--forget-merges'])
        self.build_tree(['file'])
        first_rev_id = tree.commit('initial commit')
        self.build_tree_contents([('file', 'new content')])
        existing_parents = tree.get_parent_ids()
        self.assertEquals([first_rev_id], existing_parents)
        merged_parents = existing_parents + ['merged-in-rev']
        tree.set_parent_ids(merged_parents)
        self.assertEquals(merged_parents, tree.get_parent_ids())
        self.run_bzr(['revert', '--forget-merges'])
        self.assertEquals([first_rev_id], tree.get_parent_ids())
        # changed files are not reverted
        self.assertFileEqual('new content', 'file')
        # you can give it the path of a tree
        self.run_bzr(['revert', '--forget-merges', tree.abspath('.')])