Beefy Boxes and Bandwidth Generously Provided by pair Networks
more useful options
 
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
Node Status?
node history
Node Type: note [id://216534]
help
Chatterbox?
and the web crawler heard nothing...

How do I use this? | Other CB clients
Other Users?
Others meditating upon the Monastery: (9)
As of 2020-04-01 09:01 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?
    To "Disagree to disagree" means to:









    Results (186 votes). Check out past polls.

    Notices?