Conformance/Check/CriticProfile.pm

filter: Statementstmt 12.2% 21 / 172 Branchbran 0.0% 0 / 66 Conditioncond 0.0% 0 / 55 MC/DCmcdc 0.0% 0 / 30 Subroutinesub 31.8% 7 / 22 Podpod 100% 14 / 14 totaltotal 11.7% 42 / 359 CC 65 SCAR 47.0
CC
65
Cov
7%
expected
240.0
differences
182.0
adopt
182.0
CRAP 110.4
« Conformance/Check/SystemPackages.pm ↑ Summary Conformance/Schema.pm »
1 package Dist::Conformance::Check::CriticProfile 0.008;
2  
3 # ABSTRACT: The perlcritic profile is its template, with the overrides of the repository
4  
5 1 ▶use 5.040;
Subroutine BEGIN: called CC=1 SCAR=0.0
6 1 ▶use warnings FATAL => 'all';
Subroutine BEGIN: called CC=1 SCAR=0.0
7 1 ▶use re '/aasx';
Subroutine BEGIN: called CC=1 SCAR=0.0
8 1 ▶no autovivification;
Subroutine BEGIN: called CC=1 SCAR=0.0
9 1 ▶use Mojo::Base 'Dist::Conformance::Check', -signatures;
Subroutine BEGIN: called CC=1 SCAR=0.0
10  
11 1 ▶use Config::Tiny();
Subroutine BEGIN: called CC=1 SCAR=0.0
12  
13 1 ▶use Dist::Conformance::Schema();
Subroutine BEGIN: called CC=1 SCAR=0.0
14  
15  
16 1 our $VALUE = { anyOf => [ { type => 'string', nullable => 1 }, { type => 'number' } ] };
17  
18 0 ▶sub config_schema ($) {
Subroutine config_schema: not called CC=1 SCAR=6.9
Pod documented
19 return {
20 0 required => [qw{file template}],
21 properties => {
22 file => $Dist::Conformance::Schema::PATH,
23 template => $Dist::Conformance::Schema::PATH,
24 overrides => {
25 type => 'object',
26 additionalProperties => { anyOf => [ { type => 'string', enum => ['off'] }, { type => 'object', additionalProperties => $VALUE } ] },
27 default => {},
28 },
29 settings => { type => 'object', additionalProperties => $VALUE, default => {} },
30 replace => { type => 'boolean' },
31 retired => { type => 'array', items => { type => 'string' } },
32 header => { type => 'string' },
33 outdated => { type => 'array', items => { type => 'string' } },
34 },
35 };
36 }
37  
38  
39 0 ▶sub policy_of ($section) {
Subroutine policy_of: not called CC=1 SCAR=6.9
Pod documented
40 0 my ( $off, $name ) = $section =~ m/\A(-?)(?:Perl::Critic::Policy::)?(.+)\z/;
41 0 return ( !!$off, $name );
42 }
43  
44  
45 0 ▶sub profile_of ( $text, $where ) {
Subroutine profile_of: not called CC=5 SCAR=34.0
Pod documented
46 0 ▶ my $tiny = Config::Tiny->read_string($text) // die( "$where does not parse as a perlcritic profile: " . Config::Tiny->errstr . "\n" );
Condition0%0/2
"Config::Tiny"->read_string($text) // die("$where does not parse as a perlcritic profile: " . "Config::Tiny"->errstr . "\n")
l!l
00
MC/DC0%0/1
"Config::Tiny"->read_string($text) // die("$where does not parse as a perlcritic profile: " . "Config::Tiny"->errstr . "\n")
"Config::Tiny"->read_string($text)
Truth table0%0/2
A
A= "Config::Tiny"->read_string($text)
Aresult
11
00
47 0 ▶ my %profile = ( settings => $tiny->{_} // {}, policies => {} );
Condition0%0/2
$$tiny{"_"} // {}
l!l
00
MC/DC0%0/1
$$tiny{"_"} // {}
$$tiny{"_"}
Truth table0%0/2
A
A= $$tiny{"_"}
Aresult
11
00
48 0 for my $section ( grep { $_ ne q{_} } keys %$tiny ) {
49 0 my ( $off, $name ) = policy_of($section);
50 0 ▶ $profile{policies}{$name} = { off => $off, parameters => $off ? {} : $tiny->{$section} };
Branch: 1 branch
BranchTrueFalseTotal
$off ? : 0 0 0
51 }
52 0 return \%profile;
53 }
54  
55  
56 0 ▶sub header_of ($line) {
Subroutine header_of: not called CC=1 SCAR=6.9
Pod documented
57 0 my ($section) = $line =~ s/\s;\s.+$//r =~ m/\A\s*\[\s*(.+?)\s*\]\s*$/;
58 0 return $section;
59 }
60  
61  
62 0 ▶sub parameter_of ($line) {
Subroutine parameter_of: not called CC=3 SCAR=24.8
Pod documented
63 0 ▶ return if $line =~ m/\A\s*(?:[\#;]|$)/;
Branch: 1 branch
BranchTrueFalseTotal
if $line =~ /\A\s*(?:[\#;]|$)/aasx 0 0 0
64 0 my ( $key, $value ) = $line =~ m/\A\s*([^=]+?)\s*=\s*(.*?)\s*$/;
65 0 ▶ return if !defined $key;
Branch: 1 branch
BranchTrueFalseTotal
unless defined $key 0 0 0
66 0 return ( $key, $value );
67 }
68  
69  
70 0 ▶sub rewrite ( $lines, $overlay ) {
Subroutine rewrite: not called CC=8 SCAR=42.8
Pod documented
71 0 my ( @out, %seen, $after );
72 0 for my $line (@$lines) {
73 0 my ($key) = parameter_of($line);
74 0 my $kept = $line;
75 0 ▶ if ( defined $key && exists $overlay->{$key} ) {
Branch: 1 branch
BranchTrueFalseTotal
if (defined $key and exists $$overlay{$key}) 0 0 0
Condition0%0/3
defined $key and exists $$overlay{$key}
!ll&&!rl&&r
000
MC/DC0%0/2
defined $key and exists $$overlay{$key}
defined $key exists $$overlay{$key}
Truth table0%0/3
A and B
A= defined $keyB= exists $$overlay{$key}
ABresult
0X0
100
111
76 0 $seen{$key} = 1;
77 0 ▶ next if !defined $overlay->{$key};
Branch: 1 branch
BranchTrueFalseTotal
unless defined $$overlay{$key} 0 0 0
78 0 $kept = "$key = $overlay->{$key}\n";
79 }
80 0 push @out, $kept;
81 0 ▶ $after = scalar @out if defined $key;
Branch: 1 branch
BranchTrueFalseTotal
if defined $key 0 0 0
82 }
83 0 ▶ my @new = map { "$_ = $overlay->{$_}\n" } grep { !$seen{$_} && defined $overlay->{$_} } sort keys %$overlay;
Condition0%0/3
!$seen{$_} && defined $$overlay{$_}
!ll&&!rl&&r
000
MC/DC0%0/2
!$seen{$_} && defined $$overlay{$_}
!$seen{$_} defined $$overlay{$_}
Truth table0%0/3
not(A) && B
A= !$seen{$_}B= defined $$overlay{$_}
ABresult
0X0
100
111
84 0 ▶ splice @out, $after // 0, 0, @new;
Condition0%0/2
$after // 0
l!l
00
MC/DC0%0/1
$after // 0
$after
Truth table0%0/2
A
A= $after
Aresult
11
00
85 0 return @out;
86 }
87  
88  
89 0 ▶sub overrides ($self) {
Subroutine overrides: not called CC=5 SCAR=34.0
Pod documented
90 0 my %overrides;
91 0 for my $given ( sort keys %{ $self->config->{overrides} } ) {
92 0 my ( $off, $name ) = policy_of($given);
93 0 ▶ die "overrides names $given, which is not the name of a policy\n" if $off || !Dist::Conformance::Schema::matches( $Dist::Conformance::Check::MODULE, $name );
Branch: 1 branch
BranchTrueFalseTotal
if $off or not Dist::Conformance::Schema::matches($Dist::Conformance::Check::MODULE, $name) 0 0 0
Condition0%0/3
$off or not Dist::Conformance::Schema::matches($Dist::Conformance::Check::MODULE, $name)
l!l&&r!l&&!r
000
MC/DC0%0/2
$off or not Dist::Conformance::Schema::matches($Dist::Conformance::Check::MODULE, $name)
$off not Dist::Conformance::Schema::matches($Dist::Conformance::Check::MODULE, $name)
Truth table0%0/3
A or not(B)
A= $offB= not Dist::Conformance::Schema::matches($Dist::Conformance::Check::MODULE, $name)
ABresult
1X1
011
000
94 0 ▶ die "overrides names the policy $name twice\n" if exists $overrides{$name};
Branch: 1 branch
BranchTrueFalseTotal
if exists $overrides{$name} 0 0 0
95 0 $overrides{$name} = $self->config->{overrides}{$given};
96 }
97 0 return \%overrides;
98 }
99  
100  
101 0 ▶sub expected ($self) {
Subroutine expected: not called CC=15 SCAR=54.8
Pod documented
102 0 my $template = $self->config->{template};
103 0 my ( $text, @unfilled ) = $self->template($template);
104 0 ▶ die( "$template needs the variables " . join( ', ', @unfilled ) . " in .conformance.yml: nothing sets them\n" ) if @unfilled;
Branch: 1 branch
BranchTrueFalseTotal
if @unfilled 0 0 0
105 0 my $standard = profile_of( $text, $template )->{policies};
106 0 my $given = $self->overrides;
107 0 my %overrides;
108 0 for my $as ( sort keys %$given ) {
109 0 my $name = full_name( $as, $standard );
110 0 ▶ die "overrides names the policy $name twice\n" if exists $overrides{$name};
Branch: 1 branch
BranchTrueFalseTotal
if exists $overrides{$name} 0 0 0
111 0 $overrides{$name} = $given->{$as};
112 }
113 0 my $overrides = \%overrides;
114  
115 0 ▶ $text .= "\n" if $text =~ m/[^\n]\z/;
Branch: 1 branch
BranchTrueFalseTotal
if $text =~ /[^\n]\z/aasx 0 0 0
116 0 my @lines = split /^/, $text;
117 0 my @headers = grep { defined header_of( $lines[$_] ) } 0 .. $#lines;
118 0 ▶ my @bodies = ( [ 0, $headers[0] // scalar @lines ] );
Condition0%0/2
$headers[0] // scalar @lines
l!l
00
MC/DC0%0/1
$headers[0] // scalar @lines
$headers[0]
Truth table0%0/2
A
A= $headers[0]
Aresult
11
00
119 0 ▶ push @bodies, map { [ $headers[$_] + 1, $headers[ $_ + 1 ] // scalar @lines ] } 0 .. $#headers;
Condition0%0/2
$headers[$_ + 1] // scalar @lines
l!l
00
MC/DC0%0/1
$headers[$_ + 1] // scalar @lines
$headers[$_ + 1]
Truth table0%0/2
A
A= $headers[$_ + 1]
Aresult
11
00
120  
121 0 my @out = rewrite( [ @lines[ $bodies[0][0] .. $bodies[0][1] - 1 ] ], $self->config->{settings} );
122 0 my %named;
123 0 for my $i ( 0 .. $#headers ) {
124 0 my $name = ( policy_of( header_of( $lines[ $headers[$i] ] ) ) )[1];
125 0 my @body = @lines[ $bodies[ $i + 1 ][0] .. $bodies[ $i + 1 ][1] - 1 ];
126 0 my $want = $overrides->{$name};
127 0 $named{$name} = 1;
128 0 ▶ if ( !defined $want ) {
Branch: 2 branches
BranchTrueFalseTotal
if (not defined $want) { } 0 0 0
elsif ($want eq 'off') { } 0 0 0
129 0 push @out, $lines[ $headers[$i] ], @body;
130 }
131 elsif ( $want eq 'off' ) {
132 0 my %gone;
133 0 for my $line (@body) {
134 0 my ($key) = parameter_of($line);
135 0 ▶ $gone{$key} = undef if defined $key;
Branch: 1 branch
BranchTrueFalseTotal
if defined $key 0 0 0
136 }
137 0 push @out, "[-$name]\n", rewrite( \@body, \%gone );
138 }
139 else {
140 0 push @out, "[$name]\n", rewrite( \@body, $want );
141 }
142 }
143  
144 0 my @own = grep { !$named{$_} } sort keys %$overrides;
145 0 ▶ push @out, "\n# The overrides of .conformance.yml\n" if @own;
Branch: 1 branch
BranchTrueFalseTotal
if @own 0 0 0
146 0 for my $name (@own) {
147 0 my $want = $overrides->{$name};
148 0 ▶ push @out, ref $want ? ( "[$name]\n", rewrite( [], $want ) ) : "[-$name]\n";
Branch: 1 branch
BranchTrueFalseTotal
ref $want ? : 0 0 0
149 }
150 0 return join q{}, @out;
151 }
152  
153  
154 0 ▶sub differences ( $have, $want ) {
Subroutine differences: not called CC=13 SCAR=52.0
Pod documented
155 0 my ( $hp, $wp ) = ( $have->{policies}, $want->{policies} );
156 0 ▶ my $same = sub ( $x, $y ) {
Subroutine __ANON__: not called CC=1 SCAR=6.9
157 0 return join( "\n", map { "$_=$x->{$_}" } sort keys %$x ) eq join( "\n", map { "$_=$y->{$_}" } sort keys %$y );
158 0 };
159 my @kinds = (
160 0 [ 'lacks the policy', grep { !$hp->{$_} } sort keys %$wp ],
161 0 [ 'names the policy', grep { !$wp->{$_} } sort keys %$hp ],
162 0 ▶ [ 'enables the policy', grep { $hp->{$_} && $wp->{$_}{off} && !$hp->{$_}{off} } sort keys %$wp ],
Condition0%0/6
$$hp{$_} && $$wp{$_}{'off'} && !$$hp{$_}{'off'}
!ll&&!rl&&r
000
$$hp{$_} and $$wp{$_}{"off"}
!ll&&!rl&&r
000
MC/DC0%0/3
$$hp{$_} && $$wp{$_}{'off'} && !$$hp{$_}{'off'}
$$hp{$_} $$wp{$_}{"off"} !$$hp{$_}{'off'}
Truth table0%0/4
A and B && not(C)
A= $$hp{$_}B= $$wp{$_}{"off"}C= !$$hp{$_}{'off'}
ABCresult
0XX0
10X0
1100
1111
163 0 ▶ [ 'turns off the policy', grep { $hp->{$_} && !$wp->{$_}{off} && $hp->{$_}{off} } sort keys %$wp ],
Condition0%0/6
$$hp{$_} && !$$wp{$_}{'off'} && $$hp{$_}{'off'}
!ll&&!rl&&r
000
$$hp{$_} and not $$wp{$_}{"off"}
!ll&&!rl&&r
000
MC/DC0%0/3
$$hp{$_} && !$$wp{$_}{'off'} && $$hp{$_}{'off'}
$$hp{$_} not $$wp{$_}{"off"} $$hp{$_}{'off'}
Truth table0%0/4
A and not(B) && C
A= $$hp{$_}B= not $$wp{$_}{"off"}C= $$hp{$_}{'off'}
ABCresult
0XX0
10X0
1100
1111
164 0 ▶ [ 'sets other parameters for the policy', grep { local ($_) = $_; $hp->{$_} && !$wp->{$_}{off} && !$hp->{$_}{off} && !$same->( $hp->{$_}{parameters}, $wp->{$_}{parameters} ) } sort keys %$wp ],
Condition0%0/9
$$hp{$_} && !$$wp{$_}{'off'} && !$$hp{$_}{'off'} && !&$same($$hp{$_}{'parameters'}, $$wp{$_}{'parameters'})
!ll&&!rl&&r
000
$$hp{$_} and not $$wp{$_}{"off"} and not $$hp{$_}{"off"}
!ll&&!rl&&r
000
$$hp{$_} and not $$wp{$_}{"off"}
!ll&&!rl&&r
000
MC/DC0%0/4
$$hp{$_} && !$$wp{$_}{'off'} && !$$hp{$_}{'off'} && !&$same($$hp{$_}{'parameters'}, $$wp{$_}{'parameters'})
$$hp{$_} not $$wp{$_}{"off"} not $$hp{$_}{"off"} !&$same($$hp{$_}{'parameters'}, $$wp{$_}{'parameters'})
Truth table0%0/5
A and not(B) and not(C) && not(D)
A= $$hp{$_}B= not $$wp{$_}{"off"}C= not $$hp{$_}{"off"}D= !&$same($$hp{$_}{'parameters'}, $$wp{$_}{'parameters'})
ABCDresult
0XXX0
10XX0
110X0
11100
11111
165 );
166 0 my @differences;
167 0 for my $kind (@kinds) {
168 0 my ( $phrase, @names ) = @$kind;
169 0 ▶ next if !@names;
Branch: 1 branch
BranchTrueFalseTotal
unless @names 0 0 0
170 0 ▶ my $text = ( @names > 1 ? $phrase =~ s/policy\z/policies/r : $phrase ) . ' ' . join ', ', @names;
Branch: 1 branch
BranchTrueFalseTotal
@names > 1 ? : 0 0 0
171 0 ▶ $text .= ', which neither the template nor the overrides name' if $phrase eq 'names the policy';
Branch: 1 branch
BranchTrueFalseTotal
if $phrase eq "names the policy" 0 0 0
172 0 push @differences, $text;
173 }
174 0 ▶ push @differences, 'sets other global settings' if !$same->( $have->{settings}, $want->{settings} );
Branch: 1 branch
BranchTrueFalseTotal
unless &$same($$have{"settings"}, $$want{"settings"}) 0 0 0
175 0 return @differences;
176 }
177  
178  
179 0 ▶sub check ( $self, $snapshot ) {
Subroutine check: not called CC=5 SCAR=34.0
Pod documented
180 0 my ( $file, $template ) = @{ $self->config }{qw{file template}};
181 0 ▶ my $text = $snapshot->content($file) // return ( 'fail', "$file is missing" );
Condition0%0/2
$snapshot->content($file) // (return "fail", "$file is missing")
l!l
00
MC/DC0%0/1
$snapshot->content($file) // (return "fail", "$file is missing")
$snapshot->content($file)
Truth table0%0/2
A
A= $snapshot->content($file)
Aresult
11
00
182 0 my $want = $self->expected;
183 0 ▶ return ( 'pass', "is $template with the overrides" ) if $text eq $want;
Branch: 1 branch
BranchTrueFalseTotal
if $text eq $want 0 0 0
184 0 ▶ my $have = eval { profile_of( $text, $file ) } // return ( 'fail', Dist::Conformance::Schema::first_line($@) . ', and conformance update replaces it' );
Condition0%0/2
eval { do { profile_of($text, $file) } } // (return "fail", Dist::Conformance::Schema::first_line($@) . ', and conformance update replaces it')
l!l
00
MC/DC0%0/1
eval { do { profile_of($text, $file) } } // (return "fail", Dist::Conformance::Schema::first_line($@) . ', and conformance update replaces it')
eval { do { profile_of($text, $file) } }
Truth table0%0/2
A
A= eval { do { profile_of($text, $file) } }
Aresult
11
00
185 0 my @differences = differences( $have, profile_of( $want, $template ) );
186 0 ▶ return ( 'info', "differs from $template with the overrides only in comments and layout, which conformance update rewrites" ) if !@differences;
Branch: 1 branch
BranchTrueFalseTotal
unless @differences 0 0 0
187 0 return ( 'fail', join( '; ', @differences ) . ', which conformance update corrects' );
188 }
189  
190  
191 0 ▶sub update ( $self, $update ) {
Subroutine update: not called CC=2 SCAR=17.9
Pod documented
192 0 my $want = eval { $self->expected };
193 0 ▶ return $update->by_hand( Dist::Conformance::Schema::first_line($@) . ': ' . $self->name . ' cannot write ' . $self->config->{file} ) if !defined $want;
Branch: 1 branch
BranchTrueFalseTotal
unless defined $want 0 0 0
194 0 $update->write( $self->config->{file}, $want );
195 0 return;
196 }
197  
198  
199 0 ▶sub adopt ( $self, $snapshot ) {
Subroutine adopt: not called CC=13 SCAR=52.0
Pod documented
200 0 my ( $file, $template ) = @{ $self->config }{qw{file template}};
201 0 ▶ my $text = $snapshot->content($file) // return;
Condition0%0/2
$snapshot->content($file) // (return)
l!l
00
MC/DC0%0/1
$snapshot->content($file) // (return)
$snapshot->content($file)
Truth table0%0/2
A
A= $snapshot->content($file)
Aresult
11
00
202 0 my ( $filled, @unfilled ) = $self->template($template);
203 0 ▶ die( "$template needs the variables " . join( ', ', @unfilled ) . " in .conformance.yml: nothing sets them\n" ) if @unfilled;
Branch: 1 branch
BranchTrueFalseTotal
if @unfilled 0 0 0
204 0 my $have = profile_of( $text, $file );
205 0 my $want = profile_of( $filled, $template );
206  
207 0 my %overrides;
208 0 for my $given ( sort keys %{ $have->{policies} } ) {
209 0 my $mine = $have->{policies}{$given};
210 0 my $name = full_name( $given, $want->{policies} );
211 0 my $standard = $want->{policies}{$name};
212 0 ▶ if ( $mine->{off} ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($$mine{"off"}) 0 0 0
213 0 ▶ $overrides{$name} = 'off' if !$standard || !$standard->{off};
Branch: 1 branch
BranchTrueFalseTotal
unless $standard and $$standard{"off"} 0 0 0
Condition0%0/3
$standard and $$standard{"off"}
!ll&&!rl&&r
000
MC/DC0%0/2
$standard and $$standard{"off"}
$standard $$standard{"off"}
Truth table0%0/3
A and B
A= $standardB= $$standard{"off"}
ABresult
0X0
100
111
214 0 next;
215 }
216 0 ▶ if ( !$standard || $standard->{off} ) {
Branch: 1 branch
BranchTrueFalseTotal
if (not $standard or $$standard{"off"}) 0 0 0
Condition0%0/3
not $standard or $$standard{"off"}
l!l&&r!l&&!r
000
MC/DC0%0/2
not $standard or $$standard{"off"}
not $standard $$standard{"off"}
Truth table0%0/3
not(A) or B
A= not $standardB= $$standard{"off"}
ABresult
1X1
011
000
217 0 $overrides{$name} = { %{ $mine->{parameters} } };
218 0 next;
219 }
220 0 my $changed = overlay( $standard->{parameters}, $mine->{parameters} );
221 0 ▶ $overrides{$name} = $changed if %$changed;
Branch: 1 branch
BranchTrueFalseTotal
if %$changed 0 0 0
222 }
223 0 my $settings = overlay( $want->{settings}, $have->{settings} );
224 0 ▶ my %adopted = ( %overrides ? ( overrides => \%overrides ) : (), %$settings ? ( settings => $settings ) : () );
Branch: 2 branches
BranchTrueFalseTotal
%overrides ? : 0 0 0
%$settings ? : 0 0 0
225 0 ▶ return %adopted ? \%adopted : undef;
Branch: 1 branch
BranchTrueFalseTotal
%adopted ? : 0 0 0
226 }
227  
228  
229 0 ▶sub full_name ( $name, $policies ) {
Subroutine full_name: not called CC=3 SCAR=24.8
Pod documented
230 0 ▶ return $name if exists $policies->{$name};
Branch: 1 branch
BranchTrueFalseTotal
if exists $$policies{$name} 0 0 0
231 0 my @longer = grep { local ($_) = $_; substr( $_, -length("::$name") ) eq "::$name" } keys %$policies;
232 0 ▶ return @longer == 1 ? $longer[0] : $name;
Branch: 1 branch
BranchTrueFalseTotal
@longer == 1 ? : 0 0 0
233 }
234  
235  
236 0 ▶sub overlay ( $from, $to ) {
Subroutine overlay: not called CC=3 SCAR=24.8
Pod documented
237 0 ▶ my %overlay = map { $_ => $to->{$_} } grep { !exists $from->{$_} || $from->{$_} ne $to->{$_} } keys %$to;
Condition0%0/3
!exists($$from{$_}) || $$from{$_} ne $$to{$_}
l!l&&r!l&&!r
000
MC/DC0%0/2
!exists($$from{$_}) || $$from{$_} ne $$to{$_}
!exists($$from{$_}) $$from{$_} ne $$to{$_}
Truth table0%0/3
not(A) || B
A= !exists($$from{$_})B= $$from{$_} ne $$to{$_}
ABresult
1X1
011
000
238 0 $overlay{$_} = undef for grep { !exists $to->{$_} } keys %$from;
239 0 return \%overlay;
240 }
241  
242 __END__
« Conformance/Check/SystemPackages.pm ↑ Summary Conformance/Schema.pm »