blib/lib/Git/Critic.pm

filter: Statementstmt 58.6% 85 / 145 Branchbran 12.5% 5 / 40 Conditioncond 33.3% 2 / 6 MC/DCmcdc 25.0% 1 / 4 Subroutinesub 85.7% 24 / 28 Podpod 100% 1 / 1 totaltotal 52.6% 118 / 224 CC 27 SCAR 44.7
CC
27
Cov
48%
run
270.9
_is_perl
12.4
_run
5.7
CRAP 87.6
↑ Summary
1 package Git::Critic;
2  
3 # ABSTRACT: Only run Perl::Critic on lines changed in the current branch
4 1 ▶use v5.10.0;
Subroutine BEGIN: called CC=1 SCAR=0.0
5 1 ▶use strict;
Subroutine BEGIN: called CC=1 SCAR=0.0
6 1 ▶use warnings;
Subroutine BEGIN: called CC=1 SCAR=0.0
7 # we don't use this directly, but it's an attempt to fix this issue:
8 # IPC::System::Simple required for Fatalised/autodying system() at .../lib/Git/Critic.pm line 7.
9 1 ▶use IPC::System::Simple;
Subroutine BEGIN: called CC=1 SCAR=0.0
10 1 ▶use autodie ":all";
Subroutine BEGIN: called CC=1 SCAR=0.0
11  
12 1 ▶use Capture::Tiny 'capture_stdout';
Subroutine BEGIN: called CC=1 SCAR=0.0
13 1 ▶use Carp;
Subroutine BEGIN: called CC=1 SCAR=0.0
14 1 ▶use File::Basename 'basename';
Subroutine BEGIN: called CC=1 SCAR=0.0
15 1 ▶use File::Temp 'tempfile';
Subroutine BEGIN: called CC=1 SCAR=0.0
16 1 ▶use List::Util 1.44 qw(uniq);
Subroutine BEGIN: called CC=1 SCAR=0.0
17 1 ▶use Moo;
Subroutine BEGIN: called CC=1 SCAR=0.0
18 1 ▶use Types::Standard qw( ArrayRef Bool Int Str);
Subroutine BEGIN: called CC=1 SCAR=0.0
19  
20 1 our $VERSION = '0.7';
21  
22 #
23 # Moo attributes
24 #
25  
26 1 has primary_target => (
27 is => 'ro',
28 isa => Str,
29 required => 1,
30 );
31  
32 1 has current_target => (
33 is => 'ro',
34 isa => Str,
35 lazy => 1,
36 builder => '_build_current_target',
37 );
38  
39 1 has max_file_size => (
40 is => 'ro',
41 isa => Int,
42 default => 0,
43 );
44  
45 1 has severity => (
46 is => 'ro',
47 isa => Int | Str,
48 default => 5,
49 );
50  
51 1 has profile => (
52 is => 'ro',
53 isa => Str,
54 default => '',
55 );
56  
57 1 has verbose => (
58 is => 'ro',
59 isa => Bool,
60 default => 0,
61 );
62  
63 # this is only for tests
64 has _run_test_queue => (
65 is => 'ro',
66 isa => ArrayRef,
67 1 ▶ default => sub { [] },
Subroutine __ANON__: called CC=1 SCAR=0.0
68 1 init_arg => undef,
69 );
70  
71 #
72 # Builders
73 #
74  
75 1 ▶sub _build_current_target {
Subroutine _build_current_target: called CC=1 SCAR=0.0
76 1 my $self = shift;
77 1 return $self->_run( 'git', 'rev-parse', '--abbrev-ref', 'HEAD' );
78 }
79  
80 #
81 # The following methods are for the tests
82 #
83  
84 # return true if we have any data in our test queue
85 3 ▶sub _run_queue_active {
Subroutine _run_queue_active: called CC=1 SCAR=0.0
86 3 my $self = shift;
87 3 return scalar @{ $self->_run_test_queue };
88 }
89  
90 3 ▶sub _add_to_run_queue {
Subroutine _add_to_run_queue: called CC=1 SCAR=0.0
91 3 my ( $self, $result ) = @_;
92 3 push @{ $self->_run_test_queue } => $result;
93 }
94  
95 3 ▶sub _get_next_run_queue_response {
Subroutine _get_next_run_queue_response: called CC=1 SCAR=0.0
96 3 my $self = shift;
97 3 shift @{ $self->_run_test_queue };
98 }
99  
100 #
101 # These call system commands
102 #
103  
104 # if we have a response added to the run queue via _add_to_run_queue, return
105 # that instead of calling the system command. Let it die if the system command
106 # fails
107  
108 3 ▶sub _run {
Subroutine _run: called CC=3 SCAR=17.3
109 3 my ( $self, @command ) = @_;
110 3 ▶ if ( $self->_run_queue_active ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($self->_run_queue_active) 3 0 3
111 3 return $self->_get_next_run_queue_response;
112 }
113  
114 0 ▶ if ( $self->verbose ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($self->verbose) 0 0 0
115 0 say STDERR "Running command: @command";
116 }
117  
118 # XXX yeah, this needs to be more robust
119 0 ▶ chomp( my $result = capture_stdout { system(@command) } );
Subroutine __ANON__: not called CC=1 SCAR=6.9
120 0 return $result;
121 }
122  
123 # same as _run, but don't let it die
124 0 ▶sub _run_without_die {
Subroutine _run_without_die: not called CC=2 SCAR=12.6
125 0 my ( $self, @command ) = @_;
126 0 ▶ if ( $self->verbose ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($self->verbose) 0 0 0
127 0 say STDERR "Running command: @command";
128 }
129 chomp(
130 0 ▶ my $result = capture_stdout {
Subroutine __ANON__: not called CC=1 SCAR=0.2
131 1 ▶ no autodie;
Subroutine BEGIN: called CC=1 SCAR=0.0
132 0 system(@command);
133 }
134 0 );
135 0 return $result;
136 }
137  
138 # get Perl files which have been changed in the current branch
139 1 ▶sub _get_modified_perl_files {
Subroutine _get_modified_perl_files: called CC=2 SCAR=7.0
140 1 my $self = shift;
141 1 my $primary_target = $self->primary_target;
142 1 my $current_target = $self->current_target;
143 1 ▶ my @files = uniq sort grep { /\S/ && $self->_is_perl($_) }
Condition66%2/3
/\S/ && $self->_is_perl($_)
!ll&&!rl&&r
012
MC/DC50%1/2
/\S/ && $self->_is_perl($_)
/\S/ $self->_is_perl($_)
Truth table66%2/3
A && B
A= /\S/B= $self->_is_perl($_)
ABresult
0X0
100
111
144 split /\n/ => $self->_run( 'git', 'diff', '--name-only',
145 "$primary_target...$current_target" );
146 1 return @files;
147 }
148  
149 # get the diff of the current file
150 1 ▶sub _get_diff {
Subroutine _get_diff: called CC=1 SCAR=0.0
151 1 my ( $self, $file ) = @_;
152 1 my $primary_target = $self->primary_target;
153 1 my $current_target = $self->current_target;
154 1 my @diff =
155 split /\n/ =>
156 $self->_run( 'git', 'diff', "$primary_target...$current_target", $file );
157 1 return @diff;
158 }
159  
160 # remove undefined arguments. This makes a command line
161 # script easier to follow
162 1 ▶around BUILDARGS => sub {
Subroutine __ANON__: called CC=3 SCAR=11.4
163 1 my ( $orig, $class, @args ) = @_;
164  
165 1 my $arg_for = $class->$orig(@args);
166 1 foreach my $arg ( keys %$arg_for ) {
167 1 ▶ if ( not defined $arg_for->{$arg} ) {
Branch: 1 branch
BranchTrueFalseTotal
unless (defined $arg_for->{$arg}) 0 1 1
168 0 delete $arg_for->{$arg};
169 }
170 }
171 1 return $arg_for;
172 1 };
173  
174 0 ▶sub run {
Subroutine run: not called CC=17 SCAR=56.0
Pod documented
175 0 my $self = shift;
176  
177 0 my $primary_target = $self->primary_target;
178 0 my $current_target = $self->current_target;
179 0 ▶ if ( $primary_target eq $current_target ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($primary_target eq $current_target) 0 0 0
180  
181 # in the future, we might want to allow you to check the primary
182 # branch X commits back
183 0 return;
184 }
185  
186 # We walking through every file you've changed and parse the diff to
187 # figure out the start and end of every change you've made. Any perlcritic
188 # failures which are *not* on those lines are ignored
189 0 my @files = $self->_get_modified_perl_files;
190 0 my @failures;
191 0 FILE: foreach my $file (@files) {
192 0 my %reported;
193  
194 0 ▶ my $file_text = $self->_run( 'git', 'show', "${current_target}:$file" )
Branch: 1 branch
BranchTrueFalseTotal
my $file_text = $self->_run("git", "show", "${current_target}:$file") or next FILE 0 0 0
195 or next FILE;
196 0 ▶ if ( $self->max_file_size ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($self->max_file_size) 0 0 0
197 # we want the length in bytes, not characters
198 1 ▶ use bytes;
Subroutine BEGIN: called CC=1 SCAR=0.0
199 next FILE
200 0 ▶ unless length($file_text) <= $self->max_file_size; # large files are very slow
Branch: 1 branch
BranchTrueFalseTotal
unless length $file_text <= $self->max_file_size 0 0 0
201 }
202  
203 0 my ($fh, $filename) = tempfile();
204 0 print $fh $file_text;
205 0 close $fh;
206 0 my $severity = $self->severity;
207 0 my $profile = $self->profile;
208 0 my @arguments = ("--severity=$severity");
209 0 ▶ push @arguments, "--profile=$profile" if $profile;
Branch: 1 branch
BranchTrueFalseTotal
if $profile 0 0 0
210 0 push @arguments, $filename;
211 0 my $critique =
212 $self->_run_without_die( 'perlcritic', @arguments );
213 0 ▶ next FILE unless $critique; # should never happen unless perlcritic dies
Branch: 1 branch
BranchTrueFalseTotal
unless $critique 0 0 0
214 0 my @critiques = split /\n/, $critique;
215  
216 # unified diff format
217 # @@ -3,8 +3,9 @@
218 # @@ from-file-line-numbers to-file-line-numbers @@
219 my @chunks = map {
220 0 /^ \@\@\s+ -\d+,\d+\s+
221 \+(?<start>\d+)
222 ,(?<lines>\d+)
223 \s+\@\@/xs
224 0 ▶ ? [ $+{start}, $+{start} + $+{lines} ]
Branch: 1 branch
BranchTrueFalseTotal
/^ \@\@\s+ -\d+,\d+\s+ \+(?<start>\d+) ,(?<lines>\d+) \s+\@\@/sx ? : 0 0 0
225 : ()
226 } $self->_get_diff($file);
227 0 my $max_line_number = $chunks[-1][-1];
228 0 CRITIQUE: foreach my $this_critique (@critiques) {
229 0 ▶ next CRITIQUE if $this_critique =~ / source OK$/;
Branch: 1 branch
BranchTrueFalseTotal
if $this_critique =~ / source OK$/ 0 0 0
230 0 $this_critique =~ /\bline\s+(?<line_number>\d+)/;
231 0 ▶ unless ( defined $+{line_number} ) {
Branch: 1 branch
BranchTrueFalseTotal
unless (defined $+{"line_number"}) 0 0 0
232 0 warn "Could not find line number in critique $this_critique";
233 0 next;
234 }
235  
236 # no need to keep processing
237 0 ▶ last CRITIQUE if $+{line_number} > $max_line_number;
Branch: 1 branch
BranchTrueFalseTotal
if $+{"line_number"} > $max_line_number 0 0 0
238  
239 0 foreach my $chunk (@chunks) {
240 0 my ( $min, $max ) = @$chunk;
241 0 ▶ if ( $+{line_number} >= $min && $+{line_number} <= $max ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($+{"line_number"} >= $min and $+{"line_number"} <= $max) 0 0 0
Condition0%0/3
$+{"line_number"} >= $min and $+{"line_number"} <= $max
!ll&&!rl&&r
000
MC/DC0%0/2
$+{"line_number"} >= $min and $+{"line_number"} <= $max
$+{"line_number"} >= $min $+{"line_number"} <= $max
Truth table0%0/3
A and B
A= $+{"line_number"} >= $minB= $+{"line_number"} <= $max
ABresult
0X0
100
111
242 0 ▶ unless ($reported{$this_critique}) {
Branch: 1 branch
BranchTrueFalseTotal
unless ($reported{$this_critique}) 0 0 0
243 0 push @failures => "$file: $this_critique"
244 }
245 0 $reported{$this_critique}++;
246 0 next CRITIQUE;
247 }
248 }
249 }
250 }
251 0 return @failures;
252 }
253  
254 # a heuristic to determine if the file in question is Perl. We might allow
255 # a client to override this in the future
256 3 ▶sub _is_perl {
Subroutine _is_perl: called CC=5 SCAR=25.2
257 3 my ( $self, $file ) = @_;
258 3 ▶ return unless -e $file; # sometimes we get non-existent files
Branch: 1 branch
BranchTrueFalseTotal
unless -e $file 1 2 3
259 2 ▶ return 1 if $file =~ /\.(?:p[ml]|t)$/;
Branch: 1 branch
BranchTrueFalseTotal
if $file =~ /\.(?:p[ml]|t)$/ 2 0 2
260  
261 # if we got to here, let's check to see if "perl" is in a shebang
262 0 open my $fh, '<', $file;
263 0 my $first_line = <$fh>;
264 0 close $fh;
265 0 ▶ if ( $first_line =~ /^#!.*\bperl\b/ ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($first_line =~ /^#!.*\bperl\b/) 0 0 0
266 0 ▶ say STDERR "Found changed Perl file: $file" if $self->verbose;
Branch: 1 branch
BranchTrueFalseTotal
if $self->verbose 0 0 0
267 0 return $file;
268 }
269 0 return;
270 }
271  
272 # vim: filetype=perl
273  
274 1 1;
275  
276 __END__
↑ Summary