~bzr-pqm/bzr/bzr.dev

« back to all changes in this revision

Viewing changes to bzrlib/tests/test_shelf_ui.py

  • Committer: Jelmer Vernooij
  • Date: 2010-01-20 22:32:07 UTC
  • mto: This revision was merged to the branch mainline in revision 4977.
  • Revision ID: jelmer@samba.org-20100120223207-pfz89u161ahyzvnt
Add FileTimestampUnavailable exception.

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
27
27
    tests,
28
28
)
29
29
from bzrlib.tests import script
30
 
from bzrlib.tests import (
31
 
    features,
32
 
    )
33
30
 
34
31
 
35
32
class ExpectShelver(shelf_ui.Shelver):
44
41
        self.expected = []
45
42
        self.diff_writer = StringIO()
46
43
 
47
 
    def expect(self, message, response):
48
 
        self.expected.append((message, response))
 
44
    def expect(self, prompt, response):
 
45
        self.expected.append((prompt, response))
49
46
 
50
 
    def prompt(self, message, choices, default):
 
47
    def prompt(self, message):
51
48
        try:
52
 
            expected_message, response = self.expected.pop(0)
 
49
            prompt, response = self.expected.pop(0)
53
50
        except IndexError:
54
51
            raise AssertionError('Unexpected prompt: %s' % message)
55
 
        if message != expected_message:
 
52
        if prompt != message:
56
53
            raise AssertionError('Wrong prompt: %s' % message)
57
 
        if choices != '&yes\n&No\n&finish\n&quit':
58
 
            raise AssertionError('Wrong choices: %s' % choices)
59
54
        return response
60
55
 
61
56
 
68
63
LINES_AY = 'a\nb\nc\nd\ne\nf\ng\nh\ni\ny\n'
69
64
 
70
65
 
71
 
class ShelfTestCase(tests.TestCaseWithTransport):
 
66
class TestShelver(tests.TestCaseWithTransport):
72
67
 
73
68
    def create_shelvable_tree(self):
74
69
        tree = self.make_branch_and_tree('tree')
78
73
        self.build_tree_contents([('tree/foo', LINES_ZY)])
79
74
        return tree
80
75
 
81
 
 
82
 
class TestShelver(ShelfTestCase):
83
 
 
84
76
    def test_unexpected_prompt_failure(self):
85
77
        tree = self.create_shelvable_tree()
86
78
        tree.lock_tree_write()
88
80
        shelver = ExpectShelver(tree, tree.basis_tree())
89
81
        self.addCleanup(shelver.finalize)
90
82
        e = self.assertRaises(AssertionError, shelver.run)
91
 
        self.assertEqual('Unexpected prompt: Shelve?', str(e))
 
83
        self.assertEqual('Unexpected prompt: Shelve? [yNfq?]', str(e))
92
84
 
93
85
    def test_wrong_prompt_failure(self):
94
86
        tree = self.create_shelvable_tree()
96
88
        self.addCleanup(tree.unlock)
97
89
        shelver = ExpectShelver(tree, tree.basis_tree())
98
90
        self.addCleanup(shelver.finalize)
99
 
        shelver.expect('foo', 0)
 
91
        shelver.expect('foo', 'y')
100
92
        e = self.assertRaises(AssertionError, shelver.run)
101
 
        self.assertEqual('Wrong prompt: Shelve?', str(e))
 
93
        self.assertEqual('Wrong prompt: Shelve? [yNfq?]', str(e))
102
94
 
103
95
    def test_shelve_not_diff(self):
104
96
        tree = self.create_shelvable_tree()
106
98
        self.addCleanup(tree.unlock)
107
99
        shelver = ExpectShelver(tree, tree.basis_tree())
108
100
        self.addCleanup(shelver.finalize)
109
 
        shelver.expect('Shelve?', 1)
110
 
        shelver.expect('Shelve?', 1)
 
101
        shelver.expect('Shelve? [yNfq?]', 'n')
 
102
        shelver.expect('Shelve? [yNfq?]', 'n')
111
103
        # No final shelving prompt because no changes were selected
112
104
        shelver.run()
113
105
        self.assertFileEqual(LINES_ZY, 'tree/foo')
118
110
        self.addCleanup(tree.unlock)
119
111
        shelver = ExpectShelver(tree, tree.basis_tree())
120
112
        self.addCleanup(shelver.finalize)
121
 
        shelver.expect('Shelve?', 0)
122
 
        shelver.expect('Shelve?', 0)
123
 
        shelver.expect('Shelve 2 change(s)?', 1)
 
113
        shelver.expect('Shelve? [yNfq?]', 'y')
 
114
        shelver.expect('Shelve? [yNfq?]', 'y')
 
115
        shelver.expect('Shelve 2 change(s)? [yNfq?]', 'n')
124
116
        shelver.run()
125
117
        self.assertFileEqual(LINES_ZY, 'tree/foo')
126
118
 
130
122
        self.addCleanup(tree.unlock)
131
123
        shelver = ExpectShelver(tree, tree.basis_tree())
132
124
        self.addCleanup(shelver.finalize)
133
 
        shelver.expect('Shelve?', 0)
134
 
        shelver.expect('Shelve?', 0)
135
 
        shelver.expect('Shelve 2 change(s)?', 0)
 
125
        shelver.expect('Shelve? [yNfq?]', 'y')
 
126
        shelver.expect('Shelve? [yNfq?]', 'y')
 
127
        shelver.expect('Shelve 2 change(s)? [yNfq?]', 'y')
136
128
        shelver.run()
137
129
        self.assertFileEqual(LINES_AJ, 'tree/foo')
138
130
 
142
134
        self.addCleanup(tree.unlock)
143
135
        shelver = ExpectShelver(tree, tree.basis_tree())
144
136
        self.addCleanup(shelver.finalize)
145
 
        shelver.expect('Shelve?', 0)
146
 
        shelver.expect('Shelve?', 1)
147
 
        shelver.expect('Shelve 1 change(s)?', 0)
 
137
        shelver.expect('Shelve? [yNfq?]', 'y')
 
138
        shelver.expect('Shelve? [yNfq?]', 'n')
 
139
        shelver.expect('Shelve 1 change(s)? [yNfq?]', 'y')
148
140
        shelver.run()
149
141
        self.assertFileEqual(LINES_AY, 'tree/foo')
150
142
 
155
147
        self.addCleanup(tree.unlock)
156
148
        shelver = ExpectShelver(tree, tree.basis_tree())
157
149
        self.addCleanup(shelver.finalize)
158
 
        shelver.expect('Shelve binary changes?', 0)
159
 
        shelver.expect('Shelve 1 change(s)?', 0)
 
150
        shelver.expect('Shelve binary changes? [yNfq?]', 'y')
 
151
        shelver.expect('Shelve 1 change(s)? [yNfq?]', 'y')
160
152
        shelver.run()
161
153
        self.assertFileEqual(LINES_AJ, 'tree/foo')
162
154
 
167
159
        self.addCleanup(tree.unlock)
168
160
        shelver = ExpectShelver(tree, tree.basis_tree())
169
161
        self.addCleanup(shelver.finalize)
170
 
        shelver.expect('Shelve renaming "foo" => "bar"?', 0)
171
 
        shelver.expect('Shelve?', 0)
172
 
        shelver.expect('Shelve?', 0)
173
 
        shelver.expect('Shelve 3 change(s)?', 0)
 
162
        shelver.expect('Shelve renaming "foo" => "bar"? [yNfq?]', 'y')
 
163
        shelver.expect('Shelve? [yNfq?]', 'y')
 
164
        shelver.expect('Shelve? [yNfq?]', 'y')
 
165
        shelver.expect('Shelve 3 change(s)? [yNfq?]', 'y')
174
166
        shelver.run()
175
167
        self.assertFileEqual(LINES_AJ, 'tree/foo')
176
168
 
181
173
        self.addCleanup(tree.unlock)
182
174
        shelver = ExpectShelver(tree, tree.basis_tree())
183
175
        self.addCleanup(shelver.finalize)
184
 
        shelver.expect('Shelve removing file "foo"?', 0)
185
 
        shelver.expect('Shelve 1 change(s)?', 0)
 
176
        shelver.expect('Shelve removing file "foo"? [yNfq?]', 'y')
 
177
        shelver.expect('Shelve 1 change(s)? [yNfq?]', 'y')
186
178
        shelver.run()
187
179
        self.assertFileEqual(LINES_AJ, 'tree/foo')
188
180
 
195
187
        self.addCleanup(tree.unlock)
196
188
        shelver = ExpectShelver(tree, tree.basis_tree())
197
189
        self.addCleanup(shelver.finalize)
198
 
        shelver.expect('Shelve adding file "foo"?', 0)
199
 
        shelver.expect('Shelve 1 change(s)?', 0)
 
190
        shelver.expect('Shelve adding file "foo"? [yNfq?]', 'y')
 
191
        shelver.expect('Shelve 1 change(s)? [yNfq?]', 'y')
200
192
        shelver.run()
201
 
        self.assertPathDoesNotExist('tree/foo')
 
193
        self.failIfExists('tree/foo')
202
194
 
203
195
    def test_shelve_kind_change(self):
204
196
        tree = self.create_shelvable_tree()
208
200
        self.addCleanup(tree.unlock)
209
201
        shelver = ExpectShelver(tree, tree.basis_tree())
210
202
        self.addCleanup(shelver.finalize)
211
 
        shelver.expect('Shelve changing "foo" from file to directory?',
212
 
                       0)
213
 
        shelver.expect('Shelve 1 change(s)?', 0)
 
203
        shelver.expect('Shelve changing "foo" from file to directory? [yNfq?]',
 
204
                       'y')
 
205
        shelver.expect('Shelve 1 change(s)? [yNfq?]', 'y')
214
206
 
215
207
    def test_shelve_modify_target(self):
216
 
        self.requireFeature(features.SymlinkFeature)
 
208
        self.requireFeature(tests.SymlinkFeature)
217
209
        tree = self.create_shelvable_tree()
218
210
        os.symlink('bar', 'tree/baz')
219
211
        tree.add('baz', 'baz-id')
225
217
        shelver = ExpectShelver(tree, tree.basis_tree())
226
218
        self.addCleanup(shelver.finalize)
227
219
        shelver.expect('Shelve changing target of "baz" from "bar" to '
228
 
                '"vax"?', 0)
229
 
        shelver.expect('Shelve 1 change(s)?', 0)
 
220
                '"vax"? [yNfq?]', 'y')
 
221
        shelver.expect('Shelve 1 change(s)? [yNfq?]', 'y')
230
222
        shelver.run()
231
223
        self.assertEqual('bar', os.readlink('tree/baz'))
232
224
 
236
228
        self.addCleanup(tree.unlock)
237
229
        shelver = ExpectShelver(tree, tree.basis_tree())
238
230
        self.addCleanup(shelver.finalize)
239
 
        shelver.expect('Shelve?', 2)
240
 
        shelver.expect('Shelve 2 change(s)?', 0)
 
231
        shelver.expect('Shelve? [yNfq?]', 'f')
 
232
        shelver.expect('Shelve 2 change(s)? [yNfq?]', 'y')
241
233
        shelver.run()
242
234
        self.assertFileEqual(LINES_AJ, 'tree/foo')
243
235
 
247
239
        self.addCleanup(tree.unlock)
248
240
        shelver = ExpectShelver(tree, tree.basis_tree())
249
241
        self.addCleanup(shelver.finalize)
250
 
        shelver.expect('Shelve?', 3)
 
242
        shelver.expect('Shelve? [yNfq?]', 'q')
251
243
        self.assertRaises(errors.UserAbort, shelver.run)
252
244
        self.assertFileEqual(LINES_ZY, 'tree/foo')
253
245
 
269
261
        self.addCleanup(tree.unlock)
270
262
        shelver = ExpectShelver(tree, tree.basis_tree(), file_list=['bar'])
271
263
        self.addCleanup(shelver.finalize)
272
 
        shelver.expect('Shelve adding file "bar"?', 0)
273
 
        shelver.expect('Shelve 1 change(s)?', 0)
 
264
        shelver.expect('Shelve adding file "bar"? [yNfq?]', 'y')
 
265
        shelver.expect('Shelve 1 change(s)? [yNfq?]', 'y')
 
266
        shelver.run()
 
267
 
 
268
    def test_shelve_help(self):
 
269
        tree = self.create_shelvable_tree()
 
270
        tree.lock_tree_write()
 
271
        self.addCleanup(tree.unlock)
 
272
        shelver = ExpectShelver(tree, tree.basis_tree())
 
273
        self.addCleanup(shelver.finalize)
 
274
        shelver.expect('Shelve? [yNfq?]', '?')
 
275
        shelver.expect('Shelve? [(y)es, (N)o, (f)inish, or (q)uit]', 'f')
 
276
        shelver.expect('Shelve 2 change(s)? [yNfq?]', 'y')
274
277
        shelver.run()
275
278
 
276
279
    def test_shelve_destroy(self):
296
299
        finally:
297
300
            tree.unlock()
298
301
 
299
 
    def test_shelve_old_root_preserved(self):
 
302
    def test_shelve_old_root_deleted(self):
300
303
        tree1 = self.make_branch_and_tree('tree1')
301
304
        tree1.commit('add root')
302
 
        tree1_root_id = tree1.get_root_id()
303
305
        tree2 = self.make_branch_and_tree('tree2')
304
306
        rev2 = tree2.commit('add root')
305
 
        self.assertNotEquals(tree1_root_id, tree2.get_root_id())
306
307
        tree1.merge_from_branch(tree2.branch,
307
308
                                from_revision=revision.NULL_REVISION)
308
 
        tree1.commit('merging in tree2')
309
 
        self.assertEquals(tree1_root_id, tree1.get_root_id())
 
309
        tree1.commit('Replaced root entry')
310
310
        # This is essentially assertNotRaises(InconsistentDelta)
311
 
        # With testtools 0.9.9, it can be rewritten as:
312
 
        # with ExpectedException(AssertionError,
313
 
        #                        'InconsistentDelta not raised'):
314
 
        #     with ExpectedException(errors.InconsistentDelta, ''):
315
 
        #         self.shelve_all(tree1, rev2)
316
 
        e = self.assertRaises(AssertionError, self.assertRaises,
317
 
                              errors.InconsistentDelta, self.shelve_all, tree1,
318
 
                              rev2)
319
 
        self.assertContainsRe('InconsistentDelta not raised', str(e))
 
311
        self.expectFailure('Cannot shelve replacing a root entry',
 
312
                           self.assertRaises, AssertionError,
 
313
                           self.assertRaises, errors.InconsistentDelta,
 
314
                           self.shelve_all, tree1, rev2)
320
315
 
321
316
    def test_shelve_split(self):
322
317
        outer_tree = self.make_branch_and_tree('outer')
332
327
                           outer_tree, rev2)
333
328
 
334
329
 
335
 
class TestApplyReporter(ShelfTestCase):
 
330
class TestApplyReporter(TestShelver):
336
331
 
337
332
    def test_shelve_not_diff(self):
338
333
        tree = self.create_shelvable_tree()
341
336
        shelver = ExpectShelver(tree, tree.basis_tree(),
342
337
                                reporter=shelf_ui.ApplyReporter())
343
338
        self.addCleanup(shelver.finalize)
344
 
        shelver.expect('Apply change?', 1)
345
 
        shelver.expect('Apply change?', 1)
 
339
        shelver.expect('Apply change? [yNfq?]', 'n')
 
340
        shelver.expect('Apply change? [yNfq?]', 'n')
346
341
        # No final shelving prompt because no changes were selected
347
342
        shelver.run()
348
343
        self.assertFileEqual(LINES_ZY, 'tree/foo')
354
349
        shelver = ExpectShelver(tree, tree.basis_tree(),
355
350
                                reporter=shelf_ui.ApplyReporter())
356
351
        self.addCleanup(shelver.finalize)
357
 
        shelver.expect('Apply change?', 0)
358
 
        shelver.expect('Apply change?', 0)
359
 
        shelver.expect('Apply 2 change(s)?', 1)
 
352
        shelver.expect('Apply change? [yNfq?]', 'y')
 
353
        shelver.expect('Apply change? [yNfq?]', 'y')
 
354
        shelver.expect('Apply 2 change(s)? [yNfq?]', 'n')
360
355
        shelver.run()
361
356
        self.assertFileEqual(LINES_ZY, 'tree/foo')
362
357
 
367
362
        shelver = ExpectShelver(tree, tree.basis_tree(),
368
363
                                reporter=shelf_ui.ApplyReporter())
369
364
        self.addCleanup(shelver.finalize)
370
 
        shelver.expect('Apply change?', 0)
371
 
        shelver.expect('Apply change?', 0)
372
 
        shelver.expect('Apply 2 change(s)?', 0)
 
365
        shelver.expect('Apply change? [yNfq?]', 'y')
 
366
        shelver.expect('Apply change? [yNfq?]', 'y')
 
367
        shelver.expect('Apply 2 change(s)? [yNfq?]', 'y')
373
368
        shelver.run()
374
369
        self.assertFileEqual(LINES_AJ, 'tree/foo')
375
370
 
381
376
        shelver = ExpectShelver(tree, tree.basis_tree(),
382
377
                                reporter=shelf_ui.ApplyReporter())
383
378
        self.addCleanup(shelver.finalize)
384
 
        shelver.expect('Apply binary changes?', 0)
385
 
        shelver.expect('Apply 1 change(s)?', 0)
 
379
        shelver.expect('Apply binary changes? [yNfq?]', 'y')
 
380
        shelver.expect('Apply 1 change(s)? [yNfq?]', 'y')
386
381
        shelver.run()
387
382
        self.assertFileEqual(LINES_AJ, 'tree/foo')
388
383
 
394
389
        shelver = ExpectShelver(tree, tree.basis_tree(),
395
390
                                reporter=shelf_ui.ApplyReporter())
396
391
        self.addCleanup(shelver.finalize)
397
 
        shelver.expect('Rename "bar" => "foo"?', 0)
398
 
        shelver.expect('Apply change?', 0)
399
 
        shelver.expect('Apply change?', 0)
400
 
        shelver.expect('Apply 3 change(s)?', 0)
 
392
        shelver.expect('Rename "bar" => "foo"? [yNfq?]', 'y')
 
393
        shelver.expect('Apply change? [yNfq?]', 'y')
 
394
        shelver.expect('Apply change? [yNfq?]', 'y')
 
395
        shelver.expect('Apply 3 change(s)? [yNfq?]', 'y')
401
396
        shelver.run()
402
397
        self.assertFileEqual(LINES_AJ, 'tree/foo')
403
398
 
409
404
        shelver = ExpectShelver(tree, tree.basis_tree(),
410
405
                                reporter=shelf_ui.ApplyReporter())
411
406
        self.addCleanup(shelver.finalize)
412
 
        shelver.expect('Add file "foo"?', 0)
413
 
        shelver.expect('Apply 1 change(s)?', 0)
 
407
        shelver.expect('Add file "foo"? [yNfq?]', 'y')
 
408
        shelver.expect('Apply 1 change(s)? [yNfq?]', 'y')
414
409
        shelver.run()
415
410
        self.assertFileEqual(LINES_AJ, 'tree/foo')
416
411
 
424
419
        shelver = ExpectShelver(tree, tree.basis_tree(),
425
420
                                reporter=shelf_ui.ApplyReporter())
426
421
        self.addCleanup(shelver.finalize)
427
 
        shelver.expect('Delete file "foo"?', 0)
428
 
        shelver.expect('Apply 1 change(s)?', 0)
 
422
        shelver.expect('Delete file "foo"? [yNfq?]', 'y')
 
423
        shelver.expect('Apply 1 change(s)? [yNfq?]', 'y')
429
424
        shelver.run()
430
 
        self.assertPathDoesNotExist('tree/foo')
 
425
        self.failIfExists('tree/foo')
431
426
 
432
427
    def test_shelve_kind_change(self):
433
428
        tree = self.create_shelvable_tree()
438
433
        shelver = ExpectShelver(tree, tree.basis_tree(),
439
434
                               reporter=shelf_ui.ApplyReporter())
440
435
        self.addCleanup(shelver.finalize)
441
 
        shelver.expect('Change "foo" from directory to a file?', 0)
442
 
        shelver.expect('Apply 1 change(s)?', 0)
 
436
        shelver.expect('Change "foo" from directory to a file? [yNfq?]', 'y')
 
437
        shelver.expect('Apply 1 change(s)? [yNfq?]', 'y')
443
438
 
444
439
    def test_shelve_modify_target(self):
445
 
        self.requireFeature(features.SymlinkFeature)
 
440
        self.requireFeature(tests.SymlinkFeature)
446
441
        tree = self.create_shelvable_tree()
447
442
        os.symlink('bar', 'tree/baz')
448
443
        tree.add('baz', 'baz-id')
454
449
        shelver = ExpectShelver(tree, tree.basis_tree(),
455
450
                                reporter=shelf_ui.ApplyReporter())
456
451
        self.addCleanup(shelver.finalize)
457
 
        shelver.expect('Change target of "baz" from "vax" to "bar"?',
458
 
                       0)
459
 
        shelver.expect('Apply 1 change(s)?', 0)
 
452
        shelver.expect('Change target of "baz" from "vax" to "bar"? [yNfq?]',
 
453
                       'y')
 
454
        shelver.expect('Apply 1 change(s)? [yNfq?]', 'y')
460
455
        shelver.run()
461
456
        self.assertEqual('bar', os.readlink('tree/baz'))
462
457