OLD | NEW |
1 #!/usr/bin/env python | 1 #!/usr/bin/env python |
2 # Copyright (c) 2011 The Chromium Authors. All rights reserved. | 2 # Copyright (c) 2011 The Chromium Authors. All rights reserved. |
3 # Use of this source code is governed by a BSD-style license that can be | 3 # Use of this source code is governed by a BSD-style license that can be |
4 # found in the LICENSE file. | 4 # found in the LICENSE file. |
5 | 5 |
6 """Unit tests for patch.py.""" | 6 """Unit tests for patch.py.""" |
7 | 7 |
8 import os | 8 import os |
9 import sys | 9 import sys |
10 import unittest | 10 import unittest |
11 | 11 |
12 ROOT_DIR = os.path.dirname(os.path.abspath(__file__)) | 12 ROOT_DIR = os.path.dirname(os.path.abspath(__file__)) |
13 sys.path.insert(0, os.path.join(ROOT_DIR, '..')) | 13 sys.path.insert(0, os.path.join(ROOT_DIR, '..')) |
14 | 14 |
15 import patch | 15 import patch |
16 | 16 |
17 | 17 |
| 18 SVN_PATCH = ( |
| 19 'Index: chrome/file.cc\n' |
| 20 '===================================================================\n' |
| 21 '--- chrome/file.cc\t(revision 74690)\n' |
| 22 '+++ chrome/file.cc\t(working copy)\n' |
| 23 '@@ -80,10 +80,13 @@\n' |
| 24 ' // Foo\n' |
| 25 ' // Bar\n' |
| 26 ' void foo() {\n' |
| 27 '- return bar;\n' |
| 28 '+ return foo;\n' |
| 29 ' }\n' |
| 30 ' \n' |
| 31 ' \n') |
| 32 |
| 33 |
| 34 GIT_PATCH = ( |
| 35 'diff --git a/chrome/file.cc b/chrome/file.cc\n' |
| 36 'index 0e4de76..8320059 100644\n' |
| 37 '--- a/chrome/file.cc\n' |
| 38 '+++ b/chrome/file.cc\n' |
| 39 '@@ -3,6 +3,7 @@ bb\n' |
| 40 ' ccc\n' |
| 41 ' dd\n' |
| 42 ' e\n' |
| 43 '+FOO!\n' |
| 44 ' ff\n' |
| 45 ' ggg\n' |
| 46 ' hh\n') |
| 47 |
| 48 |
| 49 # http://codereview.chromium.org/download/issue6368055_22_29.diff |
| 50 GIT_DELETE = ( |
| 51 'Index: tools/clang_check/README.chromium\n' |
| 52 'diff --git a/tools/clang_check/README.chromium ' |
| 53 'b/tools/clang_check/README.chromium\n' |
| 54 'deleted file mode 100644\n' |
| 55 'index fcaa7e0e94bb604a026c4f478fecb1c5796f5413..' |
| 56 '0000000000000000000000000000000000000000\n' |
| 57 '--- a/tools/clang_check/README.chromium\n' |
| 58 '+++ /dev/null\n' |
| 59 '@@ -1,9 +0,0 @@\n' |
| 60 '-These are terrible, terrible hacks.\n' |
| 61 '-\n' |
| 62 '-They are meant to be temporary. clang currently doesn\'t allow running a ' |
| 63 'plugin\n' |
| 64 '-AND doing the normal codegen process. We want our syntax complaining ' |
| 65 'plugins to\n' |
| 66 '-run during normal compile, but for now, you can user run_plugin.sh to ' |
| 67 'hack the\n' |
| 68 '-build system to do a syntax check.\n' |
| 69 '-\n' |
| 70 '-Also see http://code.google.com/p/chromium/wiki/WritingClangPlugins\n' |
| 71 '-\n') |
| 72 |
| 73 |
| 74 # http://codereview.chromium.org/download/issue6250123_3013_6010.diff |
| 75 GIT_RENAME_PARTIAL = ( |
| 76 'Index: chrome/browser/chromeos/views/webui_menu_widget.h\n' |
| 77 'diff --git a/chrome/browser/chromeos/views/domui_menu_widget.h ' |
| 78 'b/chrome/browser/chromeos/views/webui_menu_widget.h\n' |
| 79 'similarity index 79%\n' |
| 80 'rename from chrome/browser/chromeos/views/domui_menu_widget.h\n' |
| 81 'rename to chrome/browser/chromeos/views/webui_menu_widget.h\n' |
| 82 'index 095d4c474fd9718f5aebfa41a1ccb2d951356d41..' |
| 83 '157925075434b590e8acaaf605a64f24978ba08b 100644\n' |
| 84 '--- a/chrome/browser/chromeos/views/domui_menu_widget.h\n' |
| 85 '+++ b/chrome/browser/chromeos/views/webui_menu_widget.h\n' |
| 86 '@@ -1,9 +1,9 @@\n' |
| 87 '-// Copyright (c) 2010 The Chromium Authors. All rights reserved.\n' |
| 88 '+// Copyright (c) 2011 The Chromium Authors. All rights reserved.\n' |
| 89 ' // Use of this source code is governed by a BSD-style license that can be' |
| 90 '\n' |
| 91 ' // found in the LICENSE file.\n' |
| 92 ' \n' |
| 93 '-#ifndef CHROME_BROWSER_CHROMEOS_VIEWS_DOMUI_MENU_WIDGET_H_\n' |
| 94 '-#define CHROME_BROWSER_CHROMEOS_VIEWS_DOMUI_MENU_WIDGET_H_\n' |
| 95 '+#ifndef CHROME_BROWSER_CHROMEOS_VIEWS_WEBUI_MENU_WIDGET_H_\n' |
| 96 '+#define CHROME_BROWSER_CHROMEOS_VIEWS_WEBUI_MENU_WIDGET_H_\n' |
| 97 ' #pragma once\n' |
| 98 ' \n' |
| 99 ' #include <string>\n') |
| 100 |
| 101 |
| 102 # http://codereview.chromium.org/download/issue6287022_3001_4010.diff |
| 103 GIT_RENAME = ( |
| 104 'Index: tools/run_local_server.sh\n' |
| 105 'diff --git a/tools/run_local_server.py b/tools/run_local_server.sh\n' |
| 106 'similarity index 100%\n' |
| 107 'rename from tools/run_local_server.py\n' |
| 108 'rename to tools/run_local_server.sh\n') |
| 109 |
| 110 |
| 111 GIT_COPY = ( |
| 112 'diff --git a/PRESUBMIT.py b/pp\n' |
| 113 'similarity index 100%\n' |
| 114 'copy from PRESUBMIT.py\n' |
| 115 'copy to pp\n') |
| 116 |
| 117 |
| 118 GIT_NEW = ( |
| 119 'diff --git a/foo b/foo\n' |
| 120 'new file mode 100644\n' |
| 121 'index 0000000..5716ca5\n' |
| 122 '--- /dev/null\n' |
| 123 '+++ b/foo\n' |
| 124 '@@ -0,0 +1 @@\n' |
| 125 '+bar\n') |
| 126 |
| 127 |
18 class PatchTest(unittest.TestCase): | 128 class PatchTest(unittest.TestCase): |
19 def testFilePatchDelete(self): | 129 def testFilePatchDelete(self): |
20 c = patch.FilePatchDelete('foo', False) | 130 c = patch.FilePatchDelete('foo', False) |
21 self.assertEquals(c.is_delete, True) | 131 self.assertEquals(c.is_delete, True) |
22 self.assertEquals(c.is_binary, False) | 132 self.assertEquals(c.is_binary, False) |
23 self.assertEquals(c.filename, 'foo') | 133 self.assertEquals(c.filename, 'foo') |
24 try: | 134 try: |
25 c.get() | 135 c.get() |
26 self.fail() | 136 self.fail() |
27 except NotImplementedError: | 137 except NotImplementedError: |
28 pass | 138 pass |
29 c = patch.FilePatchDelete('foo', True) | 139 c = patch.FilePatchDelete('foo', True) |
30 self.assertEquals(c.is_delete, True) | 140 self.assertEquals(c.is_delete, True) |
31 self.assertEquals(c.is_binary, True) | 141 self.assertEquals(c.is_binary, True) |
32 self.assertEquals(c.filename, 'foo') | 142 self.assertEquals(c.filename, 'foo') |
33 try: | 143 try: |
34 c.get() | 144 c.get() |
35 self.fail() | 145 self.fail() |
36 except NotImplementedError: | 146 except NotImplementedError: |
37 pass | 147 pass |
38 | 148 |
39 def testFilePatchBinary(self): | 149 def testFilePatchBinary(self): |
40 c = patch.FilePatchBinary('foo', 'data', []) | 150 c = patch.FilePatchBinary('foo', 'data', []) |
41 self.assertEquals(c.is_delete, False) | 151 self.assertEquals(c.is_delete, False) |
42 self.assertEquals(c.is_binary, True) | 152 self.assertEquals(c.is_binary, True) |
43 self.assertEquals(c.filename, 'foo') | 153 self.assertEquals(c.filename, 'foo') |
44 self.assertEquals(c.get(), 'data') | 154 self.assertEquals(c.get(), 'data') |
45 | 155 |
46 def testFilePatchDiff(self): | 156 def testFilePatchDiff(self): |
47 c = patch.FilePatchDiff('foo', 'data', []) | 157 c = patch.FilePatchDiff('chrome/file.cc', SVN_PATCH, []) |
48 self.assertEquals(c.is_delete, False) | 158 self.assertEquals(c.is_delete, False) |
49 self.assertEquals(c.is_binary, False) | 159 self.assertEquals(c.is_binary, False) |
50 self.assertEquals(c.filename, 'foo') | 160 self.assertEquals(c.filename, 'chrome/file.cc') |
51 self.assertEquals(c.is_git_diff, False) | 161 self.assertEquals(c.is_git_diff, False) |
52 self.assertEquals(c.patchlevel, 0) | 162 self.assertEquals(c.patchlevel, 0) |
53 self.assertEquals(c.get(), 'data') | 163 self.assertEquals(c.get(), SVN_PATCH) |
54 diff = ( | 164 diff = ( |
55 'diff --git a/git_cl/git-cl b/git_cl/git-cl\n' | 165 'diff --git a/git_cl/git-cl b/git_cl/git-cl\n' |
56 'old mode 100644\n' | 166 'old mode 100644\n' |
57 'new mode 100755\n') | 167 'new mode 100755\n') |
58 c = patch.FilePatchDiff('git_cl/git-cl', diff, []) | 168 c = patch.FilePatchDiff('git_cl/git-cl', diff, []) |
59 self.assertEquals(c.is_delete, False) | 169 self.assertEquals(c.is_delete, False) |
60 self.assertEquals(c.is_binary, False) | 170 self.assertEquals(c.is_binary, False) |
61 self.assertEquals(c.filename, 'git_cl/git-cl') | 171 self.assertEquals(c.filename, 'git_cl/git-cl') |
62 self.assertEquals(c.is_git_diff, True) | 172 self.assertEquals(c.is_git_diff, True) |
63 self.assertEquals(c.patchlevel, 1) | 173 self.assertEquals(c.patchlevel, 1) |
64 self.assertEquals(c.get(), diff) | 174 self.assertEquals(c.get(), diff) |
65 diff = ( | 175 diff = ( |
66 'Index: Junk\n' | 176 'Index: Junk\n' |
67 'diff --git a/git_cl/git-cl b/git_cl/git-cl\n' | 177 'diff --git a/git_cl/git-cl b/git_cl/git-cl\n' |
68 'old mode 100644\n' | 178 'old mode 100644\n' |
69 'new mode 100755\n') | 179 'new mode 100755\n') |
70 c = patch.FilePatchDiff('git_cl/git-cl', diff, []) | 180 c = patch.FilePatchDiff('git_cl/git-cl', diff, []) |
71 self.assertEquals(c.is_delete, False) | 181 self.assertEquals(c.is_delete, False) |
72 self.assertEquals(c.is_binary, False) | 182 self.assertEquals(c.is_binary, False) |
73 self.assertEquals(c.filename, 'git_cl/git-cl') | 183 self.assertEquals(c.filename, 'git_cl/git-cl') |
74 self.assertEquals(c.is_git_diff, True) | 184 self.assertEquals(c.is_git_diff, True) |
75 self.assertEquals(c.patchlevel, 1) | 185 self.assertEquals(c.patchlevel, 1) |
76 self.assertEquals(c.get(), diff) | 186 self.assertEquals(c.get(), diff) |
77 | 187 |
| 188 def testFilePatchBadDiff(self): |
| 189 try: |
| 190 patch.FilePatchDiff('foo', 'data', []) |
| 191 self.fail() |
| 192 except patch.UnsupportedPatchFormat: |
| 193 pass |
| 194 |
| 195 def testFilePatchBadDiffName(self): |
| 196 try: |
| 197 patch.FilePatchDiff('foo', SVN_PATCH, []) |
| 198 self.fail() |
| 199 except patch.UnsupportedPatchFormat: |
| 200 pass |
| 201 |
78 def testInvalidFilePatchDiffGit(self): | 202 def testInvalidFilePatchDiffGit(self): |
79 try: | 203 try: |
80 patch.FilePatchDiff('svn_utils_test.txt', ( | 204 patch.FilePatchDiff('svn_utils_test.txt', ( |
81 'diff --git a/tests/svn_utils_test_data/svn_utils_test.txt ' | 205 'diff --git a/tests/svn_utils_test_data/svn_utils_test.txt ' |
82 'b/tests/svn_utils_test_data/svn_utils_test.txt\n' | 206 'b/tests/svn_utils_test_data/svn_utils_test.txt\n' |
83 'index 0e4de76..8320059 100644\n' | 207 'index 0e4de76..8320059 100644\n' |
84 '--- a/svn_utils_test.txt\n' | 208 '--- a/svn_utils_test.txt\n' |
85 '+++ b/svn_utils_test.txt\n' | 209 '+++ b/svn_utils_test.txt\n' |
86 '@@ -3,6 +3,7 @@ bb\n' | 210 '@@ -3,6 +3,7 @@ bb\n' |
87 'ccc\n' | 211 'ccc\n' |
(...skipping 42 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
130 'hh\n'), | 254 'hh\n'), |
131 []) | 255 []) |
132 self.fail() | 256 self.fail() |
133 except patch.UnsupportedPatchFormat: | 257 except patch.UnsupportedPatchFormat: |
134 pass | 258 pass |
135 | 259 |
136 def testValidSvn(self): | 260 def testValidSvn(self): |
137 # pylint: disable=R0201 | 261 # pylint: disable=R0201 |
138 # Method could be a function | 262 # Method could be a function |
139 # Should not throw. | 263 # Should not throw. |
140 patch.FilePatchDiff('chrome/file.cc', ( | 264 p = patch.FilePatchDiff('chrome/file.cc', SVN_PATCH, []) |
141 'Index: chrome/file.cc\n' | 265 lines = SVN_PATCH.splitlines(True) |
142 '===================================================================\n' | 266 header = ''.join(lines[:4]) |
143 '--- chrome/file.cc\t(revision 74690)\n' | 267 hunks = ''.join(lines[4:]) |
144 '+++ chrome/file.cc\t(working copy)\n' | 268 self.assertEquals(header, p.diff_header) |
145 '@@ -80,10 +80,13 @@\n' | 269 self.assertEquals(hunks, p.diff_hunks) |
146 ' // Foo\n' | 270 self.assertEquals(SVN_PATCH, p.get()) |
147 ' // Bar\n' | 271 |
148 ' void foo() {\n' | 272 def testValidSvnNew(self): |
149 '- return bar;\n' | 273 text = '--- /dev/null\t2\n+++ chrome/file.cc\tfoo\n' |
150 '+ return foo;\n' | 274 p = patch.FilePatchDiff('chrome/file.cc', text, []) |
151 ' }\n' | 275 self.assertEquals(text, p.diff_header) |
152 ' \n' | 276 self.assertEquals('', p.diff_hunks) |
153 ' \n'), []) | 277 self.assertEquals(text, p.get()) |
154 patch.FilePatchDiff('chrome/file.cc', ( | 278 |
155 '--- /dev/null\t2\n' | 279 def testValidSvnDelete(self): |
156 '+++ chrome/file.cc\tfoo\n'), []) | 280 text = '--- chrome/file.cc\tbar\n+++ /dev/null\tfoo\n' |
157 patch.FilePatchDiff('chrome/file.cc', ( | 281 p = patch.FilePatchDiff('chrome/file.cc', text, []) |
158 '--- chrome/file.cc\tbar\n' | 282 self.assertEquals(text, p.diff_header) |
159 '+++ /dev/null\tfoo\n'), []) | 283 self.assertEquals('', p.diff_hunks) |
| 284 self.assertEquals(text, p.get()) |
| 285 |
| 286 def testRelPath(self): |
| 287 patches = patch.PatchSet([ |
| 288 patch.FilePatchDiff('chrome/file.cc', SVN_PATCH, []), |
| 289 patch.FilePatchDiff( |
| 290 'tools\\clang_check/README.chromium', GIT_DELETE, []), |
| 291 patch.FilePatchDiff('tools/run_local_server.sh', GIT_RENAME, []), |
| 292 patch.FilePatchDiff( |
| 293 'chrome\\browser/chromeos/views/webui_menu_widget.h', |
| 294 GIT_RENAME_PARTIAL, []), |
| 295 patch.FilePatchDiff('pp', GIT_COPY, []), |
| 296 patch.FilePatchDiff('foo', GIT_NEW, []), |
| 297 patch.FilePatchDelete('other/place/foo', True), |
| 298 patch.FilePatchBinary('bar', 'data', []), |
| 299 ]) |
| 300 expected = [ |
| 301 'chrome/file.cc', 'tools/clang_check/README.chromium', |
| 302 'tools/run_local_server.sh', |
| 303 'chrome/browser/chromeos/views/webui_menu_widget.h', 'pp', 'foo', |
| 304 'other/place/foo', 'bar'] |
| 305 self.assertEquals(expected, patches.filenames) |
| 306 orig_name = patches.patches[0].filename |
| 307 patches.set_relpath(os.path.join('a', 'bb')) |
| 308 expected = [os.path.join('a', 'bb', x) for x in expected] |
| 309 self.assertEquals(expected, patches.filenames) |
| 310 # Make sure each header is updated accordingly. |
| 311 header = [] |
| 312 new_name = os.path.join('a', 'bb', orig_name) |
| 313 for line in SVN_PATCH.splitlines(True): |
| 314 if line.startswith('@@'): |
| 315 break |
| 316 if line[:3] in ('---', '+++', 'Ind'): |
| 317 line = line.replace(orig_name, new_name) |
| 318 header.append(line) |
| 319 header = ''.join(header) |
| 320 self.assertEquals(header, patches.patches[0].diff_header) |
| 321 |
| 322 def testRelPathBad(self): |
| 323 patches = patch.PatchSet([ |
| 324 patch.FilePatchDiff('chrome\\file.cc', SVN_PATCH, []), |
| 325 patch.FilePatchDelete('other\\place\\foo', True), |
| 326 ]) |
| 327 try: |
| 328 patches.set_relpath('..') |
| 329 self.fail() |
| 330 except patch.UnsupportedPatchFormat: |
| 331 pass |
| 332 |
| 333 def testBackSlash(self): |
| 334 mangled_patch = SVN_PATCH.replace('chrome/', 'chrome\\') |
| 335 patches = patch.PatchSet([ |
| 336 patch.FilePatchDiff('chrome\\file.cc', mangled_patch, []), |
| 337 patch.FilePatchDelete('other\\place\\foo', True), |
| 338 ]) |
| 339 expected = ['chrome/file.cc', 'other/place/foo'] |
| 340 self.assertEquals(expected, patches.filenames) |
| 341 self.assertEquals(SVN_PATCH, patches.patches[0].get()) |
| 342 |
| 343 def testGitPatches(self): |
| 344 # Shouldn't throw. |
| 345 patch.FilePatchDiff('tools/clang_check/README.chromium', GIT_DELETE, []) |
| 346 patch.FilePatchDiff('tools/run_local_server.sh', GIT_RENAME, []) |
| 347 patch.FilePatchDiff( |
| 348 'chrome/browser/chromeos/views/webui_menu_widget.h', |
| 349 GIT_RENAME_PARTIAL, []) |
| 350 patch.FilePatchDiff('pp', GIT_COPY, []) |
| 351 patch.FilePatchDiff('foo', GIT_NEW, []) |
| 352 self.assertTrue(True) |
160 | 353 |
161 | 354 |
162 if __name__ == '__main__': | 355 if __name__ == '__main__': |
163 unittest.main() | 356 unittest.main() |
OLD | NEW |