xref: /openbsd-src/gnu/usr.bin/perl/cpan/ExtUtils-MakeMaker/t/min_perl_version.t (revision 6ca44032e7be0d795b9f13c99fbce059e942c15d)
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 => 35);
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 => '999999',
132        );
133    };
134    ok( '' ne $warnings, 'MIN_PERL_VERSION=999999 triggers a warning' );
135    is( $warnings,
136        "Warning: Perl version 999999.000 or higher required. We run $].\n",
137                         '  with expected message text' );
138    is( $@, '',          '  and without a hard failure' );
139
140    $warnings = '';
141    eval {
142        WriteMakefile(
143            NAME             => 'Min::PerlVers',
144            MIN_PERL_VERSION => '999999',
145            PREREQ_FATAL     => 1,
146        );
147    };
148    is( $warnings, '', 'MIN_PERL_VERSION=999999 and PREREQ_FATAL: no warning' );
149    is( $@, <<"END",   '  correct exception' );
150MakeMaker FATAL: Perl version 999999.000 or higher required. We run $].
151END
152
153    $warnings = '';
154    eval {
155        WriteMakefile(
156            NAME             => 'Min::PerlVers',
157            MIN_PERL_VERSION => 'foobar',
158        );
159    };
160    is( $@, <<'END', 'Invalid MIN_PERL_VERSION is fatal' );
161MakeMaker FATAL: MIN_PERL_VERSION (foobar) is not in a recognized format.
162Recommended is a quoted numerical value like '5.005' or '5.008001'.
163END
164
165}
166
167
168note "PREREQ_PRINT output"; {
169    my $prereq_out = run(qq{$perl Makefile.PL "PREREQ_PRINT=1"});
170    is( $?, 0,            'PREREQ_PRINT exiting normally' );
171    $prereq_out =~ s/.*(\$PREREQ_PM\s*=)/$1/s; # strip off errors eg from chcp
172    my $prereq_out_sane = $prereq_out =~ /^\s*\$PREREQ_PM\s*=/;
173    ok( $prereq_out_sane, '  and talking like we expect' ) ||
174        diag($prereq_out);
175
176    SKIP: {
177        skip 'not going to evaluate rubbish', 3 if !$prereq_out_sane;
178
179        package _Prereq::Print::WithMPV;          ## no critic
180        our($PREREQ_PM, $BUILD_REQUIRES, $MIN_PERL_VERSION, $ERR);
181        $BUILD_REQUIRES = undef; # suppress "used only once"
182        $ERR = '';
183        eval {
184            eval $prereq_out;                     ## no critic
185            $ERR = $@;
186        };
187        ::is( $@ . $ERR, '',                      'prereqs evaluable' );
188        ::is_deeply( $PREREQ_PM, { strict => 0 }, '  and looking correct' );
189        ::is( $MIN_PERL_VERSION, '5.005',         'min version also correct' );
190    }
191}
192
193
194note "PRINT_PREREQ output"; {
195    my $prereq_out = run(qq{$perl Makefile.PL "PRINT_PREREQ=1"});
196    is( $?, 0,                      'PRINT_PREREQ exiting normally' );
197    ok( $prereq_out !~ /^warning/i, '  and not complaining loudly' );
198    like( $prereq_out,
199        qr/^perl\(perl\) \s* >= 5\.005 \s+ perl\(strict\) \s* >= \s* 0 \s*$/mx,
200                                    'dump has prereqs and perl version' );
201}
202
203
204note "generated files verification"; {
205    unlink $makefile;
206    my @mpl_out = run(qq{$perl Makefile.PL});
207    END { unlink $makefile, makefile_backup() }
208
209    cmp_ok( $?, '==', 0, 'Makefile.PL exiting normally' ) || diag(@mpl_out);
210    ok( -e $makefile, 'Makefile present' );
211}
212
213
214note "ppd output"; {
215    my $ppd_file = 'Min-PerlVers.ppd';
216    my @make_out = run(qq{$make ppd});
217    END { unlink $ppd_file }
218
219    cmp_ok( $?, '==', 0,    'Make ppd exiting normally' ) || diag(@make_out);
220
221    my $ppd_html = slurp($ppd_file);
222    ok( defined($ppd_html), '  .ppd file present' );
223
224    like( $ppd_html, qr{^\s*<PERLCORE VERSION="5,005,0,0" />}m,
225                            '  .ppd file content good' );
226}
227
228
229note "META.yml output"; SKIP: {
230    skip 'Failed to load CPAN::Meta', 4 unless $CM;
231    my $distdir  = 'Min-PerlVers-0.05';
232    $distdir =~ s{\.}{_}g if $Is_VMS;
233
234    my $meta_yml = "$distdir/META.yml";
235    my $meta_json = "$distdir/META.json";
236    my @make_out    = run(qq{$make metafile});
237    END { rmtree $distdir if defined $distdir }
238
239    for my $case (
240        ['META.yml', $meta_yml],
241        ['META.json', $meta_json],
242    ) {
243        my ($label, $meta_name) = @$case;
244        ok(
245          my $obj = eval {
246            CPAN::Meta->load_file($meta_name, {lazy_validation => 0})
247          },
248          "$label validates"
249        );
250        is( $obj->prereqs->{runtime}{requires}{perl}, '5.005',
251          "$label has runtime/requires perl 5.005"
252        );
253    }
254}
255