package VPIT::TestHelpers;

use strict;
use warnings;

use Config ();

sub export_to_pkg {
 my ($subs, $pkg) = @_;

 while (my ($name, $code) = each %$subs) {
  no strict 'refs';
  *{$pkg.'::'.$name} = $code;
 }

 return 1;
}

my %default_exports = (
 load_or_skip     => \&load_or_skip,
 load_or_skip_all => \&load_or_skip_all,
 run_perl         => \&run_perl,
 skip_all         => \&skip_all,
);

my %features = (
 threads => \&init_threads,
 usleep  => \&init_usleep,
 capture => \&init_capture,
);

sub import {
 shift;
 my @opts = @_;

 my %exports = %default_exports;

 for (my $i = 0; $i <= $#opts; ++$i) {
  my $feature = $opts[$i];
  next unless defined $feature;

  my $args;
  if ($i < $#opts and defined $opts[$i+1] and ref $opts[$i+1] eq 'ARRAY') {
   ++$i;
   $args = $opts[$i];
  } else {
   $args = [ ];
  }

  my $handler = $features{$feature};
  die "Unknown feature '$feature'" unless defined $handler;

  my %syms = $handler->(@$args);

  $exports{$_} = $syms{$_} for sort keys %syms;
 }

 export_to_pkg \%exports => scalar caller;
}

my $test_sub = sub {
 my $sub = shift;

 my $stash;
 if ($INC{'Test/Leaner.pm'}) {
  $stash = \%Test::Leaner::;
 } else {
  require Test::More;
  $stash = \%Test::More::;
 }

 my $glob = $stash->{$sub};
 return $glob ? *$glob{CODE} : undef;
};

sub skip { $test_sub->('skip')->(@_) }

sub skip_all { $test_sub->('plan')->(skip_all => $_[0]) }

sub diag {
 my $diag = $test_sub->('diag');
 $diag->($_) for @_;
}

our $TODO;
local $TODO;

sub load {
 my ($pkg, $ver, $imports) = @_;

 my $spec = $ver && $ver !~ /^[0._]*$/ ? "$pkg $ver" : $pkg;
 my $err;

 local $@;
 if (eval "use $spec (); 1") {
  $ver = do { no strict 'refs'; ${"${pkg}::VERSION"} };
  $ver = 'undef' unless defined $ver;

  if ($imports) {
   my @imports = @$imports;
   my $caller  = (caller 1)[0];
   local $@;
   my $res = eval <<"IMPORTER";
package
        $caller;
BEGIN { \$pkg->import(\@imports) }
1;
IMPORTER
   $err = "Could not import '@imports' from $pkg $ver: $@" unless $res;
  }
 } else {
  (my $file = "$pkg.pm") =~ s{::}{/}g;
  delete $INC{$file};
  $err = "Could not load $spec";
 }

 if ($err) {
  return wantarray ? (0, $err) : 0;
 } else {
  diag "Using $pkg $ver";
  return 1;
 }
}

sub load_or_skip {
 my ($pkg, $ver, $imports, $tests) = @_;

 die 'You must specify how many tests to skip' unless defined $tests;

 my ($loaded, $err) = load($pkg, $ver, $imports);
 skip $err => $tests unless $loaded;

 return $loaded;
}

sub load_or_skip_all {
 my ($pkg, $ver, $imports) = @_;

 my ($loaded, $err) = load($pkg, $ver, $imports);
 skip_all $err unless $loaded;

 return $loaded;
}

sub run_perl {
 my $code = shift;

 if ($code =~ /"/) {
  die 'Double quotes in evaluated code are not portable';
 }

 my ($SystemRoot, $PATH) = @ENV{qw<SystemRoot PATH>};
 my $ld_name  = $Config::Config{ldlibpthname};
 my $ldlibpth = $ENV{$ld_name};

 local %ENV;
 $ENV{$ld_name}   = $ldlibpth   if                      defined $ldlibpth;
 $ENV{SystemRoot} = $SystemRoot if $^O eq 'MSWin32' and defined $SystemRoot;
 $ENV{PATH}       = $PATH       if $^O eq 'cygwin'  and defined $PATH;

 my $perl = $^X;
 unless (-e $perl and -x $perl) {
  $perl = $Config::Config{perlpath};
  unless (-e $perl and -x $perl) {
   return undef;
  }
 }

 system { $perl } $perl, '-T', map("-I$_", @INC), '-e', $code;
}

sub init_capture {
 skip_all 'Cannot capture output on VMS' if $^O eq 'VMS';

 load_or_skip_all 'IO::Handle', '0', [ ];
 load_or_skip_all 'IO::Select', '0', [ ];
 load_or_skip_all 'IPC::Open3', '0', [ ];
 if ($^O eq 'MSWin32') {
  load_or_skip_all 'Socket', '0', [ ];
 }

 return capture => \&capture;
}

# Inspired from IPC::Cmd

sub capture {
 my @cmd = @_;

 my $want = wantarray;

 my $fail = sub {
  my $err     = $!;
  my $ext_err = $^O eq 'MSWin32' ? $^E : undef;

  my $syscall = shift;
  my $args    = join ', ', @_;

  my $msg = "$syscall($args) failed: ";

  if (defined $err) {
   no warnings 'numeric';
   my ($err_code, $err_str) = (int $err, "$err");
   $msg .= "$err_str ($err_code)";
  }

  if (defined $ext_err) {
   no warnings 'numeric';
   my ($ext_err_code, $ext_err_str) = (int $ext_err, "$ext_err");
   $msg .= ", $ext_err_str ($ext_err_code)";
  }

  die "$msg\n";
 };

 my ($status, $content_out, $content_err);

 local $@;
 my $ok = eval {
  my ($pid, $out, $err);

  if ($^O eq 'MSWin32') {
   my $pipe = sub {
    socketpair $_[0], $_[1],
               &Socket::AF_UNIX, &Socket::SOCK_STREAM, &Socket::PF_UNSPEC
                      or $fail->(qw<socketpair reader writer>);
    shutdown $_[0], 1 or $fail->(qw<shutdown reader>);
    shutdown $_[1], 0 or $fail->(qw<shutdown writer>);
    return 1;
   };
   local (*IN_R,  *IN_W);
   local (*OUT_R, *OUT_W);
   local (*ERR_R, *ERR_W);
   $pipe->(*IN_R,  *IN_W);
   $pipe->(*OUT_R, *OUT_W);
   $pipe->(*ERR_R, *ERR_W);

   $pid = IPC::Open3::open3('>&IN_R', '<&OUT_W', '<&ERR_W', @cmd);

   close *IN_W or $fail->(qw<close input>);
   $out = *OUT_R;
   $err = *ERR_R;
  } else {
   my $in = IO::Handle->new;
   $out   = IO::Handle->new;
   $out->autoflush(1);
   $err   = IO::Handle->new;
   $err->autoflush(1);

   $pid = IPC::Open3::open3($in, $out, $err, @cmd);

   close $in;
  }

  # Forward signals to the child (except SIGKILL)
  my %sig_handlers;
  foreach my $s (keys %SIG) {
   $sig_handlers{$s} = sub {
    kill "$s" => $pid;
    $SIG{$s} = $sig_handlers{$s};
   };
  }
  local $SIG{$_} = $sig_handlers{$_} for keys %SIG;

  unless ($want) {
   close $out or $fail->(qw<close output>);
   close $err or $fail->(qw<close error>);
   waitpid $pid, 0;
   $status = $?;
   return 1;
  }

  my $sel = IO::Select->new();
  $sel->add($out, $err);

  my $fd_out = fileno $out;
  my $fd_err = fileno $err;

  my %contents;
  $contents{$fd_out} = '';
  $contents{$fd_err} = '';

  while (my @ready = $sel->can_read) {
   for my $fh (@ready) {
    my $buf;
    my $bytes_read = sysread $fh, $buf, 4096;
    if (not defined $bytes_read) {
     $fail->('sysread', 'fd(' . fileno($fh) . ')');
    } elsif ($bytes_read) {
     $contents{fileno($fh)} .= $buf;
    } else {
     $sel->remove($fh);
     close $fh or $fail->('close', 'fd(' . fileno($fh) . ')');
     last unless $sel->count;
    }
   }
  }

  waitpid $pid, 0;
  $status = $?;

  if ($^O eq 'MSWin32') {
   # Manual CRLF translation that couldn't be done with sysread.
   s/\x0D\x0A/\n/g for values %contents;
  }

  $content_out = $contents{$fd_out};
  $content_err = $contents{$fd_err};

  1;
 };

 if ($ok) {
  return ($status, $content_out, $content_err);
 } else {
  my $err = $@;
  chomp $err;
  return (undef, $err);
 }
}

sub init_threads {
 my ($pkg, $threadsafe, $force_var) = @_;

 skip_all 'This perl wasn\'t built to support threads'
                                            unless $Config::Config{useithreads};

 $pkg = 'package' unless defined $pkg;
 skip_all "This $pkg isn't thread safe" if defined $threadsafe and !$threadsafe;

 $force_var = 'PERL_FORCE_TEST_THREADS' unless defined $force_var;
 my $force  = $ENV{$force_var} ? 1 : !1;
 skip_all 'perl 5.13.4 required to test thread safety'
                                             unless $force or "$]" >= 5.013_004;

 if (($INC{'Test/More.pm'} || $INC{'Test/Leaner.pm'}) && !$INC{'threads.pm'}) {
  die 'Test::More/Test::Leaner was loaded too soon';
 }

 load_or_skip_all 'threads',         $force ? '0' : '1.67', [ ];
 load_or_skip_all 'threads::shared', $force ? '0' : '1.14', [ ];

 require Test::Leaner;

 diag "Threads testing forced by \$ENV{$force_var}" if $force;

 return spawn => \&spawn;
}

sub init_usleep {
 my $usleep;

 if (do { local $@; eval { require Time::HiRes; 1 } }) {
  defined and diag "Using usleep() from Time::HiRes $_"
                                                      for $Time::HiRes::VERSION;
  $usleep = \&Time::HiRes::usleep;
 } else {
  diag 'Using fallback usleep()';
  $usleep = sub {
   my $s = int($_[0] / 2.5e5);
   sleep $s if $s;
  };
 }

 return usleep => $usleep;
}

sub spawn {
 local $@;
 my @diag;
 my $thread = eval {
  local $SIG{__WARN__} = sub { push @diag, "Thread creation warning: @_" };
  threads->create(@_);
 };
 push @diag, "Thread creation error: $@" if $@;
 diag @diag;
 return $thread ? $thread : ();
}

package VPIT::TestHelpers::Guard;

sub new {
 my ($class, $code) = @_;

 bless { code => $code }, $class;
}

sub DESTROY { $_[0]->{code}->() }

1;
