Re: [perl #46011] [RESOLVED] overload "0+" doesn't handle integer results
[p5sagit/p5-mst-13.2.git] / lib / CPANPLUS / t / 20_CPANPLUS-Dist-MM.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
9 use CPANPLUS::Configure;
10 use CPANPLUS::Backend;
11 use CPANPLUS::Dist;
12 use CPANPLUS::Dist::MM;
13 use CPANPLUS::Internals::Constants;
14
15 use Test::More 'no_plan';
16 use Cwd;
17 use Config;
18 use Data::Dumper;
19 use File::Basename ();
20 use File::Spec ();
21
22 my $conf    = gimme_conf();
23 my $cb      = CPANPLUS::Backend->new( $conf );
24 my $noperms = ($< and not $conf->get_program('sudo')) &&
25               ($conf->get_conf('makemakerflags') or
26                 not -w $Config{installsitelib} );
27 my $File    = 'Bar.pm';
28 my $Verbose = @ARGV ? 1 : 0;
29
30 #$IPC::Cmd::DEBUG = $Verbose;
31
32 ### Make sure we get the _EUMM_NOXS_ version
33 my $ModName = TEST_CONF_MODULE;
34
35 ### This is the module name that gets /installed/
36 my $InstName = TEST_CONF_INST_MODULE;
37
38 ### don't start sending test reports now... ###
39 $cb->_callbacks->send_test_report( sub { 0 } );
40 $conf->set_conf( cpantest => 0 );
41
42 ### Redirect errors to file ###
43 *STDERR                          = output_handle() unless $Verbose;
44
45 ### dont uncomment this, it screws up where STDOUT goes and makes
46 ### test::harness create test counter mismatches
47 #*STDOUT                          = output_handle() unless @ARGV;
48 ### for the same test-output counter mismatch, we disable verbose
49 ### mode
50 $conf->set_conf( verbose => $Verbose );
51 $conf->set_conf( allow_build_interactivity => 0 );
52
53 ### start with fresh sources ###
54 ok( $cb->reload_indices( update_source => 0 ),
55                                 "Rebuilding trees" );
56
57 ### we might need this Some Day when we're going to install into
58 ### our own sandbox dir.. but for now, no dice due to EU::I bug
59 # $conf->set_program( sudo => '' );
60 # $conf->set_conf( makemakerflags => TEST_INSTALL_EU_MM_FLAGS );
61
62 ### set alternate install dir ###
63 ### XXX rather pointless, since we can't uninstall them, due to a bug
64 ### in EU::Installed (6871). And therefor we can't test uninstall() or any of
65 ### the EU::Installed functions. So, let's just install into sitelib... =/
66 #my $prefix  = File::Spec->rel2abs( File::Spec->catdir(cwd(),'dummy-perl') );
67 #my $rv = $cb->configure_object->set_conf( makemakerflags => "PREFIX=$prefix" );
68 #ok( $rv,                        "Alternate install path set" );
69
70 my $Mod     = $cb->module_tree( $ModName );
71 my $InstMod = $cb->module_tree( $InstName );
72 ok( $Mod,                       "Loaded object for: " . $Mod->name );
73 ok( $Mod,                       "Loaded object for: " . $InstMod->name );
74
75 ### format_available tests ###
76 {   ok( CPANPLUS::Dist::MM->format_available,
77                                 "Format is available" );
78
79     ### whitebox test!
80     {   local $^W;
81         local *CPANPLUS::Dist::MM::can_load = sub { 0 };
82         ok(!CPANPLUS::Dist::MM->format_available,
83                                 "   Making format unavailable" );
84     }
85
86     ### test if the error got logged ok ###
87     like( CPANPLUS::Error->stack_as_string,
88           qr/You do not have .+?'CPANPLUS::Dist::MM' not available/s,
89                                 "   Format failure logged" );
90
91     ### flush the stack ###
92     CPANPLUS::Error->flush;
93 }
94
95 ok( $Mod->fetch,                "Fetching module to ".$Mod->status->fetch );
96 ok( $Mod->extract,              "Extracting module to ".$Mod->status->extract );
97
98 ok( $Mod->test,                 "Testing module" );
99
100 ok( $Mod->status->dist_cpan->status->test,
101                                 "   Test success registered as status" );
102 ok( $Mod->status->dist_cpan->status->prepared,
103                                 "   Prepared status registered" );
104 ok( $Mod->status->dist_cpan->status->created,
105                                 "   Created status registered" );
106 is( $Mod->status->dist_cpan->status->distdir, $Mod->status->extract,
107                                 "   Distdir status registered properly" );
108
109 ### test the convenience methods
110 ok( $Mod->prepare,              "Preparing module" );
111 ok( $Mod->create,               "Creating module" );
112
113 ok( $Mod->dist,                 "Building distribution" );
114 ok( $Mod->status->dist_cpan,    "   Dist registered as status" );
115 isa_ok( $Mod->status->dist_cpan,    "CPANPLUS::Dist::MM" );
116
117 ### flush the lib cache
118 ### otherwise, cpanplus thinks the module's already installed
119 ### since the blib is already in @INC
120 $cb->_flush( list => [qw|lib|] );
121
122 SKIP: {
123
124     skip(q[No install tests under core perl], 10) if $ENV{PERL_CORE};
125
126     skip(q[Probably no permissions to install, skipping], 10)
127         if $noperms;
128
129     ### XXX new EU::I should be forthcoming pending this patch from Steffen
130     ### Mueller on p5p: http://www.xray.mpe.mpg.de/mailing-lists/ \ 
131     ###     perl5-porters/2007-01/msg00895.html
132     ### This should become EU::I 1.42.. if so, we should upgrade this bit of
133     ### code and remove the diag, since we can then install in our dummy dir..
134     diag("\nSorry, installing into your real perl dir, rather than our test");
135     diag("area since ExtUtils::Installed does not probe for .packlists in " );
136     diag('other dirs than those in %Config. See bug #6871 on rt.cpan.org ' );
137     diag('for details');
138
139     diag(q[Note: 'sudo' might ask for your password to do the install test])
140         if $conf->get_program('sudo');
141
142     ok( $Mod->install( force =>1 ),
143                                 "Installing module" );
144     ok( $Mod->status->installed,"   Module installed according to status" );
145
146
147     SKIP: {   ### EU::Installed tests ###
148
149         skip("makemakerflags set -- probably EU::Installed tests will fail", 8)
150            if $conf->get_conf('makemakerflags');
151     
152         skip( "Old perl on cygwin detected " .
153               "-- tests will fail due to known bugs", 8
154         ) if ON_OLD_CYGWIN;
155
156         ### might need it Later when EU::I is fixed..
157         #local @INC = ( TEST_INSTALL_DIR_LIB, @INC );
158
159         {   ### validate
160             my @missing = $InstMod->validate;
161
162             is_deeply( \@missing, [],
163                                     "No missing files" );
164         }
165
166         {   ### files
167             my @files = $InstMod->files;
168
169             ### number of files may vary from OS to OS
170             ok( scalar(@files),     "All files accounted for" );
171             ok( grep( /$File/, @files),
172                                     "   Found the module" );
173
174             ### XXX does this work on all OSs?
175             #ok( grep( /man/, @files ),
176             #                        "   Found the manpage" );
177         }
178
179         {   ### packlist
180             my ($obj) = $InstMod->packlist;
181             isa_ok( $obj,           "ExtUtils::Packlist" );
182         }
183
184         {   ### directory_tree
185             my @dirs = $InstMod->directory_tree;
186             ok( scalar(@dirs),      "Directory tree obtained" );
187
188             my $found;
189             for my $dir (@dirs) {
190                 ok( -d $dir,        "   Directory exists" );
191
192                 my $file = File::Spec->catfile( $dir, $File );
193                 $found = $file if -e $file;
194             }
195
196             ok( -e $found,          "   Module found" );
197         }
198
199         SKIP: {
200             skip("Probably no permissions to uninstall", 1)
201                 if $noperms;
202
203             ok( $InstMod->uninstall,"Uninstalling module" );
204         }
205     }
206 }
207
208 ### test exceptions in Dist::MM->create ###
209 {   ok( $Mod->status->mk_flush, "Old status info flushed" );
210     my $dist = CPANPLUS::Dist->new( module => $Mod,
211                                     format => INSTALLER_MM );
212
213     ok( $dist,                  "New dist object made" );
214     ok(!$dist->prepare,         "   Dist->prepare failed" );
215     like( CPANPLUS::Error->stack_as_string, qr/No dir found to operate on/,
216                                 "       Failure logged" );
217
218     ### manually set the extract dir,
219     $Mod->status->extract($0);
220
221     ok(!$dist->create,          "   Dist->create failed" );
222     like( CPANPLUS::Error->stack_as_string, qr/not successfully prepared/s,
223                                 "       Failure logged" );
224
225     ### pretend we've been prepared ###
226     $dist->status->prepared(1);
227
228     ok(!$dist->create,          "   Dist->create failed" );
229     like( CPANPLUS::Error->stack_as_string, qr/Could not chdir/s,
230                                 "       Failure logged" );
231 }
232
233 ### writemakefile.pl tests ###
234 {   ### remove old status info
235     ok( $Mod->status->mk_flush, "Old status info flushed" );
236     ok( $Mod->fetch,            "Module fetched again" );
237     ok( $Mod->extract,          "Module extracted again" );
238
239     ### cheat and add fake prereqs ###
240     my $prereq = TEST_CONF_PREREQ;
241
242     $Mod->status->prereqs( { $prereq => 0 } );
243
244     my $makefile_pl = MAKEFILE_PL->( $Mod->status->extract );
245     my $makefile    = MAKEFILE->(    $Mod->status->extract );
246
247     my $dist        = $Mod->dist;
248     ok( $dist,                  "Dist object built" );
249
250     ### check for a makefile.pl and 'write' one
251     ok( -s $makefile_pl,        "   Makefile.PL present" );
252     ok( $dist->write_makefile_pl( force => 0 ),
253                                 "   Makefile.PL written" );
254     like( CPANPLUS::Error->stack_as_string, qr/Already created/,
255                                 "   Prior existance noted" );
256
257     ### ok, unlink the makefile.pl, now really write one
258     unlink $makefile;
259
260     ok( unlink($makefile_pl),   "Deleting Makefile.PL");
261     ok( !-s $makefile_pl,       "   Makefile.PL deleted" );
262     ok( !-s $makefile,          "   Makefile deleted" );
263     ok($dist->write_makefile_pl,"   Makefile.PL written" );
264
265     ### see if we wrote anything sensible
266     my $fh = OPEN_FILE->( $makefile_pl );
267     ok( $fh,                    "Makefile.PL open for read" );
268
269     my $str = do { local $/; <$fh> };
270     like( $str, qr/### Auto-generated .+ by CPANPLUS ###/,
271                                 "   Autogeneration noted" );
272     like( $str, '/'. $Mod->module .'/',
273                                 "   Contains module name" );
274     like( $str, '/'. quotemeta($Mod->version) . '/',
275                                 "   Contains version" );
276     like( $str, '/'. $Mod->author->author .'/',
277                                 "   Contains author" );
278     like( $str, '/PREREQ_PM/',  "   Contains prereqs" );
279     like( $str, qr/$prereq.+0/, "   Contains prereqs" );
280
281     close $fh;
282
283     ### seems ok, now delete it again and go via install()
284     ### to see if it picks up on the missing makefile.pl and
285     ### does the right thing
286     ok( unlink($makefile_pl),   "Deleting Makefile.PL");
287     ok( !-s $makefile_pl,       "   Makefile.PL deleted" );
288     ok( $dist->status->mk_flush,"Dist status flushed" );
289     ok( $dist->prepare,         "   Dist->prepare run again" );
290     ok( $dist->create,          "   Dist->create run again" );
291     ok( -s $makefile_pl,        "   Makefile.PL present" );
292     like( CPANPLUS::Error->stack_as_string,
293           qr/attempting to generate one/,
294                                 "   Makefile.PL generation attempt logged" );
295
296     ### now let's throw away the makefile.pl, flush the status and not
297     ### write a makefile.pl
298     {   local $^W;
299         local *CPANPLUS::Dist::MM::write_makefile_pl = sub { 1 };
300
301         unlink $makefile_pl;
302         unlink $makefile;
303
304         ok(!-s $makefile_pl,        "Makefile.PL deleted" );
305         ok(!-s $makefile,           "Makefile deleted" );
306         ok( $dist->status->mk_flush,"Dist status flushed" );
307         ok(!$dist->prepare,         "   Dist->prepare failed" );
308         like( CPANPLUS::Error->stack_as_string,
309               qr/Could not find 'Makefile.PL'/i,
310                                     "   Missing Makefile.PL noted" );
311         is( $dist->status->makefile, 0,
312                                     "   Did not manage to create Makefile" );
313     }
314
315     ### now let's write a makefile.pl that just does 'die'
316     {   local $^W;
317         local *CPANPLUS::Dist::MM::write_makefile_pl = 
318             __PACKAGE__->_custom_makefile_pl_sub( "exit 1;" );
319
320         ### there's no makefile.pl now, since the previous test failed
321         ### to create one
322         #ok( -e $makefile_pl,        "Makefile.PL exists" );
323         #ok( unlink($makefile_pl),   "   Deleting Makefile.PL");
324         ok(!-s $makefile_pl,        "Makefile.PL deleted" );
325         ok( $dist->status->mk_flush,"Dist status flushed" );
326         ok(!$dist->prepare,         "   Dist->prepare failed" );
327         like( CPANPLUS::Error->stack_as_string, qr/Could not run/s,
328                                     "   Logged failed 'perl Makefile.PL'" );
329         is( $dist->status->makefile, 0,
330                                     "   Did not manage to create Makefile" );
331     }
332
333     ### clean up afterwards ###
334     ok( unlink($makefile_pl),   "Deleting Makefile.PL");
335     $dist->status->mk_flush;
336
337 }
338
339 ### test ENV setting in Makefile.PL
340 {   ### use print() not die() -- we're redirecting STDERR in tests!
341     my $env     = ENV_CPANPLUS_IS_EXECUTING;
342     my $sub     = __PACKAGE__->_custom_makefile_pl_sub(
343                                     "print qq[ENV=\$ENV{$env}\n]; exit 1;" );
344     
345     my $clone   = $Mod->clone;
346     $clone->status->fetch( $Mod->status->fetch );
347     
348     ok( $clone,                 'Testing ENV settings $dist->prepare' );
349     ok( $clone->extract,        '   Files extracted' );
350     ok( $clone->prepare,        '   $mod->prepare worked first time' );
351     
352     my $dist        = $clone->status->dist;
353     my $makefile_pl = MAKEFILE_PL->( $clone->status->extract );
354
355     ok( $sub->($dist),          "   Custom Makefile.PL written" );
356     ok( -e $makefile_pl,        "       File exists" );
357
358     ### clear errors    
359     CPANPLUS::Error->flush;
360
361     my $rv = $dist->prepare( force => 1, verbose => 0 );
362     ok( !$rv,                   '   $dist->prepare failed' );
363
364     SKIP: {
365         skip( "Can't test ENV{$env} -- no buffers available", 1 )
366             unless IPC::Cmd->can_capture_buffer;
367
368         my $re = quotemeta( $makefile_pl );
369         like( CPANPLUS::Error->stack_as_string, qr/ENV=$re/,
370                                 "   \$ENV $env set correctly during execution");
371     }
372
373     ### and the ENV var should no longer be set now
374     ok( !$ENV{$env},            "   ENV var now unset" );
375 }    
376
377 sub _custom_makefile_pl_sub {
378     my $pkg = shift;
379     my $txt = shift or return;
380     
381     return sub {
382         my $dist = shift; 
383         my $self = $dist->parent;
384         my $fh   = OPEN_FILE->(
385                     MAKEFILE_PL->($self->status->extract), '>' );
386         print $fh $txt;
387         close $fh;
388     
389         return 1;
390     }
391 }
392
393
394 # Local variables:
395 # c-indentation-style: bsd
396 # c-basic-offset: 4
397 # indent-tabs-mode: nil
398 # End:
399 # vim: expandtab shiftwidth=4:
400
401