<html><head><meta name="color-scheme" content="light dark"></head><body><pre style="word-wrap: break-word; white-space: pre-wrap;">package CPANPLUS::Internals::Report;
use deprecate;

use strict;

use CPANPLUS::Error;
use CPANPLUS::Internals::Constants;
use CPANPLUS::Internals::Constants::Report;

use Data::Dumper;

use Params::Check               qw[check];
use Module::Load::Conditional   qw[can_load];
use Locale::Maketext::Simple    Class =&gt; 'CPANPLUS', Style =&gt; 'gettext';
use version;

use vars qw[$VERSION];
$VERSION = "0.9135";

$Params::Check::VERBOSE = 1;

### for the version ###
require CPANPLUS::Internals;

=head1 NAME

CPANPLUS::Internals::Report - internals for sending test reports

=head1 SYNOPSIS

  ### enable test reporting
  $cb-&gt;configure_object-&gt;set_conf( cpantest =&gt; 1 );

  ### set custom mx host, shouldn't normally be needed
  $cb-&gt;configure_object-&gt;set_conf( cpantest_mx =&gt; 'smtp.example.com' );

=head1 DESCRIPTION

This module provides all the functionality to send test reports to
C&lt;http://testers.cpan.org&gt; using the C&lt;Test::Reporter&gt; module.

All methods will be called automatically if you have C&lt;CPANPLUS&gt;
configured to enable test reporting (see the C&lt;SYNOPSIS&gt;).

=head1 METHODS

=head2 $bool = $cb-&gt;_have_query_report_modules

This function checks if all the required modules are here for querying
reports. It returns true and loads them if they are, or returns false
otherwise.

=head2 $bool = $cb-&gt;_have_send_report_modules

This function checks if all the required modules are here for sending
reports. It returns true and loads them if they are, or returns false
otherwise.

=cut

### XXX remove this list and move it into selfupdate, somehow..
### this is dual administration
{   my $query_list = {
        'File::Fetch'          =&gt; '0.13_02',
        'Parse::CPAN::Meta'    =&gt; '0.0',
        'File::Temp'           =&gt; '0.0',
    };

    my $send_list = {
        %$query_list,
        'Test::Reporter' =&gt; '1.54',
    };

    sub _have_query_report_modules {
        my $self = shift;
        my $conf = $self-&gt;configure_object;
        my %hash = @_;

        my $tmpl = {
            verbose =&gt; { default =&gt; $conf-&gt;get_conf('verbose') },
        };

        my $args = check( $tmpl, \%hash ) or return;

        return can_load( modules =&gt; $query_list, verbose =&gt; $args-&gt;{verbose} )
                ? 1
                : 0;
    }

    sub _have_send_report_modules {
        my $self = shift;
        my $conf = $self-&gt;configure_object;
        my %hash = @_;

        my $tmpl = {
            verbose =&gt; { default =&gt; $conf-&gt;get_conf('verbose') },
        };

        my $args = check( $tmpl, \%hash ) or return;

        return can_load( modules =&gt; $send_list, verbose =&gt; $args-&gt;{verbose} )
                ? 1
                : 0;
    }
}

=head2 @list = $cb-&gt;_query_report( module =&gt; $modobj, [all_versions =&gt; BOOL, verbose =&gt; BOOL] )

This function queries the CPAN testers database at
I&lt;http://testers.cpan.org/&gt; for test results of specified module objects,
module names or distributions.

The optional argument C&lt;all_versions&gt; controls whether all versions of
a given distribution should be grabbed.  It defaults to false
(fetching only reports for the current version).

Returns the a list with the following data structures (for CPANPLUS
version 0.042) on success, or false on failure. The contents of the
data structure depends on what I&lt;http://testers.cpan.org&gt; returns,
but generally looks like this:

          {
            'grade' =&gt; 'PASS',
            'dist' =&gt; 'CPANPLUS-0.042',
            'platform' =&gt; 'i686-pld-linux-thread-multi'
            'details' =&gt; 'http://nntp.x.perl.org/group/perl.cpan.testers/98316'
            ...
          },
          {
            'grade' =&gt; 'PASS',
            'dist' =&gt; 'CPANPLUS-0.042',
            'platform' =&gt; 'i686-linux-thread-multi'
            'details' =&gt; 'http://nntp.x.perl.org/group/perl.cpan.testers/99416'
            ...
          },
          {
            'grade' =&gt; 'FAIL',
            'dist' =&gt; 'CPANPLUS-0.042',
            'platform' =&gt; 'cygwin-multi-64int',
            'details' =&gt; 'http://nntp.x.perl.org/group/perl.cpan.testers/99371'
            ...
          },
          {
            'grade' =&gt; 'FAIL',
            'dist' =&gt; 'CPANPLUS-0.042',
            'platform' =&gt; 'i586-linux',
            'details' =&gt; 'http://nntp.x.perl.org/group/perl.cpan.testers/99396'
            ...
          },

The status of the test can be one of the following:
UNKNOWN, PASS, FAIL or NA (not applicable).

=cut

sub _query_report {
    my $self = shift;
    my $conf = $self-&gt;configure_object;
    my %hash = @_;

    my($mod, $verbose, $all);
    my $tmpl = {
        module          =&gt; { required =&gt; 1, allow =&gt; IS_MODOBJ,
                                store =&gt; \$mod },
        verbose         =&gt; { default =&gt; $conf-&gt;get_conf('verbose'),
                                store =&gt; \$verbose },
        all_versions    =&gt; { default =&gt; 0, store =&gt; \$all },
    };

    check( $tmpl, \%hash ) or return;

    ### check if we have the modules we need for querying
    return unless $self-&gt;_have_query_report_modules( verbose =&gt; 1 );


    ### XXX no longer use LWP here. However, that means we don't
    ### automagically set proxies anymore!!!
    # my $ua = LWP::UserAgent-&gt;new;
    # $ua-&gt;agent( CPANPLUS_UA-&gt;() );
    #
    ### set proxies if we have them ###
    # $ua-&gt;env_proxy();

    my $url = TESTERS_URL-&gt;($mod-&gt;package_name);
    my $ff  = File::Fetch-&gt;new( uri =&gt; $url );

    msg( loc("Fetching: '%1'", $url), $verbose );

    my $res = do {
        my $tempdir = File::Temp::tempdir();
        my $where   = $ff-&gt;fetch( to =&gt; $tempdir );

        unless( $where ) {
            error( loc( "Fetching report for '%1' failed: %2",
                        $url, $ff-&gt;error ) );
            return;
        }

        my $fh = OPEN_FILE-&gt;( $where );

        do { local $/; &lt;$fh&gt; };
    };

    my ($aref) = eval { Parse::CPAN::Meta::Load( $res ) };

    if( $@ ) {
        error(loc("Error reading result: %1", $@));
        return;
    };

    my $dist    = $mod-&gt;package_name .'-'. $mod-&gt;package_version;
    my $details = TESTERS_DETAILS_URL-&gt;($mod-&gt;package_name);

    my @rv;
    for my $href ( @$aref ) {
        next unless $all or defined $href-&gt;{'distversion'} &amp;&amp;
                            $href-&gt;{'distversion'} eq $dist;

        $href-&gt;{'details'}  = $details;

        ### backwards compatibility :(
        $href-&gt;{'dist'}     ||= $href-&gt;{'distversion'};
        $href-&gt;{'grade'}    ||= $href-&gt;{'action'} || $href-&gt;{'status'};

        push @rv, $href;
    }

    return @rv if @rv;
    return;
}

=pod

=head2 $bool = $cb-&gt;_send_report( module =&gt; $modobj, buffer =&gt; $make_output, failed =&gt; BOOL, [save =&gt; BOOL, address =&gt; $email_to, verbose =&gt; BOOL, force =&gt; BOOL]);

This function sends a testers report to C&lt;cpan-testers@perl.org&gt; for a
particular distribution.
It returns true on success, and false on failure.

It takes the following options:

=over 4

=item module

The module object of this particular distribution

=item buffer

The output buffer from the 'make/make test' process

=item failed

Boolean indicating if the 'make/make test' went wrong

=item save

Boolean indicating if the report should be saved locally instead of
mailed out. If provided, this function will return the location the
report was saved to, rather than a simple boolean 'TRUE'.

Defaults to false.

=item address

The email address to mail the report for. You should never need to
override this, but it might be useful for debugging purposes.

Defaults to C&lt;cpan-testers@perl.org&gt;.

=item verbose

Boolean indicating on whether or not to be verbose.

Defaults to your configuration settings

=item force

Boolean indicating whether to force the sending, even if the max
amount of reports for fails have already been reached, or if you
may already have sent it before.

Defaults to your configuration settings

=back

=cut

sub _send_report {
    my $self = shift;
    my $conf = $self-&gt;configure_object;
    my %hash = @_;

    ### do you even /have/ test::reporter? ###
    unless( $self-&gt;_have_send_report_modules(verbose =&gt; 1) ) {
        error( loc( "You don't have '%1' (or modules required by '%2') ".
                    "installed, you cannot report test results.",
                    'Test::Reporter', 'Test::Reporter' ) );
        return;
    }

    ### check arguments ###
    my ($buffer, $failed, $mod, $verbose, $force, $address, $save,
        $tests_skipped, $status );
    my $tmpl = {
            module  =&gt; { required =&gt; 1, store =&gt; \$mod, allow =&gt; IS_MODOBJ },
            buffer  =&gt; { required =&gt; 1, store =&gt; \$buffer },
            failed  =&gt; { required =&gt; 1, store =&gt; \$failed },
            status  =&gt; { default =&gt; {}, store =&gt; \$status, strict_type =&gt; 1 },
            address =&gt; { default  =&gt; CPAN_TESTERS_EMAIL, store =&gt; \$address },
            save    =&gt; { default  =&gt; 0, store =&gt; \$save },
            verbose =&gt; { default  =&gt; $conf-&gt;get_conf('verbose'),
                            store =&gt; \$verbose },
            force   =&gt; { default  =&gt; $conf-&gt;get_conf('force'),
                            store =&gt; \$force },
            tests_skipped
                    =&gt; { default =&gt; 0, store =&gt; \$tests_skipped },
    };

    check( $tmpl, \%hash ) or return;

    ### get the data to fill the email with ###
    my $name    = $mod-&gt;module;
    my $dist    = $mod-&gt;package_name . '-' . $mod-&gt;package_version;
    my $author  = $mod-&gt;author-&gt;author;
    my $distfile= $mod-&gt;author-&gt;cpanid . "/" . $mod-&gt;package;
    my $email   = $mod-&gt;author-&gt;email || CPAN_MAIL_ACCOUNT-&gt;( $author );
    my $cp_conf = $conf-&gt;get_conf('cpantest') || '';
    my $int_ver = $CPANPLUS::Internals::VERSION;
    my $cb      = $mod-&gt;parent;


    ### will be 'fetch', 'make', 'test', 'install', etc ###
    my $stage   = TEST_FAIL_STAGE-&gt;($buffer);

    ### determine the grade now ###

    my $grade;
    ### check if this is a platform specific module ###
    ### if we failed the test, there may be reasons why
    ### an 'NA' might have to be instead
    GRADE: { if ( $failed ) {


        ### XXX duplicated logic between this block
        ### and REPORTED_LOADED_PREREQS :(

        ### figure out if the prereqs are on CPAN at all
        ### -- if not, send NA grade
        ### Also, if our version of prereqs is too low,
        ### -- send NA grade.
        ### This is to address bug: #25327: do not count
        ### as FAIL modules where prereqs are not filled
        {   my $prq = $mod-&gt;status-&gt;prereqs || {};

            PREREQ: while( my($prq_name,$prq_ver) = each %$prq ) {

                # 'perl' listed as prereq

                if ( $prq_name eq 'perl' ) {
                   my $req_ver = eval { version-&gt;new( $prq_ver ) };
                   next PREREQ unless $req_ver;
                   if ( version-&gt;new( $] ) &lt; $req_ver ) {
                      msg(loc("'%1' requires a higher version of perl than your current ".
                              "version -- sending N/A grade.", $name), $verbose);

                      $grade = GRADE_NA;
                      last GRADE;
                   }
                   next PREREQ;
                }

                my $obj = $cb-&gt;module_tree( $prq_name );
                my $sub = CPANPLUS::Module-&gt;can(
                            'module_is_supplied_with_perl_core' );

                ### if we can't find the module and it's not supplied with core.
                ### this addresses: #32064: NA reports generated for failing
                ### tests where core prereqs are specified
                ### Note that due to a bug in Module::CoreList, in some released
                ### version of perl (5.8.6+ and 5.9.2-4 at the time of writing)
                ### 'Config' is not recognized as a core module. See this bug:
                ###    http://rt.cpan.org/Ticket/Display.html?id=32155
                if( !$obj and !defined $sub-&gt;( $prq_name ) ) {
                    msg(loc( "Prerequisite '%1' for '%2' could not be obtained".
                             " from CPAN -- sending N/A grade",
                             $prq_name, $name ), $verbose );

                    $grade = GRADE_NA;
                    last GRADE;
                }

                if ( !$obj ) {
                    my $vcore = $sub-&gt;( $prq_name );
                    if ( $cb-&gt;_vcmp( $prq_ver, $vcore ) &gt; 0 ) {
                      msg(loc( "Version of core module '%1' ('%2') is too low for ".
                               "'%3' (needs '%4') -- sending N/A grade",
                               $prq_name, $vcore,
                               $name, $prq_ver ), $verbose );

                      $grade = GRADE_NA;
                      last GRADE;
                    }
                }

                if( $obj and $cb-&gt;_vcmp( $prq_ver, $obj-&gt;installed_version ) &gt; 0 ) {
                    msg(loc( "Installed version of '%1' ('%2') is too low for ".
                             "'%3' (needs '%4') -- sending N/A grade",
                             $prq_name, $obj-&gt;installed_version,
                             $name, $prq_ver ), $verbose );

                    $grade = GRADE_NA;
                    last GRADE;
                }
            }
        }

        unless( RELEVANT_TEST_RESULT-&gt;($mod) ) {
            msg(loc(
                "'%1' is a platform specific module, and the test results on".
                " your platform are not relevant --sending N/A grade.",
                $name), $verbose);

            $grade = GRADE_NA;

        } elsif ( UNSUPPORTED_OS-&gt;( $buffer ) ) {
            msg(loc(
                "'%1' is a platform specific module, and the test results on".
                " your platform are not relevant --sending N/A grade.",
                $name), $verbose);

            $grade = GRADE_NA;

        ### you dont have a high enough perl version?
        } elsif ( PERL_VERSION_TOO_LOW-&gt;( $buffer ) ) {
            msg(loc("'%1' requires a higher version of perl than your current ".
                    "version -- sending N/A grade.", $name), $verbose);

            $grade = GRADE_NA;

        ### perhaps where were no tests...
        ### see if the thing even had tests ###
        } elsif ( NO_TESTS_DEFINED-&gt;( $buffer ) ) {
            $grade = GRADE_UNKNOWN;
        ### failures in PL or make/build stage are now considered UNKNOWN
        } elsif ( $stage !~ /\btest\b/ ) {

            $grade = GRADE_UNKNOWN

        } else {

            $grade = GRADE_FAIL;
        }

    ### if we got here, it didn't fail and tests were present.. so a PASS
    ### is in order
    } else {
        $grade = GRADE_PASS;
    } }

    ### so an error occurred, let's see what stage it went wrong in ###

    ### the header -- always include so the CPANPLUS version is apparent
    my $message =  REPORT_MESSAGE_HEADER-&gt;( $int_ver, $author );

    if( $grade eq GRADE_FAIL or $grade eq GRADE_UNKNOWN) {

        ### return if one or more missing external libraries
        if( my @missing = MISSING_EXTLIBS_LIST-&gt;($buffer) ) {
            msg(loc("Not sending test report - " .
                    "external libraries not pre-installed"));
            return 1;
        }

        ### return if we're only supposed to report make_test failures ###
        return 1 if $cp_conf =~  /\bmaketest_only\b/i
                    and ($stage !~ /\btest\b/);

        my $capture = ( $status &amp;&amp; defined $status-&gt;{capture} ? $status-&gt;{capture} : $buffer );
        ### the bit where we inform what went wrong
        $message .= REPORT_MESSAGE_FAIL_HEADER-&gt;( $stage, $capture );

        ### was it missing prereqs? ###
        if( my @missing = MISSING_PREREQS_LIST-&gt;($buffer) ) {
            if(!$self-&gt;_verify_missing_prereqs(
                                module  =&gt; $mod,
                                missing =&gt; \@missing
                        )) {
                msg(loc("Not sending test report - "  .
                        "bogus missing prerequisites report"));
                return 1;
            }
            $message .= REPORT_MISSING_PREREQS-&gt;($author,$email,@missing);
        }

        ### was it missing test files? ###
        if( NO_TESTS_DEFINED-&gt;($buffer) ) {
            $message .= REPORT_MISSING_TESTS-&gt;();
        }

        ### add a list of what modules have been loaded of your prereqs list
        $message .= REPORT_LOADED_PREREQS-&gt;($mod);

        ### add a list of versions of toolchain modules
        $message .= REPORT_TOOLCHAIN_VERSIONS-&gt;($mod);

        ### the footer
        $message .= REPORT_MESSAGE_FOOTER-&gt;();

    ### it may be another grade than fail/unknown.. may be worth noting
    ### that tests got skipped, since the buffer is not added in
    } elsif ( $tests_skipped ) {
        $message .= REPORT_TESTS_SKIPPED-&gt;();
    } elsif( $grade eq GRADE_NA) {

        my $capture = ( $status &amp;&amp; defined $status-&gt;{capture} ? $status-&gt;{capture} : $buffer );

        ### add the reason for the NA to the buffer
        $capture = join $/, $capture, map {
                        '[' . $_-&gt;tag . '] [' . $_-&gt;when . '] ' .
                        $_-&gt;message } ( CPANPLUS::Error-&gt;stack )[-1];

        ### the bit where we inform what went wrong
        $message .= REPORT_MESSAGE_FAIL_HEADER-&gt;( $stage, $capture );

        ### add a list of what modules have been loaded of your prereqs list
        $message .= REPORT_LOADED_PREREQS-&gt;($mod);

        ### add a list of versions of toolchain modules
        $message .= REPORT_TOOLCHAIN_VERSIONS-&gt;($mod);

        ### the footer
        $message .= REPORT_MESSAGE_FOOTER-&gt;();

    } elsif ( $grade eq GRADE_PASS and ( $status and defined $status-&gt;{capture} ) ) {
        ### the bit where we inform what went right
        $message .= REPORT_MESSAGE_PASS_HEADER-&gt;( $stage, $status-&gt;{capture} );

        ### add a list of what modules have been loaded of your prereqs list
        $message .= REPORT_LOADED_PREREQS-&gt;($mod);

        ### add a list of versions of toolchain modules
        $message .= REPORT_TOOLCHAIN_VERSIONS-&gt;($mod);

        ### the footer
        $message .= REPORT_MESSAGE_FOOTER-&gt;();

    }

    msg( loc("Sending test report for '%1'", $dist), $verbose);

    ### reporter object ###
    my $reporter = do {
        my $args = $conf-&gt;get_conf('cpantest_reporter_args') || {};

        unless( UNIVERSAL::isa( $args, 'HASH' ) ) {
            error(loc("'%1' must be a hashref, ignoring...",
                      'cpantest_reporter_args'));
            $args = {};
        }

        Test::Reporter-&gt;new(
            grade           =&gt; $grade,
            distribution    =&gt; $dist,
            distfile        =&gt; $distfile,
            via             =&gt; "CPANPLUS $int_ver",
            timeout         =&gt; $conf-&gt;get_conf('timeout') || 60,
            debug           =&gt; $conf-&gt;get_conf('debug'),
            %$args,
        );
    };

    ### set a custom mx, if requested
    $reporter-&gt;mx( [ $conf-&gt;get_conf('cpantest_mx') ] )
        if $conf-&gt;get_conf('cpantest_mx');

    ### set the from address ###
    $reporter-&gt;from( $conf-&gt;get_conf('email') )
        if $conf-&gt;get_conf('email') !~ /\@example\.\w+$/i;

    ### give the user a chance to programatically alter the message
    $message = $self-&gt;_callbacks-&gt;munge_test_report-&gt;($mod, $message, $grade);

    ### add the body if we have any ###
    $reporter-&gt;comments( $message ) if defined $message &amp;&amp; length $message;

    ### do a callback to ask if we should send the report
    unless ($self-&gt;_callbacks-&gt;send_test_report-&gt;($mod, $grade)) {
        msg(loc("Ok, not sending test report"));
        return 1;
    }

    ### do a callback to ask if we should edit the report
    if ($self-&gt;_callbacks-&gt;edit_test_report-&gt;($mod, $grade)) {
        ### test::reporter 1.20 and lower don't have a way to set
        ### the preferred editor with a method call, but it does
        ### respect your env variable, so let's set that.
        local $ENV{VISUAL} = $conf-&gt;get_program('editor')
                                if $conf-&gt;get_program('editor');

        $reporter-&gt;edit_comments;
    }

    ### allow to be overridden, but default to the normal address ###
    $reporter-&gt;address( $address );

    ### should we save it locally? ###
    if( $save ) {
        if( my $file = $reporter-&gt;write() ) {
            msg(loc("Successfully wrote report for '%1' to '%2'",
                    $dist, $file), $verbose);
            return $file;

        } else {
            error(loc("Failed to write report for '%1'", $dist));
            return;
        }

    ### XXX should we do an 'already sent' check? ###
    ### something broke :( ###
    }
    else {
        my $status;
        eval {
            $status = $reporter-&gt;send();
        };
        if ( $@ ) {
           error(loc("Could not send '%1' report for '%2': %3",
                $grade, $dist, $@));
           return;
        }
        if ( $status ) {
           msg(loc("Successfully sent '%1' report for '%2'", $grade, $dist),
              $verbose);
           return 1;
        }
        error(loc("Could not send '%1' report for '%2': %3",
                $grade, $dist, $reporter-&gt;errstr));
        return;
    }
}

sub _verify_missing_prereqs {
    my $self = shift;
    my %hash = @_;

    ### check arguments ###
    my ($mod, $missing);
    my $tmpl = {
            module  =&gt; { required =&gt; 1, store =&gt; \$mod },
            missing =&gt; { required =&gt; 1, store =&gt; \$missing },
    };

    check( $tmpl, \%hash ) or return;


    my %missing = map {$_ =&gt; 1} @$missing;
    my $conf = $self-&gt;configure_object;
    my $extract = $mod-&gt;status-&gt;extract;

    ### Read pre-requisites from Makefile.PL or Build.PL (if there is one),
    ### of the form:
    ###     'PREREQ_PM' =&gt; {
    ###                      'Compress::Zlib'        =&gt; '1.20',
    ###                      'Test::More'            =&gt; 0,
    ###                    },
    ###  Build.PL uses 'requires' instead of 'PREREQ_PM'.

    my @search;
    push @search, ($extract ? MAKEFILE_PL-&gt;( $extract ) : MAKEFILE_PL-&gt;());
    push @search, ($extract ? BUILD_PL-&gt;( $extract ) : BUILD_PL-&gt;());

    for my $file ( @search ) {
        if(-e $file and -r $file) {
            my $slurp = $self-&gt;_get_file_contents(file =&gt; $file);
            my ($prereq) =
                ($slurp =~ /'?(?:PREREQ_PM|requires)'?\s*=&gt;\s*{(.*?)}/s);
            my @prereq =
                ($prereq =~ /'?([\w\:]+)'?\s*=&gt;\s*'?\d[\d\.\-\_]*'?/sg);
            delete $missing{$_} for(@prereq);
        }
    }

    return 1    if(keys %missing);  # There ARE missing prerequisites
    return;                         # All prerequisites accounted for
}

1;


# Local variables:
# c-indentation-style: bsd
# c-basic-offset: 4
# indent-tabs-mode: nil
# End:
# vim: expandtab shiftwidth=4:
</pre></body></html>