1
# Copyright (C) 2008, 2009, 2010 Canonical Ltd
1
# Copyright (C) 2008 Canonical Ltd
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
45
42
self.diff_writer = StringIO()
47
def expect(self, message, response):
48
self.expected.append((message, response))
44
def expect(self, prompt, response):
45
self.expected.append((prompt, response))
50
def prompt(self, message, choices, default):
47
def prompt(self, message):
52
expected_message, response = self.expected.pop(0)
49
prompt, response = self.expected.pop(0)
54
51
raise AssertionError('Unexpected prompt: %s' % message)
55
if message != expected_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)
68
63
LINES_AY = 'a\nb\nc\nd\ne\nf\ng\nh\ni\ny\n'
71
class ShelfTestCase(tests.TestCaseWithTransport):
66
class TestShelver(tests.TestCaseWithTransport):
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)])
82
class TestShelver(ShelfTestCase):
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))
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))
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
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')
125
117
self.assertFileEqual(LINES_ZY, 'tree/foo')
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')
137
129
self.assertFileEqual(LINES_AJ, 'tree/foo')
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')
149
141
self.assertFileEqual(LINES_AY, 'tree/foo')
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')
161
153
self.assertFileEqual(LINES_AJ, 'tree/foo')
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')
175
167
self.assertFileEqual(LINES_AJ, 'tree/foo')
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')
187
179
self.assertFileEqual(LINES_AJ, 'tree/foo')
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')
201
self.assertPathDoesNotExist('tree/foo')
193
self.failIfExists('tree/foo')
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?',
213
shelver.expect('Shelve 1 change(s)?', 0)
203
shelver.expect('Shelve changing "foo" from file to directory? [yNfq?]',
205
shelver.expect('Shelve 1 change(s)? [yNfq?]', 'y')
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 '
229
shelver.expect('Shelve 1 change(s)?', 0)
220
'"vax"? [yNfq?]', 'y')
221
shelver.expect('Shelve 1 change(s)? [yNfq?]', 'y')
231
223
self.assertEqual('bar', os.readlink('tree/baz'))
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')
242
234
self.assertFileEqual(LINES_AJ, 'tree/foo')
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')
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')
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')
276
279
def test_shelve_destroy(self):
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,
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)
321
316
def test_shelve_split(self):
322
317
outer_tree = self.make_branch_and_tree('outer')
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
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')
361
356
self.assertFileEqual(LINES_ZY, 'tree/foo')
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')
374
369
self.assertFileEqual(LINES_AJ, 'tree/foo')
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')
387
382
self.assertFileEqual(LINES_AJ, 'tree/foo')
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')
402
397
self.assertFileEqual(LINES_AJ, 'tree/foo')
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')
415
410
self.assertFileEqual(LINES_AJ, 'tree/foo')
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')
430
self.assertPathDoesNotExist('tree/foo')
425
self.failIfExists('tree/foo')
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')
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"?',
459
shelver.expect('Apply 1 change(s)?', 0)
452
shelver.expect('Change target of "baz" from "vax" to "bar"? [yNfq?]',
454
shelver.expect('Apply 1 change(s)? [yNfq?]', 'y')
461
456
self.assertEqual('bar', os.readlink('tree/baz'))