* [PATCH 19/30] test_common: use autodie and read_all where possible
2023-10-17 23:37 7% [PATCH 00/30] autodie-ification and code simplifications Eric Wong
@ 2023-10-17 23:38 4% ` Eric Wong
0 siblings, 0 replies; 2+ results
From: Eric Wong @ 2023-10-17 23:38 UTC (permalink / raw)
To: meta
Noise reduction to help my aging eyes.
---
lib/PublicInbox/TestCommon.pm | 73 ++++++++++++++++-------------------
1 file changed, 34 insertions(+), 39 deletions(-)
diff --git a/lib/PublicInbox/TestCommon.pm b/lib/PublicInbox/TestCommon.pm
index 77da822b..3839ab45 100644
--- a/lib/PublicInbox/TestCommon.pm
+++ b/lib/PublicInbox/TestCommon.pm
@@ -16,6 +16,7 @@ our @EXPORT;
my $lei_loud = $ENV{TEST_LEI_ERR_LOUD};
my $tail_cmd = $ENV{TAIL};
our ($lei_opt, $lei_out, $lei_err, $lei_cwdfh);
+use autodie qw(chdir close fcntl open opendir seek unlink);
$_ = File::Spec->rel2abs($_) for (grep(!m!^/!, @INC));
@@ -46,11 +47,16 @@ sub require_bsd (;$) {
sub xbail (@) { BAIL_OUT join(' ', map { ref() ? (explain($_)) : ($_) } @_) }
+sub read_all ($;$$) {
+ require PublicInbox::Git;
+ PublicInbox::Git::read_all($_[0], $_[1], $_[2])
+}
+
sub eml_load ($) {
my ($path, $cb) = @_;
- open(my $fh, '<', $path) or die "open $path: $!";
+ open(my $fh, '<', $path);
require PublicInbox::Eml;
- PublicInbox::Eml->new(\(do { local $/; <$fh> }));
+ PublicInbox::Eml->new(\(read_all($fh)));
}
sub tmpdir (;$) {
@@ -243,9 +249,9 @@ sub _prepare_redirects ($) {
for (my $fd = 0; $fd <= $#io_mode; $fd++) {
my $fh = $fhref->[$fd] or next;
my ($oldfh, $mode) = @{$io_mode[$fd]};
- open my $orig, $mode, $oldfh or die "$oldfh $mode stash: $!";
+ open(my $orig, $mode, $oldfh);
$orig_io->[$fd] = $orig;
- open $oldfh, $mode, $fh or die "$oldfh $mode redirect: $!";
+ open $oldfh, $mode, $fh;
}
$orig_io;
}
@@ -255,7 +261,7 @@ sub _undo_redirects ($) {
for (my $fd = 0; $fd <= $#io_mode; $fd++) {
my $fh = $orig_io->[$fd] or next;
my ($oldfh, $mode) = @{$io_mode[$fd]};
- open $oldfh, $mode, $fh or die "$$oldfh $mode redirect: $!";
+ open $oldfh, $mode, $fh;
}
}
@@ -281,8 +287,8 @@ sub key2sub ($) {
my ($key) = @_;
$cached_scripts{$key} //= do {
my $f = key2script($key);
- open my $fh, '<', $f or die "open $f: $!";
- my $str = do { local $/; <$fh> };
+ open my $fh, '<', $f;
+ my $str = read_all($fh);
my $pkg = (split(m!/!, $f))[-1];
$pkg =~ s/([a-z])([a-z0-9]+)(\.t)?\z/\U$1\E$2/;
$pkg .= "_T" if $3;
@@ -329,7 +335,7 @@ sub _run_sub ($$$) {
sub no_coredump (@) {
my @dirs = @_;
my $cwdfh;
- if (@dirs) { opendir($cwdfh, '.') or die "opendir(.): $!" }
+ opendir($cwdfh, '.') if @dirs;
my @found;
for (@dirs, '.') {
chdir $_;
@@ -375,7 +381,7 @@ sub run_script ($;$$) {
next if $fd > 0;
$fh->autoflush(1);
print $fh $$redir or die "print: $!";
- seek($fh, 0, SEEK_SET) or die "seek: $!";
+ seek($fh, 0, SEEK_SET);
} elsif ($ref eq 'GLOB') {
$spawn_opt->{$fd} = $fhref->[$fd] = $redir;
} elsif ($ref) {
@@ -406,15 +412,13 @@ sub run_script ($;$$) {
my $orig_io = _prepare_redirects($fhref);
my $cwdfh = $lei_cwdfh;
if (my $d = $opt->{'-C'}) {
- unless ($cwdfh) {
- opendir $cwdfh, '.' or die "opendir .: $!";
- }
- chdir $d or die "chdir $d: $!";
+ $cwdfh or opendir $cwdfh, '.';
+ chdir $d;
}
_run_sub($sub, $key, \@argv);
# n.b. all our uses of PublicInbox::DS should be fine
# with this and we can't Reset here.
- die "fchdir(restore): $!" if $cwdfh && !chdir($cwdfh);
+ chdir($cwdfh) if $cwdfh;
_undo_redirects($orig_io);
select STDOUT;
umask($umask);
@@ -425,10 +429,8 @@ sub run_script ($;$$) {
for my $fd (1..2) {
my $fh = $fhref->[$fd] or next;
next unless -f $fh;
- seek($fh, 0, SEEK_SET) or die "seek: $!";
- my $redir = $opt->{$fd};
- local $/;
- $$redir = <$fh>;
+ seek($fh, 0, SEEK_SET);
+ ${$opt->{$fd}} = read_all($fh);
}
no_coredump($opt->{-C} ? ($opt->{-C}) : ());
$? == 0;
@@ -458,7 +460,7 @@ sub wait_for_tail {
$ino[0] =~ s!/fd/!/fdinfo/!;
my @info;
do {
- if (open my $fh, '<', $ino[0]) {
+ if (CORE::open(my $fh, '<', $ino[0])) {
local $/ = "\n";
@info = grep(/^inotify wd:/, <$fh>);
}
@@ -500,7 +502,6 @@ sub tail_f (@) {
$tail_cmd or return; # "tail -F" or "tail -f"
my $opt = (ref($f[-1]) eq 'HASH') ? pop(@f) : {};
my $clofork = $opt->{-CLOFORK} // [];
- use autodie qw(fcntl open);
my @cfmap = map {
my $fl = fcntl($_, F_GETFD, 0);
fcntl($_, F_SETFD, $fl | FD_CLOEXEC) unless $fl & FD_CLOEXEC;
@@ -551,9 +552,7 @@ sub start_script {
\&PublicInbox::DS::sig_setmask, $oset);
my $pid = PublicInbox::DS::do_fork();
if ($pid == 0) {
- for (@{delete($opt->{-CLOFORK}) // []}) {
- close($_) or die "close $!";
- }
+ close($_) for (@{delete($opt->{-CLOFORK}) // []});
# pretend to be systemd (cf. sd_listen_fds(3))
# 3 == SD_LISTEN_FDS_START
my $fd;
@@ -561,7 +560,7 @@ sub start_script {
my $io = $opt->{$fd} // next;
my $old = fileno($io);
if ($old == $fd) {
- fcntl($io, F_SETFD, 0) // die "F_SETFD: $!";
+ fcntl($io, F_SETFD, 0);
} else {
dup2($old, $fd) // die "dup2($old, $fd): $!";
}
@@ -572,7 +571,7 @@ sub start_script {
$ENV{LISTEN_PID} = $$;
$ENV{LISTEN_FDS} = $fds;
}
- if ($opt->{-C}) { chdir($opt->{-C}) or die "chdir: $!" }
+ if ($opt->{-C}) { chdir($opt->{-C}) }
$0 = join(' ', @$cmd);
local @SIG{keys %SIG} = map { undef } values %SIG;
local $SIG{FPE} = 'IGNORE'; # Perl default
@@ -657,7 +656,6 @@ sub need_scm_rights () {
# returns a pipe with FD_CLOEXEC disabled on the write-end
sub quit_waiter_pipe () {
- use autodie qw(fcntl pipe);
pipe(my $r, my $w);
fcntl($w, F_SETFD, fcntl($w, F_GETFD, 0) & ~FD_CLOEXEC);
($r, $w);
@@ -675,7 +673,7 @@ SKIP: {
my ($cb) = pop @_;
my $test_opt = shift // {};
local $lei_cwdfh;
- use autodie qw(mkdir open opendir);
+ use autodie qw(mkdir);
opendir $lei_cwdfh, '.';
require_git(2.6, 1);
my $mods = $test_opt->{mods} // [ 'lei' ];
@@ -766,7 +764,7 @@ sub setup_public_inboxes () {
'--newsgroup', "t.v$V", "t$V",
"$test_home/t$V", "http://example.com/t$V",
"t$V\@example.com" ]) or xbail "init v$V";
- unlink "$test_home/t$V/description" or xbail "unlink $!";
+ unlink "$test_home/t$V/description";
}
require PublicInbox::Config;
require PublicInbox::InboxWritable;
@@ -786,7 +784,7 @@ sub setup_public_inboxes () {
$im->done;
});
$seen or BAIL_OUT 'no imports';
- open my $fh, '>', $stamp or BAIL_OUT "open $stamp: $!";
+ open my $fh, '>', $stamp;
@ret;
}
@@ -815,13 +813,12 @@ sub create_coderepo ($$;@) {
my $scope = $lk->lock_for_scope;
my $tmpdir = delete $opt{tmpdir};
if (!-f "$dir/creat.stamp") {
- opendir(my $dfh, '.') or xbail "opendir .: $!";
- chdir($dir) or xbail "chdir($dir): $!";
+ opendir(my $dfh, '.');
+ chdir($dir);
local %ENV = (%ENV, %COMMIT_ENV);
$cb->($dir);
- chdir($dfh) or xbail "cd -: $!";
- open my $s, '>', "$dir/creat.stamp" or
- BAIL_OUT "error creating $dir/creat.stamp: $!";
+ chdir($dfh);
+ open my $s, '>', "$dir/creat.stamp";
}
return $dir if !defined($tmpdir);
xsys_e([qw(/bin/cp -Rp), $dir, $tmpdir]);
@@ -868,8 +865,7 @@ sub create_inbox ($$;@) {
xsys_e([ qw(git gc -q) ], { GIT_DIR => $dir });
}
}
- open my $s, '>', "$dir/creat.stamp" or
- BAIL_OUT "error creating $dir/creat.stamp: $!";
+ open my $s, '>', "$dir/creat.stamp";
}
if ($tmpdir) {
undef $ibx;
@@ -904,8 +900,8 @@ sub test_httpd ($$;$$) {
ua => $ua);
$cb->() if $cb;
$td->join('TERM');
- open my $fh, '<', $err or BAIL_OUT $!;
- my $e = do { local $/; <$fh> };
+ open my $fh, '<', $err;
+ my $e = read_all($fh);
if ($e =~ s/^Plack::Middleware::ReverseProxy missing,\n//gms) {
$e =~ s/^URL generation for redirects .*\n//gms;
}
@@ -934,7 +930,6 @@ sub no_pollerfd ($) {
sub cfg_new ($;@) {
my ($tmpdir, @body) = @_;
- use autodie;
require PublicInbox::Config;
my $f = "$tmpdir/tmp_cfg";
open my $fh, '>', $f;
^ permalink raw reply related [relevance 4%]
* [PATCH 00/30] autodie-ification and code simplifications
@ 2023-10-17 23:37 7% Eric Wong
2023-10-17 23:38 4% ` [PATCH 19/30] test_common: use autodie and read_all where possible Eric Wong
0 siblings, 1 reply; 2+ results
From: Eric Wong @ 2023-10-17 23:37 UTC (permalink / raw)
To: meta
Noisy code is less pleasant to work on, so use autodie more and
a few more simplifications. There's a couple of small bugfixes
discovered along the way, too.
Eric Wong (30):
lei_mirror: start converting to autodie
lei_mirror: autodie most `close' calls
lei_mirror: use autodie for most `open' calls
git: introduce read_all function
import: use read_all to detect short reads
lei_mirror: use read_all
use read_all in more places to improve safety
xap_helper*: use autodie in more places
xap_helper: die more easily in both implementations
xap_helper: simplify SIGTERM exit checks
xap_helper: autodie for getsockopt
xap_client: autodie for pipe and socketpair
xt/git-http-backend: remove Net::HTTP usage
ds: introduce and use do_fork helper
ds: get rid of SetLoopTimeout
cindex: drop some unused functions
syscall: common $F_SETPIPE_SZ definition
t/lei-up: additional diagnostics for match failures
test_common: use autodie and read_all where possible
test_common: only hide TCP port in messages
test_common: use $cwdfh for every run_script command
init: drop extraneous `+'
init: use autodie to reduce distractions
xt/mem-imapd-tls: remove unused/broken epoll imports
xt/mem-imapd-tls: reduce FDs for lsof use
lei: use autodie where appropriate
lei_auth: update comments and use v5.12
lei_config: drop redundant open check
convert: use read_all to simplify error checks
idx_stack: use autodie + read_all
lib/PublicInbox/CidxLogP.pm | 4 +-
lib/PublicInbox/CodeSearchIdx.pm | 5 --
lib/PublicInbox/DS.pm | 36 ++++----
lib/PublicInbox/Daemon.pm | 16 ++--
lib/PublicInbox/EOFpipe.pm | 6 +-
lib/PublicInbox/Gcf2.pm | 7 +-
lib/PublicInbox/Git.pm | 19 +++--
lib/PublicInbox/IPC.pm | 12 +--
lib/PublicInbox/IdxStack.pm | 20 ++---
lib/PublicInbox/Import.pm | 8 +-
lib/PublicInbox/InboxWritable.pm | 6 +-
lib/PublicInbox/LEI.pm | 48 +++++------
lib/PublicInbox/LeiALE.pm | 11 +--
lib/PublicInbox/LeiAuth.pm | 7 +-
lib/PublicInbox/LeiBlob.pm | 6 +-
lib/PublicInbox/LeiConfig.pm | 4 +-
lib/PublicInbox/LeiMailSync.pm | 5 +-
lib/PublicInbox/LeiMirror.pm | 131 ++++++++++++++----------------
lib/PublicInbox/LeiSucks.pm | 5 +-
lib/PublicInbox/LeiXSearch.pm | 2 +-
lib/PublicInbox/MultiGit.pm | 3 +-
lib/PublicInbox/SearchIdxShard.pm | 14 ++--
lib/PublicInbox/Syscall.pm | 16 ++--
lib/PublicInbox/TestCommon.pm | 85 +++++++++----------
lib/PublicInbox/ViewVCS.pm | 12 ++-
lib/PublicInbox/WWW.pm | 4 +-
lib/PublicInbox/Watch.pm | 11 +--
lib/PublicInbox/XapClient.pm | 11 +--
lib/PublicInbox/XapHelper.pm | 24 ++----
lib/PublicInbox/XapHelperCxx.pm | 11 +--
lib/PublicInbox/Xapcmd.pm | 5 +-
lib/PublicInbox/xap_helper.h | 60 ++++++--------
script/public-inbox-convert | 8 +-
script/public-inbox-edit | 4 +-
script/public-inbox-init | 30 +++----
t/dir_idle.t | 2 +-
t/ds-leak.t | 4 +-
t/gcf2.t | 5 +-
t/init.t | 7 ++
t/lei-sigpipe.t | 7 +-
t/lei-up.t | 4 +-
xt/git-http-backend.t | 30 +++----
xt/mem-imapd-tls.t | 21 ++---
xt/mem-nntpd-tls.t | 8 +-
44 files changed, 335 insertions(+), 409 deletions(-)
^ permalink raw reply [relevance 7%]
Results 1-2 of 2 | reverse | options above
-- pct% links below jump to the message on this page, permalinks otherwise --
2023-10-17 23:37 7% [PATCH 00/30] autodie-ification and code simplifications Eric Wong
2023-10-17 23:38 4% ` [PATCH 19/30] test_common: use autodie and read_all where possible Eric Wong
Code repositories for project(s) associated with this public inbox
https://80x24.org/public-inbox.git
This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox;
as well as URLs for read-only IMAP folder(s) and NNTP newsgroup(s).