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 my $sem = PublicInbox::Emergency->new($spamdir); # create dirs
37 my $config = PublicInbox::Config->new({
38 "$cfgpfx.address" => $addr,
39 "$cfgpfx.mainrepo" => $git_dir,
40 "$cfgpfx.watch" => "maildir:$maildir",
41 "$cfgpfx.filter" => 'PublicInbox::Filter::Vger',
42 "publicinboxlearn.watchspam" => "maildir:$spamdir",
45 PublicInbox::WatchMaildir->new($config)->scan('full');
46 my $git = PublicInbox::Git->new($git_dir);
47 my @list = $git->qx(qw(rev-list refs/heads/master));
48 is(scalar @list, 1, 'one revision in rev-list');
50 my $write_spam = sub {
51 is(scalar glob("$spamdir/new/*"), undef, 'no spam existing');
54 my @new = glob("$spamdir/new/*");
56 my @p = split(m!/+!, $new[0]);
57 ok(link($new[0], "$spamdir/cur/".$p[-1].":2,S"));
58 is(unlink($new[0]), 1);
61 is(unlink(glob("$maildir/new/*")), 1, 'unlinked old spam');
62 PublicInbox::WatchMaildir->new($config)->scan('full');
63 @list = $git->qx(qw(rev-list refs/heads/master));
64 is(scalar @list, 2, 'two revisions in rev-list');
65 @list = $git->qx(qw(ls-tree -r --name-only refs/heads/master));
66 is(scalar @list, 0, 'tree is empty');
68 # check with scrubbing
71 To unsubscribe from this list: send the line "unsubscribe git" in
72 the body of a message to majordomo\@vger.kernel.org
73 More majordomo info at http://vger.kernel.org/majordomo-info.html\n);
74 PublicInbox::Emergency->new($maildir)->prepare(\$msg);
75 PublicInbox::WatchMaildir->new($config)->scan('full');
76 @list = $git->qx(qw(ls-tree -r --name-only refs/heads/master));
77 is(scalar @list, 1, 'tree has one file');
78 my $mref = $git->cat_file('HEAD:'.$list[0]);
79 like($$mref, qr/something\n\z/s, 'message scrubbed on import');
81 is(unlink(glob("$maildir/new/*")), 1, 'unlinked spam');
83 PublicInbox::WatchMaildir->new($config)->scan('full');
84 @list = $git->qx(qw(ls-tree -r --name-only refs/heads/master));
85 is(scalar @list, 0, 'tree is empty');
86 @list = $git->qx(qw(rev-list refs/heads/master));
87 is(scalar @list, 4, 'four revisions in rev-list');
91 my $fail_bin = getcwd()."/t/fail-bin";
92 ok(-x "$fail_bin/spamc", "mock spamc exists");
93 my $fail_path = "$fail_bin:$ENV{PATH}"; # for spamc ham mock
94 local $ENV{PATH} = $fail_path;
95 PublicInbox::Emergency->new($maildir)->prepare(\$msg);
96 $config->{'publicinboxwatch.spamcheck'} = 'spamc';
98 local $SIG{__WARN__} = sub {}; # quiet spam check warning
99 PublicInbox::WatchMaildir->new($config)->scan('full');
101 @list = $git->qx(qw(ls-tree -r --name-only refs/heads/master));
102 is(scalar @list, 0, 'tree has no files spamc checked');
103 is(unlink(glob("$maildir/new/*")), 1);
107 my $main_bin = getcwd()."/t/main-bin";
108 ok(-x "$main_bin/spamc", "mock spamc exists");
109 my $main_path = "$main_bin:$ENV{PATH}"; # for spamc ham mock
110 local $ENV{PATH} = $main_path;
111 PublicInbox::Emergency->new($maildir)->prepare(\$msg);
112 $config->{'publicinboxwatch.spamcheck'} = 'spamc';
113 @list = $git->qx(qw(ls-tree -r --name-only refs/heads/master));
114 PublicInbox::WatchMaildir->new($config)->scan('full');
115 @list = $git->qx(qw(ls-tree -r --name-only refs/heads/master));
116 is(scalar @list, 1, 'tree has one file after spamc checked');
118 # XXX: workaround some weird caching/memoization in cat-file,
119 # shouldn't be an issue in real-world use, though...
120 $git = PublicInbox::Git->new($git_dir);
122 my $mref = $git->cat_file('refs/heads/master:'.$list[0]);
123 like($$mref, qr/something\n\z/s, 'message scrubbed on import');