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