~bzr-pqm/bzr/bzr.dev

« back to all changes in this revision

Viewing changes to bzrlib/tests/test_shelf_ui.py

  • Committer: Andrew Bennetts
  • Date: 2009-09-08 08:09:25 UTC
  • mto: (4634.6.27 2.0)
  • mto: This revision was merged to the branch mainline in revision 4680.
  • Revision ID: andrew.bennetts@canonical.com-20090908080925-ccmjw4kzzz7bepg7
Fix more tests to cope with new commit_write_group strictness.

Show diffs side-by-side

added added

removed removed

Lines of Context:
1
 
# Copyright (C) 2008, 2009, 2010 Canonical Ltd
 
1
# Copyright (C) 2008 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
22
21
 
23
22
from bzrlib import (
24
23
    errors,
26
25
    revision,
27
26
    tests,
28
27
)
29
 
from bzrlib.tests import script
30
 
from bzrlib.tests import (
31
 
    features,
32
 
    )
33
28
 
34
29
 
35
30
class ExpectShelver(shelf_ui.Shelver):
66
61
LINES_AY = 'a\nb\nc\nd\ne\nf\ng\nh\ni\ny\n'
67
62
 
68
63
 
69
 
class ShelfTestCase(tests.TestCaseWithTransport):
 
64
class TestShelver(tests.TestCaseWithTransport):
70
65
 
71
66
    def create_shelvable_tree(self):
72
67
        tree = self.make_branch_and_tree('tree')
76
71
        self.build_tree_contents([('tree/foo', LINES_ZY)])
77
72
        return tree
78
73
 
79
 
 
80
 
class TestShelver(ShelfTestCase):
81
 
 
82
74
    def test_unexpected_prompt_failure(self):
83
75
        tree = self.create_shelvable_tree()
84
76
        tree.lock_tree_write()
85
77
        self.addCleanup(tree.unlock)
86
78
        shelver = ExpectShelver(tree, tree.basis_tree())
87
 
        self.addCleanup(shelver.finalize)
88
79
        e = self.assertRaises(AssertionError, shelver.run)
89
80
        self.assertEqual('Unexpected prompt: Shelve? [yNfq?]', str(e))
90
81
 
93
84
        tree.lock_tree_write()
94
85
        self.addCleanup(tree.unlock)
95
86
        shelver = ExpectShelver(tree, tree.basis_tree())
96
 
        self.addCleanup(shelver.finalize)
97
87
        shelver.expect('foo', 'y')
98
88
        e = self.assertRaises(AssertionError, shelver.run)
99
89
        self.assertEqual('Wrong prompt: Shelve? [yNfq?]', str(e))
103
93
        tree.lock_tree_write()
104
94
        self.addCleanup(tree.unlock)
105
95
        shelver = ExpectShelver(tree, tree.basis_tree())
106
 
        self.addCleanup(shelver.finalize)
107
96
        shelver.expect('Shelve? [yNfq?]', 'n')
108
97
        shelver.expect('Shelve? [yNfq?]', 'n')
109
98
        # No final shelving prompt because no changes were selected
115
104
        tree.lock_tree_write()
116
105
        self.addCleanup(tree.unlock)
117
106
        shelver = ExpectShelver(tree, tree.basis_tree())
118
 
        self.addCleanup(shelver.finalize)
119
107
        shelver.expect('Shelve? [yNfq?]', 'y')
120
108
        shelver.expect('Shelve? [yNfq?]', 'y')
121
109
        shelver.expect('Shelve 2 change(s)? [yNfq?]', 'n')
127
115
        tree.lock_tree_write()
128
116
        self.addCleanup(tree.unlock)
129
117
        shelver = ExpectShelver(tree, tree.basis_tree())
130
 
        self.addCleanup(shelver.finalize)
131
118
        shelver.expect('Shelve? [yNfq?]', 'y')
132
119
        shelver.expect('Shelve? [yNfq?]', 'y')
133
120
        shelver.expect('Shelve 2 change(s)? [yNfq?]', 'y')
139
126
        tree.lock_tree_write()
140
127
        self.addCleanup(tree.unlock)
141
128
        shelver = ExpectShelver(tree, tree.basis_tree())
142
 
        self.addCleanup(shelver.finalize)
143
129
        shelver.expect('Shelve? [yNfq?]', 'y')
144
130
        shelver.expect('Shelve? [yNfq?]', 'n')
145
131
        shelver.expect('Shelve 1 change(s)? [yNfq?]', 'y')
152
138
        tree.lock_tree_write()
153
139
        self.addCleanup(tree.unlock)
154
140
        shelver = ExpectShelver(tree, tree.basis_tree())
155
 
        self.addCleanup(shelver.finalize)
156
141
        shelver.expect('Shelve binary changes? [yNfq?]', 'y')
157
142
        shelver.expect('Shelve 1 change(s)? [yNfq?]', 'y')
158
143
        shelver.run()
164
149
        tree.lock_tree_write()
165
150
        self.addCleanup(tree.unlock)
166
151
        shelver = ExpectShelver(tree, tree.basis_tree())
167
 
        self.addCleanup(shelver.finalize)
168
152
        shelver.expect('Shelve renaming "foo" => "bar"? [yNfq?]', 'y')
169
153
        shelver.expect('Shelve? [yNfq?]', 'y')
170
154
        shelver.expect('Shelve? [yNfq?]', 'y')
178
162
        tree.lock_tree_write()
179
163
        self.addCleanup(tree.unlock)
180
164
        shelver = ExpectShelver(tree, tree.basis_tree())
181
 
        self.addCleanup(shelver.finalize)
182
165
        shelver.expect('Shelve removing file "foo"? [yNfq?]', 'y')
183
166
        shelver.expect('Shelve 1 change(s)? [yNfq?]', 'y')
184
167
        shelver.run()
192
175
        tree.lock_tree_write()
193
176
        self.addCleanup(tree.unlock)
194
177
        shelver = ExpectShelver(tree, tree.basis_tree())
195
 
        self.addCleanup(shelver.finalize)
196
178
        shelver.expect('Shelve adding file "foo"? [yNfq?]', 'y')
197
179
        shelver.expect('Shelve 1 change(s)? [yNfq?]', 'y')
198
180
        shelver.run()
199
 
        self.assertPathDoesNotExist('tree/foo')
 
181
        self.failIfExists('tree/foo')
200
182
 
201
183
    def test_shelve_kind_change(self):
202
184
        tree = self.create_shelvable_tree()
205
187
        tree.lock_tree_write()
206
188
        self.addCleanup(tree.unlock)
207
189
        shelver = ExpectShelver(tree, tree.basis_tree())
208
 
        self.addCleanup(shelver.finalize)
209
190
        shelver.expect('Shelve changing "foo" from file to directory? [yNfq?]',
210
191
                       'y')
211
192
        shelver.expect('Shelve 1 change(s)? [yNfq?]', 'y')
212
193
 
213
194
    def test_shelve_modify_target(self):
214
 
        self.requireFeature(features.SymlinkFeature)
 
195
        self.requireFeature(tests.SymlinkFeature)
215
196
        tree = self.create_shelvable_tree()
216
197
        os.symlink('bar', 'tree/baz')
217
198
        tree.add('baz', 'baz-id')
221
202
        tree.lock_tree_write()
222
203
        self.addCleanup(tree.unlock)
223
204
        shelver = ExpectShelver(tree, tree.basis_tree())
224
 
        self.addCleanup(shelver.finalize)
225
205
        shelver.expect('Shelve changing target of "baz" from "bar" to '
226
206
                '"vax"? [yNfq?]', 'y')
227
207
        shelver.expect('Shelve 1 change(s)? [yNfq?]', 'y')
233
213
        tree.lock_tree_write()
234
214
        self.addCleanup(tree.unlock)
235
215
        shelver = ExpectShelver(tree, tree.basis_tree())
236
 
        self.addCleanup(shelver.finalize)
237
216
        shelver.expect('Shelve? [yNfq?]', 'f')
238
217
        shelver.expect('Shelve 2 change(s)? [yNfq?]', 'y')
239
218
        shelver.run()
244
223
        tree.lock_tree_write()
245
224
        self.addCleanup(tree.unlock)
246
225
        shelver = ExpectShelver(tree, tree.basis_tree())
247
 
        self.addCleanup(shelver.finalize)
248
226
        shelver.expect('Shelve? [yNfq?]', 'q')
249
227
        self.assertRaises(errors.UserAbort, shelver.run)
250
228
        self.assertFileEqual(LINES_ZY, 'tree/foo')
256
234
        try:
257
235
            shelver.run()
258
236
        finally:
259
 
            shelver.finalize()
 
237
            shelver.work_tree.unlock()
260
238
        self.assertFileEqual(LINES_AJ, 'tree/foo')
261
239
 
262
240
    def test_shelve_filename(self):
266
244
        tree.lock_tree_write()
267
245
        self.addCleanup(tree.unlock)
268
246
        shelver = ExpectShelver(tree, tree.basis_tree(), file_list=['bar'])
269
 
        self.addCleanup(shelver.finalize)
270
247
        shelver.expect('Shelve adding file "bar"? [yNfq?]', 'y')
271
248
        shelver.expect('Shelve 1 change(s)? [yNfq?]', 'y')
272
249
        shelver.run()
276
253
        tree.lock_tree_write()
277
254
        self.addCleanup(tree.unlock)
278
255
        shelver = ExpectShelver(tree, tree.basis_tree())
279
 
        self.addCleanup(shelver.finalize)
280
256
        shelver.expect('Shelve? [yNfq?]', '?')
281
257
        shelver.expect('Shelve? [(y)es, (N)o, (f)inish, or (q)uit]', 'f')
282
258
        shelver.expect('Shelve 2 change(s)? [yNfq?]', 'y')
283
259
        shelver.run()
284
260
 
285
 
    def test_shelve_destroy(self):
 
261
    def test_shelve_distroy(self):
286
262
        tree = self.create_shelvable_tree()
287
263
        shelver = shelf_ui.Shelver.from_args(sys.stdout, all=True,
288
264
                                             directory='tree', destroy=True)
289
 
        self.addCleanup(shelver.finalize)
290
 
        shelver.run()
 
265
        try:
 
266
            shelver.run()
 
267
        finally:
 
268
            shelver.work_tree.unlock()
291
269
        self.assertIs(None, tree.get_shelf_manager().last_shelf())
292
270
        self.assertFileEqual(LINES_AJ, 'tree/foo')
293
271
 
298
276
            target = tree.branch.repository.revision_tree(target_revision_id)
299
277
            shelver = shelf_ui.Shelver(tree, target, auto=True,
300
278
                                       auto_apply=True)
301
 
            try:
302
 
                shelver.run()
303
 
            finally:
304
 
                shelver.finalize()
 
279
            shelver.run()
305
280
        finally:
306
281
            tree.unlock()
307
282
 
308
 
    def test_shelve_old_root_preserved(self):
 
283
    def test_shelve_old_root_deleted(self):
309
284
        tree1 = self.make_branch_and_tree('tree1')
310
285
        tree1.commit('add root')
311
 
        tree1_root_id = tree1.get_root_id()
312
286
        tree2 = self.make_branch_and_tree('tree2')
313
287
        rev2 = tree2.commit('add root')
314
 
        self.assertNotEquals(tree1_root_id, tree2.get_root_id())
315
288
        tree1.merge_from_branch(tree2.branch,
316
289
                                from_revision=revision.NULL_REVISION)
317
 
        tree1.commit('merging in tree2')
318
 
        self.assertEquals(tree1_root_id, tree1.get_root_id())
 
290
        tree1.commit('Replaced root entry')
319
291
        # 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))
 
292
        self.expectFailure('Cannot shelve replacing a root entry',
 
293
                           self.assertRaises, AssertionError,
 
294
                           self.assertRaises, errors.InconsistentDelta,
 
295
                           self.shelve_all, tree1, rev2)
329
296
 
330
297
    def test_shelve_split(self):
331
298
        outer_tree = self.make_branch_and_tree('outer')
341
308
                           outer_tree, rev2)
342
309
 
343
310
 
344
 
class TestApplyReporter(ShelfTestCase):
 
311
class TestApplyReporter(TestShelver):
345
312
 
346
313
    def test_shelve_not_diff(self):
347
314
        tree = self.create_shelvable_tree()
349
316
        self.addCleanup(tree.unlock)
350
317
        shelver = ExpectShelver(tree, tree.basis_tree(),
351
318
                                reporter=shelf_ui.ApplyReporter())
352
 
        self.addCleanup(shelver.finalize)
353
319
        shelver.expect('Apply change? [yNfq?]', 'n')
354
320
        shelver.expect('Apply change? [yNfq?]', 'n')
355
321
        # No final shelving prompt because no changes were selected
362
328
        self.addCleanup(tree.unlock)
363
329
        shelver = ExpectShelver(tree, tree.basis_tree(),
364
330
                                reporter=shelf_ui.ApplyReporter())
365
 
        self.addCleanup(shelver.finalize)
366
331
        shelver.expect('Apply change? [yNfq?]', 'y')
367
332
        shelver.expect('Apply change? [yNfq?]', 'y')
368
333
        shelver.expect('Apply 2 change(s)? [yNfq?]', 'n')
375
340
        self.addCleanup(tree.unlock)
376
341
        shelver = ExpectShelver(tree, tree.basis_tree(),
377
342
                                reporter=shelf_ui.ApplyReporter())
378
 
        self.addCleanup(shelver.finalize)
379
343
        shelver.expect('Apply change? [yNfq?]', 'y')
380
344
        shelver.expect('Apply change? [yNfq?]', 'y')
381
345
        shelver.expect('Apply 2 change(s)? [yNfq?]', 'y')
389
353
        self.addCleanup(tree.unlock)
390
354
        shelver = ExpectShelver(tree, tree.basis_tree(),
391
355
                                reporter=shelf_ui.ApplyReporter())
392
 
        self.addCleanup(shelver.finalize)
393
356
        shelver.expect('Apply binary changes? [yNfq?]', 'y')
394
357
        shelver.expect('Apply 1 change(s)? [yNfq?]', 'y')
395
358
        shelver.run()
402
365
        self.addCleanup(tree.unlock)
403
366
        shelver = ExpectShelver(tree, tree.basis_tree(),
404
367
                                reporter=shelf_ui.ApplyReporter())
405
 
        self.addCleanup(shelver.finalize)
406
368
        shelver.expect('Rename "bar" => "foo"? [yNfq?]', 'y')
407
369
        shelver.expect('Apply change? [yNfq?]', 'y')
408
370
        shelver.expect('Apply change? [yNfq?]', 'y')
417
379
        self.addCleanup(tree.unlock)
418
380
        shelver = ExpectShelver(tree, tree.basis_tree(),
419
381
                                reporter=shelf_ui.ApplyReporter())
420
 
        self.addCleanup(shelver.finalize)
421
382
        shelver.expect('Add file "foo"? [yNfq?]', 'y')
422
383
        shelver.expect('Apply 1 change(s)? [yNfq?]', 'y')
423
384
        shelver.run()
432
393
        self.addCleanup(tree.unlock)
433
394
        shelver = ExpectShelver(tree, tree.basis_tree(),
434
395
                                reporter=shelf_ui.ApplyReporter())
435
 
        self.addCleanup(shelver.finalize)
436
396
        shelver.expect('Delete file "foo"? [yNfq?]', 'y')
437
397
        shelver.expect('Apply 1 change(s)? [yNfq?]', 'y')
438
398
        shelver.run()
439
 
        self.assertPathDoesNotExist('tree/foo')
 
399
        self.failIfExists('tree/foo')
440
400
 
441
401
    def test_shelve_kind_change(self):
442
402
        tree = self.create_shelvable_tree()
446
406
        self.addCleanup(tree.unlock)
447
407
        shelver = ExpectShelver(tree, tree.basis_tree(),
448
408
                               reporter=shelf_ui.ApplyReporter())
449
 
        self.addCleanup(shelver.finalize)
450
409
        shelver.expect('Change "foo" from directory to a file? [yNfq?]', 'y')
451
410
        shelver.expect('Apply 1 change(s)? [yNfq?]', 'y')
452
411
 
453
412
    def test_shelve_modify_target(self):
454
 
        self.requireFeature(features.SymlinkFeature)
 
413
        self.requireFeature(tests.SymlinkFeature)
455
414
        tree = self.create_shelvable_tree()
456
415
        os.symlink('bar', 'tree/baz')
457
416
        tree.add('baz', 'baz-id')
462
421
        self.addCleanup(tree.unlock)
463
422
        shelver = ExpectShelver(tree, tree.basis_tree(),
464
423
                                reporter=shelf_ui.ApplyReporter())
465
 
        self.addCleanup(shelver.finalize)
466
424
        shelver.expect('Change target of "baz" from "vax" to "bar"? [yNfq?]',
467
425
                       'y')
468
426
        shelver.expect('Apply 1 change(s)? [yNfq?]', 'y')
480
438
            tree.add('foo', 'foo-id')
481
439
            tree.commit('added foo')
482
440
            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()
 
441
            shelf_ui.Shelver(tree, tree.basis_tree(), auto_apply=True,
 
442
                             auto=True).run()
489
443
        finally:
490
444
            tree.unlock()
491
445
        return tree
519
473
        self.assertFileEqual(LINES_AJ, 'tree/foo')
520
474
        self.assertEqual(1, tree.get_shelf_manager().last_shelf())
521
475
 
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
476
    def test_unshelve_args_delete_only(self):
555
477
        tree = self.make_branch_and_tree('tree')
556
478
        manager = tree.get_shelf_manager()
578
500
        self.assertRaises(errors.InvalidShelfId,
579
501
            shelf_ui.Unshelver.from_args, directory='tree',
580
502
            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
 
""")