From: Vincent Pit Date: Tue, 12 Jul 2016 14:47:38 +0000 (-0300) Subject: Implement run_perl_file() X-Git-Url: http://git.vpit.fr/?a=commitdiff_plain;h=32b272838d5cd4620aec5d8b2c6d2fea43e8e0a9;p=perl%2Fmodules%2FVPIT-TestHelpers.git Implement run_perl_file() --- diff --git a/lib/VPIT/TestHelpers.pm b/lib/VPIT/TestHelpers.pm index 75ca216..10550ee 100644 --- a/lib/VPIT/TestHelpers.pm +++ b/lib/VPIT/TestHelpers.pm @@ -193,7 +193,15 @@ C<$p> is prefixed to the constants exported by this feature (defaults to C<''>). =item * -Dependencies : none +Dependencies : + +=over 8 + +=item - + +L + +=back =item * @@ -207,6 +215,10 @@ C =item - +C + +=item - + C (possibly prefixed by C<$p>) =back @@ -241,8 +253,14 @@ sub fresh_perl_env (&) { sub init_run_perl { my $p = sanitize_prefix(shift); + # This is only required for run_perl_file(), so it is not needed for the + # threads feature which only calls run_perl() - don't forget to update its + # requirements if this ever changes. + require File::Spec; + return ( run_perl => \&run_perl, + run_perl_file => \&run_perl_file, "${p}RUN_PERL_FAILED" => sub () { 'Could not execute perl subprocess' }, ); } @@ -260,6 +278,20 @@ sub run_perl { }; } +sub run_perl_file { + my $file = shift; + + $file = File::Spec->rel2abs($file); + unless (-e $file and -r _) { + die 'Could not run perl file'; + } + + fresh_perl_env { + my ($perl, @perl_args) = @_; + system { $perl } $perl, @perl_args, $file; + }; +} + =head2 C =over 4 @@ -624,6 +656,7 @@ sub init_threads { if (defined $pkg and defined $threadsafe_var) { my $threadsafe; + # run_perl() doesn't actually require anything my $stat = run_perl("require POSIX; require $pkg; exit($threadsafe_var ? POSIX::EXIT_SUCCESS() : POSIX::EXIT_FAILURE())"); if (defined $stat) { require POSIX; diff --git a/t/30-run_perl.t b/t/30-run_perl.t index bfc7bbb..21837be 100644 --- a/t/30-run_perl.t +++ b/t/30-run_perl.t @@ -5,7 +5,7 @@ use warnings; use VPIT::TestHelpers 'run_perl' => [ 'VTH' ]; -use Test::More tests => 5 + 2; +use Test::More tests => 5 + 2 + 2; use Config; use File::Temp; @@ -71,6 +71,16 @@ for my $os (qw) { is $got, $exp, "run_perl preserving $os ENV vars"; } +{ + local $@; + my $stat = eval { + run_perl_file 't/testcases/run_perl_file.pl'; + }; + $stat = $stat >> 8 if defined $stat; + is $@, '', 'run_perl_file() does not croak'; + is $stat, 123, 'run_perl_file() captures the exit value'; +} + { local $@; eval { diff --git a/t/testcases/run_perl_file.pl b/t/testcases/run_perl_file.pl new file mode 100644 index 0000000..8b5f746 --- /dev/null +++ b/t/testcases/run_perl_file.pl @@ -0,0 +1 @@ +exit 123;