Move Module::Pluggable into ext/ as the next version has actions in its
[p5sagit/p5-mst-13.2.git] / lib / Module / Build / t / xs.t
CommitLineData
bb4e9162 1#!/usr/bin/perl -w
2
3use strict;
4use lib $ENV{PERL_CORE} ? '../lib/Module/Build/t/lib' : 't/lib';
5use MBTest;
6use Module::Build;
7
8{
9 my ($have_c_compiler, $C_support_feature) = check_compiler();
10
11 if (! $C_support_feature) {
12 plan skip_all => 'C_support not enabled';
13 } elsif ( !$have_c_compiler ) {
14 plan skip_all => 'C_support enabled, but no compiler found';
9548cd0f 15 } elsif ( $^O eq 'VMS' ) {
16 plan skip_all => 'Child test output confuses harness';
bb4e9162 17 } else {
18 plan tests => 22;
19 }
20}
21
22#########################
23
24
25use Cwd ();
26my $cwd = Cwd::cwd;
7a827510 27my $tmp = MBTest->tmpdir;
bb4e9162 28
29use DistGen;
30my $dist = DistGen->new( dir => $tmp, xs => 1 );
31$dist->regen;
32
33chdir( $dist->dirname ) or die "Can't chdir to '@{[$dist->dirname]}': $!";
34my $mb = Module::Build->new_from_context;
35
36
37eval {$mb->dispatch('clean')};
38is $@, '';
39
40eval {$mb->dispatch('build')};
41is $@, '';
42
43{
44 # Make sure it actually works: that we can call methods in the XS module
45
46 # Unfortunately, We must do this is a subprocess because some OS will not
47 # release the handle on a dynamic lib until the attaching process terminates
48
49 ok $mb->run_perl_command(['-Mblib', '-M'.$dist->name, '-e1']);
50
51 like stdout_of( sub {$mb->run_perl_command([
52 '-Mblib', '-M'.$dist->name,
53 '-we', "print @{[$dist->name]}::okay()"])}), qr/ok$/;
54
55 like stdout_of( sub {$mb->run_perl_command([
56 '-Mblib', '-M'.$dist->name,
57 '-we', "print @{[$dist->name]}::version()"])}), qr/0.01$/;
58
59 like stdout_of( sub {$mb->run_perl_command([
60 '-Mblib', '-M'.$dist->name,
61 '-we', "print @{[$dist->name]}::xs_version()"])}), qr/0.01$/;
62
63}
64
65{
66 # Try again in a subprocess
67 eval {$mb->dispatch('clean')};
68 is $@, '';
69
77e96e88 70
bb4e9162 71 $mb->create_build_script;
77e96e88 72 my $script = $mb->build_script;
73 ok -e $script;
bb4e9162 74
77e96e88 75 eval {$mb->run_perl_script($script)};
bb4e9162 76 is $@, '';
77}
78
79# We can't be verbose in the sub-test, because Test::Harness will
80# think that the output is for the top-level test.
81eval {$mb->dispatch('test')};
82is $@, '';
83
84eval {$mb->dispatch('clean')};
85is $@, '';
86
87
88SKIP: {
89 skip( "skipping a Unixish-only tests", 1 )
c1d8f74e 90 unless $mb->is_unixish;
bb4e9162 91
77e96e88 92 $mb->{config}->push(ld => "FOO=BAR ".$mb->config('ld'));
bb4e9162 93 eval {$mb->dispatch('build')};
94 is $@, '';
77e96e88 95 $mb->{config}->pop('ld');
bb4e9162 96}
97
98eval {$mb->dispatch('realclean')};
99is $@, '';
100
101# Make sure blib/ is gone after 'realclean'
102ok ! -e 'blib';
103
104
105# cleanup
106chdir( $cwd ) or die "Can''t chdir to '$cwd': $!";
69f75412 107$dist->remove;
bb4e9162 108
109
110########################################
111
112# Try a XS distro with a deep namespace
113
3143ec60 114$dist = DistGen->new( name => 'Simple::With::Deep::Name',
bb4e9162 115 dir => $tmp, xs => 1 );
116$dist->regen;
117chdir( $dist->dirname ) or die "Can't chdir to '@{[$dist->dirname]}': $!";
118
119$mb = Module::Build->new_from_context;
120is $@, '';
121
122$mb->dispatch('build');
123is $@, '';
124
125$mb->dispatch('test');
126is $@, '';
127
128$mb->dispatch('realclean');
129is $@, '';
130
131# cleanup
132chdir( $cwd ) or die "Can''t chdir to '$cwd': $!";
133$dist->remove;
134
135
136########################################
137
138# Try a XS distro using a flat directory structure
139# and a 'dist_name' instead of a 'module_name'
140
141$dist = DistGen->new( name => 'Dist-Name', dir => $tmp, xs => 1 );
142
143$dist->remove_file('lib/Dist-Name.pm');
144$dist->remove_file('lib/Dist-Name.xs');
145
7a827510 146$dist->change_build_pl
147 ({
bb4e9162 148 dist_name => 'Dist-Name',
149 dist_version_from => 'Simple.pm',
150 pm_files => { 'Simple.pm' => 'lib/Simple.pm' },
151 xs_files => { 'Simple.xs' => 'lib/Simple.xs' },
7a827510 152 });
bb4e9162 153
bb4e9162 154$dist->add_file('Simple.xs', <<"---");
155#include "EXTERN.h"
156#include "perl.h"
157#include "XSUB.h"
158
159MODULE = Simple PACKAGE = Simple
160
161SV *
162okay()
163 CODE:
164 RETVAL = newSVpv( "ok", 0 );
165 OUTPUT:
166 RETVAL
167---
168
169$dist->add_file( 'Simple.pm', <<"---" );
170package Simple;
171
172\$VERSION = '0.01';
173
174require Exporter;
175require DynaLoader;
176
177\@ISA = qw( Exporter DynaLoader );
178\@EXPORT_OK = qw( okay );
179
180bootstrap Simple \$VERSION;
181
1821;
183
184__END__
185
186=head1 NAME
187
188Simple - Perl extension for blah blah blah
189
190=head1 DESCRIPTION
191
192Stub documentation for Simple.
193
194=head1 AUTHOR
195
196A. U. Thor, a.u.thor\@a.galaxy.far.far.away
197
198=cut
199---
200$dist->change_file('t/basic.t', <<"---");
201use Test::More tests => 2;
202use strict;
203
204use Simple;
205ok( 1 );
206
207ok( Simple::okay() eq 'ok' );
208---
209
210$dist->regen;
211chdir( $dist->dirname ) or die "Can't chdir to '@{[$dist->dirname]}': $!";
212
213
214$mb = Module::Build->new_from_context;
215is $@, '';
216
217$mb->dispatch('build');
218is $@, '';
219
220$mb->dispatch('test');
221is $@, '';
222
223$mb->dispatch('realclean');
224is $@, '';
225
226# cleanup
227chdir( $cwd ) or die "Can''t chdir to '$cwd': $!";
228$dist->remove;
229
230use File::Path;
231rmtree( $tmp );