1
# Copyright (C) 2004 - 2008 Aaron Bentley, Canonical Ltd
1
# Copyright (C) 2004 - 2006 Aaron Bentley, Canonical Ltd
2
2
# <aaron.bentley@utoronto.ca>
4
4
# This program is free software; you can redistribute it and/or modify
16
16
# Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
21
from bzrlib.tests import TestCase
23
22
from bzrlib.iterablefile import IterableFile
24
23
from bzrlib.patches import (MalformedLine,
25
24
MalformedHunkHeader,
34
iter_patched_from_hunks,
40
class PatchesTester(TestCase):
38
class PatchesTester(unittest.TestCase):
42
39
def datafile(self, filename):
43
40
data_path = os.path.join(os.path.dirname(__file__),
44
41
"test_patches_data", filename)
48
45
"""Parse a valid patch header"""
49
46
lines = "--- orig/commands.py\n+++ mod/dommands.py\n".split('\n')
50
47
(orig, mod) = get_patch_names(lines.__iter__())
51
self.assertEqual(orig, "orig/commands.py")
52
self.assertEqual(mod, "mod/dommands.py")
48
assert(orig == "orig/commands.py")
49
assert(mod == "mod/dommands.py")
54
51
def testInvalidPatchHeader(self):
55
52
"""Parse an invalid patch header"""
61
58
"""Parse a valid hunk header"""
62
59
header = "@@ -34,11 +50,6 @@\n"
63
60
hunk = hunk_from_header(header);
64
self.assertEqual(hunk.orig_pos, 34)
65
self.assertEqual(hunk.orig_range, 11)
66
self.assertEqual(hunk.mod_pos, 50)
67
self.assertEqual(hunk.mod_range, 6)
68
self.assertEqual(str(hunk), header)
61
assert (hunk.orig_pos == 34)
62
assert (hunk.orig_range == 11)
63
assert (hunk.mod_pos == 50)
64
assert (hunk.mod_range == 6)
65
assert (str(hunk) == header)
70
67
def testValidHunkHeader2(self):
71
68
"""Parse a tricky, valid hunk header"""
72
69
header = "@@ -1 +0,0 @@\n"
73
70
hunk = hunk_from_header(header);
74
self.assertEqual(hunk.orig_pos, 1)
75
self.assertEqual(hunk.orig_range, 1)
76
self.assertEqual(hunk.mod_pos, 0)
77
self.assertEqual(hunk.mod_range, 0)
78
self.assertEqual(str(hunk), header)
71
assert (hunk.orig_pos == 1)
72
assert (hunk.orig_range == 1)
73
assert (hunk.mod_pos == 0)
74
assert (hunk.mod_range == 0)
75
assert (str(hunk) == header)
80
77
def testPDiff(self):
81
78
"""Parse a hunk header produced by diff -p"""
102
99
def lineThing(self,text, type):
103
100
line = parse_line(text)
104
self.assertIsInstance(line, type)
105
self.assertEqual(str(line), text)
101
assert(isinstance(line, type))
102
assert(str(line)==text)
107
104
def makeMalformedLine(self, text):
108
105
self.assertRaises(MalformedLine, parse_line, text)
163
160
if line.contents != next:
164
161
sys.stdout.write(" orig:%spatch:%s" % (next,
166
self.assertEqual(line.contents, next)
163
assert(line.contents == next)
167
164
self.assertRaises(StopIteration, rem_iter.next)
169
166
def testPatching(self):
189
186
self.assertEqual(count, len(mod_lines))
191
def test_iter_patched_from_hunks(self):
192
"""Test a few patch files, and make sure they work."""
194
('diff-2', 'orig-2', 'mod-2'),
195
('diff-3', 'orig-3', 'mod-3'),
196
('diff-4', 'orig-4', 'mod-4'),
197
('diff-5', 'orig-5', 'mod-5'),
198
('diff-6', 'orig-6', 'mod-6'),
200
for diff, orig, mod in files:
201
parsed = parse_patch(self.datafile(diff))
202
orig_lines = list(self.datafile(orig))
203
mod_lines = list(self.datafile(mod))
204
iter_patched = iter_patched_from_hunks(orig_lines, parsed.hunks)
205
patched_file = IterableFile(iter_patched)
208
for patch_line in patched_file:
209
self.assertEqual(patch_line, mod_lines[count])
211
self.assertEqual(count, len(mod_lines))
213
188
def testFirstLineRenumber(self):
214
189
"""Make sure we handle lines at the beginning of the hunk"""
215
190
patch = parse_patch(self.datafile("insert_top.patch"))
216
self.assertEqual(patch.pos_in_mod(0), 1)
191
assert (patch.pos_in_mod(0)==1)
218
193
def testParsePatches(self):
219
194
"""Make sure file names can be extracted from tricky unified diffs"""
250
225
for patch in patches:
251
226
patch_files.append((patch.oldname, patch.newname))
252
self.assertEqual(patch_files, filenames)
254
def testStatsValues(self):
255
"""Test the added, removed and hunks values for stats_values."""
256
patch = parse_patch(self.datafile("diff"))
257
self.assertEqual((299, 407, 48), patch.stats_values())
227
assert (patch_files == filenames)
230
patchesTestSuite = unittest.makeSuite(PatchesTester,'test')
231
runner = unittest.TextTestRunner(verbosity=0)
232
return runner.run(patchesTestSuite)
235
if __name__ == "__main__":