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