<commit_before>#!/usr/bin/perl
use warnings;

$tim = '../tim';
$tdir = '/tmp/tim.test';
mkdir $tdir;

END {
	my $ec = $?;
	system('rm', '-rf', $tdir);
	$? = $ec;
}

sub to_file
{
	my($fnam, @l) = @_;
	open F, '>', $fnam or die "open $fnam: $!\n";
	print F "$_\n" for @l;
	close F;
}

sub runtest
{
	my $test = shift;
	my $file = "$tdir/file";
	my $expected = "$tdir/expected";

	my(@f_begin, @f_end, @cmds);

	my $target = \@cmds;

	open F, '<', $test or die "open $test: $!\n";
	while(<F>){
		chomp;

		if(/^__IN__$/){
			$target = \@f_begin;
		}elsif(/^__OUT__$/){
			$target = \@f_end;
		}else{
			push @{$target}, $_;
		}
	}

	to_file $file, @f_begin;

	my $rc = run_tim($file, join('', @cmds) . ":wq");

	if($rc != 0){
		print "failure: (exit code) $test\n";
	}else{
		to_file $expected, @f_end;

		$rc = system('diff', '-u', $expected, $file);
		printf "%s: %s\n", $rc ? "failure" : "success", $test;
	}

	return $rc;
}

sub run_tim
{
	my $pid = fork();

	if($pid == 0){
		my $inp = "$tdir/inp";
		my($file, $cmds) = @_;
		to_file $inp, $cmds;

		open STDIN, '<', $inp or die;
		exec $tim, $file;
		die;
	}

	my $dead = wait;
	my $rc = $?;

	die "bad reaped proc $dead != $pid"<commit_msg>More descriptive test output<commit_after>#!/usr/bin/perl
use warnings;

$tim = '../tim';
$tdir = '/tmp/tim.test';
mkdir $tdir;

END {
	my $ec = $?;
	system('rm', '-rf', $tdir);
	$? = $ec;
}

sub to_file
{
	my($fnam, @l) = @_;
	open F, $fnam or die "open $fnam: $!\n";
	print F "$_\n" for @l;
	close F;
}

sub runtest
{
	my $test = shift;
	my $file = "$tdir/file";
	my $expected = "$tdir/expected";

	my(@f_begin, @f_end, @cmds);

	my $target = \@cmds;

	open F, '<', $test or die "open $test: $!\n";
	while(<F>){
		chomp;

		if(/^__IN__$/){
			$target = \@f_begin;
		}elsif(/^__OUT__$/){
			$target = \@f_end;
		}else{
			push @{$target}, $_;
		}
	}

	to_file ">$file", @f_begin;

	my $rc = run_tim($file, join('', @cmds) . ":wq");

	if($rc != 0){
		print "failure: (exit code) $test\n";
	}else{
		to_file ">$expected", @f_end;

		my @diff = map { chomp; $_ } `diff -u '$expected' '$file'`;
		$rc = $?;
		printf "%s: %s\n", $rc ? "failure" : "success", $test;

		to_file '| sed -n l', @diff[2 .. $#diff];
	}

	return $rc;
}

sub run_tim
{
	my $pid = fork();

	if($pid == 0){
		my $inp = "$tdir/inp";
		my($file, $cmds) = @_;
		to_file ">$inp", $cmds;

		open STDIN, '<', $inp or die;
		exec $tim, $file;
		die;
	}

	my $dead = wait;
	my $rc = $?;

	die "bad reaped proc $dead != $pid"
{'fraction_non_alphanumeric': 0.2540322580645161, 'fraction_numerical': 0.0020161290322580645, 'mean_word_length': 3.0275974025974026, 'pattern_counts': {'":': 2, '<': 9, '<?xml version=': 0, '>': 9, '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': '167625', 'n_tokens_mistral': 1252, 'n_tokens_neox': 1165, 'n_words': 292}