1 ### make sure we can find our conf.pl file
4 require "$FindBin::Bin/inc/conf.pl";
9 use CPANPLUS::Configure;
10 use CPANPLUS::Backend;
12 use CPANPLUS::Dist::MM;
13 use CPANPLUS::Internals::Constants;
15 use Test::More 'no_plan';
19 use File::Basename ();
22 my $conf = gimme_conf();
23 my $cb = CPANPLUS::Backend->new( $conf );
26 ### if we need sudo that's no guarantee we can actually run it
27 ### so set $noperms if sudo is required, as that may mean tests
28 ### fail if you're not allowed to execute sudo. This resolves
29 ### #29904: make test should not use sudo
30 my $noperms = $conf->get_program('sudo') || #you need sudo
31 $conf->get_conf('makemakerflags') || #you set some funky flags
32 not -w $Config{installsitelib}; #cant write to install target
34 #$IPC::Cmd::DEBUG = $Verbose;
36 ### Make sure we get the _EUMM_NOXS_ version
37 my $ModName = TEST_CONF_MODULE;
39 ### This is the module name that gets /installed/
40 my $InstName = TEST_CONF_INST_MODULE;
42 ### don't start sending test reports now... ###
43 $cb->_callbacks->send_test_report( sub { 0 } );
44 $conf->set_conf( cpantest => 0 );
46 ### Redirect errors to file ###
47 *STDERR = output_handle() unless $conf->get_conf('verbose');
49 ### dont uncomment this, it screws up where STDOUT goes and makes
50 ### test::harness create test counter mismatches
51 #*STDOUT = output_handle() unless @ARGV;
52 ### for the same test-output counter mismatch, we disable verbose
54 $conf->set_conf( allow_build_interactivity => 0 );
56 ### start with fresh sources ###
57 ok( $cb->reload_indices( update_source => 0 ),
60 ### we might need this Some Day when we're going to install into
61 ### our own sandbox dir.. but for now, no dice due to EU::I bug
62 # $conf->set_program( sudo => '' );
63 # $conf->set_conf( makemakerflags => TEST_INSTALL_EU_MM_FLAGS );
65 ### set alternate install dir ###
66 ### XXX rather pointless, since we can't uninstall them, due to a bug
67 ### in EU::Installed (6871). And therefor we can't test uninstall() or any of
68 ### the EU::Installed functions. So, let's just install into sitelib... =/
69 #my $prefix = File::Spec->rel2abs( File::Spec->catdir(cwd(),'dummy-perl') );
70 #my $rv = $cb->configure_object->set_conf( makemakerflags => "PREFIX=$prefix" );
71 #ok( $rv, "Alternate install path set" );
73 my $Mod = $cb->module_tree( $ModName );
74 my $InstMod = $cb->module_tree( $InstName );
75 ok( $Mod, "Loaded object for: " . $Mod->name );
76 ok( $Mod, "Loaded object for: " . $InstMod->name );
78 ### format_available tests ###
79 { ok( CPANPLUS::Dist::MM->format_available,
80 "Format is available" );
84 local *CPANPLUS::Dist::MM::can_load = sub { 0 };
85 ok(!CPANPLUS::Dist::MM->format_available,
86 " Making format unavailable" );
89 ### test if the error got logged ok ###
90 like( CPANPLUS::Error->stack_as_string,
91 qr/You do not have .+?'CPANPLUS::Dist::MM' not available/s,
92 " Format failure logged" );
94 ### flush the stack ###
95 CPANPLUS::Error->flush;
98 ok( $Mod->fetch, "Fetching module to ".$Mod->status->fetch );
99 ok( $Mod->extract, "Extracting module to ".$Mod->status->extract );
101 ### test target => 'init'
102 { my $dist = $Mod->dist( target => TARGET_INIT );
103 ok( $dist, "Dist created with target => " . TARGET_INIT );
104 ok( !$dist->status->prepared,
105 " Prepare was not run" );
108 ok( $Mod->test, "Testing module" );
110 ok( $Mod->status->dist_cpan->status->test,
111 " Test success registered as status" );
112 ok( $Mod->status->dist_cpan->status->prepared,
113 " Prepared status registered" );
114 ok( $Mod->status->dist_cpan->status->created,
115 " Created status registered" );
116 is( $Mod->status->dist_cpan->status->distdir, $Mod->status->extract,
117 " Distdir status registered properly" );
119 ### test the convenience methods
120 ok( $Mod->prepare, "Preparing module" );
121 ok( $Mod->create, "Creating module" );
123 ok( $Mod->dist, "Building distribution" );
124 ok( $Mod->status->dist_cpan, " Dist registered as status" );
125 isa_ok( $Mod->status->dist_cpan, "CPANPLUS::Dist::MM" );
127 ### flush the lib cache
128 ### otherwise, cpanplus thinks the module's already installed
129 ### since the blib is already in @INC
130 $cb->_flush( list => [qw|lib|] );
134 skip(q[No install tests under core perl], 10) if $ENV{PERL_CORE};
135 skip(q[Possibly no permission to install, skipping], 10) if $noperms;
137 ### we now say 'no perms' if sudo is configured, as per #29904
138 #diag(q[Note: 'sudo' might ask for your password to do the install test])
139 # if $conf->get_program('sudo');
141 ### make sure no options are set in PERL5_MM_OPT, as they might
142 ### change the installation target and therefor will 1. mess up
143 ### the tests and 2. leave an installed copy of our test module
144 ### lying around. This addresses bug #29716: 20_CPANPLUS-Dist-MM.t
145 ### fails (and leaves test files installed) when EUMM options
146 ### include INSTALL_BASE
147 { local $ENV{'PERL5_MM_OPT'};
149 ### add the new dir to the configuration too, so eu::installed tests
150 ### work as they should
151 $conf->set_conf( lib => [ TEST_CONF_INSTALL_DIR ] );
153 ok( $Mod->install( force => 1,
154 makemakerflags => 'PREFIX='.TEST_CONF_INSTALL_DIR,
155 ), "Installing module" );
158 ok( $Mod->status->installed," Module installed according to status" );
161 SKIP: { ### EU::Installed tests ###
162 ### EU::I sometimes fails. See:
163 ### #43292: ~/CPANPLUS-0.85_04 fails t/20_CPANPLUS-Dist-MM.t
164 ### #46890: ExtUtils::Installed + EU::MM PREFIX= don't always work
166 skip( "ExtUtils::Installed issue #46890 prevents these tests from running reliably", 8 );
169 skip( "Old perl on cygwin detected " .
170 "-- tests will fail due to known bugs", 8
173 ### might need it Later when EU::I is fixed..
174 #local @INC = ( TEST_INSTALL_DIR_LIB, @INC );
177 my @missing = $InstMod->validate;
179 is_deeply( \@missing, [],
180 "No missing files" );
184 my @files = $InstMod->files;
186 ### number of files may vary from OS to OS
187 ok( scalar(@files), "All files accounted for" );
188 ok( grep( /$File/, @files),
189 " Found the module" );
191 ### XXX does this work on all OSs?
192 #ok( grep( /man/, @files ),
193 # " Found the manpage" );
197 my ($obj) = $InstMod->packlist;
198 isa_ok( $obj, "ExtUtils::Packlist" );
202 my @dirs = $InstMod->directory_tree;
203 ok( scalar(@dirs), "Directory tree obtained" );
206 for my $dir (@dirs) {
207 ok( -d $dir, " Directory exists" );
209 my $file = File::Spec->catfile( $dir, $File );
210 $found = $file if -e $file;
213 ok( -e $found, " Module found" );
217 skip("Probably no permissions to uninstall", 1)
220 ok( $InstMod->uninstall,"Uninstalling module" );
225 ### test exceptions in Dist::MM->create ###
226 { ok( $Mod->status->mk_flush, "Old status info flushed" );
227 my $dist = INSTALLER_MM->new( module => $Mod );
229 ok( $dist, "New dist object made" );
230 ok(!$dist->prepare, " Dist->prepare failed" );
231 like( CPANPLUS::Error->stack_as_string, qr/No dir found to operate on/,
234 ### manually set the extract dir,
235 $Mod->status->extract($0);
237 ok(!$dist->create, " Dist->create failed" );
238 like( CPANPLUS::Error->stack_as_string, qr/not successfully prepared/s,
241 ### pretend we've been prepared ###
242 $dist->status->prepared(1);
244 ok(!$dist->create, " Dist->create failed" );
245 like( CPANPLUS::Error->stack_as_string, qr/Could not chdir/s,
249 ### writemakefile.pl tests ###
250 { ### remove old status info
251 ok( $Mod->status->mk_flush, "Old status info flushed" );
252 ok( $Mod->fetch, "Module fetched again" );
253 ok( $Mod->extract, "Module extracted again" );
255 ### cheat and add fake prereqs ###
256 my $prereq = TEST_CONF_PREREQ;
258 $Mod->status->prereqs( { $prereq => 0 } );
260 my $makefile_pl = MAKEFILE_PL->( $Mod->status->extract );
261 my $makefile = MAKEFILE->( $Mod->status->extract );
263 my $dist = $Mod->dist;
264 ok( $dist, "Dist object built" );
266 ### check for a makefile.pl and 'write' one
267 ok( -s $makefile_pl, " Makefile.PL present" );
268 ok( $dist->write_makefile_pl( force => 0 ),
269 " Makefile.PL written" );
270 like( CPANPLUS::Error->stack_as_string, qr/Already created/,
271 " Prior existance noted" );
273 ### ok, unlink the makefile.pl, now really write one
274 1 while unlink $makefile;
276 ### must do '1 while' for VMS
277 { my $unlink_sts = unlink($makefile_pl);
278 1 while unlink $makefile_pl;
279 ok( $unlink_sts, "Deleting Makefile.PL");
282 ok( !-s $makefile_pl, " Makefile.PL deleted" );
283 ok( !-s $makefile, " Makefile deleted" );
284 ok($dist->write_makefile_pl," Makefile.PL written" );
286 ### see if we wrote anything sensible
287 my $fh = OPEN_FILE->( $makefile_pl );
288 ok( $fh, "Makefile.PL open for read" );
290 my $str = do { local $/; <$fh> };
291 like( $str, qr/### Auto-generated .+ by CPANPLUS ###/,
292 " Autogeneration noted" );
293 like( $str, '/'. $Mod->module .'/',
294 " Contains module name" );
295 like( $str, '/'. quotemeta($Mod->version) . '/',
296 " Contains version" );
297 like( $str, '/'. $Mod->author->author .'/',
298 " Contains author" );
299 like( $str, '/PREREQ_PM/', " Contains prereqs" );
300 like( $str, qr/$prereq.+0/, " Contains prereqs" );
304 ### seems ok, now delete it again and go via install()
305 ### to see if it picks up on the missing makefile.pl and
306 ### does the right thing
307 ### must do '1 while' for VMS
308 { my $unlink_sts = unlink($makefile_pl);
309 1 while unlink $makefile_pl;
310 ok( $unlink_sts, "Deleting Makefile.PL");
312 ok( !-s $makefile_pl, " Makefile.PL deleted" );
313 ok( $dist->status->mk_flush,"Dist status flushed" );
314 ok( $dist->prepare, " Dist->prepare run again" );
315 ok( $dist->create, " Dist->create run again" );
316 ok( -s $makefile_pl, " Makefile.PL present" );
317 like( CPANPLUS::Error->stack_as_string,
318 qr/attempting to generate one/,
319 " Makefile.PL generation attempt logged" );
321 ### now let's throw away the makefile.pl, flush the status and not
322 ### write a makefile.pl
324 local *CPANPLUS::Dist::MM::write_makefile_pl = sub { 1 };
326 1 while unlink $makefile_pl;
327 1 while unlink $makefile;
329 ok(!-s $makefile_pl, "Makefile.PL deleted" );
330 ok(!-s $makefile, "Makefile deleted" );
331 ok( $dist->status->mk_flush,"Dist status flushed" );
332 ok(!$dist->prepare, " Dist->prepare failed" );
333 like( CPANPLUS::Error->stack_as_string,
334 qr/Could not find 'Makefile.PL'/i,
335 " Missing Makefile.PL noted" );
336 is( $dist->status->makefile, 0,
337 " Did not manage to create Makefile" );
340 ### now let's write a makefile.pl that just does 'die'
342 local *CPANPLUS::Dist::MM::write_makefile_pl =
343 __PACKAGE__->_custom_makefile_pl_sub( "exit 1;" );
345 ### there's no makefile.pl now, since the previous test failed
347 #ok( -e $makefile_pl, "Makefile.PL exists" );
348 #ok( unlink($makefile_pl), " Deleting Makefile.PL");
349 ok(!-s $makefile_pl, "Makefile.PL deleted" );
350 ok( $dist->status->mk_flush,"Dist status flushed" );
351 ok(!$dist->prepare, " Dist->prepare failed" );
352 like( CPANPLUS::Error->stack_as_string, qr/Could not run/s,
353 " Logged failed 'perl Makefile.PL'" );
354 is( $dist->status->makefile, 0,
355 " Did not manage to create Makefile" );
358 ### clean up afterwards ###
359 ### must do '1 while' for VMS
360 { my $unlink_sts = unlink($makefile_pl);
361 1 while unlink $makefile_pl;
362 ok( $unlink_sts, "Deleting Makefile.PL");
365 $dist->status->mk_flush;
368 ### test ENV setting in Makefile.PL
369 { ### use print() not die() -- we're redirecting STDERR in tests!
370 my $env = ENV_CPANPLUS_IS_EXECUTING;
371 my $sub = __PACKAGE__->_custom_makefile_pl_sub(
372 "print qq[ENV=\$ENV{$env}\n]; exit 1;" );
374 my $clone = $Mod->clone;
375 $clone->status->fetch( $Mod->status->fetch );
377 ok( $clone, 'Testing ENV settings $dist->prepare' );
378 ok( $clone->extract, ' Files extracted' );
379 ok( $clone->prepare, ' $mod->prepare worked first time' );
381 my $dist = $clone->status->dist;
382 my $makefile_pl = MAKEFILE_PL->( $clone->status->extract );
384 ok( $sub->($dist), " Custom Makefile.PL written" );
385 ok( -e $makefile_pl, " File exists" );
388 CPANPLUS::Error->flush;
390 my $rv = $dist->prepare( force => 1, verbose => 0 );
391 ok( !$rv, ' $dist->prepare failed' );
394 skip( "Can't test ENV{$env} -- no buffers available", 1 )
395 unless IPC::Cmd->can_capture_buffer;
397 my $re = quotemeta( $makefile_pl );
398 like( CPANPLUS::Error->stack_as_string, qr/ENV=$re/,
399 " \$ENV $env set correctly during execution");
402 ### and the ENV var should no longer be set now
403 ok( !$ENV{$env}, " ENV var now unset" );
406 sub _custom_makefile_pl_sub {
408 my $txt = shift or return;
412 my $self = $dist->parent;
413 my $fh = OPEN_FILE->(
414 MAKEFILE_PL->($self->status->extract), '>' );
424 # c-indentation-style: bsd
426 # indent-tabs-mode: nil
428 # vim: expandtab shiftwidth=4: