[RESEND] [PATCH] Mac OS lib patches for bleadperl
[p5sagit/p5-mst-13.2.git] / t / lib / extutils.t
CommitLineData
af6c647e 1#!./perl -w
2
3414cef0 3print "1..21\n";
af6c647e 4
5BEGIN {
6 chdir 't' if -d 't';
7 @INC = '../lib';
8}
9
10use warnings;
11use strict;
12use ExtUtils::MakeMaker;
13use ExtUtils::Constant qw (constant_types C_constant XS_constant autoload);
14use Config;
835f860c 15use File::Spec::Functions;
16use File::Spec;
17# Because were are going to be changing directory before running Makefile.PL
18my $perl = File::Spec->rel2abs( $^X );
6d79cad2 19# ExtUtils::Constant::C_constant uses $^X inside a comment, and we want to
20# compare output to ensure that it is the same. We were probably run as ./perl
21# whereas we will run the child with the full path in $perl. So make $^X for
22# us the same as our child will see.
23$^X = $perl;
af6c647e 24
835f860c 25print "# perl=$perl\n";
6d79cad2 26my $runperl = "$perl -x \"-I../../lib\"";
94b1a389 27
af6c647e 28$| = 1;
29
30my $dir = "ext-$$";
0ddb8edc 31my @files;
94b1a389 32
33print "# $dir being created...\n";
34mkdir $dir, 0777 or die "mkdir: $!\n";
35
af6c647e 36
37END {
94b1a389 38 use File::Path;
39 print "# $dir being removed...\n";
40 rmtree($dir);
af6c647e 41}
42
6d79cad2 43my $package = "ExtTest";
44
835f860c 45my @names = ("FIVE", {name=>"OK6", type=>"PV",},
46 {name=>"OK7", type=>"PVN",
47 value=>['"not ok 7\\n\\0ok 7\\n"', 15]},
af6c647e 48 {name => "FARTHING", type=>"NV"},
6d79cad2 49 {name => "NOT_ZERO", type=>"UV", value=>"~(UV)0"},
3414cef0 50 {name => "OPEN", type=>"PV", value=>'"/*"',
51 macro=>["#if 1\n", "#endif\n"]},
6d79cad2 52 {name => "CLOSE", type=>"PV", value=>'"*/"',
53 macro=>["#if 1\n", "#endif\n"]},
3414cef0 54 {name => "ANSWER", default=>["UV", 42]}, "NOTDEF",
55 {name => "Yes", type=>"YES"},
56 {name => "No", type=>"NO"},
57 {name => "Undef", type=>"UNDEF"}
58);
af6c647e 59
60my @names_only = map {(ref $_) ? $_->{name} : $_} @names;
61
6d79cad2 62my $types = {};
63my $constant_types = constant_types(); # macro defs
64my $C_constant = join "\n",
65 C_constant ($package, undef, "IV", $types, undef, undef, @names);
66my $XS_constant = XS_constant ($package, $types); # XS for ExtTest::constant
67
af6c647e 68################ Header
94b1a389 69my $header = catfile($dir, "test.h");
0ddb8edc 70push @files, "test.h";
94b1a389 71open FH, ">$header" or die "open >$header: $!\n";
af6c647e 72print FH <<'EOT';
835f860c 73#define FIVE 5
74#define OK6 "ok 6\n"
75#define OK7 1
af6c647e 76#define FARTHING 0.25
77#define NOT_ZERO 1
3414cef0 78#define Yes 0
79#define No 1
80#define Undef 1
6d79cad2 81#undef NOTDEF
af6c647e 82EOT
94b1a389 83close FH or die "close $header: $!\n";
af6c647e 84
85################ XS
94b1a389 86my $xs = catfile($dir, "$package.xs");
0ddb8edc 87push @files, "$package.xs";
94b1a389 88open FH, ">$xs" or die "open >$xs: $!\n";
af6c647e 89
90print FH <<'EOT';
91#include "EXTERN.h"
92#include "perl.h"
93#include "XSUB.h"
94EOT
95
96print FH "#include \"test.h\"\n\n";
6d79cad2 97print FH $constant_types;
98print FH $C_constant, "\n";
af6c647e 99print FH "MODULE = $package PACKAGE = $package\n";
100print FH "PROTOTYPES: ENABLE\n";
6d79cad2 101print FH $XS_constant;
94b1a389 102close FH or die "close $xs: $!\n";
af6c647e 103
104################ PM
94b1a389 105my $pm = catfile($dir, "$package.pm");
0ddb8edc 106push @files, "$package.pm";
94b1a389 107open FH, ">$pm" or die "open >$pm: $!\n";
af6c647e 108print FH "package $package;\n";
109print FH "use $];\n";
110
111print FH <<'EOT';
112
113use strict;
114use warnings;
115use Carp;
116
117require Exporter;
118require DynaLoader;
af6c647e 119use vars qw ($VERSION @ISA @EXPORT_OK);
120
121$VERSION = '0.01';
122@ISA = qw(Exporter DynaLoader);
123@EXPORT_OK = qw(
124EOT
125
126print FH "\t$_\n" foreach (@names_only);
127print FH ");\n";
128print FH autoload ($package, $]);
129print FH "bootstrap $package \$VERSION;\n1;\n__END__\n";
94b1a389 130close FH or die "close $pm: $!\n";
af6c647e 131
132################ test.pl
94b1a389 133my $testpl = catfile($dir, "test.pl");
0ddb8edc 134push @files, "test.pl";
94b1a389 135open FH, ">$testpl" or die "open >$testpl: $!\n";
af6c647e 136
6d79cad2 137print FH "use strict;\n";
af6c647e 138print FH "use $package qw(@names_only);\n";
139print FH <<'EOT';
140
6d79cad2 141# IV
835f860c 142my $five = FIVE;
143if ($five == 5) {
144 print "ok 5\n";
af6c647e 145} else {
835f860c 146 print "not ok 5 # $five\n";
af6c647e 147}
148
6d79cad2 149# PV
835f860c 150print OK6;
af6c647e 151
6d79cad2 152# PVN containing embedded \0s
835f860c 153$_ = OK7;
af6c647e 154s/.*\0//s;
155print;
156
6d79cad2 157# NV
af6c647e 158my $farthing = FARTHING;
159if ($farthing == 0.25) {
835f860c 160 print "ok 8\n";
af6c647e 161} else {
835f860c 162 print "not ok 8 # $farthing\n";
af6c647e 163}
164
6d79cad2 165# UV
af6c647e 166my $not_zero = NOT_ZERO;
167if ($not_zero > 0 && $not_zero == ~0) {
835f860c 168 print "ok 9\n";
af6c647e 169} else {
835f860c 170 print "not ok 9 # \$not_zero=$not_zero ~0=" . (~0) . "\n";
af6c647e 171}
172
6d79cad2 173# Value includes a "*/" in an attempt to bust out of a C comment.
174# Also tests custom cpp #if clauses
175my $close = CLOSE;
176if ($close eq '*/') {
177 print "ok 10\n";
178} else {
179 print "not ok 10 # \$close='$close'\n";
180}
181
182# Default values if macro not defined.
183my $answer = ANSWER;
184if ($answer == 42) {
185 print "ok 11\n";
186} else {
187 print "not ok 11 # What do you get if you multiply six by nine? '$answer'\n";
188}
189
190# not defined macro
191my $notdef = eval { NOTDEF; };
192if (defined $notdef) {
193 print "not ok 12 # \$notdef='$notdef'\n";
194} elsif ($@ !~ /Your vendor has not defined ExtTest macro NOTDEF/) {
195 print "not ok 12 # \$@='$@'\n";
196} else {
197 print "ok 12\n";
198}
199
200# not a macro
201my $notthere = eval { &ExtTest::NOTTHERE; };
202if (defined $notthere) {
203 print "not ok 13 # \$notthere='$notthere'\n";
204} elsif ($@ !~ /NOTTHERE is not a valid ExtTest macro/) {
205 chomp $@;
206 print "not ok 13 # \$@='$@'\n";
207} else {
208 print "ok 13\n";
209}
af6c647e 210
3414cef0 211# Truth
212my $yes = Yes;
213if ($yes) {
214 print "ok 14\n";
215} else {
216 print "not ok 14 # $yes='\$yes'\n";
217}
218
219# Falsehood
220my $no = No;
221if (defined $no and !$no) {
222 print "ok 15\n";
223} else {
224 print "not ok 15 # \$no=" . defined ($no) ? "'$no'\n" : "undef\n";
225}
226
227# Undef
228my $undef = Undef;
229unless (defined $undef) {
230 print "ok 16\n";
231} else {
232 print "not ok 16 # \$undef='$undef'\n";
233}
234
af6c647e 235EOT
236
94b1a389 237close FH or die "close $testpl: $!\n";
af6c647e 238
835f860c 239################ Makefile.PL
6d79cad2 240# We really need a Makefile.PL because make test for a no dynamic linking perl
241# will run Makefile.PL again as part of the "make perl" target.
94b1a389 242my $makefilePL = catfile($dir, "Makefile.PL");
0ddb8edc 243push @files, "Makefile.PL";
94b1a389 244open FH, ">$makefilePL" or die "open >$makefilePL: $!\n";
835f860c 245print FH <<"EOT";
6d79cad2 246#!$perl -w
835f860c 247use ExtUtils::MakeMaker;
248WriteMakefile(
249 'NAME' => "$package",
250 'VERSION_FROM' => "$package.pm", # finds \$VERSION
251 (\$] >= 5.005 ?
252 (#ABSTRACT_FROM => "$package.pm", # XXX add this
253 AUTHOR => "$0") : ())
254 );
255EOT
256
94b1a389 257close FH or die "close $makefilePL: $!\n";
af6c647e 258
259chdir $dir or die $!; push @INC, '../../lib';
260END {chdir ".." or warn $!};
261
835f860c 262my @perlout = `$runperl Makefile.PL`;
263if ($?) {
264 print "not ok 1 # $runperl Makefile.PL failed: $?\n";
265 print "# $_" foreach @perlout;
266 exit($?);
267} else {
268 print "ok 1\n";
269}
270
271
24874030 272my $makefile = ($^O eq 'VMS' ? 'descrip' : 'Makefile');
273my $makefile_ext = ($^O eq 'VMS' ? '.mms' : '');
274if (-f "$makefile$makefile_ext") {
835f860c 275 print "ok 2\n";
af6c647e 276} else {
835f860c 277 print "not ok 2\n";
af6c647e 278}
24874030 279my $makefile_rename = ($^O eq 'VMS' ? '.mms' : '.old');
280push @files, "$makefile$makefile_rename"; # Renamed by make clean
af6c647e 281
282my $make = $Config{make};
94b1a389 283
af6c647e 284$make = $ENV{MAKE} if exists $ENV{MAKE};
94b1a389 285
286my $makeout;
287
af6c647e 288print "# make = '$make'\n";
94b1a389 289$makeout = `$make`;
290if ($?) {
835f860c 291 print "not ok 3 # $make failed: $?\n";
94b1a389 292 exit($?);
af6c647e 293} else {
835f860c 294 print "ok 3\n";
295}
296
297if ($Config{usedl}) {
298 print "ok 4\n";
299} else {
300 push @files, "perl$Config{exe_ext}";
301 my $makeperl = "$make perl";
302 print "# make = '$makeperl'\n";
303 $makeout = `$makeperl`;
304 if ($?) {
305 print "not ok 4 # $makeperl failed: $?\n";
306 exit($?);
307 } else {
308 print "ok 4\n";
309 }
af6c647e 310}
311
3414cef0 312my $test = 17;
0ddb8edc 313my $maketest = "$make test";
314print "# make = '$maketest'\n";
315$makeout = `$maketest`;
94b1a389 316
3414cef0 317# echo of running the test script
318$makeout =~ s/^\s*PERL_DL_NONLAZY=.+?\n//m;
319$makeout =~ s/^MCR.+test.pl\n//mig if $^O eq 'VMS';
320
321# GNU make babblings
322$makeout =~ s/^\w*?make.+?(?:entering|leaving) directory.+?\n//mig;
94b1a389 323
3414cef0 324# Hopefully gets most make's babblings
325# make -f Makefile.aperl perl
326$makeout =~ s/^\w*?make.+\sperl[^A-Za-z0-9]*\n//mig;
327# make[1]: `perl' is up to date.
328$makeout =~ s/^\w*?make.+perl.+?is up to date.*?\n//mig;
835f860c 329
3414cef0 330print $makeout;
331
332if ($?) {
333 print "not ok $test # $maketest failed: $?\n";
334} else {
6d79cad2 335 print "ok $test\n";
336}
337$test++;
338
339my $regen = `$runperl $package.xs`;
340if ($?) {
341 print "not ok $test # $runperl $package.xs failed: $?\n";
342} else {
343 print "ok $test\n";
af6c647e 344}
6d79cad2 345$test++;
346
347my $expect = $constant_types . $C_constant .
348 "\n#### XS Section:\n" . $XS_constant;
349
350if ($expect eq $regen) {
351 print "ok $test\n";
352} else {
353 print "not ok $test\n";
354 # open FOO, ">expect"; print FOO $expect;
355 # open FOO, ">regen"; print FOO $regen; close FOO;
356}
357$test++;
0ddb8edc 358
359my $makeclean = "$make clean";
360print "# make = '$makeclean'\n";
361$makeout = `$makeclean`;
362if ($?) {
6d79cad2 363 print "not ok $test # $make failed: $?\n";
0ddb8edc 364} else {
6d79cad2 365 print "ok $test\n";
0ddb8edc 366}
6d79cad2 367$test++;
0ddb8edc 368
369foreach (@files) {
370 unlink $_ or warn "unlink $_: $!";
371}
372
373my $fail;
374opendir DIR, "." or die "opendir '.': $!";
375while (defined (my $entry = readdir DIR)) {
376 next if $entry =~ /^\.\.?$/;
377 print "# Extra file '$entry'\n";
378 $fail = 1;
379}
380closedir DIR or warn "closedir '.': $!";
381if ($fail) {
6d79cad2 382 print "not ok $test\n";
0ddb8edc 383} else {
6d79cad2 384 print "ok $test\n";
0ddb8edc 385}