| 1 | use strict; |
| 2 | use warnings; |
| 3 | |
| 4 | # This test was generated by Dist::Zilla::Plugin::Test::ReportPrereqs 0.020 |
| 5 | # THEN modified with more info by Ed J for PDL project |
| 6 | |
| 7 | use Test::More tests => 1; |
| 8 | |
| 9 | use ExtUtils::MakeMaker; |
| 10 | use File::Spec; |
| 11 | |
| 12 | # from $version::LAX |
| 13 | my $lax_version_re = |
| 14 | qr/(?: undef | (?: (?:[0-9]+) (?: \. | (?:\.[0-9]+) (?:_[0-9]+)? )? |
| 15 | | |
| 16 | (?:\.[0-9]+) (?:_[0-9]+)? |
| 17 | ) | (?: |
| 18 | v (?:[0-9]+) (?: (?:\.[0-9]+)+ (?:_[0-9]+)? )? |
| 19 | | |
| 20 | (?:[0-9]+)? (?:\.[0-9]+){2,} (?:_[0-9]+)? |
| 21 | ) |
| 22 | )/x; |
| 23 | |
| 24 | # hide optional CPAN::Meta modules from prereq scanner |
| 25 | # and check if they are available |
| 26 | my $cpan_meta = "CPAN::Meta"; |
| 27 | my $cpan_meta_pre = "CPAN::Meta::Prereqs"; |
| 28 | my $HAS_CPAN_META = eval "require $cpan_meta; $cpan_meta->VERSION('2.120900')" && eval "require $cpan_meta_pre"; ## no critic |
| 29 | |
| 30 | # Verify requirements? |
| 31 | my $DO_VERIFY_PREREQS = 1; |
| 32 | |
| 33 | sub _max { |
| 34 | my $max = shift; |
| 35 | $max = ( $_ > $max ) ? $_ : $max for @_; |
| 36 | return $max; |
| 37 | } |
| 38 | |
| 39 | sub _merge_prereqs { |
| 40 | my ($collector, $prereqs) = @_; |
| 41 | |
| 42 | # CPAN::Meta::Prereqs object |
| 43 | if (ref $collector eq $cpan_meta_pre) { |
| 44 | return $collector->with_merged_prereqs( |
| 45 | CPAN::Meta::Prereqs->new( $prereqs ) |
| 46 | ); |
| 47 | } |
| 48 | |
| 49 | # Raw hashrefs |
| 50 | for my $phase ( keys %$prereqs ) { |
| 51 | for my $type ( keys %{ $prereqs->{$phase} } ) { |
| 52 | for my $module ( keys %{ $prereqs->{$phase}{$type} } ) { |
| 53 | $collector->{$phase}{$type}{$module} = $prereqs->{$phase}{$type}{$module}; |
| 54 | } |
| 55 | } |
| 56 | } |
| 57 | |
| 58 | return $collector; |
| 59 | } |
| 60 | |
| 61 | my @include = qw( |
| 62 | |
| 63 | ); |
| 64 | |
| 65 | my @exclude = qw( |
| 66 | |
| 67 | ); |
| 68 | |
| 69 | # Add static prereqs to the included modules list |
| 70 | my $static_prereqs = do 't/00-report-prereqs.dd'; |
| 71 | |
| 72 | # Merge all prereqs (either with ::Prereqs or a hashref) |
| 73 | my $full_prereqs = _merge_prereqs( |
| 74 | ( $HAS_CPAN_META ? $cpan_meta_pre->new : {} ), |
| 75 | $static_prereqs |
| 76 | ); |
| 77 | |
| 78 | # Add dynamic prereqs to the included modules list (if we can) |
| 79 | my ($source) = grep { -f } 'MYMETA.json', 'MYMETA.yml'; |
| 80 | if ( $source && $HAS_CPAN_META ) { |
| 81 | if ( my $meta = eval { CPAN::Meta->load_file($source) } ) { |
| 82 | $full_prereqs = _merge_prereqs($full_prereqs, $meta->prereqs); |
| 83 | } |
| 84 | } |
| 85 | else { |
| 86 | $source = 'static metadata'; |
| 87 | } |
| 88 | |
| 89 | my @full_reports; |
| 90 | my @dep_errors; |
| 91 | my $req_hash = $HAS_CPAN_META ? $full_prereqs->as_string_hash : $full_prereqs; |
| 92 | |
| 93 | # Add static includes into a fake section |
| 94 | for my $mod (@include) { |
| 95 | $req_hash->{other}{modules}{$mod} = 0; |
| 96 | } |
| 97 | |
| 98 | for my $phase ( qw(configure build test runtime develop other) ) { |
| 99 | next unless $req_hash->{$phase}; |
| 100 | next if ($phase eq 'develop' and not $ENV{AUTHOR_TESTING}); |
| 101 | |
| 102 | for my $type ( qw(requires recommends suggests conflicts modules) ) { |
| 103 | next unless $req_hash->{$phase}{$type}; |
| 104 | |
| 105 | my $title = ucfirst($phase).' '.ucfirst($type); |
| 106 | my @reports = [qw/Module Want Have Where Howbig/]; |
| 107 | |
| 108 | for my $mod ( sort keys %{ $req_hash->{$phase}{$type} } ) { |
| 109 | next if $mod eq 'perl'; |
| 110 | next if grep { $_ eq $mod } @exclude; |
| 111 | |
| 112 | my $file = $mod; |
| 113 | $file =~ s{::}{/}g; |
| 114 | $file .= ".pm"; |
| 115 | my ($prefix) = grep { -e File::Spec->catfile($_, $file) } @INC; |
| 116 | |
| 117 | my $want = $req_hash->{$phase}{$type}{$mod}; |
| 118 | $want = "undef" unless defined $want; |
| 119 | $want = "any" if !$want && $want == 0; |
| 120 | |
| 121 | my $req_string = $want eq 'any' ? 'any version required' : "version '$want' required"; |
| 122 | |
| 123 | if ($prefix) { |
| 124 | my $filename = File::Spec->catfile($prefix, $file); |
| 125 | my $have = MM->parse_version( $filename ); |
| 126 | $have = "undef" unless defined $have; |
| 127 | push @reports, [$mod, $want, $have, $prefix, (-s $filename)]; |
| 128 | |
| 129 | if ( $DO_VERIFY_PREREQS && $HAS_CPAN_META && $type eq 'requires' ) { |
| 130 | if ( $have !~ /\A$lax_version_re\z/ ) { |
| 131 | push @dep_errors, "$mod version '$have' cannot be parsed ($req_string)"; |
| 132 | } |
| 133 | elsif ( ! $full_prereqs->requirements_for( $phase, $type )->accepts_module( $mod => $have ) ) { |
| 134 | push @dep_errors, "$mod version '$have' is not in required range '$want'"; |
| 135 | } |
| 136 | } |
| 137 | } |
| 138 | else { |
| 139 | push @reports, [$mod, $want, "missing", '', 0]; |
| 140 | |
| 141 | if ( $DO_VERIFY_PREREQS && $type eq 'requires' ) { |
| 142 | push @dep_errors, "$mod is not installed ($req_string)"; |
| 143 | } |
| 144 | } |
| 145 | } |
| 146 | |
| 147 | if ( @reports ) { |
| 148 | push @full_reports, "=== $title ===\n\n"; |
| 149 | |
| 150 | my $ml = _max( map { length $_->[0] } @reports ); |
| 151 | my $wl = _max( map { length $_->[1] } @reports ); |
| 152 | my $hl = _max( map { length $_->[2] } @reports ); |
| 153 | my $ll = _max( map { length $_->[3] } @reports ); # location |
| 154 | my $sl = _max( map { length $_->[4] } @reports ); # size |
| 155 | |
| 156 | if ($type eq 'modules') { |
| 157 | splice @reports, 1, 0, ["-" x $ml, "", "-" x $hl, "-" x $ll, "-" x $sl]; |
| 158 | push @full_reports, map { sprintf(" %*s %*s\n", -$ml, $_->[0], $hl, $_->[2]) } @reports; |
| 159 | } |
| 160 | else { |
| 161 | splice @reports, 1, 0, ["-" x $ml, "-" x $wl, "-" x $hl, "-" x $ll, "-" x $sl]; |
| 162 | push @full_reports, map { sprintf(" %*s %*s %*s %*s %*s\n", -$ml, $_->[0], $wl, $_->[1], $hl, $_->[2], -$ll, $_->[3], $sl, $_->[4]) } @reports; |
| 163 | } |
| 164 | |
| 165 | push @full_reports, "\n"; |
| 166 | } |
| 167 | } |
| 168 | } |
| 169 | |
| 170 | if ( @full_reports ) { |
| 171 | diag "\nVersions for all modules listed in $source (including optional ones):\n\n", @full_reports; |
| 172 | } |
| 173 | |
| 174 | if ( @dep_errors ) { |
| 175 | diag join("\n", |
| 176 | "\n*** WARNING WARNING WARNING WARNING WARNING WARNING WARNING WARNING ***\n", |
| 177 | "The following REQUIRED prerequisites were not satisfied:\n", |
| 178 | @dep_errors, |
| 179 | "\n" |
| 180 | ); |
| 181 | } |
| 182 | |
| 183 | pass; |
| 184 | |
| 185 | # vim: ts=4 sts=4 sw=4 et: |