Skip to content

Commit

Permalink
Add support for future MYMETA.yml files
Browse files Browse the repository at this point in the history
If a MYMETA.yml file exists, it take precedence over META.yml and
is assumed to be the end-result of dynamic configuration, thus
requirements specified there are always accepted.

Also added explicit testing for $dist->read_yaml()
  • Loading branch information
xdg committed Mar 30, 2009
1 parent deb5a68 commit f2c9af5
Show file tree
Hide file tree
Showing 4 changed files with 129 additions and 14 deletions.
12 changes: 9 additions & 3 deletions lib/CPAN/Distribution.pm
Original file line number Diff line number Diff line change
Expand Up @@ -2699,7 +2699,10 @@ sub read_yaml {
$CPAN::Frontend->mywarn("Warning: cannot determine META.yml without a build_dir.\n");
return;
}
my $yaml = File::Spec->catfile($build_dir,"META.yml");
# if MYMETA.yml exists, that takes precedence over META.yml
my $meta = File::Spec->catfile($build_dir,"META.yml");
my $mymeta = File::Spec->catfile($build_dir,"MYMETA.yml");
my $yaml = -f $mymeta ? $mymeta : $meta;
$self->debug("yaml[$yaml]") if $CPAN::DEBUG;
return unless -f $yaml;
eval { $self->{yaml_content} = CPAN->_yaml_loadfile($yaml)->[0]; };
Expand All @@ -2718,8 +2721,11 @@ sub read_yaml {
$self->{yaml_content} = +{};
}
}
if (not exists $self->{yaml_content}{dynamic_config}
or $self->{yaml_content}{dynamic_config}
# MYMETA.yml is not dynamic by definition
if ( $yaml ne $mymeta &&
( not exists $self->{yaml_content}{dynamic_config}
or $self->{yaml_content}{dynamic_config}
)
) {
$self->{yaml_content} = undef;
}
Expand Down
106 changes: 95 additions & 11 deletions t/41distribution.t
Original file line number Diff line number Diff line change
Expand Up @@ -4,8 +4,12 @@

use strict;

use Cwd qw(cwd);
use File::Copy qw(cp);
use File::Path qw(rmtree mkpath);
use File::Temp qw(tempdir);
use File::Spec::Functions qw/catdir catfile/;
use File::Basename qw/basename/;

use lib "inc";
use lib "t";
Expand All @@ -18,26 +22,28 @@ local_utils::prepare_dot_cpan();
END{ local_utils::cleanup_dot_cpan(); }

use Test::More;
use File::Basename qw/basename/;

my @tarball_suffixes = qw(
.tgz
.tbz
.tar.gz
.tar.bz2
.tar.Z
.zip
);

my (@tarball_suffixes, @meta_yml_tests); # defined later in BEGIN blocks

plan tests => 1 + @tarball_suffixes;
plan tests => 1 + @tarball_suffixes + 3 * @meta_yml_tests;

require_ok( "CPAN" );

#--------------------------------------------------------------------------#
# base_id() testing
#--------------------------------------------------------------------------#

BEGIN {
@tarball_suffixes = qw(
.tgz
.tbz
.tar.gz
.tar.bz2
.tar.Z
.zip
);
}

{
my $dist_base = "Bogus-Module-1.234";
for my $s ( @tarball_suffixes ) {
Expand All @@ -48,7 +54,85 @@ require_ok( "CPAN" );
}
}

#--------------------------------------------------------------------------#
# read_meta() testing
#--------------------------------------------------------------------------#

BEGIN {
@meta_yml_tests = (
{
label => 'no META.yml',
copies => [],
requires => undef,
},
{
label => 'dynamic META.yml',
copies => [ 'META-dynamic.yml', 'META.yml' ],
requires => undef,
},
{
label => 'non-dynamic META.yml',
copies => [ 'META-static.yml', 'META.yml' ],
requires => { 'File::Spec' => 0.87 },
},
{
label => 'dynamic META.yml plus MYMETA.yml',
copies => [
'META-dynamic.yml', 'META.yml',
'META-dynamic.yml', 'MYMETA.yml',
],
requires => { 'File::Spec' => 0.87 },
},
);
}

{
for my $case ( @meta_yml_tests ) {
my $yaml;
my $label = $case->{label};
my $tempdir = tempdir( "t/41distributionXXXX", CLEANUP => 1 );

# dummy distribution
my $dist = CPAN::Distribution->new(
ID => "D/DA/DAGOLDEN/Bogus-Module-1.234"
);
$dist->{build_dir} = $tempdir;

# copy files
if ( $case->{copies} ) {
while (@{$case->{copies}}) {
my ($from, $to) = splice(@{$case->{copies}},0,2);
cp catfile( qw/t data/, $from) => catfile($tempdir, $to);
}
}

# check read_yaml
$yaml = $dist->read_yaml;
if ( defined $case->{requires} ) {
my $type = ref $yaml;
is( $type, 'HASH', "$label\: read_yaml returns HASH ref" );
is( ref $dist->read_yaml, $type, "$label\: repeat read_yaml is same" );
if ( $type ) {
my $mismatch = 0;
for my $k ( keys %{ $case->{requires} } ) {
$mismatch++ unless $yaml->{requires}{$k} == $case->{requires}{$k};
}
ok( $mismatch == 0, "$label\: found expected requirements" );
}
else {
fail( "$label\: no requirements available\n" );
}
}
else {
is( $yaml, undef, "$label\: read_yaml returns undef");
is( $dist->read_yaml, undef, "$label\: repeat read_yaml returns undef");
pass( "$label\: no requirement checks apply" );
}
}
}

# Local Variables:
# mode: cperl
# cperl-indent-level: 4
# End:
# vi: ts=4:sts=4:sw=4:et:
12 changes: 12 additions & 0 deletions t/data/META-dynamic.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
--- #YAML:1.0
name: CPAN-Test-Dummy-Perl5-Make-ConfReq
version: 1.00
abstract: CPAN Test Dummy for CPAN.pm
license: perl
generated_by: ExtUtils::MakeMaker version 6.32_01 and tweaked manually
distribution_type: module
requires:
File::Spec: 0.87
meta-spec:
url: http://module-build.sourceforge.net/META-spec-v1.3.html
version: 1.3
13 changes: 13 additions & 0 deletions t/data/META-static.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
--- #YAML:1.0
name: CPAN-Test-Dummy-Perl5-Make-ConfReq
version: 1.00
abstract: CPAN Test Dummy for CPAN.pm
license: perl
generated_by: ExtUtils::MakeMaker version 6.32_01 and tweaked manually
distribution_type: module
dynamic_config: 0
requires:
File::Spec: 0.87
meta-spec:
url: http://module-build.sourceforge.net/META-spec-v1.3.html
version: 1.3

0 comments on commit f2c9af5

Please sign in to comment.