diff options
author | Eric Wong <e@yhbt.net> | 2020-04-25 05:52:21 +0000 |
---|---|---|
committer | Eric Wong <e@yhbt.net> | 2020-04-26 07:03:12 +0000 |
commit | 5d8dbb4aca5afcc6b98a4d951f03003fb7eaf356 (patch) | |
tree | b26c754dd2822193f4fb6e96e8cad8e660cbf246 /lib | |
parent | 58852a50f6a72265cce1e4729aae3875bba5aa96 (diff) | |
download | public-inbox-5d8dbb4aca5afcc6b98a4d951f03003fb7eaf356.tar.gz |
We'll use this to create, memoize, and reuse .eml files. This will be used to reduce (and eventually eliminate) our dependency on Email::MIME in tests.
Diffstat (limited to 'lib')
-rw-r--r-- | lib/PublicInbox/TestCommon.pm | 25 |
1 files changed, 24 insertions, 1 deletions
diff --git a/lib/PublicInbox/TestCommon.pm b/lib/PublicInbox/TestCommon.pm index b50871e8..ac14d27b 100644 --- a/lib/PublicInbox/TestCommon.pm +++ b/lib/PublicInbox/TestCommon.pm @@ -9,7 +9,30 @@ use Fcntl qw(FD_CLOEXEC F_SETFD F_GETFD :seek); use POSIX qw(dup2); use IO::Socket::INET; our @EXPORT = qw(tmpdir tcp_server tcp_connect require_git require_mods - run_script start_script key2sub xsys xqx); + run_script start_script key2sub xsys xqx mime_load); + +sub mime_load ($;&) { + my ($path, $cb) = @_; + if (open(my $fh, '<', $path)) { + PublicInbox::MIME->new(\(do { local $/; <$fh> })); + } elsif ($cb) { + require File::Temp; + + my $mime = $cb->(); + my ($dir) = ($path =~ m!(.+)/(?:[^/]+)\z!); + -d $dir or die "BUG: dir=$dir is not the dir of $path"; + my $fh = File::Temp->new(DIR => $dir); + $fh->autoflush(1); + print $fh $mime->as_string or die "print: $!"; + my $fn = $fh->filename; + rename($fn, $path) or die "link $fn => $path: $!"; + $fh->unlink_on_destroy(0); + pop @_; # retry via tail recursion + goto &mime_load; + } else { + die "open $path: $!"; + } +} sub tmpdir (;$) { my ($base) = @_; |