~bzr-pqm/bzr/bzr.dev

« back to all changes in this revision

Viewing changes to bzrlib/tests/test_revert.py

merge merge tweaks from aaron, which includes latest .dev

Show diffs side-by-side

added added

removed removed

Lines of Context:
1
 
# Copyright (C) 2006 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
 
import os
18
 
 
19
 
from bzrlib import merge, tests, transform, workingtree
20
 
 
21
 
 
22
 
class TestRevert(tests.TestCaseWithTransport):
23
 
    """Ensure that revert behaves as expected"""
24
 
 
25
 
    def test_revert_merged_dir(self):
26
 
        """Reverting a merge that adds a directory deletes the directory"""
27
 
        source_tree = self.make_branch_and_tree('source')
28
 
        source_tree.commit('empty tree')
29
 
        target_tree = source_tree.bzrdir.sprout('target').open_workingtree()
30
 
        self.build_tree(['source/dir/', 'source/dir/contents'])
31
 
        source_tree.add(['dir', 'dir/contents'], ['dir-id', 'contents-id'])
32
 
        source_tree.commit('added dir')
33
 
        target_tree.lock_write()
34
 
        self.addCleanup(target_tree.unlock)
35
 
        merge.merge_inner(target_tree.branch, source_tree.basis_tree(), 
36
 
                          target_tree.basis_tree(), this_tree=target_tree)
37
 
        self.failUnlessExists('target/dir')
38
 
        self.failUnlessExists('target/dir/contents')
39
 
        target_tree.revert()
40
 
        self.failIfExists('target/dir/contents')
41
 
        self.failIfExists('target/dir')
42
 
 
43
 
    def test_revert_new(self):
44
 
        """Only locally-changed new files should be preserved when reverting
45
 
 
46
 
        When a file isn't present in revert's target tree:
47
 
        If a file hasn't been committed, revert should unversion it, but not
48
 
        delete it.
49
 
        If a file has local changes, revert should unversion it, but not
50
 
        delete it.
51
 
        If a file has no changes from the last commit, revert should delete it.
52
 
        If a file has changes due to a merge, revert should delete it.
53
 
        """
54
 
        tree = self.make_branch_and_tree('tree')
55
 
        tree.commit('empty tree')
56
 
        merge_target = tree.bzrdir.sprout('merge_target').open_workingtree()
57
 
        self.build_tree(['tree/new_file'])
58
 
 
59
 
        # newly-added files should not be deleted
60
 
        tree.add('new_file')
61
 
        basis_tree = tree.branch.repository.revision_tree(tree.last_revision())
62
 
        tree.revert()
63
 
        self.failUnlessExists('tree/new_file')
64
 
 
65
 
        # unchanged files should be deleted
66
 
        tree.add('new_file')
67
 
        tree.commit('add new_file')
68
 
        tree.revert(old_tree=basis_tree)
69
 
        self.failIfExists('tree/new_file')
70
 
        
71
 
        # files should be deleted if their changes came from merges
72
 
        merge_target.merge_from_branch(tree.branch)
73
 
        self.failUnlessExists('merge_target/new_file')
74
 
        merge_target.revert()
75
 
        self.failIfExists('merge_target/new_file')
76
 
 
77
 
        # files should not be deleted if changed after a merge
78
 
        merge_target.merge_from_branch(tree.branch)
79
 
        self.failUnlessExists('merge_target/new_file')
80
 
        self.build_tree_contents([('merge_target/new_file', 'new_contents')])
81
 
        merge_target.revert()
82
 
        self.failUnlessExists('merge_target/new_file')
83
 
 
84
 
    def tree_with_executable(self):
85
 
        tree = self.make_branch_and_tree('tree')
86
 
        tt = transform.TreeTransform(tree)
87
 
        tt.new_file('newfile', tt.root, 'helooo!', 'newfile-id', True)
88
 
        tt.apply()
89
 
        tree.lock_write()
90
 
        try:
91
 
            self.assertTrue(tree.is_executable('newfile-id'))
92
 
            tree.commit('added newfile')
93
 
        finally:
94
 
            tree.unlock()
95
 
        return tree
96
 
 
97
 
    def test_preserve_execute(self):
98
 
        tree = self.tree_with_executable()
99
 
        tt = transform.TreeTransform(tree)
100
 
        newfile = tt.trans_id_tree_file_id('newfile-id')
101
 
        tt.delete_contents(newfile)
102
 
        tt.create_file('Woooorld!', newfile)
103
 
        tt.apply()
104
 
        tree = workingtree.WorkingTree.open('tree')
105
 
        tree.lock_write()
106
 
        self.addCleanup(tree.unlock)
107
 
        self.assertTrue(tree.is_executable('newfile-id'))
108
 
        transform.revert(tree, tree.basis_tree(), None, backups=True)
109
 
        self.assertEqual('helooo!', tree.get_file('newfile-id').read())
110
 
        self.assertTrue(tree.is_executable('newfile-id'))
111
 
 
112
 
    def test_revert_executable(self):
113
 
        tree = self.tree_with_executable()
114
 
        tt = transform.TreeTransform(tree)
115
 
        newfile = tt.trans_id_tree_file_id('newfile-id')
116
 
        tt.set_executability(False, newfile)
117
 
        tt.apply()
118
 
        tree.lock_write()
119
 
        self.addCleanup(tree.unlock)
120
 
        transform.revert(tree, tree.basis_tree(), None)
121
 
        self.assertTrue(tree.is_executable('newfile-id'))
122
 
 
123
 
    def test_revert_deletes_files_from_revert(self):
124
 
        tree = self.make_branch_and_tree('.')
125
 
        self.build_tree(['file'])
126
 
        tree.add('file')
127
 
        tree.commit('added file', rev_id='rev1')
128
 
        os.unlink('file')
129
 
        tree.commit('removed file')
130
 
        self.failIfExists('file')
131
 
        tree.revert(old_tree=tree.branch.repository.revision_tree('rev1'))
132
 
        self.failUnlessExists('file')
133
 
        tree.revert()
134
 
        self.failIfExists('file')
135
 
        self.assertEqual({}, tree.merge_modified())
136
 
 
137
 
    def test_empty_deprecated(self):
138
 
        tree = self.make_branch_and_tree('.')
139
 
        self.build_tree(['file'])
140
 
        tree.add('file')
141
 
        self.callDeprecated(['Using [] to revert all files is deprecated'
142
 
            ' as of bzr 0.91.  Please use None (the default) instead.'],
143
 
            tree.revert, [])
144
 
        self.assertIs(None, tree.path2id('file'))
145
 
 
146
 
    def test_revert_file_in_deleted_dir(self):
147
 
        tree = self.make_branch_and_tree('.')
148
 
        self.build_tree(['dir/', 'dir/file1', 'dir/file2'])
149
 
        tree.add(['dir', 'dir/file1', 'dir/file2'],
150
 
                 ['dir-id', 'file1-id', 'file2-id'])
151
 
        tree.commit("Added files")
152
 
        os.unlink('dir/file1')
153
 
        os.unlink('dir/file2')
154
 
        os.rmdir('dir')
155
 
        tree.remove(['dir/', 'dir/file1', 'dir/file2'])
156
 
        tree.revert(['dir/file1'])
157
 
        self.failUnlessExists('dir/file1')
158
 
        self.failIfExists('dir/file2')
159
 
        self.assertEqual('dir-id', tree.path2id('dir'))