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