Update CPANPLUS to 0.85_06
[p5sagit/p5-mst-13.2.git] / lib / CPANPLUS / t / 40_CPANPLUS-Internals-Report.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::Backend;
9 use CPANPLUS::Internals::Constants::Report;
10
11 my $send_tests  = 55;
12 my $query_tests = 8;
13 my $total_tests = $send_tests + $query_tests;
14
15 use Test::More                  'no_plan';
16 use Module::Load::Conditional   qw[can_load];
17
18 use FileHandle;
19 use Data::Dumper;
20
21 use constant NOBODY => 'nobody@xs4all.nl';
22
23 my $conf        = gimme_conf();
24 my $CB          = CPANPLUS::Backend->new( $conf );
25 my $ModName     = TEST_CONF_MODULE;
26 my $ModPrereq   = TEST_CONF_PREREQ;
27
28 ### divide by many -- possibly ~0 is unsigned, and we cause an overflow,
29 ### as happens to version.pm 0.7203 among others.
30 my $HighVersion = ~0/1000;
31 my $Mod         = $CB->module_tree($ModName);
32 my $int_ver     = $CPANPLUS::Internals::VERSION;
33
34 ### explicitly enable testing if possible ###
35 $CB->configure_object->set_conf(cpantest =>1) if $ARGV[0];
36
37 my $map = {
38     all_ok  => {
39         buffer  => '',              # output from build process
40         failed  => 0,               # indicate failure
41         match   => [qw|/PASS/|],    # list of regexes for the output
42         check   => 0,               # check if callbacks got called?
43     },
44     skipped_test => {
45         buffer  => '',
46         failed  => 0,
47         match   => ['/PASS/',
48                     '/tests for this module were skipped during this build/',
49                 ],
50         check   => 0,
51         skiptests
52                 => 1,               # did we skip the tests?
53     },                    
54     missing_prereq  => {
55         buffer  => missing_prereq_buffer(),
56         failed  => 1,
57         match   => ['/The comments above are created mechanically/',
58                     '/computer-generated error report/',
59                     '/Below is the error stack from stage/',
60                     '/test suite seem to fail without these modules/',
61                     '/floo/',
62                     '/FAIL/',
63                     '/make test/',
64                 ],
65         check   => 1,
66     },
67     missing_tests   => {
68         buffer  => missing_tests_buffer(),
69         failed  => 1,
70         match   => ['/The comments above are created mechanically/',
71                     '/computer-generated error report/',
72                     '/Below is the error stack from stage/',
73                     '/RECOMMENDATIONS/',
74                     '/UNKNOWN/',
75                     '/make test/',
76                 ],
77         check   => 0,
78     },
79     perl_version_too_low_mm => {
80         buffer  => perl_version_too_low_buffer_mm(),
81         failed  => 1,
82         match   => ['/This distribution has been tested/',
83                     '/http://testers.cpan.org/',
84                     '/NA/',
85                 ],
86         check   => 0,
87     },    
88     perl_version_too_low_build1 => {
89         buffer  => perl_version_too_low_buffer_build(1),
90         failed  => 1,
91         match   => ['/This distribution has been tested/',
92                     '/http://testers.cpan.org/',
93                     '/NA/',
94                 ],
95         check   => 0,
96     },    
97     perl_version_too_low_build2 => {
98         buffer  => perl_version_too_low_buffer_build(2),
99         failed  => 1,
100         match   => ['/This distribution has been tested/',
101                     '/http://testers.cpan.org/',
102                     '/NA/',
103                 ],
104         check   => 0,
105     },    
106     prereq_versions_too_low => {
107         ### set the prereq version incredibly high
108         pre_hook    => sub {
109                         my $mod     = shift;
110                         my $clone   = $mod->clone;
111                         $clone->status->prereqs({ $ModPrereq => $HighVersion });
112                         return $clone;
113                     },
114         failed      => 1,
115         match       => ['/This distribution has been tested/',
116                         '/http://testers.cpan.org/',
117                         '/NA/',
118                     ],
119         check       => 0,    
120     },
121     prereq_not_on_cpan => {
122         pre_hook    => sub {
123                         my $mod     = shift;
124                         my $clone   = $mod->clone;
125                         $clone->status->prereqs( 
126                             { TEST_CONF_INVALID_MODULE, 0 } 
127                         );
128                         return $clone;
129                     },
130         failed      => 1,
131         match       => ['/This distribution has been tested/',
132                         '/http://testers.cpan.org/',
133                         '/NA/',
134                     ],
135         check       => 0,    
136     },
137     prereq_not_on_cpan_but_core => {
138         pre_hook    => sub {
139                         my $mod     = shift;
140                         my $clone   = $mod->clone;
141                         $clone->status->prereqs( 
142                             { TEST_CONF_PREREQ, 0 } 
143                         );
144                         return $clone;
145                     },
146         failed      => 1,
147         match       => ['/This distribution has been tested/',
148                         '/http://testers.cpan.org/',
149                         '/UNKNOWN/',
150                     ],
151         check       => 0,    
152     },
153 };
154
155 ### test config settings 
156 {   for my $opt ( qw[cpantest cpantest_mx] ) {
157         my $warnings;
158         local $SIG{__WARN__} = sub { $warnings .= "@_" };
159
160         my $org = $conf->get_conf( $opt );
161         ok( $conf->set_conf( $opt => $$ ),
162                                 "Setting option $opt to $$" );
163         is( $conf->get_conf( $opt ), $$,
164                                 "   Retrieved properly" );
165         ok( $conf->set_conf( $opt => $org ),
166                                 "   Option $opt set back to original" );
167         ok( !$warnings,         "   No warnings" );                                
168     }
169 }
170
171 ### test constants ###
172 {   {   my $to = CPAN_MAIL_ACCOUNT->('foo');
173         is( $to, 'foo@cpan.org',        "Got proper mail account" );
174     }
175
176     {   ok(RELEVANT_TEST_RESULT->($Mod),"Test is relevant" );
177
178         ### test non-relevant tests ###
179         my $cp = $Mod->clone;
180         $cp->module( $Mod->module . '::' . ($^O eq 'beos' ? 'MSDOS' : 'Be') );
181         ok(!RELEVANT_TEST_RESULT->($cp),"Test is irrelevant");
182     }
183
184     {   my $support = "it works!";
185         my @support = ( "No support for OS",
186                         "OS unsupported",
187                         "os unsupported",
188         );
189         ok(!UNSUPPORTED_OS->($support), "OS supported");
190         ok( UNSUPPORTED_OS->($_),   "OS not supported") for(@support);
191     }
192
193     {   ok(PERL_VERSION_TOO_LOW->( perl_version_too_low_buffer_mm() ),
194                                         "Perl version too low" );
195         ok(PERL_VERSION_TOO_LOW->( perl_version_too_low_buffer_build(1) ),
196                                         "Perl version too low" );
197         ok(PERL_VERSION_TOO_LOW->( perl_version_too_low_buffer_build(2) ),
198                                         "Perl version too low" );
199         ok(!PERL_VERSION_TOO_LOW->('foo'),
200                                         "   Perl version adequate" );
201     }
202
203     {   my $tests = "test.pl";
204         my @none  = (   "No tests defined for Foo extension.",
205                         "'No tests defined for Foo::Bar extension.'",
206                         "'No tests defined.'",
207         );
208         ok(!NO_TESTS_DEFINED->($tests), "Tests defined");
209         ok( NO_TESTS_DEFINED->($_),  "No tests defined")    for(@none);
210     }
211
212     {   my $fail = 'MAKE TEST'; my $unknown = 'foo';
213         is( TEST_FAIL_STAGE->($fail), lc $fail,
214                                         "Proper test fail stage found" );
215         is( TEST_FAIL_STAGE->($unknown), 'fetch',
216                                         "Proper test fail stage found" );
217     }
218
219     ### test missing prereqs        
220     {   my $str = q[Can't locate Foo/Bar.pm in @INC];
221     
222         ### standard test
223         {   my @list = MISSING_PREREQS_LIST->( $str );
224             is( scalar(@list),  1,      "   List of missing prereqs found" );
225             is( $list[0], 'Foo::Bar',   "       Proper prereq found" );
226         }
227     
228         ### multiple mentions of same prereq
229         {   my @list = MISSING_PREREQS_LIST->( $str . $str );
230
231             is( scalar(@list),  1,      "   1 result for multiple mentions" );
232             is( $list[0], 'Foo::Bar',   "   Proper prereq found" );
233         }
234     }
235
236     {                                       # cp version, author
237         my $header = REPORT_MESSAGE_HEADER->($int_ver,'foo');
238         ok( $header,                    "Test header generated" );
239         like( $header, qr/Dear foo,/,   "   Proper content found" );
240         like( $header, qr/puter-gen/,   "   Proper content found" );
241         like( $header, qr/CPANPLUS,/,   "   Proper content found" );
242         like( $header, qr/ments may/,   "   Proper content found" );
243     }
244
245     {                                       # stage, buffer
246         my $header = REPORT_MESSAGE_FAIL_HEADER->('test','buffer');
247         ok( $header,                    "Test header generated" );
248         like( $header, qr/uploading/,   "   Proper content found" );
249         like( $header, qr/RESULTS:/,    "   Proper content found" );
250         like( $header, qr/stack/,       "   Proper content found" );
251         like( $header, qr/buffer/,      "   Proper content found" );
252     }
253
254     {   my $prereqs = REPORT_MISSING_PREREQS->('foo','bar@example.com','Foo::Bar');
255         ok( $prereqs,                   "Test output generated" );
256         like( $prereqs, qr/'foo \(bar\@example\.com\)'/, 
257                                         "   Proper content found" );
258         like( $prereqs, qr/Foo::Bar/,   "   Proper content found" );
259         like( $prereqs, qr/prerequisi/, "   Proper content found" );
260         like( $prereqs, qr/PREREQ_PM/,  "   Proper content found" );
261     }
262
263     {   my $prereqs = REPORT_MISSING_PREREQS->(undef,undef,'Foo::Bar');    
264         ok( $prereqs,                   "Test output generated" );
265         like( $prereqs, qr/Your Name/,  "   Proper content found" );
266         like( $prereqs, qr/Foo::Bar/,   "   Proper content found" );
267         like( $prereqs, qr/prerequisi/, "   Proper content found" );
268         like( $prereqs, qr/PREREQ_PM/,  "   Proper content found" );
269     }
270
271     {   my $missing = REPORT_MISSING_TESTS->();
272         ok( $missing,                   "Missing test string generated" );
273         like( $missing, qr/tests/,      "   Proper content found" );
274         like( $missing, qr/Test::More/, "   Proper content found" );
275     }
276
277     {   my $missing = REPORT_MESSAGE_FOOTER->();
278         ok( $missing,                   "Message footer string generated" );
279         like( $missing, qr/NOTE/,       "   Proper content found" );
280         like( $missing, qr/identical/,  "   Proper content found" );
281         like( $missing, qr/mistaken/,   "   Proper content found" );
282         like( $missing, qr/appreciate/, "   Proper content found" );
283         like( $missing, qr/Additional/, "   Proper content found" );
284     }
285
286     {   my @libs = MISSING_EXTLIBS_LIST->("No library found for -lfoo\nNo library found for -lbar");
287         ok( @libs,                      "Missing external libraries found" );
288         my @list = qw(foo bar);
289         is_deeply( \@libs, \@list,      "   Proper content found" );
290     }
291     
292     {   my $clone   = $Mod->clone;
293
294         my $prereqs = { $ModPrereq => $HighVersion };
295     
296         $clone->status->prereqs( $prereqs );
297
298         my $str = REPORT_LOADED_PREREQS->( $clone );
299         
300         like($str, qr/PREREQUISITES:/,  "Listed loaded prerequisites" );
301         like($str, qr/\! $ModPrereq\s+\S+\s+\S+/,
302                                         "   Proper content found" );
303     }
304 }
305
306 ### callback tests
307 {   ### as reported in bug 13086, this callback returned the wrong item 
308     ### from the list:
309     ### $self->_callbacks->munge_test_report->($Mod, $message, $grade);     
310     my $rv = $CB->_callbacks->munge_test_report->( 1..4 );   
311     is( $rv, 2,                 "Default 'munge_test_report' callback OK" );
312 }
313
314
315 ### test creating test reports ###
316 SKIP: {
317         skip "You have chosen not to enable test reporting", $total_tests,
318         unless $CB->configure_object->get_conf('cpantest');
319
320     skip "No report send & query modules installed", $total_tests
321         unless $CB->_have_query_report_modules(verbose => 0);
322
323
324     SKIP: {   
325         my $mod = $CB->module_tree( TEST_CONF_PREREQ ); # is released to CPAN
326         ok( $mod,                           "Module retrieved" );
327         
328         ### so we're not pinned down to this specific version of perl
329         my @list = $mod->fetch_report( all_versions => 1 );
330         skip "Possibly no net connection, or server down", 7 unless @list;
331      
332         my $href = $list[0];
333         ok( scalar(@list),                  "Fetched test report" );
334         is( ref $href, ref {},              "   Return value has hashrefs" );
335
336         ok( $href->{grade},                 "   Has a grade" );
337
338         ### XXX use constants for grades?
339         like( $href->{grade}, qr/pass|fail|unknown|na/i,
340                                             "   Grade as expected" );
341
342         my $pkg_name = $mod->package_name;
343         ok( $href->{dist},                  "   Has a dist" );
344         like( $href->{dist}, qr/$pkg_name/, "   Dist as expected" );
345
346         ok( $href->{platform},              "   Has a platform" );
347     }
348
349     skip "No report sending modules installed", $send_tests
350         unless $CB->_have_send_report_modules(verbose => 0);
351
352     for my $type ( keys %$map ) {
353
354
355         ### never enter the editor for test reports
356         ### but check if the callback actually gets called;
357         my $called_edit; my $called_send;
358         $CB->_register_callback(
359             name => 'edit_test_report',
360             code => sub { $called_edit++; 0 }
361         );
362
363         $CB->_register_callback(
364             name => 'send_test_report',
365             code => sub { $called_send++; 1 }
366         );
367
368                 ### reset from earlier tests
369                 $CB->_register_callback(
370             name => 'munge_test_report',
371             code => sub { return $_[1] }
372         );
373
374         my $mod = $map->{$type}->{'pre_hook'}
375                     ? $map->{$type}->{'pre_hook'}->( $Mod )
376                     : $Mod;
377
378         my $file = do {
379             ### so T::R does not try to resolve our maildomain, which can 
380             ### lead to large timeouts for *every* invocation in T::R < 1.51_01
381             ### see: http://code.google.com/p/test-reporter/issues/detail?id=15
382             local $ENV{MAILDOMAIN} ||= 'example.com';
383             $CB->_send_report(
384                         module        => $mod,
385                         buffer        => $map->{$type}{'buffer'},
386                         failed        => $map->{$type}{'failed'},
387                         tests_skipped => ($map->{$type}{'skiptests'} ? 1 : 0),
388                         save          => 1,
389                     );
390         };
391
392         ok( $file,              "Type '$type' written to file" );
393         ok( -e $file,           "   File exists" );
394
395         my $fh = FileHandle->new($file);
396         ok( $fh,                "   Opened file for reading" );
397
398         my $in = do { local $/; <$fh> };
399         ok( $in,                "   File has contents" );
400
401         for my $regex ( @{$map->{$type}->{match}} ) {
402             like( $in, $regex,  "   File contains expected contents" );
403         }
404
405         ### check if our registered callback got called ###
406         if( $map->{$type}->{check} ) {
407             ok( $called_edit,   "   Callback to edit was called" );
408             ok( $called_send,   "   Callback to send was called" );
409         }
410
411         #unlink $file;
412
413
414 ### T::R tests don't even try to mail, let's not try and be smarter
415 ### ourselves
416 #        {   ### use a dummy 'editor' and see if the editor
417 #            ### invocation doesn't break things
418 #            $conf->set_program( editor => "$^X -le1" );
419 #            $CB->_callbacks->edit_test_report( sub { 1 } );
420 #
421 #            ### XXX whitebox test!!! Might change =/
422 #            ### this makes test::reporter not ask for what editor to use
423 #            ### XXX stupid lousy perl warnings;
424 #            local $Test::Reporter::MacApp = 1;
425 #            local $Test::Reporter::MacApp = 1;
426 #
427 #            ### now try and mail the report to a /dev/null'd mailbox
428 #            my $ok = $CB->_send_report(
429 #                            module  => $Mod,
430 #                            buffer  => $map->{$type}->{'buffer'},
431 #                            failed  => $map->{$type}->{'failed'},
432 #                            address => NOBODY,
433 #                        );
434 #            ok( $ok,                "   Mailed report to NOBODY" );
435 #       }
436     }
437 }
438
439
440 sub missing_prereq_buffer {
441     return q[
442 MAKE TEST:
443 Can't locate floo.pm in @INC (@INC contains: /Users/kane/sources/p4/other/archive-extract/lib /Users/kane/sources/p4/other/file-fetch/lib /Users/kane/sources/p4/other/archive-tar-new/lib /Users/kane/sources/p4/other/carp-trace/lib /Users/kane/sources/p4/other/log-message/lib /Users/kane/sources/p4/other/module-load/lib /Users/kane/sources/p4/other/params-check/lib /Users/kane/sources/p4/other/qmail-checkpassword/lib /Users/kane/sources/p4/other/module-load-conditional/lib /Users/kane/sources/p4/other/term-ui/lib /Users/kane/sources/p4/other/ipc-cmd/lib /Users/kane/sources/p4/other/config-auto/lib /Users/kane/sources/NSA /Users/kane/sources/NSA/misc /Users/kane/sources/NSA/test /Users/kane/sources/beheer/perl /opt/lib/perl5/5.8.3/darwin-2level /opt/lib/perl5/5.8.3 /opt/lib/perl5/site_perl/5.8.3/darwin-2level /opt/lib/perl5/site_perl/5.8.3 /opt/lib/perl5/site_perl .).
444 BEGIN failed--compilation aborted.
445     ];
446 }
447
448 sub missing_tests_buffer {
449     return q[
450 cp lib/Acme/POE/Knee.pm blib/lib/Acme/POE/Knee.pm
451 cp demo_race.pl blib/lib/Acme/POE/demo_race.pl
452 cp demo_simple.pl blib/lib/Acme/POE/demo_simple.pl
453 MAKE TEST:
454 No tests defined for Acme::POE::Knee extension.
455     ];
456 }
457
458 sub perl_version_too_low_buffer_mm {
459     return q[
460 Running [/usr/bin/perl5.8.1 Makefile.PL ]...
461 Perl v5.8.3 required--this is only v5.8.1, stopped at Makefile.PL line 1.
462 BEGIN failed--compilation aborted at Makefile.PL line 1.
463 [ERROR] Could not run '/usr/bin/perl5.8.1 Makefile.PL': Perl v5.8.3 required--this is only v5.8.1, stopped at Makefile.PL line 1.
464 BEGIN failed--compilation aborted at Makefile.PL line 1.
465  -- cannot continue
466     ];
467 }    
468
469 sub perl_version_too_low_buffer_build {
470     my $type = shift;
471     return q[
472 ERROR: perl: Version 5.006001 is installed, but we need version >= 5.008001
473 ERROR: version: Prerequisite version isn't installed
474 ERRORS/WARNINGS FOUND IN PREREQUISITES.  You may wish to install the versions
475  of the modules indicated above before proceeding with this installation.
476     ]   if($type == 1);
477     return q[
478 ERROR: Version 5.006001 of perl is installed, but we need version >= 5.008001
479 ERROR: version: Prerequisite version isn't installed
480 ERRORS/WARNINGS FOUND IN PREREQUISITES.  You may wish to install the versions
481  of the modules indicated above before proceeding with this installation.
482     ]   if($type == 2);
483 }    
484
485 # Local variables:
486 # c-indentation-style: bsd
487 # c-basic-offset: 4
488 # indent-tabs-mode: nil
489 # End:
490 # vim: expandtab shiftwidth=4: