summaryrefslogtreecommitdiff
path: root/t/mymeta.t
blob: 73cda0540c307960ef19811d21ab87149aa7dc76 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
#!/usr/bin/perl -w

use strict;
use lib 't/lib';
use MBTest;
use CPAN::Meta 2.110420;
use CPAN::Meta::YAML;
use Parse::CPAN::Meta 1.4401;
plan tests => 41;

blib_load('Module::Build');

my $tmp = MBTest->tmpdir;

use DistGen;
my $dist = DistGen->new( dir => $tmp );
$dist->change_file('Build.PL', <<"---");
use strict;
use Module::Build;

my \$builder = Module::Build->new(
  module_name         => '$dist->{name}',
  license             => 'perl',
  requires            => {
    'File::Spec' => ( \$ENV{BUMP_PREREQ} ? 0.86 : 0 ),
  },
  configure_requires  => {
    'Module::Build' => '0.42',
  }
);

\$builder->create_build_script();
---
$dist->regen;
$dist->chdir_in;

#########################

# Test MYMETA generation
{
  ok( ! -e "META.yml", "META.yml doesn't exist before Build.PL runs" );
  ok( ! -e "MYMETA.yml", "MYMETA.yml doesn't exist before Build.PL runs" );
  ok( ! -e "META.json", "META.json doesn't exist before Build.PL runs" );
  ok( ! -e "MYMETA.json", "MYMETA.json doesn't exist before Build.PL runs" );
  my $output;
  $output = stdout_of sub { $dist->run_build_pl };
  like($output, qr/Created MYMETA\.yml and MYMETA\.json/,
    "Ran Build.PL and saw MYMETA.yml creation message"
  );
  ok( -e "MYMETA.yml", "MYMETA.yml exists" );
  ok( -e "MYMETA.json", "MYMETA.json exists" );
}

#########################

# Test interactions between META/MYMETA
{
  my $output = stdout_stderr_of sub { $dist->run_build('distmeta') };
  like($output, qr/Created META\.yml and META\.json/,
    "Ran Build distmeta to create META.yml");
  # regenerate MYMETA to pick up from META instead of creating from scratch
  $output = stdout_of sub { $dist->run_build_pl };
  like($output, qr/Created MYMETA\.yml and MYMETA\.json/,
    "Re-ran Build.PL and regenerated MYMETA.yml based on META.yml"
  );

  for my $suffix ( qw/.yml .json/ ) {
    my $meta = Parse::CPAN::Meta->load_file("META$suffix");
    my $mymeta = Parse::CPAN::Meta->load_file("MYMETA$suffix");
    is( delete $meta->{dynamic_config}, 1,
      "META$suffix 'dynamic_config' is 1"
    );
    is( delete $mymeta->{dynamic_config}, 0,
      "MYMETA$suffix 'dynamic_config' is 0"
    );

    my $have_url = delete $mymeta->{'meta-spec'}->{url};
    my $want_url = delete $meta->{'meta-spec'}->{url};

    is_deeply( $mymeta, $meta, "Other generated MYMETA$suffix matches generated META$suffix" )
      or do {
        require Data::Dumper;
        diag "MYMETA:\n" . Data::Dumper::Dumper($mymeta)
          .  "META:\n" . Data::Dumper::Dumper($meta);
      };

    like $have_url, qr{Meta(::|-)Spec}i, "CPAN meta spec mentioned in meta-spec URL";
  }

  $output = stdout_stderr_of sub { $dist->run_build('realclean') };
  like( $output, qr/Cleaning up/, "Ran realclean");
  ok( ! -e 'Build', "Build file removed" );
  ok( ! -e 'MYMETA.yml', "MYMETA.yml file removed" );
  ok( ! -e 'MYMETA.json', "MYMETA.json file removed" );

  # test that dynamic prereq is picked up
  my $meta = Parse::CPAN::Meta->load_file("META.yml");
  my $meta2 = Parse::CPAN::Meta->load_file("META.json");
  local $ENV{BUMP_PREREQ} = 1;
  $output = stdout_of sub { $dist->run_build_pl };
  like($output, qr/Created MYMETA\.yml and MYMETA\.json/,
    "Ran Build.PL with dynamic config"
  );
  ok( -e "MYMETA.yml", "MYMETA.yml exists" );
  ok( -e "MYMETA.json", "MYMETA.json exists" );
  my $mymeta = Parse::CPAN::Meta->load_file('MYMETA.yml');
  my $mymeta2 = Parse::CPAN::Meta->load_file('MYMETA.json');
  isnt(   $meta->{requires}{'File::Spec'},
        $mymeta->{requires}{'File::Spec'},
        "MYMETA.yml requires differs from META.yml"
  );
  isnt(   $meta2->{prereqs}{runtime}{requires}{'File::Spec'},
        $mymeta2->{prereqs}{runtime}{requires}{'File::Spec'},
        "MYMETA.json requires differs from META.json"
  );
  $output = stdout_stderr_of sub { $dist->run_build('realclean') };
  like( $output, qr/Cleaning up/, "Ran realclean");
  ok( ! -e 'Build', "Build file removed" );
  ok( ! -e 'MYMETA.yml', "MYMETA file removed" );
  ok( ! -e 'MYMETA.json', "MYMETA file removed" );

  # manually change META and check that changes are preserved
  $meta->{author} = ['John Gault'];
  $meta2->{author} = ['John Gault'];
  ok( CPAN::Meta::YAML->new($meta)->write('META.yml'),
    "Wrote manually modified META.yml" );
  ok( CPAN::Meta->new( $meta2 )->save('META.json'),
    "Wrote manually modified META.json" );

  $output = stdout_of sub { $dist->run_build_pl };
  like($output, qr/Created MYMETA\.yml and MYMETA\.json/,
    "Ran Build.PL"
  );
  $mymeta = Parse::CPAN::Meta->load_file('MYMETA.yml');
  $mymeta2 = Parse::CPAN::Meta->load_file('MYMETA.json');
  is_deeply( $mymeta->{author}, [ 'John Gault' ],
    "MYMETA.yml preserved META.yml modifications"
  );
  is_deeply( $mymeta2->{author}, [ 'John Gault' ],
    "MYMETA.json preserved META.json modifications"
  );

}

#########################

# Test cleanup
{
  my $output = stdout_stderr_of sub { $dist->run_build('distcheck') };
  like($output, qr/Creating a temporary 'MANIFEST.SKIP'/,
    "MANIFEST.SKIP created for distcheck"
  );
  unlike($output, qr/MYMETA/,
    "MYMETA not flagged by distcheck"
  );
}


{
  my $output = stdout_of sub { $dist->run_build_pl };
  like($output, qr/Created MYMETA\.yml and MYMETA\.json/,
    "Ran Build.PL and saw MYMETA.yml creation message"
  );
  $output = stdout_stderr_of sub { $dist->run_build('distclean') };
  ok( ! -f 'MYMETA.yml', "No MYMETA.yml after distclean" );
  ok( ! -f 'MYMETA.json', "No MYMETA.json after distclean" );
  ok( ! -f 'MANIFEST.SKIP', "No MANIFEST.SKIP after distclean" );
}