This is a live mirror of the Perl 5 development currently hosted at https://github.com/perl/perl5
Workaround on Cwd bootstrap problem.
[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 BEGIN {
10     chdir 't' if -d 't';
11     @INC = '../lib';
12 }
13
14 # use strict; # we are not really testing this
15 use File::Path;  # for cleaning up with rmtree()
16 use Test;
17
18
19 my $extracted_program = '../utils/h2xs'; # unix, nt, ...
20 if ($^O eq 'VMS') { $extracted_program = '[-.utils]h2xs.com'; }
21 if ($^O eq 'MacOS') { $extracted_program = '::utils:h2xs'; }
22 if (!(-e $extracted_program)) {
23     print "1..0 # Skip: $extracted_program was not built\n";
24     exit 0;
25 }
26 # You might also wish to bail out if your perl platform does not
27 # do `$^X -e 'warn "Writing h2xst"' 2>&1`; duplicity.
28
29 my $dupe = '2>&1'; # ok on unix, nt, VMS, ...
30 my $lib = '"-I../lib"'; # ok on unix, nt, The extra \" are for VMS
31 # The >&1 would create a file named &1 on MPW (STDERR && STDOUT are
32 # already merged).
33 if ($^O eq 'MacOS') {
34     $dupe = '';
35     $lib = '-x -I::lib:'; # -x overcomes MPW $Config{startperl} anomaly
36 }
37 # $name should differ from system header file names and must
38 # not already be found in the t/ subdirectory for perl.
39 my $name = 'h2xst';
40 my $header = "$name.h";
41
42 my @tests = (
43 "-f -n $name", <<"EOXSFILES",
44 Writing $name/$name.pm
45 Writing $name/$name.xs
46 Writing $name/Makefile.PL
47 Writing $name/README
48 Writing $name/t/1.t
49 Writing $name/Changes
50 Writing $name/MANIFEST
51 EOXSFILES
52
53 "\"-X\" -f -n $name", <<"EONOXSFILES",
54 Writing $name/$name.pm
55 Writing $name/Makefile.PL
56 Writing $name/README
57 Writing $name/t/1.t
58 Writing $name/Changes
59 Writing $name/MANIFEST
60 EONOXSFILES
61
62 "-f -n $name $header", <<"EOXSFILES",
63 Writing $name/$name.pm
64 Writing $name/$name.xs
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
73 my $total_tests = 3; # opening, closing and deleting the header file.
74 for (my $i = $#tests; $i > 0; $i-=2) {
75   # 1 test for running it, 1 test for the expected result, and 1 for each file
76   # use the () to force list context and hence count the number of matches.
77   $total_tests += 2 + (() = $tests[$i] =~ /(Writing)/sg);
78 }
79
80 plan tests => $total_tests;
81
82 ok (open (HEADER, ">$header"));
83 print HEADER <<HEADER or die $!;
84 #define Camel 2
85 #define Dromedary 1
86 HEADER
87 ok (close (HEADER));
88
89 while (my ($args, $expectation) = splice @tests, 0, 2) {
90   # h2xs warns about what it is writing hence the (possibly unportable)
91   # 2>&1 dupe:
92   # does it run?
93   my $prog = "$^X $lib $extracted_program $args $dupe";
94   @result = `$prog`;
95   ok ($?, 0, "running $prog");
96   $result = join("",@result);
97
98   # accomodate MPW # comment character prependage
99   if ($^O eq 'MacOS') {
100     $result =~ s/#\s*//gs;
101   }
102
103   #print "# expectation is >$expectation<\n";
104   #print "# result is >$result<\n";
105   # Was the output the list of files that were expected?
106   ok ($result, $expectation, "running $prog");
107
108   $expectation =~ s/Writing //; # remove leader
109   foreach (split(/Writing /,$expectation)) {
110     chomp;  # remove \n
111     if ($^O eq 'MacOS') {
112       $_ = ':' . join(':',split(/\//,$_));
113       $_ =~ s/$name:t:1.t/$name:t\/1.t/; # is this an h2xs bug?
114     }
115     ok (-e $_, 1, "$_ missing");
116   }
117
118   # clean up
119   rmtree($name);
120 }
121
122 ok (unlink ($header), 1, $!);