]> Sergey Matveev's repositories - public-inbox.git/blob - t/watch_maildir.t
watch: remove Filesys::Notify::Simple dependency
[public-inbox.git] / t / watch_maildir.t
1 # Copyright (C) 2016-2020 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 PublicInbox::Eml;
6 use Cwd;
7 use PublicInbox::Config;
8 use PublicInbox::TestCommon;
9 use PublicInbox::Import;
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 PublicInbox::Import::init_bare($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 {
35         my @w;
36         local $SIG{__WARN__} = sub { push @w, @_ };
37         my $config = PublicInbox::Config->new(\<<EOF);
38 $cfgpfx.address=$addr
39 $cfgpfx.inboxdir=$git_dir
40 $cfgpfx.watch=maildir:$spamdir
41 publicinboxlearn.watchspam=maildir:$spamdir
42 EOF
43         my $wm = PublicInbox::WatchMaildir->new($config);
44         is(scalar grep(/is a spam folder/, @w), 1, 'got warning about spam');
45         is_deeply($wm->{mdmap}, { "$spamdir/cur" => 'watchspam' },
46                 'only got the spam folder to watch');
47 }
48
49 my $cfg_path = "$tmpdir/config";
50 {
51         open my $fh, '>', $cfg_path or BAIL_OUT $!;
52         print $fh <<EOF or BAIL_OUT $!;
53 [publicinbox "test"]
54         address = $addr
55         inboxdir = $git_dir
56         watch = maildir:$maildir
57         filter = PublicInbox::Filter::Vger
58 [publicinboxlearn]
59         watchspam = maildir:$spamdir
60 EOF
61         close $fh or BAIL_OUT $!;
62 }
63
64 my $config = PublicInbox::Config->new($cfg_path);
65 PublicInbox::WatchMaildir->new($config)->scan('full');
66 my $git = PublicInbox::Git->new($git_dir);
67 my @list = $git->qx(qw(rev-list refs/heads/master));
68 is(scalar @list, 1, 'one revision in rev-list');
69
70 my $write_spam = sub {
71         is(scalar glob("$spamdir/new/*"), undef, 'no spam existing');
72         $sem->prepare(\$msg);
73         $sem->commit;
74         my @new = glob("$spamdir/new/*");
75         is(scalar @new, 1);
76         my @p = split(m!/+!, $new[0]);
77         ok(link($new[0], "$spamdir/cur/".$p[-1].":2,S"));
78         is(unlink($new[0]), 1);
79 };
80 $write_spam->();
81 is(unlink(glob("$maildir/new/*")), 1, 'unlinked old spam');
82 PublicInbox::WatchMaildir->new($config)->scan('full');
83 @list = $git->qx(qw(rev-list refs/heads/master));
84 is(scalar @list, 2, 'two revisions in rev-list');
85 @list = $git->qx(qw(ls-tree -r --name-only refs/heads/master));
86 is(scalar @list, 0, 'tree is empty');
87
88 # check with scrubbing
89 {
90         $msg .= qq(--
91 To unsubscribe from this list: send the line "unsubscribe git" in
92 the body of a message to majordomo\@vger.kernel.org
93 More majordomo info at  http://vger.kernel.org/majordomo-info.html\n);
94         PublicInbox::Emergency->new($maildir)->prepare(\$msg);
95         PublicInbox::WatchMaildir->new($config)->scan('full');
96         @list = $git->qx(qw(ls-tree -r --name-only refs/heads/master));
97         is(scalar @list, 1, 'tree has one file');
98         my $mref = $git->cat_file('HEAD:'.$list[0]);
99         like($$mref, qr/something\n\z/s, 'message scrubbed on import');
100
101         is(unlink(glob("$maildir/new/*")), 1, 'unlinked spam');
102         $write_spam->();
103         PublicInbox::WatchMaildir->new($config)->scan('full');
104         @list = $git->qx(qw(ls-tree -r --name-only refs/heads/master));
105         is(scalar @list, 0, 'tree is empty');
106         @list = $git->qx(qw(rev-list refs/heads/master));
107         is(scalar @list, 4, 'four revisions in rev-list');
108 }
109
110 {
111         my $fail_bin = getcwd()."/t/fail-bin";
112         ok(-x "$fail_bin/spamc", "mock spamc exists");
113         my $fail_path = "$fail_bin:$ENV{PATH}"; # for spamc ham mock
114         local $ENV{PATH} = $fail_path;
115         PublicInbox::Emergency->new($maildir)->prepare(\$msg);
116         $config->{'publicinboxwatch.spamcheck'} = 'spamc';
117         {
118                 local $SIG{__WARN__} = sub {}; # quiet spam check warning
119                 PublicInbox::WatchMaildir->new($config)->scan('full');
120         }
121         @list = $git->qx(qw(ls-tree -r --name-only refs/heads/master));
122         is(scalar @list, 0, 'tree has no files spamc checked');
123         is(unlink(glob("$maildir/new/*")), 1);
124 }
125
126 {
127         my $main_bin = getcwd()."/t/main-bin";
128         ok(-x "$main_bin/spamc", "mock spamc exists");
129         my $main_path = "$main_bin:$ENV{PATH}"; # for spamc ham mock
130         local $ENV{PATH} = $main_path;
131         PublicInbox::Emergency->new($maildir)->prepare(\$msg);
132         $config->{'publicinboxwatch.spamcheck'} = 'spamc';
133         @list = $git->qx(qw(ls-tree -r --name-only refs/heads/master));
134         PublicInbox::WatchMaildir->new($config)->scan('full');
135         @list = $git->qx(qw(ls-tree -r --name-only refs/heads/master));
136         is(scalar @list, 1, 'tree has one file after spamc checked');
137
138         # XXX: workaround some weird caching/memoization in cat-file,
139         # shouldn't be an issue in real-world use, though...
140         $git = PublicInbox::Git->new($git_dir);
141
142         my $mref = $git->cat_file('refs/heads/master:'.$list[0]);
143         like($$mref, qr/something\n\z/s, 'message scrubbed on import');
144 }
145
146 # end-to-end test which actually uses inotify/kevent
147 {
148         my $env = { PI_CONFIG => $cfg_path };
149         $git->cleanup;
150
151         # n.b. --no-scan is only intended for testing atm
152         my $wm = start_script([qw(-watch --no-scan)], $env);
153         my $eml = eml_load('t/data/0001.patch');
154         $eml->header_set('Cc', $addr);
155         my $em = PublicInbox::Emergency->new($maildir);
156         $em->prepare(\($eml->as_string));
157
158         use_ok 'PublicInbox::InboxIdle';
159         use_ok 'PublicInbox::DS';
160         my $delivered = 0;
161         my $cb = sub {
162                 my ($ibx) = @_;
163                 diag "message delivered to `$ibx->{name}'";
164                 $delivered++;
165         };
166         PublicInbox::DS->Reset;
167         my $ii = PublicInbox::InboxIdle->new($config);
168         my $obj = bless \$cb, 'PublicInbox::TestCommon::InboxWakeup';
169         $config->each_inbox(sub { $_[0]->subscribe_unlock('ident', $obj) });
170         PublicInbox::DS->SetPostLoopCallback(sub { $delivered == 0 });
171
172         # wait for -watch to setup inotify watches
173         my $sleep = 1;
174         if (eval { require Linux::Inotify2 } && -d "/proc/$wm->{pid}/fd") {
175                 my $end = time + 2;
176                 my (@ino, @ino_info);
177                 do {
178                         @ino = grep {
179                                 (readlink($_)//'') =~ /\binotify\b/
180                         } glob("/proc/$wm->{pid}/fd/*");
181                 } until (@ino || time > $end || !tick);
182                 if (scalar(@ino) == 1) {
183                         my $ino_fd = (split('/', $ino[0]))[-1];
184                         my $ino_fdinfo = "/proc/$wm->{pid}/fdinfo/$ino_fd";
185                         while (time < $end && open(my $fh, '<', $ino_fdinfo)) {
186                                 @ino_info = grep(/^inotify wd:/, <$fh>);
187                                 last if @ino_info >= 4;
188                                 tick;
189                         }
190                         $sleep = undef if @ino_info >= 4;
191                 }
192         }
193         if ($sleep) {
194                 diag "waiting ${sleep}s for -watch to start up";
195                 sleep $sleep;
196         }
197
198         $em->commit; # wake -watch up
199         diag 'waiting for -watch to import new message';
200         PublicInbox::DS->EventLoop;
201         $wm->kill;
202         $wm->join;
203         $ii->close;
204         PublicInbox::DS->Reset;
205         my $head = $git->qx(qw(cat-file commit HEAD));
206         my $subj = $eml->header('Subject');
207         like($head, qr/^\Q$subj\E/sm, 'new commit made');
208 }
209
210 sub is_maildir {
211         my ($dir) = @_;
212         PublicInbox::WatchMaildir::is_maildir($dir);
213 }
214
215 is(is_maildir('maildir:/hello//world'), '/hello/world', 'extra slash gone');
216 is(is_maildir('maildir:/hello/world/'), '/hello/world', 'trailing slash gone');
217 is(is_maildir('faildir:/hello/world/'), undef, 'non-maildir rejected');
218
219 done_testing;