1
# Copyright (C) 2004 - 2006 Aaron Bentley
1
# Copyright (C) 2004 - 2008 Aaron Bentley, Canonical Ltd
2
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
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
22
23
from bzrlib.iterablefile import IterableFile
23
24
from bzrlib.patches import (MalformedLine,
24
25
MalformedHunkHeader,
37
class PatchesTester(unittest.TestCase):
39
class PatchesTester(TestCase):
38
41
def datafile(self, filename):
39
42
data_path = os.path.join(os.path.dirname(__file__),
40
43
"test_patches_data", filename)
44
47
"""Parse a valid patch header"""
45
48
lines = "--- orig/commands.py\n+++ mod/dommands.py\n".split('\n')
46
49
(orig, mod) = get_patch_names(lines.__iter__())
47
assert(orig == "orig/commands.py")
48
assert(mod == "mod/dommands.py")
50
self.assertEqual(orig, "orig/commands.py")
51
self.assertEqual(mod, "mod/dommands.py")
50
53
def testInvalidPatchHeader(self):
51
54
"""Parse an invalid patch header"""
57
60
"""Parse a valid hunk header"""
58
61
header = "@@ -34,11 +50,6 @@\n"
59
62
hunk = hunk_from_header(header);
60
assert (hunk.orig_pos == 34)
61
assert (hunk.orig_range == 11)
62
assert (hunk.mod_pos == 50)
63
assert (hunk.mod_range == 6)
64
assert (str(hunk) == 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)
66
69
def testValidHunkHeader2(self):
67
70
"""Parse a tricky, valid hunk header"""
68
71
header = "@@ -1 +0,0 @@\n"
69
72
hunk = hunk_from_header(header);
70
assert (hunk.orig_pos == 1)
71
assert (hunk.orig_range == 1)
72
assert (hunk.mod_pos == 0)
73
assert (hunk.mod_range == 0)
74
assert (str(hunk) == 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))
76
86
def makeMalformed(self, header):
77
87
self.assertRaises(MalformedHunkHeader, hunk_from_header, header)
91
101
def lineThing(self,text, type):
92
102
line = parse_line(text)
93
assert(isinstance(line, type))
94
assert(str(line)==text)
103
self.assertIsInstance(line, type)
104
self.assertEqual(str(line), text)
96
106
def makeMalformedLine(self, text):
97
107
self.assertRaises(MalformedLine, parse_line, text)
152
162
if line.contents != next:
153
163
sys.stdout.write(" orig:%spatch:%s" % (next,
155
assert(line.contents == next)
165
self.assertEqual(line.contents, next)
156
166
self.assertRaises(StopIteration, rem_iter.next)
158
168
def testPatching(self):
180
190
def testFirstLineRenumber(self):
181
191
"""Make sure we handle lines at the beginning of the hunk"""
182
192
patch = parse_patch(self.datafile("insert_top.patch"))
183
assert (patch.pos_in_mod(0)==1)
186
patchesTestSuite = unittest.makeSuite(PatchesTester,'test')
187
runner = unittest.TextTestRunner(verbosity=0)
188
return runner.run(patchesTestSuite)
191
if __name__ == "__main__":
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)