]> Sergey Matveev's repositories - public-inbox.git/blob - t/watch_filter_rubylang.t
treewide: run update-copyrights from gnulib for 2019
[public-inbox.git] / t / watch_filter_rubylang.t
1 # Copyright (C) 2019-2020 all contributors <meta@public-inbox.org>
2 # License: AGPL-3.0+ <https://www.gnu.org/licenses/agpl-3.0.txt>
3 use strict;
4 use warnings;
5 use PublicInbox::TestCommon;
6 use Test::More;
7 use PublicInbox::MIME;
8 use PublicInbox::Config;
9 require_mods(qw(Filesys::Notify::Simple DBD::SQLite Search::Xapian));
10 use_ok 'PublicInbox::WatchMaildir';
11 use_ok 'PublicInbox::Emergency';
12 my ($tmpdir, $for_destroy) = tmpdir();
13 local $ENV{PI_CONFIG} = "$tmpdir/pi_config";
14
15 my @v = qw(V1);
16 SKIP: {
17         if (require_git(2.6, 1)) {
18                 use_ok 'PublicInbox::V2Writable';
19                 push @v, 'V2';
20         } else {
21                 skip 'git 2.6+ needed for V2', 40;
22         }
23 }
24
25 for my $v (@v) {
26         my @warn;
27         $SIG{__WARN__} = sub { push @warn, @_ };
28         my $cfgpfx = "publicinbox.$v";
29         my $inboxdir = "$tmpdir/$v";
30         my $maildir = "$tmpdir/md-$v";
31         my $spamdir = "$tmpdir/spam-$v";
32         my $addr = "test-$v\@example.com";
33         my @cmd = ('-init', "-$v", $v, $inboxdir,
34                 "http://example.com/$v", $addr);
35         ok(run_script(\@cmd), 'public-inbox init OK');
36         if ($v eq 'V1') {
37                 ok(run_script(['-index', $inboxdir]), 'v1 indexed');
38         }
39         PublicInbox::Emergency->new($spamdir);
40
41         for my $i (1..15) {
42                 my $msg = <<EOF;
43 From: user\@example.com
44 To: $addr
45 Subject: blah $i
46 X-Mail-Count: $i
47 Message-Id: <a.$i\@b.com>
48 Date: Sat, 05 Jan 2019 04:19:17 +0000
49
50 something
51 EOF
52                 PublicInbox::Emergency->new($maildir)->prepare(\$msg);
53         }
54
55         my $spam = <<EOF;
56 From: spammer\@example.com
57 To: $addr
58 Subject: spam
59 X-Mail-Count: 99
60 Message-Id: <a.99\@b.com>
61 Date: Sat, 05 Jan 2019 04:19:17 +0000
62
63 spam
64 EOF
65         PublicInbox::Emergency->new($maildir)->prepare(\"$spam");
66
67         my $orig = <<EOF;
68 $cfgpfx.address=$addr
69 $cfgpfx.inboxdir=$inboxdir
70 $cfgpfx.watch=maildir:$maildir
71 $cfgpfx.filter=PublicInbox::Filter::RubyLang
72 $cfgpfx.altid=serial:alerts:file=msgmap.sqlite3
73 publicinboxwatch.watchspam=maildir:$spamdir
74 EOF
75         my $config = PublicInbox::Config->new(\$orig);
76         my $ibx = $config->lookup_name($v);
77         ok($ibx, 'found inbox by name');
78
79         my $w = PublicInbox::WatchMaildir->new($config);
80         for my $i (1..2) {
81                 $w->scan('full');
82         }
83
84         # make sure all serials are searchable:
85         my ($tot, $msgs);
86         for my $i (1..15) {
87                 ($tot, $msgs) = $ibx->search->query("alerts:$i");
88                 is($tot, 1, "got one result for alerts:$i");
89                 is($msgs->[0]->{mid}, "a.$i\@b.com", "got expected MID for $i");
90         }
91         ($tot, undef) = $ibx->search->query('b:spam');
92         is($tot, 1, 'got spam message');
93
94         my $nr = unlink <$maildir/new/*>;
95         is(16, $nr);
96         {
97                 PublicInbox::Emergency->new($spamdir)->prepare(\$spam);
98                 my @new = glob("$spamdir/new/*");
99                 my @p = split(m!/+!, $new[0]);
100                 ok(link($new[0], "$spamdir/cur/".$p[-1].":2,S"));
101                 is(unlink($new[0]), 1);
102         }
103         $w->scan('full');
104
105         $config = PublicInbox::Config->new(\$orig);
106         $ibx = $config->lookup_name($v);
107         ($tot, undef) = $ibx->search->reopen->query('b:spam');
108         is($tot, 0, 'spam removed');
109
110         is_deeply([], \@warn, 'no warnings');
111 }
112
113 done_testing();