File Coverage

blib/lib/Test2/Plugin/NoWarnings.pm
Criterion Covered Total %
statement 19 19 100.0
branch 1 2 50.0
condition n/a
subroutine 6 6 100.0
pod n/a
total 26 27 96.3


line stmt bran cond sub pod time code
1             package Test2::Plugin::NoWarnings;
2              
3 2     2   397216 use strict;
  2         13  
  2         49  
4 2     2   9 use warnings;
  2         4  
  2         70  
5              
6             our $VERSION = '0.08';
7              
8 2     2   738 use Test2 1.302096;
  2         189  
  2         48  
9 2     2   11 use Test2::API qw( context_do );
  2         4  
  2         72  
10 2     2   672 use Test2::Event::Warning;
  2         4  
  2         478  
11              
12             my $echo = 0;
13              
14             sub import {
15 2     2   15 shift;
16 2         5 my %args = @_;
17 2 50       8 $echo = $args{echo} if exists $args{echo};
18 2         2039 return;
19             }
20              
21             my $_orig_warn_handler = $SIG{__WARN__};
22             ## no critic (Variables::RequireLocalizedPunctuationVars)
23             $SIG{__WARN__} = sub {
24             my $w = $_[0];
25             $w =~ s/\n+$//g;
26              
27             context_do {
28             my $ctx = shift;
29             $ctx->send_event(
30             'Warning',
31             warning => "Unexpected warning: $w",
32             );
33             }
34             $_[0];
35              
36             return unless $echo;
37              
38             return if $_orig_warn_handler && $_orig_warn_handler eq 'IGNORE';
39              
40             # The rest was copied from Test::Warnings
41              
42             # TODO: this doesn't handle blessed coderefs... does anyone care?
43             goto &$_orig_warn_handler
44             if $_orig_warn_handler
45             and (
46             ( ref $_orig_warn_handler eq 'CODE' )
47             or ( $_orig_warn_handler ne 'DEFAULT'
48             and $_orig_warn_handler ne 'IGNORE'
49             and defined &$_orig_warn_handler )
50             );
51              
52             if ( $_[0] =~ /\n$/ ) {
53             warn $_[0];
54             }
55             else {
56             require Carp;
57             Carp::carp( $_[0] );
58             }
59             };
60              
61             1;
62              
63             # ABSTRACT: Fail if tests warn
64              
65             __END__
66              
67             =pod
68              
69             =encoding UTF-8
70              
71             =head1 NAME
72              
73             Test2::Plugin::NoWarnings - Fail if tests warn
74              
75             =head1 VERSION
76              
77             version 0.08
78              
79             =head1 SYNOPSIS
80              
81             use Test2::V0;
82             use Test2::Plugin::NoWarnings;
83              
84             ...;
85              
86             =head1 DESCRIPTION
87              
88             Loading this plugin causes your tests to fail if there any warnings while they
89             run. Each warning generates a new failing test and the warning content is
90             outputted via C<diag>.
91              
92             This module uses C<$SIG{__WARN__}>, so if the code you're testing sets this,
93             then this module will stop working.
94              
95             =head1 ECHOING WARNINGS
96              
97             By default, this module suppresses the warning itself so it does not go to
98             C<STDERR>. If you'd like to also have the warning go to C<STDERR> untouched,
99             you can ask for this with the C<echo> import argument:
100              
101             use Test2::Plugin::NoWarnings echo => 1;
102              
103             =head1 SUPPORT
104              
105             Bugs may be submitted at L<https://github.com/houseabsolute/Test2-Plugin-NoWarnings/issues>.
106              
107             I am also usually active on IRC as 'autarch' on C<irc://irc.perl.org>.
108              
109             =head1 SOURCE
110              
111             The source code repository for Test2-Plugin-NoWarnings can be found at L<https://github.com/houseabsolute/Test2-Plugin-NoWarnings>.
112              
113             =head1 DONATIONS
114              
115             If you'd like to thank me for the work I've done on this module, please
116             consider making a "donation" to me via PayPal. I spend a lot of free time
117             creating free software, and would appreciate any support you'd care to offer.
118              
119             Please note that B<I am not suggesting that you must do this> in order for me
120             to continue working on this particular software. I will continue to do so,
121             inasmuch as I have in the past, for as long as it interests me.
122              
123             Similarly, a donation made in this way will probably not make me work on this
124             software much more, unless I get so many donations that I can consider working
125             on free software full time (let's all have a chuckle at that together).
126              
127             To donate, log into PayPal and send money to autarch@urth.org, or use the
128             button at L<http://www.urth.org/~autarch/fs-donation.html>.
129              
130             =head1 AUTHOR
131              
132             Dave Rolsky <autarch@urth.org>
133              
134             =head1 COPYRIGHT AND LICENSE
135              
136             This software is Copyright (c) 2019 by Dave Rolsky.
137              
138             This is free software, licensed under:
139              
140             The Artistic License 2.0 (GPL Compatible)
141              
142             The full text of the license can be found in the
143             F<LICENSE> file included with this distribution.
144              
145             =cut