~abentley/bzrtools/bzrtools.dev

« back to all changes in this revision

Viewing changes to tests/shelf_tests.py

  • Committer: Aaron Bentley
  • Date: 2006-06-13 02:31:28 UTC
  • mfrom: (0.1.119 shelf)
  • Revision ID: aaron.bentley@utoronto.ca-20060613023128-20f2ff3a212a6ea2
Pull in latest shelf stuff

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
3
4
import bzrlib.tests
4
5
import os.path
5
6
 
7
8
    ORIGINAL = '\n\nhello test world\n\n'
8
9
    MODIFIED = '\n\ngoodbye test world\n\n'
9
10
    DIFF_HEADER = "=== modified file '%(filename)s'\n"
10
 
    DIFF_1 = """--- %(filename)s\t
11
 
+++ %(filename)s\t
 
11
    DIFF_1 = """--- %(filename)s\t%(old_date)s
 
12
+++ %(filename)s\t%(new_date)s
12
13
@@ -1,4 +1,4 @@
13
14
 
14
15
 
16
17
+goodbye test world
17
18
 
18
19
"""
19
 
    DIFF_2 = """--- test_file\t
20
 
+++ test_file\t
 
20
    DIFF_2 = """--- test_file\t%(old_date)s
 
21
+++ test_file\t%(new_date)s
21
22
@@ -1,4 +1,4 @@
22
23
 
23
24
 
26
27
 
27
28
"""
28
29
    def _check_diff(self, diff=DIFF_1, filename='test_file'):
29
 
        keys = { 'filename' : filename }
 
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}
30
39
        hdr  = self.DIFF_HEADER % keys
31
40
        diff = diff % keys
32
41
        self.assertEqual(self.capture('diff', retcode=1), hdr + diff + '\n')
33
42
 
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,
 
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,
37
52
                '.shelf/shelves/default/' + idx)).read()
38
53
        shelf = shelf[shelf.index('\n') + 1:] # skip the message
39
54
        self.assertEqual(shelf, diff)
55
70
            file('test_file', 'w').write(self.MODIFIED)
56
71
 
57
72
            self._check_diff()
 
73
            
 
74
            new_date = _patch_header_date(self.tree, 
 
75
                self.tree.inventory.path2id('test_file'), 'test_file')
58
76
 
59
77
            # Shelve the changes
60
78
            self.run_bzr('shelve', '--all', retcode=0)
65
83
            # Make sure the file is actually back the way it was
66
84
            self.assertEqual(file('test_file').read(), self.ORIGINAL)
67
85
 
68
 
            self._check_shelf('00')
 
86
            self._check_shelf('00', new_date=new_date)
69
87
 
70
88
            # Unshelve
71
89
            self.run_bzr('unshelve', '--all', retcode=0)
73
91
            self._check_diff()
74
92
 
75
93
            # Check the shelved patch was backed up
76
 
            self._check_shelf('00~')
 
94
            self._check_shelf('00~', new_date=new_date)
77
95
 
78
96
            # Make sure the file is back the way it should be
79
97
            self.assertEqual(file('test_file').read(), self.MODIFIED)
145
163
        self.assertEqual(self.capture('diff', retcode=0), '')
146
164
 
147
165
        # Check the shelf is right
148
 
        shelf = open(os.path.join(self.tree.branch.base,
 
166
        shelf = open(os.path.join(self.tree.basedir,
149
167
                    '.shelf/shelves/default', patch)).read()
150
168
        self.assertTrue('patch %s' % patch in shelf)
151
169
 
161
179
        self.__test_show(self.tree, '02')
162
180
 
163
181
        # Now check we can show a previously shelved patch
164
 
        shelf = open(os.path.join(self.tree.branch.base,
 
182
        shelf = open(os.path.join(self.tree.basedir,
165
183
                    '.shelf/shelves/default/00')).read()
166
184
        self.assertTrue('patch 00' in shelf)
167
185
 
169
187
        shown = self.capture('shelf show 00', retcode=0)
170
188
        self.assertEqual(shown, shelf)
171
189
 
 
190
    def test_shelf_show_unspecified(self):
 
191
        self.tree = self.make_branch_and_tree('.')
 
192
        self.__create_and_add_test_file()
 
193
        self.__test_show(self.tree, '00')
 
194
        self.__test_show(self.tree, '01')
 
195
        self.__test_show(self.tree, '02')
 
196
 
 
197
        # Check that not specifying at patch gets us the most recent
 
198
        shelf = open(os.path.join(self.tree.basedir,
 
199
                    '.shelf/shelves/default/02')).read()
 
200
        self.assertTrue('patch 02' in shelf)
 
201
 
 
202
        # Check the shown output is right
 
203
        shown = self.capture('shelf show', retcode=0)
 
204
        self.assertEqual(shown, shelf)
 
205
 
172
206
    def test_shelf_show_with_no_patch(self):
173
207
        self.tree = self.make_branch_and_tree('.')
174
208
        stderr = self.run_bzr_captured(['shelf', 'show', '00'], retcode=None)[1]
186
220
        self.run_bzr('shelve', '--all', retcode=0)
187
221
 
188
222
        # Write an unapplyable patch into the shelf
189
 
        shelf = open(os.path.join(self.tree.branch.base,
 
223
        shelf = open(os.path.join(self.tree.basedir,
190
224
                    '.shelf/shelves/default/00'), 'w')
191
225
        shelf.write(self.DIFF_2)
192
226
        shelf.close()
195
229
        self.run_bzr('unshelve', '--all', retcode=3)
196
230
 
197
231
        # Make sure the patch is still there, eventhough it's broken
198
 
        shelf = open(os.path.join(self.tree.branch.base,
 
232
        shelf = open(os.path.join(self.tree.basedir,
199
233
                    '.shelf/shelves/default/00')).read()
200
234
        self.assertEqual(shelf, self.DIFF_2)
201
235
 
254
288
        self.assertTrue(os.path.exists('.shelf/shelves/default/01~'))
255
289
 
256
290
        # Check ls works
257
 
        list = self.capture('shelf ls', retcode=0).split('\n')
258
 
        for line in list:
 
291
        lines = self.capture('shelf ls', retcode=0).split('\n')
 
292
        for line in lines:
259
293
            self.assertFalse(line.startswith(' 01'))
260
294
 
261
295
        # Unshelve, if unshelve is confused by the backup it will fail
270
304
        # Modify the test files
271
305
        file('test_file', 'w').write(self.MODIFIED)
272
306
        file('test_file2', 'w').write(self.MODIFIED)
 
307
        new_date = _patch_header_date(self.tree, 
 
308
            self.tree.inventory.path2id('test_file'), 'test_file')
273
309
 
274
310
        # Shelve the changes
275
311
        self.run_bzr('shelve', '--all', 'test_file', retcode=0)
276
312
        self.run_bzr('shelve', '--all', 'test_file2', retcode=0)
277
313
 
278
 
        self._check_shelf('00')
 
314
        self._check_shelf('00', new_date=new_date)
279
315
 
280
316
        # Delete 00
281
317
        self.run_bzr('shelf', 'delete', '00', retcode=0)
286
322
        self.assertTrue(os.path.exists('.shelf/shelves/default/01'))
287
323
 
288
324
        # Check the backup is right
289
 
        self._check_shelf('00~')
 
325
        self._check_shelf('00~', new_date=new_date)
290
326
 
291
327
        # Check ls works
292
 
        list = self.capture('shelf ls', retcode=0).split('\n')
293
 
        for line in list:
 
328
        lines = self.capture('shelf ls', retcode=0).split('\n')
 
329
        for line in lines:
294
330
            self.assertFalse(line.startswith(' 00'))
295
331
 
296
332
        # Unshelve should unshelve 01
363
399
        # Run a benign shelf command to setup .shelf for us
364
400
        self.run_bzr('shelf', 'ls', retcode=0)
365
401
 
 
402
        old_tree = self.tree.basis_tree()
 
403
        old_date = _patch_header_date(old_tree, 
 
404
                                      old_tree.inventory.path2id('test_file'),
 
405
                                      'test_file')
 
406
        new_date = _patch_header_date(self.tree, 
 
407
                                      self.tree.inventory.path2id('test_file'),
 
408
                                      'test_file')
366
409
        # Fake a -p0 shelved patch
367
 
        diff = self.DIFF_1 % { 'filename' : 'test_file' }
 
410
        diff = self.DIFF_1 % { 'filename' : 'test_file', 'old_date': old_date,
 
411
                               'new_date' : new_date}
368
412
        diff = diff.replace('--- ', '--- a/')
369
413
        diff = diff.replace('+++ ', '+++ b/')
370
414
        open('.shelf/shelves/default/00', 'w').write(diff)
405
449
    def test_shelf_shelf_bogus_subcommand(self):
406
450
        self.tree = self.make_branch_and_tree('.')
407
451
        self.run_bzr('shelf', 'foo', retcode=3) # <- retcode == 3
 
452
 
 
453
    def test_shelf_OOO_unshelve(self):
 
454
        self.tree = self.make_branch_and_tree('.')
 
455
 
 
456
        for i in range(1, 5):
 
457
            self.__create_and_add_test_file(filename='test_file%d' % i)
 
458
 
 
459
        # Modify the test files
 
460
        for i in range(1, 5):
 
461
            file('test_file%d' % i, 'w').write(self.MODIFIED)
 
462
 
 
463
        # Shelve the changes
 
464
        for i in range(1, 5):
 
465
            self.run_bzr('shelve', '--all', 'test_file%d' % i, retcode=0)
 
466
 
 
467
        # Check shelving worked
 
468
        for i in range(1, 5):
 
469
            self.assertEqual(file('test_file%d' % i).read(), self.ORIGINAL)
 
470
 
 
471
        # We should now have 00-03
 
472
        for i in range(0, 4):
 
473
            self.assertTrue(os.path.exists('.shelf/shelves/default/0%d' % i))
 
474
 
 
475
        # Unshelve 00
 
476
        self.run_bzr('unshelve', '--all', '00', retcode=0)
 
477
        self.assertEqual(file('test_file1').read(), self.MODIFIED)
 
478
 
 
479
        # Check ls works
 
480
        lines = self.capture('shelf ls', retcode=0).split('\n')
 
481
        for line in lines:
 
482
            self.assertFalse(line.startswith(' 00'))
 
483
 
 
484
        # Check we can reshelve once we've unshelved out of order, should be 04
 
485
        self.assertFalse(os.path.exists('.shelf/shelves/default/04'))
 
486
        self.run_bzr('shelve', '--all')
 
487
        self.assertTrue(os.path.exists('.shelf/shelves/default/04'))
 
488
 
 
489
        # Check ls works
 
490
        text = self.capture('shelf ls', retcode=0)
 
491
        for line in text.split('\n'):
 
492
            self.assertFalse(line.startswith(' 00'))
 
493
 
 
494
        # We now have 01,02,03,04
 
495
        # Unshelve 02
 
496
        self.run_bzr('unshelve', '--all', '02', retcode=0)
 
497
        self.assertEqual(file('test_file3').read(), self.MODIFIED)
 
498
 
 
499
        # Unshelve the default, this is the reshelved 00, hence modifies file 1
 
500
        self.run_bzr('unshelve', '--all', retcode=0)
 
501
        self.assertEqual(file('test_file1').read(), self.MODIFIED)
 
502
 
 
503
    def test_shelf_switch_basic(self):
 
504
        self.tree = self.make_branch_and_tree('.')
 
505
        self.__create_and_add_test_file()
 
506
 
 
507
        # This should go to "default"
 
508
        file('test_file', 'w').write(self.MODIFIED)
 
509
        self.run_bzr('shelve', '--all', 'test_file', retcode=0)
 
510
 
 
511
        # Switch to "other"
 
512
        self.run_bzr('shelf', 'switch', 'other', retcode=0)
 
513
        file('test_file', 'w').write(self.MODIFIED)
 
514
        self.run_bzr('shelve', '--all', 'test_file', retcode=0)
 
515
 
 
516
        # Check it worked
 
517
        self.assertTrue(os.path.exists('.shelf/shelves/default/00'))
 
518
        self.assertFalse(os.path.exists('.shelf/shelves/default/01'))
 
519
        self.assertTrue(os.path.exists('.shelf/shelves/other/00'))
 
520
 
 
521
        # Switch back
 
522
        self.run_bzr('shelf', 'switch', 'default', retcode=0)
 
523
        file('test_file', 'w').write(self.MODIFIED)
 
524
        self.run_bzr('shelve', '--all', 'test_file', retcode=0)
 
525
 
 
526
        # Check that worked
 
527
        self.assertTrue(os.path.exists('.shelf/shelves/default/01'))
 
528
        self.assertFalse(os.path.exists('.shelf/shelves/other/01'))