blib/lib/Devel/XSCover/Test.pm

filter: Statementstmt 100% 143 / 143 Branchbran 100% 78 / 78 Conditioncond 100% 15 / 15 MC/DCmcdc 100% 10 / 10 Subroutinesub 100% 18 / 18 Podpod 100% 8 / 8 totaltotal 100% 272 / 272 CC 49 SCAR 21.1
CC
49
Cov
100%
commands
14.0
_run
14.0
thread_tests
7.0
CRAP 8.2
« blib/lib/Devel/XSCover/Gcov.pm ↑ Summary blib/lib/Devel/XSCover/XS.pm »
1 package Devel::XSCover::Test;
2  
3 38 ▶use 5.020;
Subroutine BEGIN: called CC=1 SCAR=0.0
4 38 ▶use strict;
Subroutine BEGIN: called CC=1 SCAR=0.0
5 38 ▶use warnings;
Subroutine BEGIN: called CC=1 SCAR=0.0
6  
7 38 ▶use Cwd ();
Subroutine BEGIN: called CC=1 SCAR=0.0
8 38 ▶use File::Path ();
Subroutine BEGIN: called CC=1 SCAR=0.0
9 38 ▶use File::Spec ();
Subroutine BEGIN: called CC=1 SCAR=0.0
10  
11 38 our $VERSION = '0.10';
12  
13 187 ▶sub new {
Subroutine new: called CC=2 SCAR=6.9
Pod documented
14 187 my ($class, %args) = @_;
15 187 my $self = bless {
16 dir => '.',
17 db => 'cover_db',
18 make => 'make',
19 prove => 'prove',
20 test => [],
21 plain => [],
22 delete => 1,
23 no_perl => 0,
24 no_deparse => 0,
25 threads_covered => 0,
26 timeout => 0,
27 dry => 0,
28 silent => 0,
29 }, $class;
30 187 $self->{$_} = $args{$_} for keys %args;
31 187 return $self;
32 }
33  
34 258 ▶sub db_path {
Subroutine db_path: called CC=1 SCAR=0.0
Pod documented
35 258 my ($self) = @_;
36 258 return File::Spec->rel2abs($self->{db}, File::Spec->rel2abs($self->{dir}));
37 }
38  
39 132 ▶sub switches {
Subroutine switches: called CC=6 SCAR=17.9
Pod documented
40 132 my ($self) = @_;
41 132 my $db = $self->db_path;
42 132 my $dir = File::Spec->rel2abs($self->{dir});
43 132 for ($db, $dir) {
44 264 ▶ s/\\/\\\\/g if $^O eq 'MSWin32';
Branch: 1 branch
BranchTrueFalseTotal
if $^O eq "MSWin32" 24 240 264
45 264 s/ /\\ /g;
46 }
47 132 ▶ my $cover = "-MDevel::Cover=-db,$db,-dir,$dir,-ignore," . ($^O eq 'MSWin32' ? '^[A-Za-z]:' : '^/') . ',-ignore,/Install/Files\.pm$';
Branch: 1 branch
BranchTrueFalseTotal
$^O eq 'MSWin32' ? : 12 120 132
48 132 ▶ $cover .= ',-silent,1' if $self->{silent};
Branch: 1 branch
BranchTrueFalseTotal
if $self->{"silent"} 91 41 132
49 132 ▶ return join ' ', grep { defined && length } $ENV{DEVEL_COVER_TEST_OPTS}, $self->deparse_switch, $cover;
Condition100%3/3
defined $_ && length $_
!ll&&!rl&&r
12318255
MC/DC100%2/2
defined $_ && length $_
defined $_ length $_
Truth table100%3/3
A && B
A= defined $_B= length $_
ABresult
0X0
100
111
50 }
51  
52 159 ▶sub deparse_switch {
Subroutine deparse_switch: called CC=4 SCAR=13.9
Pod documented
53 159 my ($self) = @_;
54 159 ▶ return '' if $self->{no_deparse};
Branch: 1 branch
BranchTrueFalseTotal
if $self->{"no_deparse"} 18 141 159
55 141 ▶ my $pm = $INC{'Devel/XSCover/Test.pm'} or return '-MDevel::XSCover::Deparse';
Branch: 1 branch
BranchTrueFalseTotal
my $pm = $INC{"Devel/XSCover/Test.pm"} or return "-MDevel::XSCover::Deparse" 132 9 141
56 132 (my $lib = $pm) =~ s{[/\\]Devel[/\\]XSCover[/\\]Test\.pm\z}{};
57 132 $lib = File::Spec->rel2abs($lib);
58 132 ▶ return -f File::Spec->catfile($lib, 'Devel', 'XSCover', 'Deparse.pm') ? "-I$lib -MDevel::XSCover::Deparse" : '-MDevel::XSCover::Deparse';
Branch: 1 branch
BranchTrueFalseTotal
-f 'File::Spec'->catfile($lib, 'Devel', 'XSCover', 'Deparse.pm') ? : 123 9 132
59 }
60  
61 75 ▶sub reset_db {
Subroutine reset_db: called CC=4 SCAR=13.9
Pod documented
62 75 my ($self) = @_;
63 75 ▶ return 0 unless $self->{delete};
Branch: 1 branch
BranchTrueFalseTotal
unless $self->{"delete"} 1 74 75
64 74 my $db = $self->db_path;
65 74 ▶ return 0 unless -d $db;
Branch: 1 branch
BranchTrueFalseTotal
unless -d $db 68 6 74
66 6 $self->_say("xs_cover: removing $db");
67 6 ▶ File::Path::remove_tree($db) unless $self->{dry};
Branch: 1 branch
BranchTrueFalseTotal
unless $self->{"dry"} 5 1 6
68 6 return 1;
69 }
70  
71 136 ▶sub thread_tests {
Subroutine thread_tests: called CC=7 SCAR=19.5
Pod documented
72 136 my ($self, @files) = @_;
73 136 my $here = Cwd::getcwd();
74 136 ▶ chdir $self->{dir} or die "xs_cover: cannot chdir to $self->{dir}: $!\n";
Branch: 1 branch
BranchTrueFalseTotal
chdir $self->{"dir"} or die qq[xs_cover: cannot chdir to $self->{"dir"}: $!\n] 135 1 136
75 135 ▶ @files = sort glob 't/*.t' unless @files;
Subroutine BEGIN: called CC=1 SCAR=0.0
Branch: 1 branch
BranchTrueFalseTotal
unless @files 107 28 135
76 135 my @threaded;
77 135 for my $file (@files) {
78 302 ▶ open my $fh, '<', $file or next;
Branch: 1 branch
BranchTrueFalseTotal
open my $fh, "<", $file or next 293 9 302
79 293 ▶ while (my $line = <$fh>) {
Branch: 1 branch
BranchTrueFalseTotal
if (defined(my $line = readline $fh)) 140 247 387
80 140 ▶ next unless $line =~ /^\s*(?:use|require)\s+threads\b|\bthreads->create\b|\bthreads->new\b|^\s*use\s+Thread\b/;
Branch: 1 branch
BranchTrueFalseTotal
unless $line =~ /^\s*(?:use|require)\s+threads\b|\bthreads->create\b|\bthreads->new\b|^\s*use\s+Thread\b/u 94 46 140
81 46 push @threaded, $file;
82 46 last;
83 }
84 293 close $fh;
85 }
86 135 chdir $here;
87 135 return @threaded;
88 }
89  
90 150 ▶sub commands {
Subroutine commands: called CC=14 SCAR=26.4
Pod documented
91 150 my ($self) = @_;
92 150 my @plain = @{ $self->{plain} };
93 150 my @test = @{ $self->{test} };
94 150 ▶ unless ($self->{no_perl} || $self->{threads_covered}) {
Branch: 1 branch
BranchTrueFalseTotal
unless ($self->{"no_perl"} or $self->{"threads_covered"}) 117 33 150
Condition100%3/3
$self->{"no_perl"} or $self->{"threads_covered"}
l!l&&r!l&&!r
2310117
MC/DC100%2/2
$self->{"no_perl"} or $self->{"threads_covered"}
$self->{"no_perl"} $self->{"threads_covered"}
Truth table100%3/3
A or B
A= $self->{"no_perl"}B= $self->{"threads_covered"}
ABresult
1X1
011
000
95 117 my %have = map { $_ => 1 } @plain;
96 117 my @threaded = grep { !$have{$_} } $self->thread_tests(@test);
97 117 ▶ if (@threaded) {
Branch: 1 branch
BranchTrueFalseTotal
if (@threaded) 19 98 117
98 19 ▶ $self->_say("xs_cover: @threaded use threads, which Devel::Cover does not work with, so " . (@threaded == 1 ? 'it runs' : 'they run') . ' without it');
Branch: 1 branch
BranchTrueFalseTotal
@threaded == 1 ? : 10 9 19
99 19 push @plain, @threaded;
100 }
101 }
102 150 ▶ my $covered = $self->{no_perl} ? 0 : 1;
Branch: 1 branch
BranchTrueFalseTotal
$self->{'no_perl'} ? : 23 127 150
103 150 ▶ return ([ [ $self->{make}, 'test' ], $covered ]) unless @plain || @test;
Branch: 1 branch
BranchTrueFalseTotal
unless @plain or @test 81 69 150
Condition100%3/3
@plain or @test
l!l&&r!l&&!r
561381
MC/DC100%2/2
@plain or @test
@plain @test
Truth table100%3/3
A or B
A= @plainB= @test
ABresult
1X1
011
000
104 69 ▶ return ([ [ $self->{prove}, '-b', '-j1', @test ], 0 ]) if $self->{no_perl} && @test;
Branch: 1 branch
BranchTrueFalseTotal
if $self->{"no_perl"} and @test 5 64 69
Condition100%3/3
$self->{"no_perl"} and @test
!ll&&!rl&&r
5955
MC/DC100%2/2
$self->{"no_perl"} and @test
$self->{"no_perl"} @test
Truth table100%3/3
A and B
A= $self->{"no_perl"}B= @test
ABresult
0X0
100
111
105 64 my %plain = map { $_ => 1 } @plain;
106 64 ▶ unless (@test) {
Branch: 1 branch
BranchTrueFalseTotal
unless (@test) 45 19 64
107 45 my $here = Cwd::getcwd();
108 45 ▶ chdir $self->{dir} or die "xs_cover: cannot chdir to $self->{dir}: $!\n";
Branch: 1 branch
BranchTrueFalseTotal
chdir $self->{"dir"} or die qq[xs_cover: cannot chdir to $self->{"dir"}: $!\n] 44 1 45
109 44 @test = sort glob 't/*.t';
110 44 chdir $here;
111 }
112 63 my @covered = grep { !$plain{$_} } @test;
113 63 my @commands;
114 63 ▶ push @commands, [ [ $self->{prove}, '-b', '-j1', @covered ], $covered ] if @covered;
Branch: 1 branch
BranchTrueFalseTotal
if @covered 58 5 63
115 63 ▶ push @commands, [ [ $self->{prove}, '-b', '-j1', @plain ], 0 ] if @plain;
Branch: 1 branch
BranchTrueFalseTotal
if @plain 55 8 63
116 63 return @commands;
117 }
118  
119 70 ▶sub run {
Subroutine run: called CC=4 SCAR=13.9
Pod documented
120 70 my ($self) = @_;
121 70 $self->reset_db;
122 70 my $status = 0;
123 70 for my $command ($self->commands) {
124 81 my ($cmd, $covered) = @$command;
125 81 my $s = $self->_run($cmd, $covered);
126 59 ▶ $status = $s if $s && !$status;
Branch: 1 branch
BranchTrueFalseTotal
if $s and not $status 22 37 59
Condition100%3/3
$s and not $status
!ll&&!rl&&r
35222
MC/DC100%2/2
$s and not $status
$s not $status
Truth table100%3/3
A and not(B)
A= $sB= not $status
ABresult
0X0
100
111
127 }
128 48 return $status;
129 }
130  
131 81 ▶sub _run {
Subroutine _run: called CC=14 SCAR=26.4
132 81 my ($self, $cmd, $covered) = @_;
133 81 ▶ $self->_say(sprintf 'xs_cover: running %s%s', "@$cmd", $covered ? ' under Devel::Cover' : ' without Devel::Cover');
Branch: 1 branch
BranchTrueFalseTotal
$covered ? : 63 18 81
134 81 ▶ return 0 if $self->{dry};
Branch: 1 branch
BranchTrueFalseTotal
if $self->{"dry"} 3 78 81
135 78 my $here = Cwd::getcwd();
136 78 ▶ chdir $self->{dir} or die "xs_cover: cannot chdir to $self->{dir}: $!\n";
Branch: 1 branch
BranchTrueFalseTotal
chdir $self->{"dir"} or die qq[xs_cover: cannot chdir to $self->{"dir"}: $!\n] 77 1 78
137 77 my $switches = $self->switches;
138 77 my $pid = fork;
139 77 ▶ die "xs_cover: cannot fork: $!\n" unless defined $pid;
Branch: 1 branch
BranchTrueFalseTotal
unless defined $pid 3 74 77
140 74 ▶ if (!$pid) {
Branch: 1 branch
BranchTrueFalseTotal
unless ($pid) 19 55 74
141 19 eval { setpgrp 0, 0 };
142 19 ▶ if ($covered) {
Branch: 1 branch
BranchTrueFalseTotal
if ($covered) { } 16 3 19
143 16 $ENV{HARNESS_PERL_SWITCHES} = $switches;
144 16 $ENV{PERL5OPT} = $switches;
145 }
146 else {
147 3 delete @ENV{qw(HARNESS_PERL_SWITCHES PERL5OPT)};
148 }
149 19 ▶ exec @$cmd or exit 127;
Branch: 1 branch
BranchTrueFalseTotal
exec @$cmd or exit 127 1 1 2
150 }
151 56 ▶ $self->_say("xs_cover: waiting on pid $pid") if $self->{timeout};
Branch: 1 branch
BranchTrueFalseTotal
if $self->{"timeout"} 6 50 56
152 56 my $status;
153 56 my $timed_out = !eval {
154 56 ▶ local $SIG{ALRM} = sub { die "timeout\n" };
Subroutine __ANON__: called CC=1 SCAR=0.0
155 56 ▶ alarm $self->{timeout} if $self->{timeout};
Branch: 1 branch
BranchTrueFalseTotal
if $self->{"timeout"} 6 50 56
156 56 waitpid $pid, 0;
157 51 $status = $?;
158 51 alarm 0;
159 51 1;
160 };
161 56 ▶ if ($timed_out) {
Branch: 1 branch
BranchTrueFalseTotal
if ($timed_out) 5 51 56
162 5 alarm 0;
163 5 warn "xs_cover: the test run passed $self->{timeout} seconds, ending process group $pid\n";
164 5 ▶ my $group = $^O eq 'MSWin32' ? $pid : -$pid;
Branch: 1 branch
BranchTrueFalseTotal
$^O eq 'MSWin32' ? : 2 3 5
165 5 kill 'TERM', $group;
166 5 sleep 2;
167 5 kill 'KILL', $group;
168 5 waitpid $pid, 0;
169 5 $status = 124 << 8;
170 }
171 56 chdir $here;
172 56 ▶ return 127 if $status == -1;
Branch: 1 branch
BranchTrueFalseTotal
if $status == -1 1 55 56
173 55 ▶ return 128 + ($status & 127) if $status & 127;
Branch: 1 branch
BranchTrueFalseTotal
if $status & 127 1 54 55
174 54 return $status >> 8;
175 }
176  
177 112 ▶sub _say {
Subroutine _say: called CC=2 SCAR=6.9
178 112 my ($self, $line) = @_;
179 112 ▶ print "$line\n" unless $self->{silent};
Branch: 1 branch
BranchTrueFalseTotal
unless $self->{"silent"} 29 83 112
180 }
181  
182 38 1;
183  
184 __END__
« blib/lib/Devel/XSCover/Gcov.pm ↑ Summary blib/lib/Devel/XSCover/XS.pm »