xref: /openbsd-src/gnu/usr.bin/perl/cpan/ExtUtils-MakeMaker/t/basic.t (revision eac174f2741a08d8deb8aae59a7f778ef9b5d770)
1#!/usr/bin/perl -w
2
3# This test puts MakeMaker through the paces of a basic perl module
4# build, test and installation of the Big::Fat::Dummy module.
5
6# Module::Install relies on being able to patch the generated Makefile
7# to add flags to $(PERL)
8# This test includes adding ' -Iinc' to $(PERL), and checking 'make install'
9# after that works. Done here as back-compat is considered basic.
10
11BEGIN {
12    unshift @INC, 't/lib';
13}
14
15use strict;
16use warnings;
17use Config;
18use ExtUtils::MakeMaker;
19use utf8;
20
21use MakeMaker::Test::Utils;
22use MakeMaker::Test::Setup::BFD;
23use Config;
24use ExtUtils::MM;
25use Test::More
26    !MM->can_run(make()) && $ENV{PERL_CORE} && $Config{'usecrosscompile'}
27    ? (skip_all => "cross-compiling and make not available")
28    : (tests => 188);
29use File::Find;
30use File::Spec;
31use File::Path;
32use File::Temp qw[tempdir];
33
34my $perl = which_perl();
35my $Is_VMS = $^O eq 'VMS';
36my $OLD_CP; # crude but...
37my $w32worked; # or whether we had to fallback to chcp
38if ($^O eq "MSWin32") {
39    eval {
40        require Win32;
41        local $SIG{__WARN__} = sub {} if ( "$]" < 5.014 ); # suppress deprecation warning for inherited AUTOLOAD of Win32::GetConsoleCP()
42        $w32worked = $OLD_CP = Win32::GetConsoleCP();
43    };
44    $OLD_CP = $1 if !$w32worked and qx(chcp) =~ /(\d+)$/ and $? == 0;
45    if (defined $OLD_CP) {
46        if ($w32worked) {
47            Win32::SetConsoleCP(1252)
48        } else {
49            qx(chcp 1252);
50        }
51    }
52}
53END {
54    if ($^O eq "MSWin32" and defined $OLD_CP) {
55        if ($w32worked) {
56            Win32::SetConsoleCP($OLD_CP)
57        } else {
58            qx(chcp $OLD_CP);
59        }
60    }
61}
62
63chdir 't';
64perl_lib; # sets $ENV{PERL5LIB} relative to t/
65
66my $tmpdir = tempdir( DIR => '../t', CLEANUP => 1 );
67use Cwd; my $cwd = getcwd; END { chdir $cwd } # so File::Temp can cleanup
68chdir $tmpdir;
69
70my $Touch_Time = calibrate_mtime();
71
72$| = 1;
73
74ok( setup_recurs(), 'setup' );
75
76ok( chdir('Big-Dummy'), "chdir'd to Big-Dummy" ) ||
77  diag("chdir failed: $!");
78
79sub extrachar {
80  return 's'
81    if 1; # until Perl gains native support for Unicode filenames
82#    if $] <= 5.008 || $ENV{PERL_CORE}
83#      || $^O =~ /bsd|dragonfly|mswin32/i;
84  'š';
85}
86my $DUMMYINST = '../dummy-in'.extrachar().'tall';
87my @mpl_out = run(qq{$perl Makefile.PL "PREFIX=$DUMMYINST"});
88
89cmp_ok( $?, '==', 0, 'Makefile.PL exited with zero' ) ||
90  diag(@mpl_out);
91
92my $makefile = makefile_name();
93ok( grep(/^Writing $makefile for Big::Dummy/,
94         @mpl_out) == 1,
95                                           'Makefile.PL output looks right');
96
97ok( grep(/^Current package is: main$/,
98         @mpl_out) == 1,
99                                           'Makefile.PL run in package main');
100
101ok( -e $makefile,       'Makefile exists' );
102
103# -M is flakey on VMS
104my $mtime = (stat($makefile))[9];
105cmp_ok( $Touch_Time, '<=', $mtime,  '  been touched' );
106
107my $make = make_run();
108
109{
110    # Suppress 'make manifest' noise
111    local $ENV{PERL_MM_MANIFEST_VERBOSE} = 0;
112    my $manifest_out = run("$make manifest");
113    ok( -e 'MANIFEST',      'make manifest created a MANIFEST' );
114    ok( -s 'MANIFEST',      '  not empty' );
115}
116
117my $ppd_out = run("$make ppd");
118is( $?, 0,                      '  exited normally' ) || diag $ppd_out;
119ok( open(PPD, 'Big-Dummy.ppd'), '  .ppd file generated' );
120my $ppd_html;
121{ local $/; $ppd_html = <PPD> }
122close PPD;
123like( $ppd_html, qr{^<SOFTPKG NAME="Big-Dummy" VERSION="0.01">}m,
124                                                           '  <SOFTPKG>' );
125like( $ppd_html,
126      qr{^\s*<ABSTRACT>Try "our" hot dog's, \$andwiche\$ and \$\(ub\)\$!</ABSTRACT>}m,
127                                                           '  <ABSTRACT>');
128like( $ppd_html,
129      qr{^\s*<AUTHOR>Michael G Schwern &lt;schwern\@pobox.com&gt;</AUTHOR>}m,
130                                                           '  <AUTHOR>'  );
131like( $ppd_html, qr{^\s*<IMPLEMENTATION>}m,          '  <IMPLEMENTATION>');
132like( $ppd_html, qr{^\s*<REQUIRE NAME="strict::" />}m,  '  <REQUIRE>' );
133unlike( $ppd_html, qr{^\s*<REQUIRE NAME="warnings::" />}m,  'no <REQUIRE> for build_require' );
134
135my $archname = $Config{archname};
136# XXX This is a copy of the internal logic, so it's not a great test
137if( "$]" >= 5.010) {
138    $archname .= "-$^V->{version}->[0].$^V->{version}->[1]";
139}
140elsif( "$]" >= 5.008 ) {
141    $archname .= "-$Config{PERL_REVISION}.$Config{PERL_VERSION}";
142}
143like( $ppd_html, qr{^\s*<ARCHITECTURE NAME="$archname" />}m,
144                                                           '  <ARCHITECTURE>');
145like( $ppd_html, qr{^\s*<CODEBASE HREF="" />}m,            '  <CODEBASE>');
146like( $ppd_html, qr{^\s*</IMPLEMENTATION>}m,           '  </IMPLEMENTATION>');
147like( $ppd_html, qr{^\s*</SOFTPKG>}m,                      '  </SOFTPKG>');
148
149my $test_out = run("$make test");
150like( $test_out, qr/All tests successful/, 'make test' );
151is( $?, 0,                                 '  exited normally' ) ||
152    diag $test_out;
153
154# Test 'make test TEST_VERBOSE=1'
155my $make_test_verbose = make_macro($make, 'test', TEST_VERBOSE => 1);
156$test_out = run("$make_test_verbose");
157like( $test_out, qr/ok \d+ - TEST_VERBOSE/, 'TEST_VERBOSE' );
158like( $test_out, qr/ok \d+ - testing test.pl/, 'test.pl' ); # in test.pl
159like( $test_out, qr/ok \d+ - testing t\/\*.t/, 't/*.t' ); # in *.t
160like( $test_out, qr/All tests successful/,  '  successful' );
161is( $?, 0,                                  '  exited normally' ) ||
162    diag $test_out;
163
164# Test 'make testdb TEST_FILE=t/compile.t'
165# TESTDB_SW override is because perl -d is too clever for me to outwit
166my $make_testdb_file = make_macro(
167    $make,
168    'testdb',
169    TEST_FILE => 't/compile.t',
170    TESTDB_SW => '-Ixyzzy',
171);
172$test_out = run($make_testdb_file);
173unlike( $test_out, qr/harness/, 'no harness' );
174unlike( $test_out, qr/sanity\.t/, 'no wrong test' );
175like( $test_out, qr/compile\.t/, 'get right test' );
176like( $test_out, qr/xyzzy/, 'signs of TESTDB_SW' );
177is( $?, 0,                                  '  exited normally' ) ||
178    diag $test_out;
179
180# now simulate what Module::Install does, and edit $(PERL) to add flags
181open my $fh, '<', $makefile;
182my $mtext = join '', <$fh>;
183close $fh;
184$mtext =~ s/^(\s*PERL\s*=.*)$/$1 -Iinc/m;
185open $fh, '>', $makefile;
186print $fh $mtext;
187close $fh;
188
189my $install_out = run("$make install");
190is( $?, 0, 'install' ) || diag $install_out;
191like( $install_out, qr/^Installing /m );
192
193sub check_dummy_inst {
194    my ($loc, $skipsubdir) = @_;
195    my %files = ();
196    find( sub {
197	# do it case-insensitive for non-case preserving OSs
198	my $file = lc $_;
199	# VMS likes to put dots on the end of things that don't have them.
200	$file =~ s/\.$// if $Is_VMS;
201	$files{$file} = $File::Find::name;
202    }, $loc );
203    ok( $files{'dummy.pm'},     '  Dummy.pm installed' );
204    ok( $files{'liar.pm'},      '  Liar.pm installed'  ) unless $skipsubdir;
205    ok( $files{'program'},      '  program installed'  );
206    ok( $files{'.packlist'},    '  packlist created'   );
207    ok( $files{'perllocal.pod'},'  perllocal.pod created' );
208    \%files;
209}
210
211SKIP: {
212    ok( -r $DUMMYINST,     '  install dir created' )
213	or skip "$DUMMYINST doesn't exist", 5;
214    check_dummy_inst($DUMMYINST);
215}
216
217SKIP: {
218    skip 'VMS install targets do not preserve $(PREFIX)', 8 if $Is_VMS;
219
220    $install_out = run("$make install PREFIX=elsewhere");
221    is( $?, 0, 'install with PREFIX override' ) || diag $install_out;
222    like( $install_out, qr/^Installing /m );
223
224    ok( -r 'elsewhere',     '  install dir created' );
225    check_dummy_inst('elsewhere');
226    rmtree('elsewhere');
227}
228
229
230SKIP: {
231    skip 'VMS install targets do not preserve $(DESTDIR)', 10 if $Is_VMS;
232
233    $install_out = run("$make install PREFIX= DESTDIR=other");
234    is( $?, 0, 'install with DESTDIR' ) ||
235        diag $install_out;
236    like( $install_out, qr/^Installing /m );
237
238    ok( -d 'other',  '  destdir created' );
239    my $files = check_dummy_inst('other');
240
241    ok( open(PERLLOCAL, $files->{'perllocal.pod'} ) ) ||
242        diag("Can't open $files->{'perllocal.pod'}: $!");
243    { local $/;
244      unlike(<PERLLOCAL>, qr/other/, 'DESTDIR should not appear in perllocal');
245    }
246    close PERLLOCAL;
247
248# TODO not available in the min version of Test::Harness we require
249#    ok( open(PACKLIST, $files{'.packlist'} ) ) ||
250#        diag("Can't open $files{'.packlist'}: $!");
251#    { local $/;
252#      local $TODO = 'DESTDIR still in .packlist';
253#      unlike(<PACKLIST>, qr/other/, 'DESTDIR should not appear in .packlist');
254#    }
255#    close PACKLIST;
256
257    rmtree('other');
258}
259
260
261SKIP: {
262    skip 'VMS install targets do not preserve $(PREFIX)', 9 if $Is_VMS;
263
264    $install_out = run("$make install PREFIX=elsewhere DESTDIR=other/");
265    is( $?, 0, 'install with PREFIX override and DESTDIR' ) ||
266        diag $install_out;
267    like( $install_out, qr/^Installing /m );
268
269    ok( !-d 'elsewhere',       '  install dir not created' );
270    ok( -d 'other/elsewhere',  '  destdir created' );
271    check_dummy_inst('other/elsewhere');
272    rmtree('other');
273}
274
275my ($dist_test_out, $distdir, $meta_yml, $mymeta_yml, $meta_json, $mymeta_json);
276SKIP: {
277    skip 'disttest depends on metafile, which is not run in core', 1 if $ENV{PERL_CORE};
278    $dist_test_out = run("$make disttest");
279    is( $?, 0, 'disttest' ) || diag($dist_test_out);
280
281    # Test META.yml generation
282    use ExtUtils::Manifest qw(maniread);
283
284    $distdir  = 'Big-Dummy-0.01';
285    $distdir =~ s/\./_/g if $Is_VMS;
286    $meta_yml = "$distdir/META.yml";
287    $mymeta_yml = "$distdir/MYMETA.yml";
288    $meta_json = "$distdir/META.json";
289    $mymeta_json = "$distdir/MYMETA.json";
290}
291
292note "META file validity"; SKIP: {
293    skip 'disttest depends on metafile, which is not run in core', 104 if $ENV{PERL_CORE};
294
295    eval { require CPAN::Meta; };
296    skip 'Loading CPAN::Meta failed', 104 if $@;
297
298    ok( !-f 'META.yml',  'META.yml not written to source dir' );
299    ok( -f $meta_yml,    'META.yml written to dist dir' );
300    ok( !-e "META_new.yml", 'temp META.yml file not left around' );
301
302    ok( -f 'MYMETA.yml',  'MYMETA.yml is written to source dir' );
303    ok( -f $mymeta_yml,    'MYMETA.yml is written to dist dir on disttest' );
304
305    ok( !-f 'META.json',  'META.json not written to source dir' );
306    ok( -f $meta_json,    'META.json written to dist dir' );
307    ok( !-e "META_new.json", 'temp META.json file not left around' );
308
309    ok( -f 'MYMETA.json',  'MYMETA.json is written to source dir' );
310    ok( -f $mymeta_json,    'MYMETA.json is written to dist dir on disttest' );
311
312    for my $case (
313      ['META.yml', $meta_yml],
314      ['MYMETA.yml', $mymeta_yml],
315      ['META.json', $meta_json],
316      ['MYMETA.json', $mymeta_json],
317      ['MYMETA.yml', 'MYMETA.yml'],
318      ['MYMETA.json', 'MYMETA.json'],
319    ) {
320      my ($label, $meta_name) = @$case;
321      ok(
322        my $obj = eval {
323          CPAN::Meta->load_file($meta_name, {lazy_validation => 0})
324        },
325        "$label validates"
326      );
327      my $is = sub {
328        my ($m,$e) = @_;
329        is($obj->$m, $e, "$label -> $m")
330      };
331      my $is_list = sub {
332        my ($m,$e) = @_;
333        is_deeply([$obj->$m], $e, "$label -> $m")
334      };
335      my $is_map = sub {
336        my ($m,$e) = @_;
337        is_deeply($obj->$m, $e, "$label -> $m")
338      };
339      $is->( name => "Big-Dummy" );
340      $is->( version => "0.01" );
341      $is->( abstract => q{Try "our" hot dog's, $andwiche$ and $(ub)$!} );
342      $is_list->( licenses => [q{unknown}] );
343      $is_list->( authors => [ q{Michael G Schwern <schwern@pobox.com>} ] );
344      $is_map->( prereqs => {
345          configure => {
346            requires => {
347              'ExtUtils::MakeMaker' => 0
348            },
349          },
350          build => {
351            requires => {
352              'warnings' => 0
353            }
354          },
355          runtime => {
356            requires => {
357              'strict' => 0
358            }
359          },
360        }
361      );
362      $is_map->(
363        no_index => {
364          directory => [qw/t inc/],
365        }
366      );
367      $is->( dynamic_config => ($label =~ /MYMETA/) ? 0 : 1 );
368    }
369
370    my $manifest = maniread("$distdir/MANIFEST");
371    # VMS is non-case preserving, so we can't know what the MANIFEST will
372    # look like. :(
373    _normalize($manifest);
374    is( $manifest->{'meta.yml'}, 'Module YAML meta-data (added by MakeMaker)',
375      "MANIFEST has META.yml"
376    );
377    is( $manifest->{'meta.json'}, 'Module JSON meta-data (added by MakeMaker)',
378      "MANIFEST has META.json"
379    );
380
381    # Test NO_META META.yml suppression
382    for my $f ( $meta_yml, $meta_json, 'MYMETA.yml', 'MYMETA.json' ) {
383      1 while unlink $f;
384    }
385    ok( !-f $meta_yml,   'META.yml deleted' );
386    ok( !-f 'MYMETA.yml','MYMETA.yml deleted' );
387    ok( !-f $meta_json,   'META.json deleted' );
388    ok( !-f 'MYMETA.json','MYMETA.json deleted' );
389
390    @mpl_out = run(qq{$perl Makefile.PL "NO_META=1"});
391    ok( -f 'MYMETA.yml', 'MYMETA.yml generation not suppressed by NO_META' );
392    ok( -f 'MYMETA.json', 'MYMETA.json generation not suppressed by NO_META' );
393    cmp_ok( $?, '==', 0, 'Makefile.PL exited with zero' ) || diag(@mpl_out);
394    ok( !-f $meta_yml,   'META.yml generation suppressed by NO_META' );
395    ok( !-f $meta_json,   'META.json generation suppressed by NO_META' );
396    my $distdir_out = run("$make distdir");
397    is( $?, 0, 'distdir' ) || diag($distdir_out);
398    ok( !-f $meta_yml,   'META.yml generation suppressed by NO_META' );
399    ok( !-f $meta_json,   'META.json generation suppressed by NO_META' );
400
401    for my $f ( 'MYMETA.yml', 'MYMETA.json' ) {
402      1 while unlink $f;
403    }
404    ok( !-f 'MYMETA.yml','MYMETA.yml deleted' );
405    ok( !-f 'MYMETA.json','MYMETA.json deleted' );
406
407    @mpl_out = run(qq{$perl Makefile.PL "NO_MYMETA=1"});
408    cmp_ok( $?, '==', 0, 'Makefile.PL exited with zero' ) || diag(@mpl_out);
409    $distdir_out = run("$make distdir");
410    is( $?, 0, 'distdir' ) || diag($distdir_out);
411    ok( !-f 'MYMETA.yml','MYMETA.yml generation suppressed by NO_MYMETA' );
412    ok( !-f 'MYMETA.json','MYMETA.json generation suppressed by NO_MYMETA' );
413    ok( -f $meta_yml,    'META.yml generation not suppressed by NO_MYMETA' );
414    ok( -f $meta_json,    'META.json generation not suppressed by NO_MYMETA' );
415
416    # Test MYMETA really comes from META except for prereqs
417    for my $f ( $meta_yml, $meta_json, 'MYMETA.yml', 'MYMETA.json' ) {
418      1 while unlink $f;
419    }
420    @mpl_out = run(qq{$perl Makefile.PL});
421    cmp_ok( $?, '==', 0, 'Makefile.PL exited with zero' ) || diag(@mpl_out);
422    $distdir_out = run("$make distdir");
423    is( $?, 0, 'distdir' ) || diag($distdir_out);
424    ok( -f $meta_yml,    'META.yml generated in distdir' );
425    ok( -f $meta_json,    'META.json generated in distdir' );
426    ok( ! -f $mymeta_yml,    'MYMETA.yml not yet generated in distdir' );
427    ok( ! -f $mymeta_json,    'MYMETA.json generated in distdir' );
428    my $edit_meta = CPAN::Meta->load_file($meta_json)->as_struct;
429    $edit_meta->{abstract} = "New abstract";
430    my $meta_obj = CPAN::Meta->new($edit_meta);
431    is( $meta_obj->abstract, "New abstract", "MYMETA abstract from META, not Makefile.PL");
432    ok( $meta_obj->save($meta_json), "Saved edited META.json in distdir" );
433    ok( $meta_obj->save($meta_yml, {version => 1.4}), "Saved edited META.yml in distdir");
434    ok( chdir $distdir );
435    ok( -f 'META.yml',    'META.yml confirmed in distdir' );
436    ok( -f 'META.json',    'META.json confirmed in distdir' );
437    @mpl_out = run(qq{$perl Makefile.PL});
438    cmp_ok( $?, '==', 0, 'Makefile.PL in distdir exited with zero' ) || diag(@mpl_out);
439    ok( chdir File::Spec->updir );
440    ok( -f $mymeta_yml,    'MYMETA.yml generated in distdir' );
441    ok( -f $mymeta_json,    'MYMETA.json generated in distdir' );
442    $meta_obj = CPAN::Meta->load_file($meta_json);
443    is( $meta_obj->abstract, "New abstract", "META abstract is same as was saved");
444    $meta_obj = CPAN::Meta->load_file($mymeta_json);
445    is( $meta_obj->abstract, "New abstract", "MYMETA abstract from META, not Makefile.PL");
446}
447
448
449# Make sure init_dirscan doesn't go into the distdir
450# also with a "messup.PL" that will make a build fail
451open $fh, '>', 'messup.PL' or die "messup.PL: $!";
452print $fh 'print "Extracting messup (with variable substitutions)\n";' . "\n";
453print $fh 'die';
454close $fh;
455@mpl_out = run(qq{$perl Makefile.PL "PREFIX=$DUMMYINST"});
456
457cmp_ok( $?, '==', 0, 'Makefile.PL exited with zero' ) || diag(@mpl_out);
458
459ok( grep(/^Writing $makefile for Big::Dummy/, @mpl_out) == 1,
460                                'init_dirscan skipped distdir') ||
461  diag(@mpl_out);
462
463# "make test" straight after "perl Makefile.PL" is expected to work same as
464#   "make all test" so check that with "messup.PL" that will make the
465#   build step fail
466$test_out = run("$make test");
467unlike( $test_out, qr/All tests successful/, 'make test caused build' );
468isnt( $?, 0,                                 '  build should fail' ) ||
469    diag $test_out;
470
471# I know we'll get ignored errors from make here, that's ok.
472# Send STDERR off to oblivion.
473open(SAVERR, ">&STDERR") or die $!;
474open(STDERR, ">",File::Spec->devnull) or die $!;
475
476my $realclean_out = run("$make realclean");
477is( $?, 0, 'realclean' ) || diag($realclean_out);
4781 while unlink 'messup.PL'; # also zap deliberate build-breaker
479
480open(STDERR, ">&SAVERR") or die $!;
481close SAVERR;
482
483# test linkext=>{LINKTYPE=>''} still installs a pure-perl installation
484# warning, edits the Makefile.PL so either rewrite after this or do this last
485my $file = 'Makefile.PL';
486my $text = slurp $file;
487ok(($text =~ s#\);#    linkext=>{LINKTYPE=>''},\n$&#), 'successful M.PL edit');
488open $fh, '>', $file or die "$file: $!";
489print $fh $text;
490close $fh;
491# now do with "Liar" subdir still there
492rmtree $DUMMYINST; # so no false positive from before
493@mpl_out = run(qq{$perl Makefile.PL "PREFIX=$DUMMYINST"});
494$install_out = run("$make install");
495check_dummy_inst($DUMMYINST);
496# now clean, delete "Liar" subdir, do again
497$realclean_out = run("$make realclean");
498rmtree 'Liar';
499rmtree $DUMMYINST; # so no false positive from before
500@mpl_out = run(qq{$perl Makefile.PL "PREFIX=$DUMMYINST"});
501$install_out = run("$make install");
502check_dummy_inst($DUMMYINST, 1);
503
504sub _normalize {
505    my $hash = shift;
506
507    %$hash= map { lc($_) => $hash->{$_} } keys %$hash;
508}
509