~bzr-pqm/bzr/bzr.dev

« back to all changes in this revision

Viewing changes to bzrlib/tests/test_shelf_ui.py

  • Committer: Martin
  • Date: 2010-05-25 17:27:52 UTC
  • mfrom: (5254 +trunk)
  • mto: This revision was merged to the branch mainline in revision 5257.
  • Revision ID: gzlist@googlemail.com-20100525172752-amm089xcikv968sw
Merge bzr.dev to unite with similar changes already made

Show diffs side-by-side

added added

removed removed

Lines of Context:
1
 
# Copyright (C) 2008 Canonical Ltd
 
1
# Copyright (C) 2008, 2009, 2010 Canonical Ltd
2
2
#
3
3
# This program is free software; you can redistribute it and/or modify
4
4
# it under the terms of the GNU General Public License as published by
18
18
from cStringIO import StringIO
19
19
import os
20
20
import sys
 
21
from textwrap import dedent
21
22
 
22
23
from bzrlib import (
23
24
    errors,
25
26
    revision,
26
27
    tests,
27
28
)
 
29
from bzrlib.tests import script
28
30
 
29
31
 
30
32
class ExpectShelver(shelf_ui.Shelver):
76
78
        tree.lock_tree_write()
77
79
        self.addCleanup(tree.unlock)
78
80
        shelver = ExpectShelver(tree, tree.basis_tree())
 
81
        self.addCleanup(shelver.finalize)
79
82
        e = self.assertRaises(AssertionError, shelver.run)
80
83
        self.assertEqual('Unexpected prompt: Shelve? [yNfq?]', str(e))
81
84
 
84
87
        tree.lock_tree_write()
85
88
        self.addCleanup(tree.unlock)
86
89
        shelver = ExpectShelver(tree, tree.basis_tree())
 
90
        self.addCleanup(shelver.finalize)
87
91
        shelver.expect('foo', 'y')
88
92
        e = self.assertRaises(AssertionError, shelver.run)
89
93
        self.assertEqual('Wrong prompt: Shelve? [yNfq?]', str(e))
93
97
        tree.lock_tree_write()
94
98
        self.addCleanup(tree.unlock)
95
99
        shelver = ExpectShelver(tree, tree.basis_tree())
 
100
        self.addCleanup(shelver.finalize)
96
101
        shelver.expect('Shelve? [yNfq?]', 'n')
97
102
        shelver.expect('Shelve? [yNfq?]', 'n')
98
103
        # No final shelving prompt because no changes were selected
104
109
        tree.lock_tree_write()
105
110
        self.addCleanup(tree.unlock)
106
111
        shelver = ExpectShelver(tree, tree.basis_tree())
 
112
        self.addCleanup(shelver.finalize)
107
113
        shelver.expect('Shelve? [yNfq?]', 'y')
108
114
        shelver.expect('Shelve? [yNfq?]', 'y')
109
115
        shelver.expect('Shelve 2 change(s)? [yNfq?]', 'n')
115
121
        tree.lock_tree_write()
116
122
        self.addCleanup(tree.unlock)
117
123
        shelver = ExpectShelver(tree, tree.basis_tree())
 
124
        self.addCleanup(shelver.finalize)
118
125
        shelver.expect('Shelve? [yNfq?]', 'y')
119
126
        shelver.expect('Shelve? [yNfq?]', 'y')
120
127
        shelver.expect('Shelve 2 change(s)? [yNfq?]', 'y')
126
133
        tree.lock_tree_write()
127
134
        self.addCleanup(tree.unlock)
128
135
        shelver = ExpectShelver(tree, tree.basis_tree())
 
136
        self.addCleanup(shelver.finalize)
129
137
        shelver.expect('Shelve? [yNfq?]', 'y')
130
138
        shelver.expect('Shelve? [yNfq?]', 'n')
131
139
        shelver.expect('Shelve 1 change(s)? [yNfq?]', 'y')
138
146
        tree.lock_tree_write()
139
147
        self.addCleanup(tree.unlock)
140
148
        shelver = ExpectShelver(tree, tree.basis_tree())
 
149
        self.addCleanup(shelver.finalize)
141
150
        shelver.expect('Shelve binary changes? [yNfq?]', 'y')
142
151
        shelver.expect('Shelve 1 change(s)? [yNfq?]', 'y')
143
152
        shelver.run()
149
158
        tree.lock_tree_write()
150
159
        self.addCleanup(tree.unlock)
151
160
        shelver = ExpectShelver(tree, tree.basis_tree())
 
161
        self.addCleanup(shelver.finalize)
152
162
        shelver.expect('Shelve renaming "foo" => "bar"? [yNfq?]', 'y')
153
163
        shelver.expect('Shelve? [yNfq?]', 'y')
154
164
        shelver.expect('Shelve? [yNfq?]', 'y')
162
172
        tree.lock_tree_write()
163
173
        self.addCleanup(tree.unlock)
164
174
        shelver = ExpectShelver(tree, tree.basis_tree())
 
175
        self.addCleanup(shelver.finalize)
165
176
        shelver.expect('Shelve removing file "foo"? [yNfq?]', 'y')
166
177
        shelver.expect('Shelve 1 change(s)? [yNfq?]', 'y')
167
178
        shelver.run()
175
186
        tree.lock_tree_write()
176
187
        self.addCleanup(tree.unlock)
177
188
        shelver = ExpectShelver(tree, tree.basis_tree())
 
189
        self.addCleanup(shelver.finalize)
178
190
        shelver.expect('Shelve adding file "foo"? [yNfq?]', 'y')
179
191
        shelver.expect('Shelve 1 change(s)? [yNfq?]', 'y')
180
192
        shelver.run()
187
199
        tree.lock_tree_write()
188
200
        self.addCleanup(tree.unlock)
189
201
        shelver = ExpectShelver(tree, tree.basis_tree())
 
202
        self.addCleanup(shelver.finalize)
190
203
        shelver.expect('Shelve changing "foo" from file to directory? [yNfq?]',
191
204
                       'y')
192
205
        shelver.expect('Shelve 1 change(s)? [yNfq?]', 'y')
202
215
        tree.lock_tree_write()
203
216
        self.addCleanup(tree.unlock)
204
217
        shelver = ExpectShelver(tree, tree.basis_tree())
 
218
        self.addCleanup(shelver.finalize)
205
219
        shelver.expect('Shelve changing target of "baz" from "bar" to '
206
220
                '"vax"? [yNfq?]', 'y')
207
221
        shelver.expect('Shelve 1 change(s)? [yNfq?]', 'y')
213
227
        tree.lock_tree_write()
214
228
        self.addCleanup(tree.unlock)
215
229
        shelver = ExpectShelver(tree, tree.basis_tree())
 
230
        self.addCleanup(shelver.finalize)
216
231
        shelver.expect('Shelve? [yNfq?]', 'f')
217
232
        shelver.expect('Shelve 2 change(s)? [yNfq?]', 'y')
218
233
        shelver.run()
223
238
        tree.lock_tree_write()
224
239
        self.addCleanup(tree.unlock)
225
240
        shelver = ExpectShelver(tree, tree.basis_tree())
 
241
        self.addCleanup(shelver.finalize)
226
242
        shelver.expect('Shelve? [yNfq?]', 'q')
227
243
        self.assertRaises(errors.UserAbort, shelver.run)
228
244
        self.assertFileEqual(LINES_ZY, 'tree/foo')
234
250
        try:
235
251
            shelver.run()
236
252
        finally:
237
 
            shelver.work_tree.unlock()
 
253
            shelver.finalize()
238
254
        self.assertFileEqual(LINES_AJ, 'tree/foo')
239
255
 
240
256
    def test_shelve_filename(self):
244
260
        tree.lock_tree_write()
245
261
        self.addCleanup(tree.unlock)
246
262
        shelver = ExpectShelver(tree, tree.basis_tree(), file_list=['bar'])
 
263
        self.addCleanup(shelver.finalize)
247
264
        shelver.expect('Shelve adding file "bar"? [yNfq?]', 'y')
248
265
        shelver.expect('Shelve 1 change(s)? [yNfq?]', 'y')
249
266
        shelver.run()
253
270
        tree.lock_tree_write()
254
271
        self.addCleanup(tree.unlock)
255
272
        shelver = ExpectShelver(tree, tree.basis_tree())
 
273
        self.addCleanup(shelver.finalize)
256
274
        shelver.expect('Shelve? [yNfq?]', '?')
257
275
        shelver.expect('Shelve? [(y)es, (N)o, (f)inish, or (q)uit]', 'f')
258
276
        shelver.expect('Shelve 2 change(s)? [yNfq?]', 'y')
259
277
        shelver.run()
260
278
 
261
 
    def test_shelve_distroy(self):
 
279
    def test_shelve_destroy(self):
262
280
        tree = self.create_shelvable_tree()
263
281
        shelver = shelf_ui.Shelver.from_args(sys.stdout, all=True,
264
282
                                             directory='tree', destroy=True)
265
 
        try:
266
 
            shelver.run()
267
 
        finally:
268
 
            shelver.work_tree.unlock()
 
283
        self.addCleanup(shelver.finalize)
 
284
        shelver.run()
269
285
        self.assertIs(None, tree.get_shelf_manager().last_shelf())
270
286
        self.assertFileEqual(LINES_AJ, 'tree/foo')
271
287
 
276
292
            target = tree.branch.repository.revision_tree(target_revision_id)
277
293
            shelver = shelf_ui.Shelver(tree, target, auto=True,
278
294
                                       auto_apply=True)
279
 
            shelver.run()
 
295
            try:
 
296
                shelver.run()
 
297
            finally:
 
298
                shelver.finalize()
280
299
        finally:
281
300
            tree.unlock()
282
301
 
316
335
        self.addCleanup(tree.unlock)
317
336
        shelver = ExpectShelver(tree, tree.basis_tree(),
318
337
                                reporter=shelf_ui.ApplyReporter())
 
338
        self.addCleanup(shelver.finalize)
319
339
        shelver.expect('Apply change? [yNfq?]', 'n')
320
340
        shelver.expect('Apply change? [yNfq?]', 'n')
321
341
        # No final shelving prompt because no changes were selected
328
348
        self.addCleanup(tree.unlock)
329
349
        shelver = ExpectShelver(tree, tree.basis_tree(),
330
350
                                reporter=shelf_ui.ApplyReporter())
 
351
        self.addCleanup(shelver.finalize)
331
352
        shelver.expect('Apply change? [yNfq?]', 'y')
332
353
        shelver.expect('Apply change? [yNfq?]', 'y')
333
354
        shelver.expect('Apply 2 change(s)? [yNfq?]', 'n')
340
361
        self.addCleanup(tree.unlock)
341
362
        shelver = ExpectShelver(tree, tree.basis_tree(),
342
363
                                reporter=shelf_ui.ApplyReporter())
 
364
        self.addCleanup(shelver.finalize)
343
365
        shelver.expect('Apply change? [yNfq?]', 'y')
344
366
        shelver.expect('Apply change? [yNfq?]', 'y')
345
367
        shelver.expect('Apply 2 change(s)? [yNfq?]', 'y')
353
375
        self.addCleanup(tree.unlock)
354
376
        shelver = ExpectShelver(tree, tree.basis_tree(),
355
377
                                reporter=shelf_ui.ApplyReporter())
 
378
        self.addCleanup(shelver.finalize)
356
379
        shelver.expect('Apply binary changes? [yNfq?]', 'y')
357
380
        shelver.expect('Apply 1 change(s)? [yNfq?]', 'y')
358
381
        shelver.run()
365
388
        self.addCleanup(tree.unlock)
366
389
        shelver = ExpectShelver(tree, tree.basis_tree(),
367
390
                                reporter=shelf_ui.ApplyReporter())
 
391
        self.addCleanup(shelver.finalize)
368
392
        shelver.expect('Rename "bar" => "foo"? [yNfq?]', 'y')
369
393
        shelver.expect('Apply change? [yNfq?]', 'y')
370
394
        shelver.expect('Apply change? [yNfq?]', 'y')
379
403
        self.addCleanup(tree.unlock)
380
404
        shelver = ExpectShelver(tree, tree.basis_tree(),
381
405
                                reporter=shelf_ui.ApplyReporter())
 
406
        self.addCleanup(shelver.finalize)
382
407
        shelver.expect('Add file "foo"? [yNfq?]', 'y')
383
408
        shelver.expect('Apply 1 change(s)? [yNfq?]', 'y')
384
409
        shelver.run()
393
418
        self.addCleanup(tree.unlock)
394
419
        shelver = ExpectShelver(tree, tree.basis_tree(),
395
420
                                reporter=shelf_ui.ApplyReporter())
 
421
        self.addCleanup(shelver.finalize)
396
422
        shelver.expect('Delete file "foo"? [yNfq?]', 'y')
397
423
        shelver.expect('Apply 1 change(s)? [yNfq?]', 'y')
398
424
        shelver.run()
406
432
        self.addCleanup(tree.unlock)
407
433
        shelver = ExpectShelver(tree, tree.basis_tree(),
408
434
                               reporter=shelf_ui.ApplyReporter())
 
435
        self.addCleanup(shelver.finalize)
409
436
        shelver.expect('Change "foo" from directory to a file? [yNfq?]', 'y')
410
437
        shelver.expect('Apply 1 change(s)? [yNfq?]', 'y')
411
438
 
421
448
        self.addCleanup(tree.unlock)
422
449
        shelver = ExpectShelver(tree, tree.basis_tree(),
423
450
                                reporter=shelf_ui.ApplyReporter())
 
451
        self.addCleanup(shelver.finalize)
424
452
        shelver.expect('Change target of "baz" from "vax" to "bar"? [yNfq?]',
425
453
                       'y')
426
454
        shelver.expect('Apply 1 change(s)? [yNfq?]', 'y')
438
466
            tree.add('foo', 'foo-id')
439
467
            tree.commit('added foo')
440
468
            self.build_tree_contents([('tree/foo', LINES_ZY)])
441
 
            shelf_ui.Shelver(tree, tree.basis_tree(), auto_apply=True,
442
 
                             auto=True).run()
 
469
            shelver = shelf_ui.Shelver(tree, tree.basis_tree(),
 
470
                                       auto_apply=True, auto=True)
 
471
            try:
 
472
                shelver.run()
 
473
            finally:
 
474
                shelver.finalize()
443
475
        finally:
444
476
            tree.unlock()
445
477
        return tree
473
505
        self.assertFileEqual(LINES_AJ, 'tree/foo')
474
506
        self.assertEqual(1, tree.get_shelf_manager().last_shelf())
475
507
 
 
508
    def test_unshelve_args_preview(self):
 
509
        tree = self.create_tree_with_shelf()
 
510
        write_diff_to = StringIO()
 
511
        unshelver = shelf_ui.Unshelver.from_args(
 
512
            directory='tree', action='preview', write_diff_to=write_diff_to)
 
513
        try:
 
514
            unshelver.run()
 
515
        finally:
 
516
            unshelver.tree.unlock()
 
517
        # The changes were not unshelved.
 
518
        self.assertFileEqual(LINES_AJ, 'tree/foo')
 
519
        self.assertEqual(1, tree.get_shelf_manager().last_shelf())
 
520
 
 
521
        # But the diff was written to write_diff_to.
 
522
        diff = write_diff_to.getvalue()
 
523
        expected = dedent("""\
 
524
            @@ -1,4 +1,4 @@
 
525
            -a
 
526
            +z
 
527
             b
 
528
             c
 
529
             d
 
530
            @@ -7,4 +7,4 @@
 
531
             g
 
532
             h
 
533
             i
 
534
            -j
 
535
            +y
 
536
 
 
537
            """)
 
538
        self.assertEqualDiff(expected, diff[-len(expected):])
 
539
 
476
540
    def test_unshelve_args_delete_only(self):
477
541
        tree = self.make_branch_and_tree('tree')
478
542
        manager = tree.get_shelf_manager()
500
564
        self.assertRaises(errors.InvalidShelfId,
501
565
            shelf_ui.Unshelver.from_args, directory='tree',
502
566
            action='delete-only', shelf_id='foo')
 
567
 
 
568
 
 
569
class TestUnshelveScripts(TestUnshelver, 
 
570
                          script.TestCaseWithTransportAndScript): 
 
571
 
 
572
    def test_unshelve_messages_keep(self):
 
573
        self.create_tree_with_shelf()
 
574
        self.run_script("""
 
575
$ cd tree
 
576
$ bzr unshelve --keep
 
577
2>Using changes with id "1".
 
578
2> M  foo
 
579
2>All changes applied successfully.
 
580
""")
 
581
 
 
582
    def test_unshelve_messages_delete(self):
 
583
        self.create_tree_with_shelf()
 
584
        self.run_script("""
 
585
$ cd tree
 
586
$ bzr unshelve --delete-only
 
587
2>Deleted changes with id "1".
 
588
""")
 
589
 
 
590
    def test_unshelve_messages_apply(self):
 
591
        self.create_tree_with_shelf()
 
592
        self.run_script("""
 
593
$ cd tree
 
594
$ bzr unshelve --apply
 
595
2>Using changes with id "1".
 
596
2> M  foo
 
597
2>All changes applied successfully.
 
598
2>Deleted changes with id "1".
 
599
""")
 
600
 
 
601
    def test_unshelve_messages_dry_run(self):
 
602
        self.create_tree_with_shelf()
 
603
        self.run_script("""
 
604
$ cd tree
 
605
$ bzr unshelve --dry-run
 
606
2>Using changes with id "1".
 
607
2> M  foo
 
608
""")