This is a live mirror of the Perl 5 development currently hosted at https://github.com/perl/perl5
Add CPANPLUS::Dist::Build to the core
[perl5.git] / lib / CPANPLUS / Dist / Build / t / 02_CPANPLUS-Dist-Build.t
CommitLineData
9b4bd854
JB
1### make sure we can find our conf.pl file
2BEGIN {
3 use FindBin;
4 require "$FindBin::Bin/inc/conf.pl";
5}
6
7use strict;
8use CPANPLUS::Configure;
9use CPANPLUS::Backend;
10use CPANPLUS::Internals::Constants;
11use CPANPLUS::Module::Fake;
12use CPANPLUS::Module::Author::Fake;
13
14use Config;
15use Test::More 'no_plan';
16use File::Basename ();
17use Data::Dumper;
18use Config;
19use IPC::Cmd 'can_run';
20
21$SIG{__WARN__} = sub {warn @_ unless @_ && $_[0] =~ /redefined|isn't numeric/};
22
23# Load these two modules in advance, even though they would be
24# auto-loaded, because we want to override some of their subs.
25use ExtUtils::Packlist;
26use ExtUtils::Installed;
27
28my $Class = 'CPANPLUS::Dist::Build';
29my $Utils = 'CPANPLUS::Internals::Utils';
30my $Have_CC = can_run($Config{'cc'} )? 1 : 0;
31
32
33my $Lib = File::Spec->rel2abs(File::Spec->catdir( qw[dummy-perl] ));
34my $Src = File::Spec->rel2abs(File::Spec->catdir( qw[src] ));
35
36my $Verbose = @ARGV ? 1 : 0;
37my $CB = CPANPLUS::Backend->new;
38my $Conf = $CB->configure_object;
39
40
41### create a fake object, so we don't use the actual module tree
42my $Mod = CPANPLUS::Module::Fake->new(
43 module => 'Foo::Bar',
44 path => 'src',
45 author => CPANPLUS::Module::Author::Fake->new,
46 package => 'Foo-Bar-0.01.tar.gz',
47 );
48
49$Conf->set_conf( base => 'dummy-cpanplus' );
50$Conf->set_conf( dist_type => '' );
51$Conf->set_conf( verbose => $Verbose );
52$Conf->set_conf( signature => 0 );
53### running tests will mess with the test output so skip 'm
54$Conf->set_conf( skiptest => 1 );
55
56 # path, cc needed?
57my %Map = ( noxs => 0,
58 xs => 1
59 );
60
61
62### Disable certain possible settings, so we dont accidentally
63### touch anything outside our sandbox
64{
65 ### set buildflags to install in our dummy perl dir
66 $Conf->set_conf( buildflags => "install_base=$Lib" );
67
68 ### don't start sending test reports now... ###
69 $CB->_callbacks->send_test_report( sub { 0 } );
70 $Conf->set_conf( cpantest => 0 );
71
72 ### we dont need sudo -- we're installing in our own sandbox now
73 $Conf->set_program( sudo => undef );
74}
75
76use_ok( $Class );
77
78ok( $Class->format_available, "Format is available" );
79
80
81while( my($path,$need_cc) = each %Map ) {
82
83 my $mod = $Mod->clone;
84 ok( $mod, "Module object created for '$path'" );
85
86 ### set the fetch location -- it's local
87 { my $where = File::Spec->rel2abs(
88 File::Spec->catdir( $Src, $path, $mod->package )
89 );
90
91 $mod->status->fetch( $where );
92
93 ok( -e $where, " Tarball '$where' exists" );
94 }
95
96 ok( $mod->prepare, " Preparing module" );
97
98 ok( $mod->status->dist_cpan,
99 " Dist registered as status" );
100
101 isa_ok( $mod->status->dist_cpan, $Class );
102
103 ok( $mod->status->dist_cpan->status->prepared,
104 " Prepared status registered" );
105 is( $mod->status->dist_cpan->status->distdir, $mod->status->extract,
106 " Distdir status registered properly" );
107
108
109 is( $mod->status->installer_type, INSTALLER_BUILD,
110 " Proper installer type found" );
111
112
113 ### we might not have a C compiler
114 SKIP: {
115 skip("The CC compiler listed in Config.pm is not available " .
116 "-- skipping compile tests", 5) if $need_cc && !$Have_CC;
117 skip("Module::Build is not compiled with C support ".
118 "-- skipping compile tests", 5)
119 unless Module::Build->_mb_feature('C_support');
120
121 ok( $mod->create( ), "Creating module" );
122 ok( $mod->status->dist_cpan->status->created,
123 " Created status registered" );
124
125 ### install tests
126 SKIP: {
127 skip("Install tests require Module::Build 0.2606 or higher", 2)
128 unless $Module::Build::VERSION >= '0.2606';
129
130 ### flush the lib cache
131 ### otherwise, cpanplus thinks the module's already installed
132 ### since the blib is already in @INC
133 $CB->_flush( list => [qw|lib|] );
134
135 ### force the install, make sure the Dist::Build->install()
136 ### sub gets called
137 ok( $mod->install( force => 1 ),
138 "Installing module" );
139 ok( $mod->status->installed,
140 " Status says module installed" );
141 }
142
143 SKIP: {
144 my $minversion = 0.2609;
145 skip(qq[Uninstalling requires at least Module::Build $minversion], 1)
146 unless eval { Module::Build->VERSION($minversion); 1 };
147
148 # The installation directory actually needs to be in @INC
149 # in order to test uninstallation
150 'lib'->import( File::Spec->catdir($Lib, 'lib', 'perl5') );
151
152 # EU::Installed and CP+::M are only capable of searching
153 # for modules in the core directories. We need to fake
154 # them out with our own subs here.
155 my $packlist = find_module($mod->name . '::.packlist');
156 ok $packlist, "Found packlist";
157
158 my $p = ExtUtils::Packlist->new($packlist);
159 ok keys(%$p) > 0, "Packlist contains entries";
160
161 local *CPANPLUS::Module::installed_version = sub {1};
162 local *CPANPLUS::Module::packlist = sub { [$p] };
163 local *ExtUtils::Installed::files = sub { keys %$p };
164
165 ok( $mod->uninstall,"Uninstalling module" );
166 }
167 }
168
169 ### throw away all the extracted stuff
170 $Utils->_rmdir( dir => $Conf->get_conf('base') );
171}
172
173### test ENV setting while running Build.PL code
174{ ### use print() not die() -- we're redirecting STDERR in tests!
175 my $env = 'ENV_CPANPLUS_IS_EXECUTING';
176 my $clone = $Mod->clone;
177
178 ok( $clone, 'Testing ENV settings $dist->prepare' );
179
180 $clone->status->fetch( File::Spec->catfile($Src, 'noxs', $clone->package) );
181 ok( $clone->extract, ' Files extracted' );
182
183 ### write our own Build.PL file
184 my $build_pl = BUILD_PL->( $clone->status->extract );
185 { my $fh = OPEN_FILE->( $build_pl, '>' );
186 print $fh "die qq[ENV=\$ENV{$env}\n];";
187 close $fh;
188 }
189 ok( -e $build_pl, " File exists" );
190
191 ### clear errors
192 CPANPLUS::Error->flush;
193
194 ### since we're die'ing in the Build.PL, do a local *STDERR,
195 ### so we dont spam the result through the test -- this is expected
196 ### behaviour after all.
197 my $rv = do { local *STDERR; $clone->prepare( force => 1 ) };
198 ok( !$rv, ' $mod->prepare failed' );
199
200 my $re = quotemeta( $build_pl );
201 like( CPANPLUS::Error->stack_as_string, qr/ENV=$re/,
202 " \$ENV $env set correctly during execution");
203
204 ### and the ENV var should no longer be set now
205 ok( !$ENV{$env}, " ENV var now unset" );
206}
207
208
209sub find_module {
210 my $module = shift;
211
212 # Don't add the .pm yet, in case it's a packlist or something like ExtUtils::xsubpp.
213 my $file = File::Spec->catfile( split m/::/, $module );
214 my $candidate;
215 foreach (@INC) {
216 if (-e ($candidate = File::Spec->catdir($_, $file))
217 or
218 -e ($candidate = File::Spec->catdir($_, "$file.pm"))
219 or
220 -e ($candidate = File::Spec->catdir($_, 'auto', $file))
221 or
222 -e ($candidate = File::Spec->catdir($_, 'auto', "$file.pm"))) {
223 return $candidate;
224 }
225 }
226 return;
227}
228
229
230# Local variables:
231# c-indentation-style: bsd
232# c-basic-offset: 4
233# indent-tabs-mode: nil
234# End:
235# vim: expandtab shiftwidth=4: