/brz/remove-bazaar

To get this branch, use:
bzr branch http://gegoxaren.bato24.eu/bzr/brz/remove-bazaar
0.16.89 by Aaron Bentley
Add tests for Shelver
1
# Copyright (C) 2008 Canonical Ltd
2
#
0.16.101 by Aaron Bentley
Update GPL formatting and copyright
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
0.16.89 by Aaron Bentley
Add tests for Shelver
16
17
18
from cStringIO import StringIO
0.16.91 by Aaron Bentley
Test finish and quit
19
import os
0.16.108 by Aaron Bentley
Shelf supports multiple diff writers.
20
import sys
0.16.89 by Aaron Bentley
Add tests for Shelver
21
4595.9.5 by Aaron Bentley
Add expected failures for shelving root changes.
22
from bzrlib import (
23
    errors,
24
    shelf_ui,
25
    revision,
26
    tests,
27
)
4899.2.1 by Neil Martinsen-Burrell
add beter feedback from the unshelve command
28
from bzrlib.tests import script
0.16.89 by Aaron Bentley
Add tests for Shelver
29
30
31
class ExpectShelver(shelf_ui.Shelver):
32
    """A variant of Shelver that intercepts console activity, for testing."""
33
4100.3.4 by Aaron Bentley
Clean up signatures
34
    def __init__(self, work_tree, target_tree, diff_writer=None,
35
                 auto=False, auto_apply=False, file_list=None, message=None,
4526.7.1 by Aaron Bentley
Make vocabulary part of reporter.
36
                 destroy=False, reporter=None):
0.16.108 by Aaron Bentley
Shelf supports multiple diff writers.
37
        shelf_ui.Shelver.__init__(self, work_tree, target_tree, diff_writer,
4100.3.4 by Aaron Bentley
Clean up signatures
38
                                  auto, auto_apply, file_list, message,
4526.7.1 by Aaron Bentley
Make vocabulary part of reporter.
39
                                  destroy, reporter=reporter)
0.16.89 by Aaron Bentley
Add tests for Shelver
40
        self.expected = []
41
        self.diff_writer = StringIO()
42
43
    def expect(self, prompt, response):
44
        self.expected.append((prompt, response))
45
46
    def prompt(self, message):
47
        try:
48
            prompt, response = self.expected.pop(0)
49
        except IndexError:
50
            raise AssertionError('Unexpected prompt: %s' % message)
51
        if prompt != message:
52
            raise AssertionError('Wrong prompt: %s' % message)
53
        return response
54
55
0.16.90 by Aaron Bentley
Handle shelving multiple diff hunks
56
LINES_AJ = 'a\nb\nc\nd\ne\nf\ng\nh\ni\nj\n'
57
58
59
LINES_ZY = 'z\nb\nc\nd\ne\nf\ng\nh\ni\ny\n'
60
61
62
LINES_AY = 'a\nb\nc\nd\ne\nf\ng\nh\ni\ny\n'
63
64
0.16.89 by Aaron Bentley
Add tests for Shelver
65
class TestShelver(tests.TestCaseWithTransport):
66
67
    def create_shelvable_tree(self):
68
        tree = self.make_branch_and_tree('tree')
0.16.90 by Aaron Bentley
Handle shelving multiple diff hunks
69
        self.build_tree_contents([('tree/foo', LINES_AJ)])
0.16.89 by Aaron Bentley
Add tests for Shelver
70
        tree.add('foo', 'foo-id')
71
        tree.commit('added foo')
0.16.90 by Aaron Bentley
Handle shelving multiple diff hunks
72
        self.build_tree_contents([('tree/foo', LINES_ZY)])
0.16.89 by Aaron Bentley
Add tests for Shelver
73
        return tree
74
75
    def test_unexpected_prompt_failure(self):
76
        tree = self.create_shelvable_tree()
4595.13.2 by Alexander Belchenko
[cherrypick revno 4650 from bzr.dev] Fix shelve on windows. (Robert Collins, #305006)
77
        tree.lock_tree_write()
78
        self.addCleanup(tree.unlock)
0.16.89 by Aaron Bentley
Add tests for Shelver
79
        shelver = ExpectShelver(tree, tree.basis_tree())
4603.1.17 by Aaron Bentley
Fix shelf_ui tests to finalize.
80
        self.addCleanup(shelver.finalize)
0.16.89 by Aaron Bentley
Add tests for Shelver
81
        e = self.assertRaises(AssertionError, shelver.run)
3990.4.1 by Daniel Watkins
Changed all shelve tests to expect a '?'.
82
        self.assertEqual('Unexpected prompt: Shelve? [yNfq?]', str(e))
0.16.89 by Aaron Bentley
Add tests for Shelver
83
84
    def test_wrong_prompt_failure(self):
85
        tree = self.create_shelvable_tree()
4595.13.2 by Alexander Belchenko
[cherrypick revno 4650 from bzr.dev] Fix shelve on windows. (Robert Collins, #305006)
86
        tree.lock_tree_write()
87
        self.addCleanup(tree.unlock)
0.16.89 by Aaron Bentley
Add tests for Shelver
88
        shelver = ExpectShelver(tree, tree.basis_tree())
4603.1.17 by Aaron Bentley
Fix shelf_ui tests to finalize.
89
        self.addCleanup(shelver.finalize)
0.16.89 by Aaron Bentley
Add tests for Shelver
90
        shelver.expect('foo', 'y')
91
        e = self.assertRaises(AssertionError, shelver.run)
3990.4.1 by Daniel Watkins
Changed all shelve tests to expect a '?'.
92
        self.assertEqual('Wrong prompt: Shelve? [yNfq?]', str(e))
0.16.89 by Aaron Bentley
Add tests for Shelver
93
94
    def test_shelve_not_diff(self):
95
        tree = self.create_shelvable_tree()
4595.13.2 by Alexander Belchenko
[cherrypick revno 4650 from bzr.dev] Fix shelve on windows. (Robert Collins, #305006)
96
        tree.lock_tree_write()
97
        self.addCleanup(tree.unlock)
0.16.89 by Aaron Bentley
Add tests for Shelver
98
        shelver = ExpectShelver(tree, tree.basis_tree())
4603.1.17 by Aaron Bentley
Fix shelf_ui tests to finalize.
99
        self.addCleanup(shelver.finalize)
3990.4.1 by Daniel Watkins
Changed all shelve tests to expect a '?'.
100
        shelver.expect('Shelve? [yNfq?]', 'n')
101
        shelver.expect('Shelve? [yNfq?]', 'n')
0.16.89 by Aaron Bentley
Add tests for Shelver
102
        # No final shelving prompt because no changes were selected
103
        shelver.run()
0.16.90 by Aaron Bentley
Handle shelving multiple diff hunks
104
        self.assertFileEqual(LINES_ZY, 'tree/foo')
0.16.89 by Aaron Bentley
Add tests for Shelver
105
106
    def test_shelve_diff_no(self):
107
        tree = self.create_shelvable_tree()
4595.13.2 by Alexander Belchenko
[cherrypick revno 4650 from bzr.dev] Fix shelve on windows. (Robert Collins, #305006)
108
        tree.lock_tree_write()
109
        self.addCleanup(tree.unlock)
0.16.89 by Aaron Bentley
Add tests for Shelver
110
        shelver = ExpectShelver(tree, tree.basis_tree())
4603.1.17 by Aaron Bentley
Fix shelf_ui tests to finalize.
111
        self.addCleanup(shelver.finalize)
3990.4.1 by Daniel Watkins
Changed all shelve tests to expect a '?'.
112
        shelver.expect('Shelve? [yNfq?]', 'y')
113
        shelver.expect('Shelve? [yNfq?]', 'y')
114
        shelver.expect('Shelve 2 change(s)? [yNfq?]', 'n')
0.16.89 by Aaron Bentley
Add tests for Shelver
115
        shelver.run()
0.16.90 by Aaron Bentley
Handle shelving multiple diff hunks
116
        self.assertFileEqual(LINES_ZY, 'tree/foo')
0.16.89 by Aaron Bentley
Add tests for Shelver
117
118
    def test_shelve_diff(self):
119
        tree = self.create_shelvable_tree()
4595.13.2 by Alexander Belchenko
[cherrypick revno 4650 from bzr.dev] Fix shelve on windows. (Robert Collins, #305006)
120
        tree.lock_tree_write()
121
        self.addCleanup(tree.unlock)
0.16.89 by Aaron Bentley
Add tests for Shelver
122
        shelver = ExpectShelver(tree, tree.basis_tree())
4603.1.17 by Aaron Bentley
Fix shelf_ui tests to finalize.
123
        self.addCleanup(shelver.finalize)
3990.4.1 by Daniel Watkins
Changed all shelve tests to expect a '?'.
124
        shelver.expect('Shelve? [yNfq?]', 'y')
125
        shelver.expect('Shelve? [yNfq?]', 'y')
126
        shelver.expect('Shelve 2 change(s)? [yNfq?]', 'y')
0.16.90 by Aaron Bentley
Handle shelving multiple diff hunks
127
        shelver.run()
128
        self.assertFileEqual(LINES_AJ, 'tree/foo')
129
130
    def test_shelve_one_diff(self):
131
        tree = self.create_shelvable_tree()
4595.13.2 by Alexander Belchenko
[cherrypick revno 4650 from bzr.dev] Fix shelve on windows. (Robert Collins, #305006)
132
        tree.lock_tree_write()
133
        self.addCleanup(tree.unlock)
0.16.90 by Aaron Bentley
Handle shelving multiple diff hunks
134
        shelver = ExpectShelver(tree, tree.basis_tree())
4603.1.17 by Aaron Bentley
Fix shelf_ui tests to finalize.
135
        self.addCleanup(shelver.finalize)
3990.4.1 by Daniel Watkins
Changed all shelve tests to expect a '?'.
136
        shelver.expect('Shelve? [yNfq?]', 'y')
137
        shelver.expect('Shelve? [yNfq?]', 'n')
138
        shelver.expect('Shelve 1 change(s)? [yNfq?]', 'y')
0.16.89 by Aaron Bentley
Add tests for Shelver
139
        shelver.run()
0.16.90 by Aaron Bentley
Handle shelving multiple diff hunks
140
        self.assertFileEqual(LINES_AY, 'tree/foo')
0.16.89 by Aaron Bentley
Add tests for Shelver
141
0.16.92 by Aaron Bentley
Test kind, add, binary, --all
142
    def test_shelve_binary_change(self):
143
        tree = self.create_shelvable_tree()
144
        self.build_tree_contents([('tree/foo', '\x00')])
4595.13.2 by Alexander Belchenko
[cherrypick revno 4650 from bzr.dev] Fix shelve on windows. (Robert Collins, #305006)
145
        tree.lock_tree_write()
146
        self.addCleanup(tree.unlock)
0.16.92 by Aaron Bentley
Test kind, add, binary, --all
147
        shelver = ExpectShelver(tree, tree.basis_tree())
4603.1.17 by Aaron Bentley
Fix shelf_ui tests to finalize.
148
        self.addCleanup(shelver.finalize)
3990.4.1 by Daniel Watkins
Changed all shelve tests to expect a '?'.
149
        shelver.expect('Shelve binary changes? [yNfq?]', 'y')
150
        shelver.expect('Shelve 1 change(s)? [yNfq?]', 'y')
0.16.92 by Aaron Bentley
Test kind, add, binary, --all
151
        shelver.run()
152
        self.assertFileEqual(LINES_AJ, 'tree/foo')
153
0.16.89 by Aaron Bentley
Add tests for Shelver
154
    def test_shelve_rename(self):
155
        tree = self.create_shelvable_tree()
156
        tree.rename_one('foo', 'bar')
4595.13.2 by Alexander Belchenko
[cherrypick revno 4650 from bzr.dev] Fix shelve on windows. (Robert Collins, #305006)
157
        tree.lock_tree_write()
158
        self.addCleanup(tree.unlock)
0.16.89 by Aaron Bentley
Add tests for Shelver
159
        shelver = ExpectShelver(tree, tree.basis_tree())
4603.1.17 by Aaron Bentley
Fix shelf_ui tests to finalize.
160
        self.addCleanup(shelver.finalize)
3990.4.1 by Daniel Watkins
Changed all shelve tests to expect a '?'.
161
        shelver.expect('Shelve renaming "foo" => "bar"? [yNfq?]', 'y')
162
        shelver.expect('Shelve? [yNfq?]', 'y')
163
        shelver.expect('Shelve? [yNfq?]', 'y')
164
        shelver.expect('Shelve 3 change(s)? [yNfq?]', 'y')
0.16.89 by Aaron Bentley
Add tests for Shelver
165
        shelver.run()
0.16.90 by Aaron Bentley
Handle shelving multiple diff hunks
166
        self.assertFileEqual(LINES_AJ, 'tree/foo')
0.16.91 by Aaron Bentley
Test finish and quit
167
168
    def test_shelve_deletion(self):
169
        tree = self.create_shelvable_tree()
170
        os.unlink('tree/foo')
4595.13.2 by Alexander Belchenko
[cherrypick revno 4650 from bzr.dev] Fix shelve on windows. (Robert Collins, #305006)
171
        tree.lock_tree_write()
172
        self.addCleanup(tree.unlock)
0.16.91 by Aaron Bentley
Test finish and quit
173
        shelver = ExpectShelver(tree, tree.basis_tree())
4603.1.17 by Aaron Bentley
Fix shelf_ui tests to finalize.
174
        self.addCleanup(shelver.finalize)
3990.4.1 by Daniel Watkins
Changed all shelve tests to expect a '?'.
175
        shelver.expect('Shelve removing file "foo"? [yNfq?]', 'y')
176
        shelver.expect('Shelve 1 change(s)? [yNfq?]', 'y')
0.16.91 by Aaron Bentley
Test finish and quit
177
        shelver.run()
178
        self.assertFileEqual(LINES_AJ, 'tree/foo')
179
0.16.92 by Aaron Bentley
Test kind, add, binary, --all
180
    def test_shelve_creation(self):
181
        tree = self.make_branch_and_tree('tree')
182
        tree.commit('add tree root')
183
        self.build_tree(['tree/foo'])
184
        tree.add('foo')
4595.13.2 by Alexander Belchenko
[cherrypick revno 4650 from bzr.dev] Fix shelve on windows. (Robert Collins, #305006)
185
        tree.lock_tree_write()
186
        self.addCleanup(tree.unlock)
0.16.92 by Aaron Bentley
Test kind, add, binary, --all
187
        shelver = ExpectShelver(tree, tree.basis_tree())
4603.1.17 by Aaron Bentley
Fix shelf_ui tests to finalize.
188
        self.addCleanup(shelver.finalize)
3990.4.1 by Daniel Watkins
Changed all shelve tests to expect a '?'.
189
        shelver.expect('Shelve adding file "foo"? [yNfq?]', 'y')
190
        shelver.expect('Shelve 1 change(s)? [yNfq?]', 'y')
0.16.92 by Aaron Bentley
Test kind, add, binary, --all
191
        shelver.run()
192
        self.failIfExists('tree/foo')
193
194
    def test_shelve_kind_change(self):
195
        tree = self.create_shelvable_tree()
196
        os.unlink('tree/foo')
197
        os.mkdir('tree/foo')
4595.13.2 by Alexander Belchenko
[cherrypick revno 4650 from bzr.dev] Fix shelve on windows. (Robert Collins, #305006)
198
        tree.lock_tree_write()
199
        self.addCleanup(tree.unlock)
0.16.92 by Aaron Bentley
Test kind, add, binary, --all
200
        shelver = ExpectShelver(tree, tree.basis_tree())
4603.1.17 by Aaron Bentley
Fix shelf_ui tests to finalize.
201
        self.addCleanup(shelver.finalize)
3990.4.1 by Daniel Watkins
Changed all shelve tests to expect a '?'.
202
        shelver.expect('Shelve changing "foo" from file to directory? [yNfq?]',
0.16.92 by Aaron Bentley
Test kind, add, binary, --all
203
                       'y')
3990.4.1 by Daniel Watkins
Changed all shelve tests to expect a '?'.
204
        shelver.expect('Shelve 1 change(s)? [yNfq?]', 'y')
0.16.92 by Aaron Bentley
Test kind, add, binary, --all
205
4119.5.1 by James Westby
Shelve can now shelve changes to a symlink's target.
206
    def test_shelve_modify_target(self):
4526.6.12 by Aaron Bentley
Updates from review.
207
        self.requireFeature(tests.SymlinkFeature)
4119.5.1 by James Westby
Shelve can now shelve changes to a symlink's target.
208
        tree = self.create_shelvable_tree()
209
        os.symlink('bar', 'tree/baz')
210
        tree.add('baz', 'baz-id')
211
        tree.commit("Add symlink")
212
        os.unlink('tree/baz')
213
        os.symlink('vax', 'tree/baz')
4595.13.2 by Alexander Belchenko
[cherrypick revno 4650 from bzr.dev] Fix shelve on windows. (Robert Collins, #305006)
214
        tree.lock_tree_write()
215
        self.addCleanup(tree.unlock)
4119.5.1 by James Westby
Shelve can now shelve changes to a symlink's target.
216
        shelver = ExpectShelver(tree, tree.basis_tree())
4603.1.17 by Aaron Bentley
Fix shelf_ui tests to finalize.
217
        self.addCleanup(shelver.finalize)
4119.5.1 by James Westby
Shelve can now shelve changes to a symlink's target.
218
        shelver.expect('Shelve changing target of "baz" from "bar" to '
219
                '"vax"? [yNfq?]', 'y')
220
        shelver.expect('Shelve 1 change(s)? [yNfq?]', 'y')
221
        shelver.run()
222
        self.assertEqual('bar', os.readlink('tree/baz'))
223
0.16.91 by Aaron Bentley
Test finish and quit
224
    def test_shelve_finish(self):
225
        tree = self.create_shelvable_tree()
4595.13.2 by Alexander Belchenko
[cherrypick revno 4650 from bzr.dev] Fix shelve on windows. (Robert Collins, #305006)
226
        tree.lock_tree_write()
227
        self.addCleanup(tree.unlock)
0.16.91 by Aaron Bentley
Test finish and quit
228
        shelver = ExpectShelver(tree, tree.basis_tree())
4603.1.17 by Aaron Bentley
Fix shelf_ui tests to finalize.
229
        self.addCleanup(shelver.finalize)
3990.4.1 by Daniel Watkins
Changed all shelve tests to expect a '?'.
230
        shelver.expect('Shelve? [yNfq?]', 'f')
231
        shelver.expect('Shelve 2 change(s)? [yNfq?]', 'y')
0.16.91 by Aaron Bentley
Test finish and quit
232
        shelver.run()
233
        self.assertFileEqual(LINES_AJ, 'tree/foo')
234
235
    def test_shelve_quit(self):
236
        tree = self.create_shelvable_tree()
4595.13.2 by Alexander Belchenko
[cherrypick revno 4650 from bzr.dev] Fix shelve on windows. (Robert Collins, #305006)
237
        tree.lock_tree_write()
238
        self.addCleanup(tree.unlock)
0.16.91 by Aaron Bentley
Test finish and quit
239
        shelver = ExpectShelver(tree, tree.basis_tree())
4603.1.17 by Aaron Bentley
Fix shelf_ui tests to finalize.
240
        self.addCleanup(shelver.finalize)
3990.4.1 by Daniel Watkins
Changed all shelve tests to expect a '?'.
241
        shelver.expect('Shelve? [yNfq?]', 'q')
0.16.103 by Aaron Bentley
raise UserAbort instead of doing sys.exit
242
        self.assertRaises(errors.UserAbort, shelver.run)
0.16.91 by Aaron Bentley
Test finish and quit
243
        self.assertFileEqual(LINES_ZY, 'tree/foo')
0.16.92 by Aaron Bentley
Test kind, add, binary, --all
244
245
    def test_shelve_all(self):
246
        tree = self.create_shelvable_tree()
4595.13.2 by Alexander Belchenko
[cherrypick revno 4650 from bzr.dev] Fix shelve on windows. (Robert Collins, #305006)
247
        shelver = ExpectShelver.from_args(sys.stdout, all=True,
248
            directory='tree')
249
        try:
250
            shelver.run()
251
        finally:
4603.1.17 by Aaron Bentley
Fix shelf_ui tests to finalize.
252
            shelver.finalize()
0.16.92 by Aaron Bentley
Test kind, add, binary, --all
253
        self.assertFileEqual(LINES_AJ, 'tree/foo')
0.16.93 by Aaron Bentley
Test shelving one file
254
255
    def test_shelve_filename(self):
256
        tree = self.create_shelvable_tree()
257
        self.build_tree(['tree/bar'])
258
        tree.add('bar')
4595.13.2 by Alexander Belchenko
[cherrypick revno 4650 from bzr.dev] Fix shelve on windows. (Robert Collins, #305006)
259
        tree.lock_tree_write()
260
        self.addCleanup(tree.unlock)
0.16.93 by Aaron Bentley
Test shelving one file
261
        shelver = ExpectShelver(tree, tree.basis_tree(), file_list=['bar'])
4603.1.17 by Aaron Bentley
Fix shelf_ui tests to finalize.
262
        self.addCleanup(shelver.finalize)
3990.4.1 by Daniel Watkins
Changed all shelve tests to expect a '?'.
263
        shelver.expect('Shelve adding file "bar"? [yNfq?]', 'y')
264
        shelver.expect('Shelve 1 change(s)? [yNfq?]', 'y')
0.16.93 by Aaron Bentley
Test shelving one file
265
        shelver.run()
0.16.94 by Aaron Bentley
Add unshelve tests
266
3990.4.2 by Daniel Watkins
Added test for help option.
267
    def test_shelve_help(self):
268
        tree = self.create_shelvable_tree()
4595.13.2 by Alexander Belchenko
[cherrypick revno 4650 from bzr.dev] Fix shelve on windows. (Robert Collins, #305006)
269
        tree.lock_tree_write()
270
        self.addCleanup(tree.unlock)
3990.4.2 by Daniel Watkins
Added test for help option.
271
        shelver = ExpectShelver(tree, tree.basis_tree())
4603.1.17 by Aaron Bentley
Fix shelf_ui tests to finalize.
272
        self.addCleanup(shelver.finalize)
3990.4.2 by Daniel Watkins
Added test for help option.
273
        shelver.expect('Shelve? [yNfq?]', '?')
274
        shelver.expect('Shelve? [(y)es, (N)o, (f)inish, or (q)uit]', 'f')
275
        shelver.expect('Shelve 2 change(s)? [yNfq?]', 'y')
276
        shelver.run()
277
4603.1.17 by Aaron Bentley
Fix shelf_ui tests to finalize.
278
    def test_shelve_destroy(self):
4100.3.1 by Aaron Bentley
Implement shelve --destroy
279
        tree = self.create_shelvable_tree()
280
        shelver = shelf_ui.Shelver.from_args(sys.stdout, all=True,
281
                                             directory='tree', destroy=True)
4603.1.17 by Aaron Bentley
Fix shelf_ui tests to finalize.
282
        self.addCleanup(shelver.finalize)
283
        shelver.run()
4100.3.1 by Aaron Bentley
Implement shelve --destroy
284
        self.assertIs(None, tree.get_shelf_manager().last_shelf())
285
        self.assertFileEqual(LINES_AJ, 'tree/foo')
286
4595.9.5 by Aaron Bentley
Add expected failures for shelving root changes.
287
    @staticmethod
288
    def shelve_all(tree, target_revision_id):
289
        tree.lock_write()
290
        try:
291
            target = tree.branch.repository.revision_tree(target_revision_id)
292
            shelver = shelf_ui.Shelver(tree, target, auto=True,
293
                                       auto_apply=True)
4603.1.17 by Aaron Bentley
Fix shelf_ui tests to finalize.
294
            try:
295
                shelver.run()
296
            finally:
297
                shelver.finalize()
4595.9.5 by Aaron Bentley
Add expected failures for shelving root changes.
298
        finally:
299
            tree.unlock()
300
301
    def test_shelve_old_root_deleted(self):
302
        tree1 = self.make_branch_and_tree('tree1')
303
        tree1.commit('add root')
304
        tree2 = self.make_branch_and_tree('tree2')
305
        rev2 = tree2.commit('add root')
306
        tree1.merge_from_branch(tree2.branch,
307
                                from_revision=revision.NULL_REVISION)
308
        tree1.commit('Replaced root entry')
309
        # This is essentially assertNotRaises(InconsistentDelta)
310
        self.expectFailure('Cannot shelve replacing a root entry',
311
                           self.assertRaises, AssertionError,
312
                           self.assertRaises, errors.InconsistentDelta,
313
                           self.shelve_all, tree1, rev2)
314
315
    def test_shelve_split(self):
316
        outer_tree = self.make_branch_and_tree('outer')
317
        outer_tree.commit('Add root')
318
        inner_tree = self.make_branch_and_tree('outer/inner')
319
        rev2 = inner_tree.commit('Add root')
320
        outer_tree.subsume(inner_tree)
4595.9.6 by Aaron Bentley
Update from review.
321
        # This is essentially assertNotRaises(ValueError).
322
        # The ValueError is 'None is not a valid file id'.
4595.9.5 by Aaron Bentley
Add expected failures for shelving root changes.
323
        self.expectFailure('Cannot shelve a join back to the inner tree.',
324
                           self.assertRaises, AssertionError,
325
                           self.assertRaises, ValueError, self.shelve_all,
326
                           outer_tree, rev2)
327
0.16.94 by Aaron Bentley
Add unshelve tests
328
4526.7.1 by Aaron Bentley
Make vocabulary part of reporter.
329
class TestApplyReporter(TestShelver):
4526.6.1 by Aaron Bentley
Reverse the way changes are described by Shelver.
330
331
    def test_shelve_not_diff(self):
332
        tree = self.create_shelvable_tree()
4595.13.2 by Alexander Belchenko
[cherrypick revno 4650 from bzr.dev] Fix shelve on windows. (Robert Collins, #305006)
333
        tree.lock_tree_write()
334
        self.addCleanup(tree.unlock)
4526.7.1 by Aaron Bentley
Make vocabulary part of reporter.
335
        shelver = ExpectShelver(tree, tree.basis_tree(),
336
                                reporter=shelf_ui.ApplyReporter())
4603.1.17 by Aaron Bentley
Fix shelf_ui tests to finalize.
337
        self.addCleanup(shelver.finalize)
4526.6.1 by Aaron Bentley
Reverse the way changes are described by Shelver.
338
        shelver.expect('Apply change? [yNfq?]', 'n')
339
        shelver.expect('Apply change? [yNfq?]', 'n')
340
        # No final shelving prompt because no changes were selected
341
        shelver.run()
342
        self.assertFileEqual(LINES_ZY, 'tree/foo')
343
344
    def test_shelve_diff_no(self):
345
        tree = self.create_shelvable_tree()
4595.13.2 by Alexander Belchenko
[cherrypick revno 4650 from bzr.dev] Fix shelve on windows. (Robert Collins, #305006)
346
        tree.lock_tree_write()
347
        self.addCleanup(tree.unlock)
4526.7.1 by Aaron Bentley
Make vocabulary part of reporter.
348
        shelver = ExpectShelver(tree, tree.basis_tree(),
349
                                reporter=shelf_ui.ApplyReporter())
4603.1.17 by Aaron Bentley
Fix shelf_ui tests to finalize.
350
        self.addCleanup(shelver.finalize)
4526.6.1 by Aaron Bentley
Reverse the way changes are described by Shelver.
351
        shelver.expect('Apply change? [yNfq?]', 'y')
352
        shelver.expect('Apply change? [yNfq?]', 'y')
353
        shelver.expect('Apply 2 change(s)? [yNfq?]', 'n')
354
        shelver.run()
355
        self.assertFileEqual(LINES_ZY, 'tree/foo')
356
357
    def test_shelve_diff(self):
358
        tree = self.create_shelvable_tree()
4595.13.2 by Alexander Belchenko
[cherrypick revno 4650 from bzr.dev] Fix shelve on windows. (Robert Collins, #305006)
359
        tree.lock_tree_write()
360
        self.addCleanup(tree.unlock)
4526.7.1 by Aaron Bentley
Make vocabulary part of reporter.
361
        shelver = ExpectShelver(tree, tree.basis_tree(),
362
                                reporter=shelf_ui.ApplyReporter())
4603.1.17 by Aaron Bentley
Fix shelf_ui tests to finalize.
363
        self.addCleanup(shelver.finalize)
4526.6.1 by Aaron Bentley
Reverse the way changes are described by Shelver.
364
        shelver.expect('Apply change? [yNfq?]', 'y')
365
        shelver.expect('Apply change? [yNfq?]', 'y')
366
        shelver.expect('Apply 2 change(s)? [yNfq?]', 'y')
367
        shelver.run()
368
        self.assertFileEqual(LINES_AJ, 'tree/foo')
369
370
    def test_shelve_binary_change(self):
371
        tree = self.create_shelvable_tree()
372
        self.build_tree_contents([('tree/foo', '\x00')])
4595.13.2 by Alexander Belchenko
[cherrypick revno 4650 from bzr.dev] Fix shelve on windows. (Robert Collins, #305006)
373
        tree.lock_tree_write()
374
        self.addCleanup(tree.unlock)
4526.7.1 by Aaron Bentley
Make vocabulary part of reporter.
375
        shelver = ExpectShelver(tree, tree.basis_tree(),
376
                                reporter=shelf_ui.ApplyReporter())
4603.1.17 by Aaron Bentley
Fix shelf_ui tests to finalize.
377
        self.addCleanup(shelver.finalize)
4526.6.1 by Aaron Bentley
Reverse the way changes are described by Shelver.
378
        shelver.expect('Apply binary changes? [yNfq?]', 'y')
379
        shelver.expect('Apply 1 change(s)? [yNfq?]', 'y')
380
        shelver.run()
381
        self.assertFileEqual(LINES_AJ, 'tree/foo')
382
383
    def test_shelve_rename(self):
384
        tree = self.create_shelvable_tree()
385
        tree.rename_one('foo', 'bar')
4595.13.2 by Alexander Belchenko
[cherrypick revno 4650 from bzr.dev] Fix shelve on windows. (Robert Collins, #305006)
386
        tree.lock_tree_write()
387
        self.addCleanup(tree.unlock)
4526.7.1 by Aaron Bentley
Make vocabulary part of reporter.
388
        shelver = ExpectShelver(tree, tree.basis_tree(),
389
                                reporter=shelf_ui.ApplyReporter())
4603.1.17 by Aaron Bentley
Fix shelf_ui tests to finalize.
390
        self.addCleanup(shelver.finalize)
4526.6.1 by Aaron Bentley
Reverse the way changes are described by Shelver.
391
        shelver.expect('Rename "bar" => "foo"? [yNfq?]', 'y')
392
        shelver.expect('Apply change? [yNfq?]', 'y')
393
        shelver.expect('Apply change? [yNfq?]', 'y')
394
        shelver.expect('Apply 3 change(s)? [yNfq?]', 'y')
395
        shelver.run()
396
        self.assertFileEqual(LINES_AJ, 'tree/foo')
397
398
    def test_shelve_deletion(self):
399
        tree = self.create_shelvable_tree()
400
        os.unlink('tree/foo')
4595.13.2 by Alexander Belchenko
[cherrypick revno 4650 from bzr.dev] Fix shelve on windows. (Robert Collins, #305006)
401
        tree.lock_tree_write()
402
        self.addCleanup(tree.unlock)
4526.7.1 by Aaron Bentley
Make vocabulary part of reporter.
403
        shelver = ExpectShelver(tree, tree.basis_tree(),
404
                                reporter=shelf_ui.ApplyReporter())
4603.1.17 by Aaron Bentley
Fix shelf_ui tests to finalize.
405
        self.addCleanup(shelver.finalize)
4526.6.1 by Aaron Bentley
Reverse the way changes are described by Shelver.
406
        shelver.expect('Add file "foo"? [yNfq?]', 'y')
407
        shelver.expect('Apply 1 change(s)? [yNfq?]', 'y')
408
        shelver.run()
409
        self.assertFileEqual(LINES_AJ, 'tree/foo')
410
411
    def test_shelve_creation(self):
412
        tree = self.make_branch_and_tree('tree')
413
        tree.commit('add tree root')
414
        self.build_tree(['tree/foo'])
415
        tree.add('foo')
4595.13.2 by Alexander Belchenko
[cherrypick revno 4650 from bzr.dev] Fix shelve on windows. (Robert Collins, #305006)
416
        tree.lock_tree_write()
417
        self.addCleanup(tree.unlock)
4526.7.1 by Aaron Bentley
Make vocabulary part of reporter.
418
        shelver = ExpectShelver(tree, tree.basis_tree(),
419
                                reporter=shelf_ui.ApplyReporter())
4603.1.17 by Aaron Bentley
Fix shelf_ui tests to finalize.
420
        self.addCleanup(shelver.finalize)
4526.6.1 by Aaron Bentley
Reverse the way changes are described by Shelver.
421
        shelver.expect('Delete file "foo"? [yNfq?]', 'y')
422
        shelver.expect('Apply 1 change(s)? [yNfq?]', 'y')
423
        shelver.run()
424
        self.failIfExists('tree/foo')
425
426
    def test_shelve_kind_change(self):
427
        tree = self.create_shelvable_tree()
428
        os.unlink('tree/foo')
429
        os.mkdir('tree/foo')
4595.13.2 by Alexander Belchenko
[cherrypick revno 4650 from bzr.dev] Fix shelve on windows. (Robert Collins, #305006)
430
        tree.lock_tree_write()
431
        self.addCleanup(tree.unlock)
4526.7.1 by Aaron Bentley
Make vocabulary part of reporter.
432
        shelver = ExpectShelver(tree, tree.basis_tree(),
433
                               reporter=shelf_ui.ApplyReporter())
4603.1.17 by Aaron Bentley
Fix shelf_ui tests to finalize.
434
        self.addCleanup(shelver.finalize)
4526.6.1 by Aaron Bentley
Reverse the way changes are described by Shelver.
435
        shelver.expect('Change "foo" from directory to a file? [yNfq?]', 'y')
436
        shelver.expect('Apply 1 change(s)? [yNfq?]', 'y')
437
438
    def test_shelve_modify_target(self):
4526.6.12 by Aaron Bentley
Updates from review.
439
        self.requireFeature(tests.SymlinkFeature)
4526.6.1 by Aaron Bentley
Reverse the way changes are described by Shelver.
440
        tree = self.create_shelvable_tree()
441
        os.symlink('bar', 'tree/baz')
442
        tree.add('baz', 'baz-id')
443
        tree.commit("Add symlink")
444
        os.unlink('tree/baz')
445
        os.symlink('vax', 'tree/baz')
4595.13.2 by Alexander Belchenko
[cherrypick revno 4650 from bzr.dev] Fix shelve on windows. (Robert Collins, #305006)
446
        tree.lock_tree_write()
447
        self.addCleanup(tree.unlock)
4526.7.1 by Aaron Bentley
Make vocabulary part of reporter.
448
        shelver = ExpectShelver(tree, tree.basis_tree(),
449
                                reporter=shelf_ui.ApplyReporter())
4603.1.17 by Aaron Bentley
Fix shelf_ui tests to finalize.
450
        self.addCleanup(shelver.finalize)
4526.6.1 by Aaron Bentley
Reverse the way changes are described by Shelver.
451
        shelver.expect('Change target of "baz" from "vax" to "bar"? [yNfq?]',
452
                       'y')
453
        shelver.expect('Apply 1 change(s)? [yNfq?]', 'y')
454
        shelver.run()
455
        self.assertEqual('bar', os.readlink('tree/baz'))
456
457
0.16.94 by Aaron Bentley
Add unshelve tests
458
class TestUnshelver(tests.TestCaseWithTransport):
459
460
    def create_tree_with_shelf(self):
461
        tree = self.make_branch_and_tree('tree')
4595.13.2 by Alexander Belchenko
[cherrypick revno 4650 from bzr.dev] Fix shelve on windows. (Robert Collins, #305006)
462
        tree.lock_write()
463
        try:
464
            self.build_tree_contents([('tree/foo', LINES_AJ)])
465
            tree.add('foo', 'foo-id')
466
            tree.commit('added foo')
467
            self.build_tree_contents([('tree/foo', LINES_ZY)])
4603.1.17 by Aaron Bentley
Fix shelf_ui tests to finalize.
468
            shelver = shelf_ui.Shelver(tree, tree.basis_tree(),
469
                                       auto_apply=True, auto=True)
470
            try:
471
                shelver.run()
472
            finally:
473
                shelver.finalize()
4595.13.2 by Alexander Belchenko
[cherrypick revno 4650 from bzr.dev] Fix shelve on windows. (Robert Collins, #305006)
474
        finally:
475
            tree.unlock()
0.16.94 by Aaron Bentley
Add unshelve tests
476
        return tree
477
478
    def test_unshelve(self):
479
        tree = self.create_tree_with_shelf()
480
        tree.lock_write()
481
        self.addCleanup(tree.unlock)
482
        manager = tree.get_shelf_manager()
483
        shelf_ui.Unshelver(tree, manager, 1, True, True, True).run()
484
        self.assertFileEqual(LINES_ZY, 'tree/foo')
485
486
    def test_unshelve_args(self):
487
        tree = self.create_tree_with_shelf()
4595.13.2 by Alexander Belchenko
[cherrypick revno 4650 from bzr.dev] Fix shelve on windows. (Robert Collins, #305006)
488
        unshelver = shelf_ui.Unshelver.from_args(directory='tree')
489
        try:
490
            unshelver.run()
491
        finally:
492
            unshelver.tree.unlock()
0.16.94 by Aaron Bentley
Add unshelve tests
493
        self.assertFileEqual(LINES_ZY, 'tree/foo')
494
        self.assertIs(None, tree.get_shelf_manager().last_shelf())
495
496
    def test_unshelve_args_dry_run(self):
497
        tree = self.create_tree_with_shelf()
4595.13.2 by Alexander Belchenko
[cherrypick revno 4650 from bzr.dev] Fix shelve on windows. (Robert Collins, #305006)
498
        unshelver = shelf_ui.Unshelver.from_args(directory='tree',
499
            action='dry-run')
500
        try:
501
            unshelver.run()
502
        finally:
503
            unshelver.tree.unlock()
0.16.94 by Aaron Bentley
Add unshelve tests
504
        self.assertFileEqual(LINES_AJ, 'tree/foo')
505
        self.assertEqual(1, tree.get_shelf_manager().last_shelf())
506
507
    def test_unshelve_args_delete_only(self):
508
        tree = self.make_branch_and_tree('tree')
509
        manager = tree.get_shelf_manager()
510
        shelf_file = manager.new_shelf()[1]
511
        try:
512
            shelf_file.write('garbage')
513
        finally:
514
            shelf_file.close()
515
        unshelver = shelf_ui.Unshelver.from_args(directory='tree',
516
                                                 action='delete-only')
4595.13.2 by Alexander Belchenko
[cherrypick revno 4650 from bzr.dev] Fix shelve on windows. (Robert Collins, #305006)
517
        try:
518
            unshelver.run()
519
        finally:
520
            unshelver.tree.unlock()
0.16.94 by Aaron Bentley
Add unshelve tests
521
        self.assertIs(None, manager.last_shelf())
3990.2.1 by Daniel Watkins
Added test for unshelve being passed an invalid shelf_id.
522
523
    def test_unshelve_args_invalid_shelf_id(self):
524
        tree = self.make_branch_and_tree('tree')
525
        manager = tree.get_shelf_manager()
526
        shelf_file = manager.new_shelf()[1]
527
        try:
528
            shelf_file.write('garbage')
529
        finally:
530
            shelf_file.close()
531
        self.assertRaises(errors.InvalidShelfId,
3999.1.1 by Ian Clatworthy
Improve shelf documentation & fix backtrace (Daniel Watkins)
532
            shelf_ui.Unshelver.from_args, directory='tree',
533
            action='delete-only', shelf_id='foo')
4899.2.1 by Neil Martinsen-Burrell
add beter feedback from the unshelve command
534
4899.2.2 by Neil Martinsen-Burrell
tests updated to use the appropriate TestCase... class for scripts
535
536
class TestUnshelveScripts(TestUnshelver, 
537
                          script.TestCaseWithTransportAndScript): 
538
4899.2.1 by Neil Martinsen-Burrell
add beter feedback from the unshelve command
539
    def test_unshelve_messages_keep(self):
540
        self.create_tree_with_shelf()
4899.2.2 by Neil Martinsen-Burrell
tests updated to use the appropriate TestCase... class for scripts
541
        self.run_script("""
4899.2.1 by Neil Martinsen-Burrell
add beter feedback from the unshelve command
542
$ cd tree
543
$ bzr unshelve --keep
544
2>Using changes with id "1".
545
2> M  foo
546
2>All changes applied successfully.
547
""")
548
549
    def test_unshelve_messages_delete(self):
550
        self.create_tree_with_shelf()
4899.2.2 by Neil Martinsen-Burrell
tests updated to use the appropriate TestCase... class for scripts
551
        self.run_script("""
4899.2.1 by Neil Martinsen-Burrell
add beter feedback from the unshelve command
552
$ cd tree
553
$ bzr unshelve --delete-only
554
2>Deleted changes with id "1".
555
""")
556
557
    def test_unshelve_messages_apply(self):
558
        self.create_tree_with_shelf()
4899.2.2 by Neil Martinsen-Burrell
tests updated to use the appropriate TestCase... class for scripts
559
        self.run_script("""
4899.2.1 by Neil Martinsen-Burrell
add beter feedback from the unshelve command
560
$ cd tree
561
$ bzr unshelve --apply
562
2>Using changes with id "1".
563
2> M  foo
564
2>All changes applied successfully.
565
2>Deleted changes with id "1".
566
""")
567
568
    def test_unshelve_messages_dry_run(self):
569
        self.create_tree_with_shelf()
4899.2.2 by Neil Martinsen-Burrell
tests updated to use the appropriate TestCase... class for scripts
570
        self.run_script("""
4899.2.1 by Neil Martinsen-Burrell
add beter feedback from the unshelve command
571
$ cd tree
572
$ bzr unshelve --dry-run
573
2>Using changes with id "1".
574
2> M  foo
575
""")