| 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
| |||||||||||
| 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
| |||||||||||
| 26 | @ALLOW_FROM = | ||||||||||
| 27 | 1 | ▶ | ref $opts{'-allow_from'} ? @{ $opts{'-allow_from'} } : $opts{'-allow_from'}; | ||||||||
|
Branch: 1 branch
| |||||||||||
| 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
| |||||||||||
| 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
| |||||||||||
| 41 | 1 | ▶ | $filename = File::Spec->abs2rel( $filename, $ORIG_DIR ) | ||||||||
|
Branch: 1 branch
| |||||||||||
| 42 | if File::Spec->file_name_is_absolute($filename); | ||||||||||
| 43 | 1 | ▶ | return if $filename !~ /^(?:t|xt|lib|blib)/; | ||||||||
|
Branch: 1 branch
| |||||||||||
| 44 | } | ||||||||||
| 45 | |||||||||||
| 46 | 5 | ▶ | return if grep { $package eq $_ } @ALLOW_FROM; | ||||||||
|
Branch: 1 branch
| |||||||||||
| 47 | |||||||||||
| 48 | 4 | ▶ | if ( $msg !~ m/at .*? line \d/ ) { | ||||||||
|
Branch: 1 branch
| |||||||||||
| 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
| |||||||||||
| 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
| |||||||||||
| 65 | 15 | ▶ | next if $pkg =~ /^(?:Carp|warnings)/; | ||||||||
|
Branch: 1 branch
| |||||||||||
| 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__ | ||||||||||