37
bzrlib.tests.TestCaseWithTransport.setUp(self)
38
command._testing = True
39
self.addCleanup(command._stop_testing)
41
34
def _check_diff(self, diff=DIFF_1, filename='test_file'):
42
35
old_tree = self.tree.basis_tree()
43
36
old_tree.lock_read()
49
42
new_date = _patch_header_date(self.tree,
50
43
self.tree.inventory.path2id(filename), filename)
54
keys = { 'filename' : filename , 'old_date': old_date,
46
keys = { 'filename' : filename , 'old_date': old_date,
55
47
'new_date': new_date}
56
48
hdr = self.DIFF_HEADER % keys
58
self.assertEqual(self.run_bzr('diff', retcode=1)[0], hdr + diff + '\n')
50
self.assertEqual(self.capture('diff', retcode=1), hdr + diff + '\n')
60
52
def _check_shelf(self, idx, diff=DIFF_1, filename='test_file',
83
75
def __test_loop(self, count):
84
76
self.tree = self.make_branch_and_tree('.')
85
77
self.__create_and_add_test_file()
86
for counter in range(count):
87
82
# Modify the test file
88
# write in binary mode because on win32 line-endings should be
83
# write in binary mode because on win32 line-endings should be LF
90
84
f = file('test_file', 'wb')
91
85
f.write(self.MODIFIED)
96
self.tree.lock_write()
98
new_date = _patch_header_date(self.tree,
99
self.tree.inventory.path2id('test_file'), 'test_file')
90
new_date = _patch_header_date(self.tree,
91
self.tree.inventory.path2id('test_file'), 'test_file')
103
93
# Shelve the changes
104
self.run_bzr('shelve1 --all', retcode=0)
94
self.run_bzr('shelve', '--all', retcode=0)
106
96
# Make sure there is no diff anymore
107
self.assertEqual(self.run_bzr('diff', retcode=0)[0], '')
97
self.assertEqual(self.capture('diff', retcode=0), '')
109
99
# Make sure the file is actually back the way it was
110
100
self.assertEqual(file('test_file').read(), self.ORIGINAL)
112
102
self._check_shelf('00', new_date=new_date)
115
self.run_bzr('unshelve1 --all', retcode=0)
105
self.run_bzr('unshelve', '--all', retcode=0)
117
107
self._check_diff()
128
118
self.__create_and_add_test_file()
130
120
# Shelve the changes
131
self.run_bzr('shelve1 --all', retcode=3)
121
self.run_bzr('shelve', '--all', retcode=3)
133
123
if os.path.exists(os.path.join(self.tree.branch.base,
134
124
'.shelf/shelves/default/00')):
152
142
self.tree.commit(message='update test_file')
154
144
# Shelve the changes
155
self.run_bzr('shelve1 --all -r 1', retcode=0)
145
self.run_bzr('shelve', '--all', '-r', '1', retcode=0)
157
147
self._check_diff(self.DIFF_2)
160
150
self.assertEqual(file('test_file').read(), self.ORIGINAL)
163
self.run_bzr('unshelve1 --all', retcode=0)
153
self.run_bzr('unshelve', '--all', retcode=0)
165
155
# Make sure the file is back the way it should be
166
156
self.assertEqual(file('test_file').read(), self.MODIFIED)
168
158
def test_shelf_with_two_revisions(self):
169
159
self.tree = self.make_branch_and_tree('.')
171
stdout, stderr = self.run_bzr('shelve1 --all -r 1..2', retcode=None)
161
cmd = 'shelve --all -r 1..2'
162
(stdout, stderr) = self.run_bzr_captured(cmd.split(), retcode=None)
173
164
self.assertEqual(stderr.split('\n')[0],
174
165
'bzr: ERROR: shelve only accepts a single revision parameter.')
183
174
self.build_tree_contents([('test_file', 'patch %s\n' % patch)])
185
176
# Shelve the changes
186
self.run_bzr('shelve1 --all', retcode=0)
177
self.run_bzr('shelve', '--all', retcode=0)
188
179
# Make sure there is no diff anymore
189
self.assertEqual(self.run_bzr('diff', retcode=0)[0], '')
180
self.assertEqual(self.capture('diff', retcode=0), '')
191
182
# Check the shelf is right
192
183
shelf = open(os.path.join(self.tree.basedir,
194
185
self.assertTrue('patch %s' % patch in shelf)
196
187
# Check the shown output is right
197
shown = self.run_bzr('shelf1 show %s' % patch, retcode=0)[0]
188
shown = self.capture('shelf show %s' % patch, retcode=0)
198
189
self.assertEqual(shown, shelf)
200
191
def test_shelf_show_multi(self):
210
201
self.assertTrue('patch 00' in shelf)
212
203
# Check the shown output is right
213
shown = self.run_bzr('shelf1 show 00', retcode=0)[0]
204
shown = self.capture('shelf show 00', retcode=0)
214
205
self.assertEqual(shown, shelf)
216
207
def test_shelf_show_unspecified(self):
226
217
self.assertTrue('patch 02' in shelf)
228
219
# Check the shown output is right
229
shown = self.run_bzr('shelf1 show', retcode=0)[0]
220
shown = self.capture('shelf show', retcode=0)
230
221
self.assertEqual(shown, shelf)
232
223
def test_shelf_show_with_no_patch(self):
233
224
self.tree = self.make_branch_and_tree('.')
234
stderr = self.run_bzr('shelf1 show 00', retcode=None)[1]
225
stderr = self.run_bzr_captured(['shelf', 'show', '00'], retcode=None)[1]
235
226
self.assertTrue("Patch '00' doesn't exist on shelf default!" in stderr)
237
228
def test_shelf_unshelve_failure(self):
243
234
file('test_file', 'w').write(self.MODIFIED)
245
236
# Shelve the changes
246
self.run_bzr('shelve1 --all', retcode=0)
237
self.run_bzr('shelve', '--all', retcode=0)
248
239
# Write an unapplyable patch into the shelf
249
240
shelf = open(os.path.join(self.tree.basedir,
254
245
# Unshelve, should fail
255
self.run_bzr('unshelve1 --all', retcode=3)
246
self.run_bzr('unshelve', '--all', retcode=3)
257
248
# Make sure the patch is still there, eventhough it's broken
258
249
shelf = open(os.path.join(self.tree.basedir,
260
251
self.assertEqual(shelf, self.DIFF_2)
262
253
# Working tree should be unchanged
263
diff = self.run_bzr('diff', retcode=0)[0]
254
diff = self.capture('diff', retcode=0)
264
255
self.assertEqual(diff, '')
266
257
def test_shelf_unshelve_failure_two_hunks(self):
274
265
file('test_file2', 'w').write(self.MODIFIED)
276
267
# Shelve the changes
277
self.run_bzr('shelve1 --all', retcode=0)
268
self.run_bzr('shelve', '--all', retcode=0)
279
270
# Put the changes to test_file back, the shelved patch won't apply now
280
271
file('test_file', 'w').write(self.MODIFIED)
281
272
self.tree.commit(message='screw up test_file')
283
274
# Unshelve, should fail
284
self.run_bzr('unshelve1 --all', retcode=3)
275
self.run_bzr('unshelve', '--all', retcode=3)
286
277
# Working tree should be unchanged
287
diff = self.run_bzr('diff', retcode=0)[0]
278
diff = self.capture('diff', retcode=0)
288
279
self.assertEqual(diff, '')
290
281
# Force should succeed and modify test_file2, but leave shelf
291
self.run_bzr('unshelve1 --force --all', retcode=0)
282
self.run_bzr('unshelve', '--force', '--all', retcode=0)
292
283
self.assertEqual(open('test_file2').read(), self.MODIFIED)
293
284
self.assertTrue(os.path.exists('.shelf/shelves/default/00'))
303
294
file('test_file2', 'w').write(self.MODIFIED)
305
296
# Shelve the changes
306
self.run_bzr('shelve1 --all test_file', retcode=0)
307
self.run_bzr('shelve1 --all test_file2', retcode=0)
297
self.run_bzr('shelve', '--all', 'test_file', retcode=0)
298
self.run_bzr('shelve', '--all', 'test_file2', retcode=0)
310
self.run_bzr('unshelve1 --all', retcode=0)
301
self.run_bzr('unshelve', '--all', retcode=0)
312
303
# We should now have 00 and 01~
313
304
self.assertTrue(os.path.exists('.shelf/shelves/default/00'))
314
305
self.assertTrue(os.path.exists('.shelf/shelves/default/01~'))
317
lines = self.run_bzr('shelf1 ls', retcode=0)[0].split('\n')
308
lines = self.capture('shelf ls', retcode=0).split('\n')
318
309
for line in lines:
319
310
self.assertFalse(line.startswith(' 01'))
321
312
# Unshelve, if unshelve is confused by the backup it will fail
322
self.run_bzr('unshelve1 --all', retcode=0)
313
self.run_bzr('unshelve', '--all', retcode=0)
324
315
def test_shelf_delete(self):
325
316
self.tree = self.make_branch_and_tree('.')
342
333
self.tree.unlock()
344
335
# Shelve the changes
345
self.run_bzr('shelve1 --all test_file', retcode=0)
346
self.run_bzr('shelve1 --all test_file2', retcode=0)
336
self.run_bzr('shelve', '--all', 'test_file', retcode=0)
337
self.run_bzr('shelve', '--all', 'test_file2', retcode=0)
348
339
self._check_shelf('00', new_date=new_date)
351
self.run_bzr('shelf1 delete 00', retcode=0)
342
self.run_bzr('shelf', 'delete', '00', retcode=0)
353
344
# We should now have 01 but not 00, but we should have 00~
354
345
self.assertFalse(os.path.exists('.shelf/shelves/default/00'))
359
350
self._check_shelf('00~', new_date=new_date)
362
lines = self.run_bzr('shelf1 ls', retcode=0)[0].split('\n')
353
lines = self.capture('shelf ls', retcode=0).split('\n')
363
354
for line in lines:
364
355
self.assertFalse(line.startswith(' 00'))
366
357
# Unshelve should unshelve 01
367
self.run_bzr('unshelve1 --all', retcode=0)
358
self.run_bzr('unshelve', '--all', retcode=0)
368
359
self.assertEqual(file('test_file2').read(), self.MODIFIED)
370
361
def test_shelf_gaps(self):
371
362
self.tree = self.make_branch_and_tree('.')
372
363
self.__create_and_add_test_file()
373
364
file('test_file', 'w').write(self.MODIFIED)
374
self.run_bzr('shelve1 --all test_file', retcode=0)
365
self.run_bzr('shelve', '--all', 'test_file', retcode=0)
375
366
file('test_file', 'w').write(self.MODIFIED)
376
self.run_bzr('shelve1 --all test_file', retcode=0)
367
self.run_bzr('shelve', '--all', 'test_file', retcode=0)
378
369
# Now delete 00, leaving 01, next shelve should go into 02
379
self.run_bzr('shelf1 delete 0', retcode=0)
370
self.run_bzr('shelf', 'delete', '0', retcode=0)
380
371
self.assertFalse(os.path.exists('.shelf/shelves/default/00'))
381
372
self.assertFalse(os.path.exists('.shelf/shelves/default/02'))
382
373
file('test_file', 'w').write(self.MODIFIED)
383
self.run_bzr('shelve1 --all test_file', retcode=0)
374
self.run_bzr('shelve', '--all', 'test_file', retcode=0)
384
375
self.assertFalse(os.path.exists('.shelf/shelves/default/00'))
385
376
self.assertTrue(os.path.exists('.shelf/shelves/default/02'))
392
383
# Modify then shelve, so we're not upgrading to 00, just for kicks
393
384
file('test_file', 'w').write(self.MODIFIED)
394
self.run_bzr('shelve1 --all test_file', retcode=0)
385
self.run_bzr('shelve', '--all', 'test_file', retcode=0)
396
387
open('.bzr-shelf', 'w').write('First old shelf')
397
388
open('.bzr-shelf-1', 'w').write('Second old shelf')
400
391
# shelve and unshelve should bitch and do nothing
401
392
file('test_file', 'w').write('blah blah blah')
402
self.run_bzr('shelve1 --all', retcode=3)
393
self.run_bzr('shelve', '--all', retcode=3)
403
394
self.assertFalse(os.path.exists('.shelf/shelves/default/01'))
404
395
self.assertEqual(file('test_file').read(), 'blah blah blah')
405
self.run_bzr('unshelve1 --all', retcode=3)
396
self.run_bzr('unshelve', '--all', retcode=3)
406
397
self.assertTrue(os.path.exists('.shelf/shelves/default/00'))
408
399
# Upgrade, make sure it worked
409
self.run_bzr('shelf1 upgrade', retcode=0)
400
self.run_bzr('shelf', 'upgrade', retcode=0)
410
401
self.assertEqual(open('.shelf/shelves/default/01').read(),
411
402
'First old shelf')
412
403
self.assertEqual(open('.shelf/shelves/default/02').read(),
423
414
self.assertFalse(os.path.exists('.bzr-shelf-3'))
425
416
# Shelve should work now
426
self.run_bzr('shelve1 --all', retcode=0)
417
self.run_bzr('shelve', '--all', retcode=0)
428
419
def test_shelf_p1_patch(self):
429
420
self.tree = self.make_branch_and_tree('.')
431
422
self.__create_and_add_test_file()
433
424
# Run a benign shelf command to setup .shelf for us
434
self.run_bzr('shelf1 ls', retcode=0)
425
self.run_bzr('shelf', 'ls', retcode=0)
436
427
old_tree = self.tree.basis_tree()
437
428
old_tree.lock_read()
453
444
open('.shelf/shelves/default/00', 'w').write(diff)
455
446
# This should work
456
self.run_bzr('unshelve1 --all', retcode=0)
447
self.run_bzr('unshelve', '--all', retcode=0)
458
449
self._check_diff()
476
467
# Shelve the changes
477
self.run_bzr('shelve1 --all', retcode=0)
468
self.run_bzr('shelve', '--all', retcode=0)
479
470
# Working tree should be unchanged
480
diff = self.run_bzr('diff', retcode=0)[0]
471
diff = self.capture('diff', retcode=0)
481
472
self.assertEqual(diff, '')
483
474
# Unshelve, should succeed
484
self.run_bzr('unshelve1 --all', retcode=0)
475
self.run_bzr('unshelve', '--all', retcode=0)
486
477
self._check_diff(filename='subdir/test_file')
488
479
# Make sure relative filenames work ok
489
self.run_bzr('shelve1 test_file --all', retcode=0)
480
self.run_bzr('shelve', 'test_file', '--all', retcode=0)
491
482
def test_shelf_shelf_bogus_subcommand(self):
492
483
self.tree = self.make_branch_and_tree('.')
493
self.run_bzr('shelf1 foo', retcode=3) # <- retcode == 3
484
self.run_bzr('shelf', 'foo', retcode=3) # <- retcode == 3
495
486
def test_shelf_OOO_unshelve(self):
496
487
self.tree = self.make_branch_and_tree('.')
505
496
# Shelve the changes
506
497
for i in range(1, 5):
507
self.run_bzr(['shelve1', '--all', 'test_file%d' % i], retcode=0)
498
self.run_bzr('shelve', '--all', 'test_file%d' % i, retcode=0)
509
500
# Check shelving worked
510
501
for i in range(1, 5):
515
506
self.assertTrue(os.path.exists('.shelf/shelves/default/0%d' % i))
518
self.run_bzr('unshelve1 --all 00', retcode=0)
509
self.run_bzr('unshelve', '--all', '00', retcode=0)
519
510
self.assertEqual(file('test_file1').read(), self.MODIFIED)
522
lines = self.run_bzr('shelf1 ls', retcode=0)[0].split('\n')
513
lines = self.capture('shelf ls', retcode=0).split('\n')
523
514
for line in lines:
524
515
self.assertFalse(line.startswith(' 00'))
526
517
# Check we can reshelve once we've unshelved out of order, should be 04
527
518
self.assertFalse(os.path.exists('.shelf/shelves/default/04'))
528
self.run_bzr('shelve1 --all')
519
self.run_bzr('shelve', '--all')
529
520
self.assertTrue(os.path.exists('.shelf/shelves/default/04'))
532
text = self.run_bzr('shelf1 ls', retcode=0)[0]
523
text = self.capture('shelf ls', retcode=0)
533
524
for line in text.split('\n'):
534
525
self.assertFalse(line.startswith(' 00'))
536
527
# We now have 01,02,03,04
538
self.run_bzr('unshelve1 --all 02', retcode=0)
529
self.run_bzr('unshelve', '--all', '02', retcode=0)
539
530
self.assertEqual(file('test_file3').read(), self.MODIFIED)
541
532
# Unshelve the default, this is the reshelved 00, hence modifies file 1
542
self.run_bzr('unshelve1 --all', retcode=0)
533
self.run_bzr('unshelve', '--all', retcode=0)
543
534
self.assertEqual(file('test_file1').read(), self.MODIFIED)
545
536
def test_shelf_switch_basic(self):
549
540
# This should go to "default"
550
541
file('test_file', 'w').write(self.MODIFIED)
551
self.run_bzr('shelve1 --all test_file', retcode=0)
542
self.run_bzr('shelve', '--all', 'test_file', retcode=0)
553
544
# Switch to "other"
554
self.run_bzr('shelf1 switch other', retcode=0)
545
self.run_bzr('shelf', 'switch', 'other', retcode=0)
555
546
file('test_file', 'w').write(self.MODIFIED)
556
self.run_bzr('shelve1 --all test_file', retcode=0)
547
self.run_bzr('shelve', '--all', 'test_file', retcode=0)
558
549
# Check it worked
559
550
self.assertTrue(os.path.exists('.shelf/shelves/default/00'))
561
552
self.assertTrue(os.path.exists('.shelf/shelves/other/00'))
564
self.run_bzr('shelf1 switch default', retcode=0)
555
self.run_bzr('shelf', 'switch', 'default', retcode=0)
565
556
file('test_file', 'w').write(self.MODIFIED)
566
self.run_bzr('shelve1 --all test_file', retcode=0)
557
self.run_bzr('shelve', '--all', 'test_file', retcode=0)
568
559
# Check that worked
569
560
self.assertTrue(os.path.exists('.shelf/shelves/default/01'))
570
561
self.assertFalse(os.path.exists('.shelf/shelves/other/01'))
572
def test_shelf_binary(self):
573
self.tree = self.make_branch_and_tree('.')
574
self.build_tree_contents([('file', '\x00')])
575
self.tree.add('file')
576
self.run_bzr_error(['Changes involve binary files.'], 'shelve1 --all')
578
563
def test_shelf_bad_patch_arg(self):
579
564
self.tree = self.make_branch_and_tree('.')
581
566
# Check the bad arg handling
582
stdout, error = self.run_bzr('unshelve1 01', retcode=3)
567
stdout, error = self.run_bzr_captured(['unshelve', '01'], retcode=3)
583
568
self.assertTrue("Patch '01' doesn't exist on shelf" in error)
585
stdout, error = self.run_bzr('unshelve1 foo', retcode=3)
570
stdout, error = self.run_bzr_captured(['unshelve', 'foo'], retcode=3)
586
571
self.assertTrue("Invalid patch name 'foo'" in error)
588
573
# Hex and is cracky, so it shouldn't work
589
stdout, error = self.run_bzr(['unshelve1', '0x00'], retcode=3)
574
stdout, error = self.run_bzr_captured(['unshelve', '0x00'], retcode=3)
590
575
self.assertTrue("Invalid patch name '0x00'" in error)
592
577
def test_color_hunk_selector(self):
624
609
f = file('test_file', 'wb')
625
610
f.write(self.MODIFIED)
627
stdout, error = self.run_bzr('shelve1 --all --no-color')
628
stdout, error = self.run_bzr('unshelve1 --all --no-color')
630
def test_shelf_help(self):
631
self.assertContainsRe(cmd_shelf1().help(),
632
'list\n.*List the patches on the current shelf')
634
def test_show_empty_shelf(self):
635
self.tree = self.make_branch_and_tree('.')
636
self.run_bzr_error(('No patches on shelf.',), 'shelf1 show')
612
stdout, error = self.run_bzr_captured(['shelve', '--all',
614
stdout, error = self.run_bzr_captured(['unshelve', '--all',