Chromium Code Reviews| 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 logging | |
| 8 import os | 9 import os |
| 9 import sys | 10 import sys |
| 10 import unittest | 11 import unittest |
| 11 | 12 |
| 12 ROOT_DIR = os.path.dirname(os.path.abspath(__file__)) | 13 ROOT_DIR = os.path.dirname(os.path.abspath(__file__)) |
| 13 sys.path.insert(0, os.path.join(ROOT_DIR, '..')) | 14 sys.path.insert(0, os.path.join(ROOT_DIR, '..')) |
| 14 | 15 |
| 15 import patch | 16 import patch |
| 16 | 17 |
| 17 | 18 |
| (...skipping 100 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
| 118 GIT_NEW = ( | 119 GIT_NEW = ( |
| 119 'diff --git a/foo b/foo\n' | 120 'diff --git a/foo b/foo\n' |
| 120 'new file mode 100644\n' | 121 'new file mode 100644\n' |
| 121 'index 0000000..5716ca5\n' | 122 'index 0000000..5716ca5\n' |
| 122 '--- /dev/null\n' | 123 '--- /dev/null\n' |
| 123 '+++ b/foo\n' | 124 '+++ b/foo\n' |
| 124 '@@ -0,0 +1 @@\n' | 125 '@@ -0,0 +1 @@\n' |
| 125 '+bar\n') | 126 '+bar\n') |
| 126 | 127 |
| 127 | 128 |
| 129 NEW = ( | |
| 130 '--- /dev/null\n' | |
| 131 '+++ foo\n' | |
| 132 '@@ -0,0 +1 @@\n' | |
| 133 '+bar\n') | |
| 134 | |
| 135 | |
| 128 class PatchTest(unittest.TestCase): | 136 class PatchTest(unittest.TestCase): |
| 129 def testFilePatchDelete(self): | 137 def testFilePatchDelete(self): |
| 130 c = patch.FilePatchDelete('foo', False) | 138 c = patch.FilePatchDelete('foo', False) |
| 139 self.assertEquals(c.filename, 'foo') | |
| 140 self.assertEquals(c.is_binary, False) | |
| 131 self.assertEquals(c.is_delete, True) | 141 self.assertEquals(c.is_delete, True) |
| 132 self.assertEquals(c.is_binary, False) | 142 self.assertEquals(c.is_new, False) |
| 133 self.assertEquals(c.filename, 'foo') | |
| 134 try: | 143 try: |
| 135 c.get() | 144 c.get() |
| 136 self.fail() | 145 self.fail() |
| 137 except NotImplementedError: | 146 except NotImplementedError: |
| 138 pass | 147 pass |
| 139 c = patch.FilePatchDelete('foo', True) | 148 c = patch.FilePatchDelete('foo', True) |
| 149 self.assertEquals(c.filename, 'foo') | |
| 150 self.assertEquals(c.is_binary, True) | |
| 140 self.assertEquals(c.is_delete, True) | 151 self.assertEquals(c.is_delete, True) |
| 141 self.assertEquals(c.is_binary, True) | 152 self.assertEquals(c.is_new, False) |
| 142 self.assertEquals(c.filename, 'foo') | |
| 143 try: | 153 try: |
| 144 c.get() | 154 c.get() |
| 145 self.fail() | 155 self.fail() |
| 146 except NotImplementedError: | 156 except NotImplementedError: |
| 147 pass | 157 pass |
| 148 | 158 |
| 149 def testFilePatchBinary(self): | 159 def testFilePatchBinary(self): |
| 150 c = patch.FilePatchBinary('foo', 'data', []) | 160 c = patch.FilePatchBinary('foo', 'data', [], False) |
|
Dirk Pranke
2011/06/03 19:17:27
Nit: I'd normally name the parameter here as well
M-A Ruel
2011/06/03 19:42:31
done everywhere
| |
| 161 self.assertEquals(c.filename, 'foo') | |
| 162 self.assertEquals(c.is_binary, True) | |
| 151 self.assertEquals(c.is_delete, False) | 163 self.assertEquals(c.is_delete, False) |
| 164 self.assertEquals(c.is_new, False) | |
| 165 self.assertEquals(c.get(), 'data') | |
| 166 | |
| 167 def testFilePatchBinaryNew(self): | |
| 168 c = patch.FilePatchBinary('foo', 'data', [], True) | |
| 169 self.assertEquals(c.filename, 'foo') | |
| 152 self.assertEquals(c.is_binary, True) | 170 self.assertEquals(c.is_binary, True) |
| 153 self.assertEquals(c.filename, 'foo') | 171 self.assertEquals(c.is_delete, False) |
| 172 self.assertEquals(c.is_new, True) | |
| 154 self.assertEquals(c.get(), 'data') | 173 self.assertEquals(c.get(), 'data') |
| 155 | 174 |
| 156 def testFilePatchDiff(self): | 175 def testFilePatchDiff(self): |
| 157 c = patch.FilePatchDiff('chrome/file.cc', SVN_PATCH, []) | 176 c = patch.FilePatchDiff('chrome/file.cc', SVN_PATCH, []) |
| 177 self.assertEquals(c.filename, 'chrome/file.cc') | |
| 178 self.assertEquals(c.is_binary, False) | |
| 158 self.assertEquals(c.is_delete, False) | 179 self.assertEquals(c.is_delete, False) |
| 159 self.assertEquals(c.is_binary, False) | |
| 160 self.assertEquals(c.filename, 'chrome/file.cc') | |
| 161 self.assertEquals(c.is_git_diff, False) | 180 self.assertEquals(c.is_git_diff, False) |
| 181 self.assertEquals(c.is_new, False) | |
| 162 self.assertEquals(c.patchlevel, 0) | 182 self.assertEquals(c.patchlevel, 0) |
| 163 self.assertEquals(c.get(), SVN_PATCH) | 183 self.assertEquals(c.get(), SVN_PATCH) |
| 184 | |
| 185 def testFilePatchDiffHeaderMode(self): | |
| 164 diff = ( | 186 diff = ( |
| 165 'diff --git a/git_cl/git-cl b/git_cl/git-cl\n' | 187 'diff --git a/git_cl/git-cl b/git_cl/git-cl\n' |
| 166 'old mode 100644\n' | 188 'old mode 100644\n' |
| 167 'new mode 100755\n') | 189 'new mode 100755\n') |
| 168 c = patch.FilePatchDiff('git_cl/git-cl', diff, []) | 190 c = patch.FilePatchDiff('git_cl/git-cl', diff, []) |
| 191 self.assertEquals(c.filename, 'git_cl/git-cl') | |
| 192 self.assertEquals(c.is_binary, False) | |
| 169 self.assertEquals(c.is_delete, False) | 193 self.assertEquals(c.is_delete, False) |
| 170 self.assertEquals(c.is_binary, False) | |
| 171 self.assertEquals(c.filename, 'git_cl/git-cl') | |
| 172 self.assertEquals(c.is_git_diff, True) | 194 self.assertEquals(c.is_git_diff, True) |
| 195 self.assertEquals(c.is_new, False) | |
| 173 self.assertEquals(c.patchlevel, 1) | 196 self.assertEquals(c.patchlevel, 1) |
| 174 self.assertEquals(c.get(), diff) | 197 self.assertEquals(c.get(), diff) |
| 198 | |
| 199 def testFilePatchDiffHeaderModeIndex(self): | |
| 175 diff = ( | 200 diff = ( |
| 176 'Index: Junk\n' | 201 'Index: Junk\n' |
| 177 'diff --git a/git_cl/git-cl b/git_cl/git-cl\n' | 202 'diff --git a/git_cl/git-cl b/git_cl/git-cl\n' |
| 178 'old mode 100644\n' | 203 'old mode 100644\n' |
| 179 'new mode 100755\n') | 204 'new mode 100755\n') |
| 180 c = patch.FilePatchDiff('git_cl/git-cl', diff, []) | 205 c = patch.FilePatchDiff('git_cl/git-cl', diff, []) |
| 206 self.assertEquals(c.filename, 'git_cl/git-cl') | |
| 207 self.assertEquals(c.is_binary, False) | |
| 181 self.assertEquals(c.is_delete, False) | 208 self.assertEquals(c.is_delete, False) |
| 182 self.assertEquals(c.is_binary, False) | |
| 183 self.assertEquals(c.filename, 'git_cl/git-cl') | |
| 184 self.assertEquals(c.is_git_diff, True) | 209 self.assertEquals(c.is_git_diff, True) |
| 210 self.assertEquals(c.is_new, False) | |
| 185 self.assertEquals(c.patchlevel, 1) | 211 self.assertEquals(c.patchlevel, 1) |
| 186 self.assertEquals(c.get(), diff) | 212 self.assertEquals(c.get(), diff) |
| 187 | 213 |
| 188 def testFilePatchBadDiff(self): | 214 def testFilePatchDiffSvnNew(self): |
| 215 # The code path is different for git and svn. | |
| 216 c = patch.FilePatchDiff('foo', NEW, []) | |
| 217 self.assertEquals(c.filename, 'foo') | |
| 218 self.assertEquals(c.is_binary, False) | |
| 219 self.assertEquals(c.is_delete, False) | |
| 220 self.assertEquals(c.is_git_diff, False) | |
| 221 self.assertEquals(c.is_new, True) | |
| 222 self.assertEquals(c.patchlevel, 0) | |
| 223 self.assertEquals(c.get(), NEW) | |
| 224 | |
| 225 def testFilePatchDiffGitNew(self): | |
| 226 # The code path is different for git and svn. | |
| 227 c = patch.FilePatchDiff('foo', GIT_NEW, []) | |
| 228 self.assertEquals(c.filename, 'foo') | |
| 229 self.assertEquals(c.is_binary, False) | |
| 230 self.assertEquals(c.is_delete, False) | |
| 231 self.assertEquals(c.is_git_diff, True) | |
| 232 self.assertEquals(c.is_new, True) | |
| 233 self.assertEquals(c.patchlevel, 1) | |
| 234 self.assertEquals(c.get(), GIT_NEW) | |
| 235 | |
| 236 def testFilePatchDiffBad(self): | |
| 189 try: | 237 try: |
| 190 patch.FilePatchDiff('foo', 'data', []) | 238 patch.FilePatchDiff('foo', 'data', []) |
| 191 self.fail() | 239 self.fail() |
| 192 except patch.UnsupportedPatchFormat: | 240 except patch.UnsupportedPatchFormat: |
| 193 pass | 241 pass |
| 194 | 242 |
| 195 def testFilePatchNoDiff(self): | 243 def testFilePatchDiffEmpty(self): |
| 196 try: | 244 try: |
| 197 patch.FilePatchDiff('foo', '', []) | 245 patch.FilePatchDiff('foo', '', []) |
| 198 self.fail() | 246 self.fail() |
| 199 except patch.UnsupportedPatchFormat: | 247 except patch.UnsupportedPatchFormat: |
| 200 pass | 248 pass |
| 201 | 249 |
| 202 def testFilePatchNoneDiff(self): | 250 def testFilePatchDiffNone(self): |
| 203 try: | 251 try: |
| 204 patch.FilePatchDiff('foo', None, []) | 252 patch.FilePatchDiff('foo', None, []) |
| 205 self.fail() | 253 self.fail() |
| 206 except patch.UnsupportedPatchFormat: | 254 except patch.UnsupportedPatchFormat: |
| 207 pass | 255 pass |
| 208 | 256 |
| 209 def testFilePatchBadDiffName(self): | 257 def testFilePatchBadDiffName(self): |
| 210 try: | 258 try: |
| 211 patch.FilePatchDiff('foo', SVN_PATCH, []) | 259 patch.FilePatchDiff('foo', SVN_PATCH, []) |
| 212 self.fail() | 260 self.fail() |
| 261 except patch.UnsupportedPatchFormat, e: | |
| 262 self.assertEquals( | |
| 263 "Can't process patch for file foo.\nUnexpected diff: chrome/file.cc.", | |
| 264 str(e)) | |
| 265 | |
| 266 def testFilePatchDiffBadHeader(self): | |
| 267 try: | |
| 268 diff = ( | |
| 269 '+++ b/foo\n' | |
| 270 '@@ -0,0 +1 @@\n' | |
| 271 '+bar\n') | |
| 272 patch.FilePatchDiff('foo', diff, []) | |
| 273 self.fail() | |
| 213 except patch.UnsupportedPatchFormat: | 274 except patch.UnsupportedPatchFormat: |
| 214 pass | 275 pass |
| 215 | 276 |
| 216 def testInvalidFilePatchDiffGit(self): | 277 def testFilePatchDiffBadGitHeader(self): |
| 278 try: | |
| 279 diff = ( | |
| 280 'diff --git a/foo b/foo\n' | |
| 281 '+++ b/foo\n' | |
| 282 '@@ -0,0 +1 @@\n' | |
| 283 '+bar\n') | |
| 284 patch.FilePatchDiff('foo', diff, []) | |
| 285 self.fail() | |
| 286 except patch.UnsupportedPatchFormat: | |
| 287 pass | |
| 288 | |
| 289 def testFilePatchDiffBadHeaderReversed(self): | |
| 290 try: | |
| 291 diff = ( | |
| 292 '+++ b/foo\n' | |
| 293 '--- b/foo\n' | |
| 294 '@@ -0,0 +1 @@\n' | |
| 295 '+bar\n') | |
| 296 patch.FilePatchDiff('foo', diff, []) | |
| 297 self.fail() | |
| 298 except patch.UnsupportedPatchFormat: | |
| 299 pass | |
| 300 | |
| 301 def testFilePatchDiffGitBadHeaderReversed(self): | |
| 302 try: | |
| 303 diff = ( | |
| 304 'diff --git a/foo b/foo\n' | |
| 305 '+++ b/foo\n' | |
| 306 '--- b/foo\n' | |
| 307 '@@ -0,0 +1 @@\n' | |
| 308 '+bar\n') | |
| 309 patch.FilePatchDiff('foo', diff, []) | |
| 310 self.fail() | |
| 311 except patch.UnsupportedPatchFormat: | |
| 312 pass | |
| 313 | |
| 314 def testFilePatchDiffInvalidGit(self): | |
| 217 try: | 315 try: |
| 218 patch.FilePatchDiff('svn_utils_test.txt', ( | 316 patch.FilePatchDiff('svn_utils_test.txt', ( |
| 219 'diff --git a/tests/svn_utils_test_data/svn_utils_test.txt ' | 317 'diff --git a/tests/svn_utils_test_data/svn_utils_test.txt ' |
| 220 'b/tests/svn_utils_test_data/svn_utils_test.txt\n' | 318 'b/tests/svn_utils_test_data/svn_utils_test.txt\n' |
| 221 'index 0e4de76..8320059 100644\n' | 319 'index 0e4de76..8320059 100644\n' |
| 222 '--- a/svn_utils_test.txt\n' | 320 '--- a/svn_utils_test.txt\n' |
| 223 '+++ b/svn_utils_test.txt\n' | 321 '+++ b/svn_utils_test.txt\n' |
| 224 '@@ -3,6 +3,7 @@ bb\n' | 322 '@@ -3,6 +3,7 @@ bb\n' |
| 225 'ccc\n' | 323 'ccc\n' |
| 226 'dd\n' | 324 'dd\n' |
| (...skipping 57 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
| 284 patch.FilePatchDiff('chrome/file.cc', SVN_PATCH, []), | 382 patch.FilePatchDiff('chrome/file.cc', SVN_PATCH, []), |
| 285 patch.FilePatchDiff( | 383 patch.FilePatchDiff( |
| 286 'tools\\clang_check/README.chromium', GIT_DELETE, []), | 384 'tools\\clang_check/README.chromium', GIT_DELETE, []), |
| 287 patch.FilePatchDiff('tools/run_local_server.sh', GIT_RENAME, []), | 385 patch.FilePatchDiff('tools/run_local_server.sh', GIT_RENAME, []), |
| 288 patch.FilePatchDiff( | 386 patch.FilePatchDiff( |
| 289 'chrome\\browser/chromeos/views/webui_menu_widget.h', | 387 'chrome\\browser/chromeos/views/webui_menu_widget.h', |
| 290 GIT_RENAME_PARTIAL, []), | 388 GIT_RENAME_PARTIAL, []), |
| 291 patch.FilePatchDiff('pp', GIT_COPY, []), | 389 patch.FilePatchDiff('pp', GIT_COPY, []), |
| 292 patch.FilePatchDiff('foo', GIT_NEW, []), | 390 patch.FilePatchDiff('foo', GIT_NEW, []), |
| 293 patch.FilePatchDelete('other/place/foo', True), | 391 patch.FilePatchDelete('other/place/foo', True), |
| 294 patch.FilePatchBinary('bar', 'data', []), | 392 patch.FilePatchBinary('bar', 'data', [], False), |
| 295 ]) | 393 ]) |
| 296 expected = [ | 394 expected = [ |
| 297 'chrome/file.cc', 'tools/clang_check/README.chromium', | 395 'chrome/file.cc', 'tools/clang_check/README.chromium', |
| 298 'tools/run_local_server.sh', | 396 'tools/run_local_server.sh', |
| 299 'chrome/browser/chromeos/views/webui_menu_widget.h', 'pp', 'foo', | 397 'chrome/browser/chromeos/views/webui_menu_widget.h', 'pp', 'foo', |
| 300 'other/place/foo', 'bar'] | 398 'other/place/foo', 'bar'] |
| 301 self.assertEquals(expected, patches.filenames) | 399 self.assertEquals(expected, patches.filenames) |
| 302 orig_name = patches.patches[0].filename | 400 orig_name = patches.patches[0].filename |
| 303 patches.set_relpath(os.path.join('a', 'bb')) | 401 patches.set_relpath(os.path.join('a', 'bb')) |
| 304 expected = [os.path.join('a', 'bb', x) for x in expected] | 402 expected = [os.path.join('a', 'bb', x) for x in expected] |
| (...skipping 14 matching lines...) Expand all Loading... | |
| 319 patches = patch.PatchSet([ | 417 patches = patch.PatchSet([ |
| 320 patch.FilePatchDiff('chrome\\file.cc', SVN_PATCH, []), | 418 patch.FilePatchDiff('chrome\\file.cc', SVN_PATCH, []), |
| 321 patch.FilePatchDelete('other\\place\\foo', True), | 419 patch.FilePatchDelete('other\\place\\foo', True), |
| 322 ]) | 420 ]) |
| 323 try: | 421 try: |
| 324 patches.set_relpath('..') | 422 patches.set_relpath('..') |
| 325 self.fail() | 423 self.fail() |
| 326 except patch.UnsupportedPatchFormat: | 424 except patch.UnsupportedPatchFormat: |
| 327 pass | 425 pass |
| 328 | 426 |
| 427 def testRelPathEmpty(self): | |
| 428 patches = patch.PatchSet([ | |
| 429 patch.FilePatchDiff('chrome\\file.cc', SVN_PATCH, []), | |
| 430 patch.FilePatchDelete('other\\place\\foo', True), | |
| 431 ]) | |
| 432 patches.set_relpath('') | |
| 433 self.assertEquals( | |
| 434 ['chrome/file.cc', 'other/place/foo'], | |
| 435 [f.filename for f in patches]) | |
| 436 | |
| 329 def testBackSlash(self): | 437 def testBackSlash(self): |
| 330 mangled_patch = SVN_PATCH.replace('chrome/', 'chrome\\') | 438 mangled_patch = SVN_PATCH.replace('chrome/', 'chrome\\') |
| 331 patches = patch.PatchSet([ | 439 patches = patch.PatchSet([ |
| 332 patch.FilePatchDiff('chrome\\file.cc', mangled_patch, []), | 440 patch.FilePatchDiff('chrome\\file.cc', mangled_patch, []), |
| 333 patch.FilePatchDelete('other\\place\\foo', True), | 441 patch.FilePatchDelete('other\\place\\foo', True), |
| 334 ]) | 442 ]) |
| 335 expected = ['chrome/file.cc', 'other/place/foo'] | 443 expected = ['chrome/file.cc', 'other/place/foo'] |
| 336 self.assertEquals(expected, patches.filenames) | 444 self.assertEquals(expected, patches.filenames) |
| 337 self.assertEquals(SVN_PATCH, patches.patches[0].get()) | 445 self.assertEquals(SVN_PATCH, patches.patches[0].get()) |
| 338 | 446 |
| (...skipping 61 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
| 400 '--- /dev/null\n' | 508 '--- /dev/null\n' |
| 401 '+++ b/natsort_test.py\n' | 509 '+++ b/natsort_test.py\n' |
| 402 '@@ -0,0 +1,1 @@\n' | 510 '@@ -0,0 +1,1 @@\n' |
| 403 '+#!/usr/bin/env python\n') | 511 '+#!/usr/bin/env python\n') |
| 404 self.assertEquals( | 512 self.assertEquals( |
| 405 [('svn:executable', '*')], | 513 [('svn:executable', '*')], |
| 406 patch.FilePatchDiff('natsort_test.py', diff, []).svn_properties) | 514 patch.FilePatchDiff('natsort_test.py', diff, []).svn_properties) |
| 407 | 515 |
| 408 | 516 |
| 409 if __name__ == '__main__': | 517 if __name__ == '__main__': |
| 518 logging.basicConfig(level= | |
| 519 [logging.WARNING, logging.INFO, logging.DEBUG][ | |
| 520 min(2, sys.argv.count('-v'))]) | |
| 410 unittest.main() | 521 unittest.main() |
| OLD | NEW |