1use strict;
2use warnings;
3use Test::More 0.88;
4
5use CPAN::Meta;
6use File::Temp 0.20 ();
7use Parse::CPAN::Meta;
8
9delete $ENV{PERL_YAML_BACKEND};
10delete $ENV{PERL_JSON_BACKEND};
11delete $ENV{CPAN_META_JSON_BACKEND};
12delete $ENV{CPAN_META_JSON_DECODER};
13
14my $distmeta = {
15  name     => 'Module-Build',
16  abstract => 'Build and install Perl modules',
17  description =>  "Module::Build is a system for building, testing, "
18              .   "and installing Perl modules.  It is meant to be an "
19              .   "alternative to ExtUtils::MakeMaker... blah blah blah",
20  version  => '0.36',
21  author   => [
22    'Ken Williams <kwilliams@cpan.org>',
23    'Module-Build List <module-build@perl.org>', # additional contact
24  ],
25  release_status => 'stable',
26  license  => [ 'perl_5' ],
27  prereqs => {
28    runtime => {
29      requires => {
30        'perl'   => '5.006',
31        'Config' => '0',
32        'Cwd'    => '0',
33        'Data::Dumper' => '0',
34        'ExtUtils::Install' => '0',
35        'File::Basename' => '0',
36        'File::Compare'  => '0',
37        'File::Copy' => '0',
38        'File::Find' => '0',
39        'File::Path' => '0',
40        'File::Spec' => '0',
41        'IO::File'   => '0',
42      },
43      recommends => {
44        'Archive::Tar' => '1.00',
45        'ExtUtils::Install' => '0.3',
46        'ExtUtils::ParseXS' => '2.02',
47        'Pod::Text' => '0',
48        'YAML' => '0.35',
49      },
50    },
51    build => {
52      requires => {
53        'Test::More' => '0',
54      },
55    }
56  },
57  resources => {
58    license => ['http://dev.perl.org/licenses/'],
59  },
60  optional_features => {
61    domination => {
62      description => 'Take over the world',
63      prereqs     => {
64        develop => { requires => { 'Genius::Evil'     => '1.234' } },
65        runtime => { requires => { 'Machine::Weather' => '2.0'   } },
66      },
67    },
68  },
69  dynamic_config => 1,
70  keywords => [ qw/ toolchain cpan dual-life / ],
71  'meta-spec' => {
72    version => '2',
73    url     => 'http://search.cpan.org/perldoc?CPAN::Meta::Spec',
74  },
75  generated_by => 'Module::Build version 0.36',
76};
77
78my $meta = CPAN::Meta->new( $distmeta );
79
80my $tmpdir = File::Temp->newdir();
81my $metafile = File::Spec->catfile( $tmpdir, 'META.json' );
82
83ok( $meta->save($metafile), "save returns true" );
84ok( -f $metafile, "save meta to file" );
85
86ok( my $loaded = Parse::CPAN::Meta->load_file($metafile), 'load saved file' );
87is($loaded->{name},     'Module-Build', 'name correct');
88
89like(
90  $loaded->{x_serialization_backend},
91  qr/\AJSON::PP version [0-9]/,
92  "x_serialization_backend",
93);
94
95ok(
96  ! exists $meta->{x_serialization_backend},
97  "we didn't leak x_serialization_backend up into the saved struct",
98);
99
100ok( $loaded = Parse::CPAN::Meta->load_file('t/data-test/META-1_4.yml'), 'load META-1.4' );
101is($loaded->{name},     'Module-Build', 'name correct');
102
103# Test saving with conversion
104
105my $metayml = File::Spec->catfile( $tmpdir, 'META.yml' );
106
107$meta->save($metayml, {version => "1.4"});
108ok( -f $metayml, "save meta to META.yml with conversion" );
109
110ok( $loaded = Parse::CPAN::Meta->load_file($metayml), 'load saved file' );
111is( $loaded->{name},     'Module-Build', 'name correct');
112is( $loaded->{requires}{perl}, "5.006", 'prereq correct' );
113
114like(
115  $loaded->{x_serialization_backend},
116  qr/\ACPAN::Meta::YAML version [0-9]/,
117  "x_serialization_backend",
118);
119
120ok(
121  ! exists $meta->{x_serialization_backend},
122  "we didn't leak x_serialization_backend up into the saved struct",
123);
124
125# file without suffix
126
127ok( $loaded = CPAN::Meta->load_file('t/data-test/META-2.meta'), 'load_file META-2.meta' );
128
129my $string = do { open my $fh, '<', 't/data-test/META-2.meta'; local $/; <$fh> };
130ok( $loaded = CPAN::Meta->load_string($string), 'load META-2.meta from string' );
131
132done_testing;
133# vim: ts=2 sts=2 sw=2 et :
134