Unreviewed, reverting r263195, r263252, and r265394.
[WebKit-https.git] / Tools / Scripts / webkitpy / common / checkout / diff_parser_unittest.py
1 # Copyright (C) 2009 Google Inc. All rights reserved.
2 #
3 # Redistribution and use in source and binary forms, with or without
4 # modification, are permitted provided that the following conditions are
5 # met:
6 #
7 #    * Redistributions of source code must retain the above copyright
8 # notice, this list of conditions and the following disclaimer.
9 #    * Redistributions in binary form must reproduce the above
10 # copyright notice, this list of conditions and the following disclaimer
11 # in the documentation and/or other materials provided with the
12 # distribution.
13 #    * Neither the name of Google Inc. nor the names of its
14 # contributors may be used to endorse or promote products derived from
15 # this software without specific prior written permission.
16 #
17 # THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
18 # "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
19 # LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
20 # A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT
21 # OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
22 # SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
23 # LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
24 # DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
25 # THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
26 # (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
27 # OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
28
29 import re
30 import unittest
31
32 import webkitpy.common.checkout.diff_parser as diff_parser
33
34 from webkitpy.common.unicode_compatibility import StringIO
35 from webkitpy.common.checkout.diff_test_data import DIFF_TEST_DATA
36
37
38 class DiffParserTest(unittest.TestCase):
39     maxDiff = None
40
41     def test_diff_parser(self, parser=None):
42         if not parser:
43             parser = diff_parser.DiffParser(DIFF_TEST_DATA.splitlines())
44         self.assertEqual(3, len(parser.files))
45
46         self.assertTrue('WebCore/rendering/style/StyleFlexibleBoxData.h' in parser.files)
47         diff = parser.files['WebCore/rendering/style/StyleFlexibleBoxData.h']
48         self.assertEqual(7, len(diff.lines))
49         # The first two unchaged lines.
50         self.assertEqual((47, 47), diff.lines[0][0:2])
51         self.assertEqual('', diff.lines[0][2])
52         self.assertEqual((48, 48), diff.lines[1][0:2])
53         self.assertEqual('    unsigned align : 3; // EBoxAlignment', diff.lines[1][2])
54         # The deleted line
55         self.assertEqual((50, 0), diff.lines[3][0:2])
56         self.assertEqual('    unsigned orient: 1; // EBoxOrient', diff.lines[3][2])
57
58         # The first file looks OK. Let's check the next, more complicated file.
59         self.assertTrue('WebCore/rendering/style/StyleRareInheritedData.cpp' in parser.files)
60         diff = parser.files['WebCore/rendering/style/StyleRareInheritedData.cpp']
61         # There are 3 chunks.
62         self.assertEqual(7 + 7 + 9, len(diff.lines))
63         # Around an added line.
64         self.assertEqual((60, 61), diff.lines[9][0:2])
65         self.assertEqual((0, 62), diff.lines[10][0:2])
66         self.assertEqual((61, 63), diff.lines[11][0:2])
67         # Look through the last chunk, which contains both add's and delete's.
68         self.assertEqual((81, 83), diff.lines[14][0:2])
69         self.assertEqual((82, 84), diff.lines[15][0:2])
70         self.assertEqual((83, 85), diff.lines[16][0:2])
71         self.assertEqual((84, 0), diff.lines[17][0:2])
72         self.assertEqual((0, 86), diff.lines[18][0:2])
73         self.assertEqual((0, 87), diff.lines[19][0:2])
74         self.assertEqual((85, 88), diff.lines[20][0:2])
75         self.assertEqual((86, 89), diff.lines[21][0:2])
76         self.assertEqual((87, 90), diff.lines[22][0:2])
77
78         # Check if a newly added file is correctly handled.
79         diff = parser.files['LayoutTests/platform/mac/fast/flexbox/box-orient-button-expected.checksum']
80         self.assertEqual(1, len(diff.lines))
81         self.assertEqual((0, 1), diff.lines[0][0:2])
82
83     def test_diff_converter(self):
84         comment_lines = [
85             "Hey guys,\n",
86             "\n",
87             "See my awesome patch below!\n",
88             "\n",
89             " - Cool Hacker\n",
90             "\n",
91             ]
92
93         revision_lines = [
94             "Subversion Revision 289799\n",
95             ]
96
97         svn_diff_lines = [
98             "Index: Tools/Scripts/webkitpy/common/checkout/diff_parser.py\n",
99             "===================================================================\n",
100             "--- Tools/Scripts/webkitpy/common/checkout/diff_parser.py\n",
101             "+++ Tools/Scripts/webkitpy/common/checkout/diff_parser.py\n",
102             "@@ -59,6 +59,7 @@ def git_diff_to_svn_diff(line):\n",
103             ]
104         self.assertEqual(diff_parser.get_diff_converter(svn_diff_lines), diff_parser.svn_diff_to_svn_diff)
105         self.assertEqual(diff_parser.get_diff_converter(comment_lines + svn_diff_lines), diff_parser.svn_diff_to_svn_diff)
106         self.assertEqual(diff_parser.get_diff_converter(revision_lines + svn_diff_lines), diff_parser.svn_diff_to_svn_diff)
107
108         git_diff_lines = [
109             "diff --git a/Tools/Scripts/webkitpy/common/checkout/diff_parser.py b/Tools/Scripts/webkitpy/common/checkout/diff_parser.py\n",
110             "index 3c5b45b..0197ead 100644\n",
111             "--- a/Tools/Scripts/webkitpy/common/checkout/diff_parser.py\n",
112             "+++ b/Tools/Scripts/webkitpy/common/checkout/diff_parser.py\n",
113             "@@ -59,6 +59,7 @@ def git_diff_to_svn_diff(line):\n",
114             ]
115         self.assertEqual(diff_parser.get_diff_converter(git_diff_lines), diff_parser.git_diff_to_svn_diff)
116         self.assertEqual(diff_parser.get_diff_converter(comment_lines + git_diff_lines), diff_parser.git_diff_to_svn_diff)
117         self.assertEqual(diff_parser.get_diff_converter(revision_lines + git_diff_lines), diff_parser.git_diff_to_svn_diff)
118
119     def test_git_mnemonicprefix(self):
120         p = re.compile(r' ([a|b])/')
121
122         prefixes = [
123             {'a': 'i', 'b': 'w'},  # git-diff (compares the (i)ndex and the (w)ork tree)
124             {'a': 'c', 'b': 'w'},  # git-diff HEAD (compares a (c)ommit and the (w)ork tree)
125             {'a': 'c', 'b': 'i'},  # git diff --cached (compares a (c)ommit and the (i)ndex)
126             {'a': 'o', 'b': 'w'},  # git-diff HEAD:file1 file2 (compares an (o)bject and a (w)ork tree entity)
127             {'a': '1', 'b': '2'},  # git diff --no-index a b (compares two non-git things (1) and (2))
128         ]
129
130         for prefix in prefixes:
131             patch = p.sub(lambda x: " %s/" % prefix[x.group(1)], DIFF_TEST_DATA)
132             self.test_diff_parser(diff_parser.DiffParser(patch.splitlines()))
133
134     def test_git_diff_to_svn_diff(self):
135         output = """\
136 Index: Tools/Scripts/webkitpy/common/checkout/diff_parser.py
137 ===================================================================
138 --- Tools/Scripts/webkitpy/common/checkout/diff_parser.py
139 +++ Tools/Scripts/webkitpy/common/checkout/diff_parser.py
140 @@ -59,6 +59,7 @@ def git_diff_to_svn_diff(line):
141  A
142  B
143  C
144 +D
145  E
146  F
147 """
148
149         inputfmt = StringIO("""\
150 diff --git a/Tools/Scripts/webkitpy/common/checkout/diff_parser.py b/Tools/Scripts/webkitpy/common/checkout/diff_parser.py
151 index 2ed552c4555db72df16b212547f2c125ae301a04..72870482000c0dba64ce4300ed782c03ee79b74f 100644
152 --- a/Tools/Scripts/webkitpy/common/checkout/diff_parser.py
153 +++ b/Tools/Scripts/webkitpy/common/checkout/diff_parser.py
154 @@ -59,6 +59,7 @@ def git_diff_to_svn_diff(line):
155  A
156  B
157  C
158 +D
159  E
160  F
161 """)
162         shortfmt = StringIO("""\
163 diff --git a/Tools/Scripts/webkitpy/common/checkout/diff_parser.py b/Tools/Scripts/webkitpy/common/checkout/diff_parser.py
164 index b48b162..f300960 100644
165 --- a/Tools/Scripts/webkitpy/common/checkout/diff_parser.py
166 +++ b/Tools/Scripts/webkitpy/common/checkout/diff_parser.py
167 @@ -59,6 +59,7 @@ def git_diff_to_svn_diff(line):
168  A
169  B
170  C
171 +D
172  E
173  F
174 """)
175
176         self.assertMultiLineEqual(output, ''.join(diff_parser.git_diff_to_svn_diff(x) for x in shortfmt.readlines()))
177         self.assertMultiLineEqual(output, ''.join(diff_parser.git_diff_to_svn_diff(x) for x in inputfmt.readlines()))