1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201
|
package Test::TMF; # inspired by App::Yath::Tester
use strict;
use warnings;
use Test2::V0;
use Test2::Tools::Explain;
use Test2::API qw/context run_subtest/;
use Test2::Tools::Compare qw/is/;
use Carp qw/croak/;
use File::Temp qw/tempfile tempdir/;
use File::Basename qw(basename);
use POSIX;
use Fcntl qw/SEEK_CUR/;
use Cwd 'abs_path';
use Test2::Harness::Util::IPC qw/run_cmd/;
use Exporter 'import';
our @EXPORT = qw{
tmf_test_code
t2_run_script
};
our $TMP; # directory
sub _setup_tmp_dir {
$TMP //= File::Temp->newdir();
}
my @_tmf_test_args;
sub tmf_test_code {
my (%params) = @_;
if ( !scalar @_tmf_test_args ) {
require Test::MockFile;
my $path = $INC{"Test/MockFile.pm"} or die;
$path =~ s{\QTest/MockFile.pm\E$}{};
push @_tmf_test_args, '-I' . $path;
}
my $perl_args = [@_tmf_test_args];
my $extra_args = delete $params{perl_args};
if ( defined $extra_args ) {
if ( ref $extra_args ) {
push @$perl_args, @$extra_args;
}
else {
push @$perl_args, $extra_args;
}
}
return t2_run_script( perl_args => $perl_args, %params );
}
sub t2_run_script {
my (%params) = @_;
my $perl_args = delete $params{perl_args} // [];
my $test_code = delete $params{test_code} // croak("no test code");
my ( $fh, $filename ) = tempfile( DIR => _setup_tmp_dir() );
print {$fh} <<"EOS";
use strict;
use warnings;
use Test2::Bundle::Extended;
use Test2::Tools::Explain;
use Test2::Plugin::NoWarnings;
$test_code
done_testing;
EOS
close $fh;
return _test_script( sub { return ( $filename, @$perl_args ) }, %params );
}
sub _test_script {
my ( $finder, %params ) = @_;
my $ctx = context();
my $cmd = delete $params{cmd} // delete $params{command};
my $cli = delete $params{cli} // delete $params{args} // [];
my $env = delete $params{env} // {};
my $prefix = delete $params{prefix};
my $subtest = delete $params{test} // delete $params{tests} // delete $params{subtest};
my $exittest = delete $params{exit};
my $debug = delete $params{debug} // 0;
my $capture = delete $params{capture} // 1;
my $name = delete $params{name};
if ( keys %params ) {
croak "Unexpected parameters: " . join( ', ', sort keys %params );
}
my ( $wh, $cfile );
if ($capture) {
( $wh, $cfile ) = tempfile( "cpdev-$$-XXXXXXXX", TMPDIR => 1, CLEANUP => 1, SUFFIX => '.out' );
$wh->autoflush(1);
}
die q[Finder need to be a coderef] unless ref $finder eq 'CODE';
my ( $script, @lib ) = $finder->();
my @all_args = ( $cmd ? ($cmd) : (), @$cli );
my @cmd = ( $^X, @lib, $script, @all_args );
print STDERR "DEBUG: Command = " . join( ' ' => @cmd ) . "\n" if $debug;
local %ENV = %ENV;
$ENV{$_} = $env->{$_} for keys %$env;
my $pid = run_cmd(
no_set_pgrp => 1,
$capture ? ( stderr => $wh, stdout => $wh ) : (),
command => \@cmd,
run_in_parent => [ sub { close($wh) } ],
);
my ( @lines, $exit );
if ($capture) {
open( my $rh, '<', $cfile ) or die "Could not open output file: $!";
$rh->blocking(0);
while (1) {
seek( $rh, 0, SEEK_CUR ); # CLEAR EOF
my @new = <$rh>;
push @lines => @new;
print map { chomp($_); "DEBUG: > $_\n" } @new if $debug > 1;
waitpid( $pid, WNOHANG ) or next;
$exit = $?;
last;
}
while ( my @new = <$rh> ) {
push @lines => @new;
print map { chomp($_); "DEBUG: > $_\n" } @new if $debug > 1;
}
}
else {
print STDERR "DEBUG: Waiting for $pid\n" if $debug;
waitpid( $pid, 0 );
$exit = $?;
}
print STDERR "DEBUG: Exit: $exit\n" if $debug;
my $out = {
exit => $exit,
$capture ? ( output => join( '', @lines ) ) : (),
};
$name //= join( ' ', map { length($_) < 30 ? $_ : substr( $_, 0, 10 ) . "[...]" . substr( $_, -10 ) } grep { defined($_) } basename($script), @all_args );
run_subtest(
$name,
sub {
if ( defined $exittest ) {
my $ictx = context( level => 3 );
is( $exit, $exittest, "Exit Value Check" );
$ictx->release;
}
if ($subtest) {
local $_ = $out->{output};
local $? = $out->{exit};
$subtest->($out);
}
my $ictx = context( level => 3 );
$ictx->diag( "Command = " . join( ' ' => grep { defined $_ } @cmd ) . "\nExit = $exit\n==== Output ====\n$out->{output}\n========" )
unless $ictx->hub->is_passing;
$ictx->release;
},
{ buffered => 1 },
$out,
) if $subtest || defined $exittest;
$ctx->release;
return $out;
}
1;
|