~bzr-pqm/bzr/bzr.dev

« back to all changes in this revision

Viewing changes to bzrlib/tests/test_patches.py

  • Committer: Vincent Ladeuil
  • Date: 2010-02-10 15:46:03 UTC
  • mfrom: (4985.3.21 update)
  • mto: This revision was merged to the branch mainline in revision 5021.
  • Revision ID: v.ladeuil+lp@free.fr-20100210154603-k4no1gvfuqpzrw7p
Update performs two merges in a more logical order but stop on conflicts

Show diffs side-by-side

added added

removed removed

Lines of Context:
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
 
16
# Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
17
17
 
18
18
 
19
19
import os.path
21
21
from bzrlib.tests import TestCase
22
22
 
23
23
from bzrlib.iterablefile import IterableFile
24
 
from bzrlib.patches import (MalformedLine, 
25
 
                            MalformedHunkHeader, 
26
 
                            MalformedPatchHeader, 
27
 
                            ContextLine, 
 
24
from bzrlib.patches import (MalformedLine,
 
25
                            MalformedHunkHeader,
 
26
                            MalformedPatchHeader,
 
27
                            BinaryPatch,
 
28
                            BinaryFiles,
 
29
                            Patch,
 
30
                            ContextLine,
28
31
                            InsertLine,
29
 
                            RemoveLine, 
30
 
                            difference_index, 
 
32
                            RemoveLine,
 
33
                            difference_index,
31
34
                            get_patch_names,
32
 
                            hunk_from_header, 
33
 
                            iter_patched, 
 
35
                            hunk_from_header,
 
36
                            iter_patched,
34
37
                            iter_patched_from_hunks,
35
38
                            parse_line,
36
39
                            parse_patch,
37
 
                            parse_patches)
 
40
                            parse_patches,
 
41
                            NO_NL)
38
42
 
39
43
 
40
44
class PatchesTester(TestCase):
41
45
 
42
46
    def datafile(self, filename):
43
 
        data_path = os.path.join(os.path.dirname(__file__), 
 
47
        data_path = os.path.join(os.path.dirname(__file__),
44
48
                                 "test_patches_data", filename)
45
49
        return file(data_path, "rb")
46
50
 
 
51
    def data_lines(self, filename):
 
52
        datafile = self.datafile(filename)
 
53
        try:
 
54
            return datafile.readlines()
 
55
        finally:
 
56
            datafile.close()
 
57
 
47
58
    def testValidPatchHeader(self):
48
59
        """Parse a valid patch header"""
49
60
        lines = "--- orig/commands.py\n+++ mod/dommands.py\n".split('\n')
112
123
        self.lineThing(" hello\n", ContextLine)
113
124
        self.lineThing("+hello\n", InsertLine)
114
125
        self.lineThing("-hello\n", RemoveLine)
115
 
    
 
126
 
116
127
    def testMalformedLine(self):
117
128
        """Parse invalid valid hunk lines"""
118
129
        self.makeMalformedLine("hello\n")
119
 
    
 
130
 
 
131
    def testMalformedLineNO_NL(self):
 
132
        """Parse invalid '\ No newline at end of file' in hunk lines"""
 
133
        self.makeMalformedLine(NO_NL)
 
134
 
120
135
    def compare_parsed(self, patchtext):
121
136
        lines = patchtext.splitlines(True)
122
137
        patch = parse_patch(lines.__iter__())
131
146
        patchtext = self.datafile("patchtext.patch").read()
132
147
        self.compare_parsed(patchtext)
133
148
 
 
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
 
134
173
    def testInit(self):
135
174
        """Handle patches missing half the position, range tuple"""
136
175
        patchtext = \
174
213
            ('diff-4', 'orig-4', 'mod-4'),
175
214
            ('diff-5', 'orig-5', 'mod-5'),
176
215
            ('diff-6', 'orig-6', 'mod-6'),
 
216
            ('diff-7', 'orig-7', 'mod-7'),
177
217
        ]
178
218
        for diff, orig, mod in files:
179
219
            patch = self.datafile(diff)
188
228
                count += 1
189
229
            self.assertEqual(count, len(mod_lines))
190
230
 
 
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
 
191
236
    def test_iter_patched_from_hunks(self):
192
237
        """Test a few patch files, and make sure they work."""
193
238
        files = [
196
241
            ('diff-4', 'orig-4', 'mod-4'),
197
242
            ('diff-5', 'orig-5', 'mod-5'),
198
243
            ('diff-6', 'orig-6', 'mod-6'),
 
244
            ('diff-7', 'orig-7', 'mod-7'),
199
245
        ]
200
246
        for diff, orig, mod in files:
201
247
            parsed = parse_patch(self.datafile(diff))