1#!perl -w
2
3use strict;
4use warnings;
5
6BEGIN { unshift @INC, 't/lib'; }
7use Test::More eval { require CPAN::Meta; CPAN::Meta->VERSION(2.143240) } ? ()
8  : (skip_all => 'CPAN::Meta 2.143240 required for this test');
9use File::Temp qw[tempdir];
10require ExtUtils::MM_Any;
11
12my $tmpdir = tempdir( DIR => 't', CLEANUP => 1 );
13use Cwd; my $cwd = getcwd; END { chdir $cwd } # so File::Temp can cleanup
14chdir $tmpdir or die "chdir $tmpdir: $!";
15
16my $EMPTY = qr/['"]?version['"]?\s*:\s*['"]['"]/;
17my @DATA = (
18    [
19        [ DISTNAME => 'Net::FTP::Recursive', VERSION  => 'Recursive.pm', ],
20        qr{Can't parse version 'Recursive.pm'},
21        'VERSION => filename',
22        $EMPTY,
23    ],
24    [
25        [ DISTNAME => 'Image::Imgur', VERSION  => 'undef', ],
26        qr{Can't parse version 'undef'},
27        'no $VERSION in file -> VERSION=>"undef"',
28        $EMPTY,
29    ],
30    [
31        [ DISTNAME => 'SQL::Library', VERSION  => 0.0.3, ],
32        qr{Can't parse version '\x00\x00\x03'},
33        "x.y.z version",
34        $EMPTY,
35    ],
36    [
37        [ DISTNAME => 'Array::Suffix', VERSION  => '.5', ],
38        qr{Can't parse version '.5'},
39        ".5 version",
40        $EMPTY,
41    ],
42    [
43        [
44            DISTNAME   => 'Attribute::Signature',
45            META_MERGE => {
46                resources => {
47                    repository         => 'http://github.com/chorny/Attribute-Signature',
48                    'Repository-clone' => 'git://github.com/chorny/Attribute-Signature.git',
49                },
50            },
51        ],
52        qr/^$/,
53        "Non-camel case metadata",
54        qr/x_Repositoryclone/,
55    ],
56    [
57        [
58            DISTNAME   => 'CPAN::Testers::ParseReport',
59            VERSION    => '2.34',
60            META_ADD => {
61                provides => {
62                    "CPAN::Testers::ParseReport" => {
63                        version => version->new("v1.2.3"),
64                        file    => "lib/CPAN/Testers/ParseReport.pm"
65                    }
66                }
67            },
68        ],
69        qr/^$/,
70        "version object in provides",
71        qr/['"]?version['"]?\s*:\s*['"]v1\.2\.3['"]/,
72    ],
73    [
74        [
75            DISTNAME   => 'Bad::License',
76            VERSION    => '2.34',
77            LICENSE   => 'death and retribution',
78        ],
79        qr/Invalid LICENSE value/,
80        "Bad licence warns",
81        qr/['"]?version['"]?\s*:\s*['"]2\.34['"]/,
82    ],
83);
84
85plan tests => 3 * @DATA;
86run_test(@$_) for @DATA;
87
88sub ExtUtils::MM_Any::quote_literal { $_[1] }
89
90sub run_test {
91    my ($mmargs, $expected, $label, $metadata_re) = @_;
92    my $mm = bless { ARGS => {@$mmargs}, @$mmargs }, 'ExtUtils::MM_Any';
93    my @warnings;
94    my $ret;
95    {
96        local $SIG{__WARN__} = sub { push @warnings, @_ };
97        eval { $ret = $mm->metafile_target; };
98    }
99    SKIP: {
100        if ($@) {
101            diag $@;
102            skip "$label got exception", 3 if $@;
103        }
104        ok 1, "$label metafile_target";
105        like join("", @warnings), $expected, "$label right warning";
106        like $ret, $metadata_re, "$label metadata";
107    }
108}
109