]> Sergey Matveev's repositories - public-inbox.git/blobdiff - t/mbox_reader.t
No ext_urls
[public-inbox.git] / t / mbox_reader.t
index 9391dc2480717a7642e4f37f2608a47d2efdb110..87e8f397662cd058aafc0075a23d4377e2c0abaa 100644 (file)
@@ -1,5 +1,5 @@
 #!perl -w
-# Copyright (C) 2020 all contributors <meta@public-inbox.org>
+# Copyright (C) 2020-2021 all contributors <meta@public-inbox.org>
 # License: AGPL-3.0+ <https://www.gnu.org/licenses/agpl-3.0.txt>
 use strict;
 use v5.10.1;
@@ -24,6 +24,16 @@ my %raw = (
                (("b: ".('b' x 72)."\n") x 1000) .
                "From hell\n",
 );
+{
+       my $eml = PublicInbox::Eml->new($raw{small});
+       my $mbox_keywords = PublicInbox::MboxReader->can('mbox_keywords');
+       is_deeply($mbox_keywords->($eml), [], 'no keywords');
+       $eml->header_set('Status', 'RO');
+       is_deeply($mbox_keywords->($eml), ['seen'], 'seen extracted');
+       $eml->header_set('X-Status', 'A');
+       is_deeply($mbox_keywords->($eml), [qw(answered seen)],
+               'seen+answered extracted');
+}
 
 if ($ENV{TEST_EXTRA}) {
        for my $fn (glob('t/*.eml'), glob('t/*/*.{patch,eml}')) {
@@ -32,7 +42,6 @@ if ($ENV{TEST_EXTRA}) {
 }
 
 my $reader = PublicInbox::MboxReader->new;
-my $write_in_full = PublicInbox::LeiToMail->can('write_in_full');
 my $check_fmt = sub {
        my $fmt = shift;
        my @order = shuffle(keys %raw);
@@ -41,11 +50,13 @@ my $check_fmt = sub {
        for my $k (@order) {
                my $eml = PublicInbox::Eml->new($raw{$k});
                my $buf = $eml2mbox->($eml);
-               $write_in_full->($fh, $buf, undef);
+               print $fh $$buf or BAIL_OUT "print $!";
        }
        seek($fh, 0, SEEK_SET) or BAIL_OUT "seek: $!";
        $reader->$fmt($fh, sub {
                my ($eml) = @_;
+               $eml->header_set('Status');
+               $eml->header_set('Lines');
                my $cur = shift @order;
                my @cl = $eml->header_raw('Content-Length');
                if ($fmt =~ /\Amboxcl/) {
@@ -60,6 +71,12 @@ my $check_fmt = sub {
                                "Content-Length is correct $fmt $cur");
                        # clobber for ->as_string comparison below
                        $eml->header_set('Content-Length');
+
+                       # special case for t/solve/bare.patch, not sure if we
+                       # should even handle it...
+                       if ($cl[0] eq '0' && ${$eml->{hdr}} eq '') {
+                               delete $eml->{bdy};
+                       }
                } else {
                        is(scalar(@cl), 0, "Content-Length unset $fmt $cur");
                }
@@ -73,4 +90,62 @@ for my $fmt (@mbox) { $check_fmt->($fmt) }
 s/\n/\r\n/sg for (values %raw);
 for my $fmt (@mbox) { $check_fmt->($fmt) }
 
+{
+       my $no_blank_eom = <<'EOM';
+From x@y Fri Oct  2 00:00:00 1993
+a: b
+
+body1
+From x@y Fri Oct  2 00:00:00 1993
+c: d
+
+body2
+EOM
+       # chop($no_blank_eom) eq "\n" or BAIL_OUT 'broken LF';
+       for my $variant (qw(mboxrd mboxo)) {
+               my @x;
+               open my $fh, '<', \$no_blank_eom or BAIL_OUT 'PerlIO::scalar';
+               $reader->$variant($fh, sub { push @x, shift });
+               is_deeply($x[0]->{bdy}, \"body1\n", 'LF preserved in 1st');
+               is_deeply($x[1]->{bdy}, \"body2\n", 'no LF added in 2nd');
+       }
+}
+
+SKIP: {
+       use PublicInbox::Spawn qw(popen_rd);
+       my $fh = popen_rd([ $^X, '-E', <<'' ]);
+say "From x@y Fri Oct  2 00:00:00 1993";
+print "a: b\n\n", "x" x 70000, "\n\n";
+say "From x@y Fri Oct  2 00:00:00 2010";
+print "Final: bit\n\n", "Incomplete\n\n";
+exit 1
+
+       my @x;
+       eval { $reader->mboxrd($fh, sub { push @x, shift->as_string }) };
+       like($@, qr/error closing mbox/, 'detects error reading from pipe');
+       is(scalar(@x), 1, 'only saw one message');
+       is(scalar(grep(/Final/, @x)), 0, 'no incomplete bit');
+}
+
+{
+       my $html = <<EOM;
+<html><head><title>hi,</title></head><body>how are you</body></html>
+EOM
+       for my $m (qw(mboxrd mboxcl mboxcl2 mboxo)) {
+               my (@w, @x);
+               local $SIG{__WARN__} = sub { push @w, @_ };
+               open my $fh, '<', \$html or xbail 'PerlIO::scalar';
+               PublicInbox::MboxReader->$m($fh, sub {
+                       push @x, $_[0]->as_string
+               });
+               if ($m =~ /\Amboxcl/) {
+                       is_deeply(\@x, [], "messages in invalid $m");
+               } else {
+                       is_deeply(\@x, [ "\n$html" ], "body-only $m");
+               }
+               is_deeply([grep(!/^W: leftover/, @w)], [],
+                       "no extra warnings besides leftover ($m)");
+       }
+}
+
 done_testing;