File Coverage

blib/lib/PANT/Test.pm
Criterion Covered Total %
statement 78 116 67.2
branch 9 34 26.4
condition n/a
subroutine 11 12 91.6
pod 1 3 33.3
total 99 165 60.0


line stmt bran cond sub pod time code
1             # PANT::Test - Test modules from PANT
2            
3             package PANT::Test;
4            
5 1     1   1868 use 5.008;
  1         4  
  1         44  
6 1     1   6 use strict;
  1         3  
  1         40  
7 1     1   6 use warnings;
  1         2  
  1         33  
8 1     1   5 use Carp;
  1         4  
  1         74  
9 1     1   6 use Cwd;
  1         2  
  1         65  
10 1     1   6 use XML::Writer;
  1         2  
  1         24  
11 1     1   1042 use Test::Harness::Straps;
  1         38452  
  1         61  
12 1     1   1295 use Benchmark;
  1         12233  
  1         10  
13 1     1   182 use Exporter;
  1         3  
  1         1284  
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 1     1 1 3 my($clsname, $writer, @args) =@_;
35 1         3 my $self = {
36             writer=>$writer,
37             @args
38             };
39 1         2 bless $self, $clsname;
40 1         3 return $self;
41             }
42            
43             sub RunTests {
44 1     1 0 5 my($self, %args) = @_;
45 1         6 my $writer = $self->{writer};
46 1         2 my $dir = $args{directory};
47 1         1 my $pushstreams = 1;
48 1         8 my $cdir;
49 1 50       4 if ($dir) {
50 1         7 $cdir = getcwd;
51 1 50       23 chdir($dir) || Abort("Can't change to directory $dir");
52             }
53 1         2 my $retval = 1;
54 1         4 $writer->dataElement('h2', "Run the following tests");
55 1         65 $writer->startTag('ul');
56 1         29 $writer->startTag('table', border=>1);
57 1         47 $writer->startTag('tr');
58 1         28 foreach my $h (("Test", "No.", "Passed", "Failed", "Skipped", "Pass rate", "Failure reason")) {
59 7         369 $writer->dataElement('th', $h);
60             }
61 1         56 $writer->endTag('tr');
62 1         18 my $stderrfile = "xxxxstderr$$.txt";
63 1         1 my($OLDERR, $stderr);
64 1 50       4 if ($pushstreams) {
65             # push the output state
66 1 50       29 open $OLDERR, ">&", \*STDERR or die "Can't dup STDERR: $!";
67 1         3 $stderr = "";
68 1         5 close(STDERR);
69 1 50       107 open(STDERR, ">$stderrfile") or die "Can't open STDERR: $!";
70             }
71 1         3 my $totaltests = 0;
72 1         3 my $totalpass = 0;
73 1         2 my $tfiles = 0;
74 1         2 my $tfailures = 0;
75 1         12 my $strap = new Test::Harness::Straps;
76 1         28 my $t_start = new Benchmark;
77            
78 1         23 foreach my $tfile (@{$args{tests}}) {
  1         4  
79 1         3 $writer->startTag('tr');
80 1         41 $writer->dataElement('td', $tfile);
81 1         55 $tfiles ++;
82            
83 1         2 my %results;
84 1 50       4 if (!$self->{dryrun}) {
85 1         5 %results = $strap->analyze_file($tfile);
86             }
87            
88 1 50       1150317 if (!%results) {
89 0 0       0 $writer->dataElement('td', $self->{dryrun} ? "Test not run -dryrun" : $strap->{error});
90 0         0 $writer->endTag('tr');
91 0         0 $totaltests ++;
92 0         0 next;
93             };
94 1 50       21 $tfailures ++ if (!$results{passing});
95 1         46 $totalpass += $results{ok};
96 1         18 $totaltests += $results{max};
97 1 50       19 my %attr = (id=> ($results{passing} ? "pass" : "fail"));
98 1         33 $writer->dataElement('td', $results{max}, %attr);
99 1         252 $writer->dataElement('td', $results{ok}, %attr);
100 1         118 $writer->dataElement('td', $results{max} - $results{ok}, %attr);
101 1         167 $writer->dataElement('td', $results{skip}, %attr);
102 1         517 $writer->dataElement('td', sprintf("%.2f", $results{ok} / $results{max} * 100), %attr);
103 0           $writer->startTag('td', %attr);
104 0           foreach my $err (MakeFailureReport(\%results)) {
105 0           $writer->characters($err);
106 0           $writer->emptyTag('br');
107             }
108 0           $writer->endTag('td');
109 0           $writer->endTag('tr');
110             }
111 0           my $timed = timediff(new Benchmark, $t_start);
112 0 0         if ($pushstreams) {
113 0 0         open STDERR, ">&", $OLDERR or die "Can't dup OLDERR: $!";
114 0 0         if (open JUNK, "$stderrfile") {
115 0           local($/);
116 0           $stderr = ;
117 0           close(JUNK);
118             }
119 0           unlink($stderrfile);
120             }
121 0           $writer->endTag("table");
122 0           $writer->dataElement('li',
123             sprintf("Summary: Test Files $tfiles, Failed Test files $tfailures, %.2f%%",
124             ($tfiles-$tfailures) / $tfiles * 100));
125 0           $writer->dataElement('li',
126             sprintf("Summary: Total Tests $totaltests, Failed Tests %d, Pass rate %.2f%%",
127             $totaltests - $totalpass,
128             $totalpass / $totaltests * 100));
129 0           $writer->dataElement('li', "Took " . timestr($timed));
130            
131 0 0         if($stderr) {
132 0           $writer->dataElement('li', "Error output");
133 0           $writer->dataElement('pre', $stderr);
134             }
135 0 0         chdir ($cdir) if ($cdir);
136 0           $writer->endTag('ul');
137 0           return $retval;
138             }
139            
140             sub MakeFailureReport {
141 0     0 0   my $report = shift;
142 0 0         return ("All Passed") if ($report->{passing});
143 0           my @results = ();
144 0           my $tnum = 0;
145 0           foreach my $test (@{$report->{details}}) {
  0            
146 0           $tnum ++;
147 0 0         next if ($test->{ok});
148 0           push(@results, "$tnum $test->{name}");
149             }
150 0           return @results;
151             }
152             1;
153             __END__