~abentley/bzrtools/bzrtools.dev

« back to all changes in this revision

Viewing changes to tests.py

Merge most of the standalone shelf branch. This brings in a few changes which
make it easier to write a standalone shelf, although not all of them.
There's also a bunch of new features, tests, etc.

Show diffs side-by-side

added added

removed removed

Lines of Context:
1
1
#!/usr/bin/python
2
2
 
3
3
import bzrlib.tests
 
4
import os.path
4
5
 
5
 
class ShelfTests(bzrlib.tests.TestCaseInTempDir):
 
6
class ShelfTests(bzrlib.tests.TestCaseWithTransport):
6
7
    ORIGINAL = '\n\nhello test world\n\n'
7
8
    MODIFIED = '\n\ngoodbye test world\n\n'
8
9
    DIFF_HEADER = "=== modified file 'test_file'\n"
31
32
        self.__test_loop(10)
32
33
 
33
34
    def __test_loop(self, count):
34
 
        from bzrlib.branch import Branch
35
 
        b = Branch.initialize('.')
36
 
 
37
 
        # Create a test file and commit it
38
 
        file('test_file', 'w').write(self.ORIGINAL)
39
 
        b.working_tree().add('test_file')
40
 
        b.working_tree().commit(message='add test_file')
 
35
        tree = self.make_branch_and_tree('.')
 
36
        self.__create_and_add_test_file(tree)
41
37
 
42
38
        while count > 0:
43
39
            count -= 1
59
55
            self.assertEqual(file('test_file').read(), self.ORIGINAL)
60
56
 
61
57
            # Check the shelf is right
62
 
            shelf = b._transport.get('.bzr/x-shelf/default/00').read()
 
58
            shelf = open(os.path.join(tree.branch.base,
 
59
                        '.shelf/shelves/default/00')).read()
 
60
            shelf = shelf[shelf.index('\n') + 1:] # skip the message
63
61
            self.assertEqual(shelf, self.DIFF_1)
64
62
 
65
63
            # Unshelve
74
72
 
75
73
    def test_shelf_nothing_to_shelve(self):
76
74
        import os.path
77
 
        from bzrlib.branch import Branch
78
 
        b = Branch.initialize('.')
79
 
 
80
 
        # Create a test file and commit it
81
 
        file('test_file', 'w').write(self.ORIGINAL)
82
 
        b.working_tree().add('test_file')
83
 
        b.working_tree().commit(message='add test_file')
 
75
        tree = self.make_branch_and_tree('.')
 
76
        self.__create_and_add_test_file(tree)
84
77
 
85
78
        # Shelve the changes
86
79
        self.run_bzr('shelve', retcode=3)
87
80
 
88
 
        if b._transport.has('.bzr/x-shelf/default/00'):
 
81
        if os.path.exists(os.path.join(tree.branch.base,
 
82
                '.shelf/shelves/default/00')):
89
83
            self.fail("Shelf exists, but it shouldn't")
90
84
 
 
85
    def __create_and_add_test_file(self, tree):
 
86
        self.build_tree_contents([('test_file', self.ORIGINAL)])
 
87
        tree.add('test_file')
 
88
        tree.commit(message='add test_file')
 
89
 
91
90
    def test_shelf_with_revision(self):
92
 
        from bzrlib.branch import Branch
93
 
        b = Branch.initialize('.')
 
91
        tree = self.make_branch_and_tree('.')
94
92
 
95
 
        # Create a test file and commit it
96
 
        file('test_file', 'w').write(self.ORIGINAL)
97
 
        b.working_tree().add('test_file')
98
 
        b.working_tree().commit(message='add test_file')
 
93
        self.__create_and_add_test_file(tree)
99
94
 
100
95
        # Modify the test file and commit it
101
 
        file('test_file', 'w').write(self.MODIFIED)
102
 
        b.working_tree().commit(message='update test_file')
 
96
        self.build_tree_contents([('test_file', self.MODIFIED)])
 
97
        tree.commit(message='update test_file')
103
98
 
104
99
        # Shelve the changes
105
100
        self.run_bzr('shelve', '-r', '1', retcode=0)
118
113
        self.assertEqual(file('test_file').read(), self.MODIFIED)
119
114
 
120
115
    def test_shelf_with_two_revisions(self):
121
 
        from bzrlib.branch import Branch
122
 
        b = Branch.initialize('.')
 
116
        tree = self.make_branch_and_tree('.')
123
117
 
124
118
        cmd = 'shelve -r 1..2'
125
119
        (stdout, stderr) = self.run_bzr_captured(cmd.split(), retcode=None)
145
139
        file('foo', 'wb').write('baz')
146
140
        self.run_bzr('shelve', 'foo', retcode=0)
147
141
 
 
142
    def test_shelf_show_basic(self):
 
143
        tree = self.make_branch_and_tree('.')
 
144
        self.__create_and_add_test_file(tree)
 
145
        self.__test_show(tree, '00')
 
146
 
 
147
    def __test_show(self, tree, patch):
 
148
        # Modify the test file
 
149
        self.build_tree_contents([('test_file', 'patch %s\n' % patch)])
 
150
 
 
151
        # Shelve the changes
 
152
        self.run_bzr('shelve', retcode=0)
 
153
 
 
154
        # Make sure there is no diff anymore
 
155
        self.assertEqual(self.capture('diff', retcode=0), '')
 
156
 
 
157
        # Check the shelf is right
 
158
        shelf = open(os.path.join(tree.branch.base,
 
159
                    '.shelf/shelves/default', patch)).read()
 
160
        self.assertTrue('patch %s' % patch in shelf)
 
161
 
 
162
        # Check the shown output is right
 
163
        shown = self.capture('shelf show %s' % patch, retcode=0)
 
164
        self.assertEqual(shown, shelf)
 
165
 
 
166
    def test_shelf_show_multi(self):
 
167
        tree = self.make_branch_and_tree('.')
 
168
        self.__create_and_add_test_file(tree)
 
169
        self.__test_show(tree, '00')
 
170
        self.__test_show(tree, '01')
 
171
        self.__test_show(tree, '02')
 
172
 
 
173
        # Now check we can show a previously shelved patch
 
174
        shelf = open(os.path.join(tree.branch.base,
 
175
                    '.shelf/shelves/default/00')).read()
 
176
        self.assertTrue('patch 00' in shelf)
 
177
 
 
178
        # Check the shown output is right
 
179
        shown = self.capture('shelf show 00', retcode=0)
 
180
        self.assertEqual(shown, shelf)
 
181
 
 
182
    def test_shelf_show_with_no_patch(self):
 
183
        tree = self.make_branch_and_tree('.')
 
184
        stderr = self.run_bzr_captured(['shelf', 'show', '00'], retcode=None)[1]
 
185
        self.assertTrue("Patch '00' doesn't exist on shelf default!" in stderr)
 
186