1 package VPIT::TestHelpers;
14 use VPIT::TestHelpers (
15 feature1 => \@feature1_args,
16 feature2 => \@feature2_args,
22 my ($subs, $pkg) = @_;
24 while (my ($name, $code) = each %$subs) {
26 *{$pkg.'::'.$name} = $code;
35 if (defined $prefix) {
36 if (length $prefix and $prefix !~ /_$/) {
46 my %default_exports = (
47 load_or_skip => \&load_or_skip,
48 load_or_skip_all => \&load_or_skip_all,
49 skip_all => \&skip_all,
53 threads => \&init_threads,
54 usleep => \&init_usleep,
55 run_perl => \&init_run_perl,
56 capture => \&init_capture,
63 my %exports = %default_exports;
65 for (my $i = 0; $i <= $#opts; ++$i) {
66 my $feature = $opts[$i];
67 next unless defined $feature;
70 if ($i < $#opts and defined $opts[$i+1] and ref $opts[$i+1] eq 'ARRAY') {
77 my $handler = $features{$feature};
78 die "Unknown feature '$feature'" unless defined $handler;
80 my %syms = $handler->(@$args);
82 $exports{$_} = $syms{$_} for sort keys %syms;
85 export_to_pkg \%exports => scalar caller;
92 if ($INC{'Test/Leaner.pm'}) {
93 $stash = \%Test::Leaner::;
96 $stash = \%Test::More::;
99 my $glob = $stash->{$sub};
100 return ref \$glob eq 'GLOB' ? *$glob{CODE}
101 : ref $glob eq 'CODE' ? $glob
105 sub skip { $test_sub->('skip')->(@_) }
107 sub skip_all { $test_sub->('plan')->(skip_all => $_[0]) }
110 my $diag = $test_sub->('diag');
118 my ($pkg, $ver, $imports) = @_;
120 my $spec = $ver && $ver !~ /^[0._]*$/ ? "$pkg $ver" : $pkg;
124 if (eval "use $spec (); 1") {
125 $ver = do { no strict 'refs'; ${"${pkg}::VERSION"} };
126 $ver = 'undef' unless defined $ver;
129 my @imports = @$imports;
130 my $caller = (caller 1)[0];
132 my $res = eval <<"IMPORTER";
135 BEGIN { \$pkg->import(\@imports) }
138 $err = "Could not import '@imports' from $pkg $ver: $@" unless $res;
141 (my $file = "$pkg.pm") =~ s{::}{/}g;
143 $err = "Could not load $spec";
147 return wantarray ? (0, $err) : 0;
149 diag "Using $pkg $ver";
155 my ($pkg, $ver, $imports, $tests) = @_;
157 die 'You must specify how many tests to skip' unless defined $tests;
159 my ($loaded, $err) = load($pkg, $ver, $imports);
160 skip $err => $tests unless $loaded;
165 sub load_or_skip_all {
166 my ($pkg, $ver, $imports) = @_;
168 my ($loaded, $err) = load($pkg, $ver, $imports);
169 skip_all $err unless $loaded;
184 use VPIT::TestHelpers run_perl => [ $p ]
192 C<$p> is prefixed to the constants exported by this feature (defaults to C<''>).
220 C<run_perl_file $file>
224 C<RUN_PERL_FAILED> (possibly prefixed by C<$p>)
232 sub fresh_perl_env (&) {
235 my ($SystemRoot, $PATH) = @ENV{qw<SystemRoot PATH>};
236 my $ld_name = $Config::Config{ldlibpthname};
237 my $ldlibpth = $ENV{$ld_name};
240 $ENV{$ld_name} = $ldlibpth if defined $ldlibpth;
241 $ENV{SystemRoot} = $SystemRoot if $^O eq 'MSWin32' and defined $SystemRoot;
242 $ENV{PATH} = $PATH if $^O eq 'cygwin' and defined $PATH;
245 unless (-e $perl and -x $perl) {
246 $perl = $Config::Config{perlpath};
247 unless (-e $perl and -x $perl) {
252 return $handler->($perl, '-T', map("-I$_", @INC));
256 my $p = sanitize_prefix(shift);
258 # This is only required for run_perl_file(), so it is not needed for the
259 # threads feature which only calls run_perl() - don't forget to update its
260 # requirements if this ever changes.
264 run_perl => \&run_perl,
265 run_perl_file => \&run_perl_file,
266 "${p}RUN_PERL_FAILED" => sub () { 'Could not execute perl subprocess' },
274 die 'Double quotes in evaluated code are not portable';
278 my ($perl, @perl_args) = @_;
279 system { $perl } $perl, @perl_args, '-e', $code;
286 $file = File::Spec->rel2abs($file);
287 unless (-e $file and -r _) {
288 die 'Could not run perl file';
292 my ($perl, @perl_args) = @_;
293 system { $perl } $perl, @perl_args, $file;
305 use VPIT::TestHelpers capture => [ $p ];
313 C<$p> is prefixed to the constants exported by this feature (defaults to C<''>).
341 On MSWin32 : L<Socket>
357 C<CAPTURE_FAILED $details> (possibly prefixed by C<$p>)
361 C<capture_perl $code>
365 C<CAPTURE_PERL_FAILED $details> (possibly prefixed by C<$p>)
374 my $p = sanitize_prefix(shift);
376 skip_all 'Cannot capture output on VMS' if $^O eq 'VMS';
377 skip_all 'Cannot capture output on OS/2' if $^O eq 'os2';
379 load_or_skip_all 'IO::Handle', '0', [ ];
380 load_or_skip_all 'IO::Select', '0', [ ];
381 load_or_skip_all 'IPC::Open3', '0', [ ];
382 if ($^O eq 'MSWin32') {
383 load_or_skip_all 'Socket', '0', [ ];
387 capture => \&capture,
388 "${p}CAPTURE_FAILED" => \&capture_failed_msg,
389 capture_perl => \&capture_perl,
390 "${p}CAPTURE_PERL_FAILED" => \&capture_perl_failed_msg,
394 # Inspired from IPC::Cmd
399 my $want = wantarray;
403 my $ext_err = $^O eq 'MSWin32' ? $^E : undef;
406 my $args = join ', ', @_;
408 my $msg = "$syscall($args) failed: ";
411 no warnings 'numeric';
412 my ($err_code, $err_str) = (int $err, "$err");
413 $msg .= "$err_str ($err_code)";
416 if (defined $ext_err) {
417 no warnings 'numeric';
418 my ($ext_err_code, $ext_err_str) = (int $ext_err, "$ext_err");
419 $msg .= ", $ext_err_str ($ext_err_code)";
425 my ($status, $content_out, $content_err);
429 my ($pid, $out, $err);
431 if ($^O eq 'MSWin32') {
433 socketpair $_[0], $_[1],
434 &Socket::AF_UNIX, &Socket::SOCK_STREAM, &Socket::PF_UNSPEC
435 or $fail->(qw<socketpair reader writer>);
436 shutdown $_[0], 1 or $fail->(qw<shutdown reader>);
437 shutdown $_[1], 0 or $fail->(qw<shutdown writer>);
440 local (*IN_R, *IN_W);
441 local (*OUT_R, *OUT_W);
442 local (*ERR_R, *ERR_W);
443 $pipe->(*IN_R, *IN_W);
444 $pipe->(*OUT_R, *OUT_W);
445 $pipe->(*ERR_R, *ERR_W);
447 $pid = IPC::Open3::open3('>&IN_R', '<&OUT_W', '<&ERR_W', @cmd);
449 close *IN_W or $fail->(qw<close input>);
453 my $in = IO::Handle->new;
454 $out = IO::Handle->new;
456 $err = IO::Handle->new;
459 $pid = IPC::Open3::open3($in, $out, $err, @cmd);
464 # Forward signals to the child (except SIGKILL)
466 foreach my $s (keys %SIG) {
467 $sig_handlers{$s} = sub {
469 $SIG{$s} = $sig_handlers{$s};
472 local $SIG{$_} = $sig_handlers{$_} for keys %SIG;
475 close $out or $fail->(qw<close output>);
476 close $err or $fail->(qw<close error>);
482 my $sel = IO::Select->new();
483 $sel->add($out, $err);
485 my $fd_out = fileno $out;
486 my $fd_err = fileno $err;
489 $contents{$fd_out} = '';
490 $contents{$fd_err} = '';
492 while (my @ready = $sel->can_read) {
493 for my $fh (@ready) {
495 my $bytes_read = sysread $fh, $buf, 4096;
496 if (not defined $bytes_read) {
497 $fail->('sysread', 'fd(' . fileno($fh) . ')');
498 } elsif ($bytes_read) {
499 $contents{fileno($fh)} .= $buf;
502 close $fh or $fail->('close', 'fd(' . fileno($fh) . ')');
503 last unless $sel->count;
511 if ($^O eq 'MSWin32') {
512 # Manual CRLF translation that couldn't be done with sysread.
513 s/\x0D\x0A/\n/g for values %contents;
516 $content_out = $contents{$fd_out};
517 $content_err = $contents{$fd_err};
522 if ("$]" < 5.014 and $ok and ($status >> 8) == 255 and defined $content_err
523 and $content_err =~ /^open3/) {
524 # Before perl commit 8960aa87 (between 5.12 and 5.14), exceptions in open3
525 # could be reported to STDERR instead of being propagated, so work around
532 return ($status, $content_out, $content_err);
536 return (undef, $err);
540 sub capture_failed_msg {
543 my $msg = 'Could not capture command output';
544 $msg .= " ($details)" if defined $details;
553 die 'Double quotes in evaluated code are not portable';
558 capture @perl, '-e', $code;
562 sub capture_perl_failed_msg {
565 my $msg = 'Could not capture perl output';
566 $msg .= " ($details)" if defined $details;
579 use VPIT::TestHelpers threads => [
580 $pkg, $threadsafe_var, $force_var
589 C<$pkg> is the target package name that will be exercised by this test ;
593 C<$threadsafe_var> is the name of an optional variable in C<$pkg> that evaluates to true if and only if the module claims to be thread safe (not checked if either C<$threadsafe_var> or C<$pkg> is C<undef>) ;
597 C<$force_var> is the name of the environment variable that can be used to force the thread tests (defaults to C<PERL_FORCE_TEST_THREADS>).
621 L<threads::shared> 1.14
645 C<< exit => 'threads_only' >> is passed to C<< threads->import >>.
654 my ($pkg, $threadsafe_var, $force_var) = @_;
656 skip_all 'This perl wasn\'t built to support threads'
657 unless $Config::Config{useithreads};
659 if (defined $pkg and defined $threadsafe_var) {
661 # run_perl() doesn't actually require anything
662 my $stat = run_perl("require POSIX; require $pkg; exit($threadsafe_var ? POSIX::EXIT_SUCCESS() : POSIX::EXIT_FAILURE())");
665 my $res = $stat >> 8;
666 if ($res == POSIX::EXIT_SUCCESS()) {
668 } elsif ($res == POSIX::EXIT_FAILURE()) {
672 if (not defined $threadsafe) {
673 skip_all "Could not detect if $pkg is thread safe or not";
674 } elsif (not $threadsafe) {
675 skip_all "This $pkg is not thread safe";
679 $force_var = 'PERL_FORCE_TEST_THREADS' unless defined $force_var;
680 my $force = $ENV{$force_var} ? 1 : !1;
681 skip_all 'perl 5.13.4 required to test thread safety'
682 unless $force or "$]" >= 5.013_004;
684 unless ($INC{'threads.pm'}) {
686 if ($INC{'Test/Leaner.pm'}) {
687 $test_module = 'Test::Leaner';
688 } elsif ($INC{'Test/More.pm'}) {
689 $test_module = 'Test::More';
691 die "$test_module was loaded too soon" if defined $test_module;
694 load_or_skip_all 'threads', $force ? '0' : '1.67', [
695 exit => 'threads_only',
697 load_or_skip_all 'threads::shared', $force ? '0' : '1.14', [ ];
699 diag "Threads testing forced by \$ENV{$force_var}" if $force;
701 return spawn => \&spawn;
708 local $SIG{__WARN__} = sub { push @diag, "Thread creation warning: @_" };
711 push @diag, "Thread creation error: $@" if $@;
713 return $thread ? $thread : ();
724 use VPIT::TestHelpers 'usleep' => [ @impls ];
732 C<@impls> is the list of desired implementations (which may be C<'Time::HiRes'>, C<'select'> or C<'sleep'>), in the order they should be checked.
733 When the list is empty, it defaults to all of them.
749 C<usleep $microseconds>
761 'Time::HiRes' => sub {
762 if (do { local $@; eval { require Time::HiRes; 1 } }) {
763 defined and diag "Using usleep() from Time::HiRes $_"
764 for $Time::HiRes::VERSION;
765 return \&Time::HiRes::usleep;
771 if ($Config::Config{d_select}) {
772 diag 'Using select()-based fallback usleep()';
777 my ($found, $t) = select(undef, undef, undef, $s / 1e6);
778 last unless defined $t;
790 diag 'Using sleep()-based fallback usleep()';
793 my $s = int($ms / 1e6) + ($ms % 1e6 == 0 ? 0 : 1);
800 @impls = qw<Time::HiRes select sleep> unless @impls;
803 for my $impl (@impls) {
804 next unless defined $impl and $impls{$impl};
805 $usleep = $impls{$impl}->();
806 last if defined $usleep;
809 skip_all "Could not find a suitable usleep() implementation among: @impls"
812 return usleep => $usleep;
817 =head2 C<VPIT::TestHelpers::Guard>
822 my $guard = VPIT::TestHelpers::Guard->new($coderef);
824 } # $codref called here
828 package VPIT::TestHelpers::Guard;
831 my ($class, $code) = @_;
833 bless { code => $code }, $class;
836 sub DESTROY { $_[0]->{code}->() }
840 Vincent Pit C<< <vpit at cpan.org> >>.
842 =head1 COPYRIGHT & LICENSE
844 Copyright 2012,2013,2014,2015,2019 Vincent Pit, all rights reserved.
846 This program is free software; you can redistribute it and/or modify it under the same terms as Perl itself.