/usr/share/perl5/Test/Cmd/Common.pm is in libtest-cmd-perl 1.09-1.
This file is owned by root:root, with mode 0o644.
The actual contents of the file can be viewed below.
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 370 371 372 373 374 375 376 377 378 379 380 381 382 383 384 385 386 387 388 389 390 391 392 393 394 395 396 397 398 399 400 401 402 403 404 405 406 407 408 409 410 411 412 413 414 415 416 417 418 419 420 421 422 423 424 425 426 427 428 429 430 431 432 433 434 435 436 437 438 439 440 441 442 443 444 445 446 447 448 449 450 451 452 453 454 455 456 457 458 459 460 461 462 463 464 465 466 467 468 469 470 471 472 473 474 475 476 477 478 479 480 481 482 483 484 485 486 487 488 489 490 491 492 493 494 495 496 497 498 499 500 501 502 503 504 505 506 507 508 509 510 511 512 513 514 515 516 517 518 519 520 521 522 523 524 525 526 527 528 529 530 531 532 533 534 535 536 537 538 539 540 541 542 543 544 545 546 547 548 549 550 551 552 553 554 555 556 557 558 559 560 561 562 563 564 565 566 567 568 569 570 571 572 573 574 575 576 577 578 579 580 581 582 583 584 585 586 587 588 589 590 591 592 593 594 595 596 597 598 599 600 601 602 603 604 605 606 607 608 609 610 611 612 | # Copyright 1999-2001 Steven Knight. All rights reserved. This program
# is free software; you can redistribute it and/or modify it under the
# same terms as Perl itself.
#
# This package tests an executable program or script,
# managing one or more temporary working directories,
# keeping track of standard and error output,
# and cleaning up after everything is done.
package Test::Cmd::Common;
$Test::Cmd::Common::VERSION = '1.09';
use 5.006;
use strict;
use warnings;
use Exporter ();
our ($_exe, $_o, $_so, $_a, $_is_win32);
our @ISA = qw(Test::Cmd Exporter);
our @EXPORT_OK = qw($_exe $_o $_a $_so $_is_win32);
use Config;
use Cwd;
use File::Copy ();
use Test::Cmd;
=head1 NAME
Test::Cmd::Common - module for common Test::Cmd error handling
=head1 SYNOPSIS
use Test::Cmd::Common;
$test = Test::Cmd::Common->new(string => 'functionality being tested',
prog => 'program_under_test',
);
$test->run(chdir => 'subdir', fail => '$? != 0',
flags => '-x', targets => '.',
stdout => <<_EOF_, stderr => <<_EOF_);
expected standard output
_EOF_
expected error output
_EOF_
$test->subdir('subdir', ...);
$test->read(\$contents, 'file');
$test->read(\@lines, 'file');
$test->write('file', <<_EOF_);
contents of the file
_EOF_
$test->file_matches();
$test->must_exist('file', ['subdir', 'file'], ...);
$test->must_not_exist('file', ['subdir', 'file'], ...);
$test->copy('src_file', 'dst_file');
$test->chmod($mode, 'file', ...);
$test->sleep;
$test->sleep($seconds);
$test->touch('file', ...);
$test->unlink('file', ...);
=head1 DESCRIPTION
The C<Test::Cmd::Common> module provides a simple, high-level interface
for writing tests of executable commands and scripts, especially
commands and scripts that interact with the file system. All methods
throw exceptions and exit on failure. This makes it unnecessary to add
explicit checks for return values, making the test scripts themselves
simpler to write and easier to read.
The C<Test::Cmd::Common> class is a subclass of L<Test::Cmd>. In
essence, C<Test::Cmd::Common> is a wrapper that treats common
L<Test::Cmd> error conditions as exceptions that terminate the test.
You can use C<Test::Cmd::Common> directly, or subclass it for your
program and add additional (or override) methods to tailor it to your
program's specific needs. Alternatively, C<Test::Cmd::Common> serves as
a useful example of how to define your own L<Test::Cmd> subclass.
The C<Test::Cmd::Common> module provides the following importable
variables:
=over 4
=item C<$_exe>
The executable file suffix. This value is normally available
as C<$Config{_exe}> in Perl version 5.005 and later. The
C<Test::Cmd::Common> module figures it out via other means in earlier
versions.
=item C<$_o>
The object file suffix. This value is normally available
from C<$Config{_o}> in Perl version 5.005 and later. The
C<Test::Cmd::Common> module figures it out via other means in earlier
versions.
=item C<$_a>
The library file suffix. This value is normally available
from as C<$Config{_a}> in Perl version 5.005 and later. The
C<Test::Cmd::Common> module figures it out via other means in earlier
versions.
=item C<$_so>
The shared library file suffix. This value is normally available
as C<$Config{_so}> in Perl version 5.005 and later. The
C<Test::Cmd::Common> module figures it out via other means in earlier
versions.
=item C<$_is_win32>
A Boolean value that reflects whether the current platform is a Win32
system.
=back
=head1 METHODS
=over 4
=cut
BEGIN {
if ($] < 5.003) {
eval("require Win32");
$_is_win32 = ! $@;
} else {
$_is_win32 = $^O eq "MSWin32";
}
$_exe = $Config{_exe};
$_exe = $Config{exe_ext} if ! defined $_exe;
$_exe = $_is_win32 ? '.exe' : '' if ! defined $_exe;
$_o = $Config{_o};
$_o = $Config{obj_ext} if ! defined $_o;
$_o = $_is_win32 ? '.obj' : '.o' if ! defined $_o;
$_a = $Config{_a};
$_a = $Config{lib_ext} if ! defined $_a;
$_a = $_is_win32 ? '.lib' : '.a';
$_so = ".$Config{so}";
$_so = $_is_win32 ? '.dll' : '.so' if ! defined $_so;
}
=item C<new>
Creates a new test environment object. Any arguments are keyword-value
pairs that are passed through to the construct method for the base
class from which we inherit our methods (that is, the L<Test::Cmd>
class). In the normal case, this should be the program to be tested and
a description of the functionality being tested:
$test = Test::Cmd::Common->new(prog => 'my_program',
string => 'cool new feature');
By default, methods that match actual versus expected output (the
C<run>, and C<file_matches> methods) use an exact match. Tests that
require regular expression matches can specify this on initialization of
the test environment:
$test = Test::Cmd::Common->new(prog => 'my_program',
string => 'cool new feature',
match_sub => \&Test::Cmd::diff_regex);
or by executing the following after initialization of the test
environment:
$test->match_sub(\&Test::Cmd::diff_regex);
Creates a temporary working directory for the test environment and
changes directory to it.
Exits NO RESULT if the object can not be created, the temporary working
directory can not be created, or the current directory cannot be changed
to the temporary working directory.
=cut
sub new {
my $proto = shift;
my $class = ref($proto) || $proto;
my $test = $class->SUPER::new(@_);
$class->SUPER::no_result(! $test, undef, 1);
# We're going to chdir to the temporary working directory.
# So that things work properly relative to the current directory,
# turn any relative path names in @INC to absolute paths.
my $cwd = Cwd::cwd();
map { $_ = $test->catdir($cwd, $_) if ! $test->file_name_is_absolute($_) }
@INC;
my $ret = chdir $test->workdir;
$test->no_result(! $ret, undef, 1);
if (! grep {$_ eq 'match_sub'} @_) {
$test->match_sub(\&Test::Cmd::diff_exact);
}
bless($test, $class);
}
sub _fail_match_show {
my($self, $stream, $expected, $actual, $level) = @_;
my @diffs;
$self->fail(! $self->match($actual, $expected, \@diffs)
=> sub {print STDERR
"diff expected vs. actual contents of $stream =====\n",
@diffs},
$level + 1);
}
=item C<run>
Runs the program under test, checking that the test succeeded.
Arguments are keyword-value pairs that affect the manner in which the
program is executed or the results are evaluated.
chdir => 'subdir'
fail => 'failure condition' # default is '$? != 0'
flags => 'Cons flags'
stderr => 'expected error output'
stdout => 'expected standard output'
targets => 'targets to build'
The test fails if:
-- The specified failure condition is met. The default failure
condition is '$? != 0', i.e. the program exits unsuccesfully.
A not-uncommon alternative is:
$test->run(fail => '$? == 0'); # expect failure
when testing how the program handles errors.
-- Actual standard output does not match expected standard output
(if any). The expected standard output is an array of lines
or a scalar which will be split on newlines.
-- Actual error output does not match expected error output (if
any). The expected error output is an array of lines or a
scalar which will be split on newlines.
This method will test for NO error output by default if no
expected error output is specified (unlike standard output).
The error output test may be explicitly suppressed by
specifying undef as the "expected" error output:
$test->run(stderr => undef);
By default, this method performs an exact match of actual vs. expected
standard output or error output:
$test->run(stdout => <<_EOF_, stderr => _EOF_);
An expected STDOUT line, which must be matched exactly.
_EOF_
One or more expected STDERR lines,
which must be matched exactly.
_EOF_
Tests that require regular expression matches should be executed using a
test environment that calls the C<match_sub> method as follows:
$test->match_sub(\&Test::Cmd::diff_regex);
$test->run(stdout => <<_EOF_, stderr => _EOF_);
An expected (STDOUT|standard output) line\.
_EOF_
One or more expected (STDERR|error output) lines,
which may contain (regexes|regular expressions)\.
_EOF_
=cut
sub run {
my $self = shift;
my %args = @_;
my $cmd = $args{'args'};
if (! $cmd) {
$cmd = $args{'targets'};
$cmd = "$args{'flags'} $cmd" if $args{'flags'};
}
my $lev = $args{'level'} || 0;
$self->SUPER::run(@_, args => $cmd);
my $cond = $args{'fail'} || '$? != 0';
$self->fail(eval $cond
=> sub {print STDERR $self->stdout, $self->stderr},
$lev + 1);
if (defined $args{'stdout'}) {
my @stdout = $self->stdout;
$self->_fail_match_show('STDOUT', $args{'stdout'}, \@stdout, $lev + 1);
}
$args{'stderr'} = '' if ! grep($_ eq 'stderr', keys %args);
if (defined $args{'stderr'}) {
my @stderr = $self->stderr;
$self->_fail_match_show('STDERR', $args{'stderr'}, \@stderr, $lev + 1);
}
}
=item C<subdir>
Creates one or more subdirectories in the temporary working directory.
Exits NO RESULT if the number of subdirectories actually created does
not match the number expected. For compatibility with its superclass
method, returns the number of subdirectories actually created.
=cut
sub subdir {
my $self = shift;
my $expected = @_;
my $ret = $self->SUPER::subdir(@_);
$self->no_result($expected != $ret,
=> sub {print STDERR "could not create subdirectories: $!\n"},
1);
return $ret;
}
=item C<read>
Reads the contents of a file, depositing the contents in the destination
referred to by the first argument (a scalar or array reference). If the
file name is not an absolute path name, it is relative to the temporary
working directory. Exits NO RESULT if the file could not be read for
any reason. For compatibility with its superclass method, returns TRUE
on success.
=cut
sub read {
my $self = shift;
my $destref = shift;
my $ret = $self->SUPER::read($destref, @_);
$self->no_result(! $ret
=> sub {print STDERR "could not read file contents: $!\n"},
1);
return 1;
}
=item C<write>
Writes a file with the specified contents. If the file name is not an
absolute path name, it is relative to the temporary working directory.
Exits NO RESULT if there were any errors writing the file.
For compatibility with its superclass method, returns TRUE on success.
$test->write('file', <<_EOF_);
contents of the file
_EOF_
=cut
sub write {
my $self = shift;
my $file = shift; # the file to write to
my $ret = $self->SUPER::write($file, @_);
$self->no_result(! $ret
=> sub {$file = $self->catfile(@$file) if ref $file;
print STDERR "could not write $file: $!\n"},
1);
return 1;
}
=item C<file_matches>
Matches the contents of the specified file (first argument) against the
expected contents. The expected contents are an array of lines or a
scalar which will be split on newlines. By default, each expected line
must match exactly its corresponding line in the file:
$test->file_matches('file', <<_EOF_);
Line #1.
Line #2.
_EOF_
Tests that require regular expression matches should be executed using a
test environment that calls the C<match_sub> method as follows:
$test->match_sub(\&Test::Cmd::diff_regex);
$test->file_matches('file', <<_EOF_);
The (1st|first) line\.
The (2nd|second) line\.
_EOF_
=cut
sub file_matches {
my($self, $file, $regexes) = @_;
my @lines;
my $ret = $self->SUPER::read(\@lines, $file);
$self->no_result(! $ret
=> sub {print STDERR "could not read contents of $file: $!\n"},
1);
my @diffs;
$self->fail(! $self->match(\@lines, $regexes, \@diffs)
=> sub {$file = $self->catfile(@$file) if ref $file;
print STDERR
"diff expected vs. actual contents of $file =====\n",
@diffs},
1);
}
=item C<must_exist>
Ensures that the specified files must exist. Files may be specified as
an array reference of directory components, in which case the pathname
will be constructed by concatenating them. Exits FAILED if any of the
files does not exist.
=cut
sub must_exist {
my $self = shift;
map(ref $_ ? $self->catfile(@$_) : $_, @_);
my @missing = grep(! -e $_, @_);
$self->fail(0 + @missing => sub {print STDERR "files are missing: @missing\n"}, 1);
}
=item C<must_not_exist>
Ensures that the specified files must not exist. Files may be specified
as an array reference of directory components, in which case the pathname
will be constructed by concatenating them. Exits FAILED if any of the
files exists.
=cut
sub must_not_exist {
my $self = shift;
map(ref $_ ? $self->catfile(@$_) : $_, @_);
my @exist = grep(-e $_, @_);
$self->fail(0 + @exist => sub {print STDERR "unexpected files exist: @exist\n"}, 1);
}
=item C<copy>
Copies a file from the source (first argument) to the destination
(second argument). Exits NO RESULT if the file could not be copied
for any reason.
=cut
sub copy {
my($self, $src, $dest) = @_;
my $ret = File::Copy::copy($src, $dest);
$self->no_result(! $ret
=> sub {print STDERR "could not copy $src to $dest: $!\n"},
1);
}
=item C<chmod>
Changes the permissions of a list of files to the specified mode (first
argument). Exits NO RESULT if any file could not be changed for any
reason.
=cut
sub chmod {
my $self = shift;
my $mode = shift;
my $expected = @_;
my $ret = CORE::chmod($mode, @_);
$self->no_result($expected != $ret,
=> sub {print STDERR "could not chmod files: $!\n"},
1);
}
=item C<sleep>
Sleeps at least the specified number of seconds. If no number is
specified, sleeps at least a minimum number of seconds necessary to
advance file time stamps on the current system. Sleeping more seconds
is all right. Exits NO RESULT if the time slept was less than specified.
=cut
sub sleep {
my($self, $seconds) = @_;
# On Windows systems, DOS and FAT file systems have only a
# two-second granularity, so we must sleep two seconds to
# ensure that file time stamps will be newer.
$seconds = $_is_win32 ? 2 : 1 if ! defined $seconds;
my $ret = CORE::sleep($seconds);
$self->no_result($ret < $seconds,
=> sub {print STDERR "only slept $ret seconds\n"},
1);
}
=item C<touch>
Updates the access and modification times of the specified files.
Exits NO RESULT if any file could not be modified for any reason.
=cut
sub touch {
my $self = shift;
my $time = shift;
my $expected = @_;
my $ret = CORE::utime($time, $time, @_);
$self->no_result($expected != $ret,
=> sub {print STDERR "could not touch files: $!\n"},
1);
}
=item C<unlink>
Removes the specified files. Exits NO RESULT if any file could not be
removed for any reason.
=cut
sub unlink {
my $self = shift;
my @not_removed;
my $file;
foreach $file (@_) {
$file = $self->catfile(@$file) if ref $file;
if (! CORE::unlink($file)) {
push @not_removed, $file;
}
}
$self->no_result(@not_removed != 0,
=> sub {print STDERR "could not unlink files (@not_removed): $!\n"},
1);
}
1;
__END__
=back
=head1 ENVIRONMENT
The C<Test::Cmd::Common> module also uses the
C<PRESERVE>,
C<PRESERVE_FAIL>,
C<PRESERVE_NO_RESULT>,
and C<PRESERVE_PASS>
environment variables from the L<Test::Cmd> module.
See the L<Test::Cmd> documentation for details.
=head1 SEE ALSO
L<perl(1)>, L<Test::Cmd>.
The most involved example of using the C<Test::Cmd::Common> module
to test a real-world application is the C<cons-test> testing suite
for the Cons software construction utility. The suite sub-classes
C<Test::Cmd::Common> to provide common, application-specific
infrastructure across a large number of end-to-end application tests.
The suite, and other information about Cons, is available at:
http://www.dsmit.com/cons
=head1 AUTHOR
Steven Knight, knight@baldmt.com
=head1 ACKNOWLEDGEMENTS
Thanks to Johan Holmberg for asking the question that led to the
creation of this package.
The general idea of testing commands in this way, as well as the test
reporting of the C<pass>, C<fail> and C<no_result> methods, come from
the testing framework invented by Peter Miller for his Aegis project
change supervisor. Aegis is an excellent bit of work which integrates
creation and execution of regression tests into the software development
process. Information about Aegis is available at:
http://www.tip.net.au/~millerp/aegis.html
=cut
|