~bzr-pqm/bzr/bzr.dev

« back to all changes in this revision

Viewing changes to bzrlib/tests/test_patches.py

  • Committer: Aaron Bentley
  • Date: 2007-02-06 14:52:16 UTC
  • mfrom: (2266 +trunk)
  • mto: This revision was merged to the branch mainline in revision 2268.
  • Revision ID: abentley@panoramicfeedback.com-20070206145216-fcpi8o3ufvuzwbp9
Merge bzr.dev

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, Canonical Ltd
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, 
32
31
                            hunk_from_header, 
33
32
                            iter_patched, 
34
33
                            parse_line,
35
 
                            parse_patch,
36
 
                            parse_patches)
37
 
 
38
 
 
39
 
class PatchesTester(TestCase):
40
 
 
 
34
                            parse_patch)
 
35
 
 
36
 
 
37
class PatchesTester(unittest.TestCase):
41
38
    def datafile(self, filename):
42
39
        data_path = os.path.join(os.path.dirname(__file__), 
43
40
                                 "test_patches_data", filename)
47
44
        """Parse a valid patch header"""
48
45
        lines = "--- orig/commands.py\n+++ mod/dommands.py\n".split('\n')
49
46
        (orig, mod) = get_patch_names(lines.__iter__())
50
 
        self.assertEqual(orig, "orig/commands.py")
51
 
        self.assertEqual(mod, "mod/dommands.py")
 
47
        assert(orig == "orig/commands.py")
 
48
        assert(mod == "mod/dommands.py")
52
49
 
53
50
    def testInvalidPatchHeader(self):
54
51
        """Parse an invalid patch header"""
60
57
        """Parse a valid hunk header"""
61
58
        header = "@@ -34,11 +50,6 @@\n"
62
59
        hunk = hunk_from_header(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)
 
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)
68
65
 
69
66
    def testValidHunkHeader2(self):
70
67
        """Parse a tricky, valid hunk header"""
71
68
        header = "@@ -1 +0,0 @@\n"
72
69
        hunk = hunk_from_header(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)
78
 
 
79
 
    def testPDiff(self):
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))
 
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)
85
75
 
86
76
    def makeMalformed(self, header):
87
77
        self.assertRaises(MalformedHunkHeader, hunk_from_header, header)
100
90
 
101
91
    def lineThing(self,text, type):
102
92
        line = parse_line(text)
103
 
        self.assertIsInstance(line, type)
104
 
        self.assertEqual(str(line), text)
 
93
        assert(isinstance(line, type))
 
94
        assert(str(line)==text)
105
95
 
106
96
    def makeMalformedLine(self, text):
107
97
        self.assertRaises(MalformedLine, parse_line, text)
153
143
            if mod_pos is None:
154
144
                removals.append(orig[i])
155
145
                continue
156
 
            self.assertEqual(mod[mod_pos], orig[i])
 
146
            assert(mod[mod_pos]==orig[i])
157
147
        rem_iter = removals.__iter__()
158
148
        for hunk in patch.hunks:
159
149
            for line in hunk.lines:
162
152
                    if line.contents != next:
163
153
                        sys.stdout.write(" orig:%spatch:%s" % (next,
164
154
                                         line.contents))
165
 
                    self.assertEqual(line.contents, next)
 
155
                    assert(line.contents == next)
166
156
        self.assertRaises(StopIteration, rem_iter.next)
167
157
 
168
158
    def testPatching(self):
190
180
    def testFirstLineRenumber(self):
191
181
        """Make sure we handle lines at the beginning of the hunk"""
192
182
        patch = parse_patch(self.datafile("insert_top.patch"))
193
 
        self.assertEqual(patch.pos_in_mod(0), 1)
194
 
 
195
 
    def testParsePatches(self):
196
 
        """Make sure file names can be extracted from tricky unified diffs"""
197
 
        patchtext = \
198
 
"""--- orig-7
199
 
+++ mod-7
200
 
@@ -1,10 +1,10 @@
201
 
 -- a
202
 
--- b
203
 
+++ c
204
 
 xx d
205
 
 xx e
206
 
 ++ f
207
 
-++ g
208
 
+-- h
209
 
 xx i
210
 
 xx j
211
 
 -- k
212
 
--- l
213
 
+++ m
214
 
--- orig-8
215
 
+++ mod-8
216
 
@@ -1 +1 @@
217
 
--- A
218
 
+++ B
219
 
@@ -1 +1 @@
220
 
--- C
221
 
+++ D
222
 
"""
223
 
        filenames = [('orig-7', 'mod-7'),
224
 
                     ('orig-8', 'mod-8')]
225
 
        patches = parse_patches(patchtext.splitlines(True))
226
 
        patch_files = []
227
 
        for patch in patches:
228
 
            patch_files.append((patch.oldname, patch.newname))
229
 
        self.assertEqual(patch_files, filenames)
 
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()