1 # Copyright (C) 2018 all contributors <meta@public-inbox.org>
2 # License: AGPL-3.0+ <https://www.gnu.org/licenses/agpl-3.0.txt>
7 use File::Temp qw/tempdir/;
8 use Fcntl qw(SEEK_SET);
12 foreach my $mod (qw(DBD::SQLite Search::Xapian)) {
14 plan skip_all => "$mod missing for v2mda.t" if $@;
16 use_ok 'PublicInbox::V2Writable';
17 my $tmpdir = tempdir('pi-v2mda-XXXXXX', TMPDIR => 1, CLEANUP => 1);
19 mainrepo => "$tmpdir/inbox",
20 name => 'test-v2writable',
21 address => [ 'test@example.com' ],
23 my $mime = PublicInbox::MIME->create(
25 From => 'a@example.com',
26 To => 'test@example.com',
27 Subject => 'this is a subject',
28 Date => 'Fri, 02 Oct 1993 00:00:00 +0000',
29 'Message-ID' => '<foo@bar>',
30 'List-ID' => '<test.example.com>',
32 body => "hello world\n",
35 my $mda = "blib/script/public-inbox-mda";
36 ok(-f "blib/script/public-inbox-mda", '-mda exists');
37 my $main_bin = getcwd()."/t/main-bin";
38 local $ENV{PI_DIR} = "$tmpdir/foo";
39 local $ENV{PATH} = "$main_bin:blib/script:$ENV{PATH}";
40 local $ENV{PI_EMERGENCY} = "$tmpdir/fail";
41 ok(mkdir "$tmpdir/fail");
43 my @cmd = (qw(public-inbox-init), "-V$V", $ibx->{name},
44 $ibx->{mainrepo}, 'http://localhost/test',
45 $ibx->{address}->[0]);
46 ok(PublicInbox::Import::run_die(\@cmd), 'initialized v2 inbox');
48 open my $tmp, '+>', undef or die "failed to open anonymous tempfile: $!";
49 ok($tmp->print($mime->as_string), 'wrote to temporary file');
50 ok($tmp->flush, 'flushed temporary file');
51 ok($tmp->sysseek(0, SEEK_SET), 'seeked');
53 my $rdr = { 0 => fileno($tmp) };
54 local $ENV{ORIGINAL_RECIPIENT} = 'test@example.com';
55 ok(PublicInbox::Import::run_die(['public-inbox-mda'], undef, $rdr),
56 'mda delivered a message');
58 $ibx = PublicInbox::Inbox->new($ibx);
61 my $cmd = [ 'public-inbox-index', "$tmpdir/inbox" ];
62 ok(PublicInbox::Import::run_die($cmd, undef, $rdr), 'v1 indexed');
64 my $msgs = $ibx->search->query('');
65 my $saved = $ibx->smsg_mime($msgs->[0]);
66 is($saved->{mime}->as_string, $mime->as_string, 'injected message');