]> Sergey Matveev's repositories - public-inbox.git/commitdiff
inboxwritable: mime_from_path: reuse in more places
authorEric Wong <e@yhbt.net>
Sat, 18 Apr 2020 03:38:46 +0000 (03:38 +0000)
committerEric Wong <e@yhbt.net>
Sun, 19 Apr 2020 08:51:17 +0000 (08:51 +0000)
There's nothing Maildir-specific about the function, so
`maildir_path_load' was a bad name.  So give it a more
appropriate name and use it in our tests.

This save ourselves some code and inconsistency by reusing an
existing internal library routine in more places.  We can drop
the "From_" line in some of our (formerly) mbox sample files.

MANIFEST
lib/PublicInbox/InboxWritable.pm
lib/PublicInbox/WatchMaildir.pm
script/public-inbox-edit
t/iso-2202-jp.eml [moved from t/iso-2202-jp.mbox with 84% similarity]
t/mda.t
t/msg_iter.t
t/nntpd-tls.t
t/search.t
t/solver_git.t
t/utf8.eml [moved from t/utf8.mbox with 90% similarity]

index ba5cc6a4e8ac2debfabc2cfd2cada3dc32317ad3..92cda5d8fa94b64190088a91c1f6883ad3affa2b 100644 (file)
--- a/MANIFEST
+++ b/MANIFEST
@@ -253,7 +253,7 @@ t/index-git-times.t
 t/indexlevels-mirror-v1.t
 t/indexlevels-mirror.t
 t/init.t
-t/iso-2202-jp.mbox
+t/iso-2202-jp.eml
 t/linkify.t
 t/main-bin/spamc
 t/mda.t
@@ -294,7 +294,7 @@ t/spamcheck_spamc.t
 t/spawn.t
 t/thread-cycle.t
 t/time.t
-t/utf8.mbox
+t/utf8.eml
 t/v1-add-remove-add.t
 t/v1reindex.t
 t/v2-add-remove-add.t
index f2ba21fcbdfa02051d1a5d8677a81279942978f5..31aa76c627b8897c891cfd186fb888868a4aed17 100644 (file)
@@ -111,7 +111,7 @@ sub is_maildir_path ($) {
        (is_maildir_basename($p[-1]) && -f $path) ? 1 : 0;
 }
 
-sub maildir_path_load ($) {
+sub mime_from_path ($) {
        my ($path) = @_;
        if (open my $fh, '<', $path) {
                local $/;
@@ -138,7 +138,7 @@ sub import_maildir {
                opendir my $dh, "$dir/$sub" or die "opendir $dir/$sub: $!\n";
                while (defined(my $fn = readdir($dh))) {
                        next unless is_maildir_basename($fn);
-                       my $mime = maildir_path_load("$dir/$fn") or next;
+                       my $mime = mime_from_path("$dir/$fn") or next;
 
                        if (my $filter = $self->filter($im)) {
                                my $ret = $filter->scrub($mime) or return;
index e2024640c5e6182767afc45df9b2653952f04132..bea2ed2aac47b82b91a42c23cdd5b2ef2b05d893 100644 (file)
@@ -11,7 +11,7 @@ use PublicInbox::InboxWritable;
 use File::Temp 0.19 (); # 0.19 for ->newdir
 use PublicInbox::Filter::Base qw(REJECT);
 use PublicInbox::Spamcheck;
-*maildir_path_load = *PublicInbox::InboxWritable::maildir_path_load;
+*mime_from_path = \&PublicInbox::InboxWritable::mime_from_path;
 
 sub new {
        my ($class, $config) = @_;
@@ -123,7 +123,7 @@ sub _remove_spam {
        my ($self, $path) = @_;
        # path must be marked as (S)een
        $path =~ /:2,[A-R]*S[T-Za-z]*\z/ or return;
-       my $mime = maildir_path_load($path) or return;
+       my $mime = mime_from_path($path) or return;
        $self->{config}->each_inbox(sub {
                my ($ibx) = @_;
                eval {
@@ -165,7 +165,7 @@ sub _try_path {
                $warn_cb->(@_);
        };
        foreach my $ibx (@$inboxes) {
-               my $mime = maildir_path_load($path) or next;
+               my $mime = mime_from_path($path) or next;
                my $im = _importer_for($self, $ibx);
 
                # any header match means it's eligible for the inbox:
index ae5d82893e281b5e2f79ef0d266a0221e0d29721..28b1b5e827a38abc9069a00e8df6db23651d6689 100755 (executable)
@@ -92,9 +92,8 @@ Multiple messages with different content found matching
                warn "Will edit all of them\n";
        }
 } else {
-       open my $fh, '<', $file or die "open($file) failed: $!";
-       my $orig = do { local $/; <$fh> };
-       my $mime = PublicInbox::MIME->new(\$orig);
+       my $mime = PublicInbox::InboxWritable::mime_from_path($file) or
+               die "open($file) failed: $!";
        my $mids = mids($mime->header_obj);
        find_mid($found, $_, \@ibxs) for (@$mids); # populates $found
        my $cid = content_id($mime);
similarity index 84%
rename from t/iso-2202-jp.mbox
rename to t/iso-2202-jp.eml
index 1a8e19740f9106c767077b2c82085f66dcd5e271..9e0bbad4b99cf547c842316f8ff44a6c928c5eff 100644 (file)
@@ -1,4 +1,3 @@
-From historical@ruby-dev Thu Jan  1 00:00:00 1970
 Message-Id: <199707281508.AAA24167@hoyogw.example>
 Date: Tue, 29 Jul 97 00:08:29 +0900
 From: matz@example.com
diff --git a/t/mda.t b/t/mda.t
index ddc0c27975288fd66d639246da892c6eece94096..ec09cf69bb6a6e1a229b19aed2e15f3449d781d0 100644 (file)
--- a/t/mda.t
+++ b/t/mda.t
@@ -7,6 +7,7 @@ use Email::MIME;
 use Cwd qw(getcwd);
 use PublicInbox::MID qw(mid2path);
 use PublicInbox::Git;
+use PublicInbox::InboxWritable;
 use PublicInbox::TestCommon;
 my ($tmpdir, $for_destroy) = tmpdir();
 my $home = "$tmpdir/pi-home";
@@ -62,12 +63,9 @@ local $ENV{GIT_COMMITTER_NAME} = eval {
        use PublicInbox::MDA;
        use PublicInbox::Address;
        use Encode qw/encode/;
-       my $mbox = 't/utf8.mbox';
-       open(my $fh, '<', $mbox) or die "failed to open mbox: $mbox\n";
-       my $str = eval { local $/; <$fh> };
-       close $fh;
-       my $msg = Email::MIME->new($str);
-
+       my $eml = 't/utf8.eml';
+       my $msg = PublicInbox::InboxWritable::mime_from_path($eml) or
+               die "failed to open $eml: $!";
        my $from = $msg->header('From');
        my ($author) = PublicInbox::Address::names($from);
        my ($email) = PublicInbox::Address::emails($from);
index d303564fd0eaa321308fc8993fb8a782a5234f2f..573ee41209ba86742145c9d74244e3822dcab60e 100644 (file)
@@ -5,6 +5,7 @@ use warnings;
 use Test::More;
 use Email::MIME;
 use PublicInbox::Hval qw(ascii_html);
+use PublicInbox::InboxWritable;
 use_ok('PublicInbox::MsgIter');
 
 {
@@ -42,12 +43,9 @@ use_ok('PublicInbox::MsgIter');
 }
 
 {
-       my $f = 't/iso-2202-jp.mbox';
-       my $mime = Email::MIME->new(do {
-               open my $fh, '<', $f or die "open($f): $!";
-               local $/;
-               <$fh>;
-       });
+       my $f = 't/iso-2202-jp.eml';
+       my $mime = PublicInbox::InboxWritable::mime_from_path($f) or
+               die "open $f: $!";
        my $raw = '';
        msg_iter($mime, sub {
                my ($part, $level, @ex) = @{$_[0]};
@@ -61,12 +59,8 @@ use_ok('PublicInbox::MsgIter');
 
 {
        my $f = 't/x-unknown-alpine.eml';
-       my $mime = Email::MIME->new(do {
-               open my $fh, '<', $f or die "open($f): $!";
-               local $/;
-               binmode $fh;
-               <$fh>;
-       });
+       my $mime = PublicInbox::InboxWritable::mime_from_path($f) or
+               die "open $f: $!";
        my $raw = '';
        msg_iter($mime, sub {
                my ($part, $level, @ex) = @{$_[0]};
index 0714631dcb1cc001d8da4774e0a4af275f352795..a0522e1f64d9da0ef916d90d899d3b50a177c1cf 100644 (file)
@@ -63,11 +63,9 @@ EOF
 
 {
        my $im = $ibx->importer(0);
-       my $mime = PublicInbox::MIME->new(do {
-               open my $fh, '<', 't/data/0001.patch' or die;
-               local $/;
-               <$fh>
-       });
+       my $eml = 't/data/0001.patch';
+       my $mime = PublicInbox::InboxWritable::mime_from_path($eml) or
+               die "open $eml: $!";
        ok($im->add($mime), 'message added');
        $im->done;
        if ($version == 1) {
index 839a320ae6c63c0fb0ce43e6bac17dd6b58f3530..101d44e9cb769584b07ebe30588d2b734ce7c557 100644 (file)
@@ -7,6 +7,7 @@ use PublicInbox::TestCommon;
 require_mods(qw(DBD::SQLite Search::Xapian));
 require PublicInbox::SearchIdx;
 require PublicInbox::Inbox;
+require PublicInbox::InboxWritable;
 use Email::MIME;
 my ($tmpdir, $for_destroy) = tmpdir();
 my $git_dir = "$tmpdir/a.git";
@@ -290,14 +291,9 @@ $ibx->with_umask(sub {
 });
 
 $ibx->with_umask(sub {
-       my $str = eval {
-               my $mbox = 't/utf8.mbox';
-               open(my $fh, '<', $mbox) or die "failed to open mbox: $mbox\n";
-               local $/;
-               <$fh>
-       };
-       $str =~ s/\AFrom [^\n]+\n//s;
-       my $mime = Email::MIME->new($str);
+       my $eml = 't/utf8.eml';
+       my $mime = PublicInbox::InboxWritable::mime_from_path($eml) or
+               die "open $eml: $!";
        my $doc_id = $rw->add_message($mime);
        ok($doc_id > 0, 'message indexed doc_id with UTF-8');
        my $msg = $rw->query('m:testmessage@example.com', {limit => 1})->[0];
index 2dbb07b0cdef33cbe0c120e7c849dd2596f6cb61..7f0cd999344dc52da63d00c86a4b1d2d27a48c46 100644 (file)
@@ -28,8 +28,8 @@ my $im = PublicInbox::V2Writable->new($ibx, 1);
 $im->{parallel} = 0;
 
 my $deliver_patch = sub ($) {
-       open my $fh, '<', $_[0] or die "open: $!";
-       my $mime = PublicInbox::MIME->new(do { local $/; <$fh> });
+       my $mime = PublicInbox::InboxWritable::mime_from_path($_[0]) or
+               die "open $_[0]: $!";
        $im->add($mime);
        $im->done;
 };
similarity index 90%
rename from t/utf8.mbox
rename to t/utf8.eml
index cebaf9b0492771d30462d58896a9197f17bf242f..9bf1002c49eb075df47247b74d69bcd555e23422 100644 (file)
@@ -1,4 +1,3 @@
-From e@yhbt.net Thu Jan 01 00:00:00 1970
 Date: Thu, 01 Jan 1970 00:00:00 +0000
 To: =?utf-8?Q?El=C3=A9anor?= <e@example.com>
 From: =?utf-8?Q?El=C3=A9anor?= <e@example.com>