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>
4 use File::Temp qw/tempdir/;
7 use PublicInbox::Config;
8 my @mods = qw(Filesys::Notify::Simple);
9 foreach my $mod (@mods) {
11 plan skip_all => "$mod missing for watch_maildir.t" if $@;
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');
25 From: user\@example.com
28 Message-Id: <a\@b.com>
29 Date: Sat, 18 Jun 2016 00:00:00 +0000
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
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",
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');
51 my $write_spam = sub {
52 is(scalar glob("$spamdir/new/*"), undef, 'no spam existing');
55 my @new = glob("$spamdir/new/*");
57 my @p = split(m!/+!, $new[0]);
58 ok(link($new[0], "$spamdir/cur/".$p[-1].":2,S"));
59 is(unlink($new[0]), 1);
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');
69 # check with scrubbing
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');
82 is(unlink(glob("$maildir/new/*")), 1, 'unlinked 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');
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';
99 local $SIG{__WARN__} = sub {}; # quiet spam check warning
100 PublicInbox::WatchMaildir->new($config)->scan('full');
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);
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');
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);
123 my $mref = $git->cat_file('refs/heads/master:'.$list[0]);
124 like($$mref, qr/something\n\z/s, 'message scrubbed on import');
129 PublicInbox::WatchMaildir::is_maildir($dir);
132 is(is_maildir('maildir:/hello//world'), '/hello/world', 'extra slash gone');
133 is(is_maildir('maildir:/hello/world/'), '/hello/world', 'trailing slash gone');
134 is(is_maildir('faildir:/hello/world/'), undef, 'non-maildir rejected');