This is a live mirror of the Perl 5 development currently hosted at https://github.com/perl/perl5
Math::BigRat 0.22
[perl5.git] / lib / h2xs.t
... / ...
CommitLineData
1#!./perl -w
2
3# Some quick tests to see if h2xs actually runs and creates files as
4# expected. File contents include date stamps and/or usernames
5# hence are not checked. File existence is checked with -e though.
6# This test depends on File::Path::rmtree() to clean up with.
7# - pvhp
8#
9# We are now checking that the correct use $version; is present in
10# Makefile.PL and $module.pm
11
12BEGIN {
13 chdir 't' if -d 't';
14 @INC = '../lib';
15 # FIXME (or rather FIXh2xs)
16 require Config;
17 if (($Config::Config{'extensions'} !~ m!\bDevel/PPPort\b!) ){
18 print "1..0 # Skip -- Perl configured without Devel::PPPort module\n";
19 exit 0;
20 }
21}
22
23# use strict; # we are not really testing this
24use File::Path; # for cleaning up with rmtree()
25use Test::More;
26use File::Spec;
27use File::Find;
28use ExtUtils::Manifest;
29# Don't want its diagnostics getting in the way of ours.
30$ExtUtils::Manifest::Quiet=1;
31my $up = File::Spec->updir();
32
33my $extracted_program = '../utils/h2xs'; # unix, nt, ...
34if ($^O eq 'VMS') { $extracted_program = '[-.utils]h2xs.com'; }
35if ($^O eq 'MacOS') { $extracted_program = '::utils:h2xs'; }
36if (!(-e $extracted_program)) {
37 print "1..0 # Skip: $extracted_program was not built\n";
38 exit 0;
39}
40# You might also wish to bail out if your perl platform does not
41# do `$^X -e 'warn "Writing h2xst"' 2>&1`; duplicity.
42
43# ok on unix, nt, VMS, ...
44my $dupe = '2>&1';
45# ok on unix, nt, The extra \" are for VMS
46my $lib = '"-I../lib" "-I../../lib"';
47# The >&1 would create a file named &1 on MPW (STDERR && STDOUT are
48# already merged).
49if ($^O eq 'MacOS') {
50 $dupe = '';
51 # -x overcomes MPW $Config{startperl} anomaly
52 $lib = '-x -I::lib: -I:::lib:';
53}
54# $name should differ from system header file names and must
55# not already be found in the t/ subdirectory for perl.
56my $name = 'h2xst';
57my $header = "$name.h";
58my $thisversion = sprintf "%vd", $^V;
59$thisversion =~ s/^v//;
60
61# If this test has failed previously a copy may be left.
62rmtree($name);
63
64my @tests = (
65"-f -n $name", $], <<"EOXSFILES",
66Defaulting to backwards compatibility with perl $thisversion
67If you intend this module to be compatible with earlier perl versions, please
68specify a minimum perl version with the -b option.
69
70Writing $name/ppport.h
71Writing $name/lib/$name.pm
72Writing $name/$name.xs
73Writing $name/fallback/const-c.inc
74Writing $name/fallback/const-xs.inc
75Writing $name/Makefile.PL
76Writing $name/README
77Writing $name/t/$name.t
78Writing $name/Changes
79Writing $name/MANIFEST
80EOXSFILES
81
82"-f -n $name -b $thisversion", $], <<"EOXSFILES",
83Writing $name/ppport.h
84Writing $name/lib/$name.pm
85Writing $name/$name.xs
86Writing $name/fallback/const-c.inc
87Writing $name/fallback/const-xs.inc
88Writing $name/Makefile.PL
89Writing $name/README
90Writing $name/t/$name.t
91Writing $name/Changes
92Writing $name/MANIFEST
93EOXSFILES
94
95"-f -n $name -b 5.6.1", "5.006001", <<"EOXSFILES",
96Writing $name/ppport.h
97Writing $name/lib/$name.pm
98Writing $name/$name.xs
99Writing $name/fallback/const-c.inc
100Writing $name/fallback/const-xs.inc
101Writing $name/Makefile.PL
102Writing $name/README
103Writing $name/t/$name.t
104Writing $name/Changes
105Writing $name/MANIFEST
106EOXSFILES
107
108"-f -n $name -b 5.5.3", "5.00503", <<"EOXSFILES",
109Writing $name/ppport.h
110Writing $name/lib/$name.pm
111Writing $name/$name.xs
112Writing $name/fallback/const-c.inc
113Writing $name/fallback/const-xs.inc
114Writing $name/Makefile.PL
115Writing $name/README
116Writing $name/t/$name.t
117Writing $name/Changes
118Writing $name/MANIFEST
119EOXSFILES
120
121"\"-X\" -f -n $name -b $thisversion", $], <<"EONOXSFILES",
122Writing $name/lib/$name.pm
123Writing $name/Makefile.PL
124Writing $name/README
125Writing $name/t/$name.t
126Writing $name/Changes
127Writing $name/MANIFEST
128EONOXSFILES
129
130"-f -n $name -b $thisversion $header", $], <<"EOXSFILES",
131Writing $name/ppport.h
132Writing $name/lib/$name.pm
133Writing $name/$name.xs
134Writing $name/fallback/const-c.inc
135Writing $name/fallback/const-xs.inc
136Writing $name/Makefile.PL
137Writing $name/README
138Writing $name/t/$name.t
139Writing $name/Changes
140Writing $name/MANIFEST
141EOXSFILES
142);
143
144my $total_tests = 3; # opening, closing and deleting the header file.
145for (my $i = $#tests; $i > 0; $i-=3) {
146 # 1 test for running it, 1 test for the expected result, and 1 for each file
147 # plus 1 to open and 1 to check for the use in lib/$name.pm and Makefile.PL
148 # And 1 more for our check for "bonus" files, 2 more for ExtUtil::Manifest.
149 # use the () to force list context and hence count the number of matches.
150 $total_tests += 9 + (() = $tests[$i] =~ /(Writing)/sg);
151}
152
153plan tests => $total_tests;
154
155ok (open (HEADER, ">$header"), "open '$header'");
156print HEADER <<HEADER or die $!;
157#define Camel 2
158#define Dromedary 1
159HEADER
160ok (close (HEADER), "close '$header'");
161
162while (my ($args, $version, $expectation) = splice @tests, 0, 3) {
163 # h2xs warns about what it is writing hence the (possibly unportable)
164 # 2>&1 dupe:
165 # does it run?
166 my $prog = "$^X $lib $extracted_program $args $dupe";
167 @result = `$prog`;
168 cmp_ok ($?, "==", 0, "running $prog ");
169 $result = join("",@result);
170
171 # accomodate MPW # comment character prependage
172 if ($^O eq 'MacOS') {
173 $result =~ s/#\s*//gs;
174 }
175
176 #print "# expectation is >$expectation<\n";
177 #print "# result is >$result<\n";
178 # Was the output the list of files that were expected?
179 is ($result, $expectation, "running $prog");
180
181 my (%got);
182 find (sub {$got{$File::Find::name}++ unless -d $_}, $name);
183
184 foreach ($expectation =~ /Writing\s+(\S+)/gm) {
185 if ($^O eq 'MacOS') {
186 $_ = ':' . join(':',split(/\//,$_));
187 $_ =~ s/$name:t:1.t/$name:t\/1.t/; # is this an h2xs bug?
188 }
189 if ($^O eq 'VMS') {
190 $_ .= '.' unless $_ =~ m/\./;
191 $_ = lc($_) unless exists $got{$_};
192 }
193 ok (-e $_, "check for $_") and delete $got{$_};
194 }
195 my @extra = keys %got;
196 unless (ok (!@extra, "Are any extra files present?")) {
197 print "# These files are unexpectedly present:\n";
198 print "# $_\n" foreach sort @extra;
199 }
200
201 chdir ($name) or die "chdir $name failed: $!";
202 # Aargh. Something wants to load a bit of regexp. And we have to chdir
203 # for ExtUtils::Manifest. Caught between a rock and a hard place, so this
204 # seems the least evil thing to do:
205 push @INC, "../../lib";
206 my ($missing, $extra) = ExtUtils::Manifest::fullcheck();
207 is_deeply ($missing, [], "No files in the MANIFEST should be missing");
208 is_deeply ($extra, [], "and all files present should be in the MANIFEST");
209 pop @INC;
210 chdir ($up) or die "chdir $up failed: $!";
211
212 foreach my $leaf (File::Spec->catfile('lib', "$name.pm"), 'Makefile.PL') {
213 my $file = File::Spec->catfile($name, $leaf);
214 if (ok (open (FILE, $file), "open $file")) {
215 my $match = qr/use $version;/;
216 my $found;
217 while (<FILE>) {
218 last if $found = /$match/;
219 }
220 ok ($found, "looking for /$match/ in $file");
221 close FILE or die "close $file: $!";
222 }
223 }
224 # clean up
225 rmtree($name);
226}
227
228cmp_ok (unlink ($header), "==", 1, "unlink '$header'") or die "\$! is $!";