xref: /openbsd-src/gnu/usr.bin/perl/cpan/ExtUtils-MakeMaker/t/metafile_data.t (revision 50b7afb2c2c0993b0894d4e34bf857cb13ed9c80)
1BEGIN {
2    unshift @INC, 't/lib';
3}
4
5use strict;
6use Test::More tests => 22;
7
8use Data::Dumper;
9use File::Temp;
10use Cwd;
11use Parse::CPAN::Meta;
12
13require ExtUtils::MM_Any;
14
15sub in_dir(&;$) {
16    my $code = shift;
17    my $dir = shift || File::Temp->newdir;
18
19    # chdir to the new directory
20    my $orig_dir = cwd();
21    chdir $dir or die "Can't chdir to $dir: $!";
22
23    # Run the code, but trap the error so we can chdir back
24    my $return;
25    my $ok = eval { $return = $code->(); 1; };
26    my $err = $@;
27
28    # chdir back
29    chdir $orig_dir or die "Can't chdir to $orig_dir: $!";
30
31    # rethrow if necessary
32    die $err unless $ok;
33
34    return $return;
35}
36
37sub mymeta_ok {
38    my($have, $want, $name) = @_;
39
40    local $Test::Builder::Level = $Test::Builder::Level + 1;
41
42    my $have_gen = delete $have->{generated_by};
43    my $want_gen = delete $want->{generated_by};
44
45    is_deeply $have, $want, $name;
46    like $have_gen, qr{CPAN::Meta}, "CPAN::Meta mentioned in the generated_by";
47
48    return;
49}
50
51my $new_mm = sub {
52    return bless { ARGS => {@_}, @_ }, 'ExtUtils::MM_Any';
53};
54
55{
56    my $mm = $new_mm->(
57        DISTNAME        => 'Foo-Bar',
58        VERSION         => 1.23,
59        PM              => {
60            "Foo::Bar"          => 'lib/Foo/Bar.pm',
61        },
62    );
63
64    is_deeply {$mm->metafile_data}, {
65        name            => 'Foo-Bar',
66        version         => 1.23,
67        abstract        => 'unknown',
68        author          => [],
69        license         => 'unknown',
70        dynamic_config  => 1,
71        distribution_type       => 'module',
72
73        configure_requires      => {
74            'ExtUtils::MakeMaker'       => 0,
75        },
76        build_requires      => {
77            'ExtUtils::MakeMaker'       => 0,
78        },
79
80        no_index        => {
81            directory           => [qw(t inc)],
82        },
83
84        generated_by => "ExtUtils::MakeMaker version $ExtUtils::MakeMaker::VERSION",
85        'meta-spec'  => {
86            url         => 'http://module-build.sourceforge.net/META-spec-v1.4.html',
87            version     => 1.4
88        },
89    };
90
91
92    is_deeply {$mm->metafile_data({}, { no_index => { directory => [qw(foo)] } })}, {
93        name            => 'Foo-Bar',
94        version         => 1.23,
95        abstract        => 'unknown',
96        author          => [],
97        license         => 'unknown',
98        dynamic_config  => 1,
99        distribution_type       => 'module',
100
101        configure_requires      => {
102            'ExtUtils::MakeMaker'       => 0,
103        },
104        build_requires      => {
105            'ExtUtils::MakeMaker'       => 0,
106        },
107
108        no_index        => {
109            directory           => [qw(t inc foo)],
110        },
111
112        generated_by => "ExtUtils::MakeMaker version $ExtUtils::MakeMaker::VERSION",
113        'meta-spec'  => {
114            url         => 'http://module-build.sourceforge.net/META-spec-v1.4.html',
115            version     => 1.4
116        },
117    }, 'rt.cpan.org 39348';
118}
119
120
121{
122    my $mm = $new_mm->(
123        DISTNAME        => 'Foo-Bar',
124        VERSION         => 1.23,
125        AUTHOR          => ['Some Guy'],
126        PREREQ_PM       => {
127            Foo                 => 2.34,
128            Bar                 => 4.56,
129        },
130    );
131
132    is_deeply {$mm->metafile_data(
133        {
134            configure_requires => {
135                Stuff   => 2.34
136            },
137            wobble      => 42
138        },
139        {
140            no_index    => {
141                package => "Thing"
142            },
143            wibble      => 23
144        },
145    )},
146    {
147        name            => 'Foo-Bar',
148        version         => 1.23,
149        abstract        => 'unknown',
150        author          => ['Some Guy'],
151        license         => 'unknown',
152        dynamic_config  => 1,
153        distribution_type       => 'script',
154
155        configure_requires      => {
156            Stuff       => 2.34,
157        },
158        build_requires      => {
159            'ExtUtils::MakeMaker'       => 0,
160        },
161
162        requires       => {
163            Foo                 => 2.34,
164            Bar                 => 4.56,
165        },
166
167        no_index        => {
168            directory           => [qw(t inc)],
169            package             => 'Thing',
170        },
171
172        generated_by => "ExtUtils::MakeMaker version $ExtUtils::MakeMaker::VERSION",
173        'meta-spec'  => {
174            url         => 'http://module-build.sourceforge.net/META-spec-v1.4.html',
175            version     => 1.4
176        },
177
178        wibble  => 23,
179        wobble  => 42,
180    };
181}
182
183
184# Test MIN_PERL_VERSION
185{
186    my $mm = $new_mm->(
187        DISTNAME        => 'Foo-Bar',
188        VERSION         => 1.23,
189        PM              => {
190            "Foo::Bar"          => 'lib/Foo/Bar.pm',
191        },
192        MIN_PERL_VERSION => 5.006,
193    );
194
195    is_deeply {$mm->metafile_data}, {
196        name            => 'Foo-Bar',
197        version         => 1.23,
198        abstract        => 'unknown',
199        author          => [],
200        license         => 'unknown',
201        dynamic_config  => 1,
202        distribution_type       => 'module',
203
204        configure_requires      => {
205            'ExtUtils::MakeMaker'       => 0,
206        },
207        build_requires      => {
208            'ExtUtils::MakeMaker'       => 0,
209        },
210
211        requires        => {
212            perl        => '5.006',
213        },
214
215        no_index        => {
216            directory           => [qw(t inc)],
217        },
218
219        generated_by => "ExtUtils::MakeMaker version $ExtUtils::MakeMaker::VERSION",
220        'meta-spec'  => {
221            url         => 'http://module-build.sourceforge.net/META-spec-v1.4.html',
222            version     => 1.4
223        },
224    };
225}
226
227
228# Test MIN_PERL_VERSION
229{
230    my $mm = $new_mm->(
231        DISTNAME        => 'Foo-Bar',
232        VERSION         => 1.23,
233        PM              => {
234            "Foo::Bar"          => 'lib/Foo/Bar.pm',
235        },
236        MIN_PERL_VERSION => 5.006,
237        PREREQ_PM => {
238            'Foo::Bar'  => 1.23,
239        },
240    );
241
242    is_deeply {$mm->metafile_data}, {
243        name            => 'Foo-Bar',
244        version         => 1.23,
245        abstract        => 'unknown',
246        author          => [],
247        license         => 'unknown',
248        dynamic_config  => 1,
249        distribution_type       => 'module',
250
251        configure_requires      => {
252            'ExtUtils::MakeMaker'       => 0,
253        },
254        build_requires      => {
255            'ExtUtils::MakeMaker'       => 0,
256        },
257
258        requires        => {
259            perl        => '5.006',
260            'Foo::Bar'  => 1.23,
261        },
262
263        no_index        => {
264            directory           => [qw(t inc)],
265        },
266
267        generated_by => "ExtUtils::MakeMaker version $ExtUtils::MakeMaker::VERSION",
268        'meta-spec'  => {
269            url         => 'http://module-build.sourceforge.net/META-spec-v1.4.html',
270            version     => 1.4
271        },
272    };
273}
274
275# Test CONFIGURE_REQUIRES
276{
277    my $mm = $new_mm->(
278        DISTNAME        => 'Foo-Bar',
279        VERSION         => 1.23,
280        CONFIGURE_REQUIRES => {
281            "Fake::Module1" => 1.01,
282        },
283        PM              => {
284            "Foo::Bar"          => 'lib/Foo/Bar.pm',
285        },
286    );
287
288    is_deeply {$mm->metafile_data}, {
289        name            => 'Foo-Bar',
290        version         => 1.23,
291        abstract        => 'unknown',
292        author          => [],
293        license         => 'unknown',
294        dynamic_config  => 1,
295        distribution_type       => 'module',
296
297        configure_requires      => {
298            'Fake::Module1'       => 1.01,
299        },
300        build_requires      => {
301            'ExtUtils::MakeMaker'       => 0,
302        },
303
304        no_index        => {
305            directory           => [qw(t inc)],
306        },
307
308        generated_by => "ExtUtils::MakeMaker version $ExtUtils::MakeMaker::VERSION",
309        'meta-spec'  => {
310            url         => 'http://module-build.sourceforge.net/META-spec-v1.4.html',
311            version     => 1.4
312        },
313    },'CONFIGURE_REQUIRES';
314}
315
316# Test BUILD_REQUIRES
317{
318    my $mm = $new_mm->(
319        DISTNAME        => 'Foo-Bar',
320        VERSION         => 1.23,
321        BUILD_REQUIRES => {
322            "Fake::Module1" => 1.01,
323        },
324        PM              => {
325            "Foo::Bar"          => 'lib/Foo/Bar.pm',
326        },
327    );
328
329    is_deeply {$mm->metafile_data}, {
330        name            => 'Foo-Bar',
331        version         => 1.23,
332        abstract        => 'unknown',
333        author          => [],
334        license         => 'unknown',
335        dynamic_config  => 1,
336        distribution_type       => 'module',
337
338        configure_requires      => {
339            'ExtUtils::MakeMaker'       => 0,
340        },
341        build_requires      => {
342            'Fake::Module1'       => 1.01,
343        },
344
345        no_index        => {
346            directory           => [qw(t inc)],
347        },
348
349        generated_by => "ExtUtils::MakeMaker version $ExtUtils::MakeMaker::VERSION",
350        'meta-spec'  => {
351            url         => 'http://module-build.sourceforge.net/META-spec-v1.4.html',
352            version     => 1.4
353        },
354    },'CONFIGURE_REQUIRES';
355}
356
357# Test _REQUIRES key priority over META_ADD
358
359{
360    my $mm = $new_mm->(
361        DISTNAME        => 'Foo-Bar',
362        VERSION         => 1.23,
363        BUILD_REQUIRES => {
364            "Fake::Module1" => 1.01,
365        },
366        META_ADD => (my $meta_add = { build_requires => {} }),
367        PM              => {
368            "Foo::Bar"          => 'lib/Foo/Bar.pm',
369        },
370    );
371
372    is_deeply {$mm->metafile_data($meta_add)}, {
373        name            => 'Foo-Bar',
374        version         => 1.23,
375        abstract        => 'unknown',
376        author          => [],
377        license         => 'unknown',
378        dynamic_config  => 1,
379        distribution_type       => 'module',
380
381        configure_requires      => {
382            'ExtUtils::MakeMaker'       => 0,
383        },
384        build_requires      => { },
385
386        no_index        => {
387            directory           => [qw(t inc)],
388        },
389
390        generated_by => "ExtUtils::MakeMaker version $ExtUtils::MakeMaker::VERSION",
391        'meta-spec'  => {
392            url         => 'http://module-build.sourceforge.net/META-spec-v1.4.html',
393            version     => 1.4
394        },
395    },'META.yml data (META_ADD wins)';
396
397
398    # Yes, this is all hard coded.
399    require CPAN::Meta;
400    my $want_mymeta = {
401        name            => 'ExtUtils-MakeMaker',
402        version         => '6.57_07',
403        abstract        => 'Create a module Makefile',
404        author          => ['Michael G Schwern <schwern@pobox.com>'],
405        license         => ['perl_5'],
406        dynamic_config  => 0,
407
408        prereqs         => {
409            runtime => {
410                requires => {
411                    "DirHandle"         => 0,
412                    "File::Basename"    => 0,
413                    "File::Spec"        => "0.8",
414                    "Pod::Man"          => 0,
415                    "perl"              => "5.006",
416                },
417            },
418            configure => {
419                requires => {
420                },
421            },
422            build    => {
423                requires => {
424                    'Fake::Module1'       => 1.01,
425                },
426            },
427        },
428
429        release_status => 'testing',
430        resources => {
431            license     =>  [ 'http://dev.perl.org/licenses/' ],
432            homepage    =>  'http://makemaker.org',
433            bugtracker  =>  { web => 'http://rt.cpan.org/NoAuth/Bugs.html?Dist=ExtUtils-MakeMaker' },
434            repository  =>  { url => 'http://github.com/Perl-Toolchain-Gang/ExtUtils-MakeMaker' },
435            x_MailingList => 'makemaker@perl.org',
436        },
437
438        no_index        => {
439            directory           => [qw(t inc)],
440            package             => ["DynaLoader", "in"],
441        },
442
443        generated_by => "ExtUtils::MakeMaker version 6.5707, CPAN::Meta::Converter version 2.110580",
444        'meta-spec'  => {
445            url         => 'http://search.cpan.org/perldoc?CPAN::Meta::Spec',
446            version     => 2,
447        },
448    };
449
450    mymeta_ok $mm->mymeta("t/META_for_testing.json"),
451              $want_mymeta,
452              'MYMETA JSON data (BUILD_REQUIRES wins)';
453
454    mymeta_ok $mm->mymeta("t/META_for_testing.yml"),
455              $want_mymeta,
456              'MYMETA YAML data (BUILD_REQUIRES wins)';
457}
458
459{
460    my $mm = $new_mm->(
461        DISTNAME       => 'Foo-Bar',
462        VERSION        => 1.23,
463        BUILD_REQUIRES => { "Fake::Module1" => 1.01 },
464        TEST_REQUIRES  => { "Fake::Module2" => 1.23 },
465    );
466
467    my $meta = $mm->mymeta('t/META_for_testing.json');
468    is($meta->{build_requires}, undef, "no build_requires in v2 META");
469    is_deeply(
470        $meta->{prereqs}{build}{requires},
471        { "Fake::Module1" => 1.01 },
472        "build requires are one thing in META v2...",
473    );
474
475    is_deeply(
476        $meta->{prereqs}{test}{requires},
477        { "Fake::Module2" => 1.23 },
478        "...and test requires are another",
479    );
480}
481
482note "CPAN::Meta bug using the module version instead of the meta spec version"; {
483    my $mm = $new_mm->(
484        NAME      => 'GD::Barcode::Code93',
485        AUTHOR    => 'Chris DiMartino',
486        ABSTRACT  => 'Code 93 implementation of GD::Barcode family',
487        PREREQ_PM => {
488            'GD::Barcode' => 0,
489            'GD'          => 0
490        },
491        VERSION   => '1.4',
492    );
493
494    my $meta = $mm->mymeta("t/META_for_testing_tricky_version.yml");
495    is $meta->{'meta-spec'}{version}, 2, "internally, our MYMETA struct is v2";
496
497    in_dir {
498        $mm->write_mymeta($meta);
499        ok -e "MYMETA.yml";
500        ok -e "MYMETA.json";
501
502        my $meta_yml = Parse::CPAN::Meta->load_file("MYMETA.yml");
503        is $meta_yml->{'meta-spec'}{version}, 1.4, "MYMETA.yml correctly downgraded to 1.4";
504
505        my $meta_json = Parse::CPAN::Meta->load_file("MYMETA.json");
506        cmp_ok $meta_json->{'meta-spec'}{version}, ">=", 2, "MYMETA.json at 2 or better";
507    };
508}
509
510
511note "A bad license string"; {
512    my $mm = $new_mm->(
513        DISTNAME  => 'Foo::Bar',
514        VERSION   => '1.4',
515        LICENSE   => 'death and retribution',
516    );
517
518    in_dir {
519        my $meta = $mm->mymeta;
520        $mm->write_mymeta($meta);
521
522        my $meta_yml = Parse::CPAN::Meta->load_file("MYMETA.yml");
523        is $meta_yml->{license}, "unknown", "in yaml";
524
525        my $meta_json = Parse::CPAN::Meta->load_file("MYMETA.json");
526        is_deeply $meta_json->{license}, ["unknown"], "in json";
527    };
528}
529