1*b1cdbd2cSJim Jagielski#!/usr/bin/perl -w
2*b1cdbd2cSJim Jagielski# *************************************************************
3*b1cdbd2cSJim Jagielski#
4*b1cdbd2cSJim Jagielski#  Licensed to the Apache Software Foundation (ASF) under one
5*b1cdbd2cSJim Jagielski#  or more contributor license agreements.  See the NOTICE file
6*b1cdbd2cSJim Jagielski#  distributed with this work for additional information
7*b1cdbd2cSJim Jagielski#  regarding copyright ownership.  The ASF licenses this file
8*b1cdbd2cSJim Jagielski#  to you under the Apache License, Version 2.0 (the
9*b1cdbd2cSJim Jagielski#  "License"); you may not use this file except in compliance
10*b1cdbd2cSJim Jagielski#  with the License.  You may obtain a copy of the License at
11*b1cdbd2cSJim Jagielski#
12*b1cdbd2cSJim Jagielski#    http://www.apache.org/licenses/LICENSE-2.0
13*b1cdbd2cSJim Jagielski#
14*b1cdbd2cSJim Jagielski#  Unless required by applicable law or agreed to in writing,
15*b1cdbd2cSJim Jagielski#  software distributed under the License is distributed on an
16*b1cdbd2cSJim Jagielski#  "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
17*b1cdbd2cSJim Jagielski#  KIND, either express or implied.  See the License for the
18*b1cdbd2cSJim Jagielski#  specific language governing permissions and limitations
19*b1cdbd2cSJim Jagielski#  under the License.
20*b1cdbd2cSJim Jagielski#
21*b1cdbd2cSJim Jagielski# *************************************************************
22*b1cdbd2cSJim Jagielskiuse File::Temp qw/ tempfile tempdir /;
23*b1cdbd2cSJim Jagielskiuse File::Basename;
24*b1cdbd2cSJim Jagielskiuse File::stat;
25*b1cdbd2cSJim Jagielskiuse File::Copy;
26*b1cdbd2cSJim Jagielski
27*b1cdbd2cSJim Jagielskimy $binDir = dirname($0);
28*b1cdbd2cSJim Jagielskimy $timestampclean= "perl $binDir/timestampsClean.pl";
29*b1cdbd2cSJim Jagielski#sub gen_diff($)
30*b1cdbd2cSJim Jagielski
31*b1cdbd2cSJim Jagielskisub testLog
32*b1cdbd2cSJim Jagielski{
33*b1cdbd2cSJim Jagielski   # 2 No Log to compare against
34*b1cdbd2cSJim Jagielski   # 1 Log passed
35*b1cdbd2cSJim Jagielski   # 0 Log failed
36*b1cdbd2cSJim Jagielski   my $result = 0;
37*b1cdbd2cSJim Jagielski   my $testfile = shift;
38*b1cdbd2cSJim Jagielski   my $dirtocheck = shift;
39*b1cdbd2cSJim Jagielski   my $filename = basename($testfile);
40*b1cdbd2cSJim Jagielski   $filename = "$logdir/$filename";
41*b1cdbd2cSJim Jagielski   print "processing $testfile $filename\n";
42*b1cdbd2cSJim Jagielski   if ( -f $filename )  {
43*b1cdbd2cSJim Jagielski      my $tmpFile;
44*b1cdbd2cSJim Jagielski      $dir = tempdir( CLEANUP => 1 );
45*b1cdbd2cSJim Jagielski      ($fh, $tmpFile) = tempfile( DIR => $dir );
46*b1cdbd2cSJim Jagielski      close($fh);
47*b1cdbd2cSJim Jagielski      #
48*b1cdbd2cSJim Jagielski      my $status = system("diff -U 0 -p $testfile $filename |  $timestampclean > $tmpFile");
49*b1cdbd2cSJim Jagielski      my $info = stat($tmpFile) or die "no $tmpFile: $!";
50*b1cdbd2cSJim Jagielski      if ( ($status >>=8) == 0 &&  ( $info->size == 0)  ) {
51*b1cdbd2cSJim Jagielski         #print "diff worked size is 0\n";
52*b1cdbd2cSJim Jagielski         $result = 1;
53*b1cdbd2cSJim Jagielski      }
54*b1cdbd2cSJim Jagielski      elsif ( ($status >>=8) == 0 &&  ( $info->size > 0)  )
55*b1cdbd2cSJim Jagielski      {
56*b1cdbd2cSJim Jagielski         #print "diff worked size > 0\n";
57*b1cdbd2cSJim Jagielski         $result = 0;
58*b1cdbd2cSJim Jagielski      }
59*b1cdbd2cSJim Jagielski      else
60*b1cdbd2cSJim Jagielski      {
61*b1cdbd2cSJim Jagielski         #print "diff failed size > 0\n";
62*b1cdbd2cSJim Jagielski         $result = 0;
63*b1cdbd2cSJim Jagielski      }
64*b1cdbd2cSJim Jagielski   }
65*b1cdbd2cSJim Jagielski   else
66*b1cdbd2cSJim Jagielski   {
67*b1cdbd2cSJim Jagielski      #print "not file > 0\n";
68*b1cdbd2cSJim Jagielski      $result = 2;
69*b1cdbd2cSJim Jagielski   }
70*b1cdbd2cSJim Jagielski   #print "diff result = $result\n";
71*b1cdbd2cSJim Jagielski   return $result;
72*b1cdbd2cSJim Jagielski}
73*b1cdbd2cSJim Jagielski
74*b1cdbd2cSJim Jagielskiif ( ! ( $logdir = shift @ARGV ) ) {
75*b1cdbd2cSJim Jagielski    print STDERR "No logdir specified!\n";
76*b1cdbd2cSJim Jagielski    usage();
77*b1cdbd2cSJim Jagielski    exit 1;
78*b1cdbd2cSJim Jagielski}
79*b1cdbd2cSJim Jagielski
80*b1cdbd2cSJim Jagielskiif ( ! ( $testlogdir = shift @ARGV ) ) {
81*b1cdbd2cSJim Jagielski    print STDERR "No testdocuments dir to compare against specified!\n";
82*b1cdbd2cSJim Jagielski    usage();
83*b1cdbd2cSJim Jagielski    exit 1;
84*b1cdbd2cSJim Jagielski}
85*b1cdbd2cSJim Jagielski
86*b1cdbd2cSJim Jagielskiif ( !(-d $logdir ) ) {
87*b1cdbd2cSJim Jagielski   print STDERR "No output directory $logdir exists, please create it!!!!\n";
88*b1cdbd2cSJim Jagielski   exit 1;
89*b1cdbd2cSJim Jagielski}
90*b1cdbd2cSJim Jagielskiif ( !(-d $testlogdir ) ) {
91*b1cdbd2cSJim Jagielski   print STDERR "the directory containing the logfiles to compare against \"$logdir\" does not exist\n";
92*b1cdbd2cSJim Jagielski    usage();
93*b1cdbd2cSJim Jagielski    exit 1;
94*b1cdbd2cSJim Jagielski}
95*b1cdbd2cSJim Jagielskiprint "logdir $logdir\n";
96*b1cdbd2cSJim Jagielskiprint "testlogdir $testlogdir\n";
97*b1cdbd2cSJim Jagielskisub filter_crud($)
98*b1cdbd2cSJim Jagielski{
99*b1cdbd2cSJim Jagielski    my $a = shift;
100*b1cdbd2cSJim Jagielski
101*b1cdbd2cSJim Jagielski    $a =~ /~$/ && return;
102*b1cdbd2cSJim Jagielski    $a =~ /\#$/ && return;
103*b1cdbd2cSJim Jagielski    $a =~ /\.orig$/ && return;
104*b1cdbd2cSJim Jagielski    $a =~ /unxlng.*\.pro$/ && return;
105*b1cdbd2cSJim Jagielski    $a =~ /wntmsc.*\.pro$/ && return;
106*b1cdbd2cSJim Jagielski    $a =~ /.swp$/ && return;
107*b1cdbd2cSJim Jagielski    $a =~ /POSITION/ && return;
108*b1cdbd2cSJim Jagielski    $a =~ /ReadMe/ && return;
109*b1cdbd2cSJim Jagielski    $a =~ /.tmp$/ && return;
110*b1cdbd2cSJim Jagielski    $a =~ /\.svn/ && return;
111*b1cdbd2cSJim Jagielski    $a eq 'CVS' && return;
112*b1cdbd2cSJim Jagielski    $a eq '.' && return;
113*b1cdbd2cSJim Jagielski    $a eq '..' && return;
114*b1cdbd2cSJim Jagielski
115*b1cdbd2cSJim Jagielski    return $a;
116*b1cdbd2cSJim Jagielski}
117*b1cdbd2cSJim Jagielskisub slurp_dir($);
118*b1cdbd2cSJim Jagielski
119*b1cdbd2cSJim Jagielskisub slurp_dir($)
120*b1cdbd2cSJim Jagielski{
121*b1cdbd2cSJim Jagielski    my $dir = shift;
122*b1cdbd2cSJim Jagielski    my ($dirhandle, $fname);
123*b1cdbd2cSJim Jagielski    my @files = ();
124*b1cdbd2cSJim Jagielski
125*b1cdbd2cSJim Jagielski    opendir ($dirhandle, $dir) || die "Can't open $dir";
126*b1cdbd2cSJim Jagielski    while ($fname = readdir ($dirhandle)) {
127*b1cdbd2cSJim Jagielski	$fname = filter_crud($fname);
128*b1cdbd2cSJim Jagielski	defined $fname || next;
129*b1cdbd2cSJim Jagielski#	if (-d "$dir/$fname") {
130*b1cdbd2cSJim Jagielski#	    push @files, slurp_dir("$dir/$fname");
131*b1cdbd2cSJim Jagielski#	} else
132*b1cdbd2cSJim Jagielski        {
133*b1cdbd2cSJim Jagielski	    push @files, "$dir/$fname";
134*b1cdbd2cSJim Jagielski	}
135*b1cdbd2cSJim Jagielski    }
136*b1cdbd2cSJim Jagielski    closedir ($dirhandle);
137*b1cdbd2cSJim Jagielski
138*b1cdbd2cSJim Jagielski    return @files;
139*b1cdbd2cSJim Jagielski}
140*b1cdbd2cSJim Jagielski
141*b1cdbd2cSJim Jagielskiif (-d $testlogdir) {
142*b1cdbd2cSJim Jagielski    push @files, slurp_dir($testlogdir);
143*b1cdbd2cSJim Jagielski}
144*b1cdbd2cSJim Jagielski
145*b1cdbd2cSJim Jagielskimy $processed = 0;
146*b1cdbd2cSJim Jagielskimy $passed = 0;
147*b1cdbd2cSJim Jagielskimy @passedTests=();
148*b1cdbd2cSJim Jagielskimy @skippedTests=();
149*b1cdbd2cSJim Jagielskimy @failedTests=();
150*b1cdbd2cSJim Jagielski
151*b1cdbd2cSJim Jagielskimy $failureCmd="";
152*b1cdbd2cSJim Jagielskifor $a (@files) {
153*b1cdbd2cSJim Jagielski   $processed++;
154*b1cdbd2cSJim Jagielski   my $testcase = $a;
155*b1cdbd2cSJim Jagielski   $testcase =~ s/\.log/\.xls/;
156*b1cdbd2cSJim Jagielski   my $result = testLog( $a, $logdir );
157*b1cdbd2cSJim Jagielski   if ( $result == 0 ) {
158*b1cdbd2cSJim Jagielski      push @failedTests, basename($testcase);
159*b1cdbd2cSJim Jagielski      if ( $failureCmd eq "" ) { $failureCmd = " diff -up $a $logdir "; }
160*b1cdbd2cSJim Jagielski   }
161*b1cdbd2cSJim Jagielski   elsif ( $result == 2 ) {
162*b1cdbd2cSJim Jagielski      #print "skipped $a\n";
163*b1cdbd2cSJim Jagielski      push @skippedTests, $testcase;
164*b1cdbd2cSJim Jagielski   }
165*b1cdbd2cSJim Jagielski   else {
166*b1cdbd2cSJim Jagielski      $passed++;
167*b1cdbd2cSJim Jagielski      push @passedTests, $testcase;
168*b1cdbd2cSJim Jagielski      #print "Test document for $a \t \t passed. \n";
169*b1cdbd2cSJim Jagielski   }
170*b1cdbd2cSJim Jagielski}
171*b1cdbd2cSJim Jagielskimy $compared=@passedTests+@failedTests;
172*b1cdbd2cSJim Jagielskimy $skip = @skippedTests;
173*b1cdbd2cSJim Jagielskiprint "skipped $skip test-cases(s)\n";
174*b1cdbd2cSJim Jagielskiprint "compared $compared test-case documents\n";
175*b1cdbd2cSJim Jagielskiprint "\t \t $passed tests $@passedTests\n";
176*b1cdbd2cSJim Jagielskiif ( @failedTests > 0 ) {
177*b1cdbd2cSJim Jagielski   print "the following test-case documents failed, please examine the logs manually\n";
178*b1cdbd2cSJim Jagielski
179*b1cdbd2cSJim Jagielski   for $a (@failedTests) {
180*b1cdbd2cSJim Jagielski      print "\t$a\n";
181*b1cdbd2cSJim Jagielski   }
182*b1cdbd2cSJim Jagielski   print "e.g. $failureCmd\n"
183*b1cdbd2cSJim Jagielski}
184