1
# Copyright (C) 2004 - 2008 Aaron Bentley, Canonical Ltd
2
# <aaron.bentley@utoronto.ca>
4
# This program is free software; you can redistribute it and/or modify
5
# it under the terms of the GNU General Public License as published by
6
# the Free Software Foundation; either version 2 of the License, or
7
# (at your option) any later version.
9
# This program is distributed in the hope that it will be useful,
10
# but WITHOUT ANY WARRANTY; without even the implied warranty of
11
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
12
# GNU General Public License for more details.
14
# You should have received a copy of the GNU General Public License
15
# along with this program; if not, write to the Free Software
16
# Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
21
from bzrlib.tests import TestCase
23
from bzrlib.iterablefile import IterableFile
24
from bzrlib.patches import (MalformedLine,
39
class PatchesTester(TestCase):
41
def datafile(self, filename):
42
data_path = os.path.join(os.path.dirname(__file__),
43
"test_patches_data", filename)
44
return file(data_path, "rb")
46
def testValidPatchHeader(self):
47
"""Parse a valid patch header"""
48
lines = "--- orig/commands.py\n+++ mod/dommands.py\n".split('\n')
49
(orig, mod) = get_patch_names(lines.__iter__())
50
self.assertEqual(orig, "orig/commands.py")
51
self.assertEqual(mod, "mod/dommands.py")
53
def testInvalidPatchHeader(self):
54
"""Parse an invalid patch header"""
55
lines = "-- orig/commands.py\n+++ mod/dommands.py".split('\n')
56
self.assertRaises(MalformedPatchHeader, get_patch_names,
59
def testValidHunkHeader(self):
60
"""Parse a valid hunk header"""
61
header = "@@ -34,11 +50,6 @@\n"
62
hunk = hunk_from_header(header);
63
self.assertEqual(hunk.orig_pos, 34)
64
self.assertEqual(hunk.orig_range, 11)
65
self.assertEqual(hunk.mod_pos, 50)
66
self.assertEqual(hunk.mod_range, 6)
67
self.assertEqual(str(hunk), header)
69
def testValidHunkHeader2(self):
70
"""Parse a tricky, valid hunk header"""
71
header = "@@ -1 +0,0 @@\n"
72
hunk = hunk_from_header(header);
73
self.assertEqual(hunk.orig_pos, 1)
74
self.assertEqual(hunk.orig_range, 1)
75
self.assertEqual(hunk.mod_pos, 0)
76
self.assertEqual(hunk.mod_range, 0)
77
self.assertEqual(str(hunk), header)
80
"""Parse a hunk header produced by diff -p"""
81
header = "@@ -407,7 +292,7 @@ bzr 0.18rc1 2007-07-10\n"
82
hunk = hunk_from_header(header)
83
self.assertEqual('bzr 0.18rc1 2007-07-10', hunk.tail)
84
self.assertEqual(header, str(hunk))
86
def makeMalformed(self, header):
87
self.assertRaises(MalformedHunkHeader, hunk_from_header, header)
89
def testInvalidHeader(self):
90
"""Parse an invalid hunk header"""
91
self.makeMalformed(" -34,11 +50,6 \n")
92
self.makeMalformed("@@ +50,6 -34,11 @@\n")
93
self.makeMalformed("@@ -34,11 +50,6 @@")
94
self.makeMalformed("@@ -34.5,11 +50,6 @@\n")
95
self.makeMalformed("@@-34,11 +50,6@@\n")
96
self.makeMalformed("@@ 34,11 50,6 @@\n")
97
self.makeMalformed("@@ -34,11 @@\n")
98
self.makeMalformed("@@ -34,11 +50,6.5 @@\n")
99
self.makeMalformed("@@ -34,11 +50,-6 @@\n")
101
def lineThing(self,text, type):
102
line = parse_line(text)
103
self.assertIsInstance(line, type)
104
self.assertEqual(str(line), text)
106
def makeMalformedLine(self, text):
107
self.assertRaises(MalformedLine, parse_line, text)
109
def testValidLine(self):
110
"""Parse a valid hunk line"""
111
self.lineThing(" hello\n", ContextLine)
112
self.lineThing("+hello\n", InsertLine)
113
self.lineThing("-hello\n", RemoveLine)
115
def testMalformedLine(self):
116
"""Parse invalid valid hunk lines"""
117
self.makeMalformedLine("hello\n")
119
def compare_parsed(self, patchtext):
120
lines = patchtext.splitlines(True)
121
patch = parse_patch(lines.__iter__())
123
i = difference_index(patchtext, pstr)
125
print "%i: \"%s\" != \"%s\"" % (i, patchtext[i], pstr[i])
126
self.assertEqual (patchtext, str(patch))
129
"""Test parsing a whole patch"""
130
patchtext = self.datafile("patchtext.patch").read()
131
self.compare_parsed(patchtext)
134
"""Handle patches missing half the position, range tuple"""
136
"""--- orig/__vavg__.cl
139
__qbpsbezng__ = "erfgehpgherqgrkg ra"
140
+__qbp__ = Na nygreangr Nepu pbzznaqyvar vagresnpr
142
self.compare_parsed(patchtext)
144
def testLineLookup(self):
146
"""Make sure we can accurately look up mod line from orig"""
147
patch = parse_patch(self.datafile("diff"))
148
orig = list(self.datafile("orig"))
149
mod = list(self.datafile("mod"))
151
for i in range(len(orig)):
152
mod_pos = patch.pos_in_mod(i)
154
removals.append(orig[i])
156
self.assertEqual(mod[mod_pos], orig[i])
157
rem_iter = removals.__iter__()
158
for hunk in patch.hunks:
159
for line in hunk.lines:
160
if isinstance(line, RemoveLine):
161
next = rem_iter.next()
162
if line.contents != next:
163
sys.stdout.write(" orig:%spatch:%s" % (next,
165
self.assertEqual(line.contents, next)
166
self.assertRaises(StopIteration, rem_iter.next)
168
def testPatching(self):
169
"""Test a few patch files, and make sure they work."""
171
('diff-2', 'orig-2', 'mod-2'),
172
('diff-3', 'orig-3', 'mod-3'),
173
('diff-4', 'orig-4', 'mod-4'),
174
('diff-5', 'orig-5', 'mod-5'),
175
('diff-6', 'orig-6', 'mod-6'),
177
for diff, orig, mod in files:
178
patch = self.datafile(diff)
179
orig_lines = list(self.datafile(orig))
180
mod_lines = list(self.datafile(mod))
182
patched_file = IterableFile(iter_patched(orig_lines, patch))
185
for patch_line in patched_file:
186
self.assertEqual(patch_line, mod_lines[count])
188
self.assertEqual(count, len(mod_lines))
190
def testFirstLineRenumber(self):
191
"""Make sure we handle lines at the beginning of the hunk"""
192
patch = parse_patch(self.datafile("insert_top.patch"))
193
self.assertEqual(patch.pos_in_mod(0), 1)
195
def testParsePatches(self):
196
"""Make sure file names can be extracted from tricky unified diffs"""
223
filenames = [('orig-7', 'mod-7'),
225
patches = parse_patches(patchtext.splitlines(True))
227
for patch in patches:
228
patch_files.append((patch.oldname, patch.newname))
229
self.assertEqual(patch_files, filenames)