~bzr-pqm/bzr/bzr.dev

« back to all changes in this revision

Viewing changes to bzrlib/tests/test_patches.py

Show diffs side-by-side

added added

removed removed

Lines of Context:
24
24
from bzrlib.patches import (MalformedLine,
25
25
                            MalformedHunkHeader,
26
26
                            MalformedPatchHeader,
 
27
                            BinaryPatch,
 
28
                            BinaryFiles,
 
29
                            Patch,
27
30
                            ContextLine,
28
31
                            InsertLine,
29
32
                            RemoveLine,
45
48
                                 "test_patches_data", filename)
46
49
        return file(data_path, "rb")
47
50
 
 
51
    def data_lines(self, filename):
 
52
        datafile = self.datafile(filename)
 
53
        try:
 
54
            return datafile.readlines()
 
55
        finally:
 
56
            datafile.close()
 
57
 
48
58
    def testValidPatchHeader(self):
49
59
        """Parse a valid patch header"""
50
60
        lines = "--- orig/commands.py\n+++ mod/dommands.py\n".split('\n')
136
146
        patchtext = self.datafile("patchtext.patch").read()
137
147
        self.compare_parsed(patchtext)
138
148
 
 
149
    def test_parse_binary(self):
 
150
        """Test parsing a whole patch"""
 
151
        patches = parse_patches(self.data_lines("binary.patch"))
 
152
        self.assertIs(BinaryPatch, patches[0].__class__)
 
153
        self.assertIs(Patch, patches[1].__class__)
 
154
        self.assertContainsRe(patches[0].oldname, '^bar\t')
 
155
        self.assertContainsRe(patches[0].newname, '^qux\t')
 
156
        self.assertContainsRe(str(patches[0]),
 
157
                                  'Binary files bar\t.* and qux\t.* differ\n')
 
158
 
 
159
    def test_parse_binary_after_normal(self):
 
160
        patches = parse_patches(self.data_lines("binary-after-normal.patch"))
 
161
        self.assertIs(BinaryPatch, patches[1].__class__)
 
162
        self.assertIs(Patch, patches[0].__class__)
 
163
        self.assertContainsRe(patches[1].oldname, '^bar\t')
 
164
        self.assertContainsRe(patches[1].newname, '^qux\t')
 
165
        self.assertContainsRe(str(patches[1]),
 
166
                                  'Binary files bar\t.* and qux\t.* differ\n')
 
167
 
 
168
    def test_roundtrip_binary(self):
 
169
        patchtext = ''.join(self.data_lines("binary.patch"))
 
170
        patches = parse_patches(patchtext.splitlines(True))
 
171
        self.assertEqual(patchtext, ''.join(str(p) for p in patches))
 
172
 
139
173
    def testInit(self):
140
174
        """Handle patches missing half the position, range tuple"""
141
175
        patchtext = \
194
228
                count += 1
195
229
            self.assertEqual(count, len(mod_lines))
196
230
 
 
231
    def test_iter_patched_binary(self):
 
232
        binary_lines = self.data_lines('binary.patch')
 
233
        e = self.assertRaises(BinaryFiles, iter_patched, [], binary_lines)
 
234
 
 
235
 
197
236
    def test_iter_patched_from_hunks(self):
198
237
        """Test a few patch files, and make sure they work."""
199
238
        files = [