[Catalyst] How to run self-tests from within the application

J. Shirley jshirley at gmail.com
Sat Dec 20 15:11:24 GMT 2008


On Fri, Dec 19, 2008 at 7:30 PM, Ashley <apv at sedition.com> wrote:
> What am I doing wrong?
>
> This code starts to work but hangs (and always times out if a timeout is
> specified).
>
> sub auto :Private {
>    eval { require IPC::Cmd;
>           require File::Find::Rule;
>       };
> }
> sub index :Path :Args(0) {
>    my ( $self, $c ) = @_;
>    my @tests = File::Find::Rule->file()
>                     ->name( '*.t' )
>                     ->in( $c->path_to("t") );
>
>    # Messing with this doesn't seem to help...?
>    local $SIG{CHLD} = "DEFAULT";
>
>    my $prove = IPC::Cmd::can_run("prove");
>
>    # Just run 01app.t in $test[0] for the sake of testing this.
>
>    my ( $success, $error_code, $full_buf, $stdout_buf, $stderr_buf ) =
>        IPC::Cmd::run( command => [ $prove, "-I" . $c->path_to("lib"),
> $tests[0] ],
>                       timeout => 15,
>                       verbose => 0 );
>
>    $c->response->content_type("text/plain");
>    $c->response->body( join"\n", @{$full_buf} );
> #    $c->response->body( join"\n", @tests );
> }
>
>
> After it times out, this appears in the test server output in the terminal:
>
> .../t/01app......ok
> All tests successful.
> Files=1, Tests=2, 23 wallclock secs ( 0.02 usr  0.01 sys +  3.78 cusr  0.57
> csys =  4.38 CPU)
> Result: PASS
>
> Why is it hanging? Is there a pipe or something I could add to the command?
> What are the environmental caveats if I can get this to run under the test
> server v fastcgi/modperl?
>
> Thanks for looking!
>
> -Ashley
>
>

I'm sorry to not really address the original question of executing
another process, but you can easily create a model to run the tests.
The entire code of prove is simply:

use App::Prove;

my $app = App::Prove->new;
$app->process_args(@ARGV);
exit( $app->run ? 0 : 1 );


To do this in a Model class would also be trivial, and you can use
config (something like this, not compiled or checked):

package MyApp::Model::SelfTest;

use base 'Catalyst::Model';

use App::Prove;

__PACKAGE__->config({
    lib => "__path_to(lib)__",
    test_dir => "__path_to(t)__",
    timeout => 15,
    verbose => 0
});

sub self_test {
    my ( $self, $c ) = @_;
    my @args = ();
    foreach my $key ( keys %{ $self->config } ) {
        next if $key eq 'test_dir';
        push @args, "--$key", $self->config->{$key};
    }
    push @args, $self->config->{test_dir};
    my $prove = App::Prove->new;
    $prove->process_args(@args);
    $prove->run; # Handle failure somehow? This will return 1 or 0
}

1;

__END__

Hope something like that helps you!



More information about the Catalyst mailing list