~abentley/bzrtools/bzrtools.dev

« back to all changes in this revision

Viewing changes to tests/shelf_tests.py

  • Committer: Aaron Bentley
  • Date: 2007-03-16 19:33:45 UTC
  • Revision ID: abentley@panoramicfeedback.com-20070316193345-cvr6cxy6bie0lel6
Update bugs

Show diffs side-by-side

added added

removed removed

Lines of Context:
 
1
import os.path
1
2
from bzrlib.diff import _patch_header_date
2
3
import bzrlib.tests
3
 
import os.path
4
4
 
5
 
try:
6
 
    from bzrlib.plugins.bzrtools.hunk_selector import (
7
 
        ShelveHunkSelector,
8
 
        UnshelveHunkSelector,
9
 
        )
10
 
    from bzrlib.plugins.bzrtools.errors import NoColor
11
 
except ImportError:
12
 
    from bzrtools.hunk_selector import (
13
 
        ShelveHunkSelector,
14
 
        UnshelveHunkSelector,
15
 
        )
16
 
    from bzrtools.errors import NoColor
 
5
from bzrlib.plugins.bzrtools.hunk_selector import (
 
6
    ShelveHunkSelector,
 
7
    UnshelveHunkSelector,
 
8
    )
 
9
from bzrlib.plugins.bzrtools.errors import NoColor
17
10
 
18
11
 
19
12
class ShelfTests(bzrlib.tests.TestCaseWithTransport):
40
33
"""
41
34
    def _check_diff(self, diff=DIFF_1, filename='test_file'):
42
35
        old_tree = self.tree.basis_tree()
43
 
        old_date = _patch_header_date(old_tree, 
44
 
                                      old_tree.inventory.path2id(filename),
45
 
                                      filename)
46
 
        new_date = _patch_header_date(self.tree, 
47
 
                                      self.tree.inventory.path2id(filename),
48
 
                                      filename)
 
36
        old_tree.lock_read()
 
37
        self.tree.lock_read()
 
38
        try:
 
39
            old_date = _patch_header_date(old_tree,
 
40
                                          old_tree.inventory.path2id(filename),
 
41
                                          filename)
 
42
            new_date = _patch_header_date(self.tree,
 
43
                self.tree.inventory.path2id(filename), filename)
 
44
        finally:
 
45
            self.tree.unlock()
 
46
            old_tree.unlock()
49
47
        keys = { 'filename' : filename , 'old_date': old_date, 
50
48
                 'new_date': new_date}
51
49
        hdr  = self.DIFF_HEADER % keys
55
53
    def _check_shelf(self, idx, diff=DIFF_1, filename='test_file',
56
54
                     new_date=None):
57
55
        old_tree = self.tree.basis_tree()
58
 
        old_date = _patch_header_date(old_tree, 
59
 
                                      old_tree.inventory.path2id(filename),
60
 
                                      filename)
 
56
        old_tree.lock_read()
 
57
        try:
 
58
            old_date = _patch_header_date(old_tree,
 
59
                                          old_tree.inventory.path2id(filename),
 
60
                                          filename)
 
61
        finally:
 
62
            old_tree.unlock()
61
63
        diff = diff % { 'filename' : filename, 'old_date': old_date,
62
64
                        'new_date': new_date}
63
65
        shelf = open(os.path.join(self.tree.basedir,
74
76
    def __test_loop(self, count):
75
77
        self.tree = self.make_branch_and_tree('.')
76
78
        self.__create_and_add_test_file()
77
 
 
78
 
        while count > 0:
79
 
            count -= 1
80
 
 
81
 
            # Modify the test file
82
 
            # write in binary mode because on win32 line-endings should be LF
83
 
            f = file('test_file', 'wb')
84
 
            f.write(self.MODIFIED)
85
 
            f.close()
86
 
 
87
 
            self._check_diff()
88
 
            
89
 
            new_date = _patch_header_date(self.tree, 
90
 
                self.tree.inventory.path2id('test_file'), 'test_file')
91
 
 
92
 
            # Shelve the changes
93
 
            self.run_bzr('shelve', '--all', retcode=0)
94
 
 
95
 
            # Make sure there is no diff anymore
96
 
            self.assertEqual(self.capture('diff', retcode=0), '')
97
 
 
98
 
            # Make sure the file is actually back the way it was
99
 
            self.assertEqual(file('test_file').read(), self.ORIGINAL)
100
 
 
101
 
            self._check_shelf('00', new_date=new_date)
102
 
 
103
 
            # Unshelve
104
 
            self.run_bzr('unshelve', '--all', retcode=0)
105
 
 
106
 
            self._check_diff()
107
 
 
108
 
            # Check the shelved patch was backed up
109
 
            self._check_shelf('00~', new_date=new_date)
110
 
 
111
 
            # Make sure the file is back the way it should be
112
 
            self.assertEqual(file('test_file').read(), self.MODIFIED)
 
79
        self.tree.lock_write()
 
80
        try:
 
81
            while count > 0:
 
82
                count -= 1
 
83
 
 
84
                # Modify the test file
 
85
                # write in binary mode because on win32 line-endings should be
 
86
                # LF
 
87
                f = file('test_file', 'wb')
 
88
                f.write(self.MODIFIED)
 
89
                f.close()
 
90
 
 
91
                self._check_diff()
 
92
 
 
93
                new_date = _patch_header_date(self.tree,
 
94
                    self.tree.inventory.path2id('test_file'), 'test_file')
 
95
 
 
96
                # Shelve the changes
 
97
                self.run_bzr('shelve', '--all', retcode=0)
 
98
 
 
99
                # Make sure there is no diff anymore
 
100
                self.assertEqual(self.capture('diff', retcode=0), '')
 
101
 
 
102
                # Make sure the file is actually back the way it was
 
103
                self.assertEqual(file('test_file').read(), self.ORIGINAL)
 
104
 
 
105
                self._check_shelf('00', new_date=new_date)
 
106
 
 
107
                # Unshelve
 
108
                self.run_bzr('unshelve', '--all', retcode=0)
 
109
 
 
110
                self._check_diff()
 
111
 
 
112
                # Check the shelved patch was backed up
 
113
                self._check_shelf('00~', new_date=new_date)
 
114
 
 
115
                # Make sure the file is back the way it should be
 
116
                self.assertEqual(file('test_file').read(), self.MODIFIED)
 
117
        finally:
 
118
            self.tree.unlock()
113
119
 
114
120
    def test_shelf_nothing_to_shelve(self):
115
121
        import os.path
313
319
 
314
320
    def test_shelf_delete(self):
315
321
        self.tree = self.make_branch_and_tree('.')
316
 
 
317
 
        self.__create_and_add_test_file()
318
 
        self.__create_and_add_test_file(filename='test_file2')
319
 
 
320
 
        # Modify the test files
321
 
        # write in binary mode because on win32 line-endings should be LF
322
 
        f = file('test_file', 'wb')
323
 
        f.write(self.MODIFIED)
324
 
        f.close()
325
 
        f = file('test_file2', 'wb')
326
 
        f.write(self.MODIFIED)
327
 
        f.close()
328
 
        new_date = _patch_header_date(self.tree, 
329
 
            self.tree.inventory.path2id('test_file'), 'test_file')
 
322
        self.tree.lock_write()
 
323
        try:
 
324
            self.__create_and_add_test_file()
 
325
            self.__create_and_add_test_file(filename='test_file2')
 
326
 
 
327
            # Modify the test files
 
328
            # write in binary mode because on win32 line-endings should be LF
 
329
            f = file('test_file', 'wb')
 
330
            f.write(self.MODIFIED)
 
331
            f.close()
 
332
            f = file('test_file2', 'wb')
 
333
            f.write(self.MODIFIED)
 
334
            f.close()
 
335
            new_date = _patch_header_date(self.tree,
 
336
                self.tree.inventory.path2id('test_file'), 'test_file')
 
337
        finally:
 
338
            self.tree.unlock()
330
339
 
331
340
        # Shelve the changes
332
341
        self.run_bzr('shelve', '--all', 'test_file', retcode=0)
421
430
        self.run_bzr('shelf', 'ls', retcode=0)
422
431
 
423
432
        old_tree = self.tree.basis_tree()
424
 
        old_date = _patch_header_date(old_tree, 
425
 
                                      old_tree.inventory.path2id('test_file'),
426
 
                                      'test_file')
427
 
        new_date = _patch_header_date(self.tree, 
428
 
                                      self.tree.inventory.path2id('test_file'),
429
 
                                      'test_file')
 
433
        old_tree.lock_read()
 
434
        self.tree.lock_read()
 
435
        try:
 
436
            old_date = _patch_header_date(old_tree,
 
437
                old_tree.inventory.path2id('test_file'),
 
438
                                          'test_file')
 
439
            new_date = _patch_header_date(self.tree,
 
440
                self.tree.inventory.path2id('test_file'), 'test_file')
 
441
        finally:
 
442
            old_tree.unlock()
 
443
            self.tree.unlock()
430
444
        # Fake a -p0 shelved patch
431
445
        diff = self.DIFF_1 % { 'filename' : 'test_file', 'old_date': old_date,
432
446
                               'new_date' : new_date}