]> Sergey Matveev's repositories - public-inbox.git/blobdiff - t/v2mda.t
www: drop --subject from "git send-email" instructions
[public-inbox.git] / t / v2mda.t
index 61457208327702cf671a746a0693eaaf19e02a6b..8f2f335d96b8d834cfe5ffd38df13dfc6599595a 100644 (file)
--- a/t/v2mda.t
+++ b/t/v2mda.t
@@ -1,78 +1,99 @@
-# Copyright (C) 2018 all contributors <meta@public-inbox.org>
+#!perl -w
+# Copyright (C) all contributors <meta@public-inbox.org>
 # License: AGPL-3.0+ <https://www.gnu.org/licenses/agpl-3.0.txt>
+use v5.10.1;
 use strict;
-use warnings;
 use Test::More;
-use PublicInbox::MIME;
-use File::Temp qw/tempdir/;
 use Fcntl qw(SEEK_SET);
 use Cwd;
+use PublicInbox::TestCommon;
+use PublicInbox::Eml;
+require_git(2.6);
 
 my $V = 2;
-foreach my $mod (qw(DBD::SQLite Search::Xapian)) {
-       eval "require $mod";
-       plan skip_all => "$mod missing for v2mda.t" if $@;
-}
+require_mods(qw(DBD::SQLite Search::Xapian));
 use_ok 'PublicInbox::V2Writable';
-my $tmpdir = tempdir('pi-v2mda-XXXXXX', TMPDIR => 1, CLEANUP => 1);
+my ($tmpdir, $for_destroy) = tmpdir();
 my $ibx = {
-       mainrepo => "$tmpdir/inbox",
+       inboxdir => "$tmpdir/inbox",
        name => 'test-v2writable',
        address => [ 'test@example.com' ],
 };
-my $mime = PublicInbox::MIME->create(
-       header => [
-               From => 'a@example.com',
-               To => 'test@example.com',
-               Subject => 'this is a subject',
-               Date => 'Fri, 02 Oct 1993 00:00:00 +0000',
-               'Message-ID' => '<foo@bar>',
-               'List-ID' => '<test.example.com>',
-       ],
-       body => "hello world\n",
-);
+my $mime = PublicInbox::Eml->new(<<'EOF');
+From: a@example.com
+To: test@example.com
+Subject: this is a subject
+Date: Fri, 02 Oct 1993 00:00:00 +0000
+Message-ID: <foo@bar>
+List-ID: <test.example.com>
 
-my $mda = "blib/script/public-inbox-mda";
-ok(-f "blib/script/public-inbox-mda", '-mda exists');
+hello world
+EOF
 my $main_bin = getcwd()."/t/main-bin";
+my $fail_bin = getcwd()."/t/fail-bin";
 local $ENV{PI_DIR} = "$tmpdir/foo";
+my $fail_path = "$fail_bin:blib/script:$ENV{PATH}";
 local $ENV{PATH} = "$main_bin:blib/script:$ENV{PATH}";
-local $ENV{PI_EMERGENCY} = "$tmpdir/fail";
-ok(mkdir "$tmpdir/fail");
-
-my @cmd = (qw(public-inbox-init), "-V$V", $ibx->{name},
-               $ibx->{mainrepo}, 'http://localhost/test',
+my $faildir = "$tmpdir/fail";
+local $ENV{PI_EMERGENCY} = $faildir;
+ok(mkdir $faildir);
+my @cmd = (qw(-init), "-V$V", $ibx->{name},
+               $ibx->{inboxdir}, 'http://localhost/test',
                $ibx->{address}->[0]);
-ok(PublicInbox::Import::run_die(\@cmd), 'initialized v2 inbox');
+ok(run_script(\@cmd), 'initialized v2 inbox');
 
-open my $tmp, '+>', undef or die "failed to open anonymous tempfile: $!";
-ok($tmp->print($mime->as_string), 'wrote to temporary file');
-ok($tmp->flush, 'flushed temporary file');
-ok($tmp->sysseek(0, SEEK_SET), 'seeked');
-
-my $rdr = { 0 => fileno($tmp) };
+my $rdr = { 0 => \($mime->as_string) };
 local $ENV{ORIGINAL_RECIPIENT} = 'test@example.com';
-ok(PublicInbox::Import::run_die(['public-inbox-mda'], undef, $rdr),
-       'mda delivered a message');
+ok(run_script(['-mda'], undef, $rdr), 'mda delivered a message');
 
 $ibx = PublicInbox::Inbox->new($ibx);
+my $msgs = $ibx->over->recent;
+is(scalar(@$msgs), 1, 'only got one message');
+my $eml = $ibx->smsg_eml($msgs->[0]);
+is($eml->as_string, $mime->as_string, 'injected message');
+
+{
+       my @new = glob("$faildir/new/*");
+       is_deeply(\@new, [], 'nothing in faildir');
+       local $ENV{PATH} = $fail_path;
+       $mime->header_set('Message-ID', '<bar@foo>');
+       $rdr->{0} = \($mime->as_string);
+       ok(run_script(['-mda'], undef, $rdr), 'mda did not die on "spam"');
+       @new = glob("$faildir/new/*");
+       is(scalar(@new), 1, 'got a message in faildir');
+       $msgs = $ibx->over->recent;
+       is(scalar(@$msgs), 1, 'no new message');
+
+       my $config = "$ENV{PI_DIR}/config";
+       ok(-f $config, 'config exists');
+       my $k = 'publicinboxmda.spamcheck';
+       is(xsys('git', 'config', "--file=$config", $k, 'none'), 0,
+               'disabled spamcheck for mda');
+
+       ok(run_script(['-mda'], undef, $rdr), 'mda did not die');
+       my @again = glob("$faildir/new/*");
+       is_deeply(\@again, \@new, 'no new message in faildir');
+       $msgs = $ibx->over->recent;
+       is(scalar(@$msgs), 2, 'new message added OK');
+}
+
+{
+       my $patch = 't/data/0001.patch';
+       open my $fh, '<', $patch or die "failed to open $patch: $!\n";
+       $rdr->{0} = \(do { local $/; <$fh> });
+       ok(run_script(['-mda'], undef, $rdr), 'mda delivered a patch');
+       my $post = $ibx->search->reopen->mset('dfpost:6e006fd7');
+       is($post->size, 1, 'got one result for dfpost');
+       my $pre = $ibx->search->mset('dfpre:090d998');
+       is($pre->size, 1, 'got one result for dfpre');
+       $pre = $ibx->search->mset_to_smsg($ibx, $pre);
+       $post = $ibx->search->mset_to_smsg($ibx, $post);
+       is($post->[0]->{blob}, $pre->[0]->{blob}, 'same message in both cases');
 
-if ($V == 1) {
-       my $cmd = [ 'public-inbox-index', "$tmpdir/inbox" ];
-       ok(PublicInbox::Import::run_die($cmd, undef, $rdr), 'v1 indexed');
+       # git patch-id --stable <t/data/0001.patch | awk '{print $1}'
+       my $patchid = '91ee6b761fc7f47cad9f2b09b10489f313eb5b71';
+       my $mset = $ibx->search->mset("patchid:$patchid");
+       is($mset->size, 1, 'patchid search works');
 }
-my $msgs = $ibx->search->query('');
-my $saved = $ibx->smsg_mime($msgs->[0]);
-is($saved->{mime}->as_string, $mime->as_string, 'injected message');
 
-my $patch = 't/data/0001.patch';
-open my $fh, '<', $patch or die "failed to open $patch: $!\n";
-$rdr = { 0 => fileno($fh) };
-ok(PublicInbox::Import::run_die(['public-inbox-mda'], undef, $rdr),
-       'mda delivered a patch');
-my $post = $ibx->search->reopen->query('dfpost:6e006fd7');
-is(scalar(@$post), 1, 'got one result for dfpost');
-my $pre = $ibx->search->query('dfpre:090d998');
-is(scalar(@$pre), 1, 'got one result for dfpre');
-is($post->[0]->{blob}, $pre->[0]->{blob}, 'same message in both cases');
 done_testing();