]> git.vpit.fr Git - perl/modules/indirect.git/blob - t/lib/VPIT/TestHelpers.pm
Update VPIT::TestHelpers to 4eeb5afc
[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 sub export_to_pkg {
9  my ($subs, $pkg) = @_;
10
11  while (my ($name, $code) = each %$subs) {
12   no strict 'refs';
13   *{$pkg.'::'.$name} = $code;
14  }
15
16  return 1;
17 }
18
19 my %default_exports = (
20  load_or_skip     => \&load_or_skip,
21  load_or_skip_all => \&load_or_skip_all,
22  run_perl         => \&run_perl,
23  skip_all         => \&skip_all,
24 );
25
26 my %features = (
27  threads => \&init_threads,
28  usleep  => \&init_usleep,
29 );
30
31 sub import {
32  shift;
33  my @opts = @_;
34
35  my %exports = %default_exports;
36
37  for (my $i = 0; $i <= $#opts; ++$i) {
38   my $feature = $opts[$i];
39   next unless defined $feature;
40
41   my $args;
42   if ($i < $#opts and defined $opts[$i+1] and ref $opts[$i+1] eq 'ARRAY') {
43    ++$i;
44    $args = $opts[$i];
45   } else {
46    $args = [ ];
47   }
48
49   my $handler = $features{$feature};
50   die "Unknown feature '$feature'" unless defined $handler;
51
52   my %syms = $handler->(@$args);
53
54   $exports{$_} = $syms{$_} for sort keys %syms;
55  }
56
57  export_to_pkg \%exports => scalar caller;
58 }
59
60 my $test_sub = sub {
61  my $sub = shift;
62
63  my $stash;
64  if ($INC{'Test/Leaner.pm'}) {
65   $stash = \%Test::Leaner::;
66  } else {
67   require Test::More;
68   $stash = \%Test::More::;
69  }
70
71  my $glob = $stash->{$sub};
72  return $glob ? *$glob{CODE} : undef;
73 };
74
75 sub skip { $test_sub->('skip')->(@_) }
76
77 sub skip_all { $test_sub->('plan')->(skip_all => $_[0]) }
78
79 sub diag {
80  my $diag = $test_sub->('diag');
81  $diag->($_) for @_;
82 }
83
84 our $TODO;
85 local $TODO;
86
87 sub load {
88  my ($pkg, $ver, $imports) = @_;
89
90  my $spec = $ver && $ver !~ /^[0._]*$/ ? "$pkg $ver" : $pkg;
91  my $err;
92
93  local $@;
94  if (eval "use $spec (); 1") {
95   $ver = do { no strict 'refs'; ${"${pkg}::VERSION"} };
96   $ver = 'undef' unless defined $ver;
97
98   if ($imports) {
99    my @imports = @$imports;
100    my $caller  = (caller 1)[0];
101    local $@;
102    my $res = eval <<"IMPORTER";
103 package
104         $caller;
105 BEGIN { \$pkg->import(\@imports) }
106 1;
107 IMPORTER
108    $err = "Could not import '@imports' from $pkg $ver: $@" unless $res;
109   }
110  } else {
111   (my $file = "$pkg.pm") =~ s{::}{/}g;
112   delete $INC{$file};
113   $err = "Could not load $spec";
114  }
115
116  if ($err) {
117   return wantarray ? (0, $err) : 0;
118  } else {
119   diag "Using $pkg $ver";
120   return 1;
121  }
122 }
123
124 sub load_or_skip {
125  my ($pkg, $ver, $imports, $tests) = @_;
126
127  die 'You must specify how many tests to skip' unless defined $tests;
128
129  my ($loaded, $err) = load($pkg, $ver, $imports);
130  skip $err => $tests unless $loaded;
131
132  return $loaded;
133 }
134
135 sub load_or_skip_all {
136  my ($pkg, $ver, $imports) = @_;
137
138  my ($loaded, $err) = load($pkg, $ver, $imports);
139  skip_all $err unless $loaded;
140
141  return $loaded;
142 }
143
144 sub run_perl {
145  my $code = shift;
146
147  if ($code =~ /"/) {
148   die 'Double quotes in evaluated code are not portable';
149  }
150
151  my ($SystemRoot, $PATH) = @ENV{qw<SystemRoot PATH>};
152  my $ld_name  = $Config::Config{ldlibpthname};
153  my $ldlibpth = $ENV{$ld_name};
154
155  local %ENV;
156  $ENV{$ld_name}   = $ldlibpth   if                      defined $ldlibpth;
157  $ENV{SystemRoot} = $SystemRoot if $^O eq 'MSWin32' and defined $SystemRoot;
158  $ENV{PATH}       = $PATH       if $^O eq 'cygwin'  and defined $PATH;
159
160  my $perl = $^X;
161  unless (-e $perl and -x $perl) {
162   $perl = $Config::Config{perlpath};
163   unless (-e $perl and -x $perl) {
164    return undef;
165   }
166  }
167
168  system { $perl } $perl, '-T', map("-I$_", @INC), '-e', $code;
169 }
170
171 sub init_threads {
172  my ($pkg, $threadsafe, $force_var) = @_;
173
174  skip_all 'This perl wasn\'t built to support threads'
175                                             unless $Config::Config{useithreads};
176
177  $pkg = 'package' unless defined $pkg;
178  skip_all "This $pkg isn't thread safe" if defined $threadsafe and !$threadsafe;
179
180  $force_var = 'PERL_FORCE_TEST_THREADS' unless defined $force_var;
181  my $force  = $ENV{$force_var} ? 1 : !1;
182  skip_all 'perl 5.13.4 required to test thread safety'
183                                              unless $force or "$]" >= 5.013_004;
184
185  if (($INC{'Test/More.pm'} || $INC{'Test/Leaner.pm'}) && !$INC{'threads.pm'}) {
186   die 'Test::More/Test::Leaner was loaded too soon';
187  }
188
189  load_or_skip_all 'threads',         $force ? '0' : '1.67', [ ];
190  load_or_skip_all 'threads::shared', $force ? '0' : '1.14', [ ];
191
192  require Test::Leaner;
193
194  diag "Threads testing forced by \$ENV{$force_var}" if $force;
195
196  return spawn => \&spawn;
197 }
198
199 sub init_usleep {
200  my $usleep;
201
202  if (do { local $@; eval { require Time::HiRes; 1 } }) {
203   defined and diag "Using usleep() from Time::HiRes $_"
204                                                       for $Time::HiRes::VERSION;
205   $usleep = \&Time::HiRes::usleep;
206  } else {
207   diag 'Using fallback usleep()';
208   $usleep = sub {
209    my $s = int($_[0] / 2.5e5);
210    sleep $s if $s;
211   };
212  }
213
214  return usleep => $usleep;
215 }
216
217 sub spawn {
218  local $@;
219  my @diag;
220  my $thread = eval {
221   local $SIG{__WARN__} = sub { push @diag, "Thread creation warning: @_" };
222   threads->create(@_);
223  };
224  push @diag, "Thread creation error: $@" if $@;
225  diag @diag;
226  return $thread ? $thread : ();
227 }
228
229 package VPIT::TestHelpers::Guard;
230
231 sub new {
232  my ($class, $code) = @_;
233
234  bless { code => $code }, $class;
235 }
236
237 sub DESTROY { $_[0]->{code}->() }
238
239 1;