~bzr-pqm/bzr/bzr.dev

« back to all changes in this revision

Viewing changes to bzrlib/tests/test_patches.py

  • Committer: Robert Collins
  • Date: 2006-07-20 13:00:31 UTC
  • mto: (1852.9.1 Tree.compare().)
  • mto: This revision was merged to the branch mainline in revision 1890.
  • Revision ID: robertc@robertcollins.net-20060720130031-d26103a427ea10f3
StartĀ treeĀ implementationĀ tests.

Show diffs side-by-side

added added

removed removed

Lines of Context:
1
 
# Copyright (C) 2004 - 2008 Aaron Bentley, Canonical Ltd
 
1
# Copyright (C) 2004 - 2006 Aaron Bentley
2
2
# <aaron.bentley@utoronto.ca>
3
3
#
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.
8
 
#
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.
13
 
#
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., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
17
 
 
18
 
 
 
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.
 
8
#
 
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.
 
13
#
 
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
 
17
 
 
18
 
 
19
import unittest
19
20
import os.path
20
21
 
21
 
from bzrlib.tests import TestCase
22
 
 
23
22
from bzrlib.iterablefile import IterableFile
24
 
from bzrlib.patches import (MalformedLine,
25
 
                            MalformedHunkHeader,
26
 
                            MalformedPatchHeader,
27
 
                            BinaryPatch,
28
 
                            BinaryFiles,
29
 
                            Patch,
30
 
                            ContextLine,
 
23
from bzrlib.patches import (MalformedLine, 
 
24
                            MalformedHunkHeader, 
 
25
                            MalformedPatchHeader, 
 
26
                            ContextLine, 
31
27
                            InsertLine,
32
 
                            RemoveLine,
33
 
                            difference_index,
 
28
                            RemoveLine, 
 
29
                            difference_index, 
34
30
                            get_patch_names,
35
 
                            hunk_from_header,
36
 
                            iter_patched,
37
 
                            iter_patched_from_hunks,
 
31
                            hunk_from_header, 
 
32
                            iter_patched, 
38
33
                            parse_line,
39
 
                            parse_patch,
40
 
                            parse_patches,
41
 
                            NO_NL)
42
 
 
43
 
 
44
 
class PatchesTester(TestCase):
45
 
 
 
34
                            parse_patch)
 
35
 
 
36
 
 
37
class PatchesTester(unittest.TestCase):
46
38
    def datafile(self, filename):
47
 
        data_path = os.path.join(os.path.dirname(__file__),
 
39
        data_path = os.path.join(os.path.dirname(__file__), 
48
40
                                 "test_patches_data", filename)
49
41
        return file(data_path, "rb")
50
42
 
51
 
    def data_lines(self, filename):
52
 
        datafile = self.datafile(filename)
53
 
        try:
54
 
            return datafile.readlines()
55
 
        finally:
56
 
            datafile.close()
57
 
 
58
43
    def testValidPatchHeader(self):
59
44
        """Parse a valid patch header"""
60
45
        lines = "--- orig/commands.py\n+++ mod/dommands.py\n".split('\n')
61
46
        (orig, mod) = get_patch_names(lines.__iter__())
62
 
        self.assertEqual(orig, "orig/commands.py")
63
 
        self.assertEqual(mod, "mod/dommands.py")
 
47
        assert(orig == "orig/commands.py")
 
48
        assert(mod == "mod/dommands.py")
64
49
 
65
50
    def testInvalidPatchHeader(self):
66
51
        """Parse an invalid patch header"""
72
57
        """Parse a valid hunk header"""
73
58
        header = "@@ -34,11 +50,6 @@\n"
74
59
        hunk = hunk_from_header(header);
75
 
        self.assertEqual(hunk.orig_pos, 34)
76
 
        self.assertEqual(hunk.orig_range, 11)
77
 
        self.assertEqual(hunk.mod_pos, 50)
78
 
        self.assertEqual(hunk.mod_range, 6)
79
 
        self.assertEqual(str(hunk), 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)
80
65
 
81
66
    def testValidHunkHeader2(self):
82
67
        """Parse a tricky, valid hunk header"""
83
68
        header = "@@ -1 +0,0 @@\n"
84
69
        hunk = hunk_from_header(header);
85
 
        self.assertEqual(hunk.orig_pos, 1)
86
 
        self.assertEqual(hunk.orig_range, 1)
87
 
        self.assertEqual(hunk.mod_pos, 0)
88
 
        self.assertEqual(hunk.mod_range, 0)
89
 
        self.assertEqual(str(hunk), header)
90
 
 
91
 
    def testPDiff(self):
92
 
        """Parse a hunk header produced by diff -p"""
93
 
        header = "@@ -407,7 +292,7 @@ bzr 0.18rc1  2007-07-10\n"
94
 
        hunk = hunk_from_header(header)
95
 
        self.assertEqual('bzr 0.18rc1  2007-07-10', hunk.tail)
96
 
        self.assertEqual(header, str(hunk))
 
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)
97
75
 
98
76
    def makeMalformed(self, header):
99
77
        self.assertRaises(MalformedHunkHeader, hunk_from_header, header)
112
90
 
113
91
    def lineThing(self,text, type):
114
92
        line = parse_line(text)
115
 
        self.assertIsInstance(line, type)
116
 
        self.assertEqual(str(line), text)
 
93
        assert(isinstance(line, type))
 
94
        assert(str(line)==text)
117
95
 
118
96
    def makeMalformedLine(self, text):
119
97
        self.assertRaises(MalformedLine, parse_line, text)
123
101
        self.lineThing(" hello\n", ContextLine)
124
102
        self.lineThing("+hello\n", InsertLine)
125
103
        self.lineThing("-hello\n", RemoveLine)
126
 
 
 
104
    
127
105
    def testMalformedLine(self):
128
106
        """Parse invalid valid hunk lines"""
129
107
        self.makeMalformedLine("hello\n")
130
 
 
131
 
    def testMalformedLineNO_NL(self):
132
 
        """Parse invalid '\ No newline at end of file' in hunk lines"""
133
 
        self.makeMalformedLine(NO_NL)
134
 
 
 
108
    
135
109
    def compare_parsed(self, patchtext):
136
110
        lines = patchtext.splitlines(True)
137
111
        patch = parse_patch(lines.__iter__())
146
120
        patchtext = self.datafile("patchtext.patch").read()
147
121
        self.compare_parsed(patchtext)
148
122
 
149
 
    def test_parse_binary(self):
150
 
        """Test parsing a whole patch"""
151
 
        patches = parse_patches(self.data_lines("binary.patch"))
152
 
        self.assertIs(BinaryPatch, patches[0].__class__)
153
 
        self.assertIs(Patch, patches[1].__class__)
154
 
        self.assertContainsRe(patches[0].oldname, '^bar\t')
155
 
        self.assertContainsRe(patches[0].newname, '^qux\t')
156
 
        self.assertContainsRe(str(patches[0]),
157
 
                                  'Binary files bar\t.* and qux\t.* differ\n')
158
 
 
159
 
    def test_parse_binary_after_normal(self):
160
 
        patches = parse_patches(self.data_lines("binary-after-normal.patch"))
161
 
        self.assertIs(BinaryPatch, patches[1].__class__)
162
 
        self.assertIs(Patch, patches[0].__class__)
163
 
        self.assertContainsRe(patches[1].oldname, '^bar\t')
164
 
        self.assertContainsRe(patches[1].newname, '^qux\t')
165
 
        self.assertContainsRe(str(patches[1]),
166
 
                                  'Binary files bar\t.* and qux\t.* differ\n')
167
 
 
168
 
    def test_roundtrip_binary(self):
169
 
        patchtext = ''.join(self.data_lines("binary.patch"))
170
 
        patches = parse_patches(patchtext.splitlines(True))
171
 
        self.assertEqual(patchtext, ''.join(str(p) for p in patches))
172
 
 
173
123
    def testInit(self):
174
124
        """Handle patches missing half the position, range tuple"""
175
125
        patchtext = \
193
143
            if mod_pos is None:
194
144
                removals.append(orig[i])
195
145
                continue
196
 
            self.assertEqual(mod[mod_pos], orig[i])
 
146
            assert(mod[mod_pos]==orig[i])
197
147
        rem_iter = removals.__iter__()
198
148
        for hunk in patch.hunks:
199
149
            for line in hunk.lines:
202
152
                    if line.contents != next:
203
153
                        sys.stdout.write(" orig:%spatch:%s" % (next,
204
154
                                         line.contents))
205
 
                    self.assertEqual(line.contents, next)
 
155
                    assert(line.contents == next)
206
156
        self.assertRaises(StopIteration, rem_iter.next)
207
157
 
208
158
    def testPatching(self):
213
163
            ('diff-4', 'orig-4', 'mod-4'),
214
164
            ('diff-5', 'orig-5', 'mod-5'),
215
165
            ('diff-6', 'orig-6', 'mod-6'),
216
 
            ('diff-7', 'orig-7', 'mod-7'),
217
166
        ]
218
167
        for diff, orig, mod in files:
219
168
            patch = self.datafile(diff)
228
177
                count += 1
229
178
            self.assertEqual(count, len(mod_lines))
230
179
 
231
 
    def test_iter_patched_binary(self):
232
 
        binary_lines = self.data_lines('binary.patch')
233
 
        e = self.assertRaises(BinaryFiles, iter_patched, [], binary_lines)
234
 
 
235
 
 
236
 
    def test_iter_patched_from_hunks(self):
237
 
        """Test a few patch files, and make sure they work."""
238
 
        files = [
239
 
            ('diff-2', 'orig-2', 'mod-2'),
240
 
            ('diff-3', 'orig-3', 'mod-3'),
241
 
            ('diff-4', 'orig-4', 'mod-4'),
242
 
            ('diff-5', 'orig-5', 'mod-5'),
243
 
            ('diff-6', 'orig-6', 'mod-6'),
244
 
            ('diff-7', 'orig-7', 'mod-7'),
245
 
        ]
246
 
        for diff, orig, mod in files:
247
 
            parsed = parse_patch(self.datafile(diff))
248
 
            orig_lines = list(self.datafile(orig))
249
 
            mod_lines = list(self.datafile(mod))
250
 
            iter_patched = iter_patched_from_hunks(orig_lines, parsed.hunks)
251
 
            patched_file = IterableFile(iter_patched)
252
 
            lines = []
253
 
            count = 0
254
 
            for patch_line in patched_file:
255
 
                self.assertEqual(patch_line, mod_lines[count])
256
 
                count += 1
257
 
            self.assertEqual(count, len(mod_lines))
258
 
 
259
180
    def testFirstLineRenumber(self):
260
181
        """Make sure we handle lines at the beginning of the hunk"""
261
182
        patch = parse_patch(self.datafile("insert_top.patch"))
262
 
        self.assertEqual(patch.pos_in_mod(0), 1)
263
 
 
264
 
    def testParsePatches(self):
265
 
        """Make sure file names can be extracted from tricky unified diffs"""
266
 
        patchtext = \
267
 
"""--- orig-7
268
 
+++ mod-7
269
 
@@ -1,10 +1,10 @@
270
 
 -- a
271
 
--- b
272
 
+++ c
273
 
 xx d
274
 
 xx e
275
 
 ++ f
276
 
-++ g
277
 
+-- h
278
 
 xx i
279
 
 xx j
280
 
 -- k
281
 
--- l
282
 
+++ m
283
 
--- orig-8
284
 
+++ mod-8
285
 
@@ -1 +1 @@
286
 
--- A
287
 
+++ B
288
 
@@ -1 +1 @@
289
 
--- C
290
 
+++ D
291
 
"""
292
 
        filenames = [('orig-7', 'mod-7'),
293
 
                     ('orig-8', 'mod-8')]
294
 
        patches = parse_patches(patchtext.splitlines(True))
295
 
        patch_files = []
296
 
        for patch in patches:
297
 
            patch_files.append((patch.oldname, patch.newname))
298
 
        self.assertEqual(patch_files, filenames)
299
 
 
300
 
    def testStatsValues(self):
301
 
        """Test the added, removed and hunks values for stats_values."""
302
 
        patch = parse_patch(self.datafile("diff"))
303
 
        self.assertEqual((299, 407, 48), patch.stats_values())
 
183
        assert (patch.pos_in_mod(0)==1)
 
184
 
 
185
def test():
 
186
    patchesTestSuite = unittest.makeSuite(PatchesTester,'test')
 
187
    runner = unittest.TextTestRunner(verbosity=0)
 
188
    return runner.run(patchesTestSuite)
 
189
 
 
190
 
 
191
if __name__ == "__main__":
 
192
    test()