Plugin/Test/Software/Policies.pm

filter: Statementstmt 30.1% 51 / 169 Branchbran 0.0% 0 / 28 Conditioncond 0.0% 0 / 5 MC/DCmcdc 0.0% 0 / 3 Subroutinesub 51.8% 14 / 27 Podpod 0.0% 0 / 3 totaltotal 27.6% 65 / 235 CC 24 SCAR 36.3
CC
24
Cov
25%
_get_policy_config
110.0
__ANON__
72.0
__ANON__
30.0
CRAP 37.8
« App/Command/policies.pm ↑ Summary Plugin/Software/Policies.pm »
1 ## no critic (ControlStructures::ProhibitPostfixControls)
2 package Dist::Zilla::Plugin::Test::Software::Policies;
3 1 ▶use strict;
Subroutine BEGIN: called CC=1 SCAR=0.0
4 1 ▶use warnings;
Subroutine BEGIN: called CC=1 SCAR=0.0
5 1 ▶use 5.010;
Subroutine BEGIN: called CC=1 SCAR=0.0
6  
7 # ABSTRACT: Tests to check your code against best practices
8 1 our $VERSION = '0.002';
9 1 ▶use Moose;
Subroutine BEGIN: called CC=1 SCAR=0.0
10  
11 1 ▶use Moose::Util::TypeConstraints qw(
Subroutine BEGIN: called CC=1 SCAR=0.0
12 role_type
13 1 );
14 1 ▶use Dist::Zilla::File::InMemory;
Subroutine BEGIN: called CC=1 SCAR=0.0
15 1 ▶use Sub::Exporter::ForMethods 'method_installer';
Subroutine BEGIN: called CC=1 SCAR=0.0
16 1 ▶use Data::Section 0.004 { installer => method_installer }, '-setup';
Subroutine BEGIN: called CC=1 SCAR=0.0
17 1 ▶use Data::Dumper ();
Subroutine BEGIN: called CC=1 SCAR=0.0
18 1 ▶use namespace::autoclean;
Subroutine BEGIN: called CC=1 SCAR=0.0
19 1 ▶use Path::Tiny qw( path );
Subroutine BEGIN: called CC=1 SCAR=0.0
20 1 ▶use List::Util qw( any first );
Subroutine BEGIN: called CC=1 SCAR=0.0
21 1 ▶use Software::Policies ();
Subroutine BEGIN: called CC=1 SCAR=0.0
22  
23 1 with(
24 'Dist::Zilla::Role::FileFinderUser' => {
25 default_finders => [],
26 },
27 'Dist::Zilla::Role::FileGatherer',
28 'Dist::Zilla::Role::FileMunger',
29 'Dist::Zilla::Role::TextTemplate',
30 'Dist::Zilla::Role::PrereqSource',
31 );
32  
33 has include_policy => (
34 is => 'ro',
35 isa => 'ArrayRef[Str]',
36 0 ▶ default => sub { [] },
Subroutine __ANON__: not called CC=1 SCAR=6.9
37 1 );
38  
39 has exclude_policy => (
40 is => 'ro',
41 isa => 'ArrayRef[Str]',
42 0 ▶ default => sub { [] },
Subroutine __ANON__: not called CC=1 SCAR=6.9
43 1 );
44  
45 1 has filepath_template => (
46 is => 'ro',
47 ## no critic (ValuesAndExpressions::RequireInterpolationOfMetachars)
48 default => 'xt/author/policy_{{ $policy }}.t',
49 );
50  
51 # Internal
52  
53 has _project_policies => (
54 is => 'ro',
55  
56 # isa => role_type('Software::Policies'),
57 0 ▶ default => sub {
Subroutine __ANON__: not called CC=1 SCAR=6.9
58 0 Software::Policies->new;
59 },
60 1 );
61  
62 has _test_files => (
63 is => 'ro',
64  
65 # isa => q{ArrayRef[role_type('Dist::Zilla::Role::File')]},
66 lazy => 1,
67 0 ▶ default => sub {
Subroutine __ANON__: not called CC=8 SCAR=42.8
68 0 my $self = shift;
69 0 my $zilla = $self->zilla;
70  
71 # $zilla->log_debug( [ 'include_policy: %s', $self->include_policy() ] );
72 my @policy_plugins = grep {
73 0 ▶ $_->isa('Dist::Zilla::Plugin::Software::Policies')
Condition0%0/3
$_->isa('Dist::Zilla::Plugin::Software::Policies') && $_->plugin_name ne 'Software::Policies'
!ll&&!rl&&r
000
MC/DC0%0/2
$_->isa('Dist::Zilla::Plugin::Software::Policies') && $_->plugin_name ne 'Software::Policies'
$_->isa('Dist::Zilla::Plugin::Software::Policies') $_->plugin_name ne 'Software::Policies'
Truth table0%0/3
A && B
A= $_->isa('Dist::Zilla::Plugin::Software::Policies')B= $_->plugin_name ne 'Software::Policies'
ABresult
0X0
100
111
74  
75 # Exclude the "General definitions"
76 && $_->plugin_name ne 'Software::Policies'
77 0 } @{ $zilla->plugins };
78 0 my %check_policies;
79  
80 # Test::Software::Policies first checks if
81 # 1) there is any include_policy config items. If there is, limit to those.
82 # 2) If there is no include_policy,
83 # look for [Software::Policies [/policy] ] configs and take all those.
84 # 3) If there is no [Software::Policies [/policy] ] configs,
85 # ask Software::Policies->list for all availabe and take all.
86 # 4) If there is any exclude_policy config items
87 # in [Test::Software::Policies]], remove those from the list.
88 # 5) Now we have the complete list of policies which need to be checked.
89 0 ▶ if ( @{ $self->include_policy() } ) {
Branch: 2 branches
BranchTrueFalseTotal
if (@{$self->include_policy;}) { } 0 0 0
elsif (@policy_plugins) { } 0 0 0
90 0 $zilla->log_debug( ['We have include_policies'] );
91 0 foreach my $include_policy ( @{ $self->include_policy() } ) {
92 0 $zilla->log_debug( [ 'Include policy %s from include_policy config item', $include_policy ] );
93 0 $check_policies{$include_policy} = 1;
94 }
95 }
96 elsif (@policy_plugins) {
97 0 $zilla->log_debug( ['We check what [Software::Policies] plugins are in use'] );
98 0 %check_policies = map { $_->plugin_name => 1 } @policy_plugins;
99 0 $zilla->log_debug( [ 'We have the policies: %s', \%check_policies ] );
100 }
101 else {
102 0 $zilla->log_debug( ['We take all [Software::Policies] which are available'] );
103 0 %check_policies = map { $_ => 1 } keys %{ $self->_project_policies()->list() };
104 0 $zilla->log_debug( [ 'We have the policies: %s', \%check_policies ] );
105 }
106  
107 0 my @files;
108 0 foreach my $policy ( keys %check_policies ) {
109 0 my %args = $self->_get_policy_config( $policy, {} );
110 0 $args{policy} = $policy;
111 $self->zilla()
112 ->log_debug(
113 0 [ 'Looking for matching policy: %s:%s:%s:%s', $args{'policy'}, $args{'class'}, $args{'version'}, $args{'format'} ]
114 );
115  
116 0 my @p = Software::Policies->new()->create(%args);
117 0 ▶ if ( @p > 1 ) {
Branch: 1 branch
BranchTrueFalseTotal
if (@p > 1) { } 0 0 0
118 0 foreach (@p) {
119 my $fn = $self->fill_in_string( $self->filepath_template(),
120 0 { policy => $policy . q{_} . ( $_->{'filename'} =~ s/[.]/_/grmsx ), } );
121  
122 # Attach the ready made policy file content to the end of the file, in the __DATA__ section.
123 my $content = $self->fill_in_string(
124 0 ${ $self->section_data('test-policy') } . $_->{'text'},
125 0 { dumper => \\&_dumper, filepath => $_->{'filename'}, }
126 );
127 0 push @files, Dist::Zilla::File::InMemory->new( name => $fn, content => $content, );
128 }
129 }
130 else {
131 0 my $fn = $self->fill_in_string( $self->filepath_template(), { policy => $policy, } );
132  
133 # Attach the ready made policy file content to the end of the file, in the __DATA__ section.
134 my $content = $self->fill_in_string(
135 0 ${ $self->section_data('test-policy') } . $p[0]->{'text'},
136 0 { dumper => \\&_dumper, filepath => $p[0]->{'filename'}, }
137 );
138 0 push @files, Dist::Zilla::File::InMemory->new( name => $fn, content => $content, );
139 }
140  
141 }
142 0 return \@files;
143 },
144 1 );
145  
146 0 ▶sub mvp_multivalue_args {
Subroutine mvp_multivalue_args: not called CC=1 SCAR=6.9
147 0 return qw(
148 files include_policy exclude_policy
149 );
150 }
151  
152 0 ▶around BUILDARGS => sub {
Subroutine __ANON__: not called CC=5 SCAR=34.0
153 0 my ( $orig, $class, @arg ) = @_;
154 0 my $args = $class->$orig(@arg);
155 0 my %copy = %{$args};
156 0 my $zilla = delete $copy{zilla};
157 0 $zilla->log_debug( 'copy=%s', \%copy );
158 0 my $name = delete $copy{plugin_name};
159 0 my %other;
160 0 ▶ $other{'include_policy'} = delete $copy{include_policy} if $copy{include_policy};
Branch: 1 branch
BranchTrueFalseTotal
if $copy{"include_policy"} 0 0 0
161 0 ▶ $other{'exclude_policy'} = delete $copy{exclude_policy} if $copy{exclude_policy};
Branch: 1 branch
BranchTrueFalseTotal
if $copy{"exclude_policy"} 0 0 0
162 0 ▶ $other{'filepath_template'} = delete $copy{filepath_template} if $copy{filepath_template};
Branch: 1 branch
BranchTrueFalseTotal
if $copy{"filepath_template"} 0 0 0
163 0 $zilla->log_debug( [ 'Policy %s. Collected attributes: %s', $name, \%other ] );
164  
165 0 ▶ if (%copy) {
Branch: 1 branch
BranchTrueFalseTotal
if (%copy) 0 0 0
166 0 $zilla->log_fatal( [ 'Unknown configuration option(s): %s', ( join q{,}, keys %copy ) ] );
167 }
168 return {
169 0 zilla => $zilla,
170 plugin_name => $name,
171  
172 # _prereq => \%copy,
173 %other,
174 };
175 1 };
176  
177 0 ▶sub gather_files {
Subroutine gather_files: not called CC=2 SCAR=17.9
Pod undocumented
178 0 my $self = shift;
179 0 foreach my $file ( @{ $self->_test_files() } ) {
180 0 $self->add_file($file);
181 }
182 0 return;
183 }
184  
185 0 ▶sub register_prereqs {
Subroutine register_prereqs: not called CC=1 SCAR=6.9
Pod undocumented
186 0 my $self = shift;
187  
188 0 return $self->zilla->register_prereqs(
189 {
190 type => 'requires',
191 phase => 'develop',
192 },
193 'Software::Policies' => 0,
194 );
195 }
196  
197 0 ▶sub _dumper {
Subroutine _dumper: not called CC=1 SCAR=6.9
198 0 my ($value) = @_;
199 0 local $Data::Dumper::Indent = 1;
200 0 local $Data::Dumper::Useqq = 1;
201 0 local $Data::Dumper::Terse = 1;
202 0 local $Data::Dumper::Sortkeys = 1;
203 0 local $Data::Dumper::Trailingcomma = 1;
204 0 my $dump = Data::Dumper::Dumper($value);
205 0 $dump =~ s{\n\z}{}msx;
206 0 return $dump;
207 }
208  
209 0 ▶sub munge_file {
Subroutine munge_file: not called CC=1 SCAR=6.9
Pod undocumented
210 0 my ( $self, $file ) = @_;
211 0 my $zilla = $self->zilla;
212 0 $zilla->log_debug( [ 'munge_file(%s)', $file->name ] );
213  
214 0 return $file;
215 }
216  
217 0 ▶sub _get_policy_config {
Subroutine _get_policy_config: not called CC=10 SCAR=47.0
218 0 my ( $self, $policy, $opt ) = @_;
219 0 my $zilla = $self->zilla;
220  
221 # 1. "Default" values, taken from dist.ini
222 0 my %args;
223 my %attributes = (
224 name => $zilla->{'name'},
225 abstract => $zilla->{'abstract'},
226 0 authors => $zilla->{'authors'},
227  
228 # license => $zilla->{'license'},
229 # main_module => $zilla->{'main_module'},
230 version => $zilla->version,
231 );
232 0 $zilla->log_debug( ['After 1.'] );
233 0 $zilla->log_debug( [ 'args: %s', \%args, ] );
234 0 $zilla->log_debug( [ 'attributes: %s', \%attributes, ] );
235  
236 # 2. Config items applied to all policies.
237 # Only "[Software::Policies]"
238 0 ▶ my $plain_plugin = first { $_->isa('Dist::Zilla::Plugin::Software::Policies') && $_->plugin_name eq 'Software::Policies' }
Subroutine __ANON__: not called CC=2 SCAR=17.9
Branch: 1 branch
BranchTrueFalseTotal
$_->isa("Dist::Zilla::Plugin::Software::Policies") and $_->plugin_name eq "Software::Policies" 0 0 0
239 0 @{ $zilla->{'plugins'} };
240 0 ▶ if ($plain_plugin) {
Branch: 1 branch
BranchTrueFalseTotal
if ($plain_plugin) 0 0 0
241 0 $zilla->log_debug( ['Discovered general setting for Software::Policies'] );
242 0 for my $key (qw( class version format dir filename )) {
243 0 ▶ $args{$key} = $plain_plugin->{$key} if $plain_plugin->{$key};
Branch: 1 branch
BranchTrueFalseTotal
if $plain_plugin->{$key} 0 0 0
244 }
245 0 my %policy_attributes = %{ $plain_plugin->{'policy_attribute'} };
246 0 @attributes{ keys %policy_attributes } = @policy_attributes{ keys %policy_attributes };
247 }
248 0 $zilla->log_debug( ['After 2.'] );
249 0 $zilla->log_debug( [ 'args: %s', \%args, ] );
250 0 $zilla->log_debug( [ 'attributes: %s', \%attributes, ] );
251  
252 # 3. Only one policy's config
253 # Only "[Software::Policies / $policy]", plugin_name is changed in Plugin::S::p!
254 my $this_plugin =
255 0 ▶ first { $_->isa('Dist::Zilla::Plugin::Software::Policies') && $_->plugin_name =~ m/^ $policy $/msx } @{ $zilla->{'plugins'} };
Subroutine __ANON__: not called CC=2 SCAR=17.9
Branch: 1 branch
BranchTrueFalseTotal
$_->isa("Dist::Zilla::Plugin::Software::Policies") and $_->plugin_name =~ /^ $policy $/msx 0 0 0
256 0 ▶ if ($this_plugin) {
Branch: 1 branch
BranchTrueFalseTotal
if ($this_plugin) 0 0 0
257 0 $zilla->log_debug( [ 'Discovered config for Software::Policies / %s: %s', $policy, $this_plugin->plugin_name ] );
258 0 for my $key (qw( class version format dir filename )) {
259 0 ▶ $args{$key} = $this_plugin->{$key} if $this_plugin->{$key};
Branch: 1 branch
BranchTrueFalseTotal
if $this_plugin->{$key} 0 0 0
260 }
261 0 my %policy_attributes = %{ $this_plugin->{'policy_attribute'} };
262 0 @attributes{ keys %policy_attributes } = @policy_attributes{ keys %policy_attributes };
263 }
264  
265 # 4. Config from the command line.
266 0 for my $key (qw( class version format dir filename )) {
267 0 ▶ $args{$key} = $opt->{$key} if $opt->{$key};
Branch: 1 branch
BranchTrueFalseTotal
if $opt->{$key} 0 0 0
268 }
269 0 ▶ my %attrs = map { split qr/\s*=\s*/msx, $_, 2 } ( map { split qr/,/msx } $opt->{'attributes'} // q{} );
Condition0%0/2
$opt->{"attributes"} // ""
l!l
00
MC/DC0%0/1
$opt->{"attributes"} // ""
$opt->{"attributes"}
Truth table0%0/2
A
A= $opt->{"attributes"}
Aresult
11
00
270 0 @attributes{ keys %attrs } = @attrs{ keys %attrs };
271  
272 # Set attributes into %args.
273 0 $args{attributes} = \%attributes;
274 0 $zilla->log_debug( [ 'args: %s', \%args, ] );
275 0 $zilla->log_debug( [ 'attributes: %s', \%attributes, ] );
276  
277 0 return %args;
278 }
279  
280 1 ▶no Moose;
Subroutine BEGIN: called CC=1 SCAR=0.0
281 1 __PACKAGE__->meta->make_immutable;
282 1 1;
283  
284 =pod
285  
286 =encoding UTF-8
287  
288 =head1 NAME
289  
290 Dist::Zilla::Plugin::Test::Software::Policies - Tests to check your code against best practices
291  
292 =head1 VERSION
293  
294 version 0.002
295  
296 =head1 SYNOPSIS
297  
298 In your F<dist.ini>:
299  
300 =head1 DESCRIPTION
301  
302 This L<Dist::Zilla> plugin creates author test files for use during
303 the C<test> and C<release> runs of C<dzil>. Examples of these files:
304 F<xt/author/policy_contributing.t> and F<xt/author/policy_code_of_conduct.t>.
305  
306 These tests will ensure that the file in question, e.g. F<CODE_OF_CONDUCT.md>,
307 is in the distribution, and is up to date
308 and matches with the information in the F<dist.ini> file.
309  
310 To use this, make the changes to F<dist.ini>
311 above and run one of the following:
312  
313 dzil test
314 dzil release
315  
316 =for Pod::Coverage gather_files register_prereqs munge_file
317  
318 =for stopwords LICENCE
319  
320 =for test_synopsis BEGIN { die "SKIP: skip this pod!\n"; }
321  
322 [Test::Software::Policies]
323 exclude_policy = CodeOfConduct
324  
325 =head1 ATTRIBUTES
326  
327 =head2 include_policy
328  
329 =head2 include_policy
330  
331 =head2 filepath_template
332  
333 The file name of the test files to generate. Defaults to C<'xt/author/policy_{{ $policy }}.t'> as in F<xt/author/policy_contributing.t>.
334  
335 Allowed characters: [A-Za-z0-9-_./{}$]
336  
337 =head1 AUTHOR
338  
339 Mikko Koivunalho <mikkoi@cpan.org>
340  
341 =head1 COPYRIGHT AND LICENSE
342  
343 This software is copyright (c) 2025 by Mikko Koivunalho.
344  
345 This is free software; you can redistribute it and/or modify it under
346 the same terms as the Perl 5 programming language system itself.
347  
348 =cut
349  
350 __DATA__
« App/Command/policies.pm ↑ Summary Plugin/Software/Policies.pm »