Beefy Boxes and Bandwidth Generously Provided by pair Networks
laziness, impatience, and hubris
 
PerlMonks  

Re: Test module to assist sytem()

by axelrose (Scribe)
on Nov 29, 2002 at 14:47 UTC ( #216534=note: print w/replies, xml ) Need Help??


in reply to Test module to assist sytem()

perhaps:
#!/usr/bin/perl -w use strict; use Test::Simple tests => 3; systest( 0, ("who") ); systest( 0, ("who", "am", "i") ); systest( 1, ("bash", "-c", "exit 1") ); sub systest { my $expected = shift; my @cmd = @_; system @cmd; my $sysres = $? >> 8; ok( $sysres == $expected, "'@cmd' returned $sysres" ); }
The disadvantage here is the mixed output from the test script and the system() calls. I cannot simply add ">/dev/null" to @cmd. My real script parameters rely on not using a Shell for system().

Replies are listed 'Best First'.
Re: Test module to assist sytem()
by Anonymous Monk on Nov 29, 2002 at 21:45 UTC

    You want to use Test::Builder->is_num() rather than wrapping Test::Simple's ok(). You get the benefit of is() and failure diagnostics will point to the line where systest() was called, not the line where ok() was used inside systest().

    As for the problem of supressing/trapping the STDOUT/STDERR from system(), just redirect them inside perl. There's several ways to do it. Dup & close to supress. Tie or redirect to a temp file to capture. An exercise which I will leave for the reader. :)

    use Test::Simple tests => 3; use Test::Builder; my $tb = Test::Builder->new; systest( 0, ("who") ); systest( 0, ("who", "am", "i") ); systest( 1, ("bash", "-c", "exit 1") ); sub systest { my $expected = shift; my @cmd = @_; system @cmd; my $sysres = $? >> 8; $tb->is_num( $sysres, $expected, "'@cmd'" ); }

Log In?
Username:
Password:

What's my password?
Create A New User
Domain Nodelet?
Node Status?
node history
Node Type: note [id://216534]
help
Chatterbox?
and the web crawler heard nothing...

How do I use this?Last hourOther CB clients
Other Users?
Others about the Monastery: (9)
As of 2023-12-11 12:11 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?
    What's your preferred 'use VERSION' for new CPAN modules in 2023?











    Results (41 votes). Check out past polls.

    Notices?