~bzr-pqm/bzr/bzr.dev

« back to all changes in this revision

Viewing changes to bzrlib/tests/test_patches.py

  • Committer: Sidnei da Silva
  • Date: 2009-07-03 15:06:42 UTC
  • mto: (4531.1.1 integration)
  • mto: This revision was merged to the branch mainline in revision 4532.
  • Revision ID: sidnei.da.silva@canonical.com-20090703150642-hjfra5waj5879cae
- Add top-level make target to build all installers using buildout and another to cleanup

Show diffs side-by-side

added added

removed removed

Lines of Context:
1
 
# Copyright (C) 2004 - 2006 Aaron Bentley, Canonical Ltd
 
1
# Copyright (C) 2004 - 2008 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
13
13
#
14
14
# You should have received a copy of the GNU General Public License
15
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
 
16
# Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
 
17
 
 
18
 
20
19
import os.path
21
20
 
 
21
from bzrlib.tests import TestCase
 
22
 
22
23
from bzrlib.iterablefile import IterableFile
23
 
from bzrlib.patches import (MalformedLine, 
24
 
                            MalformedHunkHeader, 
25
 
                            MalformedPatchHeader, 
26
 
                            ContextLine, 
 
24
from bzrlib.patches import (MalformedLine,
 
25
                            MalformedHunkHeader,
 
26
                            MalformedPatchHeader,
 
27
                            ContextLine,
27
28
                            InsertLine,
28
 
                            RemoveLine, 
29
 
                            difference_index, 
 
29
                            RemoveLine,
 
30
                            difference_index,
30
31
                            get_patch_names,
31
 
                            hunk_from_header, 
32
 
                            iter_patched, 
 
32
                            hunk_from_header,
 
33
                            iter_patched,
 
34
                            iter_patched_from_hunks,
33
35
                            parse_line,
34
36
                            parse_patch,
35
 
                            parse_patches)
36
 
 
37
 
 
38
 
class PatchesTester(unittest.TestCase):
 
37
                            parse_patches,
 
38
                            NO_NL)
 
39
 
 
40
 
 
41
class PatchesTester(TestCase):
 
42
 
39
43
    def datafile(self, filename):
40
 
        data_path = os.path.join(os.path.dirname(__file__), 
 
44
        data_path = os.path.join(os.path.dirname(__file__),
41
45
                                 "test_patches_data", filename)
42
46
        return file(data_path, "rb")
43
47
 
45
49
        """Parse a valid patch header"""
46
50
        lines = "--- orig/commands.py\n+++ mod/dommands.py\n".split('\n')
47
51
        (orig, mod) = get_patch_names(lines.__iter__())
48
 
        assert(orig == "orig/commands.py")
49
 
        assert(mod == "mod/dommands.py")
 
52
        self.assertEqual(orig, "orig/commands.py")
 
53
        self.assertEqual(mod, "mod/dommands.py")
50
54
 
51
55
    def testInvalidPatchHeader(self):
52
56
        """Parse an invalid patch header"""
58
62
        """Parse a valid hunk header"""
59
63
        header = "@@ -34,11 +50,6 @@\n"
60
64
        hunk = hunk_from_header(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)
 
65
        self.assertEqual(hunk.orig_pos, 34)
 
66
        self.assertEqual(hunk.orig_range, 11)
 
67
        self.assertEqual(hunk.mod_pos, 50)
 
68
        self.assertEqual(hunk.mod_range, 6)
 
69
        self.assertEqual(str(hunk), header)
66
70
 
67
71
    def testValidHunkHeader2(self):
68
72
        """Parse a tricky, valid hunk header"""
69
73
        header = "@@ -1 +0,0 @@\n"
70
74
        hunk = hunk_from_header(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)
 
75
        self.assertEqual(hunk.orig_pos, 1)
 
76
        self.assertEqual(hunk.orig_range, 1)
 
77
        self.assertEqual(hunk.mod_pos, 0)
 
78
        self.assertEqual(hunk.mod_range, 0)
 
79
        self.assertEqual(str(hunk), header)
 
80
 
 
81
    def testPDiff(self):
 
82
        """Parse a hunk header produced by diff -p"""
 
83
        header = "@@ -407,7 +292,7 @@ bzr 0.18rc1  2007-07-10\n"
 
84
        hunk = hunk_from_header(header)
 
85
        self.assertEqual('bzr 0.18rc1  2007-07-10', hunk.tail)
 
86
        self.assertEqual(header, str(hunk))
76
87
 
77
88
    def makeMalformed(self, header):
78
89
        self.assertRaises(MalformedHunkHeader, hunk_from_header, header)
91
102
 
92
103
    def lineThing(self,text, type):
93
104
        line = parse_line(text)
94
 
        assert(isinstance(line, type))
95
 
        assert(str(line)==text)
 
105
        self.assertIsInstance(line, type)
 
106
        self.assertEqual(str(line), text)
96
107
 
97
108
    def makeMalformedLine(self, text):
98
109
        self.assertRaises(MalformedLine, parse_line, text)
102
113
        self.lineThing(" hello\n", ContextLine)
103
114
        self.lineThing("+hello\n", InsertLine)
104
115
        self.lineThing("-hello\n", RemoveLine)
105
 
    
 
116
 
106
117
    def testMalformedLine(self):
107
118
        """Parse invalid valid hunk lines"""
108
119
        self.makeMalformedLine("hello\n")
109
 
    
 
120
 
 
121
    def testMalformedLineNO_NL(self):
 
122
        """Parse invalid '\ No newline at end of file' in hunk lines"""
 
123
        self.makeMalformedLine(NO_NL)
 
124
 
110
125
    def compare_parsed(self, patchtext):
111
126
        lines = patchtext.splitlines(True)
112
127
        patch = parse_patch(lines.__iter__())
144
159
            if mod_pos is None:
145
160
                removals.append(orig[i])
146
161
                continue
147
 
            assert(mod[mod_pos]==orig[i])
 
162
            self.assertEqual(mod[mod_pos], orig[i])
148
163
        rem_iter = removals.__iter__()
149
164
        for hunk in patch.hunks:
150
165
            for line in hunk.lines:
153
168
                    if line.contents != next:
154
169
                        sys.stdout.write(" orig:%spatch:%s" % (next,
155
170
                                         line.contents))
156
 
                    assert(line.contents == next)
 
171
                    self.assertEqual(line.contents, next)
157
172
        self.assertRaises(StopIteration, rem_iter.next)
158
173
 
159
174
    def testPatching(self):
164
179
            ('diff-4', 'orig-4', 'mod-4'),
165
180
            ('diff-5', 'orig-5', 'mod-5'),
166
181
            ('diff-6', 'orig-6', 'mod-6'),
 
182
            ('diff-7', 'orig-7', 'mod-7'),
167
183
        ]
168
184
        for diff, orig, mod in files:
169
185
            patch = self.datafile(diff)
178
194
                count += 1
179
195
            self.assertEqual(count, len(mod_lines))
180
196
 
 
197
    def test_iter_patched_from_hunks(self):
 
198
        """Test a few patch files, and make sure they work."""
 
199
        files = [
 
200
            ('diff-2', 'orig-2', 'mod-2'),
 
201
            ('diff-3', 'orig-3', 'mod-3'),
 
202
            ('diff-4', 'orig-4', 'mod-4'),
 
203
            ('diff-5', 'orig-5', 'mod-5'),
 
204
            ('diff-6', 'orig-6', 'mod-6'),
 
205
            ('diff-7', 'orig-7', 'mod-7'),
 
206
        ]
 
207
        for diff, orig, mod in files:
 
208
            parsed = parse_patch(self.datafile(diff))
 
209
            orig_lines = list(self.datafile(orig))
 
210
            mod_lines = list(self.datafile(mod))
 
211
            iter_patched = iter_patched_from_hunks(orig_lines, parsed.hunks)
 
212
            patched_file = IterableFile(iter_patched)
 
213
            lines = []
 
214
            count = 0
 
215
            for patch_line in patched_file:
 
216
                self.assertEqual(patch_line, mod_lines[count])
 
217
                count += 1
 
218
            self.assertEqual(count, len(mod_lines))
 
219
 
181
220
    def testFirstLineRenumber(self):
182
221
        """Make sure we handle lines at the beginning of the hunk"""
183
222
        patch = parse_patch(self.datafile("insert_top.patch"))
184
 
        assert (patch.pos_in_mod(0)==1)
 
223
        self.assertEqual(patch.pos_in_mod(0), 1)
185
224
 
186
225
    def testParsePatches(self):
187
226
        """Make sure file names can be extracted from tricky unified diffs"""
217
256
        patch_files = []
218
257
        for patch in patches:
219
258
            patch_files.append((patch.oldname, patch.newname))
220
 
        assert (patch_files == filenames)
221
 
            
222
 
def test():
223
 
    patchesTestSuite = unittest.makeSuite(PatchesTester,'test')
224
 
    runner = unittest.TextTestRunner(verbosity=0)
225
 
    return runner.run(patchesTestSuite)
226
 
 
227
 
 
228
 
if __name__ == "__main__":
229
 
    test()
 
259
        self.assertEqual(patch_files, filenames)
 
260
 
 
261
    def testStatsValues(self):
 
262
        """Test the added, removed and hunks values for stats_values."""
 
263
        patch = parse_patch(self.datafile("diff"))
 
264
        self.assertEqual((299, 407, 48), patch.stats_values())