~bzr-pqm/bzr/bzr.dev

« back to all changes in this revision

Viewing changes to bzrlib/tests/test_shelf_ui.py

  • Committer: Martin Pool
  • Date: 2010-01-12 02:00:23 UTC
  • mto: This revision was merged to the branch mainline in revision 4949.
  • Revision ID: mbp@sourcefrog.net-20100112020023-ib3ii1wcpvljmprk
Update bug handling doc to deprecate fixcommitted and to explain other states better

Show diffs side-by-side

added added

removed removed

Lines of Context:
 
1
# Copyright (C) 2008 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., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
 
16
 
 
17
 
 
18
from cStringIO import StringIO
 
19
import os
 
20
import sys
 
21
 
 
22
from bzrlib import (
 
23
    errors,
 
24
    shelf_ui,
 
25
    revision,
 
26
    tests,
 
27
)
 
28
from bzrlib.tests import script
 
29
 
 
30
 
 
31
class ExpectShelver(shelf_ui.Shelver):
 
32
    """A variant of Shelver that intercepts console activity, for testing."""
 
33
 
 
34
    def __init__(self, work_tree, target_tree, diff_writer=None,
 
35
                 auto=False, auto_apply=False, file_list=None, message=None,
 
36
                 destroy=False, reporter=None):
 
37
        shelf_ui.Shelver.__init__(self, work_tree, target_tree, diff_writer,
 
38
                                  auto, auto_apply, file_list, message,
 
39
                                  destroy, reporter=reporter)
 
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
 
 
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
 
 
65
class TestShelver(tests.TestCaseWithTransport):
 
66
 
 
67
    def create_shelvable_tree(self):
 
68
        tree = self.make_branch_and_tree('tree')
 
69
        self.build_tree_contents([('tree/foo', LINES_AJ)])
 
70
        tree.add('foo', 'foo-id')
 
71
        tree.commit('added foo')
 
72
        self.build_tree_contents([('tree/foo', LINES_ZY)])
 
73
        return tree
 
74
 
 
75
    def test_unexpected_prompt_failure(self):
 
76
        tree = self.create_shelvable_tree()
 
77
        tree.lock_tree_write()
 
78
        self.addCleanup(tree.unlock)
 
79
        shelver = ExpectShelver(tree, tree.basis_tree())
 
80
        self.addCleanup(shelver.finalize)
 
81
        e = self.assertRaises(AssertionError, shelver.run)
 
82
        self.assertEqual('Unexpected prompt: Shelve? [yNfq?]', str(e))
 
83
 
 
84
    def test_wrong_prompt_failure(self):
 
85
        tree = self.create_shelvable_tree()
 
86
        tree.lock_tree_write()
 
87
        self.addCleanup(tree.unlock)
 
88
        shelver = ExpectShelver(tree, tree.basis_tree())
 
89
        self.addCleanup(shelver.finalize)
 
90
        shelver.expect('foo', 'y')
 
91
        e = self.assertRaises(AssertionError, shelver.run)
 
92
        self.assertEqual('Wrong prompt: Shelve? [yNfq?]', str(e))
 
93
 
 
94
    def test_shelve_not_diff(self):
 
95
        tree = self.create_shelvable_tree()
 
96
        tree.lock_tree_write()
 
97
        self.addCleanup(tree.unlock)
 
98
        shelver = ExpectShelver(tree, tree.basis_tree())
 
99
        self.addCleanup(shelver.finalize)
 
100
        shelver.expect('Shelve? [yNfq?]', 'n')
 
101
        shelver.expect('Shelve? [yNfq?]', 'n')
 
102
        # No final shelving prompt because no changes were selected
 
103
        shelver.run()
 
104
        self.assertFileEqual(LINES_ZY, 'tree/foo')
 
105
 
 
106
    def test_shelve_diff_no(self):
 
107
        tree = self.create_shelvable_tree()
 
108
        tree.lock_tree_write()
 
109
        self.addCleanup(tree.unlock)
 
110
        shelver = ExpectShelver(tree, tree.basis_tree())
 
111
        self.addCleanup(shelver.finalize)
 
112
        shelver.expect('Shelve? [yNfq?]', 'y')
 
113
        shelver.expect('Shelve? [yNfq?]', 'y')
 
114
        shelver.expect('Shelve 2 change(s)? [yNfq?]', 'n')
 
115
        shelver.run()
 
116
        self.assertFileEqual(LINES_ZY, 'tree/foo')
 
117
 
 
118
    def test_shelve_diff(self):
 
119
        tree = self.create_shelvable_tree()
 
120
        tree.lock_tree_write()
 
121
        self.addCleanup(tree.unlock)
 
122
        shelver = ExpectShelver(tree, tree.basis_tree())
 
123
        self.addCleanup(shelver.finalize)
 
124
        shelver.expect('Shelve? [yNfq?]', 'y')
 
125
        shelver.expect('Shelve? [yNfq?]', 'y')
 
126
        shelver.expect('Shelve 2 change(s)? [yNfq?]', 'y')
 
127
        shelver.run()
 
128
        self.assertFileEqual(LINES_AJ, 'tree/foo')
 
129
 
 
130
    def test_shelve_one_diff(self):
 
131
        tree = self.create_shelvable_tree()
 
132
        tree.lock_tree_write()
 
133
        self.addCleanup(tree.unlock)
 
134
        shelver = ExpectShelver(tree, tree.basis_tree())
 
135
        self.addCleanup(shelver.finalize)
 
136
        shelver.expect('Shelve? [yNfq?]', 'y')
 
137
        shelver.expect('Shelve? [yNfq?]', 'n')
 
138
        shelver.expect('Shelve 1 change(s)? [yNfq?]', 'y')
 
139
        shelver.run()
 
140
        self.assertFileEqual(LINES_AY, 'tree/foo')
 
141
 
 
142
    def test_shelve_binary_change(self):
 
143
        tree = self.create_shelvable_tree()
 
144
        self.build_tree_contents([('tree/foo', '\x00')])
 
145
        tree.lock_tree_write()
 
146
        self.addCleanup(tree.unlock)
 
147
        shelver = ExpectShelver(tree, tree.basis_tree())
 
148
        self.addCleanup(shelver.finalize)
 
149
        shelver.expect('Shelve binary changes? [yNfq?]', 'y')
 
150
        shelver.expect('Shelve 1 change(s)? [yNfq?]', 'y')
 
151
        shelver.run()
 
152
        self.assertFileEqual(LINES_AJ, 'tree/foo')
 
153
 
 
154
    def test_shelve_rename(self):
 
155
        tree = self.create_shelvable_tree()
 
156
        tree.rename_one('foo', 'bar')
 
157
        tree.lock_tree_write()
 
158
        self.addCleanup(tree.unlock)
 
159
        shelver = ExpectShelver(tree, tree.basis_tree())
 
160
        self.addCleanup(shelver.finalize)
 
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')
 
165
        shelver.run()
 
166
        self.assertFileEqual(LINES_AJ, 'tree/foo')
 
167
 
 
168
    def test_shelve_deletion(self):
 
169
        tree = self.create_shelvable_tree()
 
170
        os.unlink('tree/foo')
 
171
        tree.lock_tree_write()
 
172
        self.addCleanup(tree.unlock)
 
173
        shelver = ExpectShelver(tree, tree.basis_tree())
 
174
        self.addCleanup(shelver.finalize)
 
175
        shelver.expect('Shelve removing file "foo"? [yNfq?]', 'y')
 
176
        shelver.expect('Shelve 1 change(s)? [yNfq?]', 'y')
 
177
        shelver.run()
 
178
        self.assertFileEqual(LINES_AJ, 'tree/foo')
 
179
 
 
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')
 
185
        tree.lock_tree_write()
 
186
        self.addCleanup(tree.unlock)
 
187
        shelver = ExpectShelver(tree, tree.basis_tree())
 
188
        self.addCleanup(shelver.finalize)
 
189
        shelver.expect('Shelve adding file "foo"? [yNfq?]', 'y')
 
190
        shelver.expect('Shelve 1 change(s)? [yNfq?]', 'y')
 
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')
 
198
        tree.lock_tree_write()
 
199
        self.addCleanup(tree.unlock)
 
200
        shelver = ExpectShelver(tree, tree.basis_tree())
 
201
        self.addCleanup(shelver.finalize)
 
202
        shelver.expect('Shelve changing "foo" from file to directory? [yNfq?]',
 
203
                       'y')
 
204
        shelver.expect('Shelve 1 change(s)? [yNfq?]', 'y')
 
205
 
 
206
    def test_shelve_modify_target(self):
 
207
        self.requireFeature(tests.SymlinkFeature)
 
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')
 
214
        tree.lock_tree_write()
 
215
        self.addCleanup(tree.unlock)
 
216
        shelver = ExpectShelver(tree, tree.basis_tree())
 
217
        self.addCleanup(shelver.finalize)
 
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
 
 
224
    def test_shelve_finish(self):
 
225
        tree = self.create_shelvable_tree()
 
226
        tree.lock_tree_write()
 
227
        self.addCleanup(tree.unlock)
 
228
        shelver = ExpectShelver(tree, tree.basis_tree())
 
229
        self.addCleanup(shelver.finalize)
 
230
        shelver.expect('Shelve? [yNfq?]', 'f')
 
231
        shelver.expect('Shelve 2 change(s)? [yNfq?]', 'y')
 
232
        shelver.run()
 
233
        self.assertFileEqual(LINES_AJ, 'tree/foo')
 
234
 
 
235
    def test_shelve_quit(self):
 
236
        tree = self.create_shelvable_tree()
 
237
        tree.lock_tree_write()
 
238
        self.addCleanup(tree.unlock)
 
239
        shelver = ExpectShelver(tree, tree.basis_tree())
 
240
        self.addCleanup(shelver.finalize)
 
241
        shelver.expect('Shelve? [yNfq?]', 'q')
 
242
        self.assertRaises(errors.UserAbort, shelver.run)
 
243
        self.assertFileEqual(LINES_ZY, 'tree/foo')
 
244
 
 
245
    def test_shelve_all(self):
 
246
        tree = self.create_shelvable_tree()
 
247
        shelver = ExpectShelver.from_args(sys.stdout, all=True,
 
248
            directory='tree')
 
249
        try:
 
250
            shelver.run()
 
251
        finally:
 
252
            shelver.finalize()
 
253
        self.assertFileEqual(LINES_AJ, 'tree/foo')
 
254
 
 
255
    def test_shelve_filename(self):
 
256
        tree = self.create_shelvable_tree()
 
257
        self.build_tree(['tree/bar'])
 
258
        tree.add('bar')
 
259
        tree.lock_tree_write()
 
260
        self.addCleanup(tree.unlock)
 
261
        shelver = ExpectShelver(tree, tree.basis_tree(), file_list=['bar'])
 
262
        self.addCleanup(shelver.finalize)
 
263
        shelver.expect('Shelve adding file "bar"? [yNfq?]', 'y')
 
264
        shelver.expect('Shelve 1 change(s)? [yNfq?]', 'y')
 
265
        shelver.run()
 
266
 
 
267
    def test_shelve_help(self):
 
268
        tree = self.create_shelvable_tree()
 
269
        tree.lock_tree_write()
 
270
        self.addCleanup(tree.unlock)
 
271
        shelver = ExpectShelver(tree, tree.basis_tree())
 
272
        self.addCleanup(shelver.finalize)
 
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
 
 
278
    def test_shelve_destroy(self):
 
279
        tree = self.create_shelvable_tree()
 
280
        shelver = shelf_ui.Shelver.from_args(sys.stdout, all=True,
 
281
                                             directory='tree', destroy=True)
 
282
        self.addCleanup(shelver.finalize)
 
283
        shelver.run()
 
284
        self.assertIs(None, tree.get_shelf_manager().last_shelf())
 
285
        self.assertFileEqual(LINES_AJ, 'tree/foo')
 
286
 
 
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)
 
294
            try:
 
295
                shelver.run()
 
296
            finally:
 
297
                shelver.finalize()
 
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)
 
321
        # This is essentially assertNotRaises(ValueError).
 
322
        # The ValueError is 'None is not a valid file id'.
 
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
 
 
328
 
 
329
class TestApplyReporter(TestShelver):
 
330
 
 
331
    def test_shelve_not_diff(self):
 
332
        tree = self.create_shelvable_tree()
 
333
        tree.lock_tree_write()
 
334
        self.addCleanup(tree.unlock)
 
335
        shelver = ExpectShelver(tree, tree.basis_tree(),
 
336
                                reporter=shelf_ui.ApplyReporter())
 
337
        self.addCleanup(shelver.finalize)
 
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()
 
346
        tree.lock_tree_write()
 
347
        self.addCleanup(tree.unlock)
 
348
        shelver = ExpectShelver(tree, tree.basis_tree(),
 
349
                                reporter=shelf_ui.ApplyReporter())
 
350
        self.addCleanup(shelver.finalize)
 
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()
 
359
        tree.lock_tree_write()
 
360
        self.addCleanup(tree.unlock)
 
361
        shelver = ExpectShelver(tree, tree.basis_tree(),
 
362
                                reporter=shelf_ui.ApplyReporter())
 
363
        self.addCleanup(shelver.finalize)
 
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')])
 
373
        tree.lock_tree_write()
 
374
        self.addCleanup(tree.unlock)
 
375
        shelver = ExpectShelver(tree, tree.basis_tree(),
 
376
                                reporter=shelf_ui.ApplyReporter())
 
377
        self.addCleanup(shelver.finalize)
 
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')
 
386
        tree.lock_tree_write()
 
387
        self.addCleanup(tree.unlock)
 
388
        shelver = ExpectShelver(tree, tree.basis_tree(),
 
389
                                reporter=shelf_ui.ApplyReporter())
 
390
        self.addCleanup(shelver.finalize)
 
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')
 
401
        tree.lock_tree_write()
 
402
        self.addCleanup(tree.unlock)
 
403
        shelver = ExpectShelver(tree, tree.basis_tree(),
 
404
                                reporter=shelf_ui.ApplyReporter())
 
405
        self.addCleanup(shelver.finalize)
 
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')
 
416
        tree.lock_tree_write()
 
417
        self.addCleanup(tree.unlock)
 
418
        shelver = ExpectShelver(tree, tree.basis_tree(),
 
419
                                reporter=shelf_ui.ApplyReporter())
 
420
        self.addCleanup(shelver.finalize)
 
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')
 
430
        tree.lock_tree_write()
 
431
        self.addCleanup(tree.unlock)
 
432
        shelver = ExpectShelver(tree, tree.basis_tree(),
 
433
                               reporter=shelf_ui.ApplyReporter())
 
434
        self.addCleanup(shelver.finalize)
 
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):
 
439
        self.requireFeature(tests.SymlinkFeature)
 
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')
 
446
        tree.lock_tree_write()
 
447
        self.addCleanup(tree.unlock)
 
448
        shelver = ExpectShelver(tree, tree.basis_tree(),
 
449
                                reporter=shelf_ui.ApplyReporter())
 
450
        self.addCleanup(shelver.finalize)
 
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
 
 
458
class TestUnshelver(tests.TestCaseWithTransport):
 
459
 
 
460
    def create_tree_with_shelf(self):
 
461
        tree = self.make_branch_and_tree('tree')
 
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)])
 
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()
 
474
        finally:
 
475
            tree.unlock()
 
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()
 
488
        unshelver = shelf_ui.Unshelver.from_args(directory='tree')
 
489
        try:
 
490
            unshelver.run()
 
491
        finally:
 
492
            unshelver.tree.unlock()
 
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()
 
498
        unshelver = shelf_ui.Unshelver.from_args(directory='tree',
 
499
            action='dry-run')
 
500
        try:
 
501
            unshelver.run()
 
502
        finally:
 
503
            unshelver.tree.unlock()
 
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')
 
517
        try:
 
518
            unshelver.run()
 
519
        finally:
 
520
            unshelver.tree.unlock()
 
521
        self.assertIs(None, manager.last_shelf())
 
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,
 
532
            shelf_ui.Unshelver.from_args, directory='tree',
 
533
            action='delete-only', shelf_id='foo')
 
534
 
 
535
 
 
536
class TestUnshelveScripts(TestUnshelver, 
 
537
                          script.TestCaseWithTransportAndScript): 
 
538
 
 
539
    def test_unshelve_messages_keep(self):
 
540
        self.create_tree_with_shelf()
 
541
        self.run_script("""
 
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()
 
551
        self.run_script("""
 
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()
 
559
        self.run_script("""
 
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()
 
570
        self.run_script("""
 
571
$ cd tree
 
572
$ bzr unshelve --dry-run
 
573
2>Using changes with id "1".
 
574
2> M  foo
 
575
""")