blib/lib/Test/Perl/Critic.pm

filter: Statementstmt 52.5% 51 / 97 Branchbran 18.1% 4 / 22 Conditioncond 0.0% 0 / 5 MC/DCmcdc 0.0% 0 / 3 Subroutinesub 70.5% 12 / 17 Podpod 100% 2 / 2 totaltotal 47.2% 69 / 146 CC 15 SCAR 28.8
CC
15
Cov
44%
critic_ok
45.0
all_critic_ok
30.0
__ANON__
6.0
CRAP 17.8
↑ Summary
1 package Test::Perl::Critic;
2  
3 2 ▶use 5.006001;
Subroutine BEGIN: called CC=1 SCAR=0.0
4  
5 2 ▶use strict;
Subroutine BEGIN: called CC=1 SCAR=0.0
6 2 ▶use warnings;
Subroutine BEGIN: called CC=1 SCAR=0.0
7  
8 2 ▶use Carp qw(croak);
Subroutine BEGIN: called CC=1 SCAR=0.0
9 2 ▶use English qw(-no_match_vars);
Subroutine BEGIN: called CC=1 SCAR=0.0
10  
11 2 ▶use Test::Builder qw();
Subroutine BEGIN: called CC=1 SCAR=0.0
12 2 ▶use Perl::Critic qw();
Subroutine BEGIN: called CC=1 SCAR=0.0
13 2 ▶use Perl::Critic::Violation qw();
Subroutine BEGIN: called CC=1 SCAR=0.0
14 2 ▶use Perl::Critic::Utils;
Subroutine BEGIN: called CC=1 SCAR=0.0
15  
16 #---------------------------------------------------------------------------
17  
18 2 our $VERSION = '1.04';
19  
20 #---------------------------------------------------------------------------
21  
22 2 my $TEST = Test::Builder->new;
23 2 my $DIAG_INDENT = q{ };
24 2 my %CRITIC_ARGS = ();
25  
26 2 my $CRITIC_OBJ = undef;
27 0 ▶my $BUILD_CRITIC = sub {
Subroutine __ANON__: not called CC=2 SCAR=17.9
28 0 ▶ return $CRITIC_OBJ if defined $CRITIC_OBJ;
Branch: 1 branch
BranchTrueFalseTotal
if defined $CRITIC_OBJ 0 0 0
29 0 $CRITIC_OBJ = Perl::Critic->new( @_ );
30 2 };
31  
32 #---------------------------------------------------------------------------
33  
34 2 ▶sub import {
Subroutine import: called CC=2 SCAR=7.0
35  
36 2 my ( $self, %args ) = @_;
37 2 my $caller = caller;
38  
39 {
40 2 ▶ no strict 'refs'; ## no critic qw(ProhibitNoStrict)
Subroutine BEGIN: called CC=1 SCAR=0.0
41 2 *{ $caller . '::critic_ok' } = \&critic_ok;
42 2 *{ $caller . '::all_critic_ok' } = \&all_critic_ok;
43 }
44  
45 # -format is supported for backward compatibility.
46 0 ▶ if ( exists $args{-format} ) { $args{-verbose} = $args{-format}; }
Branch: 1 branch
BranchTrueFalseTotal
if (exists $args{"-format"}) 0 2 2
47 2 %CRITIC_ARGS = %args;
48  
49 # Reset possibly lazy-initialized Perl::Critic.
50 2 $CRITIC_OBJ = undef;
51  
52 2 $TEST->exported_to($caller);
53  
54 2 return 1;
55 }
56  
57 #---------------------------------------------------------------------------
58  
59 2 ▶sub critic_ok {
Subroutine critic_ok: called CC=8 SCAR=38.1
Pod documented
60  
61 2 my ( $file, $test_name ) = @_;
62 2 ▶ croak q{no file specified} if not defined $file;
Branch: 1 branch
BranchTrueFalseTotal
unless defined $file 1 1 2
63 1 ▶ croak qq{"$file" does not exist} if not -f $file;
Branch: 1 branch
BranchTrueFalseTotal
unless -f $file 1 0 1
64 0 ▶ $test_name ||= qq{Test::Perl::Critic for "$file"};
Condition0%0/2
$test_name ||= qq[Test::Perl::Critic for "$file"]
l!l
00
MC/DC0%0/1
$test_name ||= qq[Test::Perl::Critic for "$file"]
$test_name
Truth table0%0/2
A
A= $test_name
Aresult
11
00
65  
66 0 my $critic = undef;
67 0 my @violations = ();
68 0 my $ok = 0;
69  
70 # Run Perl::Critic
71 0 my $status = eval {
72 0 $critic = $BUILD_CRITIC->( %CRITIC_ARGS );
73 0 @violations = $critic->critique( $file );
74 0 $ok = not scalar @violations;
75 0 1;
76 };
77  
78 # Evaluate results
79 0 $TEST->ok($ok, $test_name );
80  
81 0 ▶ if (!$status || $EVAL_ERROR) { # Trap exceptions from P::C
Branch: 2 branches
BranchTrueFalseTotal
if (not $status or $Test::Perl::Critic::EVAL_ERROR) { } 0 0 0
elsif (not $ok) { } 0 0 0
Condition0%0/3
not $status or $Test::Perl::Critic::EVAL_ERROR
l!l&&r!l&&!r
000
MC/DC0%0/2
not $status or $Test::Perl::Critic::EVAL_ERROR
not $status $Test::Perl::Critic::EVAL_ERROR
Truth table0%0/3
not(A) or B
A= not $statusB= $Test::Perl::Critic::EVAL_ERROR
ABresult
1X1
011
000
82 0 $TEST->diag( "\n" ); # Just to get on a new line.
83 0 $TEST->diag( qq{Perl::Critic had errors in "$file":} );
84 0 $TEST->diag( qq{\t$EVAL_ERROR} );
85 }
86 elsif ( not $ok ) { # Report Policy violations
87 0 $TEST->diag( "\n" ); # Just to get on a new line.
88 0 my $verbose = $critic->config->verbose();
89 0 Perl::Critic::Violation::set_format( $verbose );
90 0 for my $viol (@violations) { $TEST->diag($DIAG_INDENT . $viol) }
91 }
92  
93 0 return $ok;
94 }
95  
96 #---------------------------------------------------------------------------
97  
98 0 ▶sub all_critic_ok {
Subroutine all_critic_ok: not called CC=5 SCAR=34.0
Pod documented
99  
100 0 ▶ my @dirs_or_files = @_ ? @_ : (-e 'blib' ? 'blib' : 'lib');
Branch: 2 branches
BranchTrueFalseTotal
@_ ? : 0 0 0
-e 'blib' ? : 0 0 0
101 0 my @files = Perl::Critic::Utils::all_perl_files(@dirs_or_files);
102 0 ▶ croak 'Nothing to critique' if not @files;
Branch: 1 branch
BranchTrueFalseTotal
unless @files 0 0 0
103  
104 0 my $have_mce = eval { require MCE::Grep; MCE::Grep->import; 1 };
105 0 ▶ return $have_mce ? _test_parallel(@files) : _test_serial(@files);
Branch: 1 branch
BranchTrueFalseTotal
$have_mce ? : 0 0 0
106 }
107  
108 #---------------------------------------------------------------------------
109  
110 0 ▶sub _test_parallel {
Subroutine _test_parallel: not called CC=2 SCAR=17.9
111 0 my @files = @_;
112  
113 # Since tests are running in forked MCE workers, test results could arrive
114 # in any order. The test numbers will be meaningless, so turn them off.
115 0 $TEST->use_numbers(0);
116  
117 # The parent won't know about any of the tests that were run by the forked
118 # workers. So we disable the T::B sanity checks at the end of its life.
119 0 $TEST->no_ending(1);
120  
121 0 ▶ my $okays = MCE::Grep->run( sub { critic_ok($_) }, @files );
Subroutine __ANON__: not called CC=1 SCAR=6.9
122 0 my $pass = $okays == @files;
123  
124 # To make Test::Harness happy, we must emit a test plan and a sensible exit
125 # status. Usually, T::B does this for us, but we disabled the ending above.
126 0 ▶ $pass || eval 'END { $? = 1 }'; ## no critic qw(Eval Interpolation)
Branch: 1 branch
BranchTrueFalseTotal
$pass or eval "END { \$? = 1 }" 0 0 0
127 0 $TEST->done_testing(scalar @files);
128  
129 0 return $pass;
130 }
131  
132 #---------------------------------------------------------------------------
133  
134 0 ▶sub _test_serial {
Subroutine _test_serial: not called CC=1 SCAR=6.9
135 0 my @files = @_;
136  
137 0 my $okays = grep {critic_ok($_)} @files;
138 0 my $pass = $okays == @files;
139  
140 0 $TEST->done_testing(scalar @files);
141  
142 0 return $pass;
143 }
144  
145 #---------------------------------------------------------------------------
146  
147 2 1;
148  
149  
150 __END__
↑ Summary