]> Sergey Matveev's repositories - public-inbox.git/blob - t/watch_maildir.t
doc: txt2pre: auto-linkify manpage references
[public-inbox.git] / t / watch_maildir.t
1 # Copyright (C) 2016-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 Test::More;
5 use Email::MIME;
6 use Cwd;
7 use PublicInbox::Config;
8 use PublicInbox::TestCommon;
9 require_mods(qw(Filesys::Notify::Simple));
10 my ($tmpdir, $for_destroy) = tmpdir();
11 my $git_dir = "$tmpdir/test.git";
12 my $maildir = "$tmpdir/md";
13 my $spamdir = "$tmpdir/spam";
14 use_ok 'PublicInbox::WatchMaildir';
15 use_ok 'PublicInbox::Emergency';
16 my $cfgpfx = "publicinbox.test";
17 my $addr = 'test-public@example.com';
18 is(system(qw(git init -q --bare), $git_dir), 0, 'initialized git dir');
19
20 my $msg = <<EOF;
21 From: user\@example.com
22 To: $addr
23 Subject: spam
24 Message-Id: <a\@b.com>
25 Date: Sat, 18 Jun 2016 00:00:00 +0000
26
27 something
28 EOF
29 PublicInbox::Emergency->new($maildir)->prepare(\$msg);
30 ok(POSIX::mkfifo("$maildir/cur/fifo", 0777),
31         'create FIFO to ensure we do not get stuck on it :P');
32 my $sem = PublicInbox::Emergency->new($spamdir); # create dirs
33
34 my $config = PublicInbox::Config->new(\<<EOF);
35 $cfgpfx.address=$addr
36 $cfgpfx.inboxdir=$git_dir
37 $cfgpfx.watch=maildir:$maildir
38 $cfgpfx.filter=PublicInbox::Filter::Vger
39 publicinboxlearn.watchspam=maildir:$spamdir
40 EOF
41
42 PublicInbox::WatchMaildir->new($config)->scan('full');
43 my $git = PublicInbox::Git->new($git_dir);
44 my @list = $git->qx(qw(rev-list refs/heads/master));
45 is(scalar @list, 1, 'one revision in rev-list');
46
47 my $write_spam = sub {
48         is(scalar glob("$spamdir/new/*"), undef, 'no spam existing');
49         $sem->prepare(\$msg);
50         $sem->commit;
51         my @new = glob("$spamdir/new/*");
52         is(scalar @new, 1);
53         my @p = split(m!/+!, $new[0]);
54         ok(link($new[0], "$spamdir/cur/".$p[-1].":2,S"));
55         is(unlink($new[0]), 1);
56 };
57 $write_spam->();
58 is(unlink(glob("$maildir/new/*")), 1, 'unlinked old spam');
59 PublicInbox::WatchMaildir->new($config)->scan('full');
60 @list = $git->qx(qw(rev-list refs/heads/master));
61 is(scalar @list, 2, 'two revisions in rev-list');
62 @list = $git->qx(qw(ls-tree -r --name-only refs/heads/master));
63 is(scalar @list, 0, 'tree is empty');
64
65 # check with scrubbing
66 {
67         $msg .= qq(--
68 To unsubscribe from this list: send the line "unsubscribe git" in
69 the body of a message to majordomo\@vger.kernel.org
70 More majordomo info at  http://vger.kernel.org/majordomo-info.html\n);
71         PublicInbox::Emergency->new($maildir)->prepare(\$msg);
72         PublicInbox::WatchMaildir->new($config)->scan('full');
73         @list = $git->qx(qw(ls-tree -r --name-only refs/heads/master));
74         is(scalar @list, 1, 'tree has one file');
75         my $mref = $git->cat_file('HEAD:'.$list[0]);
76         like($$mref, qr/something\n\z/s, 'message scrubbed on import');
77
78         is(unlink(glob("$maildir/new/*")), 1, 'unlinked spam');
79         $write_spam->();
80         PublicInbox::WatchMaildir->new($config)->scan('full');
81         @list = $git->qx(qw(ls-tree -r --name-only refs/heads/master));
82         is(scalar @list, 0, 'tree is empty');
83         @list = $git->qx(qw(rev-list refs/heads/master));
84         is(scalar @list, 4, 'four revisions in rev-list');
85 }
86
87 {
88         my $fail_bin = getcwd()."/t/fail-bin";
89         ok(-x "$fail_bin/spamc", "mock spamc exists");
90         my $fail_path = "$fail_bin:$ENV{PATH}"; # for spamc ham mock
91         local $ENV{PATH} = $fail_path;
92         PublicInbox::Emergency->new($maildir)->prepare(\$msg);
93         $config->{'publicinboxwatch.spamcheck'} = 'spamc';
94         {
95                 local $SIG{__WARN__} = sub {}; # quiet spam check warning
96                 PublicInbox::WatchMaildir->new($config)->scan('full');
97         }
98         @list = $git->qx(qw(ls-tree -r --name-only refs/heads/master));
99         is(scalar @list, 0, 'tree has no files spamc checked');
100         is(unlink(glob("$maildir/new/*")), 1);
101 }
102
103 {
104         my $main_bin = getcwd()."/t/main-bin";
105         ok(-x "$main_bin/spamc", "mock spamc exists");
106         my $main_path = "$main_bin:$ENV{PATH}"; # for spamc ham mock
107         local $ENV{PATH} = $main_path;
108         PublicInbox::Emergency->new($maildir)->prepare(\$msg);
109         $config->{'publicinboxwatch.spamcheck'} = 'spamc';
110         @list = $git->qx(qw(ls-tree -r --name-only refs/heads/master));
111         PublicInbox::WatchMaildir->new($config)->scan('full');
112         @list = $git->qx(qw(ls-tree -r --name-only refs/heads/master));
113         is(scalar @list, 1, 'tree has one file after spamc checked');
114
115         # XXX: workaround some weird caching/memoization in cat-file,
116         # shouldn't be an issue in real-world use, though...
117         $git = PublicInbox::Git->new($git_dir);
118
119         my $mref = $git->cat_file('refs/heads/master:'.$list[0]);
120         like($$mref, qr/something\n\z/s, 'message scrubbed on import');
121 }
122
123 sub is_maildir {
124         my ($dir) = @_;
125         PublicInbox::WatchMaildir::is_maildir($dir);
126 }
127
128 is(is_maildir('maildir:/hello//world'), '/hello/world', 'extra slash gone');
129 is(is_maildir('maildir:/hello/world/'), '/hello/world', 'trailing slash gone');
130 is(is_maildir('faildir:/hello/world/'), undef, 'non-maildir rejected');
131
132 done_testing;