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