blib/lib/Test/Dist/VersionSync.pm

filter: Statementstmt 100% 78 / 78 Branchbran 81.2% 26 / 32 Conditioncond 60.0% 12 / 20 MC/DCmcdc 38.4% 5 / 13 Subroutinesub 100% 10 / 10 Podpod 100% 1 / 1 totaltotal 85.7% 132 / 154 CC 25 SCAR 21.2
CC
25
Cov
89%
_get_modules_from_manifest
13.4
ok_versions
9.1
__ANON__
4.0
CRAP 8.3
↑ Summary
1 package Test::Dist::VersionSync;
2  
3 9 ▶use strict;
Subroutine BEGIN: called CC=1 SCAR=0.0
4 9 ▶use warnings;
Subroutine BEGIN: called CC=1 SCAR=0.0
5  
6 9 ▶use Data::Dumper;
Subroutine BEGIN: called CC=1 SCAR=0.0
7 9 ▶use Test::More;
Subroutine BEGIN: called CC=1 SCAR=0.0
8  
9  
10 =head1 NAME
11  
12 Test::Dist::VersionSync - Verify that all the modules in a distribution have the same version number.
13  
14  
15 =head1 VERSION
16  
17 Version 1.2.0
18  
19 =cut
20  
21 9 our $VERSION = '1.2.0';
22  
23  
24 =head1 SYNOPSIS
25  
26 use Test::Dist::VersionSync;
27 Test::Dist::VersionSync::ok_versions();
28  
29  
30 =head1 USE AS A TEST FILE
31  
32 The most common use should be to add a module_versions.t file to your tests directory for a given distribution, with the following content:
33  
34 #!perl -T
35  
36 use strict;
37 use warnings;
38  
39 use Test::More;
40  
41 # Ensure a recent version of Test::Dist::VersionSync
42 my $version_min = '1.0.1';
43 eval "use Test::Dist::VersionSync $version_min";
44 plan( skip_all => "Test::Dist::VersionSync $version_min required for testing module versions in the distribution." )
45 if $@;
46  
47 Test::Dist::VersionSync::ok_versions();
48  
49 =head1 FUNCTIONS
50  
51 =head2 ok_versions()
52  
53 Verify that all the Perl modules in the distribution have the same version
54 number.
55  
56 # Default, use MANIFEST and MANIFEST.SKIP to find out what modules exist.
57 ok_versions();
58  
59 # Optional, specify a list of modules to check for identical versions.
60 ok_versions(
61 modules =>
62 [
63 'Test::Module1',
64 'Test::Module2',
65 'Test::Module3',
66 ],
67 );
68  
69 =cut
70  
71 sub ok_versions
72 6 ▶{
Subroutine ok_versions: called CC=9 SCAR=22.1
Pod documented
73 6 my ( %args ) = @_;
74 6 my $modules = delete( $args{'modules'} );
75 6 my $return = 1;
76  
77 # Find out via Test::Builder if a plan has been declared, otherwise we'll
78 # declare our own.
79 6 my $builder = Test::More->builder();
80 6 my $plan_declared = $builder->has_plan();
81  
82 # If a list of files was passed, verify that the argument is an arrayref.
83 # Otherwise, load the files from MANIFEST and MANIFEST.SKIP.
84 6 ▶ if ( defined( $modules) )
Branch: 1 branch
BranchTrueFalseTotal
if (defined $modules) { } 2 4 6
85 {
86 2 ▶ Test::More::plan( tests => 3 )
Branch: 1 branch
BranchTrueFalseTotal
unless $plan_declared 0 2 2
87 unless $plan_declared;
88  
89 2 ▶ $return = Test::More::isa_ok(
Condition33%1/3
&Test::More::isa_ok($modules, 'ARRAY', 'modules list') && $return
!ll&&!rl&&r
002
MC/DC0%0/2
&Test::More::isa_ok($modules, 'ARRAY', 'modules list') && $return
&Test::More::isa_ok($modules, 'ARRAY', 'modules list') $return
Truth table33%1/3
A && B
A= &Test::More::isa_ok($modules, 'ARRAY', 'modules list')B= $return
ABresult
0X0
100
111
90 $modules,
91 'ARRAY',
92 'modules list',
93 ) && $return;
94 }
95 else
96 {
97 4 ▶ Test::More::plan( tests => 5 )
Branch: 1 branch
BranchTrueFalseTotal
unless $plan_declared 0 4 4
98 unless $plan_declared;
99  
100 4 $modules = _get_modules_from_manifest();
101 }
102  
103 # If we have modules, check their versions.
104 SKIP:
105 {
106 6 ▶ Test::More::skip(
Branch: 1 branch
BranchTrueFalseTotal
if scalar @$modules == 0 2 4 6
107 'No module found in the distribution.',
108 2,
109 ) if scalar( @$modules ) == 0;
110  
111 4 my $versions = {};
112 $return = Test::More::subtest(
113 'Retrieve versions for all modules listed.',
114 sub
115 4 ▶ {
Subroutine __ANON__: called CC=4 SCAR=13.9
116 4 Test::More::plan( tests => scalar( @$modules ) * 2 );
117  
118 4 foreach my $module ( @$modules )
119 {
120 8 ▶ Test::More::use_ok( $module );
Subroutine BEGIN: called CC=1 SCAR=0.0
Subroutine BEGIN: called CC=1 SCAR=0.0
121  
122 8 my $version = $module->VERSION();
123 8 my $version_declared = Test::More::ok(
124 defined( $version ),
125 "Module $module declares a version.",
126 );
127  
128 8 ▶ $version = '(undef)'
Branch: 1 branch
BranchTrueFalseTotal
unless $version_declared 1 7 8
129 unless $version_declared;
130  
131 8 ▶ $versions->{ $version } ||= [];
Condition100%2/2
$versions->{$version} ||= []
l!l
26
MC/DC100%1/1
$versions->{$version} ||= []
$versions->{$version}
Truth table100%2/2
A
A= $versions->{$version}
Aresult
11
00
132 8 push( @{ $versions->{ $version } }, $module );
133 }
134 }
135 4 ▶ ) && $return;
Condition66%2/3
Test::More::subtest('Retrieve versions for all modules listed.', sub { Test::More::plan('tests', scalar(@$modules) * 2); foreach my $module (@$modules) { &Test::More::use_ok($module); my $version = $module->VERSION; my $version_declared = &Test::More::ok(defined $version, "Module $module declares a version."); $version = '(undef)' unless $version_declared; $versions->{$version} ||= []; push @{$$versions{$version};}, $module; } ; } ) && $return
!ll&&!rl&&r
103
MC/DC50%1/2
Test::More::subtest('Retrieve versions for all modules listed.', sub { Test::More::plan('tests', scalar(@$modules) * 2); foreach my $module (@$modules) { &Test::More::use_ok($module); my $version = $module->VERSION; my $version_declared = &Test::More::ok(defined $version, "Module $module declares a version."); $version = '(undef)' unless $version_declared; $versions->{$version} ||= []; push @{$$versions{$version};}, $module; } ; } ) && $return
Test::More::subtest('Retrieve versions for all modules listed.', sub { Test::More::plan('tests', scalar(@$modules) * 2); foreach my $module (@$modules) { &Test::More::use_ok($module); my $version = $module->VERSION; my $version_declared = &Test::More::ok(defined $version, "Module $module declares a version."); $version = '(undef)' unless $version_declared; $versions->{$version} ||= []; push @{$$versions{$version};}, $module; } ; } ) $return
Truth table66%2/3
A && B
A= Test::More::subtest('Retrieve versions for all modules listed.', sub { Test::More::plan('tests', scalar(@$modules) * 2); foreach my $module (@$modules) { &Test::More::use_ok($module); my $version = $module->VERSION; my $version_declared = &Test::More::ok(defined $version, "Module $module declares a version."); $version = '(undef)' unless $version_declared; $versions->{$version} ||= []; push @{$$versions{$version};}, $module; } ; } )B= $return
ABresult
0X0
100
111
136  
137 4 my $has_only_one_version = is(
138 scalar( keys %$versions ),
139 1,
140 'The modules declare only one version.',
141 );
142 4 ▶ diag( 'Versions and the modules they were found in: ' . Dumper( $versions ) )
Branch: 1 branch
BranchTrueFalseTotal
unless $has_only_one_version 2 2 4
143 unless $has_only_one_version;
144 4 ▶ $return = $has_only_one_version && $has_only_one_version;
Condition66%2/3
$has_only_one_version && $has_only_one_version
!ll&&!rl&&r
202
MC/DC50%1/2
$has_only_one_version && $has_only_one_version
$has_only_one_version $has_only_one_version
Truth table66%2/3
A && B
A= $has_only_one_versionB= $has_only_one_version
ABresult
0X0
100
111
145  
146 }
147  
148 6 return $return;
149 }
150  
151  
152 =head2 import()
153  
154 Import a test plan. This uses the regular Test::More plan options.
155  
156 use Test::Dist::VersionSync tests => 4;
157  
158 ok_versions();
159  
160 Test::Dist::VersionSync also detects if Test::More was already used with a test
161 plan declared and will piggyback on it. For example:
162  
163 use Test::More tests => 2;
164 use Test::Dist::VersionSync;
165  
166 ok( 1, 'Some Test' );
167 ok_versions();
168  
169 =cut
170  
171 sub import
172 9 ▶{
Subroutine import: called CC=2 SCAR=7.1
173 9 my ( $self, %test_plan ) = @_;
174  
175 9 ▶ Test::More::plan( %test_plan )
Branch: 1 branch
BranchTrueFalseTotal
if scalar keys %test_plan != 0 0 9 9
176 if scalar( keys %test_plan ) != 0;
177  
178 9 return 1;
179 }
180  
181  
182 =begin _private
183  
184 =head1 INTERNAL FUNCTIONS
185  
186 =head2 _get_modules_from_manifest
187  
188 Retrieve an arrayref of modules using the MANIFEST file at the root of the
189 distribution. IF MANIFEST.SKIP is present, its list of exclusions is used
190 to filter out modules to verify.
191  
192 my $modules = _get_modules_from_manifest();
193  
194 =end _private
195  
196 =cut
197  
198 sub _get_modules_from_manifest
199 6 ▶{
Subroutine _get_modules_from_manifest: called CC=13 SCAR=25.9
200 # Gather a list of exclusion patterns for files listed in MANIFEST.
201 6 my $excluded_patterns;
202 6 ▶ if ( -e 'MANIFEST.SKIP' )
Branch: 1 branch
BranchTrueFalseTotal
if (-e 'MANIFEST.SKIP') { } 1 5 6
203 {
204 1 ▶ my $opened_manifest_skip = Test::More::ok(
Condition33%1/3
&Test::More::ok(open(my $MANIFESTSKIP, '<', 'MANIFEST.SKIP'), 'Retrieve MANIFEST.SKIP file.') || diag("Failed to open < MANIFEST.SKIP file: $!.")
l!l&&r!l&&!r
100
MC/DC0%0/2
&Test::More::ok(open(my $MANIFESTSKIP, '<', 'MANIFEST.SKIP'), 'Retrieve MANIFEST.SKIP file.') || diag("Failed to open < MANIFEST.SKIP file: $!.")
&Test::More::ok(open(my $MANIFESTSKIP, '<', 'MANIFEST.SKIP'), 'Retrieve MANIFEST.SKIP file.') diag("Failed to open < MANIFEST.SKIP file: $!.")
Truth table33%1/3
A || B
A= &Test::More::ok(open(my $MANIFESTSKIP, '<', 'MANIFEST.SKIP'), 'Retrieve MANIFEST.SKIP file.')B= diag("Failed to open < MANIFEST.SKIP file: $!.")
ABresult
1X1
011
000
205 open( my $MANIFESTSKIP, '<', 'MANIFEST.SKIP' ),
206 'Retrieve MANIFEST.SKIP file.',
207 ) || diag( "Failed to open < MANIFEST.SKIP file: $!." );
208  
209 1 ▶ if ( $opened_manifest_skip )
Branch: 1 branch
BranchTrueFalseTotal
if ($opened_manifest_skip) 1 0 1
210 {
211 1 my $exclusions = [];
212 1 ▶ while ( my $pattern = <$MANIFESTSKIP> )
Branch: 1 branch
BranchTrueFalseTotal
if (defined(my $pattern = readline $MANIFESTSKIP)) 3 1 4
213 {
214 3 chomp( $pattern );
215 3 push( @$exclusions, $pattern );
216 }
217 1 close( $MANIFESTSKIP );
218  
219 1 ▶ $excluded_patterns = '(' . join( '|', @$exclusions ) . ')'
Branch: 1 branch
BranchTrueFalseTotal
if scalar @$exclusions != 0 1 0 1
220 if scalar( @$exclusions ) != 0;
221 }
222 }
223 else
224 {
225 5 Test::More::ok(
226 1,
227 'No MANIFEST.SKIP found, skipping.',
228 );
229 }
230  
231 # Make sure that there is a MANIFEST file at the root of the distribution,
232 # before we even open it.
233 6 my $manifest_exists = Test::More::ok(
234 -e 'MANIFEST',
235 'The MANIFEST file is present at the root of the distribution.',
236 );
237  
238 # Retrieve the list of modules in MANIFEST.
239 6 my $modules = [];
240 SKIP:
241 {
242 6 ▶ Test::More::skip(
Branch: 1 branch
BranchTrueFalseTotal
unless $manifest_exists 1 5 6
243 'MANIFEST is missing, cannot retrieve list of files.',
244 1,
245 ) unless $manifest_exists;
246  
247 5 ▶ my $opened_manifest = Test::More::ok(
Condition33%1/3
&Test::More::ok(open(my $MANIFEST, '<', 'MANIFEST'), 'Retrieve MANIFEST file.') || diag("Failed to open < MANIFEST file: $!.")
l!l&&r!l&&!r
500
MC/DC0%0/2
&Test::More::ok(open(my $MANIFEST, '<', 'MANIFEST'), 'Retrieve MANIFEST file.') || diag("Failed to open < MANIFEST file: $!.")
&Test::More::ok(open(my $MANIFEST, '<', 'MANIFEST'), 'Retrieve MANIFEST file.') diag("Failed to open < MANIFEST file: $!.")
Truth table33%1/3
A || B
A= &Test::More::ok(open(my $MANIFEST, '<', 'MANIFEST'), 'Retrieve MANIFEST file.')B= diag("Failed to open < MANIFEST file: $!.")
ABresult
1X1
011
000
248 open( my $MANIFEST, '<', 'MANIFEST' ),
249 'Retrieve MANIFEST file.',
250 ) || diag( "Failed to open < MANIFEST file: $!." );
251  
252 5 ▶ if ( $opened_manifest )
Branch: 1 branch
BranchTrueFalseTotal
if ($opened_manifest) 5 0 5
253 {
254 5 ▶ while ( my $file = <$MANIFEST> )
Branch: 1 branch
BranchTrueFalseTotal
if (defined(my $file = readline $MANIFEST)) 72 5 77
255 {
256 72 chomp( $file );
257 72 ▶ next if defined( $excluded_patterns ) && $file =~ /$excluded_patterns/;
Branch: 1 branch
BranchTrueFalseTotal
if defined $excluded_patterns and $file =~ /$excluded_patterns/ 2 70 72
Condition100%3/3
defined $excluded_patterns and $file =~ /$excluded_patterns/
!ll&&!rl&&r
56142
MC/DC100%2/2
defined $excluded_patterns and $file =~ /$excluded_patterns/
defined $excluded_patterns $file =~ /$excluded_patterns/
Truth table100%3/3
A and B
A= defined $excluded_patternsB= $file =~ /$excluded_patterns/
ABresult
0X0
100
111
258 70 ▶ next unless $file =~ m/^lib[\\\/](.*)\.pm$/;
Branch: 1 branch
BranchTrueFalseTotal
unless $file =~ m[^lib[\\/](.*)\.pm$] 60 10 70
259  
260 10 my $module = $1;
261 10 $module =~ s/[\\\/]/::/g;
262 10 push( @$modules, $module );
263 }
264 5 close( $MANIFEST );
265 }
266 }
267  
268 6 return $modules;
269 }
270  
271  
272 =head1 BUGS
273  
274 Please report any bugs or feature requests through the web interface at
275 L<https://github.com/guillaumeaubert/Test-Dist-VersionSync/issues/new>.
276 I will be notified, and then you'll automatically be notified of progress on
277 your bug as I make changes.
278  
279  
280 =head1 SUPPORT
281  
282 You can find documentation for this module with the perldoc command.
283  
284 perldoc Test::Dist::VersionSync
285  
286  
287 You can also look for information at:
288  
289 =over
290  
291 =item * GitHub's request tracker
292  
293 L<https://github.com/guillaumeaubert/Test-Dist-VersionSync/issues>
294  
295 =item * AnnoCPAN: Annotated CPAN documentation
296  
297 L<http://annocpan.org/dist/test-dist-versionsync>
298  
299 =item * CPAN Ratings
300  
301 L<http://cpanratings.perl.org/d/test-dist-versionsync>
302  
303 =item * MetaCPAN
304  
305 L<https://metacpan.org/release/Test-Dist-VersionSync>
306  
307 =back
308  
309  
310 =head1 AUTHOR
311  
312 L<Guillaume Aubert|https://metacpan.org/author/AUBERTG>,
313 C<< <aubertg at cpan.org> >>.
314  
315  
316 =head1 COPYRIGHT & LICENSE
317  
318 Copyright 2012-2017 Guillaume Aubert.
319  
320 This code is free software; you can redistribute it and/or modify it under the
321 same terms as Perl 5 itself.
322  
323 This program is distributed in the hope that it will be useful, but WITHOUT ANY
324 WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS FOR A
325 PARTICULAR PURPOSE. See the LICENSE file for more details.
326  
327 =cut
328  
329 9 1;
↑ Summary