1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 370 371 372 373 374 375 376 377 378 379 380 381 382 383 384 385 386 387 388 389 390 391 392 393 394 395 396 397 398 399 400 401 402 403 404 405 406 407 408 409 410 411 412 413 414 415 416 417 418 419 420 421 422 423 424 425 426 427 428 429 430 431 432 433 434 435 436 437 438 439 440 441 442 443 444 445 446 447 448 449 450 451 452 453 454 455 456 457 458 459 460 461 462 463 464 465 466 467 468 469 470 471 472 473 474 475 476 477 478 479 480 481 482 483 484 485 486 487 488 489 490 491 492 493 494 495 496 497 498 499 500 501 502 503 504 505 506 507 508 509 510 511 512 513 514 515 516 517 518 519 520 521 522 523 524 525 526 527 528 529 530 531 532 533 534 535 536 537 538 539 540 541 542 543 544 545 546 547 548 549 550 551 552 553 554 555 556 557 558 559 560 561 562 563 564 565 566 567 568 569 570 571 572 573 574 575 576 577 578 579 580 581 582 583 584 585 586 587 588 589 590 591 592 593 594 595 596 597 598 599 600 601 602 603 604 605 606 607 608 609 610 611 612 613 614 615 616 617 618 619 620 621 622 623 624 625 626 627 628 629 630 631 632 633 634 635 636 637 638 639 640 641 642 643 644 645 646 647 648 649 650 651 652 653 654 655 656 657 658 659 660 661 662 663 664 665 666 667 668 669 670 671 672 673 674 675 676 677 678 679 680 681 682 683 684 685 686 687 688 689 690 691 692 693 694 695 696 697 698 699 700 701 702 703 704 705 706 707 708 709 710 711 712 713 714 715 716 717 718 719 720 721 722 723 724 725 726 727 728 729 730 731 732 733 734 735 736 737 738 739 740 741 742 743 744 745 746 747 748 749 750 751 752 753 754 755 756 757 758 759 760 761 762 763 764 765 766 767 768 769 770 771 772 773 774 775 776 777 778 779 780 781 782 783 784 785 786 787 788 789 790 791 792 793 794 795 796 797 798 799 800 801 802 803 804 805 806 807 808 809 810 811 812 813 814 815 816 817 818 819 820 821 822 823 824 825 826 827 828 829 830 831 832 833 834 835 836 837 838 839 840 841 842 843 844 845 846 847 848 849 850 851 852 853 854 855 856 857 858 859 860 861 862 863 864 865 866 867 868 869 870 871 872 873 874 875 876 877 878 879
|
package Helper;
use strict;
BEGIN{ if (not $] < 5.006) { require warnings; warnings->import } }
use vars qw/@EXPORT/;
@EXPORT = qw/
test_grade_make test_grade_make_plan
test_grade_PL test_grade_PL_plan
test_grade_test test_grade_test_plan
test_fake_config test_fake_config_plan
test_report test_report_plan
test_dispatch test_dispatch_plan
/;
use Exporter ();
our @ISA = 'Exporter';
use Config;
use Archive::Tar 1.54 ();
use File::Basename qw/basename/;
use File::Copy;
use File::Find;
use File::Path qw/mkpath/;
use File::pushd 0.32;
use File::Spec 3.19 ();
use File::Temp 0.16 qw/tempdir/;
use Capture::Tiny qw/capture/;
use Probe::Perl ();
use Test::More 0.62;
use MockHomeDir;
#--------------------------------------------------------------------------#
# Fixtures
#--------------------------------------------------------------------------#
my $perl = Probe::Perl->find_perl_interpreter();
$perl = qq{"$perl"}; # protect from shell
my $make = $Config{make};
my $temp_stdout = File::Temp->new()
or die "Couldn't make temporary file:$!\nIs your temp drive full?";
my $corpus_dir = "./corpus";
my $home_dir = MockHomeDir::home_dir();
my $config_dir = File::Spec->catdir( $home_dir, ".cpanreporter" );
my $config_file = File::Spec->catfile( $config_dir, "config.ini" );
my $bogus_email_from = 'johndoe@example.com';
my $bogus_email_to = 'no_one@example.com';
my %tool_constants = (
'eumm' => {
module => 'ExtUtils::MakeMaker',
have => eval "require ExtUtils::MakeMaker" || 0,
PL => 'Makefile.PL',
},
'mb' => {
module => 'Module::Build',
have => eval "require Module::Build" || 0,
PL => 'Build.PL'
},
);
my $max_report_length = 1_000_000; # 1000K
# used to capture from fixtures
use vars qw/$sent_report @cc_list/;
#--------------------------------------------------------------------------#
# test config file prep
#--------------------------------------------------------------------------#
sub test_fake_config_plan() { 4 }
sub test_fake_config {
local $Test::Builder::Level = $Test::Builder::Level + 1;
my %overrides = @_;
is( File::HomeDir::my_documents(), MockHomeDir::home_dir(),
"home directory mocked"
);
mkpath $config_dir;
ok( -d $config_dir,
"config directory created"
);
my $metabase_file = File::Spec->catfile( $config_dir, 'metabase_id.json' );
# 2-args open with bare descriptor to work in older perls
open METABASE, ">$metabase_file";
close METABASE;
ok -r $metabase_file, 'created mock metabase file for testing';
my $tiny = Config::Tiny->new();
$tiny->{_}{email_from} = $bogus_email_from;
$tiny->{_}{email_to} = $bogus_email_to; # failsafe
$tiny->{_}{send_report} = "yes";
$tiny->{_}{send_duplicates} = "yes"; # tests often repeat same stuff
$tiny->{_}{transport} = "Metabase uri https://metabase.cpantesters.org/api/v1/ id_file metabase_id.json";
for my $key ( keys %overrides ) {
$tiny->{_}{$key} = $overrides{$key};
}
ok( $tiny->write( $config_file ),
"created temp config file"
);
}
#--------------------------------------------------------------------------#
# Test grade_PL
#--------------------------------------------------------------------------#
sub test_grade_PL_iter_plan() { 5 }
sub test_grade_PL_plan() { test_grade_PL_iter_plan() * 2 }
sub test_grade_PL {
my ($case, $dist) = @_;
local $Test::Builder::Level = $Test::Builder::Level + 1;
local $ENV{PERL_MM_USE_DEFAULT} = 1;
my $short_name = _short_name( $dist );
for my $tool ( qw/eumm mb/ ) {
SKIP: {
my ($have_tool,$tool_mod,$tool_PL) =
@{$tool_constants{$tool}}{qw/have module PL/};
my $tool_label = $tool eq 'eumm' ? "Makefile.PL"
: "Build.PL";
my $tool_cmd = "$perl $tool_label";
skip "$tool_mod not installed or working", test_grade_PL_iter_plan()
if ! $have_tool;
my $tempd = _ok_clone_dist_dir( $case->{name} );
local $dist->{build_dir} = "$tempd";
$Helper::sent_report = undef;
$Helper::comments = undef;
my ($stdout, $stderr, $build_rc, $test_build_rc,
$output, $exit_value, $rc);
eval {
($stdout, $stderr) = capture {
($output, $exit_value) =
CPAN::Reporter::record_command($tool_cmd);
$rc = CPAN::Reporter::grade_PL(
$dist, $tool_cmd, $output, $exit_value
);
};
};
if ( $@ ) {
diag "DIED WITH:\n$@";
_diag_output( $stdout, $stderr );
skip "died grading PL", test_grade_PL_iter_plan() - 1;
}
my $is_rc_correct = $case->{"$tool\_success"}
? $rc : ! $rc;
ok( $is_rc_correct,
"$case->{name}: grade_PL() for $tool_PL returned " .
$case->{"$tool\_success"}
);
my $case_grade = $case->{"$tool\_grade"};
my $is_grade_correct;
# Grade evaluation with special case if discarding
my ($found_grade_result, $found_msg) =
( $stdout =~ /^CPAN::Reporter: ([^,]+), ([^\n]+)/ms );
if ( $case_grade eq 'discard' ) {
is ($found_grade_result, "test results were not valid",
"$case->{name}: '$tool_label' saw test results not valid message"
);
like( $stdout,
"/Test report will not be sent/",
"$case->{name}: discard message correct"
) and $is_grade_correct++;
ok( ! defined $Helper::sent_report,
"$case->{name}: test results discarded"
);
}
else {
my ($found_grade) = ( $found_grade_result =~ /$tool_label result is '([^']+)'/ );
is( $found_grade, $case_grade,
"$case->{name}: '$tool_label' grade reported as '$case_grade'"
) or _diag_output( $stdout, $stderr );
my $ctr_regex = "/preparing a CPAN Testers report for \Q$short_name\E/";
if ( $case_grade eq 'pass' ) {
unlike( $stdout, $ctr_regex ,
"$case->{name}: report notification correct"
) and $is_grade_correct++;
ok( ! defined $Helper::sent_report,
"$case->{name}: results not sent"
);
}
else {
like( $stdout, $ctr_regex ,
"$case->{name}: report notification correct"
) and $is_grade_correct++;
if ( -r $config_file ) {
ok( defined $Helper::sent_report && length $Helper::sent_report,
"$case->{name}: report was mock sent"
);
}
else {
ok( ! defined $Helper::sent_report,
"$case->{name}: results not sent"
);
}
}
}
_diag_output( $stdout, $stderr )
unless ( $is_rc_correct && $is_grade_correct );
} # SKIP
} # for
}
#--------------------------------------------------------------------------#
# Test grade_make
#--------------------------------------------------------------------------#
sub test_grade_make_iter_plan() { 6 }
sub test_grade_make_plan() { test_grade_make_iter_plan() * 2 }
sub test_grade_make {
my ($case, $dist) = @_;
local $Test::Builder::Level = $Test::Builder::Level + 1;
local $ENV{PERL_MM_USE_DEFAULT} = 1;
my $short_name = _short_name( $dist );
for my $tool ( qw/eumm mb/ ) {
SKIP: {
my ($have_tool,$tool_mod,$tool_PL) =
@{$tool_constants{$tool}}{qw/have module PL/};
my $tool_cmd = $tool eq 'eumm' ? $Config{make} : "$perl Build";
my $tool_label = $tool eq 'eumm' ? $Config{make} : "Build";
skip "$tool_mod not installed or working", test_grade_make_iter_plan()
if ! $have_tool;
# Set up temporary directory for the case
my $tempd = _ok_clone_dist_dir( $case->{name} );
$Helper::sent_report = undef;
$Helper::comments = undef;
my ($stdout, $stderr, $build_err, $test_build_rc,
$output, $exit_value, $rc);
($stdout, $stderr) = capture {
$build_err = system("$perl $tool_PL");
};
ok( ! $build_err, "$case->{name}: $tool_PL successful" )
or do {
_diag_output( $stdout, $stderr );
skip "$tool_PL failed", test_grade_make_iter_plan() - 1;
};
eval {
($stdout, $stderr) = capture {
($output, $exit_value) =
CPAN::Reporter::record_command($tool_cmd);
$rc = CPAN::Reporter::grade_make(
$dist, $tool_cmd, $output, $exit_value
);
};
};
if ( $@ ) {
diag "DIED WITH:\n$@";
_diag_output( $stdout, $stderr );
skip "died grading make", test_grade_make_iter_plan() - 1;
}
my $is_rc_correct = $case->{"$tool\_success"}
? $rc : ! $rc;
ok( $is_rc_correct,
"$case->{name}: grade_make() for $tool_label returned " .
$case->{"$tool\_success"}
);
my $case_grade = $case->{"$tool\_grade"};
my $is_grade_correct;
# Grade evaluation with special case if discarding
my ($found_grade_result, $found_msg) =
( $stdout =~ /^CPAN::Reporter: ([^,]+), ([^\n]+)/ms );
if ( $case_grade eq 'discard' ) {
is ($found_grade_result, "test results were not valid",
"$case->{name}: '$tool_label' prerequisites not satisifed"
);
like( $stdout,
"/Test report will not be sent/",
"$case->{name}: discard message correct"
) and $is_grade_correct++;
ok( ! defined $Helper::sent_report,
"$case->{name}: test results discarded"
);
}
else {
my ($found_grade) = ( $found_grade_result =~ /\Q$tool_label\E result is '([^']+)'/ );
is( $found_grade, $case_grade,
"$case->{name}: '$tool_label' grade reported as '$case_grade'"
) or _diag_output( $stdout, $stderr );
my $ctr_regex = "/preparing a CPAN Testers report for \Q$short_name\E/";
if ( $case_grade eq 'pass' ) {
unlike( $stdout, $ctr_regex ,
"$case->{name}: report notification correct"
) and $is_grade_correct++;
ok( ! defined $Helper::sent_report,
"$case->{name}: results not sent"
);
}
else {
like( $stdout, $ctr_regex ,
"$case->{name}: report notification correct"
) and $is_grade_correct++;
if ( -r $config_file ) {
ok( defined $Helper::sent_report && length $Helper::sent_report,
"$case->{name}: report was mock sent"
);
}
else {
ok( ! defined $Helper::sent_report,
"$case->{name}: results not sent"
);
}
}
}
_diag_output( $stdout, $stderr )
unless ( $is_rc_correct && $is_grade_correct );
} #SKIP
} #for
}
#--------------------------------------------------------------------------#
# Test grade_test
#--------------------------------------------------------------------------#
sub test_grade_test_iter_plan() { 7 }
sub test_grade_test_plan() { 2 * test_grade_test_iter_plan() }
sub test_grade_test {
my ($case, $dist) = @_;
local $Test::Builder::Level = $Test::Builder::Level + 1;
local $ENV{PERL_MM_USE_DEFAULT} = 1;
my $short_name = _short_name( $dist );
for my $tool ( qw/eumm mb/ ) {
SKIP: {
my ($have_tool,$tool_mod,$tool_PL) =
@{$tool_constants{$tool}}{qw/have module PL/};
my $tool_cmd = $tool eq 'eumm' ? "$make test"
: "$perl Build test" ;
my $tool_label = $tool eq 'eumm'? "make test"
: "perl Build test" ;
skip "$tool_mod not installed or working", test_grade_test_iter_plan()
if ! $have_tool;
my $tempd = _ok_clone_dist_dir( $case->{name} );
$Helper::sent_report = undef;
$Helper::comments = undef;
my ($stdout, $stderr, $build_err, $test_build_rc);
($stdout, $stderr) = capture {
$build_err = system("$perl $tool_PL");
};
ok( ! $build_err, "$case->{name}: $tool_PL successful" )
or do {
_diag_output( $stdout, $stderr );
skip "$tool_PL failed", test_grade_test_iter_plan() - 1;
};
eval {
($stdout, $stderr) = capture {
$test_build_rc = CPAN::Reporter::test( $dist, $tool_cmd )
};
};
if ( $@ ) {
diag "DIED WITH:\n$@";
_diag_output( $stdout, $stderr );
skip "test() died", test_grade_test_iter_plan() - 1;
}
my $is_rc_correct = $case->{"$tool\_success"}
? $test_build_rc : ! $test_build_rc;
ok( $is_rc_correct,
"$case->{name}: '$tool_label' returned " .
$case->{"$tool\_success"}
);
# Grade evaluation with special case if discarding
my ($found_grade_result, $found_msg) =
( $stdout =~ /^CPAN::Reporter: (Test result[^,]+), ([^\n]+)[.:]$/ims );
if ( $case->{"$tool\_grade"} eq 'discard' ) {
is ($found_grade_result, "test results were not valid",
"$case->{name}: '$tool_label' prerequisites not satisifed"
);
like( $stdout,
"/Test report will not be sent/",
"$case->{name}: discard message correct"
);
ok( ! defined $Helper::sent_report,
"$case->{name}: test results discarded"
);
}
else {
my $case_grade = $case->{"$tool\_grade"};
my ($found_grade) = ( $found_grade_result =~ /Test result is '([^']+)'/ );
is( $found_grade, $case_grade,
"$case->{name}: '$tool_label' grade reported as '$case_grade'"
) or _diag_output( $stdout, $stderr );
like( $stdout, "/preparing a CPAN Testers report for \Q$short_name\E/",
"$case->{name}: report notification correct"
);
if ( -r $config_file ) {
ok( defined $Helper::sent_report && length $Helper::sent_report,
"$case->{name}: test report was mock sent"
);
}
else {
ok( ! defined $Helper::sent_report,
"$case->{name}: test results not sent"
);
}
}
# Grade explanation message
is( $found_msg,
$case->{"$tool\_msg"} ? $case->{"$tool\_msg"} : q{},
"$case->{name}: '$tool_label' grade explanation correct"
);
} #SKIP
} #for
}
#--------------------------------------------------------------------------#
# report tests
#--------------------------------------------------------------------------#
my %report_para = (
'pass' => <<'HERE',
Thank you for uploading your work to CPAN. Congratulations!
All tests were successful.
HERE
'fail' => <<'HERE',
Thank you for uploading your work to CPAN. However, there was a problem
testing your distribution.
If you think this report is invalid, please consult the CPAN Testers Wiki
for suggestions on how to avoid getting FAIL reports for missing library
or binary dependencies, unsupported operating systems, and so on:
http://wiki.cpantesters.org/wiki/CPANAuthorNotes
HERE
'unknown' => << 'HERE',
Thank you for uploading your work to CPAN. However, attempting to
test your distribution gave an inconclusive result.
This could be because your distribution had an error during the make/build
stage, did not define tests, tests could not be found, because your tests were
interrupted before they finished, or because the results of the tests could not
be parsed. You may wish to consult the CPAN Testers Wiki:
http://wiki.cpantesters.org/wiki/CPANAuthorNotes
HERE
'na' => << 'HERE',
Thank you for uploading your work to CPAN. While attempting to build or test
this distribution, the distribution signaled that support is not available
either for this operating system or this version of Perl. Nevertheless, any
diagnostic output produced is provided below for reference. If this is not
what you expect, you may wish to consult the CPAN Testers Wiki:
http://wiki.cpantesters.org/wiki/CPANAuthorNotes
HERE
);
sub test_report_plan() { 17 };
sub test_report {
local $Test::Builder::Level = $Test::Builder::Level + 1;
my ($case) = @_;
my $label = "$case->{label}:";
my $expected_grade = $case->{expected_grade};
my $prereq = CPAN::Reporter::_prereq_report( $case->{dist} );
my $msg_re = $report_para{ $expected_grade };
my $default_comment = $ENV{AUTOMATED_TESTING}
? "this report is from an automated smoke testing program\nand was not reviewed by a human for accuracy"
: "none provided" ;
my $tempd = _ok_clone_dist_dir( $case->{name} );
my $commentfile = File::Spec->catfile( $config_dir, 'comment.txt' );
if ( $case->{comment_txt} ) {
if (! $ENV{AUTOMATED_TESTING} ) {
$default_comment = '';
} else {
$default_comment .= "\n\n";
}
$default_comment .= "TEST COMMENT IN\nCOMMENT FILE";
open my $fh, '>', $commentfile;
print $fh $default_comment;
close $fh;
ok (-e $commentfile, "$label created comment.txt file");
} else {
ok (! -e $commentfile, "$label comment.txt does not exist");
}
my ($stdout, $stderr, $err, $test_output) = _run_report( $case );
if ( $case->{comment_txt} ) {
unlink($commentfile);
}
ok (! -e $commentfile, "$label comment.txt removed if needed");
is( $err, q{},
"$label report ran without error"
);
ok( defined $msg_re && length $msg_re,
"$label found '$expected_grade' grade paragraph"
);
# set PERL_MM_USE_DEFAULT to mirror _run_report
local $ENV{PERL_MM_USE_DEFAULT} = 1;
my $env_vars = CPAN::Reporter::_env_report();
my $special_vars = CPAN::Reporter::_special_vars_report();
my $toolchain_versions = CPAN::Reporter::_toolchain_report();
like( $Helper::sent_report, '/' . quotemeta($msg_re) . '/ms',
"$label correct intro paragraph"
);
like( $Helper::sent_report, '/' . quotemeta($default_comment) . '/ms',
"$label correct default comment"
);
# Because we append comment.txt to the smoke test message when it is
# tested with automation, we want to make sure this message is NOT
# included when we aren't doing automated testing.
if ( $ENV{AUTOMATED_TESTING} ) {
like( $Helper::sent_report, '/automated smoke testing/ms',
"$label comment includes smoke testing notice"
);
} else {
unlike( $Helper::sent_report, '/automated smoke testing/ms',
"$label comment does not include smoke testing notice"
);
}
# Because we don't append the standard default message if we have a
# comment.txt, we want to make sure it is not there.
if ( $ENV{AUTOMATED_TESTING} ) {
unlike( $Helper::sent_report, '/none provided/ms',
"$label comment does not include 'none provided'"
);
} else {
if ( $case->{comment_txt} ) {
unlike( $Helper::sent_report, '/none provided/ms',
"$label comment does not include 'none provided'"
);
} else {
like( $Helper::sent_report, '/none provided/ms',
"$label comment does include 'none provided'"
);
}
}
like( $Helper::sent_report, '/' . quotemeta($prereq) . '/ms',
"$label found prereq report"
);
like( $Helper::sent_report, '/' . quotemeta($env_vars) . '/ms',
"$label found environment variables"
);
like( $Helper::sent_report, '/' . quotemeta($special_vars) . '/ms',
"$label found special variables"
);
like( $Helper::sent_report, '/' . quotemeta($toolchain_versions) . '/ms',
"$label found toolchain versions found"
);
my $joined_output = join("", @$test_output);
# extract just the test output
my $found_test_output = q{};
if ( $Helper::sent_report =~ m/
^Output\ from\ '[^\']+':\n # lead-in to test output
^\n # blank line
^(.+) \n # test output ending in newline
^------------------------------ \n # separator
^PREREQUISITES \n # next section
/xms )
{
$found_test_output = $1;
}
my $orig_found_length = length $found_test_output;
ok( $orig_found_length, "$label located test output in report" );
# if output appears longer than max, the excess should only be the
# error message, so look for it, strip it and check it
my $length_error = q{};
my $max_in_k = int($max_report_length / 1000) . "K";
if ( $found_test_output =~ m/
^(.+)\n # test output ending in a newline
^\n # blank line
^(\[[^\n]+\]) \n # stuff in brackets
^\n # blank line
\z
/xms
) {
$found_test_output = $1;
$length_error = $2;
}
if ( length $joined_output > $max_report_length ) {
is( $length_error, "[Output truncated after $max_in_k]",
"$label truncated length error message correct"
)
}
else {
pass( "$label no truncation message seen" );
}
# after extracting error, if any, the output should now be
# less than the max length
my $found_length = length $found_test_output;
ok( $found_length <= $max_report_length,
"$label test_output less than or equal to maximum length"
) or diag "length $found_length > $max_report_length";
# confirm that we indeed got the test output we expected
# (whether all or just a truncated portion)
if ( length $joined_output > $max_report_length ) {
$joined_output = substr( $joined_output, 0, $max_report_length );
}
like( $Helper::sent_report, '/' . quotemeta($joined_output) . '/ms',
"$label found output matches expected output"
);
return ($stdout, $stderr, $err, $test_output);
};
#--------------------------------------------------------------------------#
# test_dispatch
#
# case requires
# label -- prefix for text output
# dist -- mock dist object
# name -- name for t/dist/name to copy
# command -- command to execute within copy of dist dir
# phase -- phase of PL/make/test to pass command results to
#--------------------------------------------------------------------------#
sub test_dispatch_plan { 4 };
sub test_dispatch {
local $Test::Builder::Level = $Test::Builder::Level + 1;
my $case = shift;
my %opt = @_;
my $tempd = _ok_clone_dist_dir( $case->{name} );
local $case->{dist}{build_dir} = "$tempd";
my ($stdout, $stderr, $err) = _run_report( $case );
is( $err, q{},
"generate report for $case->{label}"
);
if ( $opt{will_send} ) {
ok( defined $Helper::sent_report && length $Helper::sent_report,
"report was sent for $case->{label}"
);
like( $stdout, "/sending test report with/",
"saw report sent message for $case->{label}"
);
}
else {
ok( ! defined $Helper::sent_report,
"report not sent for $case->{label}"
);
like( $stdout, "/report will not be sent/",
"saw report not sent message for $case->{label}"
);
}
return ($stdout, $stderr, $err);
}
#--------------------------------------------------------------------------#
# _diag_output
#--------------------------------------------------------------------------#
sub _diag_output {
my ( $stdout, $stderr ) = @_;
diag "STDOUT:\n$stdout\n\nSTDERR:\n$stderr\n";
}
#--------------------------------------------------------------------------#
# _dircopy
#--------------------------------------------------------------------------#
sub _dircopy {
my ($orig, $new) = @_;
my $count = 0;
File::Find::find({
no_chdir => 1,
wanted => sub {
my $orig_path = $_;
my $new_path = File::Spec->rel2abs(File::Spec->abs2rel($orig_path, $orig), $new);
if (-d $orig_path) {
mkdir $new_path;
}
elsif (-f _) {
copy($orig_path, $new_path) or die $!;
}
$count++;
},
}, $orig);
return $count;
}
#--------------------------------------------------------------------------#
# _ok_clone_dist_dir
#--------------------------------------------------------------------------#
sub _ok_clone_dist_dir {
local $Test::Builder::Level = $Test::Builder::Level + 1;
my $dist_name = shift;
my $dist_dir = File::Spec->rel2abs(
File::Spec->catdir( $corpus_dir, $dist_name )
);
my $work_dir = tempd()
or die "Couldn't create temporary distribution dir: $!\n";
ok( defined( _dircopy($dist_dir, "$work_dir") ),
"Copying $dist_name to temp directory $work_dir"
) or diag $!;
return $work_dir;
}
#--------------------------------------------------------------------------#
# _run_report
#--------------------------------------------------------------------------#
sub _run_report {
my $case = shift;
my $phase = $case->{phase};
# automate CPAN::Reporter prompting
local $ENV{PERL_MM_USE_DEFAULT} = 1;
my ($stdout, $stderr, $output, $exit_value);
$Helper::sent_report = undef;
$Helper::comments = undef;
eval {
($stdout, $stderr) = capture {
# run any preliminaries to the command we want to record
if ( $phase eq 'make' || $phase eq 'test' ) {
system("$perl Makefile.PL");
}
if ( $phase eq 'test' ) {
system("$make");
}
($output, $exit_value) =
CPAN::Reporter::record_command( $case->{command} );
no strict 'refs';
&{"CPAN::Reporter::grade_$phase"}(
$case->{dist},
$case->{command},
$output,
$exit_value,
);
};
};
if ( $@ ) {
diag "DIED WITH:\n$@";
_diag_output( $stdout, $stderr );
}
return ($stdout, $stderr, $@, $output);
}
#--------------------------------------------------------------------------#
# _short_name
#--------------------------------------------------------------------------#
sub _short_name {
my $dist = shift;
my $short_name = basename($dist->pretty_id);
$short_name =~ s/(\.tar\.gz|\.tgz|\.zip)$//i;
return $short_name;
}
#--------------------------------------------------------------------------#
# Mocking
#--------------------------------------------------------------------------#
BEGIN {
$INC{"Test/Reporter.pm"} = 1; # fake load
}
package Test::Reporter;
use vars qw/$AUTOLOAD $VERSION/;
$VERSION = 999; # more than 1.54 (e.g. distfile exists)
sub new { return bless {}, 'Test::Reporter::Mocked' }
package Test::Reporter::Mocked;
use Config;
use vars qw/$AUTOLOAD/;
sub comments { shift; $Helper::comments = shift }
sub send {
shift;
$Helper::sent_report = $Helper::comments;
return 1;
}
sub subject {
my $self = shift;
return uc($self->grade) . ' ' . $self->distribution .
" $Config{archname} $Config{osvers}";
}
my %mocked_data;
my @valid_transport = qw/Metabase Mail::Send/;
sub transport {
my ($self) = shift;
if (@_) {
my $t = shift;
die __PACKAGE__ . ":transport: '$t' is invalid\n"
unless grep { $t eq $_ } @valid_transport;
$mocked_data{transport} = $t;
}
return $mocked_data{transport};
}
sub AUTOLOAD {
my $self = shift;
if ( @_ ) {
$mocked_data{ $AUTOLOAD } = shift;
}
return $mocked_data{ $AUTOLOAD };
}
1;
|