File indexing completed on 2024-03-24 15:25:06

0001 #!/usr/bin/perl
0002 
0003 # This file is part of the kcalcore library.
0004 #
0005 # SPDX-FileCopyrightText: 2003 Cornelius Schumacher <schumacher@kde.org>
0006 # SPDX-FileCopyrightText: 2005 Reinhold Kainhofer <reinhold@kainhofer.com>
0007 #
0008 # SPDX-License-Identifier: LGPL-2.0-or-later
0009 
0010 # This little script runs a test program on a given (calendar) file and 
0011 # compares the output to a reference file. All discrepancies are shown 
0012 # to the user. Usage:
0013 #      runtestcase.pl appname testfile.ics
0014 # The application/script appname is required to take two arguments:
0015 #      appname inputfile outputfile
0016 # where inputfile is the file to be used as input data, and the output of the
0017 # program will go to outputfile (=testfile.ics.out if called through 
0018 # runtestcase.pl). That outputfile is then compared to the reference file
0019 # testfile.ics.ref.
0020 
0021 if ( @ARGV != 4 ) {
0022   print STDERR "Missing arg! Arguments: testapp identifier filename \n";
0023   exit 1;
0024 }
0025 
0026 $app = "$ARGV[0]";
0027 $id = "$ARGV[1]";
0028 $file_orig = "$ARGV[2]";
0029 $file = "$file_orig";
0030 $outfile ="$ARGV[3]";  # necessary to avoid creating files in the $file directory
0031 
0032 $MAXERRLINES=25;
0033 
0034 $outfile = "$outfile.$id.out";
0035 
0036 if ( $^O eq "MSWin32" || $^O eq "msys" ) {
0037   $testcmd = "\"$app\" \"$file\" \"$outfile\" 2> \$null";
0038 } else {
0039   $testcmd = "\"$app\" \"$file\" \"$outfile\"";
0040 }
0041 
0042 #print "CMD $testcmd\n";
0043 
0044 if ( system( $testcmd ) != 0 ) {
0045   print STDERR "Error running $app\n";
0046   exit 1;
0047 }
0048 
0049 checkfile( $file, $file_orig, $outfile );
0050 
0051 exit 0;
0052 
0053 sub checkfile()
0054 {
0055   my $file = shift;
0056   my $file_orig = shift;
0057   my $outfile = shift;
0058 
0059   unless (-e "$file_orig.$id.ref") {
0060     print STDERR "Missing ref file: $file_orig.$id.ref\n";
0061     exit 1;
0062   }
0063 
0064   $cmd = 'diff -u -w -B -I "^DTSTAMP:[0-9ZT]*" -I "^LAST-MODIFIED:[0-9ZT]*" -I "^CREATED:[0-9ZT]*" -I "^DCREATED:[0-9ZT]*" -I "^X-KDE-KCALCORE-ENABLED:" -I "^X-KDE-ICAL-IMPLEMENTATION-VERSION:" -I "^PRODID:.*" -I "X-UID=[0-9]*" '."$file.$id.ref $outfile";
0065   if ( !open( DIFF, "$cmd|" ) ) {
0066     print STDERR "Unable to run diff command on the files $file_orig.$id.ref and $outfile\n";
0067     exit 1;
0068   }
0069 
0070   $errors = 0;
0071   $errorstr = "";
0072   while ( <DIFF> ) {
0073     $line = $_;
0074     next if ($line =~ m/^[+-]\s*(DTSTAMP|LAST-MODIFIED|CREATED|DCREATED|X-KDE-KCALCORE-ENABLED|X-KDE-ICAL-IMPLEMENTATION-VERSION|PRODID|X-UID)/);
0075     next if ($line =~ m/^[+-]\s*$/);
0076     next if ($line =~ m/No newline at end of file/);
0077     # cannot compare outfile to "/Compat/" because of the quotemeta stuff.
0078     next if ($outfile =~ m+/Compat\\/+ && $line =~ m/^[+-](SEQUENCE|PRIORITY|ORGANIZER:MAILTO):/);
0079     if ( $line =~ /^[+-][^+-]/ ) {
0080       # it's an added/deleted/modified line. Register it as an error
0081       $errors++;
0082     }
0083     $errorstr .= $line;
0084   }
0085 
0086   if ( $errors > 0 ) {
0087     print "~=~=~=~=~=~=~=~=~=~=~=~=~=~=~=~=~=~=~=~=\n";
0088     print "Checking '$outfile':\n";
0089     print $errorstr;
0090     print "Encountered $errors errors\n";
0091 
0092     if ( !open( ERRLOG, ">>FAILED.log" ) ) {
0093       print "Unable to open FAILED.log";
0094     };
0095     print ERRLOG "\n\n\n~=~=~=~=~=~=~=~=~=~=~=~=~=~=~=~=~=~=~=~=\n\n\n";
0096     print ERRLOG "Checking '$outfile':\n";
0097     print ERRLOG "Command: $testcmd\n";
0098     print ERRLOG $errorstr;
0099 
0100     if ( -e "$file_orig.$id.fixme" ) {
0101       if ( !open( FIXME, "$file_orig.$id.fixme" ) ) {
0102         print STDERR "Unable to open $file_orig.$id.fixme\n";
0103         exit 1;
0104       }
0105       my $firstline = <FIXME>;
0106       $firstline =~ /^(\d+) known errors/;
0107       my $expected = $1;
0108       if ( $expected == $errors ) {
0109         print ERRLOG "\n  EXPECTED FAIL: $errors errors found.\n";
0110         print ERRLOG "    Fixme:\n";
0111         while( <FIXME> ) {
0112           print ERRLOG "      ";
0113           print ERRLOG;
0114         }
0115       } else {
0116         print ERRLOG "\n  UNEXPECTED FAIL: $errors errors found, $expected expected.\n";
0117         exit 1;
0118       }
0119     } else {
0120       print ERRLOG "\n  FAILED: $errors errors found.\n";
0121 #       system( "touch FAILED" );
0122       exit 1;
0123     }
0124   } else {
0125      unlink($outfile);
0126   }
0127 }