<commit_before>package TestApache::subprocess;

use strict;
use warnings FATAL => 'all';

use Apache::Const -compile => 'OK';

use Apache::Test;
use Apache::TestUtil;
use File::Spec::Functions qw(catfile catdir);

use Apache::SubProcess ();

my %scripts = (
     argv   => 'print STDOUT "@ARGV";',
     env    => 'print STDOUT $ENV{SubProcess}',
     in_out => 'print STDOUT scalar <STDIN>;',
     in_err => 'print STDERR scalar <STDIN>;',
    );

sub APACHE_TEST_CONFIGURE {
    my ($class, $self) = @_;

    my $vars = $self->{vars};

    my $target_dir = catdir $vars->{documentroot}, "util";

    while (my($file, $code) = each %scripts) {
        $file = catfile $target_dir, "$file.pl";
        $self->write_perlscript($file, "$code\n");
    }
}

sub handler {
    my $r = shift;

    my $cfg = Apache::Test::config();
    my $vars = $cfg->{vars};

    # XXX: these tests randomly fail under 5.6.1
    plan $r, todo => [1..4], tests => 4;

    my $target_dir = catfile $vars->{documentroot}, "util";

    {
        # test: passing argv + scalar context
        my $command = catfile $target_dir, "argv.pl";
        my @argv = qw(foo bar);
        my $out = Apache::SubProcess::spawn_proc_prog($r, $command, \@argv);
        ok t_cmp(\@argv,
                 [split / /, <$out>],
                 "passing ARGV"
                );
    }

    {
        # test: passing env to subprocess through subprocess_env
        my $command = catfile $target_dir, "env.pl";
        my $value = "my cool proc";
        $r->subprocess_env->set(SubProcess => $value);
        my $out = Apache::SubProcess::spawn_proc_prog($r, $command);
        ok t_cmp($value,
                 <$out>,
                 "passing env via subprocess_env"
                );
    }

    {
        # test: subproc's stdin -> stdout + list context
        my $command = catfile $target_dir, "in_out.pl";
        my $value = "my cool proc\n"; # must have \n for <IN>
        my ($in, $out, $err) = 
            Apache::SubProcess::spawn_proc_prog($r, $command);
        print $in $value;
        ok t_cmp($value,
                 <$out>,
                 "testing subproc's stdin -> stdout + list context"
                );
    }

    {
        # test: subproc's stdin -> stderr + list context
        my $command = catfile $target_dir, "in_err.pl";
        my $value = "my stderr\n"; # must have \n for <IN>
        my ($in, $out, $err) = 
            Apache::SubProcess::spawn_proc_prog($r, $command);
        print $in $value;
        ok t_cmp($value,
                 <$err>,
                 "testing subproc's stdin -> stderr + list context"
                );
    }

# could test send_fd($out), send_fd($err), but currently it's only in
# compat.pm.

# these are wannabe's
#    ok t_cmp(
#             Apache::SUCCESS,
#             Apache::SubProcess::spawn_proc_sub($r, $sub, \@args),
#             "spawn a subprocess and run a subroutine in it"
#            );

#    ok t_cmp(
#             Apache::SUCCESS,
#             Apache::SubProcess::spawn_thread_prog($r, $command, \@argv),
#             "spawn thread and run a program in it"
#            );

#     ok t_cmp(
#             Apache::SUCCESS,
#             Apache::SubProcess::spawn_thread_sub($r, $sub, \@args),
#             "spawn thread and run a subroutine in it"
#            );

   Apache::OK;
}


1;
__DATA__
PerlModule Apache::SubProcess<commit_msg>do not attempt to preload Apache::SubProcess<commit_after>package TestApache::subprocess;

use strict;
use warnings FATAL => 'all';

use Apache::Const -compile => 'OK';

use Apache::Test;
use Apache::TestUtil;
use File::Spec::Functions qw(catfile catdir);

use Apache::SubProcess ();

my %scripts = (
     argv   => 'print STDOUT "@ARGV";',
     env    => 'print STDOUT $ENV{SubProcess}',
     in_out => 'print STDOUT scalar <STDIN>;',
     in_err => 'print STDERR scalar <STDIN>;',
    );

sub APACHE_TEST_CONFIGURE {
    my ($class, $self) = @_;

    my $vars = $self->{vars};

    my $target_dir = catdir $vars->{documentroot}, "util";

    while (my($file, $code) = each %scripts) {
        $file = catfile $target_dir, "$file.pl";
        $self->write_perlscript($file, "$code\n");
    }
}

sub handler {
    my $r = shift;

    my $cfg = Apache::Test::config();
    my $vars = $cfg->{vars};

    # XXX: these tests randomly fail under 5.6.1
    plan $r, todo => [1..4], tests => 4;

    my $target_dir = catfile $vars->{documentroot}, "util";

    {
        # test: passing argv + scalar context
        my $command = catfile $target_dir, "argv.pl";
        my @argv = qw(foo bar);
        my $out = Apache::SubProcess::spawn_proc_prog($r, $command, \@argv);
        ok t_cmp(\@argv,
                 [split / /, <$out>],
                 "passing ARGV"
                );
    }

    {
        # test: passing env to subprocess through subprocess_env
        my $command = catfile $target_dir, "env.pl";
        my $value = "my cool proc";
        $r->subprocess_env->set(SubProcess => $value);
        my $out = Apache::SubProcess::spawn_proc_prog($r, $command);
        ok t_cmp($value,
                 <$out>,
                 "passing env via subprocess_env"
                );
    }

    {
        # test: subproc's stdin -> stdout + list context
        my $command = catfile $target_dir, "in_out.pl";
        my $value = "my cool proc\n"; # must have \n for <IN>
        my ($in, $out, $err) = 
            Apache::SubProcess::spawn_proc_prog($r, $command);
        print $in $value;
        ok t_cmp($value,
                 <$out>,
                 "testing subproc's stdin -> stdout + list context"
                );
    }

    {
        # test: subproc's stdin -> stderr + list context
        my $command = catfile $target_dir, "in_err.pl";
        my $value = "my stderr\n"; # must have \n for <IN>
        my ($in, $out, $err) = 
            Apache::SubProcess::spawn_proc_prog($r, $command);
        print $in $value;
        ok t_cmp($value,
                 <$err>,
                 "testing subproc's stdin -> stderr + list context"
                );
    }

# could test send_fd($out), send_fd($err), but currently it's only in
# compat.pm.

# these are wannabe's
#    ok t_cmp(
#             Apache::SUCCESS,
#             Apache::SubProcess::spawn_proc_sub($r, $sub, \@args),
#             "spawn a subprocess and run a subroutine in it"
#            );

#    ok t_cmp(
#             Apache::SUCCESS,
#             Apache::SubProcess::spawn_thread_prog($r, $command, \@argv),
#             "spawn thread and run a program in it"
#            );

#     ok t_cmp(
#             Apache::SUCCESS,
#             Apache::SubProcess::spawn_thread_sub($r, $sub, \@args),
#             "spawn thread and run a subroutine in it"
#            );

   Apache::OK;
}


1;


{'fraction_non_alphanumeric': 0.15039811265113537, 'fraction_numerical': 0.0020642878207018578, 'mean_word_length': 2.0133274100399823, 'pattern_counts': {'":': 0, '<': 19, '<?xml version=': 0, '>': 59, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 0, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '1348386', 'n_tokens_mistral': 2268, 'n_tokens_neox': 2216, 'n_words': 577}