~bzr-pqm/bzr/bzr.dev

« back to all changes in this revision

Viewing changes to bzrlib/tests/test_shelf_ui.py

  • Committer: Patch Queue Manager
  • Date: 2011-10-13 14:09:11 UTC
  • mfrom: (6207.2.1 colo-fixes)
  • Revision ID: pqm@pqm.ubuntu.com-20111013140911-c2zv6a9pux62xnr9
(jelmer) Add support for cloning into a colocated branch. (Jelmer Vernooij)

Show diffs side-by-side

added added

removed removed

Lines of Context:
 
1
# Copyright (C) 2008, 2009, 2010 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
from textwrap import dedent
 
22
 
 
23
from bzrlib import (
 
24
    errors,
 
25
    shelf_ui,
 
26
    revision,
 
27
    tests,
 
28
)
 
29
from bzrlib.tests import script
 
30
from bzrlib.tests import (
 
31
    features,
 
32
    )
 
33
 
 
34
 
 
35
class ExpectShelver(shelf_ui.Shelver):
 
36
    """A variant of Shelver that intercepts console activity, for testing."""
 
37
 
 
38
    def __init__(self, work_tree, target_tree, diff_writer=None,
 
39
                 auto=False, auto_apply=False, file_list=None, message=None,
 
40
                 destroy=False, reporter=None):
 
41
        shelf_ui.Shelver.__init__(self, work_tree, target_tree, diff_writer,
 
42
                                  auto, auto_apply, file_list, message,
 
43
                                  destroy, reporter=reporter)
 
44
        self.expected = []
 
45
        self.diff_writer = StringIO()
 
46
 
 
47
    def expect(self, prompt, response):
 
48
        self.expected.append((prompt, response))
 
49
 
 
50
    def prompt(self, message):
 
51
        try:
 
52
            prompt, response = self.expected.pop(0)
 
53
        except IndexError:
 
54
            raise AssertionError('Unexpected prompt: %s' % message)
 
55
        if prompt != message:
 
56
            raise AssertionError('Wrong prompt: %s' % message)
 
57
        return response
 
58
 
 
59
 
 
60
LINES_AJ = 'a\nb\nc\nd\ne\nf\ng\nh\ni\nj\n'
 
61
 
 
62
 
 
63
LINES_ZY = 'z\nb\nc\nd\ne\nf\ng\nh\ni\ny\n'
 
64
 
 
65
 
 
66
LINES_AY = 'a\nb\nc\nd\ne\nf\ng\nh\ni\ny\n'
 
67
 
 
68
 
 
69
class ShelfTestCase(tests.TestCaseWithTransport):
 
70
 
 
71
    def create_shelvable_tree(self):
 
72
        tree = self.make_branch_and_tree('tree')
 
73
        self.build_tree_contents([('tree/foo', LINES_AJ)])
 
74
        tree.add('foo', 'foo-id')
 
75
        tree.commit('added foo')
 
76
        self.build_tree_contents([('tree/foo', LINES_ZY)])
 
77
        return tree
 
78
 
 
79
 
 
80
class TestShelver(ShelfTestCase):
 
81
 
 
82
    def test_unexpected_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
        self.addCleanup(shelver.finalize)
 
88
        e = self.assertRaises(AssertionError, shelver.run)
 
89
        self.assertEqual('Unexpected prompt: Shelve? [yNfq?]', str(e))
 
90
 
 
91
    def test_wrong_prompt_failure(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
        self.addCleanup(shelver.finalize)
 
97
        shelver.expect('foo', 'y')
 
98
        e = self.assertRaises(AssertionError, shelver.run)
 
99
        self.assertEqual('Wrong prompt: Shelve? [yNfq?]', str(e))
 
100
 
 
101
    def test_shelve_not_diff(self):
 
102
        tree = self.create_shelvable_tree()
 
103
        tree.lock_tree_write()
 
104
        self.addCleanup(tree.unlock)
 
105
        shelver = ExpectShelver(tree, tree.basis_tree())
 
106
        self.addCleanup(shelver.finalize)
 
107
        shelver.expect('Shelve? [yNfq?]', 'n')
 
108
        shelver.expect('Shelve? [yNfq?]', 'n')
 
109
        # No final shelving prompt because no changes were selected
 
110
        shelver.run()
 
111
        self.assertFileEqual(LINES_ZY, 'tree/foo')
 
112
 
 
113
    def test_shelve_diff_no(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
        self.addCleanup(shelver.finalize)
 
119
        shelver.expect('Shelve? [yNfq?]', 'y')
 
120
        shelver.expect('Shelve? [yNfq?]', 'y')
 
121
        shelver.expect('Shelve 2 change(s)? [yNfq?]', 'n')
 
122
        shelver.run()
 
123
        self.assertFileEqual(LINES_ZY, 'tree/foo')
 
124
 
 
125
    def test_shelve_diff(self):
 
126
        tree = self.create_shelvable_tree()
 
127
        tree.lock_tree_write()
 
128
        self.addCleanup(tree.unlock)
 
129
        shelver = ExpectShelver(tree, tree.basis_tree())
 
130
        self.addCleanup(shelver.finalize)
 
131
        shelver.expect('Shelve? [yNfq?]', 'y')
 
132
        shelver.expect('Shelve? [yNfq?]', 'y')
 
133
        shelver.expect('Shelve 2 change(s)? [yNfq?]', 'y')
 
134
        shelver.run()
 
135
        self.assertFileEqual(LINES_AJ, 'tree/foo')
 
136
 
 
137
    def test_shelve_one_diff(self):
 
138
        tree = self.create_shelvable_tree()
 
139
        tree.lock_tree_write()
 
140
        self.addCleanup(tree.unlock)
 
141
        shelver = ExpectShelver(tree, tree.basis_tree())
 
142
        self.addCleanup(shelver.finalize)
 
143
        shelver.expect('Shelve? [yNfq?]', 'y')
 
144
        shelver.expect('Shelve? [yNfq?]', 'n')
 
145
        shelver.expect('Shelve 1 change(s)? [yNfq?]', 'y')
 
146
        shelver.run()
 
147
        self.assertFileEqual(LINES_AY, 'tree/foo')
 
148
 
 
149
    def test_shelve_binary_change(self):
 
150
        tree = self.create_shelvable_tree()
 
151
        self.build_tree_contents([('tree/foo', '\x00')])
 
152
        tree.lock_tree_write()
 
153
        self.addCleanup(tree.unlock)
 
154
        shelver = ExpectShelver(tree, tree.basis_tree())
 
155
        self.addCleanup(shelver.finalize)
 
156
        shelver.expect('Shelve binary changes? [yNfq?]', 'y')
 
157
        shelver.expect('Shelve 1 change(s)? [yNfq?]', 'y')
 
158
        shelver.run()
 
159
        self.assertFileEqual(LINES_AJ, 'tree/foo')
 
160
 
 
161
    def test_shelve_rename(self):
 
162
        tree = self.create_shelvable_tree()
 
163
        tree.rename_one('foo', 'bar')
 
164
        tree.lock_tree_write()
 
165
        self.addCleanup(tree.unlock)
 
166
        shelver = ExpectShelver(tree, tree.basis_tree())
 
167
        self.addCleanup(shelver.finalize)
 
168
        shelver.expect('Shelve renaming "foo" => "bar"? [yNfq?]', 'y')
 
169
        shelver.expect('Shelve? [yNfq?]', 'y')
 
170
        shelver.expect('Shelve? [yNfq?]', 'y')
 
171
        shelver.expect('Shelve 3 change(s)? [yNfq?]', 'y')
 
172
        shelver.run()
 
173
        self.assertFileEqual(LINES_AJ, 'tree/foo')
 
174
 
 
175
    def test_shelve_deletion(self):
 
176
        tree = self.create_shelvable_tree()
 
177
        os.unlink('tree/foo')
 
178
        tree.lock_tree_write()
 
179
        self.addCleanup(tree.unlock)
 
180
        shelver = ExpectShelver(tree, tree.basis_tree())
 
181
        self.addCleanup(shelver.finalize)
 
182
        shelver.expect('Shelve removing file "foo"? [yNfq?]', 'y')
 
183
        shelver.expect('Shelve 1 change(s)? [yNfq?]', 'y')
 
184
        shelver.run()
 
185
        self.assertFileEqual(LINES_AJ, 'tree/foo')
 
186
 
 
187
    def test_shelve_creation(self):
 
188
        tree = self.make_branch_and_tree('tree')
 
189
        tree.commit('add tree root')
 
190
        self.build_tree(['tree/foo'])
 
191
        tree.add('foo')
 
192
        tree.lock_tree_write()
 
193
        self.addCleanup(tree.unlock)
 
194
        shelver = ExpectShelver(tree, tree.basis_tree())
 
195
        self.addCleanup(shelver.finalize)
 
196
        shelver.expect('Shelve adding file "foo"? [yNfq?]', 'y')
 
197
        shelver.expect('Shelve 1 change(s)? [yNfq?]', 'y')
 
198
        shelver.run()
 
199
        self.assertPathDoesNotExist('tree/foo')
 
200
 
 
201
    def test_shelve_kind_change(self):
 
202
        tree = self.create_shelvable_tree()
 
203
        os.unlink('tree/foo')
 
204
        os.mkdir('tree/foo')
 
205
        tree.lock_tree_write()
 
206
        self.addCleanup(tree.unlock)
 
207
        shelver = ExpectShelver(tree, tree.basis_tree())
 
208
        self.addCleanup(shelver.finalize)
 
209
        shelver.expect('Shelve changing "foo" from file to directory? [yNfq?]',
 
210
                       'y')
 
211
        shelver.expect('Shelve 1 change(s)? [yNfq?]', 'y')
 
212
 
 
213
    def test_shelve_modify_target(self):
 
214
        self.requireFeature(features.SymlinkFeature)
 
215
        tree = self.create_shelvable_tree()
 
216
        os.symlink('bar', 'tree/baz')
 
217
        tree.add('baz', 'baz-id')
 
218
        tree.commit("Add symlink")
 
219
        os.unlink('tree/baz')
 
220
        os.symlink('vax', 'tree/baz')
 
221
        tree.lock_tree_write()
 
222
        self.addCleanup(tree.unlock)
 
223
        shelver = ExpectShelver(tree, tree.basis_tree())
 
224
        self.addCleanup(shelver.finalize)
 
225
        shelver.expect('Shelve changing target of "baz" from "bar" to '
 
226
                '"vax"? [yNfq?]', 'y')
 
227
        shelver.expect('Shelve 1 change(s)? [yNfq?]', 'y')
 
228
        shelver.run()
 
229
        self.assertEqual('bar', os.readlink('tree/baz'))
 
230
 
 
231
    def test_shelve_finish(self):
 
232
        tree = self.create_shelvable_tree()
 
233
        tree.lock_tree_write()
 
234
        self.addCleanup(tree.unlock)
 
235
        shelver = ExpectShelver(tree, tree.basis_tree())
 
236
        self.addCleanup(shelver.finalize)
 
237
        shelver.expect('Shelve? [yNfq?]', 'f')
 
238
        shelver.expect('Shelve 2 change(s)? [yNfq?]', 'y')
 
239
        shelver.run()
 
240
        self.assertFileEqual(LINES_AJ, 'tree/foo')
 
241
 
 
242
    def test_shelve_quit(self):
 
243
        tree = self.create_shelvable_tree()
 
244
        tree.lock_tree_write()
 
245
        self.addCleanup(tree.unlock)
 
246
        shelver = ExpectShelver(tree, tree.basis_tree())
 
247
        self.addCleanup(shelver.finalize)
 
248
        shelver.expect('Shelve? [yNfq?]', 'q')
 
249
        self.assertRaises(errors.UserAbort, shelver.run)
 
250
        self.assertFileEqual(LINES_ZY, 'tree/foo')
 
251
 
 
252
    def test_shelve_all(self):
 
253
        tree = self.create_shelvable_tree()
 
254
        shelver = ExpectShelver.from_args(sys.stdout, all=True,
 
255
            directory='tree')
 
256
        try:
 
257
            shelver.run()
 
258
        finally:
 
259
            shelver.finalize()
 
260
        self.assertFileEqual(LINES_AJ, 'tree/foo')
 
261
 
 
262
    def test_shelve_filename(self):
 
263
        tree = self.create_shelvable_tree()
 
264
        self.build_tree(['tree/bar'])
 
265
        tree.add('bar')
 
266
        tree.lock_tree_write()
 
267
        self.addCleanup(tree.unlock)
 
268
        shelver = ExpectShelver(tree, tree.basis_tree(), file_list=['bar'])
 
269
        self.addCleanup(shelver.finalize)
 
270
        shelver.expect('Shelve adding file "bar"? [yNfq?]', 'y')
 
271
        shelver.expect('Shelve 1 change(s)? [yNfq?]', 'y')
 
272
        shelver.run()
 
273
 
 
274
    def test_shelve_help(self):
 
275
        tree = self.create_shelvable_tree()
 
276
        tree.lock_tree_write()
 
277
        self.addCleanup(tree.unlock)
 
278
        shelver = ExpectShelver(tree, tree.basis_tree())
 
279
        self.addCleanup(shelver.finalize)
 
280
        shelver.expect('Shelve? [yNfq?]', '?')
 
281
        shelver.expect('Shelve? [(y)es, (N)o, (f)inish, or (q)uit]', 'f')
 
282
        shelver.expect('Shelve 2 change(s)? [yNfq?]', 'y')
 
283
        shelver.run()
 
284
 
 
285
    def test_shelve_destroy(self):
 
286
        tree = self.create_shelvable_tree()
 
287
        shelver = shelf_ui.Shelver.from_args(sys.stdout, all=True,
 
288
                                             directory='tree', destroy=True)
 
289
        self.addCleanup(shelver.finalize)
 
290
        shelver.run()
 
291
        self.assertIs(None, tree.get_shelf_manager().last_shelf())
 
292
        self.assertFileEqual(LINES_AJ, 'tree/foo')
 
293
 
 
294
    @staticmethod
 
295
    def shelve_all(tree, target_revision_id):
 
296
        tree.lock_write()
 
297
        try:
 
298
            target = tree.branch.repository.revision_tree(target_revision_id)
 
299
            shelver = shelf_ui.Shelver(tree, target, auto=True,
 
300
                                       auto_apply=True)
 
301
            try:
 
302
                shelver.run()
 
303
            finally:
 
304
                shelver.finalize()
 
305
        finally:
 
306
            tree.unlock()
 
307
 
 
308
    def test_shelve_old_root_preserved(self):
 
309
        tree1 = self.make_branch_and_tree('tree1')
 
310
        tree1.commit('add root')
 
311
        tree1_root_id = tree1.get_root_id()
 
312
        tree2 = self.make_branch_and_tree('tree2')
 
313
        rev2 = tree2.commit('add root')
 
314
        self.assertNotEquals(tree1_root_id, tree2.get_root_id())
 
315
        tree1.merge_from_branch(tree2.branch,
 
316
                                from_revision=revision.NULL_REVISION)
 
317
        tree1.commit('merging in tree2')
 
318
        self.assertEquals(tree1_root_id, tree1.get_root_id())
 
319
        # This is essentially assertNotRaises(InconsistentDelta)
 
320
        # With testtools 0.9.9, it can be rewritten as:
 
321
        # with ExpectedException(AssertionError,
 
322
        #                        'InconsistentDelta not raised'):
 
323
        #     with ExpectedException(errors.InconsistentDelta, ''):
 
324
        #         self.shelve_all(tree1, rev2)
 
325
        e = self.assertRaises(AssertionError, self.assertRaises,
 
326
                              errors.InconsistentDelta, self.shelve_all, tree1,
 
327
                              rev2)
 
328
        self.assertContainsRe('InconsistentDelta not raised', str(e))
 
329
 
 
330
    def test_shelve_split(self):
 
331
        outer_tree = self.make_branch_and_tree('outer')
 
332
        outer_tree.commit('Add root')
 
333
        inner_tree = self.make_branch_and_tree('outer/inner')
 
334
        rev2 = inner_tree.commit('Add root')
 
335
        outer_tree.subsume(inner_tree)
 
336
        # This is essentially assertNotRaises(ValueError).
 
337
        # The ValueError is 'None is not a valid file id'.
 
338
        self.expectFailure('Cannot shelve a join back to the inner tree.',
 
339
                           self.assertRaises, AssertionError,
 
340
                           self.assertRaises, ValueError, self.shelve_all,
 
341
                           outer_tree, rev2)
 
342
 
 
343
 
 
344
class TestApplyReporter(ShelfTestCase):
 
345
 
 
346
    def test_shelve_not_diff(self):
 
347
        tree = self.create_shelvable_tree()
 
348
        tree.lock_tree_write()
 
349
        self.addCleanup(tree.unlock)
 
350
        shelver = ExpectShelver(tree, tree.basis_tree(),
 
351
                                reporter=shelf_ui.ApplyReporter())
 
352
        self.addCleanup(shelver.finalize)
 
353
        shelver.expect('Apply change? [yNfq?]', 'n')
 
354
        shelver.expect('Apply change? [yNfq?]', 'n')
 
355
        # No final shelving prompt because no changes were selected
 
356
        shelver.run()
 
357
        self.assertFileEqual(LINES_ZY, 'tree/foo')
 
358
 
 
359
    def test_shelve_diff_no(self):
 
360
        tree = self.create_shelvable_tree()
 
361
        tree.lock_tree_write()
 
362
        self.addCleanup(tree.unlock)
 
363
        shelver = ExpectShelver(tree, tree.basis_tree(),
 
364
                                reporter=shelf_ui.ApplyReporter())
 
365
        self.addCleanup(shelver.finalize)
 
366
        shelver.expect('Apply change? [yNfq?]', 'y')
 
367
        shelver.expect('Apply change? [yNfq?]', 'y')
 
368
        shelver.expect('Apply 2 change(s)? [yNfq?]', 'n')
 
369
        shelver.run()
 
370
        self.assertFileEqual(LINES_ZY, 'tree/foo')
 
371
 
 
372
    def test_shelve_diff(self):
 
373
        tree = self.create_shelvable_tree()
 
374
        tree.lock_tree_write()
 
375
        self.addCleanup(tree.unlock)
 
376
        shelver = ExpectShelver(tree, tree.basis_tree(),
 
377
                                reporter=shelf_ui.ApplyReporter())
 
378
        self.addCleanup(shelver.finalize)
 
379
        shelver.expect('Apply change? [yNfq?]', 'y')
 
380
        shelver.expect('Apply change? [yNfq?]', 'y')
 
381
        shelver.expect('Apply 2 change(s)? [yNfq?]', 'y')
 
382
        shelver.run()
 
383
        self.assertFileEqual(LINES_AJ, 'tree/foo')
 
384
 
 
385
    def test_shelve_binary_change(self):
 
386
        tree = self.create_shelvable_tree()
 
387
        self.build_tree_contents([('tree/foo', '\x00')])
 
388
        tree.lock_tree_write()
 
389
        self.addCleanup(tree.unlock)
 
390
        shelver = ExpectShelver(tree, tree.basis_tree(),
 
391
                                reporter=shelf_ui.ApplyReporter())
 
392
        self.addCleanup(shelver.finalize)
 
393
        shelver.expect('Apply binary changes? [yNfq?]', 'y')
 
394
        shelver.expect('Apply 1 change(s)? [yNfq?]', 'y')
 
395
        shelver.run()
 
396
        self.assertFileEqual(LINES_AJ, 'tree/foo')
 
397
 
 
398
    def test_shelve_rename(self):
 
399
        tree = self.create_shelvable_tree()
 
400
        tree.rename_one('foo', 'bar')
 
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('Rename "bar" => "foo"? [yNfq?]', 'y')
 
407
        shelver.expect('Apply change? [yNfq?]', 'y')
 
408
        shelver.expect('Apply change? [yNfq?]', 'y')
 
409
        shelver.expect('Apply 3 change(s)? [yNfq?]', 'y')
 
410
        shelver.run()
 
411
        self.assertFileEqual(LINES_AJ, 'tree/foo')
 
412
 
 
413
    def test_shelve_deletion(self):
 
414
        tree = self.create_shelvable_tree()
 
415
        os.unlink('tree/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('Add file "foo"? [yNfq?]', 'y')
 
422
        shelver.expect('Apply 1 change(s)? [yNfq?]', 'y')
 
423
        shelver.run()
 
424
        self.assertFileEqual(LINES_AJ, 'tree/foo')
 
425
 
 
426
    def test_shelve_creation(self):
 
427
        tree = self.make_branch_and_tree('tree')
 
428
        tree.commit('add tree root')
 
429
        self.build_tree(['tree/foo'])
 
430
        tree.add('foo')
 
431
        tree.lock_tree_write()
 
432
        self.addCleanup(tree.unlock)
 
433
        shelver = ExpectShelver(tree, tree.basis_tree(),
 
434
                                reporter=shelf_ui.ApplyReporter())
 
435
        self.addCleanup(shelver.finalize)
 
436
        shelver.expect('Delete file "foo"? [yNfq?]', 'y')
 
437
        shelver.expect('Apply 1 change(s)? [yNfq?]', 'y')
 
438
        shelver.run()
 
439
        self.assertPathDoesNotExist('tree/foo')
 
440
 
 
441
    def test_shelve_kind_change(self):
 
442
        tree = self.create_shelvable_tree()
 
443
        os.unlink('tree/foo')
 
444
        os.mkdir('tree/foo')
 
445
        tree.lock_tree_write()
 
446
        self.addCleanup(tree.unlock)
 
447
        shelver = ExpectShelver(tree, tree.basis_tree(),
 
448
                               reporter=shelf_ui.ApplyReporter())
 
449
        self.addCleanup(shelver.finalize)
 
450
        shelver.expect('Change "foo" from directory to a file? [yNfq?]', 'y')
 
451
        shelver.expect('Apply 1 change(s)? [yNfq?]', 'y')
 
452
 
 
453
    def test_shelve_modify_target(self):
 
454
        self.requireFeature(features.SymlinkFeature)
 
455
        tree = self.create_shelvable_tree()
 
456
        os.symlink('bar', 'tree/baz')
 
457
        tree.add('baz', 'baz-id')
 
458
        tree.commit("Add symlink")
 
459
        os.unlink('tree/baz')
 
460
        os.symlink('vax', 'tree/baz')
 
461
        tree.lock_tree_write()
 
462
        self.addCleanup(tree.unlock)
 
463
        shelver = ExpectShelver(tree, tree.basis_tree(),
 
464
                                reporter=shelf_ui.ApplyReporter())
 
465
        self.addCleanup(shelver.finalize)
 
466
        shelver.expect('Change target of "baz" from "vax" to "bar"? [yNfq?]',
 
467
                       'y')
 
468
        shelver.expect('Apply 1 change(s)? [yNfq?]', 'y')
 
469
        shelver.run()
 
470
        self.assertEqual('bar', os.readlink('tree/baz'))
 
471
 
 
472
 
 
473
class TestUnshelver(tests.TestCaseWithTransport):
 
474
 
 
475
    def create_tree_with_shelf(self):
 
476
        tree = self.make_branch_and_tree('tree')
 
477
        tree.lock_write()
 
478
        try:
 
479
            self.build_tree_contents([('tree/foo', LINES_AJ)])
 
480
            tree.add('foo', 'foo-id')
 
481
            tree.commit('added foo')
 
482
            self.build_tree_contents([('tree/foo', LINES_ZY)])
 
483
            shelver = shelf_ui.Shelver(tree, tree.basis_tree(),
 
484
                                       auto_apply=True, auto=True)
 
485
            try:
 
486
                shelver.run()
 
487
            finally:
 
488
                shelver.finalize()
 
489
        finally:
 
490
            tree.unlock()
 
491
        return tree
 
492
 
 
493
    def test_unshelve(self):
 
494
        tree = self.create_tree_with_shelf()
 
495
        tree.lock_write()
 
496
        self.addCleanup(tree.unlock)
 
497
        manager = tree.get_shelf_manager()
 
498
        shelf_ui.Unshelver(tree, manager, 1, True, True, True).run()
 
499
        self.assertFileEqual(LINES_ZY, 'tree/foo')
 
500
 
 
501
    def test_unshelve_args(self):
 
502
        tree = self.create_tree_with_shelf()
 
503
        unshelver = shelf_ui.Unshelver.from_args(directory='tree')
 
504
        try:
 
505
            unshelver.run()
 
506
        finally:
 
507
            unshelver.tree.unlock()
 
508
        self.assertFileEqual(LINES_ZY, 'tree/foo')
 
509
        self.assertIs(None, tree.get_shelf_manager().last_shelf())
 
510
 
 
511
    def test_unshelve_args_dry_run(self):
 
512
        tree = self.create_tree_with_shelf()
 
513
        unshelver = shelf_ui.Unshelver.from_args(directory='tree',
 
514
            action='dry-run')
 
515
        try:
 
516
            unshelver.run()
 
517
        finally:
 
518
            unshelver.tree.unlock()
 
519
        self.assertFileEqual(LINES_AJ, 'tree/foo')
 
520
        self.assertEqual(1, tree.get_shelf_manager().last_shelf())
 
521
 
 
522
    def test_unshelve_args_preview(self):
 
523
        tree = self.create_tree_with_shelf()
 
524
        write_diff_to = StringIO()
 
525
        unshelver = shelf_ui.Unshelver.from_args(
 
526
            directory='tree', action='preview', write_diff_to=write_diff_to)
 
527
        try:
 
528
            unshelver.run()
 
529
        finally:
 
530
            unshelver.tree.unlock()
 
531
        # The changes were not unshelved.
 
532
        self.assertFileEqual(LINES_AJ, 'tree/foo')
 
533
        self.assertEqual(1, tree.get_shelf_manager().last_shelf())
 
534
 
 
535
        # But the diff was written to write_diff_to.
 
536
        diff = write_diff_to.getvalue()
 
537
        expected = dedent("""\
 
538
            @@ -1,4 +1,4 @@
 
539
            -a
 
540
            +z
 
541
             b
 
542
             c
 
543
             d
 
544
            @@ -7,4 +7,4 @@
 
545
             g
 
546
             h
 
547
             i
 
548
            -j
 
549
            +y
 
550
 
 
551
            """)
 
552
        self.assertEqualDiff(expected, diff[-len(expected):])
 
553
 
 
554
    def test_unshelve_args_delete_only(self):
 
555
        tree = self.make_branch_and_tree('tree')
 
556
        manager = tree.get_shelf_manager()
 
557
        shelf_file = manager.new_shelf()[1]
 
558
        try:
 
559
            shelf_file.write('garbage')
 
560
        finally:
 
561
            shelf_file.close()
 
562
        unshelver = shelf_ui.Unshelver.from_args(directory='tree',
 
563
                                                 action='delete-only')
 
564
        try:
 
565
            unshelver.run()
 
566
        finally:
 
567
            unshelver.tree.unlock()
 
568
        self.assertIs(None, manager.last_shelf())
 
569
 
 
570
    def test_unshelve_args_invalid_shelf_id(self):
 
571
        tree = self.make_branch_and_tree('tree')
 
572
        manager = tree.get_shelf_manager()
 
573
        shelf_file = manager.new_shelf()[1]
 
574
        try:
 
575
            shelf_file.write('garbage')
 
576
        finally:
 
577
            shelf_file.close()
 
578
        self.assertRaises(errors.InvalidShelfId,
 
579
            shelf_ui.Unshelver.from_args, directory='tree',
 
580
            action='delete-only', shelf_id='foo')
 
581
 
 
582
 
 
583
class TestUnshelveScripts(TestUnshelver, 
 
584
                          script.TestCaseWithTransportAndScript): 
 
585
 
 
586
    def test_unshelve_messages_keep(self):
 
587
        self.create_tree_with_shelf()
 
588
        self.run_script("""
 
589
$ cd tree
 
590
$ bzr unshelve --keep
 
591
2>Using changes with id "1".
 
592
2> M  foo
 
593
2>All changes applied successfully.
 
594
""")
 
595
 
 
596
    def test_unshelve_messages_delete(self):
 
597
        self.create_tree_with_shelf()
 
598
        self.run_script("""
 
599
$ cd tree
 
600
$ bzr unshelve --delete-only
 
601
2>Deleted changes with id "1".
 
602
""")
 
603
 
 
604
    def test_unshelve_messages_apply(self):
 
605
        self.create_tree_with_shelf()
 
606
        self.run_script("""
 
607
$ cd tree
 
608
$ bzr unshelve --apply
 
609
2>Using changes with id "1".
 
610
2> M  foo
 
611
2>All changes applied successfully.
 
612
2>Deleted changes with id "1".
 
613
""")
 
614
 
 
615
    def test_unshelve_messages_dry_run(self):
 
616
        self.create_tree_with_shelf()
 
617
        self.run_script("""
 
618
$ cd tree
 
619
$ bzr unshelve --dry-run
 
620
2>Using changes with id "1".
 
621
2> M  foo
 
622
""")