]> git.vpit.fr Git - perl/modules/indirect.git/blob - t/lib/VPIT/TestHelpers.pm
Update VPIT::TestHelpers to 3ba261a5
[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 : none
197
198 =item *
199
200 Exports :
201
202 =over 8
203
204 =item -
205
206 C<run_perl $code>
207
208 =item -
209
210 C<RUN_PERL_FAILED> (possibly prefixed by C<$p>)
211
212 =back
213
214 =back
215
216 =cut
217
218 sub fresh_perl_env (&) {
219  my $handler = shift;
220
221  my ($SystemRoot, $PATH) = @ENV{qw<SystemRoot PATH>};
222  my $ld_name  = $Config::Config{ldlibpthname};
223  my $ldlibpth = $ENV{$ld_name};
224
225  local %ENV;
226  $ENV{$ld_name}   = $ldlibpth   if                      defined $ldlibpth;
227  $ENV{SystemRoot} = $SystemRoot if $^O eq 'MSWin32' and defined $SystemRoot;
228  $ENV{PATH}       = $PATH       if $^O eq 'cygwin'  and defined $PATH;
229
230  my $perl = $^X;
231  unless (-e $perl and -x $perl) {
232   $perl = $Config::Config{perlpath};
233   unless (-e $perl and -x $perl) {
234    return undef;
235   }
236  }
237
238  return $handler->($perl, '-T', map("-I$_", @INC));
239 }
240
241 sub init_run_perl {
242  my $p = sanitize_prefix(shift);
243
244  return (
245   run_perl              => \&run_perl,
246   "${p}RUN_PERL_FAILED" => sub () { 'Could not execute perl subprocess' },
247  );
248 }
249
250 sub run_perl {
251  my $code = shift;
252
253  if ($code =~ /"/) {
254   die 'Double quotes in evaluated code are not portable';
255  }
256
257  fresh_perl_env {
258   my ($perl, @perl_args) = @_;
259   system { $perl } $perl, @perl_args, '-e', $code;
260  };
261 }
262
263 =head2 C<capture>
264
265 =over 4
266
267 =item *
268
269 Import :
270
271     use VPIT::TestHelpers capture => [ $p ];
272
273 where :
274
275 =over 8
276
277 =item -
278
279 C<$p> is prefixed to the constants exported by this feature (defaults to C<''>).
280
281 =back
282
283 =item *
284
285 Dependencies :
286
287 =over 8
288
289 =item -
290
291 Neither VMS nor OS/2
292
293 =item -
294
295 L<IO::Handle>
296
297 =item -
298
299 L<IO::Select>
300
301 =item -
302
303 L<IPC::Open3>
304
305 =item -
306
307 On MSWin32 : L<Socket>
308
309 =back
310
311 =item *
312
313 Exports :
314
315 =over 8
316
317 =item -
318
319 C<capture @command>
320
321 =item -
322
323 C<CAPTURE_FAILED $details> (possibly prefixed by C<$p>)
324
325 =item -
326
327 C<capture_perl $code>
328
329 =item -
330
331 C<CAPTURE_PERL_FAILED $details> (possibly prefixed by C<$p>)
332
333 =back
334
335 =back
336
337 =cut
338
339 sub init_capture {
340  my $p = sanitize_prefix(shift);
341
342  skip_all 'Cannot capture output on VMS'  if $^O eq 'VMS';
343  skip_all 'Cannot capture output on OS/2' if $^O eq 'os2';
344
345  load_or_skip_all 'IO::Handle', '0', [ ];
346  load_or_skip_all 'IO::Select', '0', [ ];
347  load_or_skip_all 'IPC::Open3', '0', [ ];
348  if ($^O eq 'MSWin32') {
349   load_or_skip_all 'Socket', '0', [ ];
350  }
351
352  return (
353   capture                   => \&capture,
354   "${p}CAPTURE_FAILED"      => \&capture_failed_msg,
355   capture_perl              => \&capture_perl,
356   "${p}CAPTURE_PERL_FAILED" => \&capture_perl_failed_msg,
357  );
358 }
359
360 # Inspired from IPC::Cmd
361
362 sub capture {
363  my @cmd = @_;
364
365  my $want = wantarray;
366
367  my $fail = sub {
368   my $err     = $!;
369   my $ext_err = $^O eq 'MSWin32' ? $^E : undef;
370
371   my $syscall = shift;
372   my $args    = join ', ', @_;
373
374   my $msg = "$syscall($args) failed: ";
375
376   if (defined $err) {
377    no warnings 'numeric';
378    my ($err_code, $err_str) = (int $err, "$err");
379    $msg .= "$err_str ($err_code)";
380   }
381
382   if (defined $ext_err) {
383    no warnings 'numeric';
384    my ($ext_err_code, $ext_err_str) = (int $ext_err, "$ext_err");
385    $msg .= ", $ext_err_str ($ext_err_code)";
386   }
387
388   die "$msg\n";
389  };
390
391  my ($status, $content_out, $content_err);
392
393  local $@;
394  my $ok = eval {
395   my ($pid, $out, $err);
396
397   if ($^O eq 'MSWin32') {
398    my $pipe = sub {
399     socketpair $_[0], $_[1],
400                &Socket::AF_UNIX, &Socket::SOCK_STREAM, &Socket::PF_UNSPEC
401                       or $fail->(qw<socketpair reader writer>);
402     shutdown $_[0], 1 or $fail->(qw<shutdown reader>);
403     shutdown $_[1], 0 or $fail->(qw<shutdown writer>);
404     return 1;
405    };
406    local (*IN_R,  *IN_W);
407    local (*OUT_R, *OUT_W);
408    local (*ERR_R, *ERR_W);
409    $pipe->(*IN_R,  *IN_W);
410    $pipe->(*OUT_R, *OUT_W);
411    $pipe->(*ERR_R, *ERR_W);
412
413    $pid = IPC::Open3::open3('>&IN_R', '<&OUT_W', '<&ERR_W', @cmd);
414
415    close *IN_W or $fail->(qw<close input>);
416    $out = *OUT_R;
417    $err = *ERR_R;
418   } else {
419    my $in = IO::Handle->new;
420    $out   = IO::Handle->new;
421    $out->autoflush(1);
422    $err   = IO::Handle->new;
423    $err->autoflush(1);
424
425    $pid = IPC::Open3::open3($in, $out, $err, @cmd);
426
427    close $in;
428   }
429
430   # Forward signals to the child (except SIGKILL)
431   my %sig_handlers;
432   foreach my $s (keys %SIG) {
433    $sig_handlers{$s} = sub {
434     kill "$s" => $pid;
435     $SIG{$s} = $sig_handlers{$s};
436    };
437   }
438   local $SIG{$_} = $sig_handlers{$_} for keys %SIG;
439
440   unless ($want) {
441    close $out or $fail->(qw<close output>);
442    close $err or $fail->(qw<close error>);
443    waitpid $pid, 0;
444    $status = $?;
445    return 1;
446   }
447
448   my $sel = IO::Select->new();
449   $sel->add($out, $err);
450
451   my $fd_out = fileno $out;
452   my $fd_err = fileno $err;
453
454   my %contents;
455   $contents{$fd_out} = '';
456   $contents{$fd_err} = '';
457
458   while (my @ready = $sel->can_read) {
459    for my $fh (@ready) {
460     my $buf;
461     my $bytes_read = sysread $fh, $buf, 4096;
462     if (not defined $bytes_read) {
463      $fail->('sysread', 'fd(' . fileno($fh) . ')');
464     } elsif ($bytes_read) {
465      $contents{fileno($fh)} .= $buf;
466     } else {
467      $sel->remove($fh);
468      close $fh or $fail->('close', 'fd(' . fileno($fh) . ')');
469      last unless $sel->count;
470     }
471    }
472   }
473
474   waitpid $pid, 0;
475   $status = $?;
476
477   if ($^O eq 'MSWin32') {
478    # Manual CRLF translation that couldn't be done with sysread.
479    s/\x0D\x0A/\n/g for values %contents;
480   }
481
482   $content_out = $contents{$fd_out};
483   $content_err = $contents{$fd_err};
484
485   1;
486  };
487
488  if ("$]" < 5.014 and $ok and ($status >> 8) == 255 and defined $content_err
489                   and $content_err =~ /^open3/) {
490   # Before perl commit 8960aa87 (between 5.12 and 5.14), exceptions in open3
491   # could be reported to STDERR instead of being propagated, so work around
492   # this.
493   $ok = 0;
494   $@  = $content_err;
495  }
496
497  if ($ok) {
498   return ($status, $content_out, $content_err);
499  } else {
500   my $err = $@;
501   chomp $err;
502   return (undef, $err);
503  }
504 }
505
506 sub capture_failed_msg {
507  my $details = shift;
508
509  my $msg = 'Could not capture command output';
510  $msg   .= " ($details)" if defined $details;
511
512  return $msg;
513 }
514
515 sub capture_perl {
516  my $code = shift;
517
518  if ($code =~ /"/) {
519   die 'Double quotes in evaluated code are not portable';
520  }
521
522  fresh_perl_env {
523   my @perl = @_;
524   capture @perl, '-e', $code;
525  };
526 }
527
528 sub capture_perl_failed_msg {
529  my $details = shift;
530
531  my $msg = 'Could not capture perl output';
532  $msg   .= " ($details)" if defined $details;
533
534  return $msg;
535 }
536
537 =head2 C<threads>
538
539 =over 4
540
541 =item *
542
543 Import :
544
545     use VPIT::TestHelpers threads => [
546      $pkg, $threadsafe_var, $force_var
547     ];
548
549 where :
550
551 =over 8
552
553 =item -
554
555 C<$pkg> is the target package name that will be exercised by this test ;
556
557 =item -
558
559 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>) ;
560
561 =item -
562
563 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>).
564
565 =back
566
567 =item *
568
569 Dependencies :
570
571 =over 8
572
573 =item -
574
575 C<perl> 5.13.4
576
577 =item -
578
579 L<POSIX>
580
581 =item -
582
583 L<threads> 1.67
584
585 =item -
586
587 L<threads::shared> 1.14
588
589 =back
590
591 =item *
592
593 Exports :
594
595 =over 8
596
597 =item -
598
599 C<spawn $coderef>
600
601 =back
602
603 =back
604
605 =cut
606
607 sub init_threads {
608  my ($pkg, $threadsafe_var, $force_var) = @_;
609
610  skip_all 'This perl wasn\'t built to support threads'
611                                             unless $Config::Config{useithreads};
612
613  if (defined $pkg and defined $threadsafe_var) {
614   my $threadsafe;
615   my $stat = run_perl("require POSIX; require $pkg; exit($threadsafe_var ? POSIX::EXIT_SUCCESS() : POSIX::EXIT_FAILURE())");
616   if (defined $stat) {
617    require POSIX;
618    my $res  = $stat >> 8;
619    if ($res == POSIX::EXIT_SUCCESS()) {
620     $threadsafe = 1;
621    } elsif ($res == POSIX::EXIT_FAILURE()) {
622     $threadsafe = !1;
623    }
624   }
625   if (not defined $threadsafe) {
626    skip_all "Could not detect if $pkg is thread safe or not";
627   } elsif (not $threadsafe) {
628    skip_all "This $pkg is not thread safe";
629   }
630  }
631
632  $force_var = 'PERL_FORCE_TEST_THREADS' unless defined $force_var;
633  my $force  = $ENV{$force_var} ? 1 : !1;
634  skip_all 'perl 5.13.4 required to test thread safety'
635                                              unless $force or "$]" >= 5.013_004;
636
637  unless ($INC{'threads.pm'}) {
638   my $test_module;
639   if ($INC{'Test/Leaner.pm'}) {
640    $test_module = 'Test::Leaner';
641   } elsif ($INC{'Test/More.pm'}) {
642    $test_module = 'Test::More';
643   }
644   die "$test_module was loaded too soon" if defined $test_module;
645  }
646
647  load_or_skip_all 'threads',         $force ? '0' : '1.67', [ ];
648  load_or_skip_all 'threads::shared', $force ? '0' : '1.14', [ ];
649
650  diag "Threads testing forced by \$ENV{$force_var}" if $force;
651
652  return spawn => \&spawn;
653 }
654
655 sub spawn {
656  local $@;
657  my @diag;
658  my $thread = eval {
659   local $SIG{__WARN__} = sub { push @diag, "Thread creation warning: @_" };
660   threads->create(@_);
661  };
662  push @diag, "Thread creation error: $@" if $@;
663  diag @diag;
664  return $thread ? $thread : ();
665 }
666
667 =head2 C<usleep>
668
669 =over 4
670
671 =item *
672
673 Import :
674
675     use VPIT::TestHelpers 'usleep' => [ @impls ];
676
677 where :
678
679 =over 8
680
681 =item -
682
683 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.
684 When the list is empty, it defaults to all of them.
685
686 =back
687
688 =item *
689
690 Dependencies : none
691
692 =item *
693
694 Exports :
695
696 =over 8
697
698 =item -
699
700 C<usleep $microseconds>
701
702 =back
703
704 =back
705
706 =cut
707
708 sub init_usleep {
709  my (@impls) = @_;
710
711  my %impls = (
712   'Time::HiRes' => sub {
713    if (do { local $@; eval { require Time::HiRes; 1 } }) {
714     defined and diag "Using usleep() from Time::HiRes $_"
715                                                       for $Time::HiRes::VERSION;
716     return \&Time::HiRes::usleep;
717    } else {
718     return undef;
719    }
720   },
721   'select' => sub {
722    if ($Config::Config{d_select}) {
723     diag 'Using select()-based fallback usleep()';
724     return sub ($) {
725      my $s = $_[0];
726      my $r = 0;
727      while ($s > 0) {
728       my ($found, $t) = select(undef, undef, undef, $s / 1e6);
729       last unless defined $t;
730       $t  = int($t * 1e6);
731       $s -= $t;
732       $r += $t;
733      }
734      return $r;
735     };
736    } else {
737     return undef;
738    }
739   },
740   'sleep' => sub {
741    diag 'Using sleep()-based fallback usleep()';
742    return sub ($) {
743     my $ms = int $_[0];
744     my $s  = int($ms / 1e6) + ($ms % 1e6 == 0 ? 0 : 1);
745     my $t  = sleep $s;
746     return $t * 1e6;
747    };
748   },
749  );
750
751  @impls = qw<Time::HiRes select sleep> unless @impls;
752
753  my $usleep;
754  for my $impl (@impls) {
755   next unless defined $impl and $impls{$impl};
756   $usleep = $impls{$impl}->();
757   last if defined $usleep;
758  }
759
760  skip_all "Could not find a suitable usleep() implementation among: @impls"
761                                                                  unless $usleep;
762
763  return usleep => $usleep;
764 }
765
766 =head1 CLASSES
767
768 =head2 C<VPIT::TestHelpers::Guard>
769
770 Syntax :
771
772     {
773      my $guard = VPIT::TestHelpers::Guard->new($coderef);
774      ...
775     } # $codref called here
776
777 =cut
778
779 package VPIT::TestHelpers::Guard;
780
781 sub new {
782  my ($class, $code) = @_;
783
784  bless { code => $code }, $class;
785 }
786
787 sub DESTROY { $_[0]->{code}->() }
788
789 =head1 AUTHOR
790
791 Vincent Pit, C<< <perl at profvince.com> >>, L<http://www.profvince.com>.
792
793 =head1 COPYRIGHT & LICENSE
794
795 Copyright 2012,2013,2014,2015 Vincent Pit, all rights reserved.
796
797 This program is free software; you can redistribute it and/or modify it under the same terms as Perl itself.
798
799 =cut
800
801 1;