~bzr-pqm/bzr/bzr.dev

« back to all changes in this revision

Viewing changes to bzrlib/tests/test_shelf_ui.py

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
 
 
29
 
 
30
class ExpectShelver(shelf_ui.Shelver):
 
31
    """A variant of Shelver that intercepts console activity, for testing."""
 
32
 
 
33
    def __init__(self, work_tree, target_tree, diff_writer=None,
 
34
                 auto=False, auto_apply=False, file_list=None, message=None,
 
35
                 destroy=False, reporter=None):
 
36
        shelf_ui.Shelver.__init__(self, work_tree, target_tree, diff_writer,
 
37
                                  auto, auto_apply, file_list, message,
 
38
                                  destroy, reporter=reporter)
 
39
        self.expected = []
 
40
        self.diff_writer = StringIO()
 
41
 
 
42
    def expect(self, prompt, response):
 
43
        self.expected.append((prompt, response))
 
44
 
 
45
    def prompt(self, message):
 
46
        try:
 
47
            prompt, response = self.expected.pop(0)
 
48
        except IndexError:
 
49
            raise AssertionError('Unexpected prompt: %s' % message)
 
50
        if prompt != message:
 
51
            raise AssertionError('Wrong prompt: %s' % message)
 
52
        return response
 
53
 
 
54
 
 
55
LINES_AJ = 'a\nb\nc\nd\ne\nf\ng\nh\ni\nj\n'
 
56
 
 
57
 
 
58
LINES_ZY = 'z\nb\nc\nd\ne\nf\ng\nh\ni\ny\n'
 
59
 
 
60
 
 
61
LINES_AY = 'a\nb\nc\nd\ne\nf\ng\nh\ni\ny\n'
 
62
 
 
63
 
 
64
class TestShelver(tests.TestCaseWithTransport):
 
65
 
 
66
    def create_shelvable_tree(self):
 
67
        tree = self.make_branch_and_tree('tree')
 
68
        self.build_tree_contents([('tree/foo', LINES_AJ)])
 
69
        tree.add('foo', 'foo-id')
 
70
        tree.commit('added foo')
 
71
        self.build_tree_contents([('tree/foo', LINES_ZY)])
 
72
        return tree
 
73
 
 
74
    def test_unexpected_prompt_failure(self):
 
75
        tree = self.create_shelvable_tree()
 
76
        tree.lock_tree_write()
 
77
        self.addCleanup(tree.unlock)
 
78
        shelver = ExpectShelver(tree, tree.basis_tree())
 
79
        e = self.assertRaises(AssertionError, shelver.run)
 
80
        self.assertEqual('Unexpected prompt: Shelve? [yNfq?]', str(e))
 
81
 
 
82
    def test_wrong_prompt_failure(self):
 
83
        tree = self.create_shelvable_tree()
 
84
        tree.lock_tree_write()
 
85
        self.addCleanup(tree.unlock)
 
86
        shelver = ExpectShelver(tree, tree.basis_tree())
 
87
        shelver.expect('foo', 'y')
 
88
        e = self.assertRaises(AssertionError, shelver.run)
 
89
        self.assertEqual('Wrong prompt: Shelve? [yNfq?]', str(e))
 
90
 
 
91
    def test_shelve_not_diff(self):
 
92
        tree = self.create_shelvable_tree()
 
93
        tree.lock_tree_write()
 
94
        self.addCleanup(tree.unlock)
 
95
        shelver = ExpectShelver(tree, tree.basis_tree())
 
96
        shelver.expect('Shelve? [yNfq?]', 'n')
 
97
        shelver.expect('Shelve? [yNfq?]', 'n')
 
98
        # No final shelving prompt because no changes were selected
 
99
        shelver.run()
 
100
        self.assertFileEqual(LINES_ZY, 'tree/foo')
 
101
 
 
102
    def test_shelve_diff_no(self):
 
103
        tree = self.create_shelvable_tree()
 
104
        tree.lock_tree_write()
 
105
        self.addCleanup(tree.unlock)
 
106
        shelver = ExpectShelver(tree, tree.basis_tree())
 
107
        shelver.expect('Shelve? [yNfq?]', 'y')
 
108
        shelver.expect('Shelve? [yNfq?]', 'y')
 
109
        shelver.expect('Shelve 2 change(s)? [yNfq?]', 'n')
 
110
        shelver.run()
 
111
        self.assertFileEqual(LINES_ZY, 'tree/foo')
 
112
 
 
113
    def test_shelve_diff(self):
 
114
        tree = self.create_shelvable_tree()
 
115
        tree.lock_tree_write()
 
116
        self.addCleanup(tree.unlock)
 
117
        shelver = ExpectShelver(tree, tree.basis_tree())
 
118
        shelver.expect('Shelve? [yNfq?]', 'y')
 
119
        shelver.expect('Shelve? [yNfq?]', 'y')
 
120
        shelver.expect('Shelve 2 change(s)? [yNfq?]', 'y')
 
121
        shelver.run()
 
122
        self.assertFileEqual(LINES_AJ, 'tree/foo')
 
123
 
 
124
    def test_shelve_one_diff(self):
 
125
        tree = self.create_shelvable_tree()
 
126
        tree.lock_tree_write()
 
127
        self.addCleanup(tree.unlock)
 
128
        shelver = ExpectShelver(tree, tree.basis_tree())
 
129
        shelver.expect('Shelve? [yNfq?]', 'y')
 
130
        shelver.expect('Shelve? [yNfq?]', 'n')
 
131
        shelver.expect('Shelve 1 change(s)? [yNfq?]', 'y')
 
132
        shelver.run()
 
133
        self.assertFileEqual(LINES_AY, 'tree/foo')
 
134
 
 
135
    def test_shelve_binary_change(self):
 
136
        tree = self.create_shelvable_tree()
 
137
        self.build_tree_contents([('tree/foo', '\x00')])
 
138
        tree.lock_tree_write()
 
139
        self.addCleanup(tree.unlock)
 
140
        shelver = ExpectShelver(tree, tree.basis_tree())
 
141
        shelver.expect('Shelve binary changes? [yNfq?]', 'y')
 
142
        shelver.expect('Shelve 1 change(s)? [yNfq?]', 'y')
 
143
        shelver.run()
 
144
        self.assertFileEqual(LINES_AJ, 'tree/foo')
 
145
 
 
146
    def test_shelve_rename(self):
 
147
        tree = self.create_shelvable_tree()
 
148
        tree.rename_one('foo', 'bar')
 
149
        tree.lock_tree_write()
 
150
        self.addCleanup(tree.unlock)
 
151
        shelver = ExpectShelver(tree, tree.basis_tree())
 
152
        shelver.expect('Shelve renaming "foo" => "bar"? [yNfq?]', 'y')
 
153
        shelver.expect('Shelve? [yNfq?]', 'y')
 
154
        shelver.expect('Shelve? [yNfq?]', 'y')
 
155
        shelver.expect('Shelve 3 change(s)? [yNfq?]', 'y')
 
156
        shelver.run()
 
157
        self.assertFileEqual(LINES_AJ, 'tree/foo')
 
158
 
 
159
    def test_shelve_deletion(self):
 
160
        tree = self.create_shelvable_tree()
 
161
        os.unlink('tree/foo')
 
162
        tree.lock_tree_write()
 
163
        self.addCleanup(tree.unlock)
 
164
        shelver = ExpectShelver(tree, tree.basis_tree())
 
165
        shelver.expect('Shelve removing file "foo"? [yNfq?]', 'y')
 
166
        shelver.expect('Shelve 1 change(s)? [yNfq?]', 'y')
 
167
        shelver.run()
 
168
        self.assertFileEqual(LINES_AJ, 'tree/foo')
 
169
 
 
170
    def test_shelve_creation(self):
 
171
        tree = self.make_branch_and_tree('tree')
 
172
        tree.commit('add tree root')
 
173
        self.build_tree(['tree/foo'])
 
174
        tree.add('foo')
 
175
        tree.lock_tree_write()
 
176
        self.addCleanup(tree.unlock)
 
177
        shelver = ExpectShelver(tree, tree.basis_tree())
 
178
        shelver.expect('Shelve adding file "foo"? [yNfq?]', 'y')
 
179
        shelver.expect('Shelve 1 change(s)? [yNfq?]', 'y')
 
180
        shelver.run()
 
181
        self.failIfExists('tree/foo')
 
182
 
 
183
    def test_shelve_kind_change(self):
 
184
        tree = self.create_shelvable_tree()
 
185
        os.unlink('tree/foo')
 
186
        os.mkdir('tree/foo')
 
187
        tree.lock_tree_write()
 
188
        self.addCleanup(tree.unlock)
 
189
        shelver = ExpectShelver(tree, tree.basis_tree())
 
190
        shelver.expect('Shelve changing "foo" from file to directory? [yNfq?]',
 
191
                       'y')
 
192
        shelver.expect('Shelve 1 change(s)? [yNfq?]', 'y')
 
193
 
 
194
    def test_shelve_modify_target(self):
 
195
        self.requireFeature(tests.SymlinkFeature)
 
196
        tree = self.create_shelvable_tree()
 
197
        os.symlink('bar', 'tree/baz')
 
198
        tree.add('baz', 'baz-id')
 
199
        tree.commit("Add symlink")
 
200
        os.unlink('tree/baz')
 
201
        os.symlink('vax', 'tree/baz')
 
202
        tree.lock_tree_write()
 
203
        self.addCleanup(tree.unlock)
 
204
        shelver = ExpectShelver(tree, tree.basis_tree())
 
205
        shelver.expect('Shelve changing target of "baz" from "bar" to '
 
206
                '"vax"? [yNfq?]', 'y')
 
207
        shelver.expect('Shelve 1 change(s)? [yNfq?]', 'y')
 
208
        shelver.run()
 
209
        self.assertEqual('bar', os.readlink('tree/baz'))
 
210
 
 
211
    def test_shelve_finish(self):
 
212
        tree = self.create_shelvable_tree()
 
213
        tree.lock_tree_write()
 
214
        self.addCleanup(tree.unlock)
 
215
        shelver = ExpectShelver(tree, tree.basis_tree())
 
216
        shelver.expect('Shelve? [yNfq?]', 'f')
 
217
        shelver.expect('Shelve 2 change(s)? [yNfq?]', 'y')
 
218
        shelver.run()
 
219
        self.assertFileEqual(LINES_AJ, 'tree/foo')
 
220
 
 
221
    def test_shelve_quit(self):
 
222
        tree = self.create_shelvable_tree()
 
223
        tree.lock_tree_write()
 
224
        self.addCleanup(tree.unlock)
 
225
        shelver = ExpectShelver(tree, tree.basis_tree())
 
226
        shelver.expect('Shelve? [yNfq?]', 'q')
 
227
        self.assertRaises(errors.UserAbort, shelver.run)
 
228
        self.assertFileEqual(LINES_ZY, 'tree/foo')
 
229
 
 
230
    def test_shelve_all(self):
 
231
        tree = self.create_shelvable_tree()
 
232
        shelver = ExpectShelver.from_args(sys.stdout, all=True,
 
233
            directory='tree')
 
234
        try:
 
235
            shelver.run()
 
236
        finally:
 
237
            shelver.work_tree.unlock()
 
238
        self.assertFileEqual(LINES_AJ, 'tree/foo')
 
239
 
 
240
    def test_shelve_filename(self):
 
241
        tree = self.create_shelvable_tree()
 
242
        self.build_tree(['tree/bar'])
 
243
        tree.add('bar')
 
244
        tree.lock_tree_write()
 
245
        self.addCleanup(tree.unlock)
 
246
        shelver = ExpectShelver(tree, tree.basis_tree(), file_list=['bar'])
 
247
        shelver.expect('Shelve adding file "bar"? [yNfq?]', 'y')
 
248
        shelver.expect('Shelve 1 change(s)? [yNfq?]', 'y')
 
249
        shelver.run()
 
250
 
 
251
    def test_shelve_help(self):
 
252
        tree = self.create_shelvable_tree()
 
253
        tree.lock_tree_write()
 
254
        self.addCleanup(tree.unlock)
 
255
        shelver = ExpectShelver(tree, tree.basis_tree())
 
256
        shelver.expect('Shelve? [yNfq?]', '?')
 
257
        shelver.expect('Shelve? [(y)es, (N)o, (f)inish, or (q)uit]', 'f')
 
258
        shelver.expect('Shelve 2 change(s)? [yNfq?]', 'y')
 
259
        shelver.run()
 
260
 
 
261
    def test_shelve_distroy(self):
 
262
        tree = self.create_shelvable_tree()
 
263
        shelver = shelf_ui.Shelver.from_args(sys.stdout, all=True,
 
264
                                             directory='tree', destroy=True)
 
265
        try:
 
266
            shelver.run()
 
267
        finally:
 
268
            shelver.work_tree.unlock()
 
269
        self.assertIs(None, tree.get_shelf_manager().last_shelf())
 
270
        self.assertFileEqual(LINES_AJ, 'tree/foo')
 
271
 
 
272
    @staticmethod
 
273
    def shelve_all(tree, target_revision_id):
 
274
        tree.lock_write()
 
275
        try:
 
276
            target = tree.branch.repository.revision_tree(target_revision_id)
 
277
            shelver = shelf_ui.Shelver(tree, target, auto=True,
 
278
                                       auto_apply=True)
 
279
            shelver.run()
 
280
        finally:
 
281
            tree.unlock()
 
282
 
 
283
    def test_shelve_old_root_deleted(self):
 
284
        tree1 = self.make_branch_and_tree('tree1')
 
285
        tree1.commit('add root')
 
286
        tree2 = self.make_branch_and_tree('tree2')
 
287
        rev2 = tree2.commit('add root')
 
288
        tree1.merge_from_branch(tree2.branch,
 
289
                                from_revision=revision.NULL_REVISION)
 
290
        tree1.commit('Replaced root entry')
 
291
        # This is essentially assertNotRaises(InconsistentDelta)
 
292
        self.expectFailure('Cannot shelve replacing a root entry',
 
293
                           self.assertRaises, AssertionError,
 
294
                           self.assertRaises, errors.InconsistentDelta,
 
295
                           self.shelve_all, tree1, rev2)
 
296
 
 
297
    def test_shelve_split(self):
 
298
        outer_tree = self.make_branch_and_tree('outer')
 
299
        outer_tree.commit('Add root')
 
300
        inner_tree = self.make_branch_and_tree('outer/inner')
 
301
        rev2 = inner_tree.commit('Add root')
 
302
        outer_tree.subsume(inner_tree)
 
303
        # This is essentially assertNotRaises(ValueError).
 
304
        # The ValueError is 'None is not a valid file id'.
 
305
        self.expectFailure('Cannot shelve a join back to the inner tree.',
 
306
                           self.assertRaises, AssertionError,
 
307
                           self.assertRaises, ValueError, self.shelve_all,
 
308
                           outer_tree, rev2)
 
309
 
 
310
 
 
311
class TestApplyReporter(TestShelver):
 
312
 
 
313
    def test_shelve_not_diff(self):
 
314
        tree = self.create_shelvable_tree()
 
315
        tree.lock_tree_write()
 
316
        self.addCleanup(tree.unlock)
 
317
        shelver = ExpectShelver(tree, tree.basis_tree(),
 
318
                                reporter=shelf_ui.ApplyReporter())
 
319
        shelver.expect('Apply change? [yNfq?]', 'n')
 
320
        shelver.expect('Apply change? [yNfq?]', 'n')
 
321
        # No final shelving prompt because no changes were selected
 
322
        shelver.run()
 
323
        self.assertFileEqual(LINES_ZY, 'tree/foo')
 
324
 
 
325
    def test_shelve_diff_no(self):
 
326
        tree = self.create_shelvable_tree()
 
327
        tree.lock_tree_write()
 
328
        self.addCleanup(tree.unlock)
 
329
        shelver = ExpectShelver(tree, tree.basis_tree(),
 
330
                                reporter=shelf_ui.ApplyReporter())
 
331
        shelver.expect('Apply change? [yNfq?]', 'y')
 
332
        shelver.expect('Apply change? [yNfq?]', 'y')
 
333
        shelver.expect('Apply 2 change(s)? [yNfq?]', 'n')
 
334
        shelver.run()
 
335
        self.assertFileEqual(LINES_ZY, 'tree/foo')
 
336
 
 
337
    def test_shelve_diff(self):
 
338
        tree = self.create_shelvable_tree()
 
339
        tree.lock_tree_write()
 
340
        self.addCleanup(tree.unlock)
 
341
        shelver = ExpectShelver(tree, tree.basis_tree(),
 
342
                                reporter=shelf_ui.ApplyReporter())
 
343
        shelver.expect('Apply change? [yNfq?]', 'y')
 
344
        shelver.expect('Apply change? [yNfq?]', 'y')
 
345
        shelver.expect('Apply 2 change(s)? [yNfq?]', 'y')
 
346
        shelver.run()
 
347
        self.assertFileEqual(LINES_AJ, 'tree/foo')
 
348
 
 
349
    def test_shelve_binary_change(self):
 
350
        tree = self.create_shelvable_tree()
 
351
        self.build_tree_contents([('tree/foo', '\x00')])
 
352
        tree.lock_tree_write()
 
353
        self.addCleanup(tree.unlock)
 
354
        shelver = ExpectShelver(tree, tree.basis_tree(),
 
355
                                reporter=shelf_ui.ApplyReporter())
 
356
        shelver.expect('Apply binary changes? [yNfq?]', 'y')
 
357
        shelver.expect('Apply 1 change(s)? [yNfq?]', 'y')
 
358
        shelver.run()
 
359
        self.assertFileEqual(LINES_AJ, 'tree/foo')
 
360
 
 
361
    def test_shelve_rename(self):
 
362
        tree = self.create_shelvable_tree()
 
363
        tree.rename_one('foo', 'bar')
 
364
        tree.lock_tree_write()
 
365
        self.addCleanup(tree.unlock)
 
366
        shelver = ExpectShelver(tree, tree.basis_tree(),
 
367
                                reporter=shelf_ui.ApplyReporter())
 
368
        shelver.expect('Rename "bar" => "foo"? [yNfq?]', 'y')
 
369
        shelver.expect('Apply change? [yNfq?]', 'y')
 
370
        shelver.expect('Apply change? [yNfq?]', 'y')
 
371
        shelver.expect('Apply 3 change(s)? [yNfq?]', 'y')
 
372
        shelver.run()
 
373
        self.assertFileEqual(LINES_AJ, 'tree/foo')
 
374
 
 
375
    def test_shelve_deletion(self):
 
376
        tree = self.create_shelvable_tree()
 
377
        os.unlink('tree/foo')
 
378
        tree.lock_tree_write()
 
379
        self.addCleanup(tree.unlock)
 
380
        shelver = ExpectShelver(tree, tree.basis_tree(),
 
381
                                reporter=shelf_ui.ApplyReporter())
 
382
        shelver.expect('Add file "foo"? [yNfq?]', 'y')
 
383
        shelver.expect('Apply 1 change(s)? [yNfq?]', 'y')
 
384
        shelver.run()
 
385
        self.assertFileEqual(LINES_AJ, 'tree/foo')
 
386
 
 
387
    def test_shelve_creation(self):
 
388
        tree = self.make_branch_and_tree('tree')
 
389
        tree.commit('add tree root')
 
390
        self.build_tree(['tree/foo'])
 
391
        tree.add('foo')
 
392
        tree.lock_tree_write()
 
393
        self.addCleanup(tree.unlock)
 
394
        shelver = ExpectShelver(tree, tree.basis_tree(),
 
395
                                reporter=shelf_ui.ApplyReporter())
 
396
        shelver.expect('Delete file "foo"? [yNfq?]', 'y')
 
397
        shelver.expect('Apply 1 change(s)? [yNfq?]', 'y')
 
398
        shelver.run()
 
399
        self.failIfExists('tree/foo')
 
400
 
 
401
    def test_shelve_kind_change(self):
 
402
        tree = self.create_shelvable_tree()
 
403
        os.unlink('tree/foo')
 
404
        os.mkdir('tree/foo')
 
405
        tree.lock_tree_write()
 
406
        self.addCleanup(tree.unlock)
 
407
        shelver = ExpectShelver(tree, tree.basis_tree(),
 
408
                               reporter=shelf_ui.ApplyReporter())
 
409
        shelver.expect('Change "foo" from directory to a file? [yNfq?]', 'y')
 
410
        shelver.expect('Apply 1 change(s)? [yNfq?]', 'y')
 
411
 
 
412
    def test_shelve_modify_target(self):
 
413
        self.requireFeature(tests.SymlinkFeature)
 
414
        tree = self.create_shelvable_tree()
 
415
        os.symlink('bar', 'tree/baz')
 
416
        tree.add('baz', 'baz-id')
 
417
        tree.commit("Add symlink")
 
418
        os.unlink('tree/baz')
 
419
        os.symlink('vax', 'tree/baz')
 
420
        tree.lock_tree_write()
 
421
        self.addCleanup(tree.unlock)
 
422
        shelver = ExpectShelver(tree, tree.basis_tree(),
 
423
                                reporter=shelf_ui.ApplyReporter())
 
424
        shelver.expect('Change target of "baz" from "vax" to "bar"? [yNfq?]',
 
425
                       'y')
 
426
        shelver.expect('Apply 1 change(s)? [yNfq?]', 'y')
 
427
        shelver.run()
 
428
        self.assertEqual('bar', os.readlink('tree/baz'))
 
429
 
 
430
 
 
431
class TestUnshelver(tests.TestCaseWithTransport):
 
432
 
 
433
    def create_tree_with_shelf(self):
 
434
        tree = self.make_branch_and_tree('tree')
 
435
        tree.lock_write()
 
436
        try:
 
437
            self.build_tree_contents([('tree/foo', LINES_AJ)])
 
438
            tree.add('foo', 'foo-id')
 
439
            tree.commit('added foo')
 
440
            self.build_tree_contents([('tree/foo', LINES_ZY)])
 
441
            shelf_ui.Shelver(tree, tree.basis_tree(), auto_apply=True,
 
442
                             auto=True).run()
 
443
        finally:
 
444
            tree.unlock()
 
445
        return tree
 
446
 
 
447
    def test_unshelve(self):
 
448
        tree = self.create_tree_with_shelf()
 
449
        tree.lock_write()
 
450
        self.addCleanup(tree.unlock)
 
451
        manager = tree.get_shelf_manager()
 
452
        shelf_ui.Unshelver(tree, manager, 1, True, True, True).run()
 
453
        self.assertFileEqual(LINES_ZY, 'tree/foo')
 
454
 
 
455
    def test_unshelve_args(self):
 
456
        tree = self.create_tree_with_shelf()
 
457
        unshelver = shelf_ui.Unshelver.from_args(directory='tree')
 
458
        try:
 
459
            unshelver.run()
 
460
        finally:
 
461
            unshelver.tree.unlock()
 
462
        self.assertFileEqual(LINES_ZY, 'tree/foo')
 
463
        self.assertIs(None, tree.get_shelf_manager().last_shelf())
 
464
 
 
465
    def test_unshelve_args_dry_run(self):
 
466
        tree = self.create_tree_with_shelf()
 
467
        unshelver = shelf_ui.Unshelver.from_args(directory='tree',
 
468
            action='dry-run')
 
469
        try:
 
470
            unshelver.run()
 
471
        finally:
 
472
            unshelver.tree.unlock()
 
473
        self.assertFileEqual(LINES_AJ, 'tree/foo')
 
474
        self.assertEqual(1, tree.get_shelf_manager().last_shelf())
 
475
 
 
476
    def test_unshelve_args_delete_only(self):
 
477
        tree = self.make_branch_and_tree('tree')
 
478
        manager = tree.get_shelf_manager()
 
479
        shelf_file = manager.new_shelf()[1]
 
480
        try:
 
481
            shelf_file.write('garbage')
 
482
        finally:
 
483
            shelf_file.close()
 
484
        unshelver = shelf_ui.Unshelver.from_args(directory='tree',
 
485
                                                 action='delete-only')
 
486
        try:
 
487
            unshelver.run()
 
488
        finally:
 
489
            unshelver.tree.unlock()
 
490
        self.assertIs(None, manager.last_shelf())
 
491
 
 
492
    def test_unshelve_args_invalid_shelf_id(self):
 
493
        tree = self.make_branch_and_tree('tree')
 
494
        manager = tree.get_shelf_manager()
 
495
        shelf_file = manager.new_shelf()[1]
 
496
        try:
 
497
            shelf_file.write('garbage')
 
498
        finally:
 
499
            shelf_file.close()
 
500
        self.assertRaises(errors.InvalidShelfId,
 
501
            shelf_ui.Unshelver.from_args, directory='tree',
 
502
            action='delete-only', shelf_id='foo')