1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 370 371 372 373 374 375 376 377 378 379 380 381 382 383 384 385 386 387 388 389 390 391 392 393 394 395 396 397 398 399 400 401 402 403 404 405 406 407 408 409 410 411 412 413 414 415 416 417 418 419 420 421 422 423 424 425 426 427 428 429 430 431 432 433 434 435 436 437 438 439 440 441 442 443 444 445 446 447 448 449 450 451 452 453 454 455 456 457 458 459 460 461 462 463 464 465 466 467 468 469 470 471 472 473 474 475 476 477 478 479 480 481 482 483 484 485 486 487 488 489 490 491 492 493 494 495 496 497 498 499 500 501 502 503 504 505 506 507 508 509 510 511 512 513 514 515 516 517 518 519 520 521 522 523 524 525 526 527 528 529 530 531 532 533 534 535 536 537 538 539 540 541 542 543 544 545 546 547 548 549 550 551 552 553 554 555 556 557 558 559 560 561 562 563 564 565 566 567 568 569 570 571 572 573 574 575 576 577 578 579 580 581 582 583 584 585 586 587 588 589 590 591 592 593 594 595 596 597 598 599 600 601 602 603 604 605 606 607 608 609 610 611 612 613 614 615 616 617 618 619 620 621 622 623 624 625 626 627 628 629 630 631 632 633 634 635 636 637 638 639 640 641 642 643 644 645 646 647 648 649 650 651 652 653 654 655 656 657 658 659 660 661 662 663 664 665 666 667 668 669 670 671 672 673 674 675 676 677 678 679 680 681 682 683 684 685 686 687 688 689 690 691 692 693 694 695 696 697 698 699 700 701 702 703 704 705 706 707 708 709 710 711 712 713 714 715 716 717 718 719 720 721 722 723 724 725 726 727 728 729 730 731 732 733 734 735 736 737 738 739 740 741 742 743 744 745 746 747 748 749 750 751 752 753 754 755 756 757 758 759 760 761 762 763 764 765
|
#!/usr/bin/env vpython3
# Copyright 2020 The Chromium Authors
# Use of this source code is governed by a BSD-style license that can be
# found in the LICENSE file.
import unittest
import apply_edits
def _FindPHB(filepath):
return apply_edits._FindPrimaryHeaderBasename(filepath)
class FindPrimaryHeaderBasenameTest(unittest.TestCase):
def testNoOpOnHeader(self):
self.assertIsNone(_FindPHB('bar.h'))
self.assertIsNone(_FindPHB('foo/bar.h'))
def testStripDirectories(self):
self.assertEqual('bar', _FindPHB('foo/bar.cc'))
def testStripPlatformSuffix(self):
self.assertEqual('bar', _FindPHB('bar_posix.cc'))
self.assertEqual('bar', _FindPHB('bar_unittest.cc'))
def testStripTestSuffix(self):
self.assertEqual('bar', _FindPHB('bar_browsertest.cc'))
self.assertEqual('bar', _FindPHB('bar_unittest.cc'))
def testStripPlatformAndTestSuffix(self):
self.assertEqual('bar', _FindPHB('bar_uitest_aura.cc'))
self.assertEqual('bar', _FindPHB('bar_linux_unittest.cc'))
def testNoSuffixStrippingWithoutUnderscore(self):
self.assertEqual('barunittest', _FindPHB('barunittest.cc'))
def _ApplyEdit(old_contents_string,
edit,
contents_filepath="some_file.cc",
last_edit=None):
if last_edit is not None:
assert (last_edit > edit) # Test or prod caller should ensure.
ba = bytearray()
ba.extend(old_contents_string.encode('utf-8'))
return apply_edits._ApplySingleEdit(contents_filepath,
old_contents_string.encode("utf-8"), edit,
last_edit).decode("utf-8")
def _InsertHeader(old_contents,
contents_filepath='foo/impl.cc',
new_header_path='new/header.h'):
edit = apply_edits.Edit("include-user-header", -1, -1,
new_header_path.encode("utf-8"))
return _ApplyEdit(old_contents, edit, contents_filepath)
class InsertIncludeHeaderTest(unittest.TestCase):
def _assertEqualContents(self, expected, actual):
if expected != actual:
print("####################### EXPECTED:")
print(expected)
print("####################### ACTUAL:")
print(actual)
print("####################### END.")
self.assertEqual(expected, actual)
def testSkippingCppComments(self):
old_contents = '''
// Copyright info here.
#include "old/header.h"
'''
expected_new_contents = '''
// Copyright info here.
#include "new/header.h"
#include "old/header.h"
'''
new_header_line = '#include "new/header.h'
self._assertEqualContents(expected_new_contents,
_InsertHeader(old_contents))
def testSkippingCppComments_DocCommentForStruct(self):
""" This is a regression test for https://crbug.com/1175684 """
old_contents = '''
// Copyright blah blah...
#ifndef SANDBOX_LINUX_SYSTEM_HEADERS_LINUX_FILTER_H_
#define SANDBOX_LINUX_SYSTEM_HEADERS_LINUX_FILTER_H_
#include <stdint.h>
// Doc comment for a struct.
// Multiline.
struct sock_filter {
uint16_t code;
};
'''
expected_new_contents = '''
// Copyright blah blah...
#ifndef SANDBOX_LINUX_SYSTEM_HEADERS_LINUX_FILTER_H_
#define SANDBOX_LINUX_SYSTEM_HEADERS_LINUX_FILTER_H_
#include <stdint.h>
#include "new/header.h"
// Doc comment for a struct.
// Multiline.
struct sock_filter {
uint16_t code;
};
'''
new_header_line = '#include "new/header.h'
self._assertEqualContents(expected_new_contents,
_InsertHeader(old_contents))
def testSkippingCppComments_DocCommentForStruct2(self):
""" This is a regression test for https://crbug.com/1175684 """
old_contents = '''
// Copyright blah blah...
// Doc comment for a struct.
struct sock_filter {
uint16_t code;
};
'''
expected_new_contents = '''
// Copyright blah blah...
#include "new/header.h"
// Doc comment for a struct.
struct sock_filter {
uint16_t code;
};
'''
new_header_line = '#include "new/header.h'
self._assertEqualContents(expected_new_contents,
_InsertHeader(old_contents))
def testSkippingCppComments_DocCommentForStruct3(self):
""" This is a regression test for https://crbug.com/1175684 """
old_contents = '''
// Doc comment for a struct.
struct sock_filter {
uint16_t code;
};
'''
expected_new_contents = '''
#include "new/header.h"
// Doc comment for a struct.
struct sock_filter {
uint16_t code;
};
'''
new_header_line = '#include "new/header.h'
self._assertEqualContents(expected_new_contents,
_InsertHeader(old_contents))
def testSkippingCppComments_DocCommentForInclude(self):
""" This is a regression test for https://crbug.com/1175684 """
old_contents = '''
// Copyright blah blah...
// System includes.
#include <stdint.h>
// Doc comment for a struct.
struct sock_filter {
uint16_t code;
};
'''
expected_new_contents = '''
// Copyright blah blah...
// System includes.
#include <stdint.h>
#include "new/header.h"
// Doc comment for a struct.
struct sock_filter {
uint16_t code;
};
'''
new_header_line = '#include "new/header.h'
self._assertEqualContents(expected_new_contents,
_InsertHeader(old_contents))
def testSkippingCppComments_DocCommentForWholeFile(self):
""" This is a regression test for https://crbug.com/1175684 """
old_contents = '''
// Copyright blah blah...
// Doc comment for the whole file.
struct sock_filter {
uint16_t code;
};
'''
expected_new_contents = '''
// Copyright blah blah...
// Doc comment for the whole file.
#include "new/header.h"
struct sock_filter {
uint16_t code;
};
'''
new_header_line = '#include "new/header.h'
self._assertEqualContents(expected_new_contents,
_InsertHeader(old_contents))
def testSkippingOldStyleComments(self):
old_contents = '''
/* Copyright
* info here.
*/
#include "old/header.h"
'''
expected_new_contents = '''
/* Copyright
* info here.
*/
#include "new/header.h"
#include "old/header.h"
'''
self._assertEqualContents(expected_new_contents,
_InsertHeader(old_contents))
def testSkippingOldStyleComments_NoWhitespaceAtLineStart(self):
old_contents = '''
/* Copyright
* info here.
*/
#include "old/header.h"
'''
expected_new_contents = '''
/* Copyright
* info here.
*/
#include "new/header.h"
#include "old/header.h"
'''
self._assertEqualContents(expected_new_contents,
_InsertHeader(old_contents))
def testSkippingSystemHeaders(self):
old_contents = '''
#include <string>
#include <vector> // blah
#include "old/header.h"
'''
expected_new_contents = '''
#include <string>
#include <vector> // blah
#include "new/header.h"
#include "old/header.h"
'''
self._assertEqualContents(expected_new_contents,
_InsertHeader(old_contents))
def testSkippingPrimaryHeader(self):
old_contents = '''
// Copyright info here.
#include "foo/impl.h"
#include "old/header.h"
'''
expected_new_contents = '''
// Copyright info here.
#include "foo/impl.h"
#include "new/header.h"
#include "old/header.h"
'''
self._assertEqualContents(expected_new_contents,
_InsertHeader(old_contents))
def testSimilarNonPrimaryHeader_WithPrimaryHeader(self):
old_contents = '''
// Copyright info here.
#include "primary/impl.h" // This is the primary header.
#include "unrelated/impl.h" // This is *not* the primary header.
#include "zzz/foo.h"
'''
expected_new_contents = '''
// Copyright info here.
#include "primary/impl.h" // This is the primary header.
#include "unrelated/impl.h" // This is *not* the primary header.
#include "new/header.h"
#include "zzz/foo.h"
'''
self._assertEqualContents(expected_new_contents,
_InsertHeader(old_contents))
def testSimilarNonPrimaryHeader_NoPrimaryHeader(self):
old_contents = '''
// Copyright info here.
#include "unrelated/impl.h" // This is *not* the primary header.
#include "zzz/foo.h"
'''
expected_new_contents = '''
// Copyright info here.
#include "unrelated/impl.h" // This is *not* the primary header.
#include "new/header.h"
#include "zzz/foo.h"
'''
self._assertEqualContents(expected_new_contents,
_InsertHeader(old_contents))
def testSkippingIncludeGuards(self):
old_contents = '''
#ifndef FOO_IMPL_H_
#define FOO_IMPL_H_
#include "old/header.h"
#endif FOO_IMPL_H_
'''
expected_new_contents = '''
#ifndef FOO_IMPL_H_
#define FOO_IMPL_H_
#include "new/header.h"
#include "old/header.h"
#endif FOO_IMPL_H_
'''
self._assertEqualContents(
expected_new_contents,
_InsertHeader(old_contents, 'foo/impl.h', 'new/header.h'))
def testSkippingIncludeGuards2(self):
# This test is based on base/third_party/valgrind/memcheck.h
old_contents = '''
#ifndef __MEMCHECK_H
#define __MEMCHECK_H
#include "old/header.h"
#endif
'''
expected_new_contents = '''
#ifndef __MEMCHECK_H
#define __MEMCHECK_H
#include "new/header.h"
#include "old/header.h"
#endif
'''
self._assertEqualContents(expected_new_contents,
_InsertHeader(old_contents))
def testSkippingIncludeGuards3(self):
# This test is based on base/third_party/xdg_mime/xdgmime.h
old_contents = '''
#ifndef __XDG_MIME_H__
#define __XDG_MIME_H__
#include "old/header.h"
#ifdef __cplusplus
extern "C" {
#endif /* __cplusplus */
typedef void (*XdgMimeCallback) (void *user_data);
#ifdef __cplusplus
}
#endif /* __cplusplus */
#endif /* __XDG_MIME_H__ */
'''
expected_new_contents = '''
#ifndef __XDG_MIME_H__
#define __XDG_MIME_H__
#include "new/header.h"
#include "old/header.h"
#ifdef __cplusplus
extern "C" {
#endif /* __cplusplus */
typedef void (*XdgMimeCallback) (void *user_data);
#ifdef __cplusplus
}
#endif /* __cplusplus */
#endif /* __XDG_MIME_H__ */
'''
self._assertEqualContents(expected_new_contents,
_InsertHeader(old_contents))
@unittest.skip(
"Failing test due to regex (in apply_edits.py) not working as expected, please fix."
)
def testSkippingIncludeGuards4(self):
# This test is based on ash/first_run/desktop_cleaner.h and/or
# components/subresource_filter/core/common/scoped_timers.h and/or
# device/gamepad/abstract_haptic_gamepad.h
old_contents = '''
#ifndef ASH_FIRST_RUN_DESKTOP_CLEANER_
#define ASH_FIRST_RUN_DESKTOP_CLEANER_
#include "old/header.h"
namespace ash {
} // namespace ash
#endif // ASH_FIRST_RUN_DESKTOP_CLEANER_
'''
expected_new_contents = '''
#ifndef ASH_FIRST_RUN_DESKTOP_CLEANER_
#define ASH_FIRST_RUN_DESKTOP_CLEANER_
#include "new/header.h"
#include "old/header.h"
namespace ash {
} // namespace ash
#endif // ASH_FIRST_RUN_DESKTOP_CLEANER_
'''
self._assertEqualContents(expected_new_contents,
_InsertHeader(old_contents))
@unittest.skip(
"Failing test due to regex (in apply_edits.py) not working as expected, please fix."
)
def testSkippingIncludeGuards5(self):
# This test is based on third_party/weston/include/GLES2/gl2.h (the |extern
# "C"| part has been removed to make the test trickier to handle right -
# otherwise it is easy to see that the header has to be included before the
# |extern "C"| part).
#
# The tricky parts below include:
# 1. upper + lower case characters allowed in the guard name
# 2. Having to recognize that GL_APIENTRYP is *not* a guard
old_contents = '''
#ifndef __gles2_gl2_h_
#define __gles2_gl2_h_ 1
#include <GLES2/gl2platform.h>
#ifndef GL_APIENTRYP
#define GL_APIENTRYP GL_APIENTRY*
#endif
#endif
'''
expected_new_contents = '''
#ifndef __gles2_gl2_h_
#define __gles2_gl2_h_ 1
#include <GLES2/gl2platform.h>
#include "new/header.h"
#ifndef GL_APIENTRYP
#define GL_APIENTRYP GL_APIENTRY*
#endif
#endif
'''
self._assertEqualContents(expected_new_contents,
_InsertHeader(old_contents))
@unittest.skip(
"Failing test due to regex (in apply_edits.py) not working as expected, please fix."
)
def testSkippingIncludeGuards6(self):
# This test is based on ios/third_party/blink/src/html_token.h
old_contents = '''
#ifndef HTMLToken_h
#define HTMLToken_h
#include <stddef.h>
#include <vector>
// ...
#endif
'''
expected_new_contents = '''
#ifndef HTMLToken_h
#define HTMLToken_h
#include <stddef.h>
#include <vector>
#include "new/header.h"
// ...
#endif
'''
self._assertEqualContents(expected_new_contents,
_InsertHeader(old_contents))
def testNoOpIfAlreadyPresent(self):
# This tests that the new header won't be inserted (and duplicated)
# if it is already included.
old_contents = '''
// Copyright info here.
#include "old/header.h"
#include "new/header.h"
#include "new/header2.h"
'''
expected_new_contents = '''
// Copyright info here.
#include "old/header.h"
#include "new/header.h"
#include "new/header2.h"
'''
self._assertEqualContents(expected_new_contents,
_InsertHeader(old_contents))
def testNoOpIfAlreadyPresent_WithTrailingComment(self):
# This tests that the new header won't be inserted (and duplicated)
# if it is already included.
old_contents = '''
// Copyright info here.
#include "old/header.h"
#include "new/header.h" // blah
#include "new/header2.h"
'''
expected_new_contents = '''
// Copyright info here.
#include "old/header.h"
#include "new/header.h" // blah
#include "new/header2.h"
'''
self._assertEqualContents(expected_new_contents,
_InsertHeader(old_contents))
def testNoOldHeaders(self):
# This tests that an extra new line is inserted after the new header
# when there are no old headers immediately below.
old_contents = '''
#include <vector>
struct S {};
'''
expected_new_contents = '''
#include <vector>
#include "new/header.h"
struct S {};
'''
self._assertEqualContents(expected_new_contents,
_InsertHeader(old_contents))
def testPlatformIfDefs(self):
# This test is based on
# //base/third_party/double_conversion/double-conversion/utils.h
# We need to insert the new header in a non-conditional part.
old_contents = '''
#ifndef DOUBLE_CONVERSION_UTILS_H_
#define DOUBLE_CONVERSION_UTILS_H_
#include <cstdlib>
#include <cstring>
#ifndef DOUBLE_CONVERSION_UNREACHABLE
#ifdef _MSC_VER
void DOUBLE_CONVERSION_NO_RETURN abort_noreturn();
inline void abort_noreturn() { abort(); }
#define DOUBLE_CONVERSION_UNREACHABLE() (abort_noreturn())
#else
#define DOUBLE_CONVERSION_UNREACHABLE() (abort())
#endif
#endif
namespace double_conversion {
'''
expected_new_contents = '''
#ifndef DOUBLE_CONVERSION_UTILS_H_
#define DOUBLE_CONVERSION_UTILS_H_
#include <cstdlib>
#include <cstring>
#include "new/header.h"
#ifndef DOUBLE_CONVERSION_UNREACHABLE
#ifdef _MSC_VER
void DOUBLE_CONVERSION_NO_RETURN abort_noreturn();
inline void abort_noreturn() { abort(); }
#define DOUBLE_CONVERSION_UNREACHABLE() (abort_noreturn())
#else
#define DOUBLE_CONVERSION_UNREACHABLE() (abort())
#endif
#endif
namespace double_conversion {
'''
self._assertEqualContents(expected_new_contents,
_InsertHeader(old_contents))
def testNoOldIncludesAndIfDefs(self):
# Artificial test: no old #includes + some #ifdefs. The main focus of the
# test is ensuring that the new header will be inserted into the
# unconditional part of the file.
old_contents = '''
#ifndef NDEBUG
#include "base/logging.h"
#endif
void foo();
'''
expected_new_contents = '''
#include "new/header.h"
#ifndef NDEBUG
#include "base/logging.h"
#endif
void foo();
'''
self._assertEqualContents(expected_new_contents,
_InsertHeader(old_contents))
def testNoOldIncludesAndIfDefs2(self):
# Artificial test: no old #includes + some #ifdefs. The main focus of the
# test is ensuring that the new header will be inserted into the
# unconditional part of the file.
old_contents = '''
#if BUILDFLAG(IS_WIN)
#include "foo_win.h"
#endif
void foo();
'''
expected_new_contents = '''
#include "new/header.h"
#if BUILDFLAG(IS_WIN)
#include "foo_win.h"
#endif
void foo();
'''
self._assertEqualContents(expected_new_contents,
_InsertHeader(old_contents))
def testUtf8BomMarker(self):
# Test based on
# //chrome/browser/ui/views/payments/payment_sheet_view_controller.cc
# which at some point began as follows:
# 00000000: efbb bf2f 2f20 436f 7079 7269 6768 7420 ...// Copyright
#
# Previous versions of apply_edits.py would not skip the BOM marker when
# figuring out where to insert the new include header.
old_contents = u'''\ufeff// Copyright
#include "old/header.h"
'''
expected_new_contents = u'''\ufeff// Copyright
#include "new/header.h"
#include "old/header.h"
'''
actual = bytearray()
actual.extend(old_contents.encode('utf-8'))
expected = bytearray()
expected.extend(expected_new_contents.encode('utf-8'))
# Test sanity check (i.e. not an assertion about code under test).
utf8_bom = [0xef, 0xbb, 0xbf]
self._assertEqualContents(list(actual[0:3]), utf8_bom)
self._assertEqualContents(list(expected[0:3]), utf8_bom)
# Actual test.
edit = apply_edits.Edit('include-user-header', -1, -1, b"new/header.h")
actual = apply_edits._ApplySingleEdit("foo/impl.cc", actual, edit, None)
self._assertEqualContents(expected, actual)
def _CreateReplacement(content_string, old_substring, new_substring):
""" Test helper for creating an Edit object with the right offset, etc. """
b_content_string = content_string.encode("utf-8")
b_old_string = old_substring.encode("utf-8")
b_new_string = new_substring.encode("utf-8")
offset = b_content_string.find(b_old_string)
return apply_edits.Edit('r', offset, len(b_old_string), b_new_string)
class ApplyReplacementTest(unittest.TestCase):
def testBasics(self):
old_text = "123 456 789"
r = _CreateReplacement(old_text, "456", "foo")
new_text = _ApplyEdit(old_text, r)
self.assertEqual("123 foo 789", new_text)
def testMiddleListElementRemoval(self):
old_text = "(123, 456, 789) // foobar"
r = _CreateReplacement(old_text, "456", "")
new_text = _ApplyEdit(old_text, r)
self.assertEqual("(123, 789) // foobar", new_text)
def testFinalElementRemoval(self):
old_text = "(123, 456, 789) // foobar"
r = _CreateReplacement(old_text, "789", "")
new_text = _ApplyEdit(old_text, r)
self.assertEqual("(123, 456) // foobar", new_text)
def testConflictingReplacement(self):
old_text = "123 456 789"
last = _CreateReplacement(old_text, "456", "foo")
edit = _CreateReplacement(old_text, "456", "bar")
expected_msg_regex = 'Conflicting replacement text'
expected_msg_regex += '.*some_file.cc at offset 4, length 3'
expected_msg_regex += '.*"bar" != "foo"'
with self.assertRaisesRegex(ValueError, expected_msg_regex):
_ApplyEdit(old_text, edit, last_edit=last)
def testUnrecognizedEditDirective(self):
old_text = "123 456 789"
edit = apply_edits.Edit('unknown_directive', 123, 456, "foo")
expected_msg_regex = 'Unrecognized edit directive "unknown_directive"'
expected_msg_regex += '.*some_file.cc'
with self.assertRaisesRegex(ValueError, expected_msg_regex):
_ApplyEdit(old_text, edit)
def testOverlappingReplacement(self):
old_text = "123 456 789"
last = _CreateReplacement(old_text, "456 789", "foo")
edit = _CreateReplacement(old_text, "123 456", "bar")
expected_msg_regex = 'Overlapping replacements'
expected_msg_regex += '.*some_file.cc'
expected_msg_regex += '.*offset 0, length 7.*"bar"'
expected_msg_regex += '.*offset 4, length 7.*"foo"'
with self.assertRaisesRegex(ValueError, expected_msg_regex):
_ApplyEdit(old_text, edit, last_edit=last)
if __name__ == '__main__':
unittest.main()
|