]> Sergey Matveev's repositories - public-inbox.git/blob - t/lei_to_mail.t
lei_to_mail: start --augment, dedupe, bz2 and xz
[public-inbox.git] / t / lei_to_mail.t
1 #!perl -w
2 # Copyright (C) 2020 all contributors <meta@public-inbox.org>
3 # License: AGPL-3.0+ <https://www.gnu.org/licenses/agpl-3.0.txt>
4 use strict;
5 use v5.10.1;
6 use Test::More;
7 use PublicInbox::TestCommon;
8 use PublicInbox::Eml;
9 use_ok 'PublicInbox::LeiToMail';
10 my $from = "Content-Length: 10\nSubject: x\n\nFrom hell\n";
11 my $noeol = "Subject: x\n\nFrom hell";
12 my $crlf = $noeol;
13 $crlf =~ s/\n/\r\n/g;
14 my $kw = [qw(seen answered flagged)];
15 for my $mbox (qw(mboxrd mboxo mboxcl mboxcl2)) {
16         my $m = "eml2$mbox";
17         my $cb = PublicInbox::LeiToMail->can($m);
18         my $s = $cb->(PublicInbox::Eml->new($from), $kw);
19         is(substr($$s, -1, 1), "\n", "trailing LF in normal $mbox");
20         my $eml = PublicInbox::Eml->new($s);
21         is($eml->header('Status'), 'R', "Status: set by $m");
22         is($eml->header('X-Status'), 'AF', "X-Status: set by $m");
23         if ($mbox eq 'mboxcl2') {
24                 like($eml->body_raw, qr/^From /, "From not escaped $m");
25         } else {
26                 like($eml->body_raw, qr/^>From /, "From escaped once by $m");
27         }
28         my @cl = $eml->header('Content-Length');
29         if ($mbox =~ /mboxcl/) {
30                 is(scalar(@cl), 1, "$m only has one Content-Length header");
31                 is($cl[0] + length("\n"),
32                         length($eml->body_raw), "$m Content-Length matches");
33         } else {
34                 is(scalar(@cl), 0, "$m clobbered Content-Length");
35         }
36         $s = $cb->(PublicInbox::Eml->new($noeol), $kw);
37         is(substr($$s, -1, 1), "\n",
38                 "trailing LF added by $m when original lacks EOL");
39         $eml = PublicInbox::Eml->new($s);
40         if ($mbox eq 'mboxcl2') {
41                 is($eml->body_raw, "From hell\n", "From not escaped by $m");
42         } else {
43                 is($eml->body_raw, ">From hell\n", "From escaped once by $m");
44         }
45         $s = $cb->(PublicInbox::Eml->new($crlf), $kw);
46         is(substr($$s, -2, 2), "\r\n",
47                 "trailing CRLF added $m by original lacks EOL");
48         $eml = PublicInbox::Eml->new($s);
49         if ($mbox eq 'mboxcl2') {
50                 is($eml->body_raw, "From hell\r\n", "From not escaped by $m");
51         } else {
52                 is($eml->body_raw, ">From hell\r\n", "From escaped once by $m");
53         }
54         if ($mbox =~ /mboxcl/) {
55                 is($eml->header('Content-Length') + length("\r\n"),
56                         length($eml->body_raw), "$m Content-Length matches");
57         } elsif ($mbox eq 'mboxrd') {
58                 $s = $cb->($eml, $kw);
59                 $eml = PublicInbox::Eml->new($s);
60                 is($eml->body_raw,
61                         ">>From hell\r\n\r\n", "From escaped again by $m");
62         }
63 }
64
65 my ($tmpdir, $for_destroy) = tmpdir();
66 local $ENV{TMPDIR} = $tmpdir;
67 open my $err, '>>', "$tmpdir/lei.err" or BAIL_OUT $!;
68 my $lei = { 2 => $err };
69 my $buf = <<'EOM';
70 From: x@example.com
71 Subject: x
72
73 blah
74 EOM
75 my $fn = "$tmpdir/x.mbox";
76 my $orig = do {
77         my $wcb = PublicInbox::LeiToMail->write_cb("mboxcl2:$fn", $lei);
78         is(ref $wcb, 'CODE', 'write_cb returned callback');
79         ok(-f $fn && !-s _, 'empty file created');
80         $wcb->(\(my $dup = $buf), 'deadbeef', [ qw(seen) ]);
81         undef $wcb;
82         open my $fh, '<', $fn or BAIL_OUT $!;
83         my $raw = do { local $/; <$fh> };
84         like($raw, qr/^blah\n/sm, 'wrote content');
85         unlink $fn or BAIL_OUT $!;
86
87         local $lei->{opt} = { jobs => 2 };
88         $wcb = PublicInbox::LeiToMail->write_cb("mboxcl2:$fn", $lei);
89         $wcb->(\($dup = $buf), 'deadbeef', [ qw(seen) ]);
90         undef $wcb;
91         open $fh, '<', $fn or BAIL_OUT $!;
92         is($raw, do { local $/; <$fh> }, 'jobs > 1');
93         $raw;
94 };
95 for my $zsfx (qw(gz bz2 xz)) { # XXX should we support zst, zz, lzo, lzma?
96         my $zsfx2cmd = PublicInbox::LeiToMail->can('zsfx2cmd');
97         SKIP: {
98                 my $cmd = eval { $zsfx2cmd->($zsfx, 0, $lei) };
99                 skip $@, 3 if $@;
100                 my $dc_cmd = eval { $zsfx2cmd->($zsfx, 1, $lei) };
101                 ok($dc_cmd, "decompressor for .$zsfx");
102                 my $f = "$fn.$zsfx";
103                 my $dst = "mboxcl2:$f";
104                 my $wcb = PublicInbox::LeiToMail->write_cb($dst, $lei);
105                 $wcb->(\(my $dup = $buf), 'deadbeef', [ qw(seen) ]);
106                 undef $wcb;
107                 my $uncompressed = xqx([@$dc_cmd, $f]);
108                 is($uncompressed, $orig, "$zsfx works unlocked");
109
110                 local $lei->{opt} = { jobs => 2 }; # for atomic writes
111                 unlink $f or BAIL_OUT "unlink $!";
112                 $wcb = PublicInbox::LeiToMail->write_cb($dst, $lei);
113                 $wcb->(\($dup = $buf), 'deadbeef', [ qw(seen) ]);
114                 undef $wcb;
115                 is(xqx([@$dc_cmd, $f]), $orig, "$zsfx matches with lock");
116         }
117 }
118
119 unlink $fn or BAIL_OUT $!;
120 if ('default deduplication uses content_hash') {
121         my $wcb = PublicInbox::LeiToMail->write_cb("mboxo:$fn", $lei);
122         $wcb->(\(my $x = $buf), 'deadbeef', []) for (1..2);
123         undef $wcb; # undef to commit changes
124         my $cmp = '';
125         open my $fh, '<', $fn or BAIL_OUT $!;
126         require PublicInbox::MboxReader;
127         PublicInbox::MboxReader->mboxo($fh, sub { $cmp .= shift->as_string });
128         is($cmp, $buf, 'only one message written');
129 }
130
131 done_testing;