This is a live mirror of the Perl 5 development currently hosted at https://github.com/perl/perl5
File::CheckTree hates @'s
[perl5.git] / lib / h2xs.t
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
12 BEGIN {
13     chdir 't' if -d 't';
14     @INC = '../lib';
15 }
16
17 # use strict; # we are not really testing this
18 use File::Path;  # for cleaning up with rmtree()
19 use Test::More;
20 use File::Spec;
21 use File::Find;
22 use ExtUtils::Manifest;
23 # Don't want its diagnostics getting in the way of ours.
24 $ExtUtils::Manifest::Quiet=1;
25 my $up = File::Spec->updir();
26
27 my $extracted_program = '../utils/h2xs'; # unix, nt, ...
28 if ($^O eq 'VMS') { $extracted_program = '[-.utils]h2xs.com'; }
29 if ($^O eq 'MacOS') { $extracted_program = '::utils:h2xs'; }
30 if (!(-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, ...
38 my $dupe = '2>&1';
39 # ok on unix, nt, The extra \" are for VMS
40 my $lib = '"-I../lib" "-I../../lib"';
41 # The >&1 would create a file named &1 on MPW (STDERR && STDOUT are
42 # already merged).
43 if ($^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.
50 my $name = 'h2xst';
51 my $header = "$name.h";
52 my $thisversion = sprintf "%vd", $^V;
53
54 my @tests = (
55 "-f -n $name", $], <<"EOXSFILES",
56 Defaulting to backwards compatibility with perl $thisversion
57 If you intend this module to be compatible with earlier perl versions, please
58 specify a minimum perl version with the -b option.
59
60 Writing $name/ppport.h
61 Writing $name/$name.pm
62 Writing $name/$name.xs
63 Writing $name/fallback/const-c.inc
64 Writing $name/fallback/const-xs.inc
65 Writing $name/Makefile.PL
66 Writing $name/README
67 Writing $name/t/1.t
68 Writing $name/Changes
69 Writing $name/MANIFEST
70 EOXSFILES
71
72 "-f -n $name -b $thisversion", $], <<"EOXSFILES",
73 Writing $name/ppport.h
74 Writing $name/$name.pm
75 Writing $name/$name.xs
76 Writing $name/fallback/const-c.inc
77 Writing $name/fallback/const-xs.inc
78 Writing $name/Makefile.PL
79 Writing $name/README
80 Writing $name/t/1.t
81 Writing $name/Changes
82 Writing $name/MANIFEST
83 EOXSFILES
84
85 "-f -n $name -b 5.6.1", "5.006001", <<"EOXSFILES",
86 Writing $name/ppport.h
87 Writing $name/$name.pm
88 Writing $name/$name.xs
89 Writing $name/fallback/const-c.inc
90 Writing $name/fallback/const-xs.inc
91 Writing $name/Makefile.PL
92 Writing $name/README
93 Writing $name/t/1.t
94 Writing $name/Changes
95 Writing $name/MANIFEST
96 EOXSFILES
97
98 "-f -n $name -b 5.5.3", "5.00503", <<"EOXSFILES",
99 Writing $name/ppport.h
100 Writing $name/$name.pm
101 Writing $name/$name.xs
102 Writing $name/fallback/const-c.inc
103 Writing $name/fallback/const-xs.inc
104 Writing $name/Makefile.PL
105 Writing $name/README
106 Writing $name/t/1.t
107 Writing $name/Changes
108 Writing $name/MANIFEST
109 EOXSFILES
110
111 "\"-X\" -f -n $name -b $thisversion", $], <<"EONOXSFILES",
112 Writing $name/$name.pm
113 Writing $name/Makefile.PL
114 Writing $name/README
115 Writing $name/t/1.t
116 Writing $name/Changes
117 Writing $name/MANIFEST
118 EONOXSFILES
119
120 "-f -n $name $header -b $thisversion", $], <<"EOXSFILES",
121 Writing $name/ppport.h
122 Writing $name/$name.pm
123 Writing $name/$name.xs
124 Writing $name/fallback/const-c.inc
125 Writing $name/fallback/const-xs.inc
126 Writing $name/Makefile.PL
127 Writing $name/README
128 Writing $name/t/1.t
129 Writing $name/Changes
130 Writing $name/MANIFEST
131 EOXSFILES
132 );
133
134 my $total_tests = 3; # opening, closing and deleting the header file.
135 for (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 $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
143 plan tests => $total_tests;
144
145 ok (open (HEADER, ">$header"), "open '$header'");
146 print HEADER <<HEADER or die $!;
147 #define Camel 2
148 #define Dromedary 1
149 HEADER
150 ok (close (HEADER), "close '$header'");
151
152 while (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 ("$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
218 cmp_ok (unlink ($header), "==", 1, "unlink '$header'") or die "\$! is $!";