use PublicInbox::Spawn qw(popen_rd which);
use List::Util qw(shuffle);
require_mods(qw(DBD::SQLite));
+require PublicInbox::MdirReader;
require PublicInbox::MboxReader;
require PublicInbox::LeiOverview;
+require PublicInbox::LEI;
use_ok 'PublicInbox::LeiToMail';
my $from = "Content-Length: 10\nSubject: x\n\nFrom hell\n";
my $noeol = "Subject: x\n\nFrom hell";
my ($tmpdir, $for_destroy) = tmpdir();
local $ENV{TMPDIR} = $tmpdir;
open my $err, '>>', "$tmpdir/lei.err" or BAIL_OUT $!;
-my $lei = { 2 => $err };
+my $lei = bless { 2 => $err }, 'PublicInbox::LEI';
+my $commit = sub {
+ $_[0] = undef; # wcb
+ delete $lei->{1};
+};
my $buf = <<'EOM';
From: x@example.com
Subject: x
my $dup = Storable::thaw(Storable::freeze($l2m));
is_deeply($dup, $l2m, "$fmt round-trips through storable");
}
- my $zpipe = $l2m->pre_augment($lei);
+ $l2m->pre_augment($lei);
$l2m->do_augment($lei);
- $l2m->post_augment($lei, $zpipe);
- my $cb = $l2m->write_cb($lei);
- delete $lei->{1};
- $cb;
+ $l2m->post_augment($lei);
+ $l2m->write_cb($lei);
};
my $deadbeef = { blob => 'deadbeef', kw => [ qw(seen) ] };
is(ref $wcb, 'CODE', 'write_cb returned callback');
ok(-f $fn && !-s _, 'empty file created');
$wcb->(\(my $dup = $buf), $deadbeef);
- undef $wcb;
+ $commit->($wcb);
open my $fh, '<', $fn or BAIL_OUT $!;
my $raw = do { local $/; <$fh> };
like($raw, qr/^blah\n/sm, 'wrote content');
$wcb = $wcb_get->($mbox, $fn);
ok(-f $fn && !-s _, 'truncated mbox destination');
$wcb->(\($dup = $buf), $deadbeef);
- undef $wcb;
+ $commit->($wcb);
open $fh, '<', $fn or BAIL_OUT $!;
is(do { local $/; <$fh> }, $raw, 'jobs > 1');
$raw;
};
+
+test_lei(sub {
+ ok(lei(qw(import -f), $mbox, $fn), 'imported mbox');
+ ok(lei(qw(q s:x)), 'lei q works') or diag $lei_err;
+ my $res = json_utf8->decode($lei_out);
+ my $x = $res->[0];
+ is($x->{'s'}, 'x', 'subject imported') or diag $lei_out;
+ is_deeply($x->{'kw'}, ['seen'], 'kw imported') or diag $lei_out;
+ is($res->[1], undef, 'only one result');
+});
+
+test_lei(sub {
+ lei_ok('import', "$mbox:$fn", \'imported mbox:/path') or diag $lei_err;
+ lei_ok(qw(q s:x), \'lei q works') or diag $lei_err;
+ my $res = json_utf8->decode($lei_out);
+ my $x = $res->[0];
+ is($x->{'s'}, 'x', 'subject imported') or diag $lei_out;
+ is_deeply($x->{'kw'}, ['seen'], 'kw imported') or diag $lei_out;
+ is($res->[1], undef, 'only one result');
+});
+
for my $zsfx (qw(gz bz2 xz)) { # XXX should we support zst, zz, lzo, lzma?
my $zsfx2cmd = PublicInbox::LeiToMail->can('zsfx2cmd');
SKIP: {
my $f = "$fn.$zsfx";
my $wcb = $wcb_get->($mbox, $f);
$wcb->(\(my $dup = $buf), $deadbeef);
- undef $wcb;
+ $commit->($wcb);
my $uncompressed = xqx([@$dc_cmd, $f]);
is($uncompressed, $orig, "$zsfx works unlocked");
unlink $f or BAIL_OUT "unlink $!";
$wcb = $wcb_get->($mbox, $f);
$wcb->(\($dup = $buf), $deadbeef);
- undef $wcb;
+ $commit->($wcb);
is(xqx([@$dc_cmd, $f]), $orig, "$zsfx matches with lock");
local $lei->{opt} = { augment => 1 };
$wcb = $wcb_get->($mbox, $f);
$wcb->(\($dup = $buf . "\nx\n"), $deadbeef);
- undef $wcb; # commit
+ $commit->($wcb);
my $cat = popen_rd([@$dc_cmd, $f]);
my @raw;
local $lei->{opt} = { augment => 1, jobs => 2 };
$wcb = $wcb_get->($mbox, $f);
$wcb->(\($dup = $buf . "\ny\n"), $deadbeef);
- undef $wcb; # commit
+ $commit->($wcb);
my @raw3;
$cat = popen_rd([@$dc_cmd, $f]);
my $wcb = $wcb_get->('mboxo', $fn);
$deadbeef->{kw} = [];
$wcb->(\(my $x = $buf), $deadbeef) for (1..2);
- undef $wcb; # undef to commit changes
+ $commit->($wcb);
my $cmp = '';
open my $fh, '<', $fn or BAIL_OUT $!;
PublicInbox::MboxReader->mboxo($fh, sub { $cmp .= $as_orig->(@_) });
local $lei->{opt} = { augment => 1 };
$wcb = $wcb_get->('mboxo', $fn);
$wcb->(\($x = $buf . "\nx\n"), $deadbeef) for (1..2);
- undef $wcb; # undef to commit changes
+ $commit->($wcb);
open $fh, '<', $fn or BAIL_OUT $!;
my @x;
PublicInbox::MboxReader->mboxo($fh, sub { push @x, $as_orig->(@_) });
local $lei->{1} = $tmp;
my $wcb = $wcb_get->('mboxrd', '/dev/stdout');
$wcb->(\(my $x = $buf), $deadbeef);
- undef $wcb; # commit
+ $commit->($wcb);
seek($tmp, 0, SEEK_SET) or BAIL_OUT $!;
my $cmp = '';
PublicInbox::MboxReader->mboxrd($tmp, sub { $cmp .= $as_orig->(@_) });
my $cat = popen_rd([which('cat'), $fn]);
my $wcb = $wcb_get->('mboxo', $fn);
$wcb->(\(my $x = $buf), $deadbeef);
- undef $wcb; # commit
+ $commit->($wcb);
my $cmp = '';
PublicInbox::MboxReader->mboxo($cat, sub { $cmp .= $as_orig->(@_) });
is($cmp, $buf, 'message written to FIFO');
}
{ # Maildir support
+ my $each_file = PublicInbox::MdirReader->can('maildir_each_file');
my $md = "$tmpdir/maildir/";
my $wcb = $wcb_get->('maildir', $md);
is(ref($wcb), 'CODE', 'got Maildir callback');
$wcb->(\(my $x = $buf), $b4dc0ffee);
my @f;
- PublicInbox::LeiToMail::_maildir_each_file($md, sub { push @f, shift });
+ $each_file->($md, sub { push @f, shift });
open my $fh, $f[0] or BAIL_OUT $!;
is(do { local $/; <$fh> }, $buf, 'wrote to Maildir');
$wcb->(\($x = $buf."\nx\n"), $deadcafe);
my @x = ();
- PublicInbox::LeiToMail::_maildir_each_file($md, sub { push @x, shift });
+ $each_file->($md, sub { push @x, shift });
is(scalar(@x), 1, 'wrote one new file');
ok(!-f $f[0], 'old file clobbered');
open $fh, $x[0] or BAIL_OUT $!;
$wcb->(\($x = $buf."\ny\n"), $deadcafe);
$wcb->(\($x = $buf."\ny\n"), $b4dc0ffee); # skipped by dedupe
@f = ();
- PublicInbox::LeiToMail::_maildir_each_file($md, sub { push @f, shift });
+ $each_file->($md, sub { push @f, shift });
is(scalar grep(/\A\Q$x[0]\E\z/, @f), 1, 'old file still there');
my @new = grep(!/\A\Q$x[0]\E\z/, @f);
is(scalar @new, 1, '1 new file written (b4dc0ffee skipped)');