blib/lib/Test/FailWarnings.pm

filter: Statementstmt 100% 54 / 54 Branchbran 70.8% 17 / 24 Conditioncond n/a MC/DCmcdc n/a Subroutinesub 100% 10 / 10 Podpod 0.0% 0 / 1 totaltotal 91.0% 81 / 89 CC 13 SCAR 14.9
CC
13
Cov
91%
handler
8.2
import
4.0
_find_source
3.0
CRAP 4.4
↑ Summary
1 7 ▶use 5.008001;
Subroutine BEGIN: called CC=1 SCAR=0.0
2 7 ▶use strict;
Subroutine BEGIN: called CC=1 SCAR=0.0
3 7 ▶use warnings;
Subroutine BEGIN: called CC=1 SCAR=0.0
4  
5 package Test::FailWarnings;
6 # ABSTRACT: Add test failures if warnings are caught
7 7 our $VERSION = '0.008'; # VERSION
8  
9 7 ▶use Test::More 0.86;
Subroutine BEGIN: called CC=1 SCAR=0.0
10 7 ▶use Cwd qw/getcwd/;
Subroutine BEGIN: called CC=1 SCAR=0.0
11 7 ▶use File::Spec;
Subroutine BEGIN: called CC=1 SCAR=0.0
12 7 ▶use Carp;
Subroutine BEGIN: called CC=1 SCAR=0.0
13  
14 7 our $ALLOW_DEPS = 0;
15 7 our @ALLOW_FROM = ();
16  
17 7 my $ORIG_DIR = getcwd(); # cache in case handler runs after a chdir
18  
19 6 ▶sub import {
Subroutine import: called CC=4 SCAR=14.0
20 6 my ( $class, @args ) = @_;
21 6 ▶ croak("import arguments must be key/value pairs")
Branch: 1 branch
BranchTrueFalseTotal
unless @args % 2 == 0 0 6 6
22 unless @args % 2 == 0;
23 6 my %opts = @args;
24 6 $ALLOW_DEPS = $opts{'-allow_deps'};
25 6 ▶ if ( $opts{'-allow_from'} ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($opts{"-allow_from"}) 1 5 6
26 @ALLOW_FROM =
27 1 ▶ ref $opts{'-allow_from'} ? @{ $opts{'-allow_from'} } : $opts{'-allow_from'};
Branch: 1 branch
BranchTrueFalseTotal
ref $opts{'-allow_from'} ? : 1 0 1
28 }
29 6 $SIG{__WARN__} = \&handler;
30 }
31  
32 6 ▶sub handler {
Subroutine handler: called CC=8 SCAR=21.0
Pod undocumented
33 6 my $msg = shift;
34 6 ▶ $msg = '' unless defined $msg;
Branch: 1 branch
BranchTrueFalseTotal
unless defined $msg 0 6 6
35 6 chomp $msg;
36 6 my ( $package, $filename, $line ) = _find_source();
37  
38 # shortcut if ignoring dependencies and warning did not
39 # come from something local
40 6 ▶ if ($ALLOW_DEPS) {
Branch: 1 branch
BranchTrueFalseTotal
if ($Test::FailWarnings::ALLOW_DEPS) 1 5 6
41 1 ▶ $filename = File::Spec->abs2rel( $filename, $ORIG_DIR )
Branch: 1 branch
BranchTrueFalseTotal
if "File::Spec"->file_name_is_absolute($filename) 1 0 1
42 if File::Spec->file_name_is_absolute($filename);
43 1 ▶ return if $filename !~ /^(?:t|xt|lib|blib)/;
Branch: 1 branch
BranchTrueFalseTotal
unless $filename =~ /^(?:t|xt|lib|blib)/ 1 0 1
44 }
45  
46 5 ▶ return if grep { $package eq $_ } @ALLOW_FROM;
Branch: 1 branch
BranchTrueFalseTotal
if grep {$package eq $_;} @Test::FailWarnings::ALLOW_FROM 1 4 5
47  
48 4 ▶ if ( $msg !~ m/at .*? line \d/ ) {
Branch: 1 branch
BranchTrueFalseTotal
if (not $msg =~ /at .*? line \d/) { } 1 3 4
49 1 chomp $msg;
50 1 $msg = "'$msg' at $filename line $line.";
51 }
52 else {
53 3 $msg = "'$msg'";
54 }
55 4 my $builder = Test::More->builder;
56 4 ▶ $builder->ok( 0, "Test::FailWarnings should catch no warnings" )
Branch: 1 branch
BranchTrueFalseTotal
$builder->ok(0, "Test::FailWarnings should catch no warnings") or $builder->diag("Warning was $msg") 0 4 4
57 or $builder->diag("Warning was $msg");
58 }
59  
60 6 ▶sub _find_source {
Subroutine _find_source: called CC=3 SCAR=11.0
61 6 my $i = 1;
62 6 while (1) {
63 15 my ( $pkg, $filename, $line ) = caller( $i++ );
64 15 ▶ return caller( $i - 2 ) unless defined $pkg;
Branch: 1 branch
BranchTrueFalseTotal
unless defined $pkg 0 15 15
65 15 ▶ next if $pkg =~ /^(?:Carp|warnings)/;
Branch: 1 branch
BranchTrueFalseTotal
if $pkg =~ /^(?:Carp|warnings)/ 9 6 15
66 6 return ( $pkg, $filename, $line );
67 }
68 }
69  
70 7 1;
71  
72  
73 # vim: ts=4 sts=4 sw=4 et:
74  
75 __END__
↑ Summary