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);
11 foreach my $mod (qw(DBD::SQLite Search::Xapian)) {
13 plan skip_all => "$mod missing for v2mda.t" if $@;
15 use_ok 'PublicInbox::V2Writable';
16 my $tmpdir = tempdir('pi-v2mda-XXXXXX', TMPDIR => 1, CLEANUP => 1);
18 mainrepo => "$tmpdir/inbox",
19 name => 'test-v2writable',
20 address => [ 'test@example.com' ],
22 my $mime = PublicInbox::MIME->create(
24 From => 'a@example.com',
25 To => 'test@example.com',
26 Subject => 'this is a subject',
27 Date => 'Fri, 02 Oct 1993 00:00:00 +0000',
28 'Message-ID' => '<foo@bar>',
29 'List-ID' => '<test.example.com>',
31 body => "hello world\n",
34 my $mda = "blib/script/public-inbox-mda";
35 ok(-f "blib/script/public-inbox-mda", '-mda exists');
36 my $main_bin = getcwd()."/t/main-bin";
37 local $ENV{PI_DIR} = "$tmpdir/foo";
38 local $ENV{PATH} = "$main_bin:blib/script:$ENV{PATH}";
39 local $ENV{PI_EMERGENCY} = "$tmpdir/fail";
40 ok(mkdir "$tmpdir/fail");
41 my @cmd = (qw(public-inbox-init -V2), $ibx->{name},
42 $ibx->{mainrepo}, 'http://localhost/test',
43 $ibx->{address}->[0]);
44 ok(PublicInbox::Import::run_die(\@cmd), 'initialized v2 inbox');
46 open my $tmp, '+>', undef or die "failed to open anonymous tempfile: $!";
47 ok($tmp->print($mime->as_string), 'wrote to temporary file');
48 ok($tmp->flush, 'flushed temporary file');
49 ok($tmp->sysseek(0, SEEK_SET), 'seeked');
51 my $rdr = { 0 => fileno($tmp) };
52 local $ENV{ORIGINAL_RECIPIENT} = 'test@example.com';
53 ok(PublicInbox::Import::run_die(['public-inbox-mda'], undef, $rdr),
54 'mda delivered a message');
56 $ibx = PublicInbox::Inbox->new($ibx);
57 my $msgs = $ibx->search->query('');
58 my $saved = $ibx->smsg_mime($msgs->[0]);
59 is($saved->{mime}->as_string, $mime->as_string, 'injected message');