1
# Copyright (C) 2008 Canonical Ltd
3
# This program is free software; you can redistribute it and/or modify
4
# it under the terms of the GNU General Public License as published by
5
# the Free Software Foundation; either version 2 of the License, or
6
# (at your option) any later version.
8
# This program is distributed in the hope that it will be useful,
9
# but WITHOUT ANY WARRANTY; without even the implied warranty of
10
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
11
# GNU General Public License for more details.
13
# You should have received a copy of the GNU General Public License
14
# along with this program; if not, write to the Free Software
15
# Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
18
from cStringIO import StringIO
30
class ExpectShelver(shelf_ui.Shelver):
31
"""A variant of Shelver that intercepts console activity, for testing."""
33
def __init__(self, work_tree, target_tree, diff_writer=None,
34
auto=False, auto_apply=False, file_list=None, message=None,
35
destroy=False, reporter=None):
36
shelf_ui.Shelver.__init__(self, work_tree, target_tree, diff_writer,
37
auto, auto_apply, file_list, message,
38
destroy, reporter=reporter)
40
self.diff_writer = StringIO()
42
def expect(self, prompt, response):
43
self.expected.append((prompt, response))
45
def prompt(self, message):
47
prompt, response = self.expected.pop(0)
49
raise AssertionError('Unexpected prompt: %s' % message)
51
raise AssertionError('Wrong prompt: %s' % message)
55
LINES_AJ = 'a\nb\nc\nd\ne\nf\ng\nh\ni\nj\n'
58
LINES_ZY = 'z\nb\nc\nd\ne\nf\ng\nh\ni\ny\n'
61
LINES_AY = 'a\nb\nc\nd\ne\nf\ng\nh\ni\ny\n'
64
class TestShelver(tests.TestCaseWithTransport):
66
def create_shelvable_tree(self):
67
tree = self.make_branch_and_tree('tree')
68
self.build_tree_contents([('tree/foo', LINES_AJ)])
69
tree.add('foo', 'foo-id')
70
tree.commit('added foo')
71
self.build_tree_contents([('tree/foo', LINES_ZY)])
74
def test_unexpected_prompt_failure(self):
75
self.thisFailsStrictLockCheck()
76
tree = self.create_shelvable_tree()
77
shelver = ExpectShelver(tree, tree.basis_tree())
78
e = self.assertRaises(AssertionError, shelver.run)
79
self.assertEqual('Unexpected prompt: Shelve? [yNfq?]', str(e))
81
def test_wrong_prompt_failure(self):
82
self.thisFailsStrictLockCheck()
83
tree = self.create_shelvable_tree()
84
shelver = ExpectShelver(tree, tree.basis_tree())
85
shelver.expect('foo', 'y')
86
e = self.assertRaises(AssertionError, shelver.run)
87
self.assertEqual('Wrong prompt: Shelve? [yNfq?]', str(e))
89
def test_shelve_not_diff(self):
90
self.thisFailsStrictLockCheck()
91
tree = self.create_shelvable_tree()
92
shelver = ExpectShelver(tree, tree.basis_tree())
93
shelver.expect('Shelve? [yNfq?]', 'n')
94
shelver.expect('Shelve? [yNfq?]', 'n')
95
# No final shelving prompt because no changes were selected
97
self.assertFileEqual(LINES_ZY, 'tree/foo')
99
def test_shelve_diff_no(self):
100
self.thisFailsStrictLockCheck()
101
tree = self.create_shelvable_tree()
102
shelver = ExpectShelver(tree, tree.basis_tree())
103
shelver.expect('Shelve? [yNfq?]', 'y')
104
shelver.expect('Shelve? [yNfq?]', 'y')
105
shelver.expect('Shelve 2 change(s)? [yNfq?]', 'n')
107
self.assertFileEqual(LINES_ZY, 'tree/foo')
109
def test_shelve_diff(self):
110
self.thisFailsStrictLockCheck()
111
tree = self.create_shelvable_tree()
112
shelver = ExpectShelver(tree, tree.basis_tree())
113
shelver.expect('Shelve? [yNfq?]', 'y')
114
shelver.expect('Shelve? [yNfq?]', 'y')
115
shelver.expect('Shelve 2 change(s)? [yNfq?]', 'y')
117
self.assertFileEqual(LINES_AJ, 'tree/foo')
119
def test_shelve_one_diff(self):
120
self.thisFailsStrictLockCheck()
121
tree = self.create_shelvable_tree()
122
shelver = ExpectShelver(tree, tree.basis_tree())
123
shelver.expect('Shelve? [yNfq?]', 'y')
124
shelver.expect('Shelve? [yNfq?]', 'n')
125
shelver.expect('Shelve 1 change(s)? [yNfq?]', 'y')
127
self.assertFileEqual(LINES_AY, 'tree/foo')
129
def test_shelve_binary_change(self):
130
self.thisFailsStrictLockCheck()
131
tree = self.create_shelvable_tree()
132
self.build_tree_contents([('tree/foo', '\x00')])
133
shelver = ExpectShelver(tree, tree.basis_tree())
134
shelver.expect('Shelve binary changes? [yNfq?]', 'y')
135
shelver.expect('Shelve 1 change(s)? [yNfq?]', 'y')
137
self.assertFileEqual(LINES_AJ, 'tree/foo')
139
def test_shelve_rename(self):
140
self.thisFailsStrictLockCheck()
141
tree = self.create_shelvable_tree()
142
tree.rename_one('foo', 'bar')
143
shelver = ExpectShelver(tree, tree.basis_tree())
144
shelver.expect('Shelve renaming "foo" => "bar"? [yNfq?]', 'y')
145
shelver.expect('Shelve? [yNfq?]', 'y')
146
shelver.expect('Shelve? [yNfq?]', 'y')
147
shelver.expect('Shelve 3 change(s)? [yNfq?]', 'y')
149
self.assertFileEqual(LINES_AJ, 'tree/foo')
151
def test_shelve_deletion(self):
152
self.thisFailsStrictLockCheck()
153
tree = self.create_shelvable_tree()
154
os.unlink('tree/foo')
155
shelver = ExpectShelver(tree, tree.basis_tree())
156
shelver.expect('Shelve removing file "foo"? [yNfq?]', 'y')
157
shelver.expect('Shelve 1 change(s)? [yNfq?]', 'y')
159
self.assertFileEqual(LINES_AJ, 'tree/foo')
161
def test_shelve_creation(self):
162
self.thisFailsStrictLockCheck()
163
tree = self.make_branch_and_tree('tree')
164
tree.commit('add tree root')
165
self.build_tree(['tree/foo'])
167
shelver = ExpectShelver(tree, tree.basis_tree())
168
shelver.expect('Shelve adding file "foo"? [yNfq?]', 'y')
169
shelver.expect('Shelve 1 change(s)? [yNfq?]', 'y')
171
self.failIfExists('tree/foo')
173
def test_shelve_kind_change(self):
174
self.thisFailsStrictLockCheck()
175
tree = self.create_shelvable_tree()
176
os.unlink('tree/foo')
178
shelver = ExpectShelver(tree, tree.basis_tree())
179
shelver.expect('Shelve changing "foo" from file to directory? [yNfq?]',
181
shelver.expect('Shelve 1 change(s)? [yNfq?]', 'y')
183
def test_shelve_modify_target(self):
184
self.thisFailsStrictLockCheck()
185
self.requireFeature(tests.SymlinkFeature)
186
tree = self.create_shelvable_tree()
187
os.symlink('bar', 'tree/baz')
188
tree.add('baz', 'baz-id')
189
tree.commit("Add symlink")
190
os.unlink('tree/baz')
191
os.symlink('vax', 'tree/baz')
192
shelver = ExpectShelver(tree, tree.basis_tree())
193
shelver.expect('Shelve changing target of "baz" from "bar" to '
194
'"vax"? [yNfq?]', 'y')
195
shelver.expect('Shelve 1 change(s)? [yNfq?]', 'y')
197
self.assertEqual('bar', os.readlink('tree/baz'))
199
def test_shelve_finish(self):
200
self.thisFailsStrictLockCheck()
201
tree = self.create_shelvable_tree()
202
shelver = ExpectShelver(tree, tree.basis_tree())
203
shelver.expect('Shelve? [yNfq?]', 'f')
204
shelver.expect('Shelve 2 change(s)? [yNfq?]', 'y')
206
self.assertFileEqual(LINES_AJ, 'tree/foo')
208
def test_shelve_quit(self):
209
self.thisFailsStrictLockCheck()
210
tree = self.create_shelvable_tree()
211
shelver = ExpectShelver(tree, tree.basis_tree())
212
shelver.expect('Shelve? [yNfq?]', 'q')
213
self.assertRaises(errors.UserAbort, shelver.run)
214
self.assertFileEqual(LINES_ZY, 'tree/foo')
216
def test_shelve_all(self):
217
self.thisFailsStrictLockCheck()
218
tree = self.create_shelvable_tree()
219
ExpectShelver.from_args(sys.stdout, all=True, directory='tree').run()
220
self.assertFileEqual(LINES_AJ, 'tree/foo')
222
def test_shelve_filename(self):
223
self.thisFailsStrictLockCheck()
224
tree = self.create_shelvable_tree()
225
self.build_tree(['tree/bar'])
227
shelver = ExpectShelver(tree, tree.basis_tree(), file_list=['bar'])
228
shelver.expect('Shelve adding file "bar"? [yNfq?]', 'y')
229
shelver.expect('Shelve 1 change(s)? [yNfq?]', 'y')
232
def test_shelve_help(self):
233
self.thisFailsStrictLockCheck()
234
tree = self.create_shelvable_tree()
235
shelver = ExpectShelver(tree, tree.basis_tree())
236
shelver.expect('Shelve? [yNfq?]', '?')
237
shelver.expect('Shelve? [(y)es, (N)o, (f)inish, or (q)uit]', 'f')
238
shelver.expect('Shelve 2 change(s)? [yNfq?]', 'y')
241
def test_shelve_distroy(self):
242
self.thisFailsStrictLockCheck()
243
tree = self.create_shelvable_tree()
244
shelver = shelf_ui.Shelver.from_args(sys.stdout, all=True,
245
directory='tree', destroy=True)
247
self.assertIs(None, tree.get_shelf_manager().last_shelf())
248
self.assertFileEqual(LINES_AJ, 'tree/foo')
251
def shelve_all(tree, target_revision_id):
254
target = tree.branch.repository.revision_tree(target_revision_id)
255
shelver = shelf_ui.Shelver(tree, target, auto=True,
261
def test_shelve_old_root_deleted(self):
262
tree1 = self.make_branch_and_tree('tree1')
263
tree1.commit('add root')
264
tree2 = self.make_branch_and_tree('tree2')
265
rev2 = tree2.commit('add root')
266
tree1.merge_from_branch(tree2.branch,
267
from_revision=revision.NULL_REVISION)
268
tree1.commit('Replaced root entry')
269
# This is essentially assertNotRaises(InconsistentDelta)
270
self.expectFailure('Cannot shelve replacing a root entry',
271
self.assertRaises, AssertionError,
272
self.assertRaises, errors.InconsistentDelta,
273
self.shelve_all, tree1, rev2)
275
def test_shelve_split(self):
276
outer_tree = self.make_branch_and_tree('outer')
277
outer_tree.commit('Add root')
278
inner_tree = self.make_branch_and_tree('outer/inner')
279
rev2 = inner_tree.commit('Add root')
280
outer_tree.subsume(inner_tree)
281
# This is essentially assertNotRaises(ValueError).
282
# The ValueError is 'None is not a valid file id'.
283
self.expectFailure('Cannot shelve a join back to the inner tree.',
284
self.assertRaises, AssertionError,
285
self.assertRaises, ValueError, self.shelve_all,
289
class TestApplyReporter(TestShelver):
291
def test_shelve_not_diff(self):
292
self.thisFailsStrictLockCheck()
293
tree = self.create_shelvable_tree()
294
shelver = ExpectShelver(tree, tree.basis_tree(),
295
reporter=shelf_ui.ApplyReporter())
296
shelver.expect('Apply change? [yNfq?]', 'n')
297
shelver.expect('Apply change? [yNfq?]', 'n')
298
# No final shelving prompt because no changes were selected
300
self.assertFileEqual(LINES_ZY, 'tree/foo')
302
def test_shelve_diff_no(self):
303
self.thisFailsStrictLockCheck()
304
tree = self.create_shelvable_tree()
305
shelver = ExpectShelver(tree, tree.basis_tree(),
306
reporter=shelf_ui.ApplyReporter())
307
shelver.expect('Apply change? [yNfq?]', 'y')
308
shelver.expect('Apply change? [yNfq?]', 'y')
309
shelver.expect('Apply 2 change(s)? [yNfq?]', 'n')
311
self.assertFileEqual(LINES_ZY, 'tree/foo')
313
def test_shelve_diff(self):
314
self.thisFailsStrictLockCheck()
315
tree = self.create_shelvable_tree()
316
shelver = ExpectShelver(tree, tree.basis_tree(),
317
reporter=shelf_ui.ApplyReporter())
318
shelver.expect('Apply change? [yNfq?]', 'y')
319
shelver.expect('Apply change? [yNfq?]', 'y')
320
shelver.expect('Apply 2 change(s)? [yNfq?]', 'y')
322
self.assertFileEqual(LINES_AJ, 'tree/foo')
324
def test_shelve_binary_change(self):
325
self.thisFailsStrictLockCheck()
326
tree = self.create_shelvable_tree()
327
self.build_tree_contents([('tree/foo', '\x00')])
328
shelver = ExpectShelver(tree, tree.basis_tree(),
329
reporter=shelf_ui.ApplyReporter())
330
shelver.expect('Apply binary changes? [yNfq?]', 'y')
331
shelver.expect('Apply 1 change(s)? [yNfq?]', 'y')
333
self.assertFileEqual(LINES_AJ, 'tree/foo')
335
def test_shelve_rename(self):
336
self.thisFailsStrictLockCheck()
337
tree = self.create_shelvable_tree()
338
tree.rename_one('foo', 'bar')
339
shelver = ExpectShelver(tree, tree.basis_tree(),
340
reporter=shelf_ui.ApplyReporter())
341
shelver.expect('Rename "bar" => "foo"? [yNfq?]', 'y')
342
shelver.expect('Apply change? [yNfq?]', 'y')
343
shelver.expect('Apply change? [yNfq?]', 'y')
344
shelver.expect('Apply 3 change(s)? [yNfq?]', 'y')
346
self.assertFileEqual(LINES_AJ, 'tree/foo')
348
def test_shelve_deletion(self):
349
self.thisFailsStrictLockCheck()
350
tree = self.create_shelvable_tree()
351
os.unlink('tree/foo')
352
shelver = ExpectShelver(tree, tree.basis_tree(),
353
reporter=shelf_ui.ApplyReporter())
354
shelver.expect('Add file "foo"? [yNfq?]', 'y')
355
shelver.expect('Apply 1 change(s)? [yNfq?]', 'y')
357
self.assertFileEqual(LINES_AJ, 'tree/foo')
359
def test_shelve_creation(self):
360
self.thisFailsStrictLockCheck()
361
tree = self.make_branch_and_tree('tree')
362
tree.commit('add tree root')
363
self.build_tree(['tree/foo'])
365
shelver = ExpectShelver(tree, tree.basis_tree(),
366
reporter=shelf_ui.ApplyReporter())
367
shelver.expect('Delete file "foo"? [yNfq?]', 'y')
368
shelver.expect('Apply 1 change(s)? [yNfq?]', 'y')
370
self.failIfExists('tree/foo')
372
def test_shelve_kind_change(self):
373
self.thisFailsStrictLockCheck()
374
tree = self.create_shelvable_tree()
375
os.unlink('tree/foo')
377
shelver = ExpectShelver(tree, tree.basis_tree(),
378
reporter=shelf_ui.ApplyReporter())
379
shelver.expect('Change "foo" from directory to a file? [yNfq?]', 'y')
380
shelver.expect('Apply 1 change(s)? [yNfq?]', 'y')
382
def test_shelve_modify_target(self):
383
self.thisFailsStrictLockCheck()
384
self.requireFeature(tests.SymlinkFeature)
385
tree = self.create_shelvable_tree()
386
os.symlink('bar', 'tree/baz')
387
tree.add('baz', 'baz-id')
388
tree.commit("Add symlink")
389
os.unlink('tree/baz')
390
os.symlink('vax', 'tree/baz')
391
shelver = ExpectShelver(tree, tree.basis_tree(),
392
reporter=shelf_ui.ApplyReporter())
393
shelver.expect('Change target of "baz" from "vax" to "bar"? [yNfq?]',
395
shelver.expect('Apply 1 change(s)? [yNfq?]', 'y')
397
self.assertEqual('bar', os.readlink('tree/baz'))
400
class TestUnshelver(tests.TestCaseWithTransport):
402
def create_tree_with_shelf(self):
403
tree = self.make_branch_and_tree('tree')
404
self.build_tree_contents([('tree/foo', LINES_AJ)])
405
tree.add('foo', 'foo-id')
406
tree.commit('added foo')
407
self.build_tree_contents([('tree/foo', LINES_ZY)])
408
shelf_ui.Shelver(tree, tree.basis_tree(), auto_apply=True,
412
def test_unshelve(self):
413
self.thisFailsStrictLockCheck()
414
tree = self.create_tree_with_shelf()
416
self.addCleanup(tree.unlock)
417
manager = tree.get_shelf_manager()
418
shelf_ui.Unshelver(tree, manager, 1, True, True, True).run()
419
self.assertFileEqual(LINES_ZY, 'tree/foo')
421
def test_unshelve_args(self):
422
self.thisFailsStrictLockCheck()
423
tree = self.create_tree_with_shelf()
424
shelf_ui.Unshelver.from_args(directory='tree').run()
425
self.assertFileEqual(LINES_ZY, 'tree/foo')
426
self.assertIs(None, tree.get_shelf_manager().last_shelf())
428
def test_unshelve_args_dry_run(self):
429
self.thisFailsStrictLockCheck()
430
tree = self.create_tree_with_shelf()
431
shelf_ui.Unshelver.from_args(directory='tree', action='dry-run').run()
432
self.assertFileEqual(LINES_AJ, 'tree/foo')
433
self.assertEqual(1, tree.get_shelf_manager().last_shelf())
435
def test_unshelve_args_delete_only(self):
436
self.thisFailsStrictLockCheck()
437
tree = self.make_branch_and_tree('tree')
438
manager = tree.get_shelf_manager()
439
shelf_file = manager.new_shelf()[1]
441
shelf_file.write('garbage')
444
unshelver = shelf_ui.Unshelver.from_args(directory='tree',
445
action='delete-only')
447
self.assertIs(None, manager.last_shelf())
449
def test_unshelve_args_invalid_shelf_id(self):
450
self.thisFailsStrictLockCheck()
451
tree = self.make_branch_and_tree('tree')
452
manager = tree.get_shelf_manager()
453
shelf_file = manager.new_shelf()[1]
455
shelf_file.write('garbage')
458
self.assertRaises(errors.InvalidShelfId,
459
shelf_ui.Unshelver.from_args, directory='tree',
460
action='delete-only', shelf_id='foo')