diff options
Diffstat (limited to 't/lei-convert.t')
-rw-r--r-- | t/lei-convert.t | 96 |
1 files changed, 94 insertions, 2 deletions
diff --git a/t/lei-convert.t b/t/lei-convert.t index e1849ff7..4670e47f 100644 --- a/t/lei-convert.t +++ b/t/lei-convert.t @@ -1,12 +1,16 @@ #!perl -w -# Copyright (C) 2021 all contributors <meta@public-inbox.org> +# Copyright (C) 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; use PublicInbox::TestCommon; +use v5.12; use PublicInbox::TestCommon; use PublicInbox::MboxReader; use PublicInbox::MdirReader; use PublicInbox::NetReader; use PublicInbox::Eml; use IO::Uncompress::Gunzip; +use File::Path qw(remove_tree); +use PublicInbox::Spawn qw(which run_qx); +use File::Compare; +use autodie qw(open); require_mods(qw(lei -imapd -nntpd Mail::IMAPClient Net::NNTP)); my ($tmpdir, $for_destroy) = tmpdir; my $sock = tcp_server; @@ -25,8 +29,36 @@ test_lei({ tmpdir => $tmpdir }, sub { my $d = $ENV{HOME}; lei_ok('convert', '-o', "mboxrd:$d/foo.mboxrd", "imap://$imap_host_port/t.v2.0"); + my ($nc0) = ($lei_err =~ /converted (\d+) messages/); ok(-f "$d/foo.mboxrd", 'mboxrd created from imap://'); + lei_ok qw(convert -o), "v2:$d/v2-test", "mboxrd:$d/foo.mboxrd"; + my ($nc) = ($lei_err =~ /converted (\d+) messages/); + is $nc, $nc0, 'converted all messages messages'; + lei_ok qw(q z:0.. -f jsonl --only), "$d/v2-test"; + is(scalar(split(/^/sm, $lei_out)), $nc, 'got all messages in v2-test'); + + lei_ok qw(convert -o), "mboxrd:$d/from-v2.mboxrd", "$d/v2-test"; + like $lei_err, qr/converted $nc messages/; + is(compare("$d/foo.mboxrd", "$d/from-v2.mboxrd"), 0, + 'convert mboxrd -> v2 ->mboxrd roundtrip') or + diag run_qx([qw(git diff --no-index), + "$d/foo.mboxrd", "$d/from-v2.mboxrd"]); + + lei_ok [qw(convert -F eml -o), "$d/v2-test"], undef, + { 0 => \<<'EOM', %$lei_opt }; +From: f@example.com +To: t@example.com +Subject: append-to-v2-on-convert +Message-ID: <append-to-v2-on-convert@example> +Date: Fri, 02 Oct 1993 00:00:00 +0000 +EOM + like $lei_err, qr/converted 1 messages/, 'only one message added'; + lei_ok qw(q z:0.. -f jsonl --only), "$d/v2-test"; + is(scalar(split(/^/sm, $lei_out)), $nc + 1, + 'got expected number of messages after append convert'); + like $lei_out, qr/append-to-v2-on-convert/; + lei_ok('convert', '-o', "mboxrd:$d/nntp.mboxrd", "nntp://$nntp_host_port/t.v2"); ok(-f "$d/nntp.mboxrd", 'mboxrd created from nntp://'); @@ -125,5 +157,65 @@ test_lei({ tmpdir => $tmpdir }, sub { like($md[0], qr/:2,S\z/, "`seen' flag set in Maildir"); lei_ok(qw(convert -o mboxrd:/dev/stdout), "$d/md2"); like($lei_out, qr/^Status: RO/sm, "`seen' flag preserved"); + + SKIP: { + my $ok; + for my $x (($ENV{GZIP}//''), qw(pigz gzip)) { + $x && (`$x -h 2>&1`//'') =~ /--rsyncable\b/s or next; + $ok = $x; + last; + } + skip 'pigz || gzip do not support --rsyncable', 1 if !$ok; + lei_ok qw(convert --rsyncable), "mboxrd:$d/qp.gz", + '-o', "mboxcl2:$d/qp2.gz"; + undef $fh; # necessary to make IO::Uncompress::Gunzip happy + open $fh, '<', "$d/qp2.gz"; + $fh = IO::Uncompress::Gunzip->new($fh, MultiStream => 1); + my @tmp; + PublicInbox::MboxReader->mboxcl2($fh, sub { + my ($eml) = @_; + $eml->header_set($_) for qw(Content-Length Lines); + push @tmp, $eml; + }); + is_deeply(\@tmp, \@bar, 'read rsyncable-gzipped mboxcl2'); + } + my $cp = which('cp') or xbail 'cp(1) not available (WTF?)'; + for my $v (1, 2) { + my $ibx_dir = "$ro_home/t$v"; + lei_ok qw(convert -f mboxrd), $ibx_dir, + \"dump v$v inbox to mboxrd"; + my $out = $lei_out; + lei_ok qw(convert -f mboxrd), "v$v:$ibx_dir", + \"dump v$v inbox to mboxrd w/ v$v:// prefix"; + is $out, $lei_out, "v$v:// prefix accepted"; + open my $fh, '<', \$out; + my (@mb, @md, @md2); + PublicInbox::MboxReader->mboxrd($fh, sub { + $_[0]->header_set('Status'); + push @mb, $_[0]->as_string; + }); + undef $out; + ok(scalar(@mb), 'got messages output'); + my $mdir = "$d/v$v-mdir"; + lei_ok qw(convert -o), $mdir, $ibx_dir, + \"dump v$v inbox to Maildir"; + PublicInbox::MdirReader->new->maildir_each_eml($mdir, sub { + push @md, $_[2]->as_string; + }); + @md = sort { $a cmp $b } @md; + @mb = sort { $a cmp $b } @mb; + is_deeply(\@mb, \@md, 'got matching inboxes'); + xsys_e([$cp, '-Rp', $ibx_dir, "$d/tv$v" ]); + remove_tree($mdir, "$d/tv$v/public-inbox", + glob("$d/tv$v/xap*")); + + lei_ok qw(convert -o), $mdir, "$d/tv$v", + \"dump u indexed v$v inbox to Maildir"; + PublicInbox::MdirReader->new->maildir_each_eml($mdir, sub { + push @md2, $_[2]->as_string; + }); + @md2 = sort { $a cmp $b } @md2; + is_deeply(\@md, \@md2, 'got matching inboxes even unindexed'); + } }); done_testing; |