21
21
from bzrlib.tests import TestCase
23
23
from bzrlib.iterablefile import IterableFile
24
from bzrlib.patches import (MalformedLine,
24
from bzrlib.patches import (MalformedLine,
34
iter_patched_from_hunks,
39
41
class PatchesTester(TestCase):
41
43
def datafile(self, filename):
42
data_path = os.path.join(os.path.dirname(__file__),
44
data_path = os.path.join(os.path.dirname(__file__),
43
45
"test_patches_data", filename)
44
46
return file(data_path, "rb")
111
113
self.lineThing(" hello\n", ContextLine)
112
114
self.lineThing("+hello\n", InsertLine)
113
115
self.lineThing("-hello\n", RemoveLine)
115
117
def testMalformedLine(self):
116
118
"""Parse invalid valid hunk lines"""
117
119
self.makeMalformedLine("hello\n")
121
def testMalformedLineNO_NL(self):
122
"""Parse invalid '\ No newline at end of file' in hunk lines"""
123
self.makeMalformedLine(NO_NL)
119
125
def compare_parsed(self, patchtext):
120
126
lines = patchtext.splitlines(True)
121
127
patch = parse_patch(lines.__iter__())
173
179
('diff-4', 'orig-4', 'mod-4'),
174
180
('diff-5', 'orig-5', 'mod-5'),
175
181
('diff-6', 'orig-6', 'mod-6'),
182
('diff-7', 'orig-7', 'mod-7'),
177
184
for diff, orig, mod in files:
178
185
patch = self.datafile(diff)
188
195
self.assertEqual(count, len(mod_lines))
197
def test_iter_patched_from_hunks(self):
198
"""Test a few patch files, and make sure they work."""
200
('diff-2', 'orig-2', 'mod-2'),
201
('diff-3', 'orig-3', 'mod-3'),
202
('diff-4', 'orig-4', 'mod-4'),
203
('diff-5', 'orig-5', 'mod-5'),
204
('diff-6', 'orig-6', 'mod-6'),
205
('diff-7', 'orig-7', 'mod-7'),
207
for diff, orig, mod in files:
208
parsed = parse_patch(self.datafile(diff))
209
orig_lines = list(self.datafile(orig))
210
mod_lines = list(self.datafile(mod))
211
iter_patched = iter_patched_from_hunks(orig_lines, parsed.hunks)
212
patched_file = IterableFile(iter_patched)
215
for patch_line in patched_file:
216
self.assertEqual(patch_line, mod_lines[count])
218
self.assertEqual(count, len(mod_lines))
190
220
def testFirstLineRenumber(self):
191
221
"""Make sure we handle lines at the beginning of the hunk"""
192
222
patch = parse_patch(self.datafile("insert_top.patch"))
227
257
for patch in patches:
228
258
patch_files.append((patch.oldname, patch.newname))
229
259
self.assertEqual(patch_files, filenames)
261
def testStatsValues(self):
262
"""Test the added, removed and hunks values for stats_values."""
263
patch = parse_patch(self.datafile("diff"))
264
self.assertEqual((299, 407, 48), patch.stats_values())