Test2::Tools::Command(3pm) | User Contributed Perl Documentation | Test2::Tools::Command(3pm) |
Test2::Tools::Command - test simple unix commands
use Test2::Tools::Command; # test some typical unix tools; implicit checks are that status # is 0, and that stdout and stderr are the empty string, unless # otherwise specified command { args => [ 'true' ] }; command { args => [ 'false' ], status => 1 }; command { args => [ 'echo', 'foo' ], stdout => "foo\n" }; # subsequent args are prefixed with this local @Test2::Tools::Command::command = ( 'perl', '-E' ); # return values and a variety of the options available my ($result, $exit_status, $stdout_ref, $stderr_ref) = command { args => [ q{say "out";warn "err";kill TERM => $$} ], chdir => '/some/dir', env => { API_KEY => 42 }, stdin => "printed to program\n", stdout => qr/out/, stderr => qr/err/, status => { code => 0, signal => 15, iscore => 0 }, timeout => 7 }; # check on a $? exit status word from somewhere is_exit $?, 42; is_exit $?, { code => 0, signal => 9, iscore => 0 };
This module tests that commands given particular arguments result in particular outputs by way of the exit status word, standard output, and standard error. Various parameters to the command function alter exactly how this is done, in addition to variables that can be set.
The commands are expected to be simple, for example filters that maybe accept standard input and respond with some but not too much output. Interactive or otherwise complicated commands will need some other module such as Expect to test them, as will programs that generate too much output.
Also, is_exit is provided to check on the 16-bit exit status word from other code.
These are not exported.
local @Test2::Tools::Command::command = ($^X, '--', 'bin/foo'); command { args => [ 'bar', '-c', 'baz' ] };
will result in "perl -- bin/foo bar -c baz" being run.
If chdir is used, a command that uses a relative path may need to be fully qualified, e.g. with "rel2abs" of File::Spec::Functions.
command is exported by default; this can be disabled by using this module with an empty import list. The test keys are status, stdout, and stderr. The other keys influence how the command is run or change test metadata.
A command that uses a relative path may need to be fully qualified, e.g. with "rel2abs" of File::Spec::Functions.
status => 42 status => { code => 42, iscore => 0, signal => 0 }
Obviously the 16-bit exit status word is decomposed into a hash reference. If the program is instead expected to exit by a SIGPIPE, one might use:
status => { code => 0, iscore => 0, signal => 13 }
See also munge_signal and munge_status.
command returns a list consisting of the result of the tests, the original 16-bit exit status word, and scalar references to strings that contain the standard output and standard error of the test program, if any.
my ($result, $status, $out_ref, $err_ref) = command { ...
is_exit $?, 42; is_exit $?, { code => 0, signal => 9, iscore => 0 };
None known. There are probably portability problems if you stray from the unix path.
Test2::Suite
Expect may be necessary to test complicated programs.
IPC::Open3 is used to run programs; this may run into portability problems on systems that stray from the way of unix?
Copyright 2022 Jeremy Mates
This program is distributed under the (Revised) BSD License: <https://opensource.org/licenses/BSD-3-Clause>
2023-01-15 | perl v5.36.0 |