From: Vincent Pit Date: Fri, 27 Nov 2009 18:24:33 +0000 (+0100) Subject: Rename the 'minimum' option to a more versatile 'range' parameter X-Git-Tag: v0.09~15 X-Git-Url: http://git.vpit.fr/?a=commitdiff_plain;h=5a5575483b3f3ae59a3895e1a571edd687becec0;p=perl%2Fmodules%2FCPANPLUS-Dist-Gentoo.git Rename the 'minimum' option to a more versatile 'range' parameter And expand the logical AND between atoms in a new C::D::G::Atom->and --- diff --git a/MANIFEST b/MANIFEST index b34c59d..858868a 100644 --- a/MANIFEST +++ b/MANIFEST @@ -14,6 +14,7 @@ t/11-maps-name.t t/12-maps-version.t t/13-maps-license.t t/20-version.t +t/31-atom-and.t t/91-pod.t t/92-pod-coverage.t t/95-portability-files.t diff --git a/lib/CPANPLUS/Dist/Gentoo.pm b/lib/CPANPLUS/Dist/Gentoo.pm index c27c5b5..3bc66a5 100644 --- a/lib/CPANPLUS/Dist/Gentoo.pm +++ b/lib/CPANPLUS/Dist/Gentoo.pm @@ -550,29 +550,34 @@ sub ebuild_source { sub _cpan2portage { my ($self, $name, $version) = @_; - $name = CPANPLUS::Dist::Gentoo::Maps::name_c2g($name); - my $ver; - $ver = CPANPLUS::Dist::Gentoo::Maps::version_c2g($version) if defined $version; + $name = CPANPLUS::Dist::Gentoo::Maps::name_c2g($name); + $version = CPANPLUS::Dist::Gentoo::Maps::version_c2g($version); my @portdirs = ($main_portdir, @{$self->status->portdir_overlay}); for my $category (qw/virtual perl-core dev-perl perl-gcpan/, CATEGORY) { - my $atom = ($category eq 'virtual' ? 'perl-' : '') . $name; + my $name = ($category eq 'virtual' ? 'perl-' : '') . $name; for my $portdir (@portdirs) { my @ebuilds = glob File::Spec->catfile( $portdir, $category, - $atom, - "$atom-*.ebuild", + $name, + "$name-*.ebuild", ) or next; - my $atom = reduce { $a < $b ? $b : $a } # handles overloading - map CPANPLUS::Dist::Gentoo::Atom->new( - ebuild => $_, - minimum => 1, - ), @ebuilds; - next if defined $ver and $atom < $ver; + my @atoms = map CPANPLUS::Dist::Gentoo::Atom->new( + defined $version ? ( + ebuild => $_, + range => '>=', + ) : ( + category => $category, + name => $name, + ), + ), @ebuilds; + + my $atom = reduce { $a < $b ? $b : $a } @atoms; # handles overloading + next if defined $version and $atom < $version; return $atom; } diff --git a/lib/CPANPLUS/Dist/Gentoo/Atom.pm b/lib/CPANPLUS/Dist/Gentoo/Atom.pm index ebe6a4e..bc0e9c0 100644 --- a/lib/CPANPLUS/Dist/Gentoo/Atom.pm +++ b/lib/CPANPLUS/Dist/Gentoo/Atom.pm @@ -49,20 +49,30 @@ sub new { } } - Carp::confess('Minimum atoms require a valid version') if not defined $version - and $args{minimum}; + my $range = $args{range}; + if (defined $version) { + if (defined $range) { + Carp::confess("Invalid range $range") + unless grep $range eq $_, qw|< <= = >= >|; + } else { + $range = '='; + } + } else { + Carp::confess('Range atoms require a valid version') + if defined $range and length $range; + } bless { category => $category, name => $name, version => $version, - minimum => $args{minimum}, + range => $range, ebuild => $args{ebuild}, }, $class; } BEGIN { - eval "sub $_ { \$_[0]->{$_} }" for qw/category name version minimum ebuild/; + eval "sub $_ { \$_[0]->{$_} }" for qw/category name version range ebuild/; } sub cmp { @@ -90,44 +100,70 @@ sub as_string { my $atom = $a->category . '/' . $a->name; my $version = $a->version; - if (defined $version) { - $atom = "=$atom-$version"; - $atom = ">$atom" if $a->minimum; - } + $atom = $a->range . $atom . '-' . $version if defined $version; return $atom; } -sub fold { +my %order = ( + '<' => -2, + '<=' => -1, + '=' => 0, + '>=' => 1, + '>' => 2, +); + +sub and { shift unless length ref $_[0]; - my %seen; - for my $atom (@_) { - my ($category, $name, $version) = map $atom->$_, qw/category name version/; - my $key = join '/', $category, $name; - my $cur = $seen{$key}; + my $a1 = shift; + my $a2 = shift; + $a2 = $a2->and(@_) if @_; - unless (defined $cur) { - $seen{$key} = $atom; - next; - } + my $p1 = join '/', $a1->category, $a1->name; + my $p2 = join '/', $a2->category, $a2->name; + Carp::confess("Atoms for different packages $p1 and $p2") unless $p1 eq $p2; - next unless defined $version; + my $v1 = $a1->version; + return $a2 unless defined $v1; + my $r1 = $a1->range; # Defined if $v1 is defined - if (not defined $cur->version) { - $seen{$key} = $atom; - next; - } + my $v2 = $a2->version; + return $a1 unless defined $v2; + my $r2 = $a2->range; # defined if $v2 is defined - if ($atom->minimum) { - if ($cur->minimum) { - $seen{$key} = $atom < $cur ? $cur : $atom; - } else { - Carp::confess('Version mismatch') if $atom > $cur; - } - } elsif ($cur->minimum) { - Carp::confess('Version mismatch') if $cur > $atom; - } + my $o1 = $order{$r1}; + my $o2 = $order{$r2}; + + Carp::confess("Incompatible ranges $r1$p1 and $r2$p2") if $o1 * $o2 < 0; + + if ($r2 eq '=') { + ($a1, $a2) = ($a2, $a1); + ($v1, $v2) = ($v2, $v1); + ($r1, $r2) = ($r2, $r1); + ($o1, $o2) = ($o2, $o1); + } + + if ($r1 eq '=') { + my $r = $r2 eq '=' ? '==' : $r2; + Carp::confess("Version mismatch $v1 $r $v2") unless eval "\$a1 $r \$a2"; + return $a1; + } elsif ($o1 > 0) { + return $a1 < $a2 ? $a2 : $a1; + } else { + return $a1 < $a2 ? $a1 : $a2; + } +} + +sub fold { + shift unless length ref $_[0]; + + my %seen; + for my $atom (@_) { + my $key = join '/', $atom->category, $atom->name; + + my $cur = $seen{$key}; + $seen{$key} = defined $cur ? $cur->and($atom) : $atom; } return values %seen; diff --git a/t/31-atom-and.t b/t/31-atom-and.t new file mode 100644 index 0000000..6de1b3a --- /dev/null +++ b/t/31-atom-and.t @@ -0,0 +1,120 @@ +#!perl + +use strict; +use warnings; + +use Test::More tests => 2 * ((8 * 7) / 2 + 2); + +use CPANPLUS::Dist::Gentoo::Atom; + +sub A () { 'CPANPLUS::Dist::Gentoo::Atom' } + +my $a0 = A->new( + category => 'test', + name => 'a', +); + +my $a1 = A->new( + category => 'test', + name => 'a', + version => '1.0', + range => '=', +); + +my $a2 = A->new( + category => 'test', + name => 'a', + version => '1.0', + range => '<', +); + +my $a3 = A->new( + category => 'test', + name => 'a', + version => '1.0', + range => '<=', +); + +my $a4 = A->new( + category => 'test', + name => 'a', + version => '2.0', + range => '=', +); + +my $a5 = A->new( + category => 'test', + name => 'a', + version => '2.0', + range => '>=', +); + +my $a6 = A->new( + category => 'test', + name => 'a', + version => '2.0', + range => '>', +); + +my $x_ver = qr/Version mismatch/; +my $x_range = qr/Incompatible ranges/; + +my @tests = ( + [ [ $a0, $a0 ] => $a0 ], + [ [ $a0, $a1 ] => $a1 ], + [ [ $a0, $a2 ] => $a2 ], + [ [ $a0, $a3 ] => $a3 ], + [ [ $a0, $a4 ] => $a4 ], + [ [ $a0, $a5 ] => $a5 ], + [ [ $a0, $a6 ] => $a6 ], + + [ [ $a1, $a1 ] => $a1 ], + [ [ $a1, $a2 ] => $x_ver ], + [ [ $a1, $a3 ] => $a1 ], + [ [ $a1, $a4 ] => $x_ver ], + [ [ $a1, $a5 ] => $x_ver ], + [ [ $a1, $a6 ] => $x_ver ], + + [ [ $a2, $a2 ] => $a2 ], + [ [ $a2, $a3 ] => $a2 ], + [ [ $a2, $a4 ] => $x_ver ], + [ [ $a2, $a5 ] => $x_range ], + [ [ $a2, $a5 ] => $x_range ], + + [ [ $a3, $a3 ] => $a3 ], + [ [ $a3, $a4 ] => $a4 ], + [ [ $a3, $a5 ] => $x_range ], + [ [ $a3, $a6 ] => $x_range ], + + [ [ $a4, $a4 ] => $a4 ], + [ [ $a4, $a5 ] => $a4 ], + [ [ $a4, $a6 ] => $x_ver ], + + [ [ $a5, $a5 ] => $a5 ], + [ [ $a5, $a6 ] => $a6 ], + + [ [ $a6, $a6 ] => $a6 ], + + [ [ ($a1) x 3 ] => $a1 ], + [ [ ($a2) x 4 ] => $a2 ], +); + +for my $t (@tests) { + my ($args, $exp) = @$t; + + for my $r (0 .. 1) { + my @a = @$args; + @a = reverse @a if $r; + + my $desc = join ' AND ', map "'$_'", @a; + + my $a = eval { A->and(@a) }; + my $err = $@; + + if (ref $exp eq 'Regexp') { + like $err, $exp, "$desc should fail"; + } else { + cmp_ok $a, '==', $exp, "$desc == '$exp'"; + } + } +}