File Coverage

lib/Dist/Zilla/Plugin/Test/ReportMetadata.pm
Criterion Covered Total %
statement 64 64 100.0
branch 3 6 50.0
condition 1 3 33.3
subroutine 16 16 100.0
pod 3 3 100.0
total 87 92 94.5


line stmt bran cond sub pod time code
1             package Dist::Zilla::Plugin::Test::ReportMetadata;
2              
3 1     1   1457200 use 5.008;
  1         7  
4 1     1   10 use version; our $VERSION = qv( sprintf '0.5.%d', q$Rev: 1 $ =~ /\d+/gmx );
  1         2  
  1         10  
5              
6 1     1   208 use Data::Dumper;
  1         2  
  1         129  
7 1     1   8 use Dist::Zilla 4 ();
  1         33  
  1         41  
8 1     1   8 use File::Spec;
  1         2  
  1         32  
9 1     1   6 use Moose;
  1         3  
  1         11  
10 1     1   9534 use Sub::Exporter::ForMethods;
  1         2  
  1         24  
11              
12             use Data::Section 0.200002 # encoding and bytes
13 1     1   262 { installer => Sub::Exporter::ForMethods::method_installer }, '-setup';
  1         45  
  1         8  
14              
15             with 'Dist::Zilla::Role::FileGatherer', 'Dist::Zilla::Role::PrereqSource';
16              
17             has 'env_vars' => is => 'ro', traits => [ 'Array' ], default => sub {
18             [ qw( AUTHOR_TESTING AUTOMATED_TESTING EXTENDED_TESTING
19             NONINTERACTIVE_TESTING PERL_CPAN_REPORTER_CONFIG
20             PERL_CR_SMOKER_CURRENT PERL5_CPAN_IS_RUNNING
21             PERL5_CPANPLUS_IS_VERSION TEST_CRITIC TEST_SPELLING ) ] },
22             handles => { 'display_vars' => 'elements', }, init_arg => 'env_var';
23              
24             has 'excludes' => is => 'ro', traits => [ 'Array' ], default => sub { [] },
25             handles => { 'excluded_modules' => 'elements', }, init_arg => 'exclude';
26              
27             has 'includes' => is => 'ro', traits => [ 'Array' ], default => sub { [] },
28             handles => { 'included_modules' => 'elements', }, init_arg => 'include';
29              
30             has 'verify_prereqs' => is => 'ro', isa => 'Bool', default => 1;
31              
32             sub _dump_filename {
33 2     2   89 return File::Spec->catfile( 't', '00report-metadata.dd' );
34             }
35              
36             sub _dump_prereqs {
37 2     2   6 my $self = shift;
38 2         96 my $prereqs = $self->zilla->prereqs->as_string_hash;
39 2         1748 my $dumper = Data::Dumper->new( [ $prereqs ], [ 'x' ] );
40 2         80 my $dumped = $dumper->Purity( 1 )->Sortkeys( 1 )->Terse( 0 )->Dump();
41              
42 2         361 return "do { my ${dumped} \$x;\n }";
43             }
44              
45             sub _format_list {
46 3     3   9 return join "\n", map { " ${_}" } @_;
  13         103  
47             }
48              
49             sub _munge_test {
50 1     1   9 my ($self, $guts) = @_;
51              
52 1 50       40 $guts =~ s{INSERT_VERSION_HERE}{$self->VERSION || '<self>'}e;
  1         55  
53 1         75 $guts =~ s{INSERT_DD_FILENAME_HERE}{$self->_dump_filename}e;
  1         7  
54 1         63 $guts =~ s{INSERT_DISPLAY_VARS_HERE}{_format_list( $self->display_vars )}e;
  1         111  
55 1         82 $guts =~ s{INSERT_INCLUDED_MODULES_HERE}{_format_list( $self->included_modules )}e;
  1         54  
56 1         71 $guts =~ s{INSERT_EXCLUDED_MODULES_HERE}{_format_list( $self->excluded_modules )}e;
  1         61  
57 1 50       75 $guts =~ s{INSERT_VERIFY_PREREQS_CONFIG}{$self->verify_prereqs ? 1 : 0}e;
  1         96  
58 1         36 return $guts;
59             }
60              
61             sub gather_files {
62 1     1 1 74034 my $self = shift; my $data = $self->merged_section_data;
  1         6  
63              
64 1 50 33     9224 $data and %{ $data } or return;
  1         10  
65              
66 1         9 require Dist::Zilla::File::InMemory;
67              
68 1         2 for my $filename (keys %{ $data }) {
  1         5  
69             $self->add_file
70             ( Dist::Zilla::File::InMemory->new
71             ( { name => $filename,
72 1         3 content => $self->_munge_test( ${ $data->{ $filename } } ) } ) );
  1         5  
73             }
74              
75 1         2566 require Dist::Zilla::File::FromCode;
76              
77             $self->add_file
78             ( Dist::Zilla::File::FromCode->new
79             ( { name => $self->_dump_filename,
80 1     2   76596 code => sub { $self->_dump_prereqs }, } ) );
  2         245100  
81              
82 1         589 return;
83             }
84              
85             sub mvp_multivalue_args {
86 1     1 1 191 return qw( env_var exclude include );
87             }
88              
89             sub register_prereqs {
90 1     1 1 453613 my $self = shift; my $zilla = $self->zilla;
  1         64  
91              
92 1         12 $zilla->register_prereqs
93             ( { phase => 'test', type => 'requires', },
94             'File::Spec' => 0,
95             'Module::Metadata' => 0,
96             'Sys::Hostname' => 0, );
97              
98 1         199 $zilla->register_prereqs
99             ( { phase => 'test', type => 'recommends', },
100             'CPAN::Meta' => '2.120900', );
101              
102 1         177 return;
103             }
104              
105             __PACKAGE__->meta->make_immutable;
106              
107             1;
108              
109             =pod
110              
111             =encoding utf-8
112              
113             =begin html
114              
115             <a href="https://travis-ci.org/pjfl/p5-dist-zilla-plugin-test-reportmetadata"><img src="https://travis-ci.org/pjfl/p5-dist-zilla-plugin-test-reportmetadata.svg?branch=master" alt="Travis CI Badge"></a>
116             <a href="http://badge.fury.io/pl/Dist-Zilla-Plugin-Test-ReportMetadata"><img src="https://badge.fury.io/pl/Dist-Zilla-Plugin-Test-ReportMetadata.svg" alt="CPAN Badge"></a>
117             <a href="http://cpants.cpanauthors.org/dist/Dist-Zilla-Plugin-Test-ReportMetadata"><img src="http://cpants.cpanauthors.org/dist/Dist-Zilla-Plugin-Test-ReportMetadata.png" alt="Kwalitee Badge"></a>
118              
119             =end html
120              
121             =head1 Name
122              
123             Dist::Zilla::Plugin::Test::ReportMetadata - Report on prerequisite versions during automated testing
124              
125             =head1 Synopsis
126              
127             # In dist.ini
128             [Test::ReportMetadata]
129              
130             =head1 Description
131              
132             The is a clone of L<Dist::Zilla::Plugin::Test::ReportPrereqs> but with the
133             dependency on L<ExtUtils::MakeMaker> replaced with one on
134             L<Module::Metadata>. If you are using L<Module::Build> then L<Module::Metadata>
135             is already a dependency
136              
137             Versions are reported based on the result of the C<version> attribute from
138             L<Module::Metadata>
139              
140             Additionally a selection of environment variables are also displayed
141              
142             =head1 Configuration and Environment
143              
144             Defines the following attributes;
145              
146             =over 3
147              
148             =item C<env_vars>
149              
150             An array reference of environment variable names displayed on the test report.
151             Set using the multivalued initialisation argument C<env_var>. If the list
152             has no values then nothing is printed. An empty list can be set with
153              
154             [Test::ReportMetadata]
155             env_var = none
156              
157             =item C<excludes>
158              
159             An array reference of module names to exclude from the test report.
160             Set using the multivalued initialisation argument C<exclude>
161              
162             =item C<includes>
163              
164             An array reference of module names to include in the test report.
165             Set using the multivalued initialisation argument C<include>
166              
167             =item C<verify_prereqs>
168              
169             A boolean defaulting to true. If true emits lots of warnings if prerequisites
170             are not satisfied
171              
172             =back
173              
174             =head1 Subroutines/Methods
175              
176             =head2 C<gather_files>
177              
178             Required by L<Dist::Zilla::Role::FileGatherer>
179              
180             =head2 C<mvp_multivalue_args>
181              
182             Returns a list of configuration attribute names that are treated as
183             multi valued
184              
185             =head2 C<register_prereqs>
186              
187             Required by L<Dist::Zilla::Role::PrereqSource>
188              
189             =head1 Diagnostics
190              
191             None
192              
193             =head1 Dependencies
194              
195             =over 3
196              
197             =item L<Data::Dumper>
198              
199             =item L<Data::Section>
200              
201             =item L<Dist::Zilla>
202              
203             =item L<Moose>
204              
205             =item L<Sub::Exporter::ForMethods>
206              
207             =back
208              
209             =head1 Incompatibilities
210              
211             There are no known incompatibilities in this module
212              
213             =head1 Bugs and Limitations
214              
215             There are no known bugs in this module. Please report problems to
216             http://rt.cpan.org/NoAuth/Bugs.html?Dist=Dist-Zilla-Plugin-Test-ReportMetadata.
217             Patches are welcome
218              
219             =head1 Acknowledgements
220              
221             Larry Wall - For the Perl programming language
222              
223             =head1 Author
224              
225             Peter Flanigan, C<< <pjfl@cpan.org> >>
226              
227             =head1 License and Copyright
228              
229             Copyright (c) 2016 Peter Flanigan. All rights reserved
230              
231             This program is free software; you can redistribute it and/or modify it
232             under the same terms as Perl itself. See L<perlartistic>
233              
234             This program is distributed in the hope that it will be useful,
235             but WITHOUT WARRANTY; without even the implied warranty of
236             MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE
237              
238             =cut
239              
240             # Local Variables:
241             # mode: perl
242             # tab-width: 3
243             # End:
244             # vim: expandtab shiftwidth=3:
245              
246             __DATA__
247             ___[ t/00report-metadata.t ]___
248             #!perl
249             # Generated by Dist::Zilla::Plugin::Test::ReportMetadata INSERT_VERSION_HERE
250              
251             use strict;
252             use warnings;
253              
254             use File::Spec;
255             use Module::Metadata;
256             use Sys::Hostname;
257              
258             # Instead of importing from Test::More
259             sub diag (;@) {
260             warn @_; return;
261             }
262              
263             sub pass () {
264             print "ok 1\n1..1\n"; return;
265             }
266              
267             # Hide optional CPAN::Meta modules from prereq scanner
268             # and check if they are available
269             my $CPAN_META = 'CPAN::Meta';
270             my $CPAN_META_PRE = 'CPAN::Meta::Prereqs';
271             my @DISPLAY_VARS = grep { $_ ne 'none' } qw( INSERT_DISPLAY_VARS_HERE );
272             my $DO_VERIFY_PREREQS = INSERT_VERIFY_PREREQS_CONFIG; # Verify requirements?
273             my @EXCLUDE = qw( INSERT_EXCLUDED_MODULES_HERE );
274             my $HOST = lc hostname;
275             my @INCLUDE = qw( INSERT_INCLUDED_MODULES_HERE );
276             my $LAX_VERSION_RE = # From $version::LAX
277             qr{(?: undef | (?: (?:[0-9]+) (?: \. | (?:\.[0-9]+) (?:_[0-9]+)? )?
278             |
279             (?:\.[0-9]+) (?:_[0-9]+)?
280             ) | (?:
281             v (?:[0-9]+) (?: (?:\.[0-9]+)+ (?:_[0-9]+)? )?
282             |
283             (?:[0-9]+)? (?:\.[0-9]+){2,} (?:_[0-9]+)?
284             )
285             )}x;
286             my $OSNAME = lc $^O;
287             # Add static prereqs to the included modules list
288             my $STATIC_PREREQS = do './INSERT_DD_FILENAME_HERE';
289              
290             my $diag_env = sub {
291             my $k = shift; my $v = exists $ENV{ $k } ? $ENV{ $k } : 'undef';
292              
293             return diag sprintf " \$%-30s %s\n", $k, $v;
294             };
295             my $max = sub {
296             my $v = shift; $v = ( $_ > $v ) ? $_ : $v for @_; return $v;
297             };
298             my $merge_prereqs = sub {
299             my ($collector, $prereqs) = @_;
300              
301             ref $collector eq $CPAN_META_PRE # CPAN::Meta::Prereqs object
302             and return $collector->with_merged_prereqs
303             ( CPAN::Meta::Prereqs->new( $prereqs ) );
304              
305             for my $phase (keys %{ $prereqs }) { # Raw hashrefs
306             for my $type (keys %{ $prereqs->{ $phase } }) {
307             for my $module (keys %{ $prereqs->{ $phase }{ $type } }) {
308             $collector->{ $phase }{ $type }{ $module }
309             = $prereqs->{ $phase }{ $type }{ $module };
310             }
311             }
312             }
313              
314             return $collector;
315             };
316             my $cpan_meta_ver = "${CPAN_META}->VERSION( '2.120900' )";
317             my $has_cpan_meta = eval "require ${CPAN_META}; ${cpan_meta_ver}"
318             && eval "require ${CPAN_META_PRE}";
319             # Merge all prereqs (either with ::Prereqs or a hashref)
320             my $full_prereqs = $merge_prereqs->
321             ( ( $has_cpan_meta ? $CPAN_META_PRE->new : {} ), $STATIC_PREREQS );
322             # Add dynamic prereqs to the included modules list (if we can)
323             my ($source) = grep { -f } 'MYMETA.json', 'MYMETA.yml';
324              
325             if ($source and $has_cpan_meta) {
326             if (my $meta = eval { CPAN::Meta->load_file( $source ) }) {
327             $full_prereqs = $merge_prereqs->( $full_prereqs, $meta->prereqs );
328             }
329             }
330             else { $source = 'static metadata' }
331              
332             my @full_reports;
333             my @dep_errors;
334             my $req_hash = $has_cpan_meta ? $full_prereqs->as_string_hash : $full_prereqs;
335              
336             for my $mod (@INCLUDE) { # Add static includes into a fake section
337             $req_hash->{other}{modules}{ $mod } = 0;
338             }
339              
340             for my $phase (qw( configure build test runtime develop other )) {
341             $req_hash->{ $phase } or next;
342             $phase eq 'develop' and not $ENV{AUTHOR_TESTING} and next;
343              
344             for my $type ( qw( requires recommends suggests conflicts modules ) ) {
345             $req_hash->{ $phase }{ $type } or next;
346              
347             my $title = (ucfirst $phase).' '.(ucfirst $type);
348             my @reports = [ qw( Module Want Have ) ];
349              
350             for my $mod (sort keys %{ $req_hash->{ $phase }{ $type } }) {
351             $mod eq 'perl' and next; grep { $_ eq $mod } @EXCLUDE and next;
352              
353             my $file = $mod; $file =~ s{ :: }{/}gmx; $file .= '.pm';
354             my ($prefix) = grep { -e File::Spec->catfile( $_, $file ) } @INC;
355             my $want = $req_hash->{ $phase }{ $type }{ $mod };
356              
357             defined $want or $want = 'undef';
358             not $want and $want == 0 and $want = 'any';
359              
360             my $req_string = $want eq 'any'
361             ? 'any version required' : "version '${want}' required";
362              
363             if ($prefix) {
364             my $path = File::Spec->catfile( $prefix, $file );
365             my $info = Module::Metadata->new_from_file( $path );
366             my $have = $info->version;
367              
368             defined $have or $have = 'undef';
369             push @reports, [ $mod, $want, $have ];
370              
371             if ($DO_VERIFY_PREREQS and $has_cpan_meta and $type eq 'requires') {
372             if ($have !~ m{ \A $LAX_VERSION_RE \z }mx) {
373             push @dep_errors,
374             "${mod} version '${have}' cannot be parsed (${req_string})";
375             }
376             elsif ( !$full_prereqs->requirements_for( $phase, $type )->accepts_module( $mod => $have ) ) {
377             push @dep_errors,
378             "${mod} version '${have}' is not in required range '${want}'";
379             }
380             }
381             }
382             else {
383             push @reports, [ $mod, $want, 'missing' ];
384              
385             $DO_VERIFY_PREREQS and $type eq 'requires'
386             and push @dep_errors, "${mod} is not installed (${req_string})";
387             }
388             }
389              
390             if (@reports) {
391             push @full_reports, "=== ${title} ===\n\n";
392              
393             my $ml = $max->( map { length $_->[ 0 ] } @reports );
394             my $wl = $max->( map { length $_->[ 1 ] } @reports );
395             my $hl = $max->( map { length $_->[ 2 ] } @reports );
396              
397             if ($type eq 'modules') {
398             splice @reports, 1, 0, [ '-' x $ml, q(), '-' x $hl ];
399             push @full_reports, map { sprintf " %*s %*s\n", -$ml,
400             $_->[ 0 ], $hl, $_->[ 2 ] } @reports;
401             }
402             else {
403             splice @reports, 1, 0, [ '-' x $ml, '-' x $wl, '-' x $hl ];
404             push @full_reports, map { sprintf " %*s %*s %*s\n", -$ml,
405             $_->[ 0 ], $wl, $_->[ 1 ], $hl,
406             $_->[ 2 ] } @reports;
407             }
408              
409             push @full_reports, "\n";
410             }
411             }
412             }
413              
414             if (@DISPLAY_VARS) {
415             diag "\nOS: ${OSNAME}, Host: ${HOST}\n";
416             diag "\n=== Environment variables ===\n\n";
417              
418             $diag_env->( $_ ) for (@DISPLAY_VARS);
419             }
420              
421             if (@full_reports) {
422             diag "\nVersions for all modules listed in ${source} (including optional ones):\n\n", @full_reports;
423             }
424              
425             if (@dep_errors) {
426             diag join "\n",
427             "\n*** WARNING WARNING WARNING WARNING WARNING WARNING ***\n",
428             "The following REQUIRED prerequisites were not satisfied:\n",
429             @dep_errors, "\n";
430             }
431              
432             pass;
433             exit 0;