lei/store: stop shard workers + cat-file on idle
[public-inbox.git] / t / watch_maildir_v2.t
blobfa86f7bf80362b88281573476e21dbd85ee10bef
1 # Copyright (C) all contributors <meta@public-inbox.org>
2 # License: AGPL-3.0+ <https://www.gnu.org/licenses/agpl-3.0.txt>
3 use v5.12;
4 use PublicInbox::Eml;
5 use Cwd;
6 use PublicInbox::TestCommon;
7 use PublicInbox::Import;
8 require_git(2.6);
9 require_mods(qw(Xapian DBD::SQLite));
10 require PublicInbox::V2Writable;
11 my ($tmpdir, $for_destroy) = tmpdir();
12 my $inboxdir = "$tmpdir/v2";
13 my $maildir = "$tmpdir/md";
14 my $spamdir = "$tmpdir/spam";
15 use_ok 'PublicInbox::Watch';
16 use_ok 'PublicInbox::Emergency';
17 my $cfgpfx = "publicinbox.test";
18 my $addr = 'test-public@example.com';
19 my @cmd = ('-init', '-V2', 'test', $inboxdir,
20         'http://example.com/v2list', $addr);
21 local $ENV{PI_CONFIG} = "$tmpdir/pi_config";
22 ok(run_script(\@cmd), 'public-inbox init OK');
24 my $msg = <<EOF;
25 From: user\@example.com
26 To: $addr
27 Subject: spam
28 Message-Id: <a\@b.com>
29 Date: Sat, 18 Jun 2016 00:00:00 +0000
31 something
32 EOF
33 PublicInbox::Emergency->new($maildir)->prepare(\$msg);
34 ok(POSIX::mkfifo("$maildir/cur/fifo", 0777),
35         'create FIFO to ensure we do not get stuck on it :P');
36 my $sem = PublicInbox::Emergency->new($spamdir); # create dirs
38 my $orig = <<EOF;
39 [publicinbox "test"]
40         address = $addr
41         inboxdir = $inboxdir
42         watch = maildir:$maildir
43         filter = PublicInbox::Filter::Vger
44 [publicinboxlearn]
45         watchspam = maildir:$spamdir
46 EOF
47 my $cfg = cfg_new $tmpdir, $orig;
48 my $ibx = $cfg->lookup_name('test');
49 ok($ibx, 'found inbox by name');
50 $ibx->{-no_fsync} = 1;
52 PublicInbox::Watch->new($cfg)->scan('full');
53 my $total = scalar @{$ibx->over->recent};
54 is($total, 1, 'got one revision');
56 # my $git = PublicInbox::Git->new("$inboxdir/git/0.git");
57 # my @list = $git->qx(qw(rev-list refs/heads/master));
58 # is(scalar @list, 1, 'one revision in rev-list');
60 my $write_spam = sub {
61         is(scalar glob("$spamdir/new/*"), undef, 'no spam existing');
62         $sem->prepare(\$msg);
63         $sem->commit;
64         my @new = glob("$spamdir/new/*");
65         is(scalar @new, 1);
66         my @p = split(m!/+!, $new[0]);
67         ok(link($new[0], "$spamdir/cur/".$p[-1].":2,S"));
68         is(unlink($new[0]), 1);
70 $write_spam->();
71 is(unlink(glob("$maildir/new/*")), 1, 'unlinked old spam');
72 PublicInbox::Watch->new($cfg)->scan('full');
73 is_deeply($ibx->over->recent, [], 'deleted file');
74 is(unlink(glob("$spamdir/cur/*")), 1, 'unlinked trained spam');
76 # check with scrubbing
78         $msg .= qq(--
79 To unsubscribe from this list: send the line "unsubscribe git" in
80 the body of a message to majordomo\@vger.kernel.org
81 More majordomo info at  http://vger.kernel.org/majordomo-info.html\n);
82         PublicInbox::Emergency->new($maildir)->prepare(\$msg);
83         PublicInbox::Watch->new($cfg)->scan('full');
84         my $msgs = $ibx->over->recent;
85         is(scalar(@$msgs), 1, 'got one file back');
86         my $mref = $ibx->msg_by_smsg($msgs->[0]);
87         like($$mref, qr/something\n\z/s, 'message scrubbed on import');
89         is(unlink(glob("$maildir/new/*")), 1, 'unlinked spam');
90         $write_spam->();
91         PublicInbox::Watch->new($cfg)->scan('full');
92         $msgs = $ibx->over->recent;
93         is(scalar(@$msgs), 0, 'inbox is empty again');
94         is(unlink(glob("$spamdir/cur/*")), 1, 'unlinked trained spam');
98         my $fail_bin = getcwd()."/t/fail-bin";
99         ok(-x "$fail_bin/spamc", "mock spamc exists");
100         my $fail_path = "$fail_bin:$ENV{PATH}"; # for spamc ham mock
101         local $ENV{PATH} = $fail_path;
102         PublicInbox::Emergency->new($maildir)->prepare(\$msg);
103         $cfg->{'publicinboxwatch.spamcheck'} = 'spamc';
104         {
105                 local $SIG{__WARN__} = sub {}; # quiet spam check warning
106                 PublicInbox::Watch->new($cfg)->scan('full');
107         }
108         my $msgs = $ibx->over->recent;
109         is(scalar(@$msgs), 0, 'inbox is still empty');
110         is(unlink(glob("$maildir/new/*")), 1);
114         my $main_bin = getcwd()."/t/main-bin";
115         ok(-x "$main_bin/spamc", "mock spamc exists");
116         my $main_path = "$main_bin:$ENV{PATH}"; # for spamc ham mock
117         local $ENV{PATH} = $main_path;
118         PublicInbox::Emergency->new($maildir)->prepare(\$msg);
119         $cfg->{'publicinboxwatch.spamcheck'} = 'spamc';
120         PublicInbox::Watch->new($cfg)->scan('full');
121         my $msgs = $ibx->over->recent;
122         is(scalar(@$msgs), 1, 'inbox has one mail after spamc OK-ed a message');
123         my $mref = $ibx->msg_by_smsg($msgs->[0]);
124         like($$mref, qr/something\n\z/s, 'message scrubbed on import');
125         delete $cfg->{'publicinboxwatch.spamcheck'};
129         my $patch = 't/data/0001.patch';
130         open my $fh, '<', $patch or die "failed to open $patch: $!\n";
131         $msg = do { local $/; <$fh> };
132         PublicInbox::Emergency->new($maildir)->prepare(\$msg);
133         PublicInbox::Watch->new($cfg)->scan('full');
134         my $post = $ibx->search->reopen->mset('dfpost:6e006fd7');
135         is($post->size, 1, 'diff postimage found');
136         my $pre = $ibx->search->mset('dfpre:090d998b6c2c');
137         is($pre->size, 1, 'diff preimage found');
138         $pre = $ibx->search->mset_to_smsg($ibx, $pre);
139         $post = $ibx->search->mset_to_smsg($ibx, $post);
140         is(scalar(@$pre), 1, 'diff preimage found');
141         is($post->[0]->{blob}, $pre->[0]->{blob}, 'same message');
144 # multiple inboxes in the same maildir
146         my $v1repo = "$tmpdir/v1";
147         my $v1pfx = "publicinbox.v1";
148         my $v1addr = 'v1-public@example.com';
149         PublicInbox::Import::init_bare($v1repo);
150         my $cfg = cfg_new $tmpdir, <<EOF;
151 $orig
152 [publicinbox "v1"]
153         address = $v1addr
154         inboxdir = $v1repo
155         watch = maildir:$maildir
157         my $both = <<EOF;
158 From: user\@example.com
159 To: $addr, $v1addr
160 Subject: both
161 Message-Id: <both\@b.com>
162 Date: Sat, 18 Jun 2016 00:00:00 +0000
164 both
166         PublicInbox::Emergency->new($maildir)->prepare(\$both);
167         PublicInbox::Watch->new($cfg)->scan('full');
168         my $mset = $ibx->search->reopen->mset('m:both@b.com');
169         my $msgs = $ibx->search->mset_to_smsg($ibx, $mset);
170         my $v1 = $cfg->lookup_name('v1');
171         my $msg = $v1->git->cat_file($msgs->[0]->{blob});
172         is($both, $$msg, 'got original message back from v1');
173         $msg = $ibx->git->cat_file($msgs->[0]->{blob});
174         is($both, $$msg, 'got original message back from v2');
178         my $want = <<'EOF';
179 From: <u@example.com>
180 List-Id: <i.want.you.to.want.me>
181 Message-ID: <do.want@example.com>
183         my $do_not_want = <<'EOF';
184 From: <u@example.com>
185 List-Id: <do.not.want>
186 X-Mailing-List: no@example.com
187 Message-ID: <do.not.want@example.com>
189         PublicInbox::Emergency->new($maildir)->prepare(\$want);
190         PublicInbox::Emergency->new($maildir)->prepare(\$do_not_want);
191         my $raw = <<EOM;
192 $orig
193 [publicinbox "test"]
194         listid = i.want.you.to.want.me
196         my $cfg = cfg_new $tmpdir, $raw;
197         PublicInbox::Watch->new($cfg)->scan('full');
198         $ibx = $cfg->lookup_name('test');
199         my $num = $ibx->mm->num_for('do.want@example.com');
200         ok(defined $num, 'List-ID matched for watch');
201         $num = $ibx->mm->num_for('do.not.want@example.com');
202         is($num, undef, 'unaccepted List-ID matched for watch');
203         $raw .= "\twatchheader = X-Mailing-List:no\@example.com\n";
204         $cfg = cfg_new $tmpdir, $raw;
205         PublicInbox::Watch->new($cfg)->scan('full');
206         $ibx = $cfg->lookup_name('test');
207         $num = $ibx->mm->num_for('do.not.want@example.com');
208         ok(defined $num, 'X-Mailing-List matched');
211 done_testing;