1 # Copyright (C) 2016-2021 all contributors <meta@public-inbox.org>
2 # License: AGPL-3.0+ <https://www.gnu.org/licenses/agpl-3.0.txt>
8 use Fcntl qw(:DEFAULT SEEK_SET);
9 use PublicInbox::TestCommon;
10 my ($tmpdir, $for_destroy) = tmpdir();
12 use_ok 'PublicInbox::Spamcheck::Spamc';
13 my $spamc = PublicInbox::Spamcheck::Spamc->new;
14 $spamc->{checkcmd} = [qw(cat)];
17 open my $fh, '+>', "$tmpdir/file" or die "open failed: $!";
18 ok(!$spamc->spamcheck($fh), 'empty '.ref($fh));
20 ok(!$spamc->spamcheck(IO::File->new_tmpfile), 'IO::File->new_tmpfile');
24 Date: Thu, 01 Jan 1970 00:00:00 +0000
28 Message-ID: <testmessage@example.com>
31 ok($spamc->spamcheck(PublicInbox::Eml->new($src), \$dst), 'PublicInbox::Eml works');
32 is($dst, $src, 'input == output');
35 $spamc->{checkcmd} = ['sh', '-c', 'cat; false'];
36 ok(!$spamc->spamcheck(PublicInbox::Eml->new($src), \$dst), 'Failed check works');
37 is($dst, $src, 'input == output for spammy example');
39 for my $l (qw(ham spam)) {
40 my $file = "$tmpdir/$l.out";
41 $spamc->{$l.'cmd'} = ['tee', $file ];
42 my $method = $l.'learn';
43 ok($spamc->$method(PublicInbox::Eml->new($src)), "$method OK");
44 open my $fh, '<', $file or die "failed to open $file: $!";
45 is(eval { local $/, <$fh> }, $src, "$l command ran alright");