| 1 | # PANT::Test - Test modules from PANT | ||
| 2 | |||
| 3 | package PANT::Test; | ||
| 4 | |||
| 5 | 1 | ▶ | use 5.008; |
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||
| 6 | 1 | ▶ | use strict; |
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||
| 7 | 1 | ▶ | use warnings; |
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||
| 8 | 1 | ▶ | use Carp; |
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||
| 9 | 1 | ▶ | use Cwd; |
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||
| 10 | 1 | ▶ | use XML::Writer; |
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||
| 11 | 0 | ▶ | use Test::Harness::Straps; |
|
Subroutine
BEGIN: called CC=1 SCAR=2.6
| |||
| 12 | use Benchmark; | ||
| 13 | use Exporter; | ||
| 14 | |||
| 15 | our @ISA = qw(Exporter); | ||
| 16 | |||
| 17 | # Items to export into callers namespace by default. Note: do not export | ||
| 18 | # names by default without a very good reason. Use EXPORT_OK instead. | ||
| 19 | # Do not simply export all your public functions/methods/constants. | ||
| 20 | |||
| 21 | # This allows declaration use PANT ':all'; | ||
| 22 | # If you do not need this, moving things directly into @EXPORT or @EXPORT_OK | ||
| 23 | # will save memory. | ||
| 24 | our %EXPORT_TAGS = ( 'all' => [ qw() ] ); | ||
| 25 | |||
| 26 | our @EXPORT_OK = ( @{ $EXPORT_TAGS{'all'} } ); | ||
| 27 | |||
| 28 | our @EXPORT = qw( ); | ||
| 29 | |||
| 30 | our $VERSION = '0.15'; | ||
| 31 | |||
| 32 | |||
| 33 | sub new { | ||
| 34 | my($clsname, $writer, @args) =@_; | ||
| 35 | my $self = { | ||
| 36 | writer=>$writer, | ||
| 37 | @args | ||
| 38 | }; | ||
| 39 | bless $self, $clsname; | ||
| 40 | return $self; | ||
| 41 | } | ||
| 42 | |||
| 43 | sub RunTests { | ||
| 44 | my($self, %args) = @_; | ||
| 45 | my $writer = $self->{writer}; | ||
| 46 | my $dir = $args{directory}; | ||
| 47 | my $pushstreams = 1; | ||
| 48 | my $cdir; | ||
| 49 | if ($dir) { | ||
| 50 | $cdir = getcwd; | ||
| 51 | chdir($dir) || Abort("Can't change to directory $dir"); | ||
| 52 | } | ||
| 53 | my $retval = 1; | ||
| 54 | $writer->dataElement('h2', "Run the following tests"); | ||
| 55 | $writer->startTag('ul'); | ||
| 56 | $writer->startTag('table', border=>1); | ||
| 57 | $writer->startTag('tr'); | ||
| 58 | foreach my $h (("Test", "No.", "Passed", "Failed", "Skipped", "Pass rate", "Failure reason")) { | ||
| 59 | $writer->dataElement('th', $h); | ||
| 60 | } | ||
| 61 | $writer->endTag('tr'); | ||
| 62 | my $stderrfile = "xxxxstderr$$.txt"; | ||
| 63 | my($OLDERR, $stderr); | ||
| 64 | if ($pushstreams) { | ||
| 65 | # push the output state | ||
| 66 | open $OLDERR, ">&", \*STDERR or die "Can't dup STDERR: $!"; | ||
| 67 | $stderr = ""; | ||
| 68 | close(STDERR); | ||
| 69 | open(STDERR, ">$stderrfile") or die "Can't open STDERR: $!"; | ||
| 70 | } | ||
| 71 | my $totaltests = 0; | ||
| 72 | my $totalpass = 0; | ||
| 73 | my $tfiles = 0; | ||
| 74 | my $tfailures = 0; | ||
| 75 | my $strap = new Test::Harness::Straps; | ||
| 76 | my $t_start = new Benchmark; | ||
| 77 | |||
| 78 | foreach my $tfile (@{$args{tests}}) { | ||
| 79 | $writer->startTag('tr'); | ||
| 80 | $writer->dataElement('td', $tfile); | ||
| 81 | $tfiles ++; | ||
| 82 | |||
| 83 | my %results; | ||
| 84 | if (!$self->{dryrun}) { | ||
| 85 | %results = $strap->analyze_file($tfile); | ||
| 86 | } | ||
| 87 | |||
| 88 | if (!%results) { | ||
| 89 | $writer->dataElement('td', $self->{dryrun} ? "Test not run -dryrun" : $strap->{error}); | ||
| 90 | $writer->endTag('tr'); | ||
| 91 | $totaltests ++; | ||
| 92 | next; | ||
| 93 | }; | ||
| 94 | $tfailures ++ if (!$results{passing}); | ||
| 95 | $totalpass += $results{ok}; | ||
| 96 | $totaltests += $results{max}; | ||
| 97 | my %attr = (id=> ($results{passing} ? "pass" : "fail")); | ||
| 98 | $writer->dataElement('td', $results{max}, %attr); | ||
| 99 | $writer->dataElement('td', $results{ok}, %attr); | ||
| 100 | $writer->dataElement('td', $results{max} - $results{ok}, %attr); | ||
| 101 | $writer->dataElement('td', $results{skip}, %attr); | ||
| 102 | $writer->dataElement('td', sprintf("%.2f", $results{ok} / $results{max} * 100), %attr); | ||
| 103 | $writer->startTag('td', %attr); | ||
| 104 | foreach my $err (MakeFailureReport(\%results)) { | ||
| 105 | $writer->characters($err); | ||
| 106 | $writer->emptyTag('br'); | ||
| 107 | } | ||
| 108 | $writer->endTag('td'); | ||
| 109 | $writer->endTag('tr'); | ||
| 110 | } | ||
| 111 | my $timed = timediff(new Benchmark, $t_start); | ||
| 112 | if ($pushstreams) { | ||
| 113 | open STDERR, ">&", $OLDERR or die "Can't dup OLDERR: $!"; | ||
| 114 | if (open JUNK, "$stderrfile") { | ||
| 115 | local($/); | ||
| 116 | $stderr = <JUNK>; | ||
| 117 | close(JUNK); | ||
| 118 | } | ||
| 119 | unlink($stderrfile); | ||
| 120 | } | ||
| 121 | $writer->endTag("table"); | ||
| 122 | $writer->dataElement('li', | ||
| 123 | sprintf("Summary: Test Files $tfiles, Failed Test files $tfailures, %.2f%%", | ||
| 124 | ($tfiles-$tfailures) / $tfiles * 100)); | ||
| 125 | $writer->dataElement('li', | ||
| 126 | sprintf("Summary: Total Tests $totaltests, Failed Tests %d, Pass rate %.2f%%", | ||
| 127 | $totaltests - $totalpass, | ||
| 128 | $totalpass / $totaltests * 100)); | ||
| 129 | $writer->dataElement('li', "Took " . timestr($timed)); | ||
| 130 | |||
| 131 | if($stderr) { | ||
| 132 | $writer->dataElement('li', "Error output"); | ||
| 133 | $writer->dataElement('pre', $stderr); | ||
| 134 | } | ||
| 135 | chdir ($cdir) if ($cdir); | ||
| 136 | $writer->endTag('ul'); | ||
| 137 | return $retval; | ||
| 138 | } | ||
| 139 | |||
| 140 | sub MakeFailureReport { | ||
| 141 | my $report = shift; | ||
| 142 | return ("All Passed") if ($report->{passing}); | ||
| 143 | my @results = (); | ||
| 144 | my $tnum = 0; | ||
| 145 | foreach my $test (@{$report->{details}}) { | ||
| 146 | $tnum ++; | ||
| 147 | next if ($test->{ok}); | ||
| 148 | push(@results, "$tnum $test->{name}"); | ||
| 149 | } | ||
| 150 | return @results; | ||
| 151 | } | ||
| 152 | 1; | ||
| 153 | __END__ | ||