blib/lib/Devel/XSCover/Test.pm

filter: Statementstmt 100% 141 / 141 Branchbran 100% 78 / 78 Conditioncond 100% 15 / 15 MC/DCmcdc 100% 10 / 10 Subroutinesub 100% 18 / 18 Podpod 100% 8 / 8 totaltotal 100% 270 / 270 CC 48 SCAR 21.0
CC
48
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.08';
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=5 SCAR=16.1
Pod documented
40 132 my ($self) = @_;
41 132 my $db = $self->db_path;
42 132 ▶ $db =~ s/\\/\\\\/g if $^O eq 'MSWin32';
Branch: 1 branch
BranchTrueFalseTotal
if $^O eq "MSWin32" 12 120 132
43 132 $db =~ s/ /\\ /g;
44 132 ▶ my $cover = "-MDevel::Cover=-db,$db,-ignore," . ($^O eq 'MSWin32' ? '^[A-Za-z]:' : '^/') . ',-ignore,/Install/Files\.pm$';
Branch: 1 branch
BranchTrueFalseTotal
$^O eq 'MSWin32' ? : 12 120 132
45 132 ▶ $cover .= ',-silent,1' if $self->{silent};
Branch: 1 branch
BranchTrueFalseTotal
if $self->{"silent"} 91 41 132
46 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
47 }
48  
49 159 ▶sub deparse_switch {
Subroutine deparse_switch: called CC=4 SCAR=13.9
Pod documented
50 159 my ($self) = @_;
51 159 ▶ return '' if $self->{no_deparse};
Branch: 1 branch
BranchTrueFalseTotal
if $self->{"no_deparse"} 18 141 159
52 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
53 132 (my $lib = $pm) =~ s{[/\\]Devel[/\\]XSCover[/\\]Test\.pm\z}{};
54 132 $lib = File::Spec->rel2abs($lib);
55 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
56 }
57  
58 75 ▶sub reset_db {
Subroutine reset_db: called CC=4 SCAR=13.9
Pod documented
59 75 my ($self) = @_;
60 75 ▶ return 0 unless $self->{delete};
Branch: 1 branch
BranchTrueFalseTotal
unless $self->{"delete"} 1 74 75
61 74 my $db = $self->db_path;
62 74 ▶ return 0 unless -d $db;
Branch: 1 branch
BranchTrueFalseTotal
unless -d $db 68 6 74
63 6 $self->_say("xs_cover: removing $db");
64 6 ▶ File::Path::remove_tree($db) unless $self->{dry};
Branch: 1 branch
BranchTrueFalseTotal
unless $self->{"dry"} 5 1 6
65 6 return 1;
66 }
67  
68 136 ▶sub thread_tests {
Subroutine thread_tests: called CC=7 SCAR=19.5
Pod documented
69 136 my ($self, @files) = @_;
70 136 my $here = Cwd::getcwd();
71 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
72 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
73 135 my @threaded;
74 135 for my $file (@files) {
75 302 ▶ open my $fh, '<', $file or next;
Branch: 1 branch
BranchTrueFalseTotal
open my $fh, "<", $file or next 293 9 302
76 293 ▶ while (my $line = <$fh>) {
Branch: 1 branch
BranchTrueFalseTotal
if (defined(my $line = readline $fh)) 140 247 387
77 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
78 46 push @threaded, $file;
79 46 last;
80 }
81 293 close $fh;
82 }
83 135 chdir $here;
84 135 return @threaded;
85 }
86  
87 150 ▶sub commands {
Subroutine commands: called CC=14 SCAR=26.4
Pod documented
88 150 my ($self) = @_;
89 150 my @plain = @{ $self->{plain} };
90 150 my @test = @{ $self->{test} };
91 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
92 117 my %have = map { $_ => 1 } @plain;
93 117 my @threaded = grep { !$have{$_} } $self->thread_tests(@test);
94 117 ▶ if (@threaded) {
Branch: 1 branch
BranchTrueFalseTotal
if (@threaded) 19 98 117
95 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
96 19 push @plain, @threaded;
97 }
98 }
99 150 ▶ my $covered = $self->{no_perl} ? 0 : 1;
Branch: 1 branch
BranchTrueFalseTotal
$self->{'no_perl'} ? : 23 127 150
100 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
101 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
102 64 my %plain = map { $_ => 1 } @plain;
103 64 ▶ unless (@test) {
Branch: 1 branch
BranchTrueFalseTotal
unless (@test) 45 19 64
104 45 my $here = Cwd::getcwd();
105 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
106 44 @test = sort glob 't/*.t';
107 44 chdir $here;
108 }
109 63 my @covered = grep { !$plain{$_} } @test;
110 63 my @commands;
111 63 ▶ push @commands, [ [ $self->{prove}, '-b', '-j1', @covered ], $covered ] if @covered;
Branch: 1 branch
BranchTrueFalseTotal
if @covered 58 5 63
112 63 ▶ push @commands, [ [ $self->{prove}, '-b', '-j1', @plain ], 0 ] if @plain;
Branch: 1 branch
BranchTrueFalseTotal
if @plain 55 8 63
113 63 return @commands;
114 }
115  
116 70 ▶sub run {
Subroutine run: called CC=4 SCAR=13.9
Pod documented
117 70 my ($self) = @_;
118 70 $self->reset_db;
119 70 my $status = 0;
120 70 for my $command ($self->commands) {
121 81 my ($cmd, $covered) = @$command;
122 81 my $s = $self->_run($cmd, $covered);
123 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
124 }
125 48 return $status;
126 }
127  
128 81 ▶sub _run {
Subroutine _run: called CC=14 SCAR=26.4
129 81 my ($self, $cmd, $covered) = @_;
130 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
131 81 ▶ return 0 if $self->{dry};
Branch: 1 branch
BranchTrueFalseTotal
if $self->{"dry"} 3 78 81
132 78 my $here = Cwd::getcwd();
133 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
134 77 my $switches = $self->switches;
135 77 my $pid = fork;
136 77 ▶ die "xs_cover: cannot fork: $!\n" unless defined $pid;
Branch: 1 branch
BranchTrueFalseTotal
unless defined $pid 3 74 77
137 74 ▶ if (!$pid) {
Branch: 1 branch
BranchTrueFalseTotal
unless ($pid) 19 55 74
138 19 eval { setpgrp 0, 0 };
139 19 ▶ if ($covered) {
Branch: 1 branch
BranchTrueFalseTotal
if ($covered) { } 16 3 19
140 16 $ENV{HARNESS_PERL_SWITCHES} = $switches;
141 16 $ENV{PERL5OPT} = $switches;
142 }
143 else {
144 3 delete @ENV{qw(HARNESS_PERL_SWITCHES PERL5OPT)};
145 }
146 19 ▶ exec @$cmd or exit 127;
Branch: 1 branch
BranchTrueFalseTotal
exec @$cmd or exit 127 1 1 2
147 }
148 56 ▶ $self->_say("xs_cover: waiting on pid $pid") if $self->{timeout};
Branch: 1 branch
BranchTrueFalseTotal
if $self->{"timeout"} 6 50 56
149 56 my $status;
150 56 my $timed_out = !eval {
151 56 ▶ local $SIG{ALRM} = sub { die "timeout\n" };
Subroutine __ANON__: called CC=1 SCAR=0.0
152 56 ▶ alarm $self->{timeout} if $self->{timeout};
Branch: 1 branch
BranchTrueFalseTotal
if $self->{"timeout"} 6 50 56
153 56 waitpid $pid, 0;
154 51 $status = $?;
155 51 alarm 0;
156 51 1;
157 };
158 56 ▶ if ($timed_out) {
Branch: 1 branch
BranchTrueFalseTotal
if ($timed_out) 5 51 56
159 5 alarm 0;
160 5 warn "xs_cover: the test run passed $self->{timeout} seconds, ending process group $pid\n";
161 5 ▶ my $group = $^O eq 'MSWin32' ? $pid : -$pid;
Branch: 1 branch
BranchTrueFalseTotal
$^O eq 'MSWin32' ? : 2 3 5
162 5 kill 'TERM', $group;
163 5 sleep 2;
164 5 kill 'KILL', $group;
165 5 waitpid $pid, 0;
166 5 $status = 124 << 8;
167 }
168 56 chdir $here;
169 56 ▶ return 127 if $status == -1;
Branch: 1 branch
BranchTrueFalseTotal
if $status == -1 1 55 56
170 55 ▶ return 128 + ($status & 127) if $status & 127;
Branch: 1 branch
BranchTrueFalseTotal
if $status & 127 1 54 55
171 54 return $status >> 8;
172 }
173  
174 112 ▶sub _say {
Subroutine _say: called CC=2 SCAR=6.9
175 112 my ($self, $line) = @_;
176 112 ▶ print "$line\n" unless $self->{silent};
Branch: 1 branch
BranchTrueFalseTotal
unless $self->{"silent"} 29 83 112
177 }
178  
179 38 1;
180  
181 __END__
« blib/lib/Devel/XSCover/Gcov.pm ↑ Summary blib/lib/Devel/XSCover/XS.pm »