~abentley/bzrtools/bzrtools.dev

« back to all changes in this revision

Viewing changes to tests/shelf_tests.py

  • Committer: Aaron Bentley
  • Date: 2006-05-18 15:48:22 UTC
  • Revision ID: abentley@panoramicfeedback.com-20060518154822-7faf0c05951fb95b
Got import working decently

Show diffs side-by-side

added added

removed removed

Lines of Context:
1
1
#!/usr/bin/python
2
2
 
3
 
from bzrlib.diff import _patch_header_date
4
3
import bzrlib.tests
5
4
import os.path
6
5
 
8
7
    ORIGINAL = '\n\nhello test world\n\n'
9
8
    MODIFIED = '\n\ngoodbye test world\n\n'
10
9
    DIFF_HEADER = "=== modified file '%(filename)s'\n"
11
 
    DIFF_1 = """--- %(filename)s\t%(old_date)s
12
 
+++ %(filename)s\t%(new_date)s
 
10
    DIFF_1 = """--- %(filename)s\t
 
11
+++ %(filename)s\t
13
12
@@ -1,4 +1,4 @@
14
13
 
15
14
 
17
16
+goodbye test world
18
17
 
19
18
"""
20
 
    DIFF_2 = """--- test_file\t%(old_date)s
21
 
+++ test_file\t%(new_date)s
 
19
    DIFF_2 = """--- test_file\t
 
20
+++ test_file\t
22
21
@@ -1,4 +1,4 @@
23
22
 
24
23
 
27
26
 
28
27
"""
29
28
    def _check_diff(self, diff=DIFF_1, filename='test_file'):
30
 
        old_tree = self.tree.basis_tree()
31
 
        old_date = _patch_header_date(old_tree, 
32
 
                                      old_tree.inventory.path2id(filename),
33
 
                                      filename)
34
 
        new_date = _patch_header_date(self.tree, 
35
 
                                      self.tree.inventory.path2id(filename),
36
 
                                      filename)
37
 
        keys = { 'filename' : filename , 'old_date': old_date, 
38
 
                 'new_date': new_date}
 
29
        keys = { 'filename' : filename }
39
30
        hdr  = self.DIFF_HEADER % keys
40
31
        diff = diff % keys
41
32
        self.assertEqual(self.capture('diff', retcode=1), hdr + diff + '\n')
42
33
 
43
 
    def _check_shelf(self, idx, diff=DIFF_1, filename='test_file',
44
 
                     new_date=None):
45
 
        old_tree = self.tree.basis_tree()
46
 
        old_date = _patch_header_date(old_tree, 
47
 
                                      old_tree.inventory.path2id(filename),
48
 
                                      filename)
49
 
        diff = diff % { 'filename' : filename, 'old_date': old_date,
50
 
                        'new_date': new_date}
51
 
        shelf = open(os.path.join(self.tree.basedir,
 
34
    def _check_shelf(self, idx, diff=DIFF_1, filename='test_file'):
 
35
        diff = diff % { 'filename' : filename }
 
36
        shelf = open(os.path.join(self.tree.branch.base,
52
37
                '.shelf/shelves/default/' + idx)).read()
53
38
        shelf = shelf[shelf.index('\n') + 1:] # skip the message
54
39
        self.assertEqual(shelf, diff)
67
52
            count -= 1
68
53
 
69
54
            # Modify the test file
70
 
            # write in binary mode because on win32 line-endings should be LF
71
 
            f = file('test_file', 'wb')
72
 
            f.write(self.MODIFIED)
73
 
            f.close()
 
55
            file('test_file', 'w').write(self.MODIFIED)
74
56
 
75
57
            self._check_diff()
76
 
            
77
 
            new_date = _patch_header_date(self.tree, 
78
 
                self.tree.inventory.path2id('test_file'), 'test_file')
79
58
 
80
59
            # Shelve the changes
81
60
            self.run_bzr('shelve', '--all', retcode=0)
86
65
            # Make sure the file is actually back the way it was
87
66
            self.assertEqual(file('test_file').read(), self.ORIGINAL)
88
67
 
89
 
            self._check_shelf('00', new_date=new_date)
 
68
            self._check_shelf('00')
90
69
 
91
70
            # Unshelve
92
71
            self.run_bzr('unshelve', '--all', retcode=0)
94
73
            self._check_diff()
95
74
 
96
75
            # Check the shelved patch was backed up
97
 
            self._check_shelf('00~', new_date=new_date)
 
76
            self._check_shelf('00~')
98
77
 
99
78
            # Make sure the file is back the way it should be
100
79
            self.assertEqual(file('test_file').read(), self.MODIFIED)
112
91
            self.fail("Shelf exists, but it shouldn't")
113
92
 
114
93
    def __create_and_add_test_file(self, filename='test_file'):
115
 
        # write in binary mode because on win32 line-endings should be LF
116
 
        f = open(filename, 'wb')
 
94
        f = open(filename, 'w')
117
95
        f.write(self.ORIGINAL)
118
96
        f.close()
119
97
        self.tree.add(self.tree.relpath(os.path.join(os.getcwd(), filename)))
167
145
        self.assertEqual(self.capture('diff', retcode=0), '')
168
146
 
169
147
        # Check the shelf is right
170
 
        shelf = open(os.path.join(self.tree.basedir,
 
148
        shelf = open(os.path.join(self.tree.branch.base,
171
149
                    '.shelf/shelves/default', patch)).read()
172
150
        self.assertTrue('patch %s' % patch in shelf)
173
151
 
183
161
        self.__test_show(self.tree, '02')
184
162
 
185
163
        # Now check we can show a previously shelved patch
186
 
        shelf = open(os.path.join(self.tree.basedir,
 
164
        shelf = open(os.path.join(self.tree.branch.base,
187
165
                    '.shelf/shelves/default/00')).read()
188
166
        self.assertTrue('patch 00' in shelf)
189
167
 
191
169
        shown = self.capture('shelf show 00', retcode=0)
192
170
        self.assertEqual(shown, shelf)
193
171
 
194
 
    def test_shelf_show_unspecified(self):
195
 
        self.tree = self.make_branch_and_tree('.')
196
 
        self.__create_and_add_test_file()
197
 
        self.__test_show(self.tree, '00')
198
 
        self.__test_show(self.tree, '01')
199
 
        self.__test_show(self.tree, '02')
200
 
 
201
 
        # Check that not specifying at patch gets us the most recent
202
 
        shelf = open(os.path.join(self.tree.basedir,
203
 
                    '.shelf/shelves/default/02')).read()
204
 
        self.assertTrue('patch 02' in shelf)
205
 
 
206
 
        # Check the shown output is right
207
 
        shown = self.capture('shelf show', retcode=0)
208
 
        self.assertEqual(shown, shelf)
209
 
 
210
172
    def test_shelf_show_with_no_patch(self):
211
173
        self.tree = self.make_branch_and_tree('.')
212
174
        stderr = self.run_bzr_captured(['shelf', 'show', '00'], retcode=None)[1]
224
186
        self.run_bzr('shelve', '--all', retcode=0)
225
187
 
226
188
        # Write an unapplyable patch into the shelf
227
 
        shelf = open(os.path.join(self.tree.basedir,
 
189
        shelf = open(os.path.join(self.tree.branch.base,
228
190
                    '.shelf/shelves/default/00'), 'w')
229
191
        shelf.write(self.DIFF_2)
230
192
        shelf.close()
233
195
        self.run_bzr('unshelve', '--all', retcode=3)
234
196
 
235
197
        # Make sure the patch is still there, eventhough it's broken
236
 
        shelf = open(os.path.join(self.tree.basedir,
 
198
        shelf = open(os.path.join(self.tree.branch.base,
237
199
                    '.shelf/shelves/default/00')).read()
238
200
        self.assertEqual(shelf, self.DIFF_2)
239
201
 
292
254
        self.assertTrue(os.path.exists('.shelf/shelves/default/01~'))
293
255
 
294
256
        # Check ls works
295
 
        lines = self.capture('shelf ls', retcode=0).split('\n')
296
 
        for line in lines:
 
257
        list = self.capture('shelf ls', retcode=0).split('\n')
 
258
        for line in list:
297
259
            self.assertFalse(line.startswith(' 01'))
298
260
 
299
261
        # Unshelve, if unshelve is confused by the backup it will fail
306
268
        self.__create_and_add_test_file(filename='test_file2')
307
269
 
308
270
        # Modify the test files
309
 
        # write in binary mode because on win32 line-endings should be LF
310
 
        f = file('test_file', 'wb')
311
 
        f.write(self.MODIFIED)
312
 
        f.close()
313
 
        f = file('test_file2', 'wb')
314
 
        f.write(self.MODIFIED)
315
 
        f.close()
316
 
        new_date = _patch_header_date(self.tree, 
317
 
            self.tree.inventory.path2id('test_file'), 'test_file')
 
271
        file('test_file', 'w').write(self.MODIFIED)
 
272
        file('test_file2', 'w').write(self.MODIFIED)
318
273
 
319
274
        # Shelve the changes
320
275
        self.run_bzr('shelve', '--all', 'test_file', retcode=0)
321
276
        self.run_bzr('shelve', '--all', 'test_file2', retcode=0)
322
277
 
323
 
        self._check_shelf('00', new_date=new_date)
 
278
        self._check_shelf('00')
324
279
 
325
280
        # Delete 00
326
281
        self.run_bzr('shelf', 'delete', '00', retcode=0)
331
286
        self.assertTrue(os.path.exists('.shelf/shelves/default/01'))
332
287
 
333
288
        # Check the backup is right
334
 
        self._check_shelf('00~', new_date=new_date)
 
289
        self._check_shelf('00~')
335
290
 
336
291
        # Check ls works
337
 
        lines = self.capture('shelf ls', retcode=0).split('\n')
338
 
        for line in lines:
 
292
        list = self.capture('shelf ls', retcode=0).split('\n')
 
293
        for line in list:
339
294
            self.assertFalse(line.startswith(' 00'))
340
295
 
341
296
        # Unshelve should unshelve 01
408
363
        # Run a benign shelf command to setup .shelf for us
409
364
        self.run_bzr('shelf', 'ls', retcode=0)
410
365
 
411
 
        old_tree = self.tree.basis_tree()
412
 
        old_date = _patch_header_date(old_tree, 
413
 
                                      old_tree.inventory.path2id('test_file'),
414
 
                                      'test_file')
415
 
        new_date = _patch_header_date(self.tree, 
416
 
                                      self.tree.inventory.path2id('test_file'),
417
 
                                      'test_file')
418
366
        # Fake a -p0 shelved patch
419
 
        diff = self.DIFF_1 % { 'filename' : 'test_file', 'old_date': old_date,
420
 
                               'new_date' : new_date}
 
367
        diff = self.DIFF_1 % { 'filename' : 'test_file' }
421
368
        diff = diff.replace('--- ', '--- a/')
422
369
        diff = diff.replace('+++ ', '+++ b/')
423
370
        open('.shelf/shelves/default/00', 'w').write(diff)
438
385
        self.__create_and_add_test_file()
439
386
 
440
387
        # Modify the test file
441
 
        # write in binary mode because on win32 line-endings should be LF
442
 
        f = file('test_file', 'wb')
443
 
        f.write(self.MODIFIED)
444
 
        f.close()
 
388
        file('test_file', 'w').write(self.MODIFIED)
445
389
 
446
390
        # Shelve the changes
447
391
        self.run_bzr('shelve', '--all', retcode=0)
461
405
    def test_shelf_shelf_bogus_subcommand(self):
462
406
        self.tree = self.make_branch_and_tree('.')
463
407
        self.run_bzr('shelf', 'foo', retcode=3) # <- retcode == 3
464
 
 
465
 
    def test_shelf_OOO_unshelve(self):
466
 
        self.tree = self.make_branch_and_tree('.')
467
 
 
468
 
        for i in range(1, 5):
469
 
            self.__create_and_add_test_file(filename='test_file%d' % i)
470
 
 
471
 
        # Modify the test files
472
 
        for i in range(1, 5):
473
 
            file('test_file%d' % i, 'w').write(self.MODIFIED)
474
 
 
475
 
        # Shelve the changes
476
 
        for i in range(1, 5):
477
 
            self.run_bzr('shelve', '--all', 'test_file%d' % i, retcode=0)
478
 
 
479
 
        # Check shelving worked
480
 
        for i in range(1, 5):
481
 
            self.assertEqual(file('test_file%d' % i).read(), self.ORIGINAL)
482
 
 
483
 
        # We should now have 00-03
484
 
        for i in range(0, 4):
485
 
            self.assertTrue(os.path.exists('.shelf/shelves/default/0%d' % i))
486
 
 
487
 
        # Unshelve 00
488
 
        self.run_bzr('unshelve', '--all', '00', retcode=0)
489
 
        self.assertEqual(file('test_file1').read(), self.MODIFIED)
490
 
 
491
 
        # Check ls works
492
 
        lines = self.capture('shelf ls', retcode=0).split('\n')
493
 
        for line in lines:
494
 
            self.assertFalse(line.startswith(' 00'))
495
 
 
496
 
        # Check we can reshelve once we've unshelved out of order, should be 04
497
 
        self.assertFalse(os.path.exists('.shelf/shelves/default/04'))
498
 
        self.run_bzr('shelve', '--all')
499
 
        self.assertTrue(os.path.exists('.shelf/shelves/default/04'))
500
 
 
501
 
        # Check ls works
502
 
        text = self.capture('shelf ls', retcode=0)
503
 
        for line in text.split('\n'):
504
 
            self.assertFalse(line.startswith(' 00'))
505
 
 
506
 
        # We now have 01,02,03,04
507
 
        # Unshelve 02
508
 
        self.run_bzr('unshelve', '--all', '02', retcode=0)
509
 
        self.assertEqual(file('test_file3').read(), self.MODIFIED)
510
 
 
511
 
        # Unshelve the default, this is the reshelved 00, hence modifies file 1
512
 
        self.run_bzr('unshelve', '--all', retcode=0)
513
 
        self.assertEqual(file('test_file1').read(), self.MODIFIED)
514
 
 
515
 
    def test_shelf_switch_basic(self):
516
 
        self.tree = self.make_branch_and_tree('.')
517
 
        self.__create_and_add_test_file()
518
 
 
519
 
        # This should go to "default"
520
 
        file('test_file', 'w').write(self.MODIFIED)
521
 
        self.run_bzr('shelve', '--all', 'test_file', retcode=0)
522
 
 
523
 
        # Switch to "other"
524
 
        self.run_bzr('shelf', 'switch', 'other', retcode=0)
525
 
        file('test_file', 'w').write(self.MODIFIED)
526
 
        self.run_bzr('shelve', '--all', 'test_file', retcode=0)
527
 
 
528
 
        # Check it worked
529
 
        self.assertTrue(os.path.exists('.shelf/shelves/default/00'))
530
 
        self.assertFalse(os.path.exists('.shelf/shelves/default/01'))
531
 
        self.assertTrue(os.path.exists('.shelf/shelves/other/00'))
532
 
 
533
 
        # Switch back
534
 
        self.run_bzr('shelf', 'switch', 'default', retcode=0)
535
 
        file('test_file', 'w').write(self.MODIFIED)
536
 
        self.run_bzr('shelve', '--all', 'test_file', retcode=0)
537
 
 
538
 
        # Check that worked
539
 
        self.assertTrue(os.path.exists('.shelf/shelves/default/01'))
540
 
        self.assertFalse(os.path.exists('.shelf/shelves/other/01'))
541
 
 
542
 
    def test_shelf_bad_patch_arg(self):
543
 
        self.tree = self.make_branch_and_tree('.')
544
 
 
545
 
        # Check the bad arg handling
546
 
        stdout, error = self.run_bzr_captured(['unshelve', '01'], retcode=3)
547
 
        self.assertTrue("Patch '01' doesn't exist on shelf" in error)
548
 
 
549
 
        stdout, error = self.run_bzr_captured(['unshelve', 'foo'], retcode=3)
550
 
        self.assertTrue("Invalid patch name 'foo'" in error)
551
 
 
552
 
        # Hex and is cracky, so it shouldn't work
553
 
        stdout, error = self.run_bzr_captured(['unshelve', '0x00'], retcode=3)
554
 
        self.assertTrue("Invalid patch name '0x00'" in error)