App-Spec

 view release on metacpan or  search on metacpan

lib/App/Spec/Run.pm  view on Meta::CPAN

    global_options => 1,
);

sub process {
    my ($self) = @_;

    my $plugins = $self->spec->plugins || [];
    for my $plugin (@$plugins) {
        $plugin->init_run($self);
    }
    my @callbacks;
    my $subscriber_events = $self->subscribers;
    for my $key (qw/ global_options print_output /) {
        my $subscribers = $subscriber_events->{ $key };
        for my $sub (@$subscribers) {
            my $plugin = $sub->{plugin};
            my $method = $sub->{method};
            my $callback = sub {
                $plugin->$method( run => $self, @_);
            };
            push @callbacks, $callback;
        }
        $self->response->add_callbacks($key => \@callbacks);
    }

    my $argv = $self->argv;
    unless ($argv) {
        $argv = \@ARGV;
        $self->argv($argv);
        $self->argv_orig([ @$argv ]);
    }

    my $completion_parameter = $ENV{PERL5_APPSPECRUN_COMPLETION_PARAMETER};

lib/App/Spec/Run/Response.pm  view on Meta::CPAN

use App::Spec::Run::Output;
use Scalar::Util qw/ blessed /;

use Moo;

has exit => ( is => 'rw', default => 0 );
has outputs => ( is => 'rw', default => sub { [] } );
has finished => ( is => 'rw' );
has halted => ( is => 'rw' );
has buffered => ( is => 'rw', default => 0 );
has callbacks => ( is => 'rw', default => sub { +{} } );

sub add_output {
    my ($self, @out) = @_;

    for my $out (@out) {
        unless (blessed $out) {
            $out = App::Spec::Run::Output->new(
                content => $out,
                ref $out ? (type => 'data') : (),
            );

lib/App/Spec/Run/Response.pm  view on Meta::CPAN

    else {
        $self->print_output(@out);
    }
}

sub print_output {
    my ($self, @out) = @_;
    my $outputs = $self->outputs;
    push @$outputs, @out;

    my $callbacks = $self->callbacks->{print_output} || [];
    for my $cb (@$callbacks) {
        $cb->();
    }

    while (my $out = shift @$outputs) {
        my $content = $out->content;
        if (ref $content) {
            require Data::Dumper;
            $content = Data::Dumper->Dump([$content], ['output']);
        }
        if ($out->error) {
            print STDERR $content;
        }
        else {
            print $content;
        }
    }
}

sub add_callbacks {
    my ($self, $event, $cb_add) = @_;
    my $callbacks = $self->callbacks;
    my $cb = $callbacks->{ $event } ||= [];
    push @$cb, @$cb_add;
}

1;

__END__

=pod

=head1 NAME

lib/App/Spec/Run/Response.pm  view on Meta::CPAN

        content => "string\n",
    );
    $res->add_error($output);

=item print_output

    $res->print_output(@out);

Prints the given output and all output in C<outputs>.

=item add_callbacks

    $response->add_callbacks(print_output => \@callbacks);

Where C<@callbacks> are coderefs.

=back

=head1 ATTRIBUTES

=over 4

=item buffered

If true, output should be buffered until print_output is called.

lib/App/Spec/Run/Response.pm  view on Meta::CPAN

Holds an array of L<App::Spec::Run::Output> objects.

=item finished

Set to 1 after print_output has been called.

=item halted

If set to 1, no further processing should be done.

=item callbacks

Contains a hashref of callbacks

    {
        print_output => $coderef,
    },

=back

=cut

t/13.argv.t  view on Meta::CPAN

        $runner1->process;
    };

    my $runner2 = $spec->runner(
        argv => [@args],
    );
    $runner2->process;

    my $res1 = $runner1->response;
    my $res2 = $runner2->response;
    # we don't care about the callbacks here
    $_->callbacks([]) for ($res1, $res2);
    cmp_deeply(
        $res1,
        $res2,
        "response is the same for default and custom \@ARGV",
        );

    cmp_deeply(
        $runner1->argv_orig,
        [@args],
        "argv_orig() correct",



( run in 0.410 second using v1.01-cache-2.11-cpan-9b1e4054eb1 )