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