This is a live mirror of the Perl 5 development currently hosted at https://github.com/perl/perl5
Retract #12436 (Abhijit already did this at #12426)
[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 BEGIN {
12     chdir 't' if -d 't';
13     @INC = '../lib';
14 }
15
16 # use strict; # we are not really testing this
17 use File::Path;  # for cleaning up with rmtree()
18 use Test;
19 use File::Spec;
20
21 my $extracted_program = '../utils/h2xs'; # unix, nt, ...
22 if ($^O eq 'VMS') { $extracted_program = '[-.utils]h2xs.com'; }
23 if ($^O eq 'MacOS') { $extracted_program = '::utils:h2xs'; }
24 if (!(-e $extracted_program)) {
25     print "1..0 # Skip: $extracted_program was not built\n";
26     exit 0;
27 }
28 # You might also wish to bail out if your perl platform does not
29 # do `$^X -e 'warn "Writing h2xst"' 2>&1`; duplicity.
30
31 my $dupe = '2>&1'; # ok on unix, nt, VMS, ...
32 my $lib = '"-I../lib"'; # ok on unix, nt, The extra \" are for VMS
33 # The >&1 would create a file named &1 on MPW (STDERR && STDOUT are
34 # already merged).
35 if ($^O eq 'MacOS') {
36     $dupe = '';
37     $lib = '-x -I::lib:'; # -x overcomes MPW $Config{startperl} anomaly
38 }
39 # $name should differ from system header file names and must
40 # not already be found in the t/ subdirectory for perl.
41 my $name = 'h2xst';
42 my $header = "$name.h";
43 my $thisversion = sprintf "%vd", $^V;
44
45 my @tests = (
46 "-f -n $name", $], <<"EOXSFILES",
47 Defaulting to backwards compatibility with perl $thisversion
48 If you intend this module to be compatible with earlier perl versions, please
49 specify a minimum perl version with the -b option.
50
51 Writing $name/$name.pm
52 Writing $name/$name.xs
53 Writing $name/fallback.c
54 Writing $name/fallback.xs
55 Writing $name/Makefile.PL
56 Writing $name/README
57 Writing $name/t/1.t
58 Writing $name/Changes
59 Writing $name/MANIFEST
60 EOXSFILES
61
62 "-f -n $name -b $thisversion", $], <<"EOXSFILES",
63 Writing $name/$name.pm
64 Writing $name/$name.xs
65 Writing $name/fallback.c
66 Writing $name/fallback.xs
67 Writing $name/Makefile.PL
68 Writing $name/README
69 Writing $name/t/1.t
70 Writing $name/Changes
71 Writing $name/MANIFEST
72 EOXSFILES
73
74 "-f -n $name -b 5.6.1", "5.006001", <<"EOXSFILES",
75 Writing $name/$name.pm
76 Writing $name/$name.xs
77 Writing $name/fallback.c
78 Writing $name/fallback.xs
79 Writing $name/Makefile.PL
80 Writing $name/README
81 Writing $name/t/1.t
82 Writing $name/Changes
83 Writing $name/MANIFEST
84 EOXSFILES
85
86 "-f -n $name -b 5.5.3", "5.00503", <<"EOXSFILES",
87 Writing $name/$name.pm
88 Writing $name/$name.xs
89 Writing $name/fallback.c
90 Writing $name/fallback.xs
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 "\"-X\" -f -n $name -b $thisversion", $], <<"EONOXSFILES",
99 Writing $name/$name.pm
100 Writing $name/Makefile.PL
101 Writing $name/README
102 Writing $name/t/1.t
103 Writing $name/Changes
104 Writing $name/MANIFEST
105 EONOXSFILES
106
107 "-f -n $name $header -b $thisversion", $], <<"EOXSFILES",
108 Writing $name/$name.pm
109 Writing $name/$name.xs
110 Writing $name/fallback.c
111 Writing $name/fallback.xs
112 Writing $name/Makefile.PL
113 Writing $name/README
114 Writing $name/t/1.t
115 Writing $name/Changes
116 Writing $name/MANIFEST
117 EOXSFILES
118 );
119
120 my $total_tests = 3; # opening, closing and deleting the header file.
121 for (my $i = $#tests; $i > 0; $i-=3) {
122   # 1 test for running it, 1 test for the expected result, and 1 for each file
123   # plus 1 to open and 1 to check for the use in $name.pm and Makefile.PL
124   # use the () to force list context and hence count the number of matches.
125   $total_tests += 6 + (() = $tests[$i] =~ /(Writing)/sg);
126 }
127
128 plan tests => $total_tests;
129
130 ok (open (HEADER, ">$header"));
131 print HEADER <<HEADER or die $!;
132 #define Camel 2
133 #define Dromedary 1
134 HEADER
135 ok (close (HEADER));
136
137 while (my ($args, $version, $expectation) = splice @tests, 0, 3) {
138   # h2xs warns about what it is writing hence the (possibly unportable)
139   # 2>&1 dupe:
140   # does it run?
141   my $prog = "$^X $lib $extracted_program $args $dupe";
142   @result = `$prog`;
143   ok ($?, 0, "running $prog");
144   $result = join("",@result);
145
146   # accomodate MPW # comment character prependage
147   if ($^O eq 'MacOS') {
148     $result =~ s/#\s*//gs;
149   }
150
151   #print "# expectation is >$expectation<\n";
152   #print "# result is >$result<\n";
153   # Was the output the list of files that were expected?
154   ok ($result, $expectation, "running $prog");
155
156   foreach ($expectation =~ /Writing\s+(\S+)/gm) {
157     if ($^O eq 'MacOS') {
158       $_ = ':' . join(':',split(/\//,$_));
159       $_ =~ s/$name:t:1.t/$name:t\/1.t/; # is this an h2xs bug?
160     }
161     ok (-e $_, 1, "$_ missing");
162   }
163
164   foreach my $leaf ("$name.pm", 'Makefile.PL') {
165     my $file = File::Spec->catfile($name, $leaf);
166     if (ok (open (FILE, $file), 1, "open $file")) {
167       my $match = qr/use $version;/;
168       my $found;
169       while (<FILE>) {
170         last if $found = /$match/;
171       }
172       ok ($found, 1, "looking for /$match/ in $file");
173       close FILE or die "close $file: $!";
174     }
175   }
176   # clean up
177   rmtree($name);
178 }
179
180 ok (unlink ($header), 1, $!);