<commit_before>package Smolder::Build;
use strict;
use warnings;
use base 'Module::Build::TAPArchive';
use File::Temp;
use Cwd qw(cwd);
use File::Spec::Functions qw(catdir catfile tmpdir curdir rel2abs abs2rel splitdir);
use IPC::Run qw(start finish pump);
use LWP::UserAgent;
use WWW::Mechanize;
use File::Find ();
use File::Copy qw(copy);

my $HOSTNAME = 'localhost.localdomain';
my $PORT     = '112234';

=head1 NAME

Smolder::Build

=head1 DESCRIPTION

L<Module::Build> subclass for Smolder specific testing and installation.

=head1 OVERRIDDEN ACTIONS

=head2 test

Make sure that we test against a new, empty SQLite db and that Smolder
is up and running before running the test files. Then make sure we shut
down Smolder when done.

=cut

sub ACTION_test {
    my $self = shift;
    $self->_wrap_test_action('test');
}

sub ACTION_test_archive {
    my $self = shift;
    $self->_wrap_test_action('test_archive');
}

sub _wrap_test_action {
    my ($self, $action) = @_;
    my $cwd = cwd();

    # create a temporary database
    my $tmp_dir = File::Temp->newdir(template => 'smolder-XXXXXX');
    my $conf =
        "Port $PORT\nHostname $HOSTNAME\n"
      . "FromAddress smolder\@$HOSTNAME\nSecret ad01i11932lsk\n"
      . "TemplateDir '"
      . catdir($cwd, 'templates') . "'\n"
      . "HtdocsDir '"
      . catdir($cwd, 'htdocs') . "'\n"
      . "SQLDir '"
      . catdir($cwd, 'sql') . "'\n"
      . "DataDir '"
      . $tmp_dir->dirname . "'\n";

    my $tmp_conf = File::Temp->new(template => 'smolder-XXXXXX', suffix => '.conf', dir => tmpdir);
    print $tmp_conf $conf;
    close $tmp_conf;
    $ENV{SMOLDER_CONF}                 = $tmp_conf->filename;
    $ENV{SMOLDER_TEST_HARNESS_ARCHIVE} = 1;

    # start the smolder server
    my ($in, $out, $err);
    my $subprocess = start(["$cwd/bin/smolder"], \$in, \$out, \$err);
    my $tries = 0;
    warn "Waiting for Smolder to start...\n";
    while (!_is_smolder_running() && $tries < 7) {
        sleep(3);
        $tries++;
    }

    my $method = "SUPER::ACTION_$action";
    {

        # make sure depends_on('code') doesn't get run since we've already taken care of it
        local *Module::Build::TAPArchive::depends_on = sub {
            my ($self, @args) = @_;
            if ($args[0] && $args[0] ne 'code') {
                return $self->SUPER::depends_on(@args);
            }
        };
        $self->$method(@_);
    }

    # finish() seems to hang, so just kill it
    $subprocess->kill_kill;
}

sub _is_smolder_running {
    my $url = "http://$HOSTNAME:$PORT/app";
<commit_msg>make sure we have a DB before running the tests and dont rely on smolder to create it on startup in case it doesnt start up<commit_after>package Smolder::Build;
use strict;
use warnings;
use base 'Module::Build::TAPArchive';
use File::Temp;
use Cwd qw(cwd);
use File::Spec::Functions qw(catdir catfile tmpdir curdir rel2abs abs2rel splitdir);
use IPC::Run qw(start finish pump);
use LWP::UserAgent;
use WWW::Mechanize;
use File::Find ();
use File::Copy qw(copy);

my $HOSTNAME = 'localhost.localdomain';
my $PORT     = '112234';

=head1 NAME

Smolder::Build

=head1 DESCRIPTION

L<Module::Build> subclass for Smolder specific testing and installation.

=head1 OVERRIDDEN ACTIONS

=head2 test

Make sure that we test against a new, empty SQLite db and that Smolder
is up and running before running the test files. Then make sure we shut
down Smolder when done.

=cut

sub ACTION_test {
    my $self = shift;
    $self->_wrap_test_action('test');
}

sub ACTION_test_archive {
    my $self = shift;
    $self->_wrap_test_action('test_archive');
}

sub _wrap_test_action {
    my ($self, $action) = @_;
    my $cwd = cwd();

    # create a temporary database
    my $tmp_dir = File::Temp->newdir(template => 'smolder-XXXXXX');
    my $conf =
        "Port $PORT\nHostname $HOSTNAME\n"
      . "FromAddress smolder\@$HOSTNAME\nSecret ad01i11932lsk\n"
      . "TemplateDir '"
      . catdir($cwd, 'templates') . "'\n"
      . "HtdocsDir '"
      . catdir($cwd, 'htdocs') . "'\n"
      . "SQLDir '"
      . catdir($cwd, 'sql') . "'\n"
      . "DataDir '"
      . $tmp_dir->dirname . "'\n";

    my $tmp_conf = File::Temp->new(template => 'smolder-XXXXXX', suffix => '.conf', dir => tmpdir);
    print $tmp_conf $conf;
    close $tmp_conf;
    $ENV{SMOLDER_CONF}                 = $tmp_conf->filename;
    $ENV{SMOLDER_TEST_HARNESS_ARCHIVE} = 1;

    # make sure we create a DB first. Smolder will do this when it starts,
    # but we still want to run some tests even if we fail to start smolder
    $self->depends_on('db');

    # start the smolder server
    my ($in, $out, $err);
    my $subprocess = start(["$cwd/bin/smolder"], \$in, \$out, \$err);
    my $tries = 0;
    warn "Waiting for Smolder to start...\n";
    while (!_is_smolder_running() && $tries < 7) {
        sleep(3);
        $tries++;
    }

    my $method = "SUPER::ACTION_$action";
    {

        # make sure depends_on('code') doesn't get run since we've already taken care of it
        local *Module::Build::TAPArchive::depends_on = sub {
            my ($self, @args) = @_;
            if ($args[0] && $args[0] ne 'code') {
                return $self->SUPER::depends_on(@args);
            }
        };
        $self->$method(@_);
    }

    # finish() seems to hang, so just kill it
    $subprocess->kill_kill;
}

sub _is_smolder_running {
    my $url = "http://$HOSTNAME:$PORT/app";

{'fraction_non_alphanumeric': 0.1482847657690889, 'fraction_numerical': 0.009221689413500553, 'mean_word_length': 2.9098774333093007, 'pattern_counts': {'":': 0, '<': 7, '<?xml version=': 0, '>': 32, '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': '3177355', 'n_tokens_mistral': 1934, 'n_tokens_neox': 1861, 'n_words': 525}