xref: /openbsd-src/gnu/usr.bin/perl/cpan/ExtUtils-MakeMaker/t/min_perl_version.t (revision e068048151d29f2562a32185e21a8ba885482260)
1#!/usr/bin/perl -w
2
3# This is a test checking various aspects of the optional argument
4# MIN_PERL_VERSION to WriteMakefile.
5
6BEGIN {
7    unshift @INC, 't/lib';
8}
9
10use strict;
11use warnings;
12
13use TieOut;
14use MakeMaker::Test::Utils;
15use Config;
16use ExtUtils::MM;
17use Test::More
18    !MM->can_run(make()) && $ENV{PERL_CORE} && $Config{'usecrosscompile'}
19    ? (skip_all => "cross-compiling and make not available")
20    : (tests => 37);
21use File::Path;
22
23use ExtUtils::MakeMaker;
24my $CM = eval { require CPAN::Meta; };
25
26my $DIRNAME = 'Min-PerlVers';
27my %FILES = (
28    'Makefile.PL'   => <<'END',
29use ExtUtils::MakeMaker;
30WriteMakefile(
31    NAME             => 'Min::PerlVers',
32    AUTHOR           => 'John Doe <jd@example.com>',
33    VERSION_FROM     => 'lib/Min/PerlVers.pm',
34    PREREQ_PM        => { strict => 0 },
35    MIN_PERL_VERSION => '5.005',
36);
37END
38
39    'lib/Min/PerlVers.pm'    => <<'END',
40package Min::PerlVers;
41$VERSION = 0.05;
42
43=head1 NAME
44
45Min::PerlVers - being picky about perl versions
46
47=cut
48
491;
50END
51
52);
53
54# avoid environment variables interfering with our make runs
55delete @ENV{qw(PERL_JSON_BACKEND CPAN_META_JSON_BACKEND PERL_YAML_BACKEND)} if $ENV{PERL_CORE};
56delete @ENV{qw(LIB MAKEFLAGS PERL_CORE)};
57
58my $perl     = which_perl();
59my $make     = make_run();
60my $makefile = makefile_name();
61
62chdir 't';
63
64perl_lib();
65
66rmtree($DIRNAME);
67hash2files($DIRNAME, \%FILES);
68END {
69    ok( chdir(File::Spec->updir), 'leaving dir' );
70    ok( rmtree($DIRNAME), 'teardown' );
71}
72
73ok( chdir 'Min-PerlVers', 'entering dir Min-PerlVers' ) ||
74    diag("chdir failed: $!");
75
76note "Argument verification"; {
77    my $stdout = tie *STDOUT, 'TieOut';
78    ok( $stdout, 'capturing stdout' );
79    my $warnings = '';
80    local $SIG{__WARN__} = sub {
81        $warnings .= join '', @_;
82    };
83
84    eval {
85        WriteMakefile(
86            NAME             => 'Min::PerlVers',
87            MIN_PERL_VERSION => '5',
88        );
89    };
90    is( $warnings, '', 'MIN_PERL_VERSION=5 does not trigger a warning' );
91    is( $@, '',        '  nor a hard failure' );
92
93
94    $warnings = '';
95    eval {
96        WriteMakefile(
97            NAME             => 'Min::PerlVers',
98            MIN_PERL_VERSION => '5.4.4',
99        );
100    };
101    is( $warnings, '', 'MIN_PERL_VERSION=X.Y.Z does not trigger a warning' );
102    is( $@, '',        '  nor a hard failure' );
103
104
105    $warnings = '';
106    eval {
107        WriteMakefile(
108            NAME             => 'Min::PerlVers',
109            MIN_PERL_VERSION => 5.4.4,
110        );
111    };
112    is( $warnings, '', 'MIN_PERL_VERSION=X.Y.Z does not trigger a warning' );
113    is( $@, '',        '  nor a hard failure' );
114
115
116    $warnings = '';
117    eval {
118        WriteMakefile(
119            NAME             => 'Min::PerlVers',
120            MIN_PERL_VERSION => v5.4.4,
121        );
122    };
123    is( $warnings, '', 'MIN_PERL_VERSION=X.Y.Z does not trigger a warning' );
124    is( $@, '',        '  nor a hard failure' );
125
126
127    $warnings = '';
128    eval {
129        WriteMakefile(
130            NAME             => 'Min::PerlVers',
131            MIN_PERL_VERSION => '5.005_04',
132        );
133    };
134    is( $warnings, '', 'MIN_PERL_VERSION=5.005_04 does not trigger a warning' );
135    is( $@, '',        '  nor a hard failure' );
136
137
138    $warnings = '';
139    eval {
140        WriteMakefile(
141            NAME             => 'Min::PerlVers',
142            MIN_PERL_VERSION => '999999',
143        );
144    };
145    ok( '' ne $warnings, 'MIN_PERL_VERSION=999999 triggers a warning' );
146    is( $warnings,
147        "Warning: Perl version 999999.000 or higher required. We run $].\n",
148                         '  with expected message text' );
149    is( $@, '',          '  and without a hard failure' );
150
151    $warnings = '';
152    eval {
153        WriteMakefile(
154            NAME             => 'Min::PerlVers',
155            MIN_PERL_VERSION => '999999',
156            PREREQ_FATAL     => 1,
157        );
158    };
159    is( $warnings, '', 'MIN_PERL_VERSION=999999 and PREREQ_FATAL: no warning' );
160    is( $@, <<"END",   '  correct exception' );
161MakeMaker FATAL: Perl version 999999.000 or higher required. We run $].
162END
163
164    $warnings = '';
165    eval {
166        WriteMakefile(
167            NAME             => 'Min::PerlVers',
168            MIN_PERL_VERSION => 'foobar',
169        );
170    };
171    is( $@, <<'END', 'Invalid MIN_PERL_VERSION is fatal' );
172MakeMaker FATAL: MIN_PERL_VERSION (foobar) is not in a recognized format.
173Recommended is a quoted numerical value like '5.005' or '5.008001'.
174END
175
176}
177
178
179note "PREREQ_PRINT output"; {
180    my $prereq_out = run(qq{$perl Makefile.PL "PREREQ_PRINT=1"});
181    is( $?, 0,            'PREREQ_PRINT exiting normally' );
182    $prereq_out =~ s/.*(\$PREREQ_PM\s*=)/$1/s; # strip off errors eg from chcp
183    my $prereq_out_sane = $prereq_out =~ /^\s*\$PREREQ_PM\s*=/;
184    ok( $prereq_out_sane, '  and talking like we expect' ) ||
185        diag($prereq_out);
186
187    SKIP: {
188        skip 'not going to evaluate rubbish', 3 if !$prereq_out_sane;
189
190        package _Prereq::Print::WithMPV;          ## no critic
191        our($PREREQ_PM, $BUILD_REQUIRES, $MIN_PERL_VERSION, $ERR);
192        $BUILD_REQUIRES = undef; # suppress "used only once"
193        $ERR = '';
194        eval {
195            eval $prereq_out;                     ## no critic
196            $ERR = $@;
197        };
198        ::is( $@ . $ERR, '',                      'prereqs evaluable' );
199        ::is_deeply( $PREREQ_PM, { strict => 0 }, '  and looking correct' );
200        ::is( $MIN_PERL_VERSION, '5.005',         'min version also correct' );
201    }
202}
203
204
205note "PRINT_PREREQ output"; {
206    my $prereq_out = run(qq{$perl Makefile.PL "PRINT_PREREQ=1"});
207    is( $?, 0,                      'PRINT_PREREQ exiting normally' );
208    ok( $prereq_out !~ /^warning/i, '  and not complaining loudly' );
209    like( $prereq_out,
210        qr/^perl\(perl\) \s* >= 5\.005 \s+ perl\(strict\) \s* >= \s* 0 \s*$/mx,
211                                    'dump has prereqs and perl version' );
212}
213
214
215note "generated files verification"; {
216    unlink $makefile;
217    my @mpl_out = run(qq{$perl Makefile.PL});
218    END { unlink $makefile, makefile_backup() }
219
220    cmp_ok( $?, '==', 0, 'Makefile.PL exiting normally' ) || diag(@mpl_out);
221    ok( -e $makefile, 'Makefile present' );
222}
223
224
225note "ppd output"; {
226    my $ppd_file = 'Min-PerlVers.ppd';
227    my @make_out = run(qq{$make ppd});
228    END { unlink $ppd_file }
229
230    cmp_ok( $?, '==', 0,    'Make ppd exiting normally' ) || diag(@make_out);
231
232    my $ppd_html = slurp($ppd_file);
233    ok( defined($ppd_html), '  .ppd file present' );
234
235    like( $ppd_html, qr{^\s*<PERLCORE VERSION="5,005,0,0" />}m,
236                            '  .ppd file content good' );
237}
238
239
240note "META.yml output"; SKIP: {
241    skip 'Failed to load CPAN::Meta', 4 unless $CM;
242    my $distdir  = 'Min-PerlVers-0.05';
243    $distdir =~ s{\.}{_}g if $Is_VMS;
244
245    my $meta_yml = "$distdir/META.yml";
246    my $meta_json = "$distdir/META.json";
247    my @make_out    = run(qq{$make metafile});
248    END { rmtree $distdir if defined $distdir }
249
250    for my $case (
251        ['META.yml', $meta_yml],
252        ['META.json', $meta_json],
253    ) {
254        my ($label, $meta_name) = @$case;
255        ok(
256          my $obj = eval {
257            CPAN::Meta->load_file($meta_name, {lazy_validation => 0})
258          },
259          "$label validates"
260        );
261        is( $obj->prereqs->{runtime}{requires}{perl}, '5.005',
262          "$label has runtime/requires perl 5.005"
263        );
264    }
265}
266