236
189
    def test_shelve_quit(self):
 
237
190
        tree = self.create_shelvable_tree()
 
238
 
        tree.lock_tree_write()
 
239
 
        self.addCleanup(tree.unlock)
 
240
191
        shelver = ExpectShelver(tree, tree.basis_tree())
 
241
 
        self.addCleanup(shelver.finalize)
 
242
192
        shelver.expect('Shelve? [yNfq?]', 'q')
 
243
193
        self.assertRaises(errors.UserAbort, shelver.run)
 
244
194
        self.assertFileEqual(LINES_ZY, 'tree/foo')
 
246
196
    def test_shelve_all(self):
 
247
197
        tree = self.create_shelvable_tree()
 
248
 
        shelver = ExpectShelver.from_args(sys.stdout, all=True,
 
 
198
        ExpectShelver.from_args(sys.stdout, all=True, directory='tree').run()
 
254
199
        self.assertFileEqual(LINES_AJ, 'tree/foo')
 
256
201
    def test_shelve_filename(self):
 
257
202
        tree = self.create_shelvable_tree()
 
258
203
        self.build_tree(['tree/bar'])
 
260
 
        tree.lock_tree_write()
 
261
 
        self.addCleanup(tree.unlock)
 
262
205
        shelver = ExpectShelver(tree, tree.basis_tree(), file_list=['bar'])
 
263
 
        self.addCleanup(shelver.finalize)
 
264
206
        shelver.expect('Shelve adding file "bar"? [yNfq?]', 'y')
 
265
207
        shelver.expect('Shelve 1 change(s)? [yNfq?]', 'y')
 
268
210
    def test_shelve_help(self):
 
269
211
        tree = self.create_shelvable_tree()
 
270
 
        tree.lock_tree_write()
 
271
 
        self.addCleanup(tree.unlock)
 
272
212
        shelver = ExpectShelver(tree, tree.basis_tree())
 
273
 
        self.addCleanup(shelver.finalize)
 
274
213
        shelver.expect('Shelve? [yNfq?]', '?')
 
275
214
        shelver.expect('Shelve? [(y)es, (N)o, (f)inish, or (q)uit]', 'f')
 
276
215
        shelver.expect('Shelve 2 change(s)? [yNfq?]', 'y')
 
279
 
    def test_shelve_destroy(self):
 
 
218
    def test_shelve_distroy(self):
 
280
219
        tree = self.create_shelvable_tree()
 
281
220
        shelver = shelf_ui.Shelver.from_args(sys.stdout, all=True,
 
282
221
                                             directory='tree', destroy=True)
 
283
 
        self.addCleanup(shelver.finalize)
 
285
223
        self.assertIs(None, tree.get_shelf_manager().last_shelf())
 
286
224
        self.assertFileEqual(LINES_AJ, 'tree/foo')
 
289
 
    def shelve_all(tree, target_revision_id):
 
292
 
            target = tree.branch.repository.revision_tree(target_revision_id)
 
293
 
            shelver = shelf_ui.Shelver(tree, target, auto=True,
 
302
 
    def test_shelve_old_root_deleted(self):
 
303
 
        tree1 = self.make_branch_and_tree('tree1')
 
304
 
        tree1.commit('add root')
 
305
 
        tree2 = self.make_branch_and_tree('tree2')
 
306
 
        rev2 = tree2.commit('add root')
 
307
 
        tree1.merge_from_branch(tree2.branch,
 
308
 
                                from_revision=revision.NULL_REVISION)
 
309
 
        tree1.commit('Replaced root entry')
 
310
 
        # This is essentially assertNotRaises(InconsistentDelta)
 
311
 
        self.expectFailure('Cannot shelve replacing a root entry',
 
312
 
                           self.assertRaises, AssertionError,
 
313
 
                           self.assertRaises, errors.InconsistentDelta,
 
314
 
                           self.shelve_all, tree1, rev2)
 
316
 
    def test_shelve_split(self):
 
317
 
        outer_tree = self.make_branch_and_tree('outer')
 
318
 
        outer_tree.commit('Add root')
 
319
 
        inner_tree = self.make_branch_and_tree('outer/inner')
 
320
 
        rev2 = inner_tree.commit('Add root')
 
321
 
        outer_tree.subsume(inner_tree)
 
322
 
        # This is essentially assertNotRaises(ValueError).
 
323
 
        # The ValueError is 'None is not a valid file id'.
 
324
 
        self.expectFailure('Cannot shelve a join back to the inner tree.',
 
325
 
                           self.assertRaises, AssertionError,
 
326
 
                           self.assertRaises, ValueError, self.shelve_all,
 
330
 
class TestApplyReporter(TestShelver):
 
332
 
    def test_shelve_not_diff(self):
 
333
 
        tree = self.create_shelvable_tree()
 
334
 
        tree.lock_tree_write()
 
335
 
        self.addCleanup(tree.unlock)
 
336
 
        shelver = ExpectShelver(tree, tree.basis_tree(),
 
337
 
                                reporter=shelf_ui.ApplyReporter())
 
338
 
        self.addCleanup(shelver.finalize)
 
339
 
        shelver.expect('Apply change? [yNfq?]', 'n')
 
340
 
        shelver.expect('Apply change? [yNfq?]', 'n')
 
341
 
        # No final shelving prompt because no changes were selected
 
343
 
        self.assertFileEqual(LINES_ZY, 'tree/foo')
 
345
 
    def test_shelve_diff_no(self):
 
346
 
        tree = self.create_shelvable_tree()
 
347
 
        tree.lock_tree_write()
 
348
 
        self.addCleanup(tree.unlock)
 
349
 
        shelver = ExpectShelver(tree, tree.basis_tree(),
 
350
 
                                reporter=shelf_ui.ApplyReporter())
 
351
 
        self.addCleanup(shelver.finalize)
 
352
 
        shelver.expect('Apply change? [yNfq?]', 'y')
 
353
 
        shelver.expect('Apply change? [yNfq?]', 'y')
 
354
 
        shelver.expect('Apply 2 change(s)? [yNfq?]', 'n')
 
356
 
        self.assertFileEqual(LINES_ZY, 'tree/foo')
 
358
 
    def test_shelve_diff(self):
 
359
 
        tree = self.create_shelvable_tree()
 
360
 
        tree.lock_tree_write()
 
361
 
        self.addCleanup(tree.unlock)
 
362
 
        shelver = ExpectShelver(tree, tree.basis_tree(),
 
363
 
                                reporter=shelf_ui.ApplyReporter())
 
364
 
        self.addCleanup(shelver.finalize)
 
365
 
        shelver.expect('Apply change? [yNfq?]', 'y')
 
366
 
        shelver.expect('Apply change? [yNfq?]', 'y')
 
367
 
        shelver.expect('Apply 2 change(s)? [yNfq?]', 'y')
 
369
 
        self.assertFileEqual(LINES_AJ, 'tree/foo')
 
371
 
    def test_shelve_binary_change(self):
 
372
 
        tree = self.create_shelvable_tree()
 
373
 
        self.build_tree_contents([('tree/foo', '\x00')])
 
374
 
        tree.lock_tree_write()
 
375
 
        self.addCleanup(tree.unlock)
 
376
 
        shelver = ExpectShelver(tree, tree.basis_tree(),
 
377
 
                                reporter=shelf_ui.ApplyReporter())
 
378
 
        self.addCleanup(shelver.finalize)
 
379
 
        shelver.expect('Apply binary changes? [yNfq?]', 'y')
 
380
 
        shelver.expect('Apply 1 change(s)? [yNfq?]', 'y')
 
382
 
        self.assertFileEqual(LINES_AJ, 'tree/foo')
 
384
 
    def test_shelve_rename(self):
 
385
 
        tree = self.create_shelvable_tree()
 
386
 
        tree.rename_one('foo', 'bar')
 
387
 
        tree.lock_tree_write()
 
388
 
        self.addCleanup(tree.unlock)
 
389
 
        shelver = ExpectShelver(tree, tree.basis_tree(),
 
390
 
                                reporter=shelf_ui.ApplyReporter())
 
391
 
        self.addCleanup(shelver.finalize)
 
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')
 
397
 
        self.assertFileEqual(LINES_AJ, 'tree/foo')
 
399
 
    def test_shelve_deletion(self):
 
400
 
        tree = self.create_shelvable_tree()
 
401
 
        os.unlink('tree/foo')
 
402
 
        tree.lock_tree_write()
 
403
 
        self.addCleanup(tree.unlock)
 
404
 
        shelver = ExpectShelver(tree, tree.basis_tree(),
 
405
 
                                reporter=shelf_ui.ApplyReporter())
 
406
 
        self.addCleanup(shelver.finalize)
 
407
 
        shelver.expect('Add file "foo"? [yNfq?]', 'y')
 
408
 
        shelver.expect('Apply 1 change(s)? [yNfq?]', 'y')
 
410
 
        self.assertFileEqual(LINES_AJ, 'tree/foo')
 
412
 
    def test_shelve_creation(self):
 
413
 
        tree = self.make_branch_and_tree('tree')
 
414
 
        tree.commit('add tree root')
 
415
 
        self.build_tree(['tree/foo'])
 
417
 
        tree.lock_tree_write()
 
418
 
        self.addCleanup(tree.unlock)
 
419
 
        shelver = ExpectShelver(tree, tree.basis_tree(),
 
420
 
                                reporter=shelf_ui.ApplyReporter())
 
421
 
        self.addCleanup(shelver.finalize)
 
422
 
        shelver.expect('Delete file "foo"? [yNfq?]', 'y')
 
423
 
        shelver.expect('Apply 1 change(s)? [yNfq?]', 'y')
 
425
 
        self.failIfExists('tree/foo')
 
427
 
    def test_shelve_kind_change(self):
 
428
 
        tree = self.create_shelvable_tree()
 
429
 
        os.unlink('tree/foo')
 
431
 
        tree.lock_tree_write()
 
432
 
        self.addCleanup(tree.unlock)
 
433
 
        shelver = ExpectShelver(tree, tree.basis_tree(),
 
434
 
                               reporter=shelf_ui.ApplyReporter())
 
435
 
        self.addCleanup(shelver.finalize)
 
436
 
        shelver.expect('Change "foo" from directory to a file? [yNfq?]', 'y')
 
437
 
        shelver.expect('Apply 1 change(s)? [yNfq?]', 'y')
 
439
 
    def test_shelve_modify_target(self):
 
440
 
        self.requireFeature(tests.SymlinkFeature)
 
441
 
        tree = self.create_shelvable_tree()
 
442
 
        os.symlink('bar', 'tree/baz')
 
443
 
        tree.add('baz', 'baz-id')
 
444
 
        tree.commit("Add symlink")
 
445
 
        os.unlink('tree/baz')
 
446
 
        os.symlink('vax', 'tree/baz')
 
447
 
        tree.lock_tree_write()
 
448
 
        self.addCleanup(tree.unlock)
 
449
 
        shelver = ExpectShelver(tree, tree.basis_tree(),
 
450
 
                                reporter=shelf_ui.ApplyReporter())
 
451
 
        self.addCleanup(shelver.finalize)
 
452
 
        shelver.expect('Change target of "baz" from "vax" to "bar"? [yNfq?]',
 
454
 
        shelver.expect('Apply 1 change(s)? [yNfq?]', 'y')
 
456
 
        self.assertEqual('bar', os.readlink('tree/baz'))
 
459
227
class TestUnshelver(tests.TestCaseWithTransport):
 
461
229
    def create_tree_with_shelf(self):
 
462
230
        tree = self.make_branch_and_tree('tree')
 
465
 
            self.build_tree_contents([('tree/foo', LINES_AJ)])
 
466
 
            tree.add('foo', 'foo-id')
 
467
 
            tree.commit('added foo')
 
468
 
            self.build_tree_contents([('tree/foo', LINES_ZY)])
 
469
 
            shelver = shelf_ui.Shelver(tree, tree.basis_tree(),
 
470
 
                                       auto_apply=True, auto=True)
 
 
231
        self.build_tree_contents([('tree/foo', LINES_AJ)])
 
 
232
        tree.add('foo', 'foo-id')
 
 
233
        tree.commit('added foo')
 
 
234
        self.build_tree_contents([('tree/foo', LINES_ZY)])
 
 
235
        shelf_ui.Shelver(tree, tree.basis_tree(), auto_apply=True,
 
479
239
    def test_unshelve(self):