Beefy Boxes and Bandwidth Generously Provided by pair Networks
No such thing as a small change

Duplicate Test:: output

by tye (Sage)
on Jun 04, 2008 at 20:49 UTC ( #690242=snippet: print w/replies, xml ) Need Help??

I have some tests that include a lot of 'diagnostic' output so, by default, STDERR gets redirected to a file so I can easily go look at the details if there is a failure.

The "ok" vs "not ok" messages all need to go to STDOUT which either goes to my interactive session or to a test harness. But they would be extremely useful on STDERR to provide milestones in the detailed output. So I went looking for how to get them written to both STDOUT and STDERR.

I often find it at least mildly entertaining the rather convoluted OO-koolaidesque framework that most of the Test:: modules are now part of. I suspect that there is some way that I can define a subclass and convince Test::More to use it such that I can have "ok" message go to two different file handles. However, that wasn't obvious while the following hack was obvious to me.

Given that the functionality I wanted to replace was named "_print" (note the leading underscore), I also figured that overriding it in a subclass isn't officially sanctioned anyway. (Seems like something not unreasonable to want to override, tho.)

Replies noting the "proper" way to do something like this welcome. :)

if( (stat \*STDOUT)[1] != (stat \*STDERR)[1] ) {
    # Send "ok" messages to both STDOUT and STDERR if they are differe
    my $print= \&Test::Builder::_print;
    *Test::Builder::_print= sub {
        my( $self )= shift @_;
        $self->output( \*STDOUT );
        $self->$print( @_ );
        $self->output( \*STDERR );
        $self->$print( @_ );
        $self->output( \*STDOUT );
Replies are listed 'Best First'.
Re: Duplicate Test:: output
by chromatic (Archbishop) on Jun 05, 2008 at 06:07 UTC
    Replies noting the "proper" way to do something like this welcome.

    If this were my project, I'd use TAP::Harness to collect the information and worry about filehandles... but it's not clear if you need pre- or post-processed TAP.

Re: Duplicate Test:: output
by jdporter (Canon) on Jun 05, 2008 at 14:13 UTC
Log In?

What's my password?
Create A New User
Node Status?
node history
Node Type: snippet [id://690242]
and all is quiet...

How do I use this? | Other CB clients
Other Users?
Others contemplating the Monastery: (2)
As of 2018-05-26 10:40 GMT
Find Nodes?
    Voting Booth?