/usr/share/perl5/Test/Script.pm is in libtest-script-perl 1.14-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 | package Test::Script;
# ABSTRACT: Basic cross-platform tests for scripts
our $VERSION = '1.14'; # VERSION
use 5.006;
use strict;
use warnings;
use Carp ();
use Exporter ();
use File::Spec ();
use File::Spec::Unix ();
use Probe::Perl ();
use IPC::Run3 qw( run3 );
use Test::Builder ();
use File::Temp ();
use File::Path ();
our @ISA = 'Exporter';
our @EXPORT = qw{
script_compiles
script_compiles_ok
script_runs
script_stdout_is
script_stdout_isnt
script_stdout_like
script_stdout_unlike
script_stderr_is
script_stderr_isnt
script_stderr_like
script_stderr_unlike
};
sub import {
my $self = shift;
my $pack = caller;
my $test = Test::Builder->new;
$test->exported_to($pack);
$test->plan(@_);
foreach ( @EXPORT ) {
$self->export_to_level(1, $self, $_);
}
}
my $perl = undef;
sub perl () {
$perl or
$perl = Probe::Perl->find_perl_interpreter;
}
sub path ($) {
my $path = shift;
unless ( defined $path ) {
Carp::croak("Did not provide a script name");
}
if ( File::Spec::Unix->file_name_is_absolute($path) ) {
Carp::croak("Script name must be relative");
}
File::Spec->catfile(
File::Spec->curdir,
split /\//, $path
);
}
## This can and should be removed if/when IPC::Run3 is fixed on MSWin32
## See rt94685, rt46333, rt95308 and IPC-Run3/gh#9"
sub _borked_ipc_run3 () {
$^O eq 'MSWin32' &&
! eval { IPC::Run3::run3 [ perl, -e => 'BEGIN {die}' ], \undef, \undef, \undef; 1 }
}
if(_borked_ipc_run3())
{
no warnings 'redefine';
*run3 = sub {
$! = 0;
my $r = IPC::Run3::run3(@_, { return_if_system_error => 1 });
Carp::croak($!) if $! && $! !~ /Inappropriate I\/O control operation/;
$r;
};
}
#####################################################################
# Test Functions
sub script_compiles {
my $args = _script(shift);
my $unix = shift @$args;
my $path = path( $unix );
my @libs = map { "-I$_" } grep {!ref($_)} @INC;
my $dir = _preload_module();
my $cmd = [ perl, "-I$dir", '-M__TEST_SCRIPT__', '-c', $path, @$args ];
my $stdin = '';
my $stdout = '';
my $stderr = '';
my $rv = eval { run3( $cmd, \$stdin, \$stdout, \$stderr ) };
my $error = $@;
my $exit = $? ? ($? >> 8) : 0;
my $signal = $? ? ($? & 127) : 0;
my $ok = !! (
$error eq '' and $rv and $exit == 0 and $signal == 0 and $stderr =~ /syntax OK\s+\z/si
);
File::Path::rmtree($dir);
my $test = Test::Builder->new;
$test->ok( $ok, $_[0] || "Script $unix compiles" );
$test->diag( "$exit - $stderr" ) unless $ok;
$test->diag( "exception: $error" ) if $error;
$test->diag( "signal: $signal" ) if $signal;
return $ok;
}
# this is noticably slower for long @INC lists (sometimes present in cpantesters
# boxes) than the previous implementation, which added a -I for every element in
# @INC. (also slower for more reasonable @INCs, but not noticably). But it is
# safer as very long argument lists can break calls to system
sub _preload_module
{
my $dir = File::Temp::tempdir( CLEANUP => 1 );
# this is hopefully a pm file that nobody would use
my $filename = File::Spec->catfile($dir, '__TEST_SCRIPT__.pm');
my $fh;
open($fh, ">$filename")
|| die "unable to open $filename: $!";
print($fh 'unshift @INC, ',
join ',',
# quotemeta is overkill, but it will make sure that characters
# like " are quoted
map { '"' . quotemeta($_) . '"' }
grep { ! ref } @INC)
|| die "unable to write $filename: $!";
close($fh) || die "unable to close $filename: $!";;
$dir;
}
my $stdout;
my $stderr;
sub script_runs {
my $args = _script(shift);
my $opt = _options(\@_);
my $unix = shift @$args;
my $path = path( $unix );
my $dir = _preload_module();
my $cmd = [ perl, "-I$dir", '-M__TEST_SCRIPT__', $path, @$args ];
$stdout = '';
$stderr = '';
my $rv = eval { run3( $cmd, $opt->{stdin}, $opt->{stdout}, $opt->{stderr} ) };
my $error = $@;
my $exit = $? ? ($? >> 8) : 0;
my $signal = $? ? ($? & 127) : 0;
my $ok = !! ( $error eq '' and $rv and $exit == $opt->{exit} and $signal == $opt->{signal} );
File::Path::rmtree($dir);
my $test = Test::Builder->new;
$test->ok( $ok, $_[0] || "Script $unix runs" );
$test->diag( "$exit - $stderr" ) unless $ok;
$test->diag( "exception: $error" ) if $error;
$test->diag( "signal: $signal" ) unless $signal == $opt->{signal};
return $ok;
}
sub _like
{
my($text, $pattern, $regex, $not, $name) = @_;
my $ok = $regex ? $text =~ $pattern : $text eq $pattern;
$ok = !$ok if $not;
my $test = Test::Builder->new;
$test->ok( $ok, $name );
unless($ok) {
$test->diag( "The output" );
$test->diag( " $_") for split /\n/, $text;
$test->diag( $not ? "does match" : "does not match" );
if($regex) {
$test->diag( " $pattern" );
} else {
$test->diag( " $_" ) for split /\n/, $pattern;
}
}
$ok;
}
sub script_stdout_is
{
my($pattern, $name) = @_;
@_ = ($stdout, $pattern, 0, 0, $name || 'stdout matches' );
goto &_like;
}
sub script_stdout_isnt
{
my($pattern, $name) = @_;
@_ = ($stdout, $pattern, 0, 1, $name || 'stdout does not match' );
goto &_like;
}
sub script_stdout_like
{
my($pattern, $name) = @_;
@_ = ($stdout, $pattern, 1, 0, $name || 'stdout matches' );
goto &_like;
}
sub script_stdout_unlike
{
my($pattern, $name) = @_;
@_ = ($stdout, $pattern, 1, 1, $name || 'stdout does not match' );
goto &_like;
}
sub script_stderr_is
{
my($pattern, $name) = @_;
@_ = ($stderr, $pattern, 0, 0, $name || 'stderr matches' );
goto &_like;
}
sub script_stderr_isnt
{
my($pattern, $name) = @_;
@_ = ($stderr, $pattern, 0, 1, $name || 'stderr does not match' );
goto &_like;
}
sub script_stderr_like
{
my($pattern, $name) = @_;
@_ = ($stderr, $pattern, 1, 0, $name || 'stderr matches' );
goto &_like;
}
sub script_stderr_unlike
{
my($pattern, $name) = @_;
@_ = ($stderr, $pattern, 1, 1, $name || 'stderr does not match' );
goto &_like;
}
######################################################################
# Support Functions
# Script params must be either a simple non-null string with the script
# name, or an array reference with one or more non-null strings.
sub _script {
my $in = shift;
if ( defined _STRING($in) ) {
return [ $in ];
}
if ( _ARRAY($in) ) {
unless ( scalar grep { not defined _STRING($_) } @$in ) {
return $in;
}
}
Carp::croak("Invalid command parameter");
}
# Inline some basic Params::Util functions
sub _options {
my %options = ref($_[0]->[0]) eq 'HASH' ? %{ shift @{ $_[0] } }: ();
$options{exit} = 0 unless defined $options{exit};
$options{signal} = 0 unless defined $options{signal};
my $stdin = '';
$options{stdin} = \$stdin unless defined $options{stdin};
$options{stdout} = \$stdout unless defined $options{stdout};
$options{stderr} = \$stderr unless defined $options{stderr};
\%options;
}
sub _ARRAY ($) {
(ref $_[0] eq 'ARRAY' and @{$_[0]}) ? $_[0] : undef;
}
sub _STRING ($) {
(defined $_[0] and ! ref $_[0] and length($_[0])) ? $_[0] : undef;
}
BEGIN {
# Alias to old name
*script_compiles_ok = *script_compiles;
}
1;
__END__
=pod
=encoding UTF-8
=head1 NAME
Test::Script - Basic cross-platform tests for scripts
=head1 VERSION
version 1.14
=head1 SYNOPSIS
use Test::More tests => 2;
use Test::Script;
script_compiles('script/myscript.pl');
script_runs(['script/myscript.pl', '--my-argument']);
=head1 DESCRIPTION
The intent of this module is to provide a series of basic tests for 80%
of the testing you will need to do for scripts in the F<script> (or F<bin>
as is also commonly used) paths of your Perl distribution.
Further, it aims to provide this functionality with perfect
platform-compatibility, and in a way that is as unobtrusive as possible.
That is, if the program works on a platform, then B<Test::Script>
should always work on that platform as well. Anything less than 100% is
considered unacceptable.
In doing so, it is hoped that B<Test::Script> can become a module that
you can safely make a dependency of all your modules, without risking that
your module won't on some platform because of the dependency.
Where a clash exists between wanting more functionality and maintaining
platform safety, this module will err on the side of platform safety.
=head1 FUNCTIONS
=head2 script_compiles
script_compiles( $script, $test_name );
The L</script_compiles> test calls the script with "perl -c script.pl",
and checks that it returns without error.
The path it should be passed is a relative unix-format script name. This
will be localised when running C<perl -c> and if the test fails the local
name used will be shown in the diagnostic output.
Note also that the test will be run with the same L<perl> interpreter that
is running the test script (and not with the default system perl). This
will also be shown in the diagnostic output on failure.
=head2 script_runs
script_runs( $script, $test_name );
script_runs( \@script_and_arguments, $test_name );
script_runs( $script, \%options, $test_name );
script_runs( \@script_and_arguments, \%options, $test_name );
The L</script_runs> test executes the script with "perl script.pl" and checks
that it returns success.
The path it should be passed is a relative unix-format script name. This
will be localised when running C<perl -c> and if the test fails the local
name used will be shown in the diagnostic output.
The test will be run with the same L<perl> interpreter that is running the
test script (and not with the default system perl). This will also be shown
in the diagnostic output on failure.
You may pass in options as a hash as the second argument.
=over 4
=item exit
The expected exit value. The default is to use whatever indicates success
on your platform (usually 0).
=item signal
The expected signal. The default is 0. Use with care! This may not be
portable, and is known not to work on Windows.
=item stdin
The input to be passed into the script via stdin. The value may be one of
=over 4
=item simple scalar
Is considered to be a filename.
=item scalar reference
In which case the input will be drawn from the data contained in the referenced
scalar.
=back
The behavior for any other types is undefined (the current implementation uses
L<IPC::Run3>, but that may change in the future).
=item stdout
Where to send the standard output to. If you use this option, then the the
behavior of the C<script_stdout_> functions below are undefined. The value
may be one of
=over 4
=item simple scalar
Is considered to be a filename.
=item scalar reference
=back
In which case the standard output will be places into the referenced scalar
The behavior for any other types is undefined (the current implementation uses
L<IPC::Run3>, but that may change in the future).
=item stderr
Same as C<stdout> above, except for stderr.
=back
=head2 script_stdout_is
script_stdout_is $expected_stdout, $test_name;
Tests if the output to stdout from the previous L</script_runs> matches the
expected value exactly.
=head2 script_stdout_isnt
script_stdout_is $expected_stdout, $test_name;
Tests if the output to stdout from the previous L</script_runs> does NOT match the
expected value exactly.
=head2 script_stdout_like
script_stdout_like $regex, $test_name;
Tests if the output to stdout from the previous L</script_runs> matches the regular
expression.
=head2 script_stdout_unlike
script_stdout_unlike $regex, $test_name;
Tests if the output to stdout from the previous L</script_runs> does NOT match the regular
expression.
=head2 script_stderr_is
script_stderr_is $expected_stderr, $test_name;
Tests if the output to stderr from the previous L</script_runs> matches the
expected value exactly.
=head2 script_stderr_isnt
script_stderr_is $expected_stderr, $test_name;
Tests if the output to stderr from the previous L</script_runs> does NOT match the
expected value exactly.
=head2 script_stderr_like
script_stderr_like $regex, $test_name;
Tests if the output to stderr from the previous L</script_runs> matches the regular
expression.
=head2 script_stderr_unlike
script_stderr_unlike $regex, $test_name;
Tests if the output to stderr from the previous L</script_runs> does NOT match the regular
expression.
=head1 CAVEATS
This module is fully supported back to Perl 5.8.1. It may work on 5.8.0.
It should work on Perl 5.6.x and I may even test on 5.6.2. I will accept
patches to maintain compatibility for such older Perls, but you may
need to fix it on 5.6.x / 5.8.0 and send me a patch.
This module uses L<IPC::Run3> to compile and run scripts. There are a number of
outstanding issues with this module, and maintenance for L<IPC::Run3> is not swift.
One of these is that L<IPC::Run3> incorrectly throws an exception on Windows when
you feed it a Perl script with a compile error. Currently L<Test::Script> probes
for this bug (it checks for the bug, not for a specific version) and applies a
workaround in that case. I am hoping to remove the work around once the bug is
fixed in L<IPC::Run3>.
=head1 SEE ALSO
L<Test::Script::Run>, L<Test::More>
=head1 AUTHOR
Original author: Adam Kennedy
Current maintainer: Graham Ollis E<lt>plicease@cpan.orgE<gt>
Contributors:
Brendan Byrd
=head1 COPYRIGHT AND LICENSE
This software is copyright (c) 2006 by Adam Kennedy.
This is free software; you can redistribute it and/or modify it under
the same terms as the Perl 5 programming language system itself.
=cut
|