| 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
| |||||||||||||||||||||||||||||
| 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
| |||||||||||||||||||||||||||||
| 45 | 132 | ▶ | $cover .= ',-silent,1' if $self->{silent}; | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 46 | 132 | ▶ | return join ' ', grep { defined && length } $ENV{DEVEL_COVER_TEST_OPTS}, $self->deparse_switch, $cover; | ||||||||||||||||||||||||||
|
Condition100%3/3
defined $_ && length $_
MC/DC100%2/2
defined $_ && length $_
defined $_
length $_
Truth table100%3/3
A && B
A= defined $_B= length $_
| |||||||||||||||||||||||||||||
| 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
| |||||||||||||||||||||||||||||
| 52 | 141 | ▶ | my $pm = $INC{'Devel/XSCover/Test.pm'} or return '-MDevel::XSCover::Deparse'; | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 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
| |||||||||||||||||||||||||||||
| 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
| |||||||||||||||||||||||||||||
| 61 | 74 | my $db = $self->db_path; | |||||||||||||||||||||||||||
| 62 | 74 | ▶ | return 0 unless -d $db; | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 63 | 6 | $self->_say("xs_cover: removing $db"); | |||||||||||||||||||||||||||
| 64 | 6 | ▶ | File::Path::remove_tree($db) unless $self->{dry}; | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 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
| |||||||||||||||||||||||||||||
| 72 | 135 | ▶ | @files = sort glob 't/*.t' unless @files; | ||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 73 | 135 | my @threaded; | |||||||||||||||||||||||||||
| 74 | 135 | for my $file (@files) { | |||||||||||||||||||||||||||
| 75 | 302 | ▶ | open my $fh, '<', $file or next; | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 76 | 293 | ▶ | while (my $line = <$fh>) { | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 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
| |||||||||||||||||||||||||||||
| 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
Condition100%3/3
$self->{"no_perl"} or $self->{"threads_covered"}
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"}
| |||||||||||||||||||||||||||||
| 92 | 117 | my %have = map { $_ => 1 } @plain; | |||||||||||||||||||||||||||
| 93 | 117 | my @threaded = grep { !$have{$_} } $self->thread_tests(@test); | |||||||||||||||||||||||||||
| 94 | 117 | ▶ | if (@threaded) { | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 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
| |||||||||||||||||||||||||||||
| 96 | 19 | push @plain, @threaded; | |||||||||||||||||||||||||||
| 97 | } | ||||||||||||||||||||||||||||
| 98 | } | ||||||||||||||||||||||||||||
| 99 | 150 | ▶ | my $covered = $self->{no_perl} ? 0 : 1; | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 100 | 150 | ▶ | return ([ [ $self->{make}, 'test' ], $covered ]) unless @plain || @test; | ||||||||||||||||||||||||||
|
Branch: 1 branch
Condition100%3/3
@plain or @test
MC/DC100%2/2
@plain or @test
@plain
@test
Truth table100%3/3
A or B
A= @plainB= @test
| |||||||||||||||||||||||||||||
| 101 | 69 | ▶ | return ([ [ $self->{prove}, '-b', '-j1', @test ], 0 ]) if $self->{no_perl} && @test; | ||||||||||||||||||||||||||
|
Branch: 1 branch
Condition100%3/3
$self->{"no_perl"} and @test
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
| |||||||||||||||||||||||||||||
| 102 | 64 | my %plain = map { $_ => 1 } @plain; | |||||||||||||||||||||||||||
| 103 | 64 | ▶ | unless (@test) { | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 104 | 45 | my $here = Cwd::getcwd(); | |||||||||||||||||||||||||||
| 105 | 45 | ▶ | chdir $self->{dir} or die "xs_cover: cannot chdir to $self->{dir}: $!\n"; | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 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
| |||||||||||||||||||||||||||||
| 112 | 63 | ▶ | push @commands, [ [ $self->{prove}, '-b', '-j1', @plain ], 0 ] if @plain; | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 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
Condition100%3/3
$s and not $status
MC/DC100%2/2
$s and not $status
$s
not $status
Truth table100%3/3
A and not(B)
A= $sB= not $status
| |||||||||||||||||||||||||||||
| 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
| |||||||||||||||||||||||||||||
| 131 | 81 | ▶ | return 0 if $self->{dry}; | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 132 | 78 | my $here = Cwd::getcwd(); | |||||||||||||||||||||||||||
| 133 | 78 | ▶ | chdir $self->{dir} or die "xs_cover: cannot chdir to $self->{dir}: $!\n"; | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 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
| |||||||||||||||||||||||||||||
| 137 | 74 | ▶ | if (!$pid) { | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 138 | 19 | eval { setpgrp 0, 0 }; | |||||||||||||||||||||||||||
| 139 | 19 | ▶ | if ($covered) { | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 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
| |||||||||||||||||||||||||||||
| 147 | } | ||||||||||||||||||||||||||||
| 148 | 56 | ▶ | $self->_say("xs_cover: waiting on pid $pid") if $self->{timeout}; | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 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
| |||||||||||||||||||||||||||||
| 153 | 56 | waitpid $pid, 0; | |||||||||||||||||||||||||||
| 154 | 51 | $status = $?; | |||||||||||||||||||||||||||
| 155 | 51 | alarm 0; | |||||||||||||||||||||||||||
| 156 | 51 | 1; | |||||||||||||||||||||||||||
| 157 | }; | ||||||||||||||||||||||||||||
| 158 | 56 | ▶ | if ($timed_out) { | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 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
| |||||||||||||||||||||||||||||
| 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
| |||||||||||||||||||||||||||||
| 170 | 55 | ▶ | return 128 + ($status & 127) if $status & 127; | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 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
| |||||||||||||||||||||||||||||
| 177 | } | ||||||||||||||||||||||||||||
| 178 | |||||||||||||||||||||||||||||
| 179 | 38 | 1; | |||||||||||||||||||||||||||
| 180 | |||||||||||||||||||||||||||||
| 181 | __END__ | ||||||||||||||||||||||||||||