~bzr-pqm/bzr/bzr.dev

« back to all changes in this revision

Viewing changes to bzrlib/tests/test_patches.py

  • Committer: Patch Queue Manager
  • Date: 2014-12-17 18:12:50 UTC
  • mfrom: (6601.1.8 bug-1400567)
  • Revision ID: pqm@pqm.ubuntu.com-20141217181250-ue0ku3dibz7vle02
(richard-wilbur) Added keep_dirty kwarg to bzrlib.patches.parse_patches()
 allowing preservation of dirty patch headers. (Bayard 'kit' Randel)

Show diffs side-by-side

added added

removed removed

Lines of Context:
58
58
        # https://bugs.launchpad.net/bzr/+bug/502076
59
59
        # https://code.launchpad.net/~toshio/bzr/allow-dirty-patches/+merge/18854
60
60
        lines = ["diff -pruN commands.py",
61
 
            "--- orig/commands.py",
62
 
            "+++ mod/dommands.py"]
 
61
                 "--- orig/commands.py",
 
62
                 "+++ mod/dommands.py"]
63
63
        bits = parse_patches(iter(lines), allow_dirty=True)
64
64
 
 
65
    def test_preserve_dirty_head(self):
 
66
        """Parse a patch containing a dirty header, and preserve lines"""
 
67
        lines = ["=== added directory 'foo/bar'\n",
 
68
                 "=== modified file 'orig/commands.py'\n",
 
69
                 "--- orig/commands.py\n",
 
70
                 "+++ mod/dommands.py\n"]
 
71
        patches = parse_patches(
 
72
            lines.__iter__(), allow_dirty=True, keep_dirty=True)
 
73
        self.assertEqual(patches[0]['dirty_head'],
 
74
                         ["=== added directory 'foo/bar'\n",
 
75
                          "=== modified file 'orig/commands.py'\n"])
 
76
        self.assertEqual(patches[0]['patch'].get_header().splitlines(True),
 
77
                         ["--- orig/commands.py\n", "+++ mod/dommands.py\n"])
 
78
 
65
79
    def testValidPatchHeader(self):
66
80
        """Parse a valid patch header"""
67
81
        lines = "--- orig/commands.py\n+++ mod/dommands.py\n".split('\n')
78
92
    def testValidHunkHeader(self):
79
93
        """Parse a valid hunk header"""
80
94
        header = "@@ -34,11 +50,6 @@\n"
81
 
        hunk = hunk_from_header(header);
 
95
        hunk = hunk_from_header(header)
82
96
        self.assertEqual(hunk.orig_pos, 34)
83
97
        self.assertEqual(hunk.orig_range, 11)
84
98
        self.assertEqual(hunk.mod_pos, 50)
88
102
    def testValidHunkHeader2(self):
89
103
        """Parse a tricky, valid hunk header"""
90
104
        header = "@@ -1 +0,0 @@\n"
91
 
        hunk = hunk_from_header(header);
 
105
        hunk = hunk_from_header(header)
92
106
        self.assertEqual(hunk.orig_pos, 1)
93
107
        self.assertEqual(hunk.orig_range, 1)
94
108
        self.assertEqual(hunk.mod_pos, 0)
117
131
        self.makeMalformed("@@ -34,11 +50,6.5 @@\n")
118
132
        self.makeMalformed("@@ -34,11 +50,-6 @@\n")
119
133
 
120
 
    def lineThing(self,text, type):
 
134
    def lineThing(self, text, type):
121
135
        line = parse_line(text)
122
136
        self.assertIsInstance(line, type)
123
137
        self.assertEqual(str(line), text)
146
160
        i = difference_index(patchtext, pstr)
147
161
        if i is not None:
148
162
            print "%i: \"%s\" != \"%s\"" % (i, patchtext[i], pstr[i])
149
 
        self.assertEqual (patchtext, str(patch))
 
163
        self.assertEqual(patchtext, str(patch))
150
164
 
151
165
    def testAll(self):
152
166
        """Test parsing a whole patch"""
161
175
        self.assertContainsRe(patches[0].oldname, '^bar\t')
162
176
        self.assertContainsRe(patches[0].newname, '^qux\t')
163
177
        self.assertContainsRe(str(patches[0]),
164
 
                                  'Binary files bar\t.* and qux\t.* differ\n')
 
178
                              'Binary files bar\t.* and qux\t.* differ\n')
165
179
 
166
180
    def test_parse_binary_after_normal(self):
167
181
        patches = parse_patches(self.data_lines("binary-after-normal.patch"))
170
184
        self.assertContainsRe(patches[1].oldname, '^bar\t')
171
185
        self.assertContainsRe(patches[1].newname, '^qux\t')
172
186
        self.assertContainsRe(str(patches[1]),
173
 
                                  'Binary files bar\t.* and qux\t.* differ\n')
 
187
                              'Binary files bar\t.* and qux\t.* differ\n')
174
188
 
175
189
    def test_roundtrip_binary(self):
176
190
        patchtext = ''.join(self.data_lines("binary.patch"))
228
242
            mod_lines = list(self.datafile(mod))
229
243
 
230
244
            patched_file = IterableFile(iter_patched(orig_lines, patch))
231
 
            lines = []
232
245
            count = 0
233
246
            for patch_line in patched_file:
234
247
                self.assertEqual(patch_line, mod_lines[count])
239
252
        binary_lines = self.data_lines('binary.patch')
240
253
        e = self.assertRaises(BinaryFiles, iter_patched, [], binary_lines)
241
254
 
242
 
 
243
255
    def test_iter_patched_from_hunks(self):
244
256
        """Test a few patch files, and make sure they work."""
245
257
        files = [
256
268
            mod_lines = list(self.datafile(mod))
257
269
            iter_patched = iter_patched_from_hunks(orig_lines, parsed.hunks)
258
270
            patched_file = IterableFile(iter_patched)
259
 
            lines = []
260
271
            count = 0
261
272
            for patch_line in patched_file:
262
273
                self.assertEqual(patch_line, mod_lines[count])