]> Sergey Matveev's repositories - public-inbox.git/blob - t/watch_filter_rubylang.t
remove hard Devel::Peek dependency and lazy load for daemons
[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 Test::More;
6 use File::Temp qw/tempdir/;
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::V2Writable';
16 use_ok 'PublicInbox::WatchMaildir';
17 use_ok 'PublicInbox::Emergency';
18 my $tmpdir = tempdir('watch-XXXXXX', TMPDIR => 1, CLEANUP => 1);
19 local $ENV{PI_CONFIG} = "$tmpdir/pi_config";
20
21 for my $v (qw(V1 V2)) {
22         my @warn;
23         $SIG{__WARN__} = sub { push @warn, @_ };
24         my $cfgpfx = "publicinbox.$v";
25         my $mainrepo = "$tmpdir/$v";
26         my $maildir = "$tmpdir/md-$v";
27         my $spamdir = "$tmpdir/spam-$v";
28         my $addr = "test-$v\@example.com";
29         my @cmd = ('blib/script/public-inbox-init', "-$v", $v, $mainrepo,
30                 "http://example.com/$v", $addr);
31         is(system(@cmd), 0, 'public-inbox init OK');
32         if ($v eq 'V1') {
33                 is(system('blib/script/public-inbox-index', $mainrepo), 0);
34         }
35         PublicInbox::Emergency->new($spamdir);
36
37         for my $i (1..15) {
38                 my $msg = <<EOF;
39 From: user\@example.com
40 To: $addr
41 Subject: blah $i
42 X-Mail-Count: $i
43 Message-Id: <a.$i\@b.com>
44 Date: Sat, 05 Jan 2019 04:19:17 +0000
45
46 something
47 EOF
48                 PublicInbox::Emergency->new($maildir)->prepare(\$msg);
49         }
50
51         my $spam = <<EOF;
52 From: spammer\@example.com
53 To: $addr
54 Subject: spam
55 X-Mail-Count: 99
56 Message-Id: <a.99\@b.com>
57 Date: Sat, 05 Jan 2019 04:19:17 +0000
58
59 spam
60 EOF
61         PublicInbox::Emergency->new($maildir)->prepare(\"$spam");
62
63         my %orig = (
64                 "$cfgpfx.address" => $addr,
65                 "$cfgpfx.mainrepo" => $mainrepo,
66                 "$cfgpfx.watch" => "maildir:$maildir",
67                 "$cfgpfx.filter" => 'PublicInbox::Filter::RubyLang',
68                 "$cfgpfx.altid" => 'serial:alerts:file=msgmap.sqlite3',
69                 "publicinboxwatch.watchspam" => "maildir:$spamdir",
70         );
71         my $config = PublicInbox::Config->new({%orig});
72         my $ibx = $config->lookup_name($v);
73         ok($ibx, 'found inbox by name');
74
75         my $w = PublicInbox::WatchMaildir->new($config);
76         for my $i (1..2) {
77                 $w->scan('full');
78         }
79
80         # make sure all serials are searchable:
81         my ($tot, $msgs);
82         for my $i (1..15) {
83                 ($tot, $msgs) = $ibx->search->query("alerts:$i");
84                 is($tot, 1, "got one result for alerts:$i");
85                 is($msgs->[0]->{mid}, "a.$i\@b.com", "got expected MID for $i");
86         }
87         ($tot, undef) = $ibx->search->query('b:spam');
88         is($tot, 1, 'got spam message');
89
90         my $nr = unlink <$maildir/new/*>;
91         is(16, $nr);
92         {
93                 PublicInbox::Emergency->new($spamdir)->prepare(\$spam);
94                 my @new = glob("$spamdir/new/*");
95                 my @p = split(m!/+!, $new[0]);
96                 ok(link($new[0], "$spamdir/cur/".$p[-1].":2,S"));
97                 is(unlink($new[0]), 1);
98         }
99         $w->scan('full');
100
101         $config = PublicInbox::Config->new({%orig});
102         $ibx = $config->lookup_name($v);
103         ($tot, undef) = $ibx->search->reopen->query('b:spam');
104         is($tot, 0, 'spam removed');
105
106         is_deeply([], \@warn, 'no warnings');
107 }
108
109 done_testing();