Conformance/Check/AuthorDeps.pm

filter: Statementstmt 14.5% 24 / 165 Branchbran 0.0% 0 / 52 Conditioncond 0.0% 0 / 42 MC/DCmcdc 0.0% 0 / 24 Subroutinesub 40.0% 8 / 20 Podpod 100% 12 / 12 totaltotal 13.9% 44 / 315 CC 54 SCAR 46.6
CC
54
Cov
9%
with_overrides
306.0
update
110.0
wanted
72.0
CRAP 105.2
« Conformance/Check/SystemPackages.pm ↑ Summary Conformance/Check/CriticProfile.pm »
1 package Dist::Conformance::Check::AuthorDeps 0.011;
2  
3 # ABSTRACT: dist.ini asks for each authordep of its template, at the version of the template or later
4  
5 2 ▶use 5.040;
Subroutine BEGIN: called CC=1 SCAR=0.0
6 2 ▶use warnings FATAL => 'all';
Subroutine BEGIN: called CC=1 SCAR=0.0
7 2 ▶use re '/aasx';
Subroutine BEGIN: called CC=1 SCAR=0.0
8 2 ▶no autovivification;
Subroutine BEGIN: called CC=1 SCAR=0.0
9 2 ▶use Mojo::Base 'Dist::Conformance::Check', -signatures;
Subroutine BEGIN: called CC=1 SCAR=0.0
10  
11 2 ▶use version();
Subroutine BEGIN: called CC=1 SCAR=0.0
12  
13 2 ▶use Dist::Conformance::Check();
Subroutine BEGIN: called CC=1 SCAR=0.0
14 2 ▶use Dist::Conformance::Schema();
Subroutine BEGIN: called CC=1 SCAR=0.0
15  
16  
17 2 my $VERSION_OR_OFF = { type => 'string', minLength => 1 };
18  
19 0 ▶sub config_schema ($) {
Subroutine config_schema: not called CC=1 SCAR=6.9
Pod documented
20 return {
21 0 required => [qw{file template}],
22 properties => {
23 file => $Dist::Conformance::Schema::PATH,
24 template => $Dist::Conformance::Schema::PATH,
25 overrides => {
26 type => 'object',
27 propertyNames => $Dist::Conformance::Check::MODULE,
28 additionalProperties => $VERSION_OR_OFF,
29 default => {},
30 },
31 missing => { %$Dist::Conformance::Schema::STATUS, default => 'info' },
32 },
33 };
34 }
35  
36 # The authordep lines of the INI text C<$text>, as C<dzil authordeps> reads
37 # them, in their order.
38 # @param text Str the text of an INI file, such as F<dist.ini>
39 # @returns the lines, as a list of hash references with the C<line>, counted
40 # from 0, the C<module>, the C<version>, which is C<0> when the line has no
41 # C<=>, the C<from> and C<to> offsets of the version in the line, which are
42 # undef when it has none, and the offset C<after> the name of the module
43 # @example my @deps = Dist::Conformance::Check::AuthorDeps::authordeps_of( $snapshot->content('dist.ini') );
44 0 ▶sub authordeps_of ($text) {
Subroutine authordeps_of: not called CC=5 SCAR=34.0
Pod documented
45 0 my @lines = split /^/, $text;
46 0 my @deps;
47 0 for my $index ( 0 .. $#lines ) {
48 0 ▶ my ( $module, $version ) = $lines[$index] =~ m/\A\s*;\s*authordep\s*(\S+)\s*(?:=\s*([^;]*[^;\s]))?/ or next;
Branch: 1 branch
BranchTrueFalseTotal
() = $lines[$index] =~ /\A\s*;\s*authordep\s*(\S+)\s*(?:=\s*([^;]*[^;\s]))?/aasx or next 0 0 0
49 0 ▶ my %dep = ( line => $index, module => $module, version => $version // 0, after => $+[1] );
Condition0%0/2
$version // 0
l!l
00
MC/DC0%0/1
$version // 0
$version
Truth table0%0/2
A
A= $version
Aresult
11
00
50 0 ▶ @dep{qw{from to}} = ( $-[2], $+[2] ) if defined $version;
Branch: 1 branch
BranchTrueFalseTotal
if defined $version 0 0 0
51 0 push @deps, \%dep;
52 }
53 0 return @deps;
54 }
55  
56 # What the repository in C<$snapshot> must ask for: the authordeps of the
57 # template with the overrides, by module, in the order of the template, and
58 # then the modules that only the overrides name, sorted. A module that the
59 # repository has under F<lib/> is left out.
60 # @param snapshot InstanceOf["Dist::Conformance::Snapshot"] the repository
61 # @returns the authordeps, as a list of hash references with the C<module>, the
62 # C<version>, and the C<text> of the line to add for it
63 # @throws when the source has no template, with the message of
64 # L<Dist::Conformance::Source/"file(relative)">
65 # @example my @wanted = $check->wanted($snapshot);
66 0 ▶sub wanted ( $self, $snapshot ) {
Subroutine wanted: not called CC=8 SCAR=42.8
Pod documented
67 0 my $config = $self->config;
68 0 my $overrides = $config->{overrides};
69 0 my ($text) = $self->template( $config->{template} );
70 0 my @lines = split /^/, $text;
71 0 my @named = ( ( map { $_->{module} } authordeps_of($text) ), keys %$overrides );
72 0 my %seen = map { $_ => 1 } grep { local ($_) = $_; own_module( $snapshot, $_ ) } @named;
73 0 my @wanted;
74 0 for my $dep ( authordeps_of($text) ) {
75 0 my $module = $dep->{module};
76 0 ▶ next if $seen{$module}++;
Branch: 1 branch
BranchTrueFalseTotal
if $seen{$module}++ 0 0 0
77 0 my $override = $overrides->{$module};
78 0 ▶ next if defined $override && $override eq 'off';
Branch: 1 branch
BranchTrueFalseTotal
if defined $override and $override eq "off" 0 0 0
Condition0%0/3
defined $override and $override eq "off"
!ll&&!rl&&r
000
MC/DC0%0/2
defined $override and $override eq "off"
defined $override $override eq "off"
Truth table0%0/3
A and B
A= defined $overrideB= $override eq "off"
ABresult
0X0
100
111
79 0 my $line = $lines[ $dep->{line} ] =~ s/\n?\z/\n/r;
80 0 ▶ push @wanted, defined $override ? authordep_line( $module, $override ) : { module => $module, version => $dep->{version}, text => $line };
Branch: 1 branch
BranchTrueFalseTotal
defined $override ? : 0 0 0
81 }
82 0 ▶ for my $module ( sort grep { !$seen{$_} && $overrides->{$_} ne 'off' } keys %$overrides ) {
Condition0%0/3
!$seen{$_} && $$overrides{$_} ne 'off'
!ll&&!rl&&r
000
MC/DC0%0/2
!$seen{$_} && $$overrides{$_} ne 'off'
!$seen{$_} $$overrides{$_} ne 'off'
Truth table0%0/3
not(A) && B
A= !$seen{$_}B= $$overrides{$_} ne 'off'
ABresult
0X0
100
111
83 0 push @wanted, authordep_line( $module, $overrides->{$module} );
84 }
85 0 return @wanted;
86 }
87  
88 # Whether the repository in C<$snapshot> has the module C<$module> under
89 # F<lib/>, so that it builds with its own copy.
90 # @param snapshot InstanceOf["Dist::Conformance::Snapshot"] the repository, or undef for none
91 # @param module Str the name of the module
92 # @returns Bool
93 # @example my $own = own_module( $snapshot, 'Foo::Bar' );
94 0 ▶sub own_module ( $snapshot, $module ) {
Subroutine own_module: not called CC=3 SCAR=24.8
Pod documented
95 0 ▶ return 0 if !$snapshot;
Branch: 1 branch
BranchTrueFalseTotal
unless $snapshot 0 0 0
96 0 ▶ return $snapshot->contains( 'lib/' . ( $module =~ s{::}{/}gr ) . '.pm' ) ? 1 : 0;
Branch: 1 branch
BranchTrueFalseTotal
$snapshot->contains('lib/' . $module =~ s[::][/]aagrsx . '.pm') ? : 0 0 0
97 }
98  
99 # The text C<$text> with its authordeps as C<$overrides> says, exactly: a
100 # module that is C<off>, or that the repository in C<$snapshot> has under
101 # F<lib/>, loses its lines; a version replaces that of the lines; and a module
102 # that the text lacks gets a line after its last authordep line, or at its end.
103 # Every other line stays as it was.
104 # @param text Str the text of the file
105 # @param overrides HashRef from the name of a module to C<off> or a version
106 # @param snapshot InstanceOf["Dist::Conformance::Snapshot"] optional: the repository
107 # @returns Str the new text
108 # @example my $text = with_overrides( $template, { 'Foo::Bar' => '1.2' }, $snapshot );
109 0 ▶sub with_overrides ( $text, $overrides, $snapshot = undef ) {
Subroutine with_overrides: not called CC=17 SCAR=57.2
Pod documented
110 0 my @lines = split /^/, $text;
111 0 my ( %seen, %drop );
112 0 for my $dep ( authordeps_of($text) ) {
113 0 my $module = $dep->{module};
114 0 my $override = $overrides->{$module};
115 0 $seen{$module} = 1;
116 0 ▶ if ( own_module( $snapshot, $module ) || ( defined $override && $override eq 'off' ) ) {
Branch: 1 branch
BranchTrueFalseTotal
if (own_module($snapshot, $module) or defined $override and $override eq "off") 0 0 0
Condition0%0/6
own_module($snapshot, $module) or defined $override and $override eq "off"
l!l&&r!l&&!r
000
defined $override and $override eq "off"
!ll&&!rl&&r
000
MC/DC0%0/3
own_module($snapshot, $module) or defined $override and $override eq "off"
own_module($snapshot, $module) defined $override $override eq "off"
Truth table0%0/4
A or B and C
A= own_module($snapshot, $module)B= defined $overrideC= $override eq "off"
ABCresult
1XX1
0111
00X0
0100
117 0 $drop{ $dep->{line} } = 1;
118 0 next;
119 }
120 0 ▶ next if !defined $override;
Branch: 1 branch
BranchTrueFalseTotal
unless defined $override 0 0 0
121 0 my $line = \$lines[ $dep->{line} ];
122 0 ▶ if ( defined $dep->{from} ) {
Branch: 1 branch
BranchTrueFalseTotal
if (defined $$dep{'from'}) { } 0 0 0
123 0 substr $$line, $dep->{from}, $dep->{to} - $dep->{from}, $override;
124 }
125 else {
126 0 substr $$line, $dep->{after}, 0, " = $override";
127 }
128 }
129 0 ▶ my @add = map { authordep_line( $_, $overrides->{$_} )->{text} } grep { !$seen{$_} && $overrides->{$_} ne 'off' && !own_module( $snapshot, $_ ) } sort keys %$overrides;
Condition0%0/6
!$seen{$_} && $$overrides{$_} ne 'off' && !own_module($snapshot, $_)
!ll&&!rl&&r
000
not $seen{$_} and $$overrides{$_} ne "off"
!ll&&!rl&&r
000
MC/DC0%0/3
!$seen{$_} && $$overrides{$_} ne 'off' && !own_module($snapshot, $_)
not $seen{$_} $$overrides{$_} ne "off" !own_module($snapshot, $_)
Truth table0%0/4
not(A) and B && not(C)
A= not $seen{$_}B= $$overrides{$_} ne "off"C= !own_module($snapshot, $_)
ABCresult
0XX0
10X0
1100
1111
130 0 my @kept;
131 my $last;
132 0 for my $index ( 0 .. $#lines ) {
133 0 ▶ next if $drop{$index};
Branch: 1 branch
BranchTrueFalseTotal
if $drop{$index} 0 0 0
134 0 push @kept, $lines[$index];
135 0 ▶ $last = $#kept if $lines[$index] =~ m/\A\s*;\s*authordep\s/;
Branch: 1 branch
BranchTrueFalseTotal
if $lines[$index] =~ /\A\s*;\s*authordep\s/aasx 0 0 0
136 }
137 0 ▶ if (@add) {
Branch: 1 branch
BranchTrueFalseTotal
if (@add) 0 0 0
138 0 ▶ my $after = $last // $#kept;
Condition0%0/2
$last // $#kept
l!l
00
MC/DC0%0/1
$last // $#kept
$last
Truth table0%0/2
A
A= $last
Aresult
11
00
139 0 ▶ $kept[$after] .= "\n" if $after >= 0 && $kept[$after] !~ m/\n\z/;
Branch: 1 branch
BranchTrueFalseTotal
if $after >= 0 and not $kept[$after] =~ /\n\z/aasx 0 0 0
Condition0%0/3
$after >= 0 and not $kept[$after] =~ /\n\z/aasx
!ll&&!rl&&r
000
MC/DC0%0/2
$after >= 0 and not $kept[$after] =~ /\n\z/aasx
$after >= 0 not $kept[$after] =~ /\n\z/aasx
Truth table0%0/3
A and not(B)
A= $after >= 0B= not $kept[$after] =~ /\n\z/aasx
ABresult
0X0
100
111
140 0 splice @kept, $after + 1, 0, @add;
141 }
142 0 return join q{}, @kept;
143 }
144  
145 # How the authordeps of the text C<$have> differ from those of C<$want>, as
146 # phrases for a person.
147 # @param have Str the text that the repository has
148 # @param want Str the text that it must have
149 # @returns the phrases, as a list, which is empty when both ask for the same
150 # @example my @differences = authordep_differences( $text, $expected );
151 0 ▶sub authordep_differences ( $have, $want ) {
Subroutine authordep_differences: not called CC=2 SCAR=17.9
Pod documented
152 0 my %have = map { $_->{module} => $_->{version} } authordeps_of($have);
153 0 my %want = map { $_->{module} => $_->{version} } authordeps_of($want);
154 return (
155 0 ( map { "lacks the authordep $_ $want{$_}" } grep { !exists $have{$_} } sort keys %want ),
156 0 ▶ ( map { "asks for $_ $have{$_}, not $want{$_}" } grep { exists $have{$_} && $have{$_} ne $want{$_} } sort keys %want ),
Condition0%0/3
exists $have{$_} && $have{$_} ne $want{$_}
!ll&&!rl&&r
000
MC/DC0%0/2
exists $have{$_} && $have{$_} ne $want{$_}
exists $have{$_} $have{$_} ne $want{$_}
Truth table0%0/3
A && B
A= exists $have{$_}B= $have{$_} ne $want{$_}
ABresult
0X0
100
111
157 0 ( map { "has the authordep $_, which neither the template nor the overrides name" } grep { !exists $want{$_} } sort keys %have ),
158 );
159 }
160  
161 # The authordep of C<$module> at C<$version>, and its line.
162 # @param module Str the name of the module
163 # @param version Str the version to ask for
164 # @returns HashRef with the C<module>, the C<version> and the C<text>
165 # @example my $dep = authordep_line( 'Foo::Bar', '1.2' );
166 0 ▶sub authordep_line ( $module, $version ) {
Subroutine authordep_line: not called CC=1 SCAR=6.9
Pod documented
167 0 return { module => $module, version => $version, text => "; authordep $module = $version\n" };
168 }
169  
170 # Whether version C<$have> is at least version C<$want>, or undef when either
171 # is not a version that can be compared, such as a range.
172 # @param have Str the version that a line asks for
173 # @param want Str the version that it must ask for
174 # @returns Bool or undef
175 # @example my $enough = at_least( '0.003', '0.004' ); # 0
176 0 ▶sub at_least ( $have, $want ) {
Subroutine at_least: not called CC=4 SCAR=30.0
Pod documented
177 0 ▶ return if !version::is_lax($have) || !version::is_lax($want);
Branch: 1 branch
BranchTrueFalseTotal
unless version::is_lax($have) and version::is_lax($want) 0 0 0
Condition0%0/3
version::is_lax($have) and version::is_lax($want)
!ll&&!rl&&r
000
MC/DC0%0/2
version::is_lax($have) and version::is_lax($want)
version::is_lax($have) version::is_lax($want)
Truth table0%0/3
A and B
A= version::is_lax($have)B= version::is_lax($want)
ABresult
0X0
100
111
178 0 ▶ return version->parse($have) >= version->parse($want) ? 1 : 0;
Branch: 1 branch
BranchTrueFalseTotal
'version'->parse($have) >= 'version'->parse($want) ? : 0 0 0
179 }
180  
181 # How the authordeps of the INI text C<$text> differ from what the repository
182 # in C<$snapshot> must ask for.
183 # @param text Str the text of the file
184 # @param snapshot InstanceOf["Dist::Conformance::Snapshot"] the repository
185 # @returns HashRef with C<missing>, the wanted authordeps that no line asks
186 # for; C<low>, the lines that ask for too little, each with the C<want> that
187 # they must ask for; and C<unknown>, the lines whose version cannot be
188 # compared, each with its C<want>. Each is an ArrayRef, in the order of the
189 # file or of the template.
190 # @throws when the source has no template, as L<wanted|/"wanted(snapshot)"> says
191 # @example my $differences = $check->differences( $snapshot->content('dist.ini'), $snapshot );
192 0 ▶sub differences ( $self, $text, $snapshot ) {
Subroutine differences: not called CC=7 SCAR=40.3
Pod documented
193 0 my %have;
194 0 push @{ $have{ $_->{module} } }, $_ for authordeps_of($text);
195 0 my %differences = ( missing => [], low => [], unknown => [] );
196 0 for my $want ( $self->wanted($snapshot) ) {
197 0 my $lines = $have{ $want->{module} };
198 0 ▶ if ( !$lines ) {
Branch: 1 branch
BranchTrueFalseTotal
unless ($lines) 0 0 0
199 0 push @{ $differences{missing} }, $want;
200 0 next;
201 }
202 0 for my $line (@$lines) {
203 0 my $enough = at_least( $line->{version}, $want->{version} );
204 0 ▶ push @{ $differences{ defined $enough ? 'low' : 'unknown' } }, { %$line, want => $want->{version} } if !$enough;
Branch: 2 branches
BranchTrueFalseTotal
unless $enough 0 0 0
defined $enough ? : 0 0 0
205 }
206 }
207 0 return \%differences;
208 }
209  
210 0 ▶sub check ( $self, $snapshot ) {
Subroutine check: not called CC=4 SCAR=30.0
Pod documented
211 0 my $file = $self->config->{file};
212 0 ▶ my $text = $snapshot->content($file) // return ( $self->config->{missing}, "there is no $file" );
Condition0%0/2
$snapshot->content($file) // (return $self->config->{"missing"}, "there is no $file")
l!l
00
MC/DC0%0/1
$snapshot->content($file) // (return $self->config->{"missing"}, "there is no $file")
$snapshot->content($file)
Truth table0%0/2
A
A= $snapshot->content($file)
Aresult
11
00
213 0 ▶ my $differences = eval { $self->differences( $text, $snapshot ) } // return ( 'fail', Dist::Conformance::Schema::first_line($@) );
Condition0%0/2
eval { do { $self->differences($text, $snapshot) } } // (return "fail", Dist::Conformance::Schema::first_line($@))
l!l
00
MC/DC0%0/1
eval { do { $self->differences($text, $snapshot) } } // (return "fail", Dist::Conformance::Schema::first_line($@))
eval { do { $self->differences($text, $snapshot) } }
Truth table0%0/2
A
A= eval { do { $self->differences($text, $snapshot) } }
Aresult
11
00
214 my @problems = (
215 0 ( map { "lacks $_->{module} $_->{version}" } @{ $differences->{missing} } ),
216 0 ( map { "asks for $_->{module} $_->{version}, not $_->{want}" } @{ $differences->{low} } ),
217 0 ( map { "asks for $_->{module} $_->{version}, which cannot be compared with $_->{want}" } @{ $differences->{unknown} } ),
218 );
219 0 ▶ return ( 'pass', 'asks for each authordep of the template, at its version or later' ) if !@problems;
Branch: 1 branch
BranchTrueFalseTotal
unless @problems 0 0 0
220 0 return ( 'fail', "$file " . join '; ', @problems );
221 }
222  
223 0 ▶sub update ( $self, $update ) {
Subroutine update: not called CC=10 SCAR=47.0
Pod documented
224 0 my $file = $self->config->{file};
225 0 my $snapshot = $update->snapshot;
226 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
227 0 my $differences = eval { $self->differences( $text, $snapshot ) };
228 0 ▶ if ( !$differences ) {
Branch: 1 branch
BranchTrueFalseTotal
unless ($differences) 0 0 0
229 0 $update->by_hand( Dist::Conformance::Schema::first_line($@) . ': fix it by hand' );
230 0 return;
231 }
232 0 foreach my $unknown ( @{ $differences->{unknown} } ) {
233 0 $update->by_hand("In $file, make the authordep of $unknown->{module} ask for $unknown->{want} or later: its version $unknown->{version} cannot be compared");
234 }
235  
236 0 my @lines = split /^/, $text;
237 0 for my $low ( @{ $differences->{low} } ) {
238 0 my $line = \$lines[ $low->{line} ];
239 0 ▶ if ( defined $low->{from} ) {
Branch: 1 branch
BranchTrueFalseTotal
if (defined $$low{'from'}) { } 0 0 0
240 0 substr $$line, $low->{from}, $low->{to} - $low->{from}, $low->{want};
241 }
242 else {
243 0 substr $$line, $low->{after}, 0, " = $low->{want}";
244 }
245 }
246 0 ▶ if ( my @add = map { $_->{text} } @{ $differences->{missing} } ) {
Branch: 1 branch
BranchTrueFalseTotal
if (my(@add) = map({$our $_{"text"};} @{$$differences{"missing"};})) 0 0 0
247 0 my @deps = authordeps_of($text);
248 0 ▶ my $after = @deps ? $deps[-1]{line} : $#lines;
Branch: 1 branch
BranchTrueFalseTotal
@deps ? : 0 0 0
249 0 ▶ $lines[$after] .= "\n" if $after >= 0 && $lines[$after] !~ m/\n\z/;
Branch: 1 branch
BranchTrueFalseTotal
if $after >= 0 and not $lines[$after] =~ /\n\z/aasx 0 0 0
Condition0%0/3
$after >= 0 and not $lines[$after] =~ /\n\z/aasx
!ll&&!rl&&r
000
MC/DC0%0/2
$after >= 0 and not $lines[$after] =~ /\n\z/aasx
$after >= 0 not $lines[$after] =~ /\n\z/aasx
Truth table0%0/3
A and not(B)
A= $after >= 0B= not $lines[$after] =~ /\n\z/aasx
ABresult
0X0
100
111
250 0 splice @lines, $after + 1, 0, @add;
251 }
252 0 $update->write( $file, join q{}, @lines );
253 0 return;
254 }
255  
256 0 ▶sub adopt ( $self, $snapshot ) {
Subroutine adopt: not called CC=3 SCAR=24.8
Pod documented
257 0 ▶ my $text = $snapshot->content( $self->config->{file} ) // return;
Condition0%0/2
$snapshot->content($self->config->{"file"}) // (return)
l!l
00
MC/DC0%0/1
$snapshot->content($self->config->{"file"}) // (return)
$snapshot->content($self->config->{"file"})
Truth table0%0/2
A
A= $snapshot->content($self->config->{"file"})
Aresult
11
00
258 0 my $differences = $self->differences( $text, $snapshot );
259 my %overrides = (
260 0 ( map { $_->{module} => 'off' } @{ $differences->{missing} } ),
261 0 ( map { $_->{module} => $_->{version} } @{ $differences->{low} }, @{ $differences->{unknown} } ),
262 );
263 0 ▶ return if !%overrides;
Branch: 1 branch
BranchTrueFalseTotal
unless %overrides 0 0 0
264 0 return { overrides => { %{ $self->config->{overrides} }, %overrides } };
265 }
266  
267 __END__
« Conformance/Check/SystemPackages.pm ↑ Summary Conformance/Check/CriticProfile.pm »