~bzr-pqm/bzr/bzr.dev

« back to all changes in this revision

Viewing changes to bzrlib/tests/test_patches.py

  • Committer: John Arbash Meinel
  • Date: 2006-10-11 23:08:27 UTC
  • mto: This revision was merged to the branch mainline in revision 2080.
  • Revision ID: john@arbash-meinel.com-20061011230827-2bdfc45020695281
Change Copyright .. by Canonical to Copyright ... Canonical

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
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
17
17
 
18
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
23
from bzrlib.patches import (MalformedLine, 
25
24
                            MalformedHunkHeader, 
31
30
                            get_patch_names,
32
31
                            hunk_from_header, 
33
32
                            iter_patched, 
34
 
                            iter_patched_from_hunks,
35
33
                            parse_line,
36
 
                            parse_patch,
37
 
                            parse_patches)
38
 
 
39
 
 
40
 
class PatchesTester(TestCase):
41
 
 
 
34
                            parse_patch)
 
35
 
 
36
 
 
37
class PatchesTester(unittest.TestCase):
42
38
    def datafile(self, filename):
43
39
        data_path = os.path.join(os.path.dirname(__file__), 
44
40
                                 "test_patches_data", filename)
48
44
        """Parse a valid patch header"""
49
45
        lines = "--- orig/commands.py\n+++ mod/dommands.py\n".split('\n')
50
46
        (orig, mod) = get_patch_names(lines.__iter__())
51
 
        self.assertEqual(orig, "orig/commands.py")
52
 
        self.assertEqual(mod, "mod/dommands.py")
 
47
        assert(orig == "orig/commands.py")
 
48
        assert(mod == "mod/dommands.py")
53
49
 
54
50
    def testInvalidPatchHeader(self):
55
51
        """Parse an invalid patch header"""
61
57
        """Parse a valid hunk header"""
62
58
        header = "@@ -34,11 +50,6 @@\n"
63
59
        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)
 
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)
69
65
 
70
66
    def testValidHunkHeader2(self):
71
67
        """Parse a tricky, valid hunk header"""
72
68
        header = "@@ -1 +0,0 @@\n"
73
69
        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)
79
 
 
80
 
    def testPDiff(self):
81
 
        """Parse a hunk header produced by diff -p"""
82
 
        header = "@@ -407,7 +292,7 @@ bzr 0.18rc1  2007-07-10\n"
83
 
        hunk = hunk_from_header(header)
84
 
        self.assertEqual('bzr 0.18rc1  2007-07-10', hunk.tail)
85
 
        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)
86
75
 
87
76
    def makeMalformed(self, header):
88
77
        self.assertRaises(MalformedHunkHeader, hunk_from_header, header)
101
90
 
102
91
    def lineThing(self,text, type):
103
92
        line = parse_line(text)
104
 
        self.assertIsInstance(line, type)
105
 
        self.assertEqual(str(line), text)
 
93
        assert(isinstance(line, type))
 
94
        assert(str(line)==text)
106
95
 
107
96
    def makeMalformedLine(self, text):
108
97
        self.assertRaises(MalformedLine, parse_line, text)
154
143
            if mod_pos is None:
155
144
                removals.append(orig[i])
156
145
                continue
157
 
            self.assertEqual(mod[mod_pos], orig[i])
 
146
            assert(mod[mod_pos]==orig[i])
158
147
        rem_iter = removals.__iter__()
159
148
        for hunk in patch.hunks:
160
149
            for line in hunk.lines:
163
152
                    if line.contents != next:
164
153
                        sys.stdout.write(" orig:%spatch:%s" % (next,
165
154
                                         line.contents))
166
 
                    self.assertEqual(line.contents, next)
 
155
                    assert(line.contents == next)
167
156
        self.assertRaises(StopIteration, rem_iter.next)
168
157
 
169
158
    def testPatching(self):
188
177
                count += 1
189
178
            self.assertEqual(count, len(mod_lines))
190
179
 
191
 
    def test_iter_patched_from_hunks(self):
192
 
        """Test a few patch files, and make sure they work."""
193
 
        files = [
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'),
199
 
        ]
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)
206
 
            lines = []
207
 
            count = 0
208
 
            for patch_line in patched_file:
209
 
                self.assertEqual(patch_line, mod_lines[count])
210
 
                count += 1
211
 
            self.assertEqual(count, len(mod_lines))
212
 
 
213
180
    def testFirstLineRenumber(self):
214
181
        """Make sure we handle lines at the beginning of the hunk"""
215
182
        patch = parse_patch(self.datafile("insert_top.patch"))
216
 
        self.assertEqual(patch.pos_in_mod(0), 1)
217
 
 
218
 
    def testParsePatches(self):
219
 
        """Make sure file names can be extracted from tricky unified diffs"""
220
 
        patchtext = \
221
 
"""--- orig-7
222
 
+++ mod-7
223
 
@@ -1,10 +1,10 @@
224
 
 -- a
225
 
--- b
226
 
+++ c
227
 
 xx d
228
 
 xx e
229
 
 ++ f
230
 
-++ g
231
 
+-- h
232
 
 xx i
233
 
 xx j
234
 
 -- k
235
 
--- l
236
 
+++ m
237
 
--- orig-8
238
 
+++ mod-8
239
 
@@ -1 +1 @@
240
 
--- A
241
 
+++ B
242
 
@@ -1 +1 @@
243
 
--- C
244
 
+++ D
245
 
"""
246
 
        filenames = [('orig-7', 'mod-7'),
247
 
                     ('orig-8', 'mod-8')]
248
 
        patches = parse_patches(patchtext.splitlines(True))
249
 
        patch_files = []
250
 
        for patch in patches:
251
 
            patch_files.append((patch.oldname, patch.newname))
252
 
        self.assertEqual(patch_files, filenames)
253
 
 
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())
 
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()