~bzr-pqm/bzr/bzr.dev

« back to all changes in this revision

Viewing changes to bzrlib/tests/test_shelf_ui.py

  • Committer: Canonical.com Patch Queue Manager
  • Date: 2011-07-11 02:46:35 UTC
  • mfrom: (6017.1.2 test-isolation-speed)
  • Revision ID: pqm@pqm.ubuntu.com-20110711024635-f39c8kz23s347m1t
(spiv) Speed up TestCaseWithMemoryTransport._check_safety_net by reading the
 dirstate file directly rather than using WorkingTree.open(). (Andrew
 Bennetts)

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