Test-Command-0.11/0000755000175000010010000000000012137537222012642 5ustar danbooNoneTest-Command-0.11/Build.PL0000444000175000010010000000137312137537222014140 0ustar danbooNoneuse strict; use warnings; use Module::Build; my $builder = Module::Build->new( module_name => 'Test::Command', license => 'perl', dist_author => 'Daniel B. Boorstein ', dist_version_from => 'lib/Test/Command.pm', build_requires => { 'Test::Simple' => 0.62, }, add_to_cleanup => [ 'Test-Command-*' ], create_makefile_pl => 'traditional', meta_merge => { resources => { repository => 'https://github.com/danboo/perl-test-command', homepage => 'https://metacpan.org/release/Test-Command', bugtracker => 'http://rt.cpan.org/NoAuth/ReportBug.html?Queue=Test-Command', }, }, ); $builder->create_build_script(); Test-Command-0.11/Changes0000444000175000010010000000427712137537222014145 0ustar danbooNoneRevision history for Test-Command 0.11 4/29/2013 - fix determination of diagnostic test name in signal_is_undef() 0.10 2/8/2013 - fix signal tests on Win32 platforms 0.09 2/1/2013 - added exit_value(), signal_value(), stdout_value(), stdout_file(), stderr_value() and stderr_file() (these provide access to raw value for arbitrary testing not covered by this module) 0.08 Wed May 13 23:04 2009 - removed use of POSIX module (need a better understanding of how to use it on BSD systems), reverted back to hand-rolled exit and signal determination - changed signal tests to use Config.pm to determine symbolic signal values 0.07 Mon May 11 23:42 2009 - removed internal requirement that ref $self eq 'Test::Command' to allow subclassing (requested by Lanny Ripple via http://rt.cpan.org/Public/Bug/Display.html?id=43541) 0.06 Mon May 11 23:01 2009 - only use POSIX::W* macros when available - skip signal tests on MSWin32 0.05 Mon May 11 21:32 2009 - use ${^CHILD_ERROR_NATIVE} if available (hopefully will help with BSD test failures) 0.04 Sun May 10 09:38 2009 - exit and signal values can be undef if POSIX::WIFEXITED() or POSIX::WIFSIGNALED return false respectively - added exit_is_defined(), exit_is_undef(), signal_is_defined() and signal_is_undef() 0.03 Sat May 9 23:21 2009 - use POSIX::WEXITSTATUS() to find exit status instead of manual bit shift - added terminating signal handling via POSIX::WTERMSIG() (was mentioned in "DEVELOPMENT IDEAS" but finally spurred on by Lanny Ripple via http://rt.cpan.org/Public/Bug/Display.html?id=43541) - added t/06-signal.t 0.02 Tue Apr 10 21:58 2007 - added 'tests => 11' import to 'use Test::Command' in SYNOPSIS - modified "No such file" tests to only test for $@ after the eval since the regex test wasn't valid for other languages - added development idea about creating a script to generate test files 0.01 Sun Apr 8 15:36 2007 - First release. Test-Command-0.11/lib/0000755000175000010010000000000012137537222013410 5ustar danbooNoneTest-Command-0.11/lib/Test/0000755000175000010010000000000012137537222014327 5ustar danbooNoneTest-Command-0.11/lib/Test/Command.pm0000444000175000010010000006566612137537222016264 0ustar danbooNonepackage Test::Command; use warnings; use strict; use Carp qw/ confess /; use File::Temp qw/ tempfile /; use base 'Test::Builder::Module'; our @EXPORT = qw( exit_value exit_is_num exit_isnt_num exit_cmp_ok exit_is_defined exit_is_undef signal_value signal_is_num signal_isnt_num signal_cmp_ok signal_is_defined signal_is_undef stdout_value stdout_file stdout_is_eq stdout_isnt_eq stdout_is_num stdout_isnt_num stdout_like stdout_unlike stdout_cmp_ok stdout_is_file stderr_value stderr_file stderr_is_eq stderr_isnt_eq stderr_is_num stderr_isnt_num stderr_like stderr_unlike stderr_cmp_ok stderr_is_file ); =head1 NAME Test::Command - Test routines for external commands =head1 VERSION Version 0.11 =cut our $VERSION = '0.11'; =head1 SYNOPSIS Test the exit status, signal, STDOUT or STDERR of an external command. use Test::Command tests => 11; ## testing exit status my $cmd = 'true'; exit_is_num($cmd, 0); exit_cmp_ok($cmd, '<', 10); $cmd = 'false'; exit_isnt_num($cmd, 0); ## testing terminating signal $cmd = 'true'; signal_is_num($cmd, 0); ## testing STDOUT $cmd = [qw/ echo out /]; ## run as "system @$cmd" my $file_exp = 'echo_stdout.exp'; stdout_is_eq($cmd, "out\n"); stdout_isnt_eq($cmd, "out"); stdout_is_file($cmd, $file_exp); ## testing STDERR $cmd = 'echo err >&2'; stderr_like($cmd, /err/); stderr_unlike($cmd, /rre/); stderr_cmp_ok($cmd, 'eq', "err\n"); ## run-once-test-many-OO-style ## the first test lazily runs command ## the second test uses cached results my $echo_test = Test::Command->new( cmd => 'echo out' ); $echo_test->exit_is_num(0); $echo_test->signal_is_num(0); $echo_test->stdout_is_eq("out\n"); ## force a re-run of the command $echo_test->run; ## arbitrary results inspection is( $echo_test->exit_value, 0, 'echo exit' ); is( $echo_test->signal_value, undef, 'echo signal' ); is( $echo_test->stdout_value, "out\n", 'echo stdout' ); is( $echo_test->stderr_value, '', 'echo stderr' ); is( -s $echo_test->stdout_file, 4, 'echo stdout file size' ); is( -s $echo_test->stderr_file, 0, 'echo stderr file size' ); =head1 DESCRIPTION C intends to bridge the gap between the well tested functions and objects you choose and their usage in your programs. By examining the exit status, terminating signal, STDOUT and STDERR of your program you can determine if it is behaving as expected. This includes testing the various combinations and permutations of options and arguments as well as the interactions between the various functions and objects that make up your program. The various test functions below can accept either a command string or an array reference for the first argument. If the command is expressed as a string it is passed to C as is. If the command is expressed as an array reference it is dereferenced and passed to C as a list. See 'C' for how these may differ. The final argument for the test functions, C<$name>, is optional. By default the C<$name> is a concatenation of the test function name, the command string and the expected value. This construction is generally sufficient for identifying a failing test, but you may always specify your own C<$name> if desired. Any of the test functions can be used as instance methods on a C object. This is done by dropping the initial C<$cmd> argument and instead using arrow notation. All of the following C calls are equivalent. exit_is_num('true', 0); exit_is_num('true', 0, 'exit_is_num: true, 0'); exit_is_num(['true'], 0); exit_is_num(['true'], 0, 'exit_is_num: true, 0'); my $cmd = Test::Command->new( cmd => 'true' ); exit_is_num($cmd, 0); exit_is_num($cmd, 0, 'exit_is_num: true, 0'); $cmd->exit_is_num(0); $cmd->exit_is_num(0, 'exit_is_num: true, 0'); $cmd = Test::Command->new( cmd => ['true'] ); exit_is_num($cmd, 0); exit_is_num($cmd, 0, 'exit_is_num: true, 0'); $cmd->exit_is_num(0); $cmd->exit_is_num(0, 'exit_is_num: true, 0'); =head1 EXPORT All of the test functions mentioned below are exported by default. =head1 METHODS =head2 new my $test_cmd_obj = Test::Command->new( cmd => $cmd ) This constructor creates and returns a C object. Use this to test multiple aspects of a single command execution while avoiding repeatedly running commands which are slow or resource intensive. The C parameter can accept either a string or an array reference for its value. The value is dereferenced if necessary and passed directly to the C builtin. =cut sub new { my ($class, @args) = @_; my $self = bless { @args }, $class; return $self; } =head2 run $test_cmd_obj->run; This instance method forces the execution of the command specified by the invocant. You only need to call this when you wish to re-run a command since the first test method invoked will lazily execute the command if necessary. However, if the state of your inputs has changed and you wish to re-run the command, you may do so by invoking this method at any point between your tests. =cut sub run { my ($self) = @_; my $run_info = _run_cmd( $self->{'cmd'} ); $self->{'result'}{'exit_status'} = $run_info->{'exit_status'}; $self->{'result'}{'term_signal'} = $run_info->{'term_signal'}; $self->{'result'}{'stdout_file'} = $run_info->{'stdout_file'}; $self->{'result'}{'stderr_file'} = $run_info->{'stderr_file'}; return $self; } =head1 FUNCTIONS =cut ## private helper functions sub _slurp { my ($file_name) = @_; defined $file_name or confess '$file_name is undefined'; open my $fh, '<', $file_name or confess "$file_name: $!"; my $text = do { local $/ = undef; <$fh> }; close $fh or confess "failed to close $file_name: $!"; return $text; } sub _diff_column { my ($line_1, $line_2) = @_; my $diff_column; my $defined_args = grep defined($_), $line_1, $line_2; if (1 == $defined_args) { $diff_column = 1; } elsif (2 == $defined_args) { my $max_length = ( sort { $b <=> $a } map length($_), $line_1, $line_2 )[0]; for my $position ( 1 .. $max_length ) { my $char_line_1 = substr $line_1, $position - 1, 1; my $char_line_2 = substr $line_2, $position - 1, 1; if ($char_line_1 ne $char_line_2) { $diff_column = $position; last; } } } return $diff_column; } sub _compare_files { my ($got_file, $exp_file) = @_; defined $got_file or confess '$got_file is undefined'; defined $exp_file or confess '$exp_file is undefined'; open my $got_fh, '<', $got_file or confess "$got_file: $!"; open my $exp_fh, '<', $exp_file or confess "$exp_file: $!"; my $ok = 1; my $diff_line; my $diff_column; my $got_line; my $exp_line; my $col_mark; CHECK_LINE: { $got_line = <$got_fh>; $exp_line = <$exp_fh>; last CHECK_LINE if ! defined $got_line && ! defined $exp_line; $diff_line++; $ok = defined $got_line && defined $exp_line && $got_line eq $exp_line; if (! $ok) { $diff_column = _diff_column($got_line, $exp_line); $col_mark = ' ' x ( $diff_column - 1 ); $col_mark .= '^'; last CHECK_LINE; } redo CHECK_LINE; }; close $got_fh or confess "failed to close 'got' handle: $!"; close $exp_fh or confess "failed to close 'exp' handle: $!"; return $ok, $diff_line, $got_line, $exp_line, $col_mark; } sub _build_name { my ($name, $cmd, @args) = @_; if (defined $name) { return $name; } defined $cmd or confess '$cmd is undefined'; if ( ref $cmd && UNIVERSAL::isa($cmd, 'Test::Command') ) { $cmd = $cmd->{'cmd'}; } if (ref $cmd eq 'ARRAY') { $cmd = join ' ', @{ $cmd }; } ## remove any leading package information from the subroutine name (my $test_sub = (caller 1)[3]) =~ s/.*:://; return "$test_sub: " . join ', ', $cmd, @args; } sub _get_result { my ($cmd) = @_; defined $cmd or confess '$cmd is undefined'; if ( ref $cmd && UNIVERSAL::isa($cmd, 'Test::Command') ) { ## run the command if needed if ( ! $cmd->{'result'} ) { $cmd->run; } return $cmd->{'result'}; } else { return _run_cmd(@_); } } sub _run_cmd { my ($cmd) = @_; ## do as much as we can before redirecting STDOUT and STDERR, we want ## to avoid getting our peanut butter in their chocolate defined $cmd or confess '$cmd is undefined'; if ( ! ref $cmd ) { $cmd = [ $cmd ]; } ## save copies of STDOUT and STDERR open my $saved_stdout, '>&STDOUT' or confess 'Cannot duplicate STDOUT'; open my $saved_stderr, '>&STDERR' or confess 'Cannot duplicate STDERR'; ## create tempfiles for capturing STDOUT and STDERR my ($temp_stdout_fh, $temp_stdout_file) = tempfile(UNLINK => 1); my ($temp_stderr_fh, $temp_stderr_file) = tempfile(UNLINK => 1); ## close and reopen STDOUT and STDERR to temp files close STDOUT or confess "failed to close STDOUT: $!"; close STDERR or confess "failed to close STDERR: $!"; open STDOUT, '>&' . fileno $temp_stdout_fh or confess 'Cannot duplicate temporary STDOUT'; open STDERR, '>&' . fileno $temp_stderr_fh or confess 'Cannot duplicate temporary STDERR'; ## run the command system(@{ $cmd }); my $system_return = defined ${^CHILD_ERROR_NATIVE} ? ${^CHILD_ERROR_NATIVE} : $?; my $exit_status; my $term_signal; my $wait_status = $system_return & 127; if ($wait_status) { $exit_status = undef; $term_signal = $wait_status; } else { $exit_status = $system_return >> 8; $term_signal = undef; } ## close and restore STDOUT and STDERR to original handles close STDOUT or confess "failed to close STDOUT: $!"; close STDERR or confess "failed to close STDERR: $!"; open STDOUT, '>&' . fileno $saved_stdout or confess 'Cannot restore STDOUT'; open STDERR, '>&' . fileno $saved_stderr or confess 'Cannot restore STDERR'; return { exit_status => $exit_status, term_signal => $term_signal, stdout_file => $temp_stdout_file, stderr_file => $temp_stderr_file }; } =head2 Testing Exit Status The test routines below compare against the exit status of the executed command right shifted by 8 (that is, C<$? EE 8>). =head3 exit_value exit_value($cmd) Return the exit status of the command. Useful for performing arbitrary tests not covered by this module. =cut sub exit_value { my ($cmd) = @_; my $result = _get_result($cmd); return $result->{'exit_status'}; } =head3 exit_is_num exit_is_num($cmd, $exp_num, $name) If the exit status of the command is numerically equal to the expected number, this passes. Otherwise it fails. =cut sub exit_is_num { my ($cmd, $exp, $name) = @_; my $result = _get_result($cmd); $name = _build_name($name, @_); return __PACKAGE__->builder->is_num($result->{'exit_status'}, $exp, $name); } =head3 exit_isnt_num exit_isnt_num($cmd, $unexp_num, $name) If the exit status of the command is B numerically equal to the given number, this passes. Otherwise it fails. =cut sub exit_isnt_num { my ($cmd, $not_exp, $name) = @_; my $result = _get_result($cmd); $name = _build_name($name, @_); return __PACKAGE__->builder->isnt_num($result->{'exit_status'}, $not_exp, $name); } =head3 exit_cmp_ok exit_cmp_ok($cmd, $op, $operand, $name) If the exit status of the command is compared with the given operand using the given operator, and that operation returns true, this passes. Otherwise it fails. =cut sub exit_cmp_ok { my ($cmd, $op, $exp, $name) = @_; my $result = _get_result($cmd); $name = _build_name($name, @_); return __PACKAGE__->builder->cmp_ok($result->{'exit_status'}, $op, $exp, $name); } =head3 exit_is_defined exit_is_defined($cmd, $name) If the exit status of the command is defined, this passes. Otherwise it fails. A defined exit status indicates that the command exited normally by calling exit() or running off the end of the program. =cut sub exit_is_defined { my ($cmd, $op, $exp, $name) = @_; my $result = _get_result($cmd); $name = _build_name($name, @_); return __PACKAGE__->builder->ok(defined $result->{'exit_status'}, $name); } =head3 exit_is_undef exit_is_undef($cmd, $name) If the exit status of the command is not defined, this passes. Otherwise it fails. An undefined exit status indicates that the command likely exited due to a signal. =cut sub exit_is_undef { my ($cmd, $op, $exp, $name) = @_; my $result = _get_result($cmd); $name = _build_name($name, @_); return __PACKAGE__->builder->ok(! defined $result->{'exit_status'}, $name); } =head2 Testing Terminating Signal The test routines below compare against the lower 8 bits of the exit status of the executed command. =head3 signal_value signal_value($cmd) Return the signal code of the command. Useful for performing arbitrary tests not covered by this module. =cut sub signal_value { my ($cmd) = @_; my $result = _get_result($cmd); return $result->{'term_signal'}; } =head3 signal_is_num signal_is_num($cmd, $exp_num, $name) If the terminating signal of the command is numerically equal to the expected number, this passes. Otherwise it fails. =cut sub signal_is_num { my ($cmd, $exp, $name) = @_; my $result = _get_result($cmd); $name = _build_name($name, @_); return __PACKAGE__->builder->is_num($result->{'term_signal'}, $exp, $name); } =head3 signal_isnt_num signal_isnt_num($cmd, $unexp_num, $name) If the terminating signal of the command is B numerically equal to the given number, this passes. Otherwise it fails. =cut sub signal_isnt_num { my ($cmd, $not_exp, $name) = @_; my $result = _get_result($cmd); $name = _build_name($name, @_); return __PACKAGE__->builder->isnt_num($result->{'term_signal'}, $not_exp, $name); } =head3 signal_cmp_ok signal_cmp_ok($cmd, $op, $operand, $name) If the terminating signal of the command is compared with the given operand using the given operator, and that operation returns true, this passes. Otherwise it fails. =cut sub signal_cmp_ok { my ($cmd, $op, $exp, $name) = @_; my $result = _get_result($cmd); $name = _build_name($name, @_); return __PACKAGE__->builder->cmp_ok($result->{'term_signal'}, $op, $exp, $name); } =head3 signal_is_defined signal_is_defined($cmd, $name) If the terminating signal of the command is defined, this passes. Otherwise it fails. A defined signal indicates that the command likely exited due to a signal. =cut sub signal_is_defined { my ($cmd, $op, $exp, $name) = @_; my $result = _get_result($cmd); $name = _build_name($name, @_); return __PACKAGE__->builder->ok(defined $result->{'term_signal'}, $name); } =head3 signal_is_undef signal_is_undef($cmd, $name) If the terminating signal of the command is not defined, this passes. Otherwise it fails. An undefined signal indicates that the command exited normally by calling exit() or running off the end of the program. =cut sub signal_is_undef { my ($cmd, $name) = @_; my $result = _get_result($cmd); $name = _build_name($name, @_); return __PACKAGE__->builder->ok(! defined $result->{'term_signal'}, $name); } =head2 Testing STDOUT Except where specified, the test routines below treat STDOUT as a single slurped string. =head3 stdout_value stdout_value($cmd) Return the STDOUT of the command. Useful for performing arbitrary tests not covered by this module. =cut sub stdout_value { my ($cmd) = @_; my $result = _get_result($cmd); my $stdout_text = _slurp($result->{'stdout_file'}); return $stdout_text; } =head3 stdout_file stdout_file($cmd) Return the file name containing the STDOUT of the command. Useful for performing arbitrary tests not covered by this module. =cut sub stdout_file { my ($cmd) = @_; my $result = _get_result($cmd); return $result->{'stdout_file'}; } =head3 stdout_is_eq stdout_is_eq($cmd, $exp_string, $name) If the STDOUT of the command is equal (compared using C) to the expected string, then this passes. Otherwise it fails. =cut sub stdout_is_eq { my ($cmd, $exp, $name) = @_; my $result = _get_result($cmd); my $stdout_text = _slurp($result->{'stdout_file'}); $name = _build_name($name, @_); return __PACKAGE__->builder->is_eq($stdout_text, $exp, $name); } =head3 stdout_isnt_eq stdout_isnt_eq($cmd, $unexp_string, $name) If the STDOUT of the command is B equal (compared using C) to the given string, this passes. Otherwise it fails. =cut sub stdout_isnt_eq { my ($cmd, $not_exp, $name) = @_; my $result = _get_result($cmd); my $stdout_text = _slurp($result->{'stdout_file'}); $name = _build_name($name, @_); return __PACKAGE__->builder->isnt_eq($stdout_text, $not_exp, $name); } =head3 stdout_is_num stdout_is_num($cmd, $exp_num, $name) If the STDOUT of the command is equal (compared using C<==>) to the expected number, then this passes. Otherwise it fails. =cut sub stdout_is_num { my ($cmd, $exp, $name) = @_; my $result = _get_result($cmd); my $stdout_text = _slurp($result->{'stdout_file'}); $name = _build_name($name, @_); return __PACKAGE__->builder->is_num($stdout_text, $exp, $name); } =head3 stdout_isnt_num stdout_isnt_num($cmd, $unexp_num, $name) If the STDOUT of the command is B equal (compared using C<==>) to the given number, this passes. Otherwise it fails. =cut sub stdout_isnt_num { my ($cmd, $not_exp, $name) = @_; my $result = _get_result($cmd); my $stdout_text = _slurp($result->{'stdout_file'}); $name = _build_name($name, @_); return __PACKAGE__->builder->isnt_num($stdout_text, $not_exp, $name); } =head3 stdout_like stdout_like($cmd, $exp_regex, $name) If the STDOUT of the command matches the expected regular expression, this passes. Otherwise it fails. =cut sub stdout_like { my ($cmd, $exp, $name) = @_; my $result = _get_result($cmd); my $stdout_text = _slurp($result->{'stdout_file'}); $name = _build_name($name, @_); return __PACKAGE__->builder->like($stdout_text, $exp, $name); } =head3 stdout_unlike stdout_unlike($cmd, $unexp_regex, $name) If the STDOUT of the command does B match the given regular expression, this passes. Otherwise it fails. =cut sub stdout_unlike { my ($cmd, $exp, $name) = @_; my $result = _get_result($cmd); my $stdout_text = _slurp($result->{'stdout_file'}); $name = _build_name($name, @_); return __PACKAGE__->builder->unlike($stdout_text, $exp, $name); } =head3 stdout_cmp_ok stdout_cmp_ok($cmd, $op, $operand, $name) If the STDOUT of the command is compared with the given operand using the given operator, and that operation returns true, this passes. Otherwise it fails. =cut sub stdout_cmp_ok { my ($cmd, $op, $exp, $name) = @_; my $result = _get_result($cmd); my $stdout_text = _slurp($result->{'stdout_file'}); $name = _build_name($name, @_); return __PACKAGE__->builder->cmp_ok($stdout_text, $op, $exp, $name); } =head3 stdout_is_file stdout_is_file($cmd, $exp_file, $name) If the STDOUT of the command is equal (compared using C) to the contents of the given file, then this passes. Otherwise it fails. Note that this comparison is performed line by line, rather than slurping the entire file. =cut sub stdout_is_file { my ($cmd, $exp_file, $name) = @_; my $result = _get_result($cmd); my ($ok, $diff_start, $got_line, $exp_line, $col_mark) = _compare_files($result->{'stdout_file'}, $exp_file); $name = _build_name($name, @_); my $is_ok = __PACKAGE__->builder->ok($ok, $name); if (! $is_ok) { chomp( $got_line, $exp_line ); __PACKAGE__->builder->diag(<{'stderr_file'}); return $stderr_text; } =head3 stderr_file stderr_file($cmd) Return the file name containing the STDERR of the command. Useful for performing arbitrary tests not covered by this module. =cut sub stderr_file { my ($cmd) = @_; my $result = _get_result($cmd); return $result->{'stderr_file'}; } =head3 stderr_is_eq stderr_is_eq($cmd, $exp_string, $name) If the STDERR of the command is equal (compared using C) to the expected string, then this passes. Otherwise it fails. =cut sub stderr_is_eq { my ($cmd, $exp, $name) = @_; my $result = _get_result($cmd); my $stderr_text = _slurp($result->{'stderr_file'}); $name = _build_name($name, @_); return __PACKAGE__->builder->is_eq($stderr_text, $exp, $name); } =head3 stderr_isnt_eq stderr_isnt_eq($cmd, $unexp_string, $name) If the STDERR of the command is B equal (compared using C) to the given string, this passes. Otherwise it fails. =cut sub stderr_isnt_eq { my ($cmd, $not_exp, $name) = @_; my $result = _get_result($cmd); my $stderr_text = _slurp($result->{'stderr_file'}); $name = _build_name($name, @_); return __PACKAGE__->builder->isnt_eq($stderr_text, $not_exp, $name); } =head3 stderr_is_num stderr_is_num($cmd, $exp_num, $name) If the STDERR of the command is equal (compared using C<==>) to the expected number, then this passes. Otherwise it fails. =cut sub stderr_is_num { my ($cmd, $exp, $name) = @_; my $result = _get_result($cmd); my $stderr_text = _slurp($result->{'stderr_file'}); $name = _build_name($name, @_); return __PACKAGE__->builder->is_num($stderr_text, $exp, $name); } =head3 stderr_isnt_num stderr_isnt_num($cmd, $unexp_num, $name) If the STDERR of the command is B equal (compared using C<==>) to the given number, this passes. Otherwise it fails. =cut sub stderr_isnt_num { my ($cmd, $not_exp, $name) = @_; my $result = _get_result($cmd); my $stderr_text = _slurp($result->{'stderr_file'}); $name = _build_name($name, @_); return __PACKAGE__->builder->isnt_num($stderr_text, $not_exp, $name); } =head3 stderr_like stderr_like($cmd, $exp_regex, $name) If the STDERR of the command matches the expected regular expression, this passes. Otherwise it fails. =cut sub stderr_like { my ($cmd, $exp, $name) = @_; my $result = _get_result($cmd); my $stderr_text = _slurp($result->{'stderr_file'}); $name = _build_name($name, @_); return __PACKAGE__->builder->like($stderr_text, $exp, $name); } =head3 stderr_unlike stderr_unlike($cmd, $unexp_regex, $name) If the STDERR of the command does B match the given regular expression, this passes. Otherwise it fails. =cut sub stderr_unlike { my ($cmd, $exp, $name) = @_; my $result = _get_result($cmd); my $stderr_text = _slurp($result->{'stderr_file'}); $name = _build_name($name, @_); return __PACKAGE__->builder->unlike($stderr_text, $exp, $name); } =head3 stderr_cmp_ok stderr_cmp_ok($cmd, $op, $operand, $name) If the STDERR of the command is compared with the given operand using the given operator, and that operation returns true, this passes. Otherwise it fails. =cut sub stderr_cmp_ok { my ($cmd, $op, $exp, $name) = @_; my $result = _get_result($cmd); my $stderr_text = _slurp($result->{'stderr_file'}); $name = _build_name($name, @_); return __PACKAGE__->builder->cmp_ok($stderr_text, $op, $exp, $name); } =head3 stderr_is_file stderr_is_file($cmd, $exp_file, $name) If the STDERR of the command is equal (compared using C) to the contents of the given file, then this passes. Otherwise it fails. Note that this comparison is performed line by line, rather than slurping the entire file. =cut sub stderr_is_file { my ($cmd, $exp_file, $name) = @_; my $result = _get_result($cmd); my ($ok, $diff_start, $got_line, $exp_line, $col_mark) = _compare_files($result->{'stderr_file'}, $exp_file); $name = _build_name($name, @_); my $is_ok = __PACKAGE__->builder->ok($ok, $name); if (! $is_ok) { chomp( $got_line, $exp_line ); __PACKAGE__->builder->diag(< >> =head1 BUGS Please report any bugs or feature requests to C, or through the web interface at L. I will be notified, and then you'll automatically be notified of progress on your bug as I make changes. =head1 SUPPORT You can find documentation for this module with the perldoc command. perldoc Test::Command You can also look for information at: =over 4 =item * AnnoCPAN: Annotated CPAN documentation L =item * CPAN Ratings L =item * RT: CPAN's request tracker L =item * Search CPAN L =back =head1 ACKNOWLEDGEMENTS Test::Builder by Michael Schwern allowed me to focus on the specifics related to testing system commands by making it easy to produce proper test output. =head1 COPYRIGHT & LICENSE Copyright 2007 Daniel B. Boorstein, all rights reserved. This program is free software; you can redistribute it and/or modify it under the same terms as Perl itself. =head1 DEVELOPMENT IDEAS =over 3 =item * create a tool that produces test scripts given a list of commands to run =item * optionally save the temp files with STDOUT and STDERR for user debugging =item * if user defines all options and sample arguments to basic command =over 3 =item * create tool to enumerate all possible means of calling program =item * allow testing with randomized/permuted/collapsed opts and args =back =item * potential test functions: =over 3 =item * time_lt($cmd, $seconds) =item * time_gt($cmd, $seconds) =item * stdout_line_custom($cmd, \&code) =item * stderr_line_custom($cmd, \&code) =back =back =head1 SEE ALSO L provides the testing methods used in this module. L is the superclass of this module. =cut 1; Test-Command-0.11/Makefile.PL0000444000175000010010000000064312137537222014615 0ustar danbooNone# Note: this file was auto-generated by Module::Build::Compat version 0.2808_01 use ExtUtils::MakeMaker; WriteMakefile ( 'PL_FILES' => {}, 'INSTALLDIRS' => 'site', 'NAME' => 'Test::Command', 'EXE_FILES' => [], 'VERSION_FROM' => 'lib/Test/Command.pm', 'PREREQ_PM' => { 'Test::Simple' => '0.62' } ) ; Test-Command-0.11/MANIFEST0000444000175000010010000000034112137537222013767 0ustar danbooNoneBuild.PL Changes MANIFEST META.yml # Will be created by "make dist" Makefile.PL README lib/Test/Command.pm t/00-load.t t/01-funcs.t t/02-exit.t t/03-stdout.t t/04-stderr.t t/05-object.t t/06-signal.t t/pod-coverage.t t/pod.t Test-Command-0.11/META.yml0000444000175000010010000000117112137537222014111 0ustar danbooNone--- name: Test-Command version: 0.11 author: - 'Daniel B. Boorstein ' abstract: Test routines for external commands license: perl resources: bugtracker: http://rt.cpan.org/NoAuth/ReportBug.html?Queue=Test-Command homepage: https://metacpan.org/release/Test-Command license: http://dev.perl.org/licenses/ repository: https://github.com/danboo/perl-test-command build_requires: Test::Simple: 0.62 provides: Test::Command: file: lib/Test/Command.pm version: 0.11 generated_by: Module::Build version 0.280802 meta-spec: url: http://module-build.sourceforge.net/META-spec-v1.2.html version: 1.2 Test-Command-0.11/README0000444000175000010010000000200512137537222013515 0ustar danbooNoneTest-Command Test the exit status, STDOUT or STDERR of an external command. INSTALLATION To install this module, run the following commands: perl Makefile.PL make make test make install Alternatively, to install with Module::Build, you can use the following commands: perl Build.PL ./Build ./Build test ./Build install SUPPORT AND DOCUMENTATION After installing, you can find documentation for this module with the perldoc command. perldoc Test::Command You can also look for information at: Search CPAN http://search.cpan.org/dist/Test-Command CPAN Request Tracker: http://rt.cpan.org/NoAuth/Bugs.html?Dist=Test-Command AnnoCPAN, annotated CPAN documentation: http://annocpan.org/dist/Test-Command CPAN Ratings: http://cpanratings.perl.org/d/Test-Command COPYRIGHT AND LICENCE Copyright (C) 2007 Daniel B. Boorstein This program is free software; you can redistribute it and/or modify it under the same terms as Perl itself. Test-Command-0.11/t/0000755000175000010010000000000012137537222013105 5ustar danbooNoneTest-Command-0.11/t/00-load.t0000444000175000010010000000022712137537222014425 0ustar danbooNone#!perl -T use Test::More tests => 1; BEGIN { use_ok( 'Test::Command' ); } diag( "Testing Test::Command $Test::Command::VERSION, Perl $], $^X" ); Test-Command-0.11/t/01-funcs.t0000444000175000010010000001166412137537222014634 0ustar danbooNone#!perl use Test::More tests => 31; use Test::Command; use FindBin; ## write the output files to be used in all later tests open my $short_data_fh, '>', "$FindBin::Bin/short.txt" or BAIL_OUT("$FindBin::Bin/short.txt: $!"); print $short_data_fh "foo\n"; close $short_data_fh or die BAIL_OUT($!); open my $stdout_data_fh, '>', "$FindBin::Bin/stdout.txt" or BAIL_OUT("$FindBin::Bin/stdout.txt: $!"); print $stdout_data_fh "foo\nbar\n"; close $stdout_data_fh or die BAIL_OUT($!); open my $stderr_data_fh, '>', "$FindBin::Bin/stderr.txt" or BAIL_OUT("$FindBin::Bin/stderr.txt: $!"); print $stderr_data_fh "bar\nfoo\n"; close $stderr_data_fh or die BAIL_OUT($!); ## _slurp() tests my $text = Test::Command::_slurp("$FindBin::Bin/stdout.txt"); is($text, "foo\nbar\n", '_slurp'); ## make 10 attempts to find a non-existent file my $rand_file; for ( 1 .. 10 ) { $rand_file = rand; last if ! -e $rand_file; } SKIP: { skip 'could not find a non-existent file name', 1 if -e $rand_file; eval { $text = Test::Command::_slurp($rand_file) }; ok($@, '_slurp - no such file'); } eval { Test::Command::_slurp() }; like($@, qr/\$file_name is undefined/, '_slurp - no args'); ## _build_name() tests ## use anon sub to avoid uninitialized sub name warning my $name = sub { Test::Command::_build_name(undef, qw/ potato monkey /) }->(); is($name, "__ANON__: potato, monkey", '_build_name - string'); $name = sub { Test::Command::_build_name('rutabaga', qw/ potato monkey /) }->(); is($name, 'rutabaga', '_build_name - defined - string'); $name = sub { Test::Command::_build_name(undef, [qw/ potato -f monkey /], 'rutabaga') }->(); is($name, '__ANON__: potato -f monkey, rutabaga', '_build_name - array'); $name = sub { Test::Command::_build_name('chicken', [qw/ potato -f monkey /], 'rutabaga') }->(); is($name, 'chicken', '_build_name - defined - array'); eval { Test::Command::_build_name() }; like($@, qr/\$cmd is undefined/, '_build_name - no args'); eval { Test::Command::_get_result() }; like($@, qr/\$cmd is undefined/, '_get_result - no args'); eval { Test::Command::_run_cmd() }; like($@, qr/\$cmd is undefined/, '_run_cmd - no args'); ## _compare_files tests eval { Test::Command::_compare_files() }; like($@, qr/\$got_file is undefined/, '_compare_files - no args'); eval { Test::Command::_compare_files(1) }; like($@, qr/\$exp_file is undefined/, '_compare_files - no exp file'); eval { Test::Command::_compare_files(undef, 1) }; like($@, qr/\$got_file is undefined/, '_compare_files - no got file'); my ($files_ok, $diff_line) = Test::Command::_compare_files("$FindBin::Bin/stdout.txt", "$FindBin::Bin/stderr.txt"); ok(!$files_ok, '_compare_files - not ok'); cmp_ok($diff_line, '==', 1, "_compare_files - diff start"); ($files_ok, $diff_line) = Test::Command::_compare_files("$FindBin::Bin/stdout.txt", "$FindBin::Bin/stdout.txt"); ok($files_ok, '_compare_files - ok'); cmp_ok($diff_line, '==', 2, "_compare_files - no diff start"); ($files_ok, $diff_line) = Test::Command::_compare_files("$FindBin::Bin/short.txt", "$FindBin::Bin/stdout.txt"); ok(!$files_ok, '_compare_files - not ok'); cmp_ok($diff_line, '==', 2, "_compare_files - diff start"); ($files_ok, $diff_line) = Test::Command::_compare_files("$FindBin::Bin/stdout.txt", "$FindBin::Bin/short.txt"); ok(!$files_ok, '_compare_files - not ok'); cmp_ok($diff_line, '==', 2, "_compare_files - diff start"); SKIP: { skip 'could not find a non-existent file name', 2 if -e $rand_file; eval { Test::Command::_compare_files($rand_file, "$FindBin::Bin/stdout.txt") }; ok($@, '_compare_files - no such file - got'); eval { Test::Command::_compare_files("$FindBin::Bin/stdout.txt", $rand_file) }; ok($@, '_compare_files - no such file - exp'); } my $diff_column = Test::Command::_diff_column(); ok(! defined $diff_column, '_diff_column - no args'); $diff_column = Test::Command::_diff_column("potato"); cmp_ok($diff_column, '==', 1, '_diff_column - first arg'); $diff_column = Test::Command::_diff_column(undef, "potato"); cmp_ok($diff_column, '==', 1, '_diff_column - second arg'); $diff_column = Test::Command::_diff_column("potato", "potato"); ok(! defined $diff_column, '_diff_column - eq args'); $diff_column = Test::Command::_diff_column("potato", "patato"); cmp_ok($diff_column, '==', 2, '_diff_column - col 2(1)'); $diff_column = Test::Command::_diff_column("potato\n", "potato"); cmp_ok($diff_column, '==', 7, '_diff_column - col 7(1)'); $diff_column = Test::Command::_diff_column("potato", "potato\n"); cmp_ok($diff_column, '==', 7, '_diff_column - col 7(2)'); $diff_column = Test::Command::_diff_column("br\n", "bar\n"); cmp_ok($diff_column, '==', 2, '_diff_column - col 2(2)'); Test-Command-0.11/t/02-exit.t0000444000175000010010000000116212137537222014460 0ustar danbooNone#!perl use Test::Command tests => 8; use Test::More; ## determine whether we can run perl or not system qq($^X -e 1) and BAIL_OUT('error calling perl via system'); is( exit_value(qq($^X -e "1")), 0, "exit_value 0"); is( exit_value(qq($^X -e "exit 1")), 1, "exit_value 1"); exit_is_num(qq($^X -e "exit 1"), 1); exit_is_num(qq($^X -e "exit 255"), 255); exit_is_defined(qq($^X -e "exit 255")); SKIP: { skip("not sure about Win32 signal support", 1) if $^O eq 'MSWin32'; exit_is_undef([$^X, '-e', 'kill q(TERM), $$']); } exit_isnt_num(qq($^X -e 1), 2); exit_cmp_ok(qq($^X -e "exit 1"), '<', 2); Test-Command-0.11/t/03-stdout.t0000444000175000010010000000177512137537222015044 0ustar danbooNone#!perl use Test::Command tests => 15; use Test::More; use FindBin; ## determine whether we can run perl or not system qq($^X -e 1) and BAIL_OUT('error calling perl via system'); is( stdout_value(qq($^X -e "print 'foo'")), "foo", "stdout_value is foo"); is( Test::Command::_slurp(stdout_file(qq($^X -e "print 'foo'"))), "foo", "stdout_file contains foo"); stdout_is_eq(qq($^X -e "print 'foo'"), "foo"); stderr_is_eq(qq($^X -e "print 'foo'"), ""); stdout_is_eq([$^X, '-e', q(print 'foo')], 'foo'); stdout_is_eq(qq($^X -e "print STDERR 'foo'"), ''); stdout_isnt_eq(qq($^X -e "print 'foo'"), "bar"); stdout_is_num(qq($^X -e "print 123"), 123); stdout_isnt_num(qq($^X -e "print 321"), 123); stdout_like(qq($^X -e "print 'foo'"), qr/fo+/); stdout_unlike(qq($^X -e "print 'foo'"), qr/fooo/); stdout_cmp_ok(qq($^X -e "print 1"), '<', 2); stdout_cmp_ok(qq($^X -e "print 1"), '==', 1); stdout_cmp_ok(qq($^X -e "print 1"), 'eq', 1); stdout_is_file(qq($^X -le "print qq(foo\nbar)"), "$FindBin::Bin/stdout.txt"); Test-Command-0.11/t/04-stderr.t0000444000175000010010000000213212137537222015012 0ustar danbooNone#!perl use Test::Command tests => 15; use Test::More; use FindBin; ## determine whether we can run perl or not system qq($^X -e 1) and BAIL_OUT('error calling perl via system'); is( stderr_value(qq($^X -e "print STDERR 'foo'")), "foo", "stderr_value is foo"); is( Test::Command::_slurp(stderr_file(qq($^X -e "print STDERR 'foo'"))), "foo", "stderr_file contains foo"); stderr_is_eq(qq($^X -e "print STDERR 'foo'"), "foo"); stdout_is_eq(qq($^X -e "print STDERR 'foo'"), ""); stderr_is_eq([$^X, '-e', q(print STDERR 'foo')], 'foo'); stderr_is_eq(qq($^X -e "print 'foo'"), ''); stderr_isnt_eq(qq($^X -e "print STDERR 'foo'"), "bar"); stderr_is_num(qq($^X -e "print STDERR 123"), 123); stderr_isnt_num(qq($^X -e "print STDERR 321"), 123); stderr_like(qq($^X -e "print STDERR 'foo'"), qr/fo+/); stderr_unlike(qq($^X -e "print STDERR 'foo'"), qr/fooo/); stderr_cmp_ok(qq($^X -e "print STDERR 1"), '<', 2); stderr_cmp_ok(qq($^X -e "print STDERR 1"), '==', 1); stderr_cmp_ok(qq($^X -e "print STDERR 1"), 'eq', 1); stderr_is_file(qq($^X -le "print STDERR qq(bar\nfoo)"), "$FindBin::Bin/stderr.txt"); Test-Command-0.11/t/05-object.t0000444000175000010010000000503012137537222014756 0ustar danbooNone#!perl use Test::More tests => 38; use Test::Command; use FindBin; ## determine whether we can run perl or not system qq($^X -e 1) and BAIL_OUT('error calling perl via system'); my $test_perl = Test::Command->new( cmd => qq($^X -le "print qq(foo\nbar); print STDERR qq(bar\nfoo)") ); ok(defined $test_perl, 'defined $test_perl'); is(ref $test_perl, 'Test::Command', 'ref $test_perl'); $test_perl->run; is( $test_perl->exit_value, 0, "exit_value" ); $test_perl->exit_is_num(0); $test_perl->exit_isnt_num(1); $test_perl->exit_cmp_ok('<', 1); SKIP: { skip("not sure about Win32 signal support", 2) if $^O eq 'MSWin32'; is( $test_perl->signal_value, undef, "signal_value" ); $test_perl->signal_is_undef; } is( $test_perl->stdout_value, "foo\nbar\n", "stdout_value" ); is( Test::Command::_slurp($test_perl->stdout_file), "foo\nbar\n", "stdout_file" ); $test_perl->stdout_is_eq("foo\nbar\n"); $test_perl->stdout_isnt_eq("bar\nfoo\n"); { local $^W; $test_perl->stdout_is_num(0); $test_perl->stdout_isnt_num(1); } $test_perl->stdout_like(qr/foo\nBAR/i); $test_perl->stdout_unlike(qr/foo\nBAR/); $test_perl->stdout_cmp_ok('ne', "bar\nfoo\n"); $test_perl->stdout_is_file("$FindBin::Bin/stdout.txt"); is( $test_perl->stderr_value, "bar\nfoo\n", "stderr_value" ); is( Test::Command::_slurp($test_perl->stderr_file), "bar\nfoo\n", "stderr_file" ); $test_perl->stderr_is_eq("bar\nfoo\n"); $test_perl->stderr_isnt_eq("foo\nbar\n"); { local $^W; $test_perl->stderr_is_num(0); $test_perl->stderr_isnt_num(1); } $test_perl->stderr_like(qr/BAR\nFOO/i); $test_perl->stderr_unlike(qr/BAR\nFOO/); $test_perl->stderr_cmp_ok('ne', "foo\nbar\n"); $test_perl->stderr_is_file("$FindBin::Bin/stderr.txt"); ## test object with ARRAY ref command $test_perl = Test::Command->new( cmd => [$^X, '-le', 'print qq(foo\nbar); print STDERR qq(bar\nfoo)' ] ); ok(defined $test_perl, 'defined $test_perl'); is(ref $test_perl, 'Test::Command', 'ref $test_perl'); ## lazily run at first test $test_perl->exit_is_num(0); $test_perl->stdout_is_eq("foo\nbar\n"); $test_perl->stderr_is_eq("bar\nfoo\n"); package Test::Command::Derived; use base Test::Command; package main; $test_perl = Test::Command::Derived->new( cmd => qq($^X -le "print qq(foo\nbar); print STDERR qq(bar\nfoo)") ); ok(defined $test_perl, 'defined $test_perl'); is(ref $test_perl, 'Test::Command::Derived', 'ref $test_perl'); $test_perl->run; $test_perl->exit_is_num(0); $test_perl->exit_isnt_num(1); $test_perl->exit_cmp_ok('<', 1); Test-Command-0.11/t/06-signal.t0000444000175000010010000000206412137537222014772 0ustar danbooNone#!perl use strict; use warnings; use Test::Command tests => 9; use Test::More; use Config; my @sig_names = split ' ', $Config{'sig_name'}; my @sig_nums = split ' ', $Config{'sig_num'}; my %sig; @sig{@sig_names} = @sig_nums; ## determine whether we can run perl or not system qq($^X -e 1) and BAIL_OUT('error calling perl via system'); SKIP: { skip("not sure about Win32 signal support", 9) if $^O eq 'MSWin32'; signal_is_undef(qq($^X -e "exit 0")); signal_is_undef(qq($^X -e "exit 1")); signal_is_undef(qq($^X -e "exit 255")); signal_is_undef([$^X, '-e', 1]); skip("no SIGTERM found", 5) if ! exists $sig{'TERM'}; is(signal_value([$^X, '-e', 'kill ' . $sig{'TERM'} . ', $$']), $sig{'TERM'}, "signal_value is SIGTERM" );; signal_is_defined([$^X, '-e', 'kill ' . $sig{'TERM'} . ', $$']); signal_cmp_ok([$^X, '-e', 'kill ' . $sig{'TERM'} . ', $$'], '>', -1 ); signal_isnt_num([$^X, '-e', 'kill ' . $sig{'TERM'} . ', $$'], $sig{'TERM'} + 1 ); signal_is_num([$^X, '-e', 'kill ' . $sig{'TERM'} . ', $$'], $sig{'TERM'} ); } Test-Command-0.11/t/pod-coverage.t0000444000175000010010000000025412137537222015644 0ustar danbooNone#!perl -T use Test::More; eval "use Test::Pod::Coverage 1.04"; plan skip_all => "Test::Pod::Coverage 1.04 required for testing POD coverage" if $@; all_pod_coverage_ok(); Test-Command-0.11/t/pod.t0000444000175000010010000000021412137537222014047 0ustar danbooNone#!perl -T use Test::More; eval "use Test::Pod 1.14"; plan skip_all => "Test::Pod 1.14 required for testing POD" if $@; all_pod_files_ok();