/brz/remove-bazaar

To get this branch, use:
bzr branch http://gegoxaren.bato24.eu/bzr/brz/remove-bazaar
0.12.12 by Aaron Bentley
Implement shelf creator
1
# Copyright (C) 2008 Aaron Bentley <aaron@aaronbentley.com>
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
0.12.17 by Aaron Bentley
Handle creating symlinks
17
import os
18
0.12.19 by Aaron Bentley
Add support for writing shelves
19
from bzrlib import pack, tests, transform
0.14.7 by Aaron Bentley
Misc test cleanup
20
from bzrlib.plugins.shelf2 import shelf, serialize_transform
0.12.12 by Aaron Bentley
Implement shelf creator
21
22
23
class TestPrepareShelf(tests.TestCaseWithTransport):
24
25
    def test_shelve_rename(self):
26
        tree = self.make_branch_and_tree('.')
27
        self.build_tree(['foo'])
28
        tree.add(['foo'], ['foo-id'])
29
        tree.commit('foo')
30
        tree.rename_one('foo', 'bar')
0.14.7 by Aaron Bentley
Misc test cleanup
31
        creator = shelf.ShelfCreator(tree, tree.basis_tree())
0.12.12 by Aaron Bentley
Implement shelf creator
32
        self.addCleanup(creator.finalize)
33
        self.assertEqual([('rename', 'foo-id', 'foo', 'bar')], list(creator))
34
        creator.shelve_rename('foo-id')
35
        work_trans_id = creator.work_transform.trans_id_file_id('foo-id')
36
        self.assertEqual('foo', creator.work_transform.final_name(
37
                         work_trans_id))
38
        shelf_trans_id = creator.shelf_transform.trans_id_file_id('foo-id')
39
        self.assertEqual('bar', creator.shelf_transform.final_name(
40
                         shelf_trans_id))
41
42
    def test_shelve_move(self):
43
        tree = self.make_branch_and_tree('.')
44
        self.build_tree(['foo/', 'bar/', 'foo/baz'])
45
        tree.add(['foo', 'bar', 'foo/baz'], ['foo-id', 'bar-id', 'baz-id'])
46
        tree.commit('foo')
47
        tree.rename_one('foo/baz', 'bar/baz')
0.14.7 by Aaron Bentley
Misc test cleanup
48
        creator = shelf.ShelfCreator(tree, tree.basis_tree())
0.12.12 by Aaron Bentley
Implement shelf creator
49
        self.addCleanup(creator.finalize)
50
        self.assertEqual([('rename', 'baz-id', 'foo/baz', 'bar/baz')],
51
                         list(creator))
52
        creator.shelve_rename('baz-id')
53
        work_trans_id = creator.work_transform.trans_id_file_id('baz-id')
54
        work_foo = creator.work_transform.trans_id_file_id('foo-id')
55
        self.assertEqual(work_foo, creator.work_transform.final_parent(
56
                         work_trans_id))
57
        shelf_trans_id = creator.shelf_transform.trans_id_file_id('baz-id')
58
        shelf_bar = creator.shelf_transform.trans_id_file_id('bar-id')
59
        self.assertEqual(shelf_bar, creator.shelf_transform.final_parent(
60
                         shelf_trans_id))
0.12.13 by Aaron Bentley
Implement shelving content
61
        creator.transform()
62
        self.assertEqual('foo/baz', tree.id2path('baz-id'))
63
0.12.14 by Aaron Bentley
Add shelving of created files
64
    def assertShelvedFileEqual(self, expected_content, creator, file_id):
65
        s_trans_id = creator.shelf_transform.trans_id_file_id(file_id)
66
        shelf_file = creator.shelf_transform._limbo_name(s_trans_id)
67
        self.assertFileEqual(expected_content, shelf_file)
68
0.12.13 by Aaron Bentley
Implement shelving content
69
    def test_shelve_content_change(self):
70
        tree = self.make_branch_and_tree('.')
71
        tree.lock_write()
72
        self.addCleanup(tree.unlock)
73
        self.build_tree_contents([('foo', 'a\n')])
74
        tree.add('foo', 'foo-id')
75
        tree.commit('Committed foo')
76
        self.build_tree_contents([('foo', 'b\na\nc\n')])
0.14.7 by Aaron Bentley
Misc test cleanup
77
        creator = shelf.ShelfCreator(tree, tree.basis_tree())
0.12.13 by Aaron Bentley
Implement shelving content
78
        self.addCleanup(creator.finalize)
79
        self.assertEqual([('modify text', 'foo-id')], list(creator))
80
        creator.shelve_text('foo-id', 'a\nc\n')
81
        creator.transform()
82
        self.assertFileEqual('a\nc\n', 'foo')
0.12.14 by Aaron Bentley
Add shelving of created files
83
        self.assertShelvedFileEqual('b\na\n', creator, 'foo-id')
84
85
    def test_shelve_creation(self):
86
        tree = self.make_branch_and_tree('.')
87
        tree.lock_write()
88
        self.addCleanup(tree.unlock)
89
        tree.commit('Empty tree')
0.12.16 by Aaron Bentley
Handle shelving directory creation
90
        self.build_tree_contents([('foo', 'a\n'), ('bar/',)])
91
        tree.add(['foo', 'bar'], ['foo-id', 'bar-id'])
0.14.7 by Aaron Bentley
Misc test cleanup
92
        creator = shelf.ShelfCreator(tree, tree.basis_tree())
0.12.14 by Aaron Bentley
Add shelving of created files
93
        self.addCleanup(creator.finalize)
0.12.16 by Aaron Bentley
Handle shelving directory creation
94
        self.assertEqual([('add file', 'bar-id', 'directory'),
95
                          ('add file', 'foo-id', 'file')],
96
                          sorted(list(creator)))
0.14.2 by Aaron Bentley
Somewhat clean up shelving
97
        creator.shelve_creation('foo-id')
98
        creator.shelve_creation('bar-id')
0.12.14 by Aaron Bentley
Add shelving of created files
99
        creator.transform()
0.12.15 by Aaron Bentley
Handle file-id when shelving creation
100
        self.assertRaises(StopIteration,
101
                          tree.iter_entries_by_dir(['foo-id']).next)
102
        s_trans_id = creator.shelf_transform.trans_id_file_id('foo-id')
103
        self.assertEqual('foo-id',
104
                         creator.shelf_transform.final_file_id(s_trans_id))
0.12.14 by Aaron Bentley
Add shelving of created files
105
        self.failIfExists('foo')
0.12.16 by Aaron Bentley
Handle shelving directory creation
106
        self.failIfExists('bar')
0.12.14 by Aaron Bentley
Add shelving of created files
107
        self.assertShelvedFileEqual('a\n', creator, 'foo-id')
0.12.16 by Aaron Bentley
Handle shelving directory creation
108
        s_bar_trans_id = creator.shelf_transform.trans_id_file_id('bar-id')
109
        self.assertEqual('directory',
110
            creator.shelf_transform.final_kind(s_bar_trans_id))
0.12.17 by Aaron Bentley
Handle creating symlinks
111
112
    def test_shelve_symlink_creation(self):
113
        self.requireFeature(tests.SymlinkFeature)
114
        tree = self.make_branch_and_tree('.')
115
        tree.lock_write()
116
        self.addCleanup(tree.unlock)
117
        tree.commit('Empty tree')
118
        os.symlink('bar', 'foo')
119
        tree.add('foo', 'foo-id')
0.14.7 by Aaron Bentley
Misc test cleanup
120
        creator = shelf.ShelfCreator(tree, tree.basis_tree())
0.12.17 by Aaron Bentley
Handle creating symlinks
121
        self.addCleanup(creator.finalize)
122
        self.assertEqual([('add file', 'foo-id', 'symlink')], list(creator))
0.14.2 by Aaron Bentley
Somewhat clean up shelving
123
        creator.shelve_creation('foo-id')
0.12.17 by Aaron Bentley
Handle creating symlinks
124
        creator.transform()
125
        s_trans_id = creator.shelf_transform.trans_id_file_id('foo-id')
126
        self.failIfExists('foo')
127
        limbo_name = creator.shelf_transform._limbo_name(s_trans_id)
128
        self.assertEqual('bar', os.readlink(limbo_name))
0.12.19 by Aaron Bentley
Add support for writing shelves
129
0.14.4 by Aaron Bentley
Implement shelving deletion
130
    def test_shelve_deletion(self):
131
        tree = self.make_branch_and_tree('tree')
132
        self.build_tree_contents([('tree/foo/',), ('tree/foo/bar', 'baz')])
133
        tree.add(['foo', 'foo/bar'], ['foo-id', 'bar-id'])
134
        tree.commit('Added file and directory')
135
        os.unlink('tree/foo/bar')
136
        os.rmdir('tree/foo')
0.14.7 by Aaron Bentley
Misc test cleanup
137
        creator = shelf.ShelfCreator(tree, tree.basis_tree())
0.14.4 by Aaron Bentley
Implement shelving deletion
138
        self.addCleanup(creator.finalize)
139
        self.assertEqual([('delete file', 'bar-id'),
140
                          ('delete file', 'foo-id')],
141
                          sorted(list(creator)))
142
        creator.shelve_deletion('foo-id')
143
        creator.shelve_deletion('bar-id')
0.14.6 by Aaron Bentley
Fix deletion test
144
        creator.transform()
0.14.4 by Aaron Bentley
Implement shelving deletion
145
        self.failUnlessExists('tree/foo')
146
        self.assertFileEqual('baz', 'tree/foo/bar')
147
0.12.19 by Aaron Bentley
Add support for writing shelves
148
    def test_write_shelf(self):
149
        tree = self.make_branch_and_tree('tree')
150
        self.build_tree(['tree/foo'])
151
        tree.add('foo', 'foo-id')
0.14.7 by Aaron Bentley
Misc test cleanup
152
        creator = shelf.ShelfCreator(tree, tree.basis_tree())
153
        self.addCleanup(creator.finalize)
0.12.19 by Aaron Bentley
Add support for writing shelves
154
        list(creator)
0.14.2 by Aaron Bentley
Somewhat clean up shelving
155
        creator.shelve_creation('foo-id')
0.12.19 by Aaron Bentley
Add support for writing shelves
156
        filename = creator.write_shelf()
157
        self.assertContainsRe(filename, 'tree/.shelf2/01$')
158
        self.failUnlessExists(filename)
159
        parser = pack.ContainerPushParser()
160
        shelf_file = open(filename, 'rb')
161
        try:
162
            parser.accept_bytes(shelf_file.read())
163
        finally:
164
            shelf_file.close()
165
        tt = transform.TransformPreview(tree)
0.14.7 by Aaron Bentley
Misc test cleanup
166
        self.addCleanup(tt.finalize)
0.15.7 by Aaron Bentley
Backport test fixes
167
        records = iter(parser.read_pending_records())
168
        #skip revision-id
169
        records.next()
170
        serialize_transform.deserialize(tt, records)
0.12.21 by Aaron Bentley
Add failing test of unshelver
171
172
173
class TestUnshelver(tests.TestCaseWithTransport):
174
175
    def test_unshelve(self):
176
        tree = self.make_branch_and_tree('tree')
0.15.7 by Aaron Bentley
Backport test fixes
177
        tree.commit('first commit')
0.12.21 by Aaron Bentley
Add failing test of unshelver
178
        self.build_tree_contents([('tree/foo', 'bar')])
0.12.24 by Aaron Bentley
Get unshelve using merge codepath, not applying transform directly
179
        tree.lock_write()
180
        self.addCleanup(tree.unlock)
0.12.21 by Aaron Bentley
Add failing test of unshelver
181
        tree.add('foo', 'foo-id')
0.15.5 by Aaron Bentley
Rename to shelf
182
        creator = shelf.ShelfCreator(tree, tree.basis_tree())
183
        self.addCleanup(creator.finalize)
0.12.21 by Aaron Bentley
Add failing test of unshelver
184
        list(creator)
0.12.23 by Aaron Bentley
Fix up unshelve some more
185
        creator.shelve_creation('foo-id')
0.12.21 by Aaron Bentley
Add failing test of unshelver
186
        creator.transform()
187
        filename = creator.write_shelf()
0.15.5 by Aaron Bentley
Rename to shelf
188
        unshelver = shelf.Unshelver.from_tree_and_shelf(tree, filename)
0.12.21 by Aaron Bentley
Add failing test of unshelver
189
        unshelver.unshelve()
190
        self.assertFileEqual('bar', 'tree/foo')
0.12.26 by Aaron Bentley
Use correct base for shelving
191
192
    def test_unshelve_base(self):
193
        tree = self.make_branch_and_tree('tree')
194
        tree.lock_write()
195
        self.addCleanup(tree.unlock)
196
        tree.commit('rev1', rev_id='rev1')
0.15.5 by Aaron Bentley
Rename to shelf
197
        creator = shelf.ShelfCreator(tree, tree.basis_tree())
198
        self.addCleanup(creator.finalize)
0.12.26 by Aaron Bentley
Use correct base for shelving
199
        filename = creator.write_shelf()
200
        tree.commit('rev2', rev_id='rev2')
0.15.5 by Aaron Bentley
Rename to shelf
201
        unshelver = shelf.Unshelver.from_tree_and_shelf(tree, filename)
0.12.26 by Aaron Bentley
Use correct base for shelving
202
        self.assertEqual('rev1', unshelver.base_tree.get_revision_id())