Home | History | Annotate | Download | only in VCSUtils_unittest
      1 #!/usr/bin/perl
      2 #
      3 # Copyright (C) 2009, 2010 Chris Jerdonek (chris.jerdonek (at] gmail.com)
      4 #
      5 # Redistribution and use in source and binary forms, with or without
      6 # modification, are permitted provided that the following conditions are
      7 # met:
      8 # 
      9 #     * Redistributions of source code must retain the above copyright
     10 # notice, this list of conditions and the following disclaimer.
     11 #     * Redistributions in binary form must reproduce the above
     12 # copyright notice, this list of conditions and the following disclaimer
     13 # in the documentation and/or other materials provided with the
     14 # distribution.
     15 #     * Neither the name of Google Inc. nor the names of its
     16 # contributors may be used to endorse or promote products derived from
     17 # this software without specific prior written permission.
     18 # 
     19 # THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
     20 # "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
     21 # LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
     22 # A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT
     23 # OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
     24 # SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
     25 # LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
     26 # DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
     27 # THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
     28 # (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
     29 # OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
     30 
     31 # Unit tests of VCSUtils::runPatchCommand().
     32 
     33 use Test::Simple tests => 4;
     34 use VCSUtils;
     35 
     36 # Call a function while suppressing STDERR.
     37 sub callSilently($@) {
     38     my ($func, @args) = @_;
     39 
     40     open(OLDERR, ">&STDERR");
     41     close(STDERR);
     42     my @returnValue = &$func(@args);
     43     open(STDERR, ">&OLDERR");
     44     close(OLDERR); # FIXME: Is this necessary?
     45 
     46     return @returnValue;
     47 }
     48 
     49 # New test
     50 $title = "runPatchCommand: Unsuccessful patch, forcing.";
     51 
     52 # Since $patch has no "Index:" path, passing this to runPatchCommand
     53 # should not affect any files.
     54 my $patch = <<'END';
     55 Garbage patch contents
     56 END
     57 
     58 # We call via callSilently() to avoid output like the following to STDERR:
     59 # patch: **** Only garbage was found in the patch input.
     60 $argsHashRef = {ensureForce => 1};
     61 $exitStatus = callSilently(\&runPatchCommand, $patch, ".", "file_to_patch.txt", $argsHashRef);
     62 
     63 ok($exitStatus != 0, $title);
     64 
     65 # New test
     66 $title = "runPatchCommand: New file, --dry-run.";
     67 
     68 # This file should not exist after the tests, but we take care with the
     69 # file name and contents just in case.
     70 my $fileToPatch = "temp_OK_TO_ERASE__README_FOR_MORE.txt";
     71 $patch = <<END;
     72 Index: $fileToPatch
     73 ===================================================================
     74 --- $fileToPatch	(revision 0)
     75 +++ $fileToPatch	(revision 0)
     76 @@ -0,0 +1,5 @@
     77 +This is a test file for WebKitTools/Scripts/VCSUtils_unittest.pl.
     78 +This file should not have gotten created on your system.
     79 +If it did, some unit tests don't seem to be working quite right:
     80 +It would be great if you could file a bug report. Thanks!
     81 +---------------------------------------------------------------------
     82 END
     83 
     84 # --dry-run prevents creating any files.
     85 # --silent suppresses the success message to STDOUT.
     86 $argsHashRef = {options => ["--dry-run", "--silent"]};
     87 $exitStatus = runPatchCommand($patch, ".", $fileToPatch, $argsHashRef);
     88 
     89 ok($exitStatus == 0, $title);
     90 
     91 # New test
     92 $title = "runPatchCommand: New file: \"$fileToPatch\".";
     93 
     94 $argsHashRef = {options => ["--silent"]};
     95 $exitStatus = runPatchCommand($patch, ".", $fileToPatch, $argsHashRef);
     96 
     97 ok($exitStatus == 0, $title);
     98 
     99 # New test
    100 $title = "runPatchCommand: Reverse new file (clean up previous).";
    101 
    102 $argsHashRef = {shouldReverse => 1,
    103                 options => ["--silent", "--remove-empty-files"]}; # To clean up.
    104 $exitStatus = runPatchCommand($patch, ".", $fileToPatch, $argsHashRef);
    105 ok($exitStatus == 0, $title);
    106