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>
7 use PublicInbox::Config;
8 use PublicInbox::TestCommon;
9 my @mods = qw(Filesys::Notify::Simple);
10 foreach my $mod (@mods) {
12 plan skip_all => "$mod missing for watch_maildir.t" if $@;
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');
26 From: user\@example.com
29 Message-Id: <a\@b.com>
30 Date: Sat, 18 Jun 2016 00:00:00 +0000
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
39 my $config = PublicInbox::Config->new(\<<EOF);
41 $cfgpfx.inboxdir=$git_dir
42 $cfgpfx.watch=maildir:$maildir
43 $cfgpfx.filter=PublicInbox::Filter::Vger
44 publicinboxlearn.watchspam=maildir:$spamdir
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');
52 my $write_spam = sub {
53 is(scalar glob("$spamdir/new/*"), undef, 'no spam existing');
56 my @new = glob("$spamdir/new/*");
58 my @p = split(m!/+!, $new[0]);
59 ok(link($new[0], "$spamdir/cur/".$p[-1].":2,S"));
60 is(unlink($new[0]), 1);
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');
70 # check with scrubbing
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');
83 is(unlink(glob("$maildir/new/*")), 1, 'unlinked 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');
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';
100 local $SIG{__WARN__} = sub {}; # quiet spam check warning
101 PublicInbox::WatchMaildir->new($config)->scan('full');
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);
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');
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);
124 my $mref = $git->cat_file('refs/heads/master:'.$list[0]);
125 like($$mref, qr/something\n\z/s, 'message scrubbed on import');
130 PublicInbox::WatchMaildir::is_maildir($dir);
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');