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