How can I verify that a Perl program compiles from my test suite?

I am creating a regression system (not unit testing) for some Perl scripts.

The main component of the system is

  `perl script.pl @params 1>stdoutfile 2>stderrfile`;

However, in the process of working on scripts, they sometimes do not compile (Shock!). But perl itself will execute correctly. However, I do not know how to detect on stderr whether Perl failed to compile (and therefore wrote stderr), or my script barfed at the input (and therefore wrote stderr).

How to determine if a program was executed or not without exhaustively searching for Perl error messages and grepping the stderr file?

+3
source share
5 answers

:

system('$^X -c script.pl');
if ($? == 0) {
    # it compiled, now let see if it runs
    system('$^X script.pl', @params, '1>stdoutfile', '2>stderrfile');
    # check $?
}
else {
    warn "script.pl didn't compile";
}

$^X perl. . , , - , . ( PERL5LIB), perl .

+6

, , , :)

t/compile.t . "", script :

use Test::More tests => 1;

my $file = '...';

print "bail out! Script file is missing!" unless -e $file;

my $output = `$^X -c $file 2>&1`;

print "bail out! Script file does not compile!"
    unless like( $output, qr/syntax OK$/, 'script compiles' );
+3

, , . , . unit test ... ?

#!/usr/bin/perl -w

# Only run if we're the file being executed by Perl
main() if $0 eq __FILE__;

sub main {
    ...your code here...
}

1;

script, .

#!/usr/bin/perl -w

use Test::More;

require_ok("./script.pl");

main(). Test::Output . local @ARGV main(), @ARGV ().

main() , unit test.

+3

$?.

perldoc perlvar:

The status returned by the last channel close, backtick ("` `"), a successful call to wait () or waitpid () or from the system () operator. This is just a 16-bit status word returned by the traditional wait () Unix system call (otherwise it is composed to look like this). Thus, the output value of the subprocess is valid ("$? → 8"), and "$? And 127" gives which signal, if any, the process died, and "$? And 128" tells whether there was a kernel dump.

+2
source

Sounds like you need to IPC::Open3.

0
source

Source: https://habr.com/ru/post/1711107/


All Articles