]> git.vpit.fr Git - perl/modules/indirect.git/blob - t/lib/VPIT/TestHelpers.pm
Update VPIT::TestHelpers to 32b27283
[perl/modules/indirect.git] / t / lib / VPIT / TestHelpers.pm
1 package VPIT::TestHelpers;
2
3 use strict;
4 use warnings;
5
6 use Config ();
7
8 =head1 NAME
9
10 VPIT::TestHelpers
11
12 =head1 SYNTAX
13
14     use VPIT::TestHelpers (
15      feature1 => \@feature1_args,
16      feature2 => \@feature2_args,
17     );
18
19 =cut
20
21 sub export_to_pkg {
22  my ($subs, $pkg) = @_;
23
24  while (my ($name, $code) = each %$subs) {
25   no strict 'refs';
26   *{$pkg.'::'.$name} = $code;
27  }
28
29  return 1;
30 }
31
32 sub sanitize_prefix {
33  my $prefix = shift;
34
35  if (defined $prefix) {
36   if (length $prefix and $prefix !~ /_$/) {
37    $prefix .= '_';
38   }
39  } else {
40   $prefix = '';
41  }
42
43  return $prefix;
44 }
45
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,
50 );
51
52 my %features = (
53  threads  => \&init_threads,
54  usleep   => \&init_usleep,
55  run_perl => \&init_run_perl,
56  capture  => \&init_capture,
57 );
58
59 sub import {
60  shift;
61  my @opts = @_;
62
63  my %exports = %default_exports;
64
65  for (my $i = 0; $i <= $#opts; ++$i) {
66   my $feature = $opts[$i];
67   next unless defined $feature;
68
69   my $args;
70   if ($i < $#opts and defined $opts[$i+1] and ref $opts[$i+1] eq 'ARRAY') {
71    ++$i;
72    $args = $opts[$i];
73   } else {
74    $args = [ ];
75   }
76
77   my $handler = $features{$feature};
78   die "Unknown feature '$feature'" unless defined $handler;
79
80   my %syms = $handler->(@$args);
81
82   $exports{$_} = $syms{$_} for sort keys %syms;
83  }
84
85  export_to_pkg \%exports => scalar caller;
86 }
87
88 my $test_sub = sub {
89  my $sub = shift;
90
91  my $stash;
92  if ($INC{'Test/Leaner.pm'}) {
93   $stash = \%Test::Leaner::;
94  } else {
95   require Test::More;
96   $stash = \%Test::More::;
97  }
98
99  my $glob = $stash->{$sub};
100  return $glob ? *$glob{CODE} : undef;
101 };
102
103 sub skip { $test_sub->('skip')->(@_) }
104
105 sub skip_all { $test_sub->('plan')->(skip_all => $_[0]) }
106
107 sub diag {
108  my $diag = $test_sub->('diag');
109  $diag->($_) for @_;
110 }
111
112 our $TODO;
113 local $TODO;
114
115 sub load {
116  my ($pkg, $ver, $imports) = @_;
117
118  my $spec = $ver && $ver !~ /^[0._]*$/ ? "$pkg $ver" : $pkg;
119  my $err;
120
121  local $@;
122  if (eval "use $spec (); 1") {
123   $ver = do { no strict 'refs'; ${"${pkg}::VERSION"} };
124   $ver = 'undef' unless defined $ver;
125
126   if ($imports) {
127    my @imports = @$imports;
128    my $caller  = (caller 1)[0];
129    local $@;
130    my $res = eval <<"IMPORTER";
131 package
132         $caller;
133 BEGIN { \$pkg->import(\@imports) }
134 1;
135 IMPORTER
136    $err = "Could not import '@imports' from $pkg $ver: $@" unless $res;
137   }
138  } else {
139   (my $file = "$pkg.pm") =~ s{::}{/}g;
140   delete $INC{$file};
141   $err = "Could not load $spec";
142  }
143
144  if ($err) {
145   return wantarray ? (0, $err) : 0;
146  } else {
147   diag "Using $pkg $ver";
148   return 1;
149  }
150 }
151
152 sub load_or_skip {
153  my ($pkg, $ver, $imports, $tests) = @_;
154
155  die 'You must specify how many tests to skip' unless defined $tests;
156
157  my ($loaded, $err) = load($pkg, $ver, $imports);
158  skip $err => $tests unless $loaded;
159
160  return $loaded;
161 }
162
163 sub load_or_skip_all {
164  my ($pkg, $ver, $imports) = @_;
165
166  my ($loaded, $err) = load($pkg, $ver, $imports);
167  skip_all $err unless $loaded;
168
169  return $loaded;
170 }
171
172 =head1 FEATURES
173
174 =head2 C<run_perl>
175
176 =over 4
177
178 =item *
179
180 Import :
181
182     use VPIT::TestHelpers run_perl => [ $p ]
183
184 where :
185
186 =over 8
187
188 =item -
189
190 C<$p> is prefixed to the constants exported by this feature (defaults to C<''>).
191
192 =back
193
194 =item *
195
196 Dependencies :
197
198 =over 8
199
200 =item -
201
202 L<File::Spec>
203
204 =back
205
206 =item *
207
208 Exports :
209
210 =over 8
211
212 =item -
213
214 C<run_perl $code>
215
216 =item -
217
218 C<run_perl_file $file>
219
220 =item -
221
222 C<RUN_PERL_FAILED> (possibly prefixed by C<$p>)
223
224 =back
225
226 =back
227
228 =cut
229
230 sub fresh_perl_env (&) {
231  my $handler = shift;
232
233  my ($SystemRoot, $PATH) = @ENV{qw<SystemRoot PATH>};
234  my $ld_name  = $Config::Config{ldlibpthname};
235  my $ldlibpth = $ENV{$ld_name};
236
237  local %ENV;
238  $ENV{$ld_name}   = $ldlibpth   if                      defined $ldlibpth;
239  $ENV{SystemRoot} = $SystemRoot if $^O eq 'MSWin32' and defined $SystemRoot;
240  $ENV{PATH}       = $PATH       if $^O eq 'cygwin'  and defined $PATH;
241
242  my $perl = $^X;
243  unless (-e $perl and -x $perl) {
244   $perl = $Config::Config{perlpath};
245   unless (-e $perl and -x $perl) {
246    return undef;
247   }
248  }
249
250  return $handler->($perl, '-T', map("-I$_", @INC));
251 }
252
253 sub init_run_perl {
254  my $p = sanitize_prefix(shift);
255
256  # This is only required for run_perl_file(), so it is not needed for the
257  # threads feature which only calls run_perl() - don't forget to update its
258  # requirements if this ever changes.
259  require File::Spec;
260
261  return (
262   run_perl              => \&run_perl,
263   run_perl_file         => \&run_perl_file,
264   "${p}RUN_PERL_FAILED" => sub () { 'Could not execute perl subprocess' },
265  );
266 }
267
268 sub run_perl {
269  my $code = shift;
270
271  if ($code =~ /"/) {
272   die 'Double quotes in evaluated code are not portable';
273  }
274
275  fresh_perl_env {
276   my ($perl, @perl_args) = @_;
277   system { $perl } $perl, @perl_args, '-e', $code;
278  };
279 }
280
281 sub run_perl_file {
282  my $file = shift;
283
284  $file = File::Spec->rel2abs($file);
285  unless (-e $file and -r _) {
286   die 'Could not run perl file';
287  }
288
289  fresh_perl_env {
290   my ($perl, @perl_args) = @_;
291   system { $perl } $perl, @perl_args, $file;
292  };
293 }
294
295 =head2 C<capture>
296
297 =over 4
298
299 =item *
300
301 Import :
302
303     use VPIT::TestHelpers capture => [ $p ];
304
305 where :
306
307 =over 8
308
309 =item -
310
311 C<$p> is prefixed to the constants exported by this feature (defaults to C<''>).
312
313 =back
314
315 =item *
316
317 Dependencies :
318
319 =over 8
320
321 =item -
322
323 Neither VMS nor OS/2
324
325 =item -
326
327 L<IO::Handle>
328
329 =item -
330
331 L<IO::Select>
332
333 =item -
334
335 L<IPC::Open3>
336
337 =item -
338
339 On MSWin32 : L<Socket>
340
341 =back
342
343 =item *
344
345 Exports :
346
347 =over 8
348
349 =item -
350
351 C<capture @command>
352
353 =item -
354
355 C<CAPTURE_FAILED $details> (possibly prefixed by C<$p>)
356
357 =item -
358
359 C<capture_perl $code>
360
361 =item -
362
363 C<CAPTURE_PERL_FAILED $details> (possibly prefixed by C<$p>)
364
365 =back
366
367 =back
368
369 =cut
370
371 sub init_capture {
372  my $p = sanitize_prefix(shift);
373
374  skip_all 'Cannot capture output on VMS'  if $^O eq 'VMS';
375  skip_all 'Cannot capture output on OS/2' if $^O eq 'os2';
376
377  load_or_skip_all 'IO::Handle', '0', [ ];
378  load_or_skip_all 'IO::Select', '0', [ ];
379  load_or_skip_all 'IPC::Open3', '0', [ ];
380  if ($^O eq 'MSWin32') {
381   load_or_skip_all 'Socket', '0', [ ];
382  }
383
384  return (
385   capture                   => \&capture,
386   "${p}CAPTURE_FAILED"      => \&capture_failed_msg,
387   capture_perl              => \&capture_perl,
388   "${p}CAPTURE_PERL_FAILED" => \&capture_perl_failed_msg,
389  );
390 }
391
392 # Inspired from IPC::Cmd
393
394 sub capture {
395  my @cmd = @_;
396
397  my $want = wantarray;
398
399  my $fail = sub {
400   my $err     = $!;
401   my $ext_err = $^O eq 'MSWin32' ? $^E : undef;
402
403   my $syscall = shift;
404   my $args    = join ', ', @_;
405
406   my $msg = "$syscall($args) failed: ";
407
408   if (defined $err) {
409    no warnings 'numeric';
410    my ($err_code, $err_str) = (int $err, "$err");
411    $msg .= "$err_str ($err_code)";
412   }
413
414   if (defined $ext_err) {
415    no warnings 'numeric';
416    my ($ext_err_code, $ext_err_str) = (int $ext_err, "$ext_err");
417    $msg .= ", $ext_err_str ($ext_err_code)";
418   }
419
420   die "$msg\n";
421  };
422
423  my ($status, $content_out, $content_err);
424
425  local $@;
426  my $ok = eval {
427   my ($pid, $out, $err);
428
429   if ($^O eq 'MSWin32') {
430    my $pipe = sub {
431     socketpair $_[0], $_[1],
432                &Socket::AF_UNIX, &Socket::SOCK_STREAM, &Socket::PF_UNSPEC
433                       or $fail->(qw<socketpair reader writer>);
434     shutdown $_[0], 1 or $fail->(qw<shutdown reader>);
435     shutdown $_[1], 0 or $fail->(qw<shutdown writer>);
436     return 1;
437    };
438    local (*IN_R,  *IN_W);
439    local (*OUT_R, *OUT_W);
440    local (*ERR_R, *ERR_W);
441    $pipe->(*IN_R,  *IN_W);
442    $pipe->(*OUT_R, *OUT_W);
443    $pipe->(*ERR_R, *ERR_W);
444
445    $pid = IPC::Open3::open3('>&IN_R', '<&OUT_W', '<&ERR_W', @cmd);
446
447    close *IN_W or $fail->(qw<close input>);
448    $out = *OUT_R;
449    $err = *ERR_R;
450   } else {
451    my $in = IO::Handle->new;
452    $out   = IO::Handle->new;
453    $out->autoflush(1);
454    $err   = IO::Handle->new;
455    $err->autoflush(1);
456
457    $pid = IPC::Open3::open3($in, $out, $err, @cmd);
458
459    close $in;
460   }
461
462   # Forward signals to the child (except SIGKILL)
463   my %sig_handlers;
464   foreach my $s (keys %SIG) {
465    $sig_handlers{$s} = sub {
466     kill "$s" => $pid;
467     $SIG{$s} = $sig_handlers{$s};
468    };
469   }
470   local $SIG{$_} = $sig_handlers{$_} for keys %SIG;
471
472   unless ($want) {
473    close $out or $fail->(qw<close output>);
474    close $err or $fail->(qw<close error>);
475    waitpid $pid, 0;
476    $status = $?;
477    return 1;
478   }
479
480   my $sel = IO::Select->new();
481   $sel->add($out, $err);
482
483   my $fd_out = fileno $out;
484   my $fd_err = fileno $err;
485
486   my %contents;
487   $contents{$fd_out} = '';
488   $contents{$fd_err} = '';
489
490   while (my @ready = $sel->can_read) {
491    for my $fh (@ready) {
492     my $buf;
493     my $bytes_read = sysread $fh, $buf, 4096;
494     if (not defined $bytes_read) {
495      $fail->('sysread', 'fd(' . fileno($fh) . ')');
496     } elsif ($bytes_read) {
497      $contents{fileno($fh)} .= $buf;
498     } else {
499      $sel->remove($fh);
500      close $fh or $fail->('close', 'fd(' . fileno($fh) . ')');
501      last unless $sel->count;
502     }
503    }
504   }
505
506   waitpid $pid, 0;
507   $status = $?;
508
509   if ($^O eq 'MSWin32') {
510    # Manual CRLF translation that couldn't be done with sysread.
511    s/\x0D\x0A/\n/g for values %contents;
512   }
513
514   $content_out = $contents{$fd_out};
515   $content_err = $contents{$fd_err};
516
517   1;
518  };
519
520  if ("$]" < 5.014 and $ok and ($status >> 8) == 255 and defined $content_err
521                   and $content_err =~ /^open3/) {
522   # Before perl commit 8960aa87 (between 5.12 and 5.14), exceptions in open3
523   # could be reported to STDERR instead of being propagated, so work around
524   # this.
525   $ok = 0;
526   $@  = $content_err;
527  }
528
529  if ($ok) {
530   return ($status, $content_out, $content_err);
531  } else {
532   my $err = $@;
533   chomp $err;
534   return (undef, $err);
535  }
536 }
537
538 sub capture_failed_msg {
539  my $details = shift;
540
541  my $msg = 'Could not capture command output';
542  $msg   .= " ($details)" if defined $details;
543
544  return $msg;
545 }
546
547 sub capture_perl {
548  my $code = shift;
549
550  if ($code =~ /"/) {
551   die 'Double quotes in evaluated code are not portable';
552  }
553
554  fresh_perl_env {
555   my @perl = @_;
556   capture @perl, '-e', $code;
557  };
558 }
559
560 sub capture_perl_failed_msg {
561  my $details = shift;
562
563  my $msg = 'Could not capture perl output';
564  $msg   .= " ($details)" if defined $details;
565
566  return $msg;
567 }
568
569 =head2 C<threads>
570
571 =over 4
572
573 =item *
574
575 Import :
576
577     use VPIT::TestHelpers threads => [
578      $pkg, $threadsafe_var, $force_var
579     ];
580
581 where :
582
583 =over 8
584
585 =item -
586
587 C<$pkg> is the target package name that will be exercised by this test ;
588
589 =item -
590
591 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>) ;
592
593 =item -
594
595 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>).
596
597 =back
598
599 =item *
600
601 Dependencies :
602
603 =over 8
604
605 =item -
606
607 C<perl> 5.13.4
608
609 =item -
610
611 L<POSIX>
612
613 =item -
614
615 L<threads> 1.67
616
617 =item -
618
619 L<threads::shared> 1.14
620
621 =back
622
623 =item *
624
625 Exports :
626
627 =over 8
628
629 =item -
630
631 C<spawn $coderef>
632
633 =back
634
635 =item *
636
637 Notes :
638
639 =over 8
640
641 =item -
642
643 C<< exit => 'threads_only' >> is passed to C<< threads->import >>.
644
645 =back
646
647 =back
648
649 =cut
650
651 sub init_threads {
652  my ($pkg, $threadsafe_var, $force_var) = @_;
653
654  skip_all 'This perl wasn\'t built to support threads'
655                                             unless $Config::Config{useithreads};
656
657  if (defined $pkg and defined $threadsafe_var) {
658   my $threadsafe;
659   # run_perl() doesn't actually require anything
660   my $stat = run_perl("require POSIX; require $pkg; exit($threadsafe_var ? POSIX::EXIT_SUCCESS() : POSIX::EXIT_FAILURE())");
661   if (defined $stat) {
662    require POSIX;
663    my $res  = $stat >> 8;
664    if ($res == POSIX::EXIT_SUCCESS()) {
665     $threadsafe = 1;
666    } elsif ($res == POSIX::EXIT_FAILURE()) {
667     $threadsafe = !1;
668    }
669   }
670   if (not defined $threadsafe) {
671    skip_all "Could not detect if $pkg is thread safe or not";
672   } elsif (not $threadsafe) {
673    skip_all "This $pkg is not thread safe";
674   }
675  }
676
677  $force_var = 'PERL_FORCE_TEST_THREADS' unless defined $force_var;
678  my $force  = $ENV{$force_var} ? 1 : !1;
679  skip_all 'perl 5.13.4 required to test thread safety'
680                                              unless $force or "$]" >= 5.013_004;
681
682  unless ($INC{'threads.pm'}) {
683   my $test_module;
684   if ($INC{'Test/Leaner.pm'}) {
685    $test_module = 'Test::Leaner';
686   } elsif ($INC{'Test/More.pm'}) {
687    $test_module = 'Test::More';
688   }
689   die "$test_module was loaded too soon" if defined $test_module;
690  }
691
692  load_or_skip_all 'threads',         $force ? '0' : '1.67', [
693   exit => 'threads_only',
694  ];
695  load_or_skip_all 'threads::shared', $force ? '0' : '1.14', [ ];
696
697  diag "Threads testing forced by \$ENV{$force_var}" if $force;
698
699  return spawn => \&spawn;
700 }
701
702 sub spawn {
703  local $@;
704  my @diag;
705  my $thread = eval {
706   local $SIG{__WARN__} = sub { push @diag, "Thread creation warning: @_" };
707   threads->create(@_);
708  };
709  push @diag, "Thread creation error: $@" if $@;
710  diag @diag;
711  return $thread ? $thread : ();
712 }
713
714 =head2 C<usleep>
715
716 =over 4
717
718 =item *
719
720 Import :
721
722     use VPIT::TestHelpers 'usleep' => [ @impls ];
723
724 where :
725
726 =over 8
727
728 =item -
729
730 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.
731 When the list is empty, it defaults to all of them.
732
733 =back
734
735 =item *
736
737 Dependencies : none
738
739 =item *
740
741 Exports :
742
743 =over 8
744
745 =item -
746
747 C<usleep $microseconds>
748
749 =back
750
751 =back
752
753 =cut
754
755 sub init_usleep {
756  my (@impls) = @_;
757
758  my %impls = (
759   'Time::HiRes' => sub {
760    if (do { local $@; eval { require Time::HiRes; 1 } }) {
761     defined and diag "Using usleep() from Time::HiRes $_"
762                                                       for $Time::HiRes::VERSION;
763     return \&Time::HiRes::usleep;
764    } else {
765     return undef;
766    }
767   },
768   'select' => sub {
769    if ($Config::Config{d_select}) {
770     diag 'Using select()-based fallback usleep()';
771     return sub ($) {
772      my $s = $_[0];
773      my $r = 0;
774      while ($s > 0) {
775       my ($found, $t) = select(undef, undef, undef, $s / 1e6);
776       last unless defined $t;
777       $t  = int($t * 1e6);
778       $s -= $t;
779       $r += $t;
780      }
781      return $r;
782     };
783    } else {
784     return undef;
785    }
786   },
787   'sleep' => sub {
788    diag 'Using sleep()-based fallback usleep()';
789    return sub ($) {
790     my $ms = int $_[0];
791     my $s  = int($ms / 1e6) + ($ms % 1e6 == 0 ? 0 : 1);
792     my $t  = sleep $s;
793     return $t * 1e6;
794    };
795   },
796  );
797
798  @impls = qw<Time::HiRes select sleep> unless @impls;
799
800  my $usleep;
801  for my $impl (@impls) {
802   next unless defined $impl and $impls{$impl};
803   $usleep = $impls{$impl}->();
804   last if defined $usleep;
805  }
806
807  skip_all "Could not find a suitable usleep() implementation among: @impls"
808                                                                  unless $usleep;
809
810  return usleep => $usleep;
811 }
812
813 =head1 CLASSES
814
815 =head2 C<VPIT::TestHelpers::Guard>
816
817 Syntax :
818
819     {
820      my $guard = VPIT::TestHelpers::Guard->new($coderef);
821      ...
822     } # $codref called here
823
824 =cut
825
826 package VPIT::TestHelpers::Guard;
827
828 sub new {
829  my ($class, $code) = @_;
830
831  bless { code => $code }, $class;
832 }
833
834 sub DESTROY { $_[0]->{code}->() }
835
836 =head1 AUTHOR
837
838 Vincent Pit, C<< <perl at profvince.com> >>, L<http://www.profvince.com>.
839
840 =head1 COPYRIGHT & LICENSE
841
842 Copyright 2012,2013,2014,2015 Vincent Pit, all rights reserved.
843
844 This program is free software; you can redistribute it and/or modify it under the same terms as Perl itself.
845
846 =cut
847
848 1;