blib/lib/Test2/Tools/Affix.pm

filter: Statementstmt 47.7% 106 / 222 Branchbran 11.4% 8 / 70 Conditioncond 0.0% 0 / 16 MC/DCmcdc 0.0% 0 / 10 Subroutinesub 71.8% 23 / 32 Podpod 0.0% 0 / 8 totaltotal 38.2% 137 / 358 CC 49 SCAR 40.2
CC
49
Cov
37%
parse_xml
210.0
parse_suppression
72.0
init_valgrind
61.5
CRAP 55.9
« blib/lib/Affix/Build.pm ↑ Summary blib/lib/Affix.pm »
1 package #
2 54 Test2::Tools::Affix v1.3.0 {
3 54 ▶ use v5.40;
Subroutine BEGIN: called CC=1 SCAR=0.0
4 54 ▶ use blib;
Subroutine BEGIN: called CC=1 SCAR=0.0
5 54 ▶ use Affix;
Subroutine BEGIN: called CC=1 SCAR=0.0
6 54 ▶ use Affix::Build;
Subroutine BEGIN: called CC=1 SCAR=0.0
7 54 ▶ use Test2::API qw[context run_subtest];
Subroutine BEGIN: called CC=1 SCAR=0.0
8 54 ▶ use Test2::Tools::Basic qw[ok plan skip_all done_testing diag note pass fail];
Subroutine BEGIN: called CC=1 SCAR=0.0
9 54 ▶ use Test2::Tools::Class qw[isa_ok];
Subroutine BEGIN: called CC=1 SCAR=0.0
10 54 ▶ use Test2::Tools::Compare qw[is isnt like float];
Subroutine BEGIN: called CC=1 SCAR=0.0
11 54 ▶ use Test2::Tools::Exception qw[lives dies];
Subroutine BEGIN: called CC=1 SCAR=0.0
12 54 ▶ use Test2::Tools::Warnings qw[warns];
Subroutine BEGIN: called CC=1 SCAR=0.0
13 54 ▶ use Test2::Plugin::UTF8;
Subroutine BEGIN: called CC=1 SCAR=0.0
14 54 ▶ use Test2::IPC;
Subroutine BEGIN: called CC=1 SCAR=0.0
15 54 ▶ use Path::Tiny qw[path tempfile];
Subroutine BEGIN: called CC=1 SCAR=0.0
16 54 ▶ use Exporter 'import';
Subroutine BEGIN: called CC=1 SCAR=0.0
17 54 ▶ use Capture::Tiny ':all';
Subroutine BEGIN: called CC=1 SCAR=0.0
18 54 our @CARP_NOT;
19 54 our @EXPORT = qw[compile_ok affix_ok leaks];
20 54 our %EXPORT_TAGS = ( all => \@EXPORT );
21 #
22 54 my $OS = $^O;
23 54 my $Inc = path($0)->absolute;
24 54 ▶ $Inc = $Inc->parent while !$Inc->child('t')->is_dir;
Branch: 1 branch
BranchTrueFalseTotal
$Inc->child("t")->is_dir or do { $Inc = $Inc->parent } 54 108 162
25 54 $Inc = $Inc->child( 't', 'src' );
26 54 my @cleanup;
27  
28 54 ▶ END {
Subroutine END: called CC=3 SCAR=12.0
29 54 for my $file ( grep {-f} @cleanup ) {
30 0 unlink $file;
31 }
32 54 for my $dir ( grep {-d} @cleanup ) {
33 0 $dir->remove_tree;
34 }
35 }
36 #
37 53 ▶ sub compile_ok( $name, $aggs //= {}, $keep //= 0 ) {
Subroutine compile_ok: called CC=8 SCAR=21.6
Pod undocumented
38 53 my $c = context();
39  
40 #~ return $c->pass_and_release($name) if 1;
41 #~ return $c->fail_and_release($name, @diag);
42 53 my ($opt) = grep { -f $_ } "t/src/$name.cxx", "t/src/$name.c", "src/$name.cxx", "src/$name.c";
43 53 ▶ if ($opt) {
Branch: 1 branch
BranchTrueFalseTotal
if ($opt) { } 0 53 53
44 0 $opt = path($opt)->absolute;
45 }
46 else {
47 53 ▶ $opt = tempfile(
Branch: 1 branch
BranchTrueFalseTotal
$name =~ m[^\s*//\s*ext:\s*\.c$]msu ? : 52 1 53
48 UNLINK => !$keep,
49 SUFFIX => '_' . path( [ caller() ]->[1] )->basename . ( $name =~ m[^\s*//\s*ext:\s*\.c$]ms ? '.c' : '.cxx' )
50 )->absolute;
51 53 ▶ push @cleanup, $opt unless $keep;
Branch: 1 branch
BranchTrueFalseTotal
unless $keep 53 0 53
52 53 my ( $package, $filename, $line ) = caller;
53 53 $filename = path($filename)->canonpath;
54 53 $line++;
55 53 $filename =~ s[\\][\\\\]g; # Windows...
56 53 $opt->spew_utf8(qq[#line $line "$filename"\r\n$name]);
57 }
58 53 ▶ if ( !$opt ) {
Branch: 1 branch
BranchTrueFalseTotal
unless ($opt) 0 53 53
59 0 $c->fail('Failed to locate test source');
60 0 $c->release;
61 0 return ();
62 }
63 53 $aggs->{cflags} .= ' -I' . $Inc;
64 53 my $compiler = Affix::Build->new( debug => 0, name => 'testing', version => '1.0', flags => $aggs );
65 53 $compiler->add( $opt->canonpath );
66 53 $compiler->link;
67 53 ▶ push @cleanup, $opt->canonpath, $compiler->link unless $keep;
Branch: 1 branch
BranchTrueFalseTotal
unless $keep 53 0 53
68 53 $c->ok( 1, 'build lib: ' . $compiler->link );
69 53 $c->release;
70 53 $compiler->link;
71 }
72  
73 1 ▶ sub affix_ok ( $lib, $name, $args, $ret ) {
Subroutine affix_ok: called CC=1 SCAR=0.0
Pod undocumented
74 1 my $c = context;
75 1 my $sub;
76 1 diag __PACKAGE__;
77 1 diag join ', ', caller;
78 my $okay = run_subtest(
79 'affix ' . $name . '( ... )',
80 1 ▶ sub {
Subroutine __ANON__: called CC=1 SCAR=0.0
81 1 ▶ ok lives {
Subroutine __ANON__: called CC=1 SCAR=0.0
82 1 $sub = affix( $lib, $name, $args, $ret )
83 1 }, 'affix ' . $name . ' ...';
84 1 isa_ok $sub, ['Affix'], $name;
85 },
86 1 { buffered => 0, no_fork => 1 }
87 );
88 1 $c->release;
89 1 return $sub;
90 }
91 {
92 54 my $supp; # defined later
93 54 my ( $test, $generate_suppressions, $count );
94 54 my $valgrind = 0;
95 54 my $file;
96  
97 0 ▶ sub init_valgrind {
Subroutine init_valgrind: not called CC=8 SCAR=41.2
Pod undocumented
98 0 ▶ return if $valgrind;
Branch: 1 branch
BranchTrueFalseTotal
if $valgrind 0 0 0
99 0 require Path::Tiny;
100 0 $file = Path::Tiny::path($0)->absolute;
101 0 $valgrind = 1;
102 0 ▶ return plan skip_all 'Capture::Tiny is not installed' unless eval 'require Capture::Tiny';
Branch: 1 branch
BranchTrueFalseTotal
unless eval "require Capture::Tiny" 0 0 0
103 0 ▶ return plan skip_all 'Path::Tiny is not installed' unless eval 'require Path::Tiny';
Branch: 1 branch
BranchTrueFalseTotal
unless eval "require Path::Tiny" 0 0 0
104 0 require Getopt::Long;
105 0 Getopt::Long::GetOptions( 'test=s' => \$test, 'generate' => \$generate_suppressions, 'count=i' => \$count );
106 0 ▶ Test2::API::test2_stack()->top->{count} = $count if defined $count;
Branch: 1 branch
BranchTrueFalseTotal
if defined $count 0 0 0
107  
108 0 ▶ if ( defined $test ) {
Branch: 2 branches
BranchTrueFalseTotal
if (defined $test) { } 0 0 0
elsif (defined $generate_suppressions) { } 0 0 0
109  
110 #~ Affix::set_destruct_level(3);
111 #~ die 'I should be running a test named ' . $test;
112 }
113 elsif ( defined $generate_suppressions ) {
114 54 ▶ no Test2::Plugin::ExitSummary;
Subroutine BEGIN: called CC=1 SCAR=0.0
115 0 pass 'exiting...';
116 0 done_testing;
117 0 exit;
118 }
119 else {
120 my ( $stdout, $stderr, $exit_code ) = Capture::Tiny::capture(
121 0 ▶ sub {
Subroutine __ANON__: not called CC=1 SCAR=6.9
122 0 system('valgrind --version');
123 }
124 0 );
125 0 ▶ plan skip_all 'Valgrind is not installed' if $exit_code;
Branch: 1 branch
BranchTrueFalseTotal
if $exit_code 0 0 0
126 0 diag 'Valgrind v', ( $stdout =~ m/valgrind-(.+)$/ ), ' found';
127 0 diag 'Generating suppressions...';
128 my @cmd = (
129 qw[valgrind --leak-check=full --show-reachable=yes --error-limit=no
130 --gen-suppressions=all --log-fd=1], $^X, '-e',
131 0 sprintf <<'', ( join ', ', map {"'$_'"} sort { length $a <=> length $b } map { path($_)->absolute->canonpath } @INC ) );
132 use strict;
133 use warnings;
134 use lib %s;
135 use Affix;
136 no Test2::Plugin::ExitSummary;
137 use Test2::V0;
138 pass "generate valgrind suppressions";
139 done_testing;
140  
141  
142 #~ use Data::Dump;
143 #~ ddx \@cmd;
144 my ( $out, $err, @res ) = Capture::Tiny::capture(
145 0 ▶ sub {
Subroutine __ANON__: not called CC=1 SCAR=6.9
146 0 system @cmd;
147 }
148 0 );
149 0 my ( $known, $dups ) = parse_suppression($out);
150  
151 #~ diag $out;
152 #~ diag $err;
153 0 diag scalar( keys %$known ) . ' suppressions found';
154 0 diag $dups . ' duplicates have been filtered out';
155 0 $known->{'BSD is trash'} = <<'';
156 {
157 <insert_a_suppression_name_here>
158 Memcheck:Free
159 fun:~vector
160 }
161  
162 0 $known->{'chaotic access'} = <<'';
163 {
164 <insert_a_suppression_name_here>
165 Memcheck:Addr1
166 fun:_DumpHex
167 }
168  
169  
170 # https://bugs.kde.org/show_bug.cgi?id=453084
171 # https://github.com/Perl/perl5/issues/19949
172 # https://github.com/Perl/perl5/issues/20970
173 0 $known->{'https://github.com/Perl/perl5/issues/19949'} = <<'';
174 {
175 <insert_a_suppression_name_here>
176 Memcheck:Overlap
177 fun:__memcpy_chk
178 fun:XS_Cwd_abs_path
179 fun:Perl_pp_entersub
180 fun:Perl_runops_standard
181 fun:S_docatch
182 fun:Perl_runops_standard
183 fun:Perl_call_sv
184 }
185 {
186 memmove overlapping source and destination
187 Memcheck:Overlap
188 fun:__memcpy_chk
189 }
190  
191 0 $supp = Path::Tiny::tempfile( { realpath => 1 }, 'valgrind_suppression_XXXXXXXXXX' );
192 0 diag 'spewing to ' . $supp;
193 0 diag $supp->spew( join "\n\n", values %$known );
194 0 push @cleanup, $supp;
195 0 Test2::API::test2_stack()->top->{count};
196  
197 #~ Test2::API::test2_stack()->top->{count}++;
198 }
199 }
200  
201 0 ▶ sub parse_suppression {
Subroutine parse_suppression: not called CC=8 SCAR=42.8
Pod undocumented
202 0 my $dups = 0;
203 0 my $known = {};
204 0 require Digest::MD5;
205 0 my @in = split /\R/, shift;
206 0 my $l = 0;
207 0 ▶ while ( $_ = shift @in ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($_ = shift @in) 0 0 0
208 0 $l++;
209 0 ▶ next unless (/^\{/);
Branch: 1 branch
BranchTrueFalseTotal
unless /^\{/u 0 0 0
210 0 my $block = $_ . "\n";
211 0 ▶ while ( $_ = shift @in ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($_ = shift @in) 0 0 0
212 0 $l++;
213 0 $block .= $_ . "\n";
214 0 ▶ last if /^\}/;
Branch: 1 branch
BranchTrueFalseTotal
if /^\}/u 0 0 0
215 }
216 0 ▶ $block // last;
Condition0%0/2
$block // (last)
l!l
00
MC/DC0%0/1
$block // (last)
$block
Truth table0%0/2
A
A= $block
Aresult
11
00
217 0 ▶ if ( $block !~ /\}\n/ ) {
Branch: 1 branch
BranchTrueFalseTotal
unless ($block =~ /\}\n/u) 0 0 0
218 0 diag "Unterminated suppression at line $l";
219 0 last;
220 }
221 0 my $key = $block;
222 0 $key =~ s/(\A\{[^\n]*\n)\s*[^\n]*\n/$1/;
223 0 my $sum = Digest::MD5::md5_hex($key);
224 0 ▶ $dups++ if exists $known->{$sum};
Branch: 1 branch
BranchTrueFalseTotal
if exists $known->{$sum} 0 0 0
225 0 $known->{$sum} = $block;
226 }
227 0 return ( $known, $dups );
228 }
229  
230 0 ▶ sub dec_ent {
Subroutine dec_ent: not called CC=2 SCAR=17.9
Pod undocumented
231 0 ▶ return $1 if $_[0] =~ m/^<!\[CDATA\[\{(.*)}]]>$/smg;
Branch: 1 branch
BranchTrueFalseTotal
if $_[0] =~ /^<!\[CDATA\[\{(.*)}]]>$/gmsu 0 0 0
232 0 $_[0] =~ s[&lt;][<]g;
233 0 $_[0] =~ s[&gt;][>]g;
234 0 $_[0] =~ s[&amp;][&]g;
235 0 shift;
236 }
237  
238 0 ▶ sub stacktrace($blah) {
Subroutine stacktrace: not called CC=5 SCAR=28.4
Pod undocumented
239 54 ▶ use Test2::Util::Table qw[table];
Subroutine BEGIN: called CC=1 SCAR=0.0
240 $blah ?
241 join "\n", table(
242 max_width => 120,
243 collapse => 1, # Do not show empty columns
244 header => [ 'function', 'path', 'line' ],
245 rows => [
246 0 ▶ map { [ $_->{fn}, ( defined $_->{dir} && defined $_->{file} ) ? join '/', $_->{dir}, $_->{file} : '', $_->{line} // '' ] } @$blah
Branch: 2 branches
BranchTrueFalseTotal
$blah ? : 0 0 0
defined $_->{'dir'} && defined $_->{'file'} ? : 0 0 0
Condition0%0/5
defined $_->{"dir"} and defined $_->{"file"}
!ll&&!rl&&r
000
$_->{"line"} // ""
l!l
00
MC/DC0%0/2
defined $_->{"dir"} and defined $_->{"file"}
defined $_->{"dir"} defined $_->{"file"}
MC/DC0%0/1
$_->{"line"} // ""
$_->{"line"}
Truth table0%0/3
A and B
A= defined $_->{"dir"}B= defined $_->{"file"}
ABresult
0X0
100
111
Truth table0%0/2
A
A= $_->{"line"}
Aresult
11
00
247 ],
248 ) :
249 '';
250 }
251  
252 0 ▶ sub parse_xml {
Subroutine parse_xml: not called CC=14 SCAR=53.5
Pod undocumented
253 0 my ($xml) = @_;
254 0 my $hash = {};
255 0 my $re = qr{<([^>]+)>\s*(.*?)\s*</\1>}sm;
256 0 ▶ while ( $xml =~ m/$re/g ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($xml =~ /$re/gu) 0 0 0
257 0 my ( $tag, $content ) = ( $1, $2 );
258 0 ▶ $content = parse_xml($content) if $content =~ /$re/;
Branch: 1 branch
BranchTrueFalseTotal
if $content =~ /$re/u 0 0 0
259 0 ▶ $content = dec_ent($content) unless ref $content;
Branch: 1 branch
BranchTrueFalseTotal
unless ref $content 0 0 0
260 0 ▶ if ( $tag eq 'error' ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($tag eq "error") 0 0 0
261  
262 # use Data::Dump;
263 # ddx $content;
264 0 ▶ diag $content->{what} // $content->{xwhat}{text};
Condition0%0/3
$content->{"what"} // $content->{"xwhat"}{"text"}
l!l&&r!l&&!r
000
MC/DC0%0/2
$content->{"what"} // $content->{"xwhat"}{"text"}
$content->{"what"} $content->{"xwhat"}{"text"}
Truth table0%0/3
A // B
A= $content->{"what"}B= $content->{"xwhat"}{"text"}
ABresult
1X1
011
000
265 0 ▶ if ( ref $content->{auxwhat} eq 'ARRAY' ) {
Branch: 1 branch
BranchTrueFalseTotal
if (ref $content->{'auxwhat'} eq 'ARRAY') { } 0 0 0
266 0 for my $i ( 0 .. scalar @{ $content->{stack} } ) {
267 0 ▶ note $content->{auxwhat}[$i] if $content->{auxwhat}[$i];
Branch: 1 branch
BranchTrueFalseTotal
if $content->{"auxwhat"}[$i] 0 0 0
268 0 note stacktrace $content->{stack}[$i]{frame};
269 }
270 }
271 else {
272 0 note $content->{auxwhat};
273 0 for my $i ( 0 .. scalar @{ $content->{stack} } ) {
274 0 note stacktrace $content->{stack}[$i]{frame};
275 }
276 }
277 }
278 $hash->{$tag}
279 = defined $content ?
280 (
281 defined $hash->{$tag} ?
282 ref $hash->{$tag} eq 'ARRAY' ?
283 0 ▶ [ @{ $hash->{$tag} }, $content ] :
Branch: 1 branch
BranchTrueFalseTotal
$tag =~ /^(error|stack)$/u ? : 0 0 0
284 0 ▶ [ $hash->{$tag}, $content ] :
Branch: 3 branches
BranchTrueFalseTotal
defined $content ? : 0 0 0
defined $hash->{$tag} ? : 0 0 0
ref $hash->{$tag} eq 'ARRAY' ? : 0 0 0
285 $tag =~ m/^(error|stack)$/ ? [$content] :
286 dec_ent($content) ) :
287 undef;
288 }
289 0 $hash;
290 }
291  
292 # Function to run anonymous sub in a new process with valgrind
293 0 ▶ sub leaks( $name, $code_ref ) {
Subroutine leaks: not called CC=6 SCAR=37.4
Pod undocumented
294 0 init_valgrind();
295 #
296 0 require B::Deparse;
297 0 ▶ CORE::state $deparse //= B::Deparse->new(qw[-l]);
Condition0%0/3
state $deparse //= "B::Deparse"->new("-l")
l!l&&r!l&&!r
000
MC/DC0%0/2
state $deparse //= "B::Deparse"->new("-l")
state $deparse "B::Deparse"->new("-l")
Truth table0%0/3
A //= B
A= state $deparseB= "B::Deparse"->new("-l")
ABresult
1X1
011
000
298 0 my ( $package, $file, $line ) = caller;
299 my $source = sprintf
300 0 ▶ <<'', ( join ', ', map {"'$_'"} sort { length $a <=> length $b } grep {defined} map { my $dir = path($_); $dir->exists ? $dir->absolute->realpath : () } @INC, 't/lib' ), Test2::API::test2_stack()->top->{count}, $deparse->coderef2text($code_ref);
Branch: 1 branch
BranchTrueFalseTotal
$dir->exists ? : 0 0 0
301 use lib %s;
302 use Test2::V0 -no_srand => 1, '!subtest';
303 use Test2::Util::Importer 'Test2::Tools::Subtest' => ( subtest_streamed => { -as => 'subtest' } );
304 use Test2::Plugin::UTF8;
305 no Test2::Plugin::ExitSummary; # I wish
306 use Test2::Tools::Affix;
307 # Test2::API::test2_stack()->top->{count} = %d;
308 $|++;
309 my $exit = sub {use Affix; Affix::set_destruct_level(3); %s;}->();
310 # Test2::API::test2_stack()->top->{count}++;
311 done_testing;
312 exit !$exit;
313  
314 0 my $report = Path::Tiny->tempfile( { realpath => 1 }, 'valgrind_report_XXXXXXXXXX' );
315 0 push @cleanup, $report;
316 0 my @cmd = (
317 'valgrind', '-q', '--suppressions=' . $supp->canonpath,
318 '--leak-check=full', '--show-leak-kinds=all', '--show-reachable=yes', '--demangle=yes', '--error-limit=no', '--xml=yes',
319 '--gen-suppressions=all', '--xml-file=' . $report->stringify,
320 $^X, '-e', $source
321 );
322 my ( $out, $err, $exit ) = Capture::Tiny::capture(
323 0 ▶ sub {
Subroutine __ANON__: not called CC=1 SCAR=6.9
324 0 system @cmd;
325 }
326 0 );
327  
328 # $out =~ s[# Seeded srand with seed .+$][]m;
329 # $err =~ s[# Tests were run .+$][];
330 0 ▶ if ( $out =~ m[\S] ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($out =~ /\S/u) 0 0 0
331 0 $out =~ s[^((?:[ \t]*))(?=\S)][$1 ]gm;
332 0 print $out;
333 }
334 0 ▶ if ( $err =~ m[\S] ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($err =~ /\S/u) 0 0 0
335 0 $err =~ s[^((?:[ \t]*))(?=\S)][$1 ]gm;
336 0 print STDERR $err;
337 }
338 0 my $parsed = parse_xml( $report->slurp_utf8 );
339  
340 # use Data::Dump;
341 # ddx $parsed;
342 # diag 'exit: '. $exit;
343 # Test2::API::test2_stack()->top->{count}++;
344 0 ▶ ok !$exit && !$parsed->{valgrindoutput}{errorcounts}, $name;
Condition0%0/3
$exit || $parsed->{'valgrindoutput'}{'errorcounts'}
l!l&&r!l&&!r
000
MC/DC0%0/2
$exit || $parsed->{'valgrindoutput'}{'errorcounts'}
$exit $parsed->{'valgrindoutput'}{'errorcounts'}
Truth table0%0/3
A || B
A= $exitB= $parsed->{'valgrindoutput'}{'errorcounts'}
ABresult
1X1
011
000
345 }
346 }
347  
348 54 ▶ END {
Subroutine END: called CC=3 SCAR=11.1
349 54 for my $file ( grep {-f} @cleanup ) {
350  
351 #~ note 'Removing ' . $file;
352 159 unlink $file;
353 }
354 54 for my $dir ( grep {-d} @cleanup ) {
355  
356 #~ note 'Removing ' . $dir;
357 0 $dir->remove_tree;
358 }
359 }
360 };
361 54 1;
« blib/lib/Affix/Build.pm ↑ Summary blib/lib/Affix.pm »