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'" ); }