From 34a6a9b1599788ce4e85a08d579ff19bcb6a4b89 Mon Sep 17 00:00:00 2001 From: Richard Levitte Date: Tue, 21 Mar 2017 10:53:56 +0100 Subject: [PATCH] OpenSSL::Test: add a statusvar option for run with capture => 1 When using run() with capture => 1, there was no way to find out if the command was successful or not. This change adds a statusvar option, that must refer to a scalar variable, for example: my $status = undef; my @line = run(["whatever"], capture => 1, statusvar => \$status); $status will be 1 if the command "whatever" was successful, 0 otherwise. Reviewed-by: Rich Salz (Merged from https://github.com/openssl/openssl/pull/3004) --- test/testlib/OpenSSL/Test.pm | 17 ++++++++++++++--- 1 file changed, 14 insertions(+), 3 deletions(-) diff --git a/test/testlib/OpenSSL/Test.pm b/test/testlib/OpenSSL/Test.pm index 66fa4dcb0f..cbfc867a7f 100644 --- a/test/testlib/OpenSSL/Test.pm +++ b/test/testlib/OpenSSL/Test.pm @@ -403,6 +403,12 @@ return the resulting output as an array of lines. If false or not given, the command will be executed with C, and C will return 1 if the command was successful or 0 if it wasn't. +=item B VARREF> + +If used, B must be a reference to a scalar variable. It will be +assigned a boolean indicating if the command succeeded or not. This is +particularly useful together with B. + =back For further discussion on what is considered a successful command or not, see @@ -427,6 +433,9 @@ sub run { my $r = 0; my $e = 0; + die "OpenSSL::Test::run(): statusvar value not a scalar reference" + if $opts{statusvar} && ref($opts{statusvar}) ne "SCALAR"; + # In non-verbose, we want to shut up the command interpreter, in case # it has something to complain about. On VMS, it might complain both # on stdout and stderr @@ -445,11 +454,13 @@ sub run { # to make it easier to compare with a manual run of the command. if ($opts{capture}) { @r = `$prefix$cmd`; - $e = ($? & 0x7f) ? ($? & 0x7f)|0x80 : ($? >> 8); } else { system("$prefix$cmd"); - $e = ($? & 0x7f) ? ($? & 0x7f)|0x80 : ($? >> 8); - $r = $hooks{exit_checker}->($e); + } + $e = ($? & 0x7f) ? ($? & 0x7f)|0x80 : ($? >> 8); + $r = $hooks{exit_checker}->($e); + if ($opts{statusvar}) { + ${$opts{statusvar}} = $r; } if ($ENV{HARNESS_ACTIVE} && !$ENV{HARNESS_VERBOSE}) { -- GitLab