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