| 1 |
use v5.8; |
| 2 |
use strict; |
| 3 |
use warnings; |
| 4 |
package Test::MinimumVersion; |
| 5 |
$Test::MinimumVersion::VERSION = '0.101083'; |
| 6 |
# ABSTRACT: does your code require newer perl than you think? |
| 7 |
use base 'Exporter'; |
| 8 |
|
| 9 |
#pod =head1 SYNOPSIS |
| 10 |
#pod |
| 11 |
#pod Example F<minimum-perl.t>: |
| 12 |
#pod |
| 13 |
#pod #!perl |
| 14 |
#pod use Test::MinimumVersion; |
| 15 |
#pod all_minimum_version_ok('5.008'); |
| 16 |
#pod |
| 17 |
#pod =cut |
| 18 |
|
| 19 |
use CPAN::Meta; |
| 20 |
use File::Find::Rule; |
| 21 |
use File::Find::Rule::Perl; |
| 22 |
use Perl::MinimumVersion 1.32; # numerous bugfies |
| 23 |
use version 0.70; |
| 24 |
|
| 25 |
use Test::Builder; |
| 26 |
@Test::MinimumVersion::EXPORT = qw( |
| 27 |
minimum_version_ok |
| 28 |
all_minimum_version_ok |
| 29 |
all_minimum_version_from_metayml_ok |
| 30 |
all_minimum_version_from_metajson_ok |
| 31 |
all_minimum_version_from_mymetayml_ok |
| 32 |
all_minimum_version_from_mymetajson_ok |
| 33 |
); |
| 34 |
|
| 35 |
sub import { |
| 36 |
my($self) = shift; |
| 37 |
my $pack = caller; |
| 38 |
|
| 39 |
my $Test = Test::Builder->new; |
| 40 |
|
| 41 |
$Test->exported_to($pack); |
| 42 |
$Test->plan(@_); |
| 43 |
|
| 44 |
$self->export_to_level(1, $self, @Test::MinimumVersion::EXPORT); |
| 45 |
} |
| 46 |
|
| 47 |
sub _objectify_version { |
| 48 |
my ($version) = @_; |
| 49 |
$version = eval { $version->isa('version') } |
| 50 |
? $version |
| 51 |
: version->new($version); |
| 52 |
} |
| 53 |
|
| 54 |
#pod =func minimum_version_ok |
| 55 |
#pod |
| 56 |
#pod minimum_version_ok($file, $version); |
| 57 |
#pod |
| 58 |
#pod This test passes if the given file does not seem to require any version of perl |
| 59 |
#pod newer than C<$version>, which may be given as a version string or a version |
| 60 |
#pod object. |
| 61 |
#pod |
| 62 |
#pod =cut |
| 63 |
|
| 64 |
sub minimum_version_ok { |
| 65 |
my ($file, $version) = @_; |
| 66 |
|
| 67 |
my $Test = Test::Builder->new; |
| 68 |
|
| 69 |
$version = _objectify_version($version); |
| 70 |
|
| 71 |
my $pmv = Perl::MinimumVersion->new($file); |
| 72 |
|
| 73 |
unless (defined $pmv) { |
| 74 |
$Test->ok(0, $file); |
| 75 |
$Test->diag( |
| 76 |
"$file could not be parsed: " . PPI::Document->errstr |
| 77 |
); |
| 78 |
return; |
| 79 |
} |
| 80 |
|
| 81 |
my $explicit_minimum = $pmv->minimum_explicit_version || 0; |
| 82 |
my $minimum = $pmv->minimum_syntax_version($explicit_minimum) || 0; |
| 83 |
|
| 84 |
my $is_syntax = 1 |
| 85 |
if $minimum and $minimum > $explicit_minimum; |
| 86 |
|
| 87 |
$minimum = $explicit_minimum |
| 88 |
if $explicit_minimum and $explicit_minimum > $minimum; |
| 89 |
|
| 90 |
my %min = $pmv->version_markers; |
| 91 |
|
| 92 |
if ($minimum <= $version) { |
| 93 |
$Test->ok(1, $file); |
| 94 |
} else { |
| 95 |
$Test->ok(0, $file); |
| 96 |
$Test->diag( |
| 97 |
"$file requires $minimum " |
| 98 |
. ($is_syntax ? 'due to syntax' : 'due to explicit requirement') |
| 99 |
); |
| 100 |
|
| 101 |
if ($is_syntax and my $markers = $min{ $minimum }) { |
| 102 |
$Test->diag("version markers for $minimum:"); |
| 103 |
$Test->diag("- $_ ") for @$markers; |
| 104 |
} |
| 105 |
} |
| 106 |
} |
| 107 |
|
| 108 |
#pod =func all_minimum_version_ok |
| 109 |
#pod |
| 110 |
#pod all_minimum_version_ok($version, \%arg); |
| 111 |
#pod |
| 112 |
#pod Given either a version string or a L<version> object, this routine produces a |
| 113 |
#pod test plan (if there is no plan) and tests each relevant file with |
| 114 |
#pod C<minimum_version_ok>. |
| 115 |
#pod |
| 116 |
#pod Relevant files are found by L<File::Find::Rule::Perl>. |
| 117 |
#pod |
| 118 |
#pod C<\%arg> is optional. Valid arguments are: |
| 119 |
#pod |
| 120 |
#pod paths - in what paths to look for files; defaults to (bin, script, t, lib, |
| 121 |
#pod xt/smoke, and any .pm or .PL files in the current working |
| 122 |
#pod directory) if it contains files, they will be checked |
| 123 |
#pod no_plan - do not plan the tests about to be run |
| 124 |
#pod skip - files to skip; this can be useful in weird cases like gigantic |
| 125 |
#pod files, files falsely detected as Perl, or code that uses |
| 126 |
#pod a source filter; this should be an arrayref of filenames |
| 127 |
#pod |
| 128 |
#pod =cut |
| 129 |
|
| 130 |
sub all_minimum_version_ok { |
| 131 |
my ($version, $arg) = @_; |
| 132 |
$arg ||= {}; |
| 133 |
$arg->{paths} ||= [ |
| 134 |
qw(bin script lib t xt/smoke), |
| 135 |
glob("*.pm"), |
| 136 |
glob("*.PL"), |
| 137 |
]; |
| 138 |
|
| 139 |
$arg->{skip} ||= []; |
| 140 |
|
| 141 |
my $Test = Test::Builder->new; |
| 142 |
|
| 143 |
$version = _objectify_version($version); |
| 144 |
|
| 145 |
my @perl_files; |
| 146 |
for my $path (@{ $arg->{paths} }) { |
| 147 |
if (-f $path and -s $path) { |
| 148 |
push @perl_files, $path; |
| 149 |
} elsif (-d $path) { |
| 150 |
push @perl_files, File::Find::Rule->perl_file->in($path); |
| 151 |
} |
| 152 |
} |
| 153 |
|
| 154 |
my %skip = map {; $_ => 1 } @{ $arg->{skip} }; |
| 155 |
@perl_files = grep {; ! $skip{$_} } @perl_files; |
| 156 |
|
| 157 |
unless ($Test->has_plan or $arg->{no_plan}) { |
| 158 |
$Test->plan(tests => scalar @perl_files); |
| 159 |
} |
| 160 |
|
| 161 |
minimum_version_ok($_, $version) for @perl_files; |
| 162 |
} |
| 163 |
|
| 164 |
#pod =func all_minimum_version_from_metayml_ok |
| 165 |
#pod |
| 166 |
#pod all_minimum_version_from_metayml_ok(\%arg); |
| 167 |
#pod |
| 168 |
#pod This routine checks F<META.yml> for an entry in F<requires> for F<perl>. If no |
| 169 |
#pod META.yml file or no perl version is found, all tests are skipped. If a version |
| 170 |
#pod is found, the test proceeds as if C<all_minimum_version_ok> had been called |
| 171 |
#pod with that version. |
| 172 |
#pod |
| 173 |
#pod =cut |
| 174 |
|
| 175 |
sub __version_from_meta { |
| 176 |
my ($fn) = @_; |
| 177 |
|
| 178 |
my $meta = CPAN::Meta->load_file($fn, { lazy_validation => 1 })->as_struct; |
| 179 |
my $version = $meta->{prereqs}{runtime}{requires}{perl}; |
| 180 |
} |
| 181 |
|
| 182 |
sub __from_meta { |
| 183 |
my ($fn, $arg) = @_; |
| 184 |
$arg ||= {}; |
| 185 |
|
| 186 |
my $Test = Test::Builder->new; |
| 187 |
|
| 188 |
$Test->plan(skip_all => "$fn could not be found") |
| 189 |
unless -f $fn and -r _; |
| 190 |
|
| 191 |
$Test->plan(skip_all => "no minimum perl version could be determined") |
| 192 |
unless my $version = __version_from_meta($fn); |
| 193 |
|
| 194 |
all_minimum_version_ok($version, $arg); |
| 195 |
} |
| 196 |
|
| 197 |
sub all_minimum_version_from_metayml_ok { |
| 198 |
__from_meta('META.yml', @_); |
| 199 |
} |
| 200 |
|
| 201 |
#pod =func all_minimum_version_from_metajson_ok |
| 202 |
#pod |
| 203 |
#pod all_minimum_version_from_metajson_ok(\%arg); |
| 204 |
#pod |
| 205 |
#pod This routine checks F<META.json> for an entry in F<requires> for F<perl>. If |
| 206 |
#pod no META.json file or no perl version is found, all tests are skipped. If a |
| 207 |
#pod version is found, the test proceeds as if C<all_minimum_version_ok> had been |
| 208 |
#pod called with that version. |
| 209 |
#pod |
| 210 |
#pod =cut |
| 211 |
|
| 212 |
sub all_minimum_version_from_metajson_ok { __from_meta('META.json', @_); } |
| 213 |
|
| 214 |
#pod =func all_minimum_version_from_mymetayml_ok |
| 215 |
#pod |
| 216 |
#pod all_minimum_version_from_mymetayml_ok(\%arg); |
| 217 |
#pod |
| 218 |
#pod This routine checks F<MYMETA.yml> for an entry in F<requires> for F<perl>. If |
| 219 |
#pod no MYMETA.yml file or no perl version is found, all tests are skipped. If a |
| 220 |
#pod version is found, the test proceeds as if C<all_minimum_version_ok> had been |
| 221 |
#pod called with that version. |
| 222 |
#pod |
| 223 |
#pod =cut |
| 224 |
|
| 225 |
sub all_minimum_version_from_mymetayml_ok { __from_meta('MYMETA.yml', @_); } |
| 226 |
|
| 227 |
#pod =func all_minimum_version_from_mymetajson_ok |
| 228 |
#pod |
| 229 |
#pod all_minimum_version_from_mymetajson_ok(\%arg); |
| 230 |
#pod |
| 231 |
#pod This routine checks F<MYMETA.json> for an entry in F<requires> for F<perl>. If |
| 232 |
#pod no MYMETA.json file or no perl version is found, all tests are skipped. If a |
| 233 |
#pod version is found, the test proceeds as if C<all_minimum_version_ok> had been |
| 234 |
#pod called with that version. |
| 235 |
#pod |
| 236 |
#pod =cut |
| 237 |
|
| 238 |
sub all_minimum_version_from_mymetajson_ok { __from_meta('MYMETA.json', @_); } |
| 239 |
|
| 240 |
1; |
| 241 |
|
| 242 |
__END__ |
| 243 |
|
| 244 |
=pod |
| 245 |
|
| 246 |
=encoding UTF-8 |
| 247 |
|
| 248 |
=head1 NAME |
| 249 |
|
| 250 |
Test::MinimumVersion - does your code require newer perl than you think? |
| 251 |
|
| 252 |
=head1 VERSION |
| 253 |
|
| 254 |
version 0.101083 |
| 255 |
|
| 256 |
=head1 SYNOPSIS |
| 257 |
|
| 258 |
Example F<minimum-perl.t>: |
| 259 |
|
| 260 |
#!perl |
| 261 |
use Test::MinimumVersion; |
| 262 |
all_minimum_version_ok('5.008'); |
| 263 |
|
| 264 |
=head1 PERL VERSION |
| 265 |
|
| 266 |
This library should run on perls released even an extremely long time ago. It |
| 267 |
should work on any version of perl released in the last ten years. |
| 268 |
|
| 269 |
Although it may work on older versions of perl, no guarantee is made that the |
| 270 |
minimum required version will not be increased. The version may be increased |
| 271 |
for any reason, and there is no promise that patches will be accepted to lower |
| 272 |
the minimum required perl. |
| 273 |
|
| 274 |
=head1 FUNCTIONS |
| 275 |
|
| 276 |
=head2 minimum_version_ok |
| 277 |
|
| 278 |
minimum_version_ok($file, $version); |
| 279 |
|
| 280 |
This test passes if the given file does not seem to require any version of perl |
| 281 |
newer than C<$version>, which may be given as a version string or a version |
| 282 |
object. |
| 283 |
|
| 284 |
=head2 all_minimum_version_ok |
| 285 |
|
| 286 |
all_minimum_version_ok($version, \%arg); |
| 287 |
|
| 288 |
Given either a version string or a L<version> object, this routine produces a |
| 289 |
test plan (if there is no plan) and tests each relevant file with |
| 290 |
C<minimum_version_ok>. |
| 291 |
|
| 292 |
Relevant files are found by L<File::Find::Rule::Perl>. |
| 293 |
|
| 294 |
C<\%arg> is optional. Valid arguments are: |
| 295 |
|
| 296 |
paths - in what paths to look for files; defaults to (bin, script, t, lib, |
| 297 |
xt/smoke, and any .pm or .PL files in the current working |
| 298 |
directory) if it contains files, they will be checked |
| 299 |
no_plan - do not plan the tests about to be run |
| 300 |
skip - files to skip; this can be useful in weird cases like gigantic |
| 301 |
files, files falsely detected as Perl, or code that uses |
| 302 |
a source filter; this should be an arrayref of filenames |
| 303 |
|
| 304 |
=head2 all_minimum_version_from_metayml_ok |
| 305 |
|
| 306 |
all_minimum_version_from_metayml_ok(\%arg); |
| 307 |
|
| 308 |
This routine checks F<META.yml> for an entry in F<requires> for F<perl>. If no |
| 309 |
META.yml file or no perl version is found, all tests are skipped. If a version |
| 310 |
is found, the test proceeds as if C<all_minimum_version_ok> had been called |
| 311 |
with that version. |
| 312 |
|
| 313 |
=head2 all_minimum_version_from_metajson_ok |
| 314 |
|
| 315 |
all_minimum_version_from_metajson_ok(\%arg); |
| 316 |
|
| 317 |
This routine checks F<META.json> for an entry in F<requires> for F<perl>. If |
| 318 |
no META.json file or no perl version is found, all tests are skipped. If a |
| 319 |
version is found, the test proceeds as if C<all_minimum_version_ok> had been |
| 320 |
called with that version. |
| 321 |
|
| 322 |
=head2 all_minimum_version_from_mymetayml_ok |
| 323 |
|
| 324 |
all_minimum_version_from_mymetayml_ok(\%arg); |
| 325 |
|
| 326 |
This routine checks F<MYMETA.yml> for an entry in F<requires> for F<perl>. If |
| 327 |
no MYMETA.yml file or no perl version is found, all tests are skipped. If a |
| 328 |
version is found, the test proceeds as if C<all_minimum_version_ok> had been |
| 329 |
called with that version. |
| 330 |
|
| 331 |
=head2 all_minimum_version_from_mymetajson_ok |
| 332 |
|
| 333 |
all_minimum_version_from_mymetajson_ok(\%arg); |
| 334 |
|
| 335 |
This routine checks F<MYMETA.json> for an entry in F<requires> for F<perl>. If |
| 336 |
no MYMETA.json file or no perl version is found, all tests are skipped. If a |
| 337 |
version is found, the test proceeds as if C<all_minimum_version_ok> had been |
| 338 |
called with that version. |
| 339 |
|
| 340 |
=head1 AUTHOR |
| 341 |
|
| 342 |
Ricardo Signes |
| 343 |
|
| 344 |
=head1 CONTRIBUTORS |
| 345 |
|
| 346 |
=for stopwords Ricardo SIGNES Signes Steve Hay |
| 347 |
|
| 348 |
=over 4 |
| 349 |
|
| 350 |
=item * |
| 351 |
|
| 352 |
Ricardo SIGNES <[email protected]> |
| 353 |
|
| 354 |
=item * |
| 355 |
|
| 356 |
Ricardo Signes <[email protected]> |
| 357 |
|
| 358 |
=item * |
| 359 |
|
| 360 |
Steve Hay <[email protected]> |
| 361 |
|
| 362 |
=back |
| 363 |
|
| 364 |
=head1 COPYRIGHT AND LICENSE |
| 365 |
|
| 366 |
This software is copyright (c) 2007 by Ricardo Signes. |
| 367 |
|
| 368 |
This is free software; you can redistribute it and/or modify it under |
| 369 |
the same terms as the Perl 5 programming language system itself. |
| 370 |
|
| 371 |
=cut |