Changeset 53667 in webkit


Ignore:
Timestamp:
Jan 21, 2010 6:35:12 PM (14 years ago)
Author:
Chris Jerdonek
Message:

Create a unit-tested subroutine to parse patch files created
by svn-create-patch.

Reviewed by David Kilzer.

https://bugs.webkit.org/show_bug.cgi?id=33475

  • Scripts/VCSUtils.pm:
    • Added parseDiff().
    • Added parsePatch().
  • Scripts/webkitperl/VCSUtils_unittest/parseDiff.pl: Added.
    • Added unit tests for parseDiff().
  • Scripts/webkitperl/VCSUtils_unittest/parseDiffHeader.pl:
    • Total number of tests now computed dynamically.
  • Scripts/webkitperl/VCSUtils_unittest/parsePatch.pl: Added.
    • Added unit tests for parsePatch().
Location:
trunk/WebKitTools
Files:
2 added
3 edited

Legend:

Unmodified
Added
Removed
  • trunk/WebKitTools/ChangeLog

    r53665 r53667  
     12010-01-21  Chris Jerdonek  <cjerdonek@webkit.org>
     2
     3        Reviewed by David Kilzer.
     4
     5        Create a unit-tested subroutine to parse patch files created
     6        by svn-create-patch.
     7
     8        https://bugs.webkit.org/show_bug.cgi?id=33475
     9
     10        * Scripts/VCSUtils.pm:
     11          - Added parseDiff().
     12          - Added parsePatch().
     13
     14        * Scripts/webkitperl/VCSUtils_unittest/parseDiff.pl: Added.
     15          - Added unit tests for parseDiff().
     16
     17        * Scripts/webkitperl/VCSUtils_unittest/parseDiffHeader.pl:
     18          - Total number of tests now computed dynamically.
     19
     20        * Scripts/webkitperl/VCSUtils_unittest/parsePatch.pl: Added.
     21          - Added unit tests for parsePatch().
     22
    1232010-01-21  Kevin Ollivier  <kevino@theolliviers.com>
    224
  • trunk/WebKitTools/Scripts/VCSUtils.pm

    r53340 r53667  
    6363        &makeFilePathRelative
    6464        &normalizePath
     65        &parsePatch
    6566        &pathRelativeToSVNRepositoryRootForPath
    6667        &runPatchCommand
     
    481482}
    482483
     484# Parse one diff from a patch file created by svn-create-patch, and
     485# advance the file handle so the last line read is the first line
     486# of the next header block.
     487#
     488# This subroutine preserves any leading junk encountered before the header.
     489#
     490# Args:
     491#   $fileHandle: a file handle advanced to the first line of the next
     492#                header block. Leading junk is okay.
     493#   $line: the line last read from $fileHandle.
     494#
     495# Returns ($diffHashRef, $lastReadLine):
     496#   $diffHashRef:
     497#     copiedFromPath: if a file copy, the path from which the file was
     498#                     copied. Otherwise, undefined.
     499#     indexPath: the path in the "Index:" line.
     500#     sourceRevision: the revision number of the source. This is the same
     501#                     as the revision number the file was copied from, in
     502#                     the case of a file copy.
     503#     svnConvertedText: the diff converted to SVN format.
     504#   $lastReadLine: the line last read from $fileHandle
     505sub parseDiff($$)
     506{
     507    my ($fileHandle, $line) = @_;
     508
     509    my $headerStartRegEx = qr#^Index: #; # SVN-style header for the default
     510    my $gitHeaderStartRegEx = qr#^diff --git \w/#;
     511
     512    my $headerHashRef; # Last header found, as returned by parseDiffHeader().
     513    my $svnText;
     514    while (defined($line)) {
     515        if (!$headerHashRef && ($line =~ $gitHeaderStartRegEx)) {
     516            # Then assume all diffs in the patch are Git-formatted. This
     517            # block was made to be enterable at most once since we assume
     518            # all diffs in the patch are formatted the same (SVN or Git).
     519            $headerStartRegEx = $gitHeaderStartRegEx;
     520        }
     521
     522        if ($line !~ $headerStartRegEx) {
     523            # Then we are in the body of the diff.
     524            $svnText .= $line;
     525            $line = <$fileHandle>;
     526            next;
     527        } # Otherwise, we found a diff header.
     528
     529        if ($headerHashRef) {
     530            # Then this is the second diff header of this while loop.
     531            last;
     532        }
     533
     534        ($headerHashRef, $line) = parseDiffHeader($fileHandle, $line);
     535
     536        $svnText .= $headerHashRef->{svnConvertedText};
     537    }
     538
     539    my %diffHashRef;
     540    $diffHashRef{copiedFromPath} = $headerHashRef->{copiedFromPath};
     541    $diffHashRef{indexPath} = $headerHashRef->{indexPath};
     542    $diffHashRef{sourceRevision} = $headerHashRef->{sourceRevision};
     543    $diffHashRef{svnConvertedText} = $svnText;
     544
     545    return (\%diffHashRef, $line);
     546}
     547
     548# Parse a patch file created by svn-create-patch.
     549#
     550# Args:
     551#   $fileHandle: A file handle to the patch file that has not yet been
     552#                read from.
     553#
     554# Returns:
     555#   @diffHashRefs: an array of diff hash references. See parseDiff() for
     556#                  a description of each $diffHashRef.
     557sub parsePatch($)
     558{
     559    my ($fileHandle) = @_;
     560
     561    my @diffHashRefs; # return value
     562
     563    my $line = <$fileHandle>;
     564
     565    while (defined($line)) { # Otherwise, at EOF.
     566
     567        my $diffHashRef;
     568        ($diffHashRef, $line) = parseDiff($fileHandle, $line);
     569
     570        push @diffHashRefs, $diffHashRef;
     571    }
     572
     573    return @diffHashRefs;
     574}
     575
    483576# If possible, returns a ChangeLog patch equivalent to the given one,
    484577# but with the newest ChangeLog entry inserted at the top of the
  • trunk/WebKitTools/Scripts/webkitperl/VCSUtils_unittest/parseDiffHeader.pl

    r53340 r53667  
    3737use VCSUtils;
    3838
    39 my @headerKeys = ( # The $headerHashRef keys to check.
     39my @diffHeaderHashRefKeys = ( # The $diffHeaderHashRef keys to check.
    4040    "copiedFromPath",
    4141    "indexPath",
     
    4444);
    4545
    46 # Test parseDiffHeader() for the given $diffTestHashRef.
    47 sub doDiffTest($)
    48 {
    49     my ($diffTestHashRef) = @_;
    50 
    51     my $fileHandle;
    52     open($fileHandle, "<", \$diffTestHashRef->{inputText});
    53 
    54     my $line = <$fileHandle>;
    55 
    56     my ($headerHashRef, $lastReadLine) = VCSUtils::parseDiffHeader($fileHandle, $line);
    57 
    58     my $titleHeader = "parseDiffHeader(): [$diffTestHashRef->{diffName}] ";
    59     my $title;
    60 
    61     foreach my $headerKey (@headerKeys) {
    62         my $title = "${titleHeader}key=\"$headerKey\"";
    63         is($headerHashRef->{$headerKey}, $diffTestHashRef->{$headerKey}, $title);
    64     }
    65 
    66     is($lastReadLine, $diffTestHashRef->{lastReadLine}, "${titleHeader}lastReadLine");
    67 
    68     my $nextLine = <$fileHandle>;
    69     is($nextLine, $diffTestHashRef->{nextLine}, "${titleHeader}nextLine");
    70 }
    71 
    72 my @diffTests = (
     46# The array of test cases.
     47my @testCaseHashRefs = (
    7348{
    7449    # New test
     
    248223);
    249224
    250 plan(tests => @diffTests * 6); # Multiply by number of assertions per call to doDiffTest().
    251 
    252 foreach my $diffTestHashRef (@diffTests) {
    253     doDiffTest($diffTestHashRef);
     225# Return the arguments for each assertion per test case.
     226#
     227# In particular, the number of assertions per test case is the length
     228# of the return value of this subroutine on a sample input.
     229#
     230# Returns @assertionArgsArrayRefs:
     231#   $assertionArgsArrayRef: A reference to an array of parameters to pass
     232#                           to each call to is(). The parameters are--
     233#                             $got: The value obtained
     234#                             $expected: The expected value
     235#                             $testName: The name of the test
     236sub testParseDiffHeaderAssertionArgs($)
     237{
     238    my ($testCaseHashRef) = @_;
     239
     240    my $fileHandle;
     241    open($fileHandle, "<", \$testCaseHashRef->{inputText});
     242
     243    my $line = <$fileHandle>;
     244
     245    my ($headerHashRef, $lastReadLine) = VCSUtils::parseDiffHeader($fileHandle, $line);
     246
     247    my $testNameStart = "parseDiffHeader(): [$testCaseHashRef->{diffName}] ";
     248
     249    my @assertionArgsArrayRefs; # Return value
     250    my @assertionArgs;
     251    foreach my $diffHeaderHashRefKey (@diffHeaderHashRefKeys) {
     252        my $testName = "${testNameStart}key=\"$diffHeaderHashRefKey\"";
     253        @assertionArgs = ($headerHashRef->{$diffHeaderHashRefKey}, $testCaseHashRef->{$diffHeaderHashRefKey}, $testName);
     254        push(@assertionArgsArrayRefs, \@assertionArgs);
     255    }
     256
     257    @assertionArgs = ($lastReadLine, $testCaseHashRef->{lastReadLine}, "${testNameStart}lastReadLine");
     258    push(@assertionArgsArrayRefs, \@assertionArgs);
     259
     260    my $nextLine = <$fileHandle>;
     261    @assertionArgs = ($nextLine, $testCaseHashRef->{nextLine}, "${testNameStart}nextLine");
     262    push(@assertionArgsArrayRefs, \@assertionArgs);
     263
     264    return @assertionArgsArrayRefs;
    254265}
     266
     267# Test parseDiffHeader() for the given test case.
     268sub testParseDiffHeader($)
     269{
     270    my ($testCaseHashRef) = @_;
     271
     272    my @assertionArgsArrayRefs = testParseDiffHeaderAssertionArgs($testCaseHashRef);
     273
     274    foreach my $arrayRef (@assertionArgsArrayRefs) {
     275        # The parameters are -- is($got, $expected, $testName).
     276        is($arrayRef->[0], $arrayRef->[1], $arrayRef->[2]);
     277    }
     278}
     279
     280# Count the number of assertions per test case to calculate the total number
     281# of Test::More tests. We could have used any test case for the count.
     282my $assertionCount = testParseDiffHeaderAssertionArgs($testCaseHashRefs[0]);
     283
     284plan(tests => @testCaseHashRefs * $assertionCount); # Total number of tests
     285
     286foreach my $testCaseHashRef (@testCaseHashRefs) {
     287    testParseDiffHeader($testCaseHashRef);
     288}
Note: See TracChangeset for help on using the changeset viewer.