/brz/remove-bazaar

To get this branch, use:
bzr branch http://gegoxaren.bato24.eu/bzr/brz/remove-bazaar
4634.122.4 by John Arbash Meinel
Add a test which exposes the bug in revert.
1
# Copyright (C) 2006, 2007, 2009, 2010 Canonical Ltd
1551.8.15 by Aaron Bentley
bug #54172: handle new directories properly in revert
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
4183.7.1 by Sabin Iacob
update FSF mailing address
15
# Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
1551.8.15 by Aaron Bentley
bug #54172: handle new directories properly in revert
16
2499.1.1 by Aaron Bentley
Revert does not try to preserve file contents produced by revert
17
import os
1551.8.15 by Aaron Bentley
bug #54172: handle new directories properly in revert
18
6622.1.34 by Jelmer Vernooij
Rename brzlib => breezy.
19
from breezy import merge, tests, transform, workingtree
1551.8.15 by Aaron Bentley
bug #54172: handle new directories properly in revert
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')
6653.6.1 by Jelmer Vernooij
Rename a number of attributes from bzrdir to controldir.
29
        target_tree = source_tree.controldir.sprout('target').open_workingtree()
1551.8.15 by Aaron Bentley
bug #54172: handle new directories properly in revert
30
        self.build_tree(['source/dir/', 'source/dir/contents'])
6855.4.1 by Jelmer Vernooij
Yet more bees.
31
        source_tree.add(['dir', 'dir/contents'], [b'dir-id', b'contents-id'])
1551.8.15 by Aaron Bentley
bug #54172: handle new directories properly in revert
32
        source_tree.commit('added dir')
3146.4.6 by Aaron Bentley
Fix locking issues in revert tests
33
        target_tree.lock_write()
34
        self.addCleanup(target_tree.unlock)
3943.8.1 by Marius Kruger
remove all trailing whitespace from bzr source
35
        merge.merge_inner(target_tree.branch, source_tree.basis_tree(),
1551.8.15 by Aaron Bentley
bug #54172: handle new directories properly in revert
36
                          target_tree.basis_tree(), this_tree=target_tree)
5784.1.3 by Martin Pool
Switch away from using failUnlessExists and failIfExists
37
        self.assertPathExists('target/dir')
38
        self.assertPathExists('target/dir/contents')
2748.3.2 by Aaron Bentley
Fix revert, remove-tree, and various tests to use None for 'no files specified'
39
        target_tree.revert()
5784.1.3 by Martin Pool
Switch away from using failUnlessExists and failIfExists
40
        self.assertPathDoesNotExist('target/dir/contents')
41
        self.assertPathDoesNotExist('target/dir')
1551.8.32 by Aaron Bentley
Fix iteraction of revert and files not present in target_tree
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')
6653.6.1 by Jelmer Vernooij
Rename a number of attributes from bzrdir to controldir.
56
        merge_target = tree.controldir.sprout('merge_target').open_workingtree()
1551.8.32 by Aaron Bentley
Fix iteraction of revert and files not present in target_tree
57
        self.build_tree(['tree/new_file'])
1551.8.33 by Aaron Bentley
Changes from review
58
59
        # newly-added files should not be deleted
1551.8.32 by Aaron Bentley
Fix iteraction of revert and files not present in target_tree
60
        tree.add('new_file')
2255.7.37 by Robert Collins
Dont use a basis tree that is not in the tree's parents for revert testing - its not guaranteed usable.
61
        basis_tree = tree.branch.repository.revision_tree(tree.last_revision())
2748.3.2 by Aaron Bentley
Fix revert, remove-tree, and various tests to use None for 'no files specified'
62
        tree.revert()
5784.1.3 by Martin Pool
Switch away from using failUnlessExists and failIfExists
63
        self.assertPathExists('tree/new_file')
1551.8.33 by Aaron Bentley
Changes from review
64
65
        # unchanged files should be deleted
1551.8.32 by Aaron Bentley
Fix iteraction of revert and files not present in target_tree
66
        tree.add('new_file')
67
        tree.commit('add new_file')
2748.3.2 by Aaron Bentley
Fix revert, remove-tree, and various tests to use None for 'no files specified'
68
        tree.revert(old_tree=basis_tree)
5784.1.3 by Martin Pool
Switch away from using failUnlessExists and failIfExists
69
        self.assertPathDoesNotExist('tree/new_file')
3943.8.1 by Marius Kruger
remove all trailing whitespace from bzr source
70
1551.8.33 by Aaron Bentley
Changes from review
71
        # files should be deleted if their changes came from merges
1551.8.32 by Aaron Bentley
Fix iteraction of revert and files not present in target_tree
72
        merge_target.merge_from_branch(tree.branch)
5784.1.3 by Martin Pool
Switch away from using failUnlessExists and failIfExists
73
        self.assertPathExists('merge_target/new_file')
2748.3.2 by Aaron Bentley
Fix revert, remove-tree, and various tests to use None for 'no files specified'
74
        merge_target.revert()
5784.1.3 by Martin Pool
Switch away from using failUnlessExists and failIfExists
75
        self.assertPathDoesNotExist('merge_target/new_file')
1551.8.33 by Aaron Bentley
Changes from review
76
77
        # files should not be deleted if changed after a merge
1551.8.32 by Aaron Bentley
Fix iteraction of revert and files not present in target_tree
78
        merge_target.merge_from_branch(tree.branch)
5784.1.3 by Martin Pool
Switch away from using failUnlessExists and failIfExists
79
        self.assertPathExists('merge_target/new_file')
6855.4.1 by Jelmer Vernooij
Yet more bees.
80
        self.build_tree_contents([('merge_target/new_file', b'new_contents')])
2748.3.2 by Aaron Bentley
Fix revert, remove-tree, and various tests to use None for 'no files specified'
81
        merge_target.revert()
5784.1.3 by Martin Pool
Switch away from using failUnlessExists and failIfExists
82
        self.assertPathExists('merge_target/new_file')
1551.9.10 by Aaron Bentley
Fix bugs in execute bit handling by revert
83
84
    def tree_with_executable(self):
85
        tree = self.make_branch_and_tree('tree')
86
        tt = transform.TreeTransform(tree)
6973.13.2 by Jelmer Vernooij
Fix some more tests.
87
        tt.new_file('newfile', tt.root, [b'helooo!'], b'newfile-id', True)
1551.9.10 by Aaron Bentley
Fix bugs in execute bit handling by revert
88
        tt.apply()
7027.3.3 by Jelmer Vernooij
Add some more bees; support writing both bytes and unicode strings in build_tree_contents.
89
        with tree.lock_write():
6809.4.4 by Jelmer Vernooij
Swap arguments for Tree.is_executable.
90
            self.assertTrue(tree.is_executable('newfile'))
3146.4.6 by Aaron Bentley
Fix locking issues in revert tests
91
            tree.commit('added newfile')
1551.9.10 by Aaron Bentley
Fix bugs in execute bit handling by revert
92
        return tree
93
94
    def test_preserve_execute(self):
95
        tree = self.tree_with_executable()
96
        tt = transform.TreeTransform(tree)
6885.1.1 by Jelmer Vernooij
Get rid of TreeTransform.trans_id_tree_file_id.
97
        newfile = tt.trans_id_tree_path('newfile')
1551.9.10 by Aaron Bentley
Fix bugs in execute bit handling by revert
98
        tt.delete_contents(newfile)
7027.3.3 by Jelmer Vernooij
Add some more bees; support writing both bytes and unicode strings in build_tree_contents.
99
        tt.create_file([b'Woooorld!'], newfile)
1551.9.10 by Aaron Bentley
Fix bugs in execute bit handling by revert
100
        tt.apply()
101
        tree = workingtree.WorkingTree.open('tree')
3146.4.6 by Aaron Bentley
Fix locking issues in revert tests
102
        tree.lock_write()
103
        self.addCleanup(tree.unlock)
6809.4.4 by Jelmer Vernooij
Swap arguments for Tree.is_executable.
104
        self.assertTrue(tree.is_executable('newfile'))
2748.3.2 by Aaron Bentley
Fix revert, remove-tree, and various tests to use None for 'no files specified'
105
        transform.revert(tree, tree.basis_tree(), None, backups=True)
6809.4.5 by Jelmer Vernooij
Swap arguments for get_file_*.
106
        self.assertEqual('helooo!', tree.get_file('newfile').read())
6809.4.4 by Jelmer Vernooij
Swap arguments for Tree.is_executable.
107
        self.assertTrue(tree.is_executable('newfile'))
1551.9.10 by Aaron Bentley
Fix bugs in execute bit handling by revert
108
109
    def test_revert_executable(self):
110
        tree = self.tree_with_executable()
111
        tt = transform.TreeTransform(tree)
6885.1.1 by Jelmer Vernooij
Get rid of TreeTransform.trans_id_tree_file_id.
112
        newfile = tt.trans_id_tree_path('newfile')
1551.9.10 by Aaron Bentley
Fix bugs in execute bit handling by revert
113
        tt.set_executability(False, newfile)
114
        tt.apply()
3146.4.6 by Aaron Bentley
Fix locking issues in revert tests
115
        tree.lock_write()
116
        self.addCleanup(tree.unlock)
2748.3.2 by Aaron Bentley
Fix revert, remove-tree, and various tests to use None for 'no files specified'
117
        transform.revert(tree, tree.basis_tree(), None)
6809.4.4 by Jelmer Vernooij
Swap arguments for Tree.is_executable.
118
        self.assertTrue(tree.is_executable('newfile'))
2499.1.1 by Aaron Bentley
Revert does not try to preserve file contents produced by revert
119
120
    def test_revert_deletes_files_from_revert(self):
121
        tree = self.make_branch_and_tree('.')
122
        self.build_tree(['file'])
123
        tree.add('file')
6855.4.1 by Jelmer Vernooij
Yet more bees.
124
        tree.commit('added file', rev_id=b'rev1')
2499.1.1 by Aaron Bentley
Revert does not try to preserve file contents produced by revert
125
        os.unlink('file')
126
        tree.commit('removed file')
5784.1.3 by Martin Pool
Switch away from using failUnlessExists and failIfExists
127
        self.assertPathDoesNotExist('file')
6973.5.2 by Jelmer Vernooij
Add more bees.
128
        tree.revert(old_tree=tree.branch.repository.revision_tree(b'rev1'))
5784.1.3 by Martin Pool
Switch away from using failUnlessExists and failIfExists
129
        self.assertPathExists('file')
2748.3.2 by Aaron Bentley
Fix revert, remove-tree, and various tests to use None for 'no files specified'
130
        tree.revert()
5784.1.3 by Martin Pool
Switch away from using failUnlessExists and failIfExists
131
        self.assertPathDoesNotExist('file')
2499.1.1 by Aaron Bentley
Revert does not try to preserve file contents produced by revert
132
        self.assertEqual({}, tree.merge_modified())
2748.3.3 by Aaron Bentley
Add a deprecation warning for revert([]), and handle as revert(None) for now
133
1551.19.4 by Aaron Bentley
Add failing test case
134
    def test_revert_file_in_deleted_dir(self):
135
        tree = self.make_branch_and_tree('.')
1551.19.9 by Aaron Bentley
Update from review
136
        self.build_tree(['dir/', 'dir/file1', 'dir/file2'])
137
        tree.add(['dir', 'dir/file1', 'dir/file2'],
7027.3.3 by Jelmer Vernooij
Add some more bees; support writing both bytes and unicode strings in build_tree_contents.
138
                 [b'dir-id', b'file1-id', b'file2-id'])
1551.19.9 by Aaron Bentley
Update from review
139
        tree.commit("Added files")
140
        os.unlink('dir/file1')
141
        os.unlink('dir/file2')
1551.19.4 by Aaron Bentley
Add failing test case
142
        os.rmdir('dir')
1551.19.9 by Aaron Bentley
Update from review
143
        tree.remove(['dir/', 'dir/file1', 'dir/file2'])
144
        tree.revert(['dir/file1'])
5784.1.3 by Martin Pool
Switch away from using failUnlessExists and failIfExists
145
        self.assertPathExists('dir/file1')
146
        self.assertPathDoesNotExist('dir/file2')
6973.13.2 by Jelmer Vernooij
Fix some more tests.
147
        self.assertEqual(b'dir-id', tree.path2id('dir'))
4634.122.4 by John Arbash Meinel
Add a test which exposes the bug in revert.
148
149
    def test_revert_root_id_change(self):
150
        tree = self.make_branch_and_tree('.')
6855.4.1 by Jelmer Vernooij
Yet more bees.
151
        tree.set_root_id(b'initial-root-id')
4634.122.4 by John Arbash Meinel
Add a test which exposes the bug in revert.
152
        self.build_tree(['file1'])
153
        tree.add(['file1'])
154
        tree.commit('first')
6855.4.1 by Jelmer Vernooij
Yet more bees.
155
        tree.set_root_id(b'temp-root-id')
7027.3.3 by Jelmer Vernooij
Add some more bees; support writing both bytes and unicode strings in build_tree_contents.
156
        self.assertEqual(b'temp-root-id', tree.get_root_id())
4634.122.4 by John Arbash Meinel
Add a test which exposes the bug in revert.
157
        tree.revert()
7027.3.3 by Jelmer Vernooij
Add some more bees; support writing both bytes and unicode strings in build_tree_contents.
158
        self.assertEqual(b'initial-root-id', tree.get_root_id())