]> git.vpit.fr Git - perl/modules/VPIT-TestHelpers.git/blob - lib/VPIT/TestHelpers.pm
Add minimal POD
[perl/modules/VPIT-TestHelpers.git] / 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 my %default_exports = (
33  load_or_skip     => \&load_or_skip,
34  load_or_skip_all => \&load_or_skip_all,
35  skip_all         => \&skip_all,
36 );
37
38 my %features = (
39  threads  => \&init_threads,
40  usleep   => \&init_usleep,
41  run_perl => \&init_run_perl,
42  capture  => \&init_capture,
43 );
44
45 sub import {
46  shift;
47  my @opts = @_;
48
49  my %exports = %default_exports;
50
51  for (my $i = 0; $i <= $#opts; ++$i) {
52   my $feature = $opts[$i];
53   next unless defined $feature;
54
55   my $args;
56   if ($i < $#opts and defined $opts[$i+1] and ref $opts[$i+1] eq 'ARRAY') {
57    ++$i;
58    $args = $opts[$i];
59   } else {
60    $args = [ ];
61   }
62
63   my $handler = $features{$feature};
64   die "Unknown feature '$feature'" unless defined $handler;
65
66   my %syms = $handler->(@$args);
67
68   $exports{$_} = $syms{$_} for sort keys %syms;
69  }
70
71  export_to_pkg \%exports => scalar caller;
72 }
73
74 my $test_sub = sub {
75  my $sub = shift;
76
77  my $stash;
78  if ($INC{'Test/Leaner.pm'}) {
79   $stash = \%Test::Leaner::;
80  } else {
81   require Test::More;
82   $stash = \%Test::More::;
83  }
84
85  my $glob = $stash->{$sub};
86  return $glob ? *$glob{CODE} : undef;
87 };
88
89 sub skip { $test_sub->('skip')->(@_) }
90
91 sub skip_all { $test_sub->('plan')->(skip_all => $_[0]) }
92
93 sub diag {
94  my $diag = $test_sub->('diag');
95  $diag->($_) for @_;
96 }
97
98 our $TODO;
99 local $TODO;
100
101 sub load {
102  my ($pkg, $ver, $imports) = @_;
103
104  my $spec = $ver && $ver !~ /^[0._]*$/ ? "$pkg $ver" : $pkg;
105  my $err;
106
107  local $@;
108  if (eval "use $spec (); 1") {
109   $ver = do { no strict 'refs'; ${"${pkg}::VERSION"} };
110   $ver = 'undef' unless defined $ver;
111
112   if ($imports) {
113    my @imports = @$imports;
114    my $caller  = (caller 1)[0];
115    local $@;
116    my $res = eval <<"IMPORTER";
117 package
118         $caller;
119 BEGIN { \$pkg->import(\@imports) }
120 1;
121 IMPORTER
122    $err = "Could not import '@imports' from $pkg $ver: $@" unless $res;
123   }
124  } else {
125   (my $file = "$pkg.pm") =~ s{::}{/}g;
126   delete $INC{$file};
127   $err = "Could not load $spec";
128  }
129
130  if ($err) {
131   return wantarray ? (0, $err) : 0;
132  } else {
133   diag "Using $pkg $ver";
134   return 1;
135  }
136 }
137
138 sub load_or_skip {
139  my ($pkg, $ver, $imports, $tests) = @_;
140
141  die 'You must specify how many tests to skip' unless defined $tests;
142
143  my ($loaded, $err) = load($pkg, $ver, $imports);
144  skip $err => $tests unless $loaded;
145
146  return $loaded;
147 }
148
149 sub load_or_skip_all {
150  my ($pkg, $ver, $imports) = @_;
151
152  my ($loaded, $err) = load($pkg, $ver, $imports);
153  skip_all $err unless $loaded;
154
155  return $loaded;
156 }
157
158 =head1 FEATURES
159
160 =head2 C<run_perl>
161
162 =over 4
163
164 =item *
165
166 Import :
167
168     use VPIT::TestHelpers run_perl => [ $p ]
169
170 where :
171
172 =over 8
173
174 =item -
175
176 C<$p> is prefixed to the constants exported by this feature (defaults to C<''>).
177
178 =back
179
180 =item *
181
182 Dependencies : none
183
184 =item *
185
186 Exports :
187
188 =over 8
189
190 =item -
191
192 C<run_perl $code>
193
194 =item -
195
196 C<RUN_PERL_FAILED> (possibly prefixed by C<$p>)
197
198 =back
199
200 =back
201
202 =cut
203
204 sub fresh_perl_env (&) {
205  my $handler = shift;
206
207  my ($SystemRoot, $PATH) = @ENV{qw<SystemRoot PATH>};
208  my $ld_name  = $Config::Config{ldlibpthname};
209  my $ldlibpth = $ENV{$ld_name};
210
211  local %ENV;
212  $ENV{$ld_name}   = $ldlibpth   if                      defined $ldlibpth;
213  $ENV{SystemRoot} = $SystemRoot if $^O eq 'MSWin32' and defined $SystemRoot;
214  $ENV{PATH}       = $PATH       if $^O eq 'cygwin'  and defined $PATH;
215
216  my $perl = $^X;
217  unless (-e $perl and -x $perl) {
218   $perl = $Config::Config{perlpath};
219   unless (-e $perl and -x $perl) {
220    return undef;
221   }
222  }
223
224  return $handler->($perl, '-T', map("-I$_", @INC));
225 }
226
227 sub init_run_perl {
228  my $prefix = shift;
229
230  if (defined $prefix) {
231   if (length $prefix and $prefix !~ /_$/) {
232    $prefix .= '_';
233   }
234  } else {
235   $prefix = '';
236  }
237
238  my $p = $prefix;
239
240  return (
241   run_perl              => \&run_perl,
242   "${p}RUN_PERL_FAILED" => sub () { 'Could not execute perl subprocess' },
243  );
244 }
245
246 sub run_perl {
247  my $code = shift;
248
249  if ($code =~ /"/) {
250   die 'Double quotes in evaluated code are not portable';
251  }
252
253  fresh_perl_env {
254   my ($perl, @perl_args) = @_;
255   system { $perl } $perl, @perl_args, '-e', $code;
256  };
257 }
258
259 =head2 C<capture>
260
261 =over 4
262
263 =item *
264
265 Import :
266
267     use VPIT::TestHelpers 'capture'
268
269 =item *
270
271 Dependencies :
272
273 =over 8
274
275 =item -
276
277 Not VMS
278
279 =item -
280
281 L<IO::Handle>, L<IO::Select>, L<IPC::Open3>
282
283 =item -
284
285 On MSWin32 : L<Socket>
286
287 =back
288
289 =item *
290
291 Exports :
292
293 =over 8
294
295 =item -
296
297 C<capture @command>
298
299 =item -
300
301 C<capture_perl $code>
302
303 =back
304
305 =back
306
307 =cut
308
309 sub init_capture {
310  skip_all 'Cannot capture output on VMS' if $^O eq 'VMS';
311
312  load_or_skip_all 'IO::Handle', '0', [ ];
313  load_or_skip_all 'IO::Select', '0', [ ];
314  load_or_skip_all 'IPC::Open3', '0', [ ];
315  if ($^O eq 'MSWin32') {
316   load_or_skip_all 'Socket', '0', [ ];
317  }
318
319  return (
320   capture      => \&capture,
321   capture_perl => \&capture_perl,
322  );
323 }
324
325 # Inspired from IPC::Cmd
326
327 sub capture {
328  my @cmd = @_;
329
330  my $want = wantarray;
331
332  my $fail = sub {
333   my $err     = $!;
334   my $ext_err = $^O eq 'MSWin32' ? $^E : undef;
335
336   my $syscall = shift;
337   my $args    = join ', ', @_;
338
339   my $msg = "$syscall($args) failed: ";
340
341   if (defined $err) {
342    no warnings 'numeric';
343    my ($err_code, $err_str) = (int $err, "$err");
344    $msg .= "$err_str ($err_code)";
345   }
346
347   if (defined $ext_err) {
348    no warnings 'numeric';
349    my ($ext_err_code, $ext_err_str) = (int $ext_err, "$ext_err");
350    $msg .= ", $ext_err_str ($ext_err_code)";
351   }
352
353   die "$msg\n";
354  };
355
356  my ($status, $content_out, $content_err);
357
358  local $@;
359  my $ok = eval {
360   my ($pid, $out, $err);
361
362   if ($^O eq 'MSWin32') {
363    my $pipe = sub {
364     socketpair $_[0], $_[1],
365                &Socket::AF_UNIX, &Socket::SOCK_STREAM, &Socket::PF_UNSPEC
366                       or $fail->(qw<socketpair reader writer>);
367     shutdown $_[0], 1 or $fail->(qw<shutdown reader>);
368     shutdown $_[1], 0 or $fail->(qw<shutdown writer>);
369     return 1;
370    };
371    local (*IN_R,  *IN_W);
372    local (*OUT_R, *OUT_W);
373    local (*ERR_R, *ERR_W);
374    $pipe->(*IN_R,  *IN_W);
375    $pipe->(*OUT_R, *OUT_W);
376    $pipe->(*ERR_R, *ERR_W);
377
378    $pid = IPC::Open3::open3('>&IN_R', '<&OUT_W', '<&ERR_W', @cmd);
379
380    close *IN_W or $fail->(qw<close input>);
381    $out = *OUT_R;
382    $err = *ERR_R;
383   } else {
384    my $in = IO::Handle->new;
385    $out   = IO::Handle->new;
386    $out->autoflush(1);
387    $err   = IO::Handle->new;
388    $err->autoflush(1);
389
390    $pid = IPC::Open3::open3($in, $out, $err, @cmd);
391
392    close $in;
393   }
394
395   # Forward signals to the child (except SIGKILL)
396   my %sig_handlers;
397   foreach my $s (keys %SIG) {
398    $sig_handlers{$s} = sub {
399     kill "$s" => $pid;
400     $SIG{$s} = $sig_handlers{$s};
401    };
402   }
403   local $SIG{$_} = $sig_handlers{$_} for keys %SIG;
404
405   unless ($want) {
406    close $out or $fail->(qw<close output>);
407    close $err or $fail->(qw<close error>);
408    waitpid $pid, 0;
409    $status = $?;
410    return 1;
411   }
412
413   my $sel = IO::Select->new();
414   $sel->add($out, $err);
415
416   my $fd_out = fileno $out;
417   my $fd_err = fileno $err;
418
419   my %contents;
420   $contents{$fd_out} = '';
421   $contents{$fd_err} = '';
422
423   while (my @ready = $sel->can_read) {
424    for my $fh (@ready) {
425     my $buf;
426     my $bytes_read = sysread $fh, $buf, 4096;
427     if (not defined $bytes_read) {
428      $fail->('sysread', 'fd(' . fileno($fh) . ')');
429     } elsif ($bytes_read) {
430      $contents{fileno($fh)} .= $buf;
431     } else {
432      $sel->remove($fh);
433      close $fh or $fail->('close', 'fd(' . fileno($fh) . ')');
434      last unless $sel->count;
435     }
436    }
437   }
438
439   waitpid $pid, 0;
440   $status = $?;
441
442   if ($^O eq 'MSWin32') {
443    # Manual CRLF translation that couldn't be done with sysread.
444    s/\x0D\x0A/\n/g for values %contents;
445   }
446
447   $content_out = $contents{$fd_out};
448   $content_err = $contents{$fd_err};
449
450   1;
451  };
452
453  if ($ok) {
454   return ($status, $content_out, $content_err);
455  } else {
456   my $err = $@;
457   chomp $err;
458   return (undef, $err);
459  }
460 }
461
462 sub capture_perl {
463  my $code = shift;
464
465  if ($code =~ /"/) {
466   die 'Double quotes in evaluated code are not portable';
467  }
468
469  fresh_perl_env {
470   my @perl = @_;
471   capture @perl, '-e', $code;
472  };
473 }
474
475 =head2 C<threads>
476
477 =over 4
478
479 =item *
480
481 Import :
482
483     use VPIT::TestHelpers threads => [
484      $pkg, $is_threadsafe, $force_var
485     ];
486
487 where :
488
489 =over 8
490
491 =item -
492
493 C<$pkg> is the target package name to be used in error messages (defaults to C<'package'>) ;
494
495 =item -
496
497 C<$is_threadsafe> is a boolean telling whether the target module is thread-safe (not tested if C<undef>) ;
498
499 =item -
500
501 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>).
502
503 =back
504
505 =item *
506
507 Dependencies :
508
509 =over 8
510
511 =item -
512
513 C<perl> 5.13.4
514
515 =item -
516
517 L<threads> 1.67
518
519 =item -
520
521 L<threads::shared> 1.14
522
523 =item -
524
525 L<Test::Leaner>
526
527 =back
528
529 =item *
530
531 Exports :
532
533 =over 8
534
535 =item -
536
537 C<spawn $coderef>
538
539 =back
540
541 =back
542
543 =cut
544
545 sub init_threads {
546  my ($pkg, $threadsafe, $force_var) = @_;
547
548  skip_all 'This perl wasn\'t built to support threads'
549                                             unless $Config::Config{useithreads};
550
551  $pkg = 'package' unless defined $pkg;
552  skip_all "This $pkg isn't thread safe" if defined $threadsafe and !$threadsafe;
553
554  $force_var = 'PERL_FORCE_TEST_THREADS' unless defined $force_var;
555  my $force  = $ENV{$force_var} ? 1 : !1;
556  skip_all 'perl 5.13.4 required to test thread safety'
557                                              unless $force or "$]" >= 5.013_004;
558
559  if (($INC{'Test/More.pm'} || $INC{'Test/Leaner.pm'}) && !$INC{'threads.pm'}) {
560   die 'Test::More/Test::Leaner was loaded too soon';
561  }
562
563  load_or_skip_all 'threads',         $force ? '0' : '1.67', [ ];
564  load_or_skip_all 'threads::shared', $force ? '0' : '1.14', [ ];
565
566  require Test::Leaner;
567
568  diag "Threads testing forced by \$ENV{$force_var}" if $force;
569
570  return spawn => \&spawn;
571 }
572
573 sub spawn {
574  local $@;
575  my @diag;
576  my $thread = eval {
577   local $SIG{__WARN__} = sub { push @diag, "Thread creation warning: @_" };
578   threads->create(@_);
579  };
580  push @diag, "Thread creation error: $@" if $@;
581  diag @diag;
582  return $thread ? $thread : ();
583 }
584
585 =head2 C<usleep>
586
587 =over 4
588
589 =item *
590
591 Import :
592
593     use VPIT::TestHelpers 'usleep'
594
595 =item *
596
597 Dependencies : none
598
599 =item *
600
601 Exports :
602
603 =over 8
604
605 =item -
606
607 C<usleep $microseconds>
608
609 =back
610
611 =back
612
613 =cut
614
615 sub init_usleep {
616  my $usleep;
617
618  if (do { local $@; eval { require Time::HiRes; 1 } }) {
619   defined and diag "Using usleep() from Time::HiRes $_"
620                                                       for $Time::HiRes::VERSION;
621   $usleep = \&Time::HiRes::usleep;
622  } else {
623   diag 'Using fallback usleep()';
624   $usleep = sub {
625    my $s = int($_[0] / 2.5e5);
626    sleep $s if $s;
627   };
628  }
629
630  return usleep => $usleep;
631 }
632
633 =head1 CLASSES
634
635 =head2 C<VPIT::TestHelpers::Guard>
636
637 Syntax :
638
639     {
640      my $guard = VPIT::TestHelpers::Guard->new($coderef);
641      ...
642     } # $codref called here
643
644 =cut
645
646 package VPIT::TestHelpers::Guard;
647
648 sub new {
649  my ($class, $code) = @_;
650
651  bless { code => $code }, $class;
652 }
653
654 sub DESTROY { $_[0]->{code}->() }
655
656 =head1 AUTHOR
657
658 Vincent Pit, C<< <perl at profvince.com> >>, L<http://www.profvince.com>.
659
660 =head1 COPYRIGHT & LICENSE
661
662 Copyright 2012,2013,2014,2015 Vincent Pit, all rights reserved.
663
664 This program is free software; you can redistribute it and/or modify it under the same terms as Perl itself.
665
666 =cut
667
668 1;