2 # Copyright (C) 2020-2021 all contributors <meta@public-inbox.org>
3 # License: AGPL-3.0+ <https://www.gnu.org/licenses/agpl-3.0.txt>
4 use strict; use v5.10.1; use PublicInbox::TestCommon;
6 use Fcntl qw(SEEK_SET O_RDONLY O_NONBLOCK);
7 use IO::Uncompress::Gunzip qw(gunzip);
8 use IO::Compress::Gzip qw(gzip);
9 use PublicInbox::MboxReader;
10 use PublicInbox::LeiToMail;
11 use PublicInbox::Spawn qw(popen_rd);
13 '<qp@example.com>' => eml_load('t/plack-qp.eml'),
14 '<testmessage@example.com>' => eml_load('t/utf8.eml'),
16 $exp->{'<qp@example.com>'}->header_set('Status', 'OR');
17 $exp->{'<testmessage@example.com>'}->header_set('Status', 'O');
20 lei_ok(qw(import -F eml t/plack-qp.eml));
21 my $o = "$ENV{HOME}/dst";
22 lei_ok(qw(q -o), "maildir:$o", qw(m:qp@example.com));
23 my @fn = glob("$o/cur/*:2,");
24 scalar(@fn) == 1 or BAIL_OUT "wrote multiple or zero files: ".explain(\@fn);
25 rename($fn[0], "$fn[0]S") or BAIL_OUT "rename $!";
27 lei_ok(qw(q -o), "maildir:$o", qw(m:bogus-noresults@example.com));
28 ok(!glob("$o/cur/*"), 'last result cleared after augment-import');
30 lei_ok(qw(q -o), "maildir:$o", qw(m:qp@example.com));
31 @fn = glob("$o/cur/*:2,S");
32 is(scalar(@fn), 1, "`seen' flag set on Maildir file");
34 # ensure --no-import-before works
37 rename($fn[0], $n) or BAIL_OUT "rename $!";
38 lei_ok(qw(q --no-import-before -o), "maildir:$o",
39 qw(m:bogus-noresults@example.com));
40 ok(!glob("$o/cur/*"), '--no-import-before cleared destination');
41 lei_ok(qw(q -o), "maildir:$o", qw(m:qp@example.com));
42 @fn = glob("$o/cur/*:2,S");
43 is(scalar(@fn), 1, "`seen' flag (but not `replied') set on Maildir file");
46 $o = "$ENV{HOME}/fifo";
47 mkfifo($o, 0600) or skip("mkfifo not supported: $!", 1);
48 # cat(1) since lei() may not execve for FD_CLOEXEC to work
49 my $cat = popen_rd(['cat', $o]);
50 ok(!lei(qw(q --import-before bogus -o), "mboxrd:$o"),
51 '--import-before fails on non-seekable output');
52 is(do { local $/; <$cat> }, '', 'no output on FIFO');
54 $cat = popen_rd(['cat', $o]);
55 lei_ok(qw(q m:qp@example.com -o), "mboxrd:$o");
56 my $buf = do { local $/; <$cat> };
57 open my $fh, '<', \$buf or BAIL_OUT $!;
58 PublicInbox::MboxReader->mboxrd($fh, sub {
60 $eml->header_set('Status', 'OR');
61 is_deeply($eml, $exp->{'<qp@example.com>'},
62 'FIFO output works as expected');
66 lei_ok qw(import -F eml t/utf8.eml), \'for augment test';
68 if ($_[0] =~ /\.gz\z/) {
69 gunzip($_[0] => \(my $buf = ''), MultiStream => 1) or
73 open my $fh, '+<', $_[0] or BAIL_OUT $!;
74 do { local $/; <$fh> };
78 my $write_file = sub {
79 if ($_[0] =~ /\.gz\z/) {
80 gzip(\($_[1]), $_[0]) or BAIL_OUT 'gzip';
82 open my $fh, '>', $_[0] or BAIL_OUT $!;
83 print $fh $_[1] or BAIL_OUT $!;
84 close $fh or BAIL_OUT;
88 for my $sfx ('', '.gz') {
89 $o = "$ENV{HOME}/dst.mboxrd$sfx";
90 lei_ok(qw(q -o), "mboxrd:$o", qw(m:qp@example.com));
91 my $buf = $read_file->($o);
92 $buf =~ s/^Status: [^\n]*\n//sm or BAIL_OUT "no status in $buf";
93 $write_file->($o, $buf);
94 lei_ok(qw(q -o), "mboxrd:$o", qw(rereadandimportkwchange));
95 $buf = $read_file->($o);
96 is($buf, '', 'emptied');
97 lei_ok(qw(q -o), "mboxrd:$o", qw(m:qp@example.com));
98 $buf = $read_file->($o);
99 $buf =~ s/\nStatus: O\n\n/\nStatus: OR\n\n/s or
100 BAIL_OUT "no Status in $buf";
101 $write_file->($o, $buf);
102 lei_ok(qw(q -a -o), "mboxrd:$o", qw(m:testmessage@example.com));
103 $buf = $read_file->($o);
104 open my $fh, '<', \$buf or BAIL_OUT "PerlIO::scalar $!";
106 PublicInbox::MboxReader->mboxrd($fh, sub {
108 $res{$eml->header_raw('Message-ID')} = $eml;
110 is_deeply(\%res, $exp, '--augment worked');
112 lei_ok(qw(q -o), "mboxrd:/dev/stdout", qw(m:qp@example.com)) or
114 like($lei_out, qr/^Status: OR\n/sm, 'Status set by previous augment');